Chris Lattner | a3b8b5c | 2004-07-23 17:56:30 +0000 | [diff] [blame] | 1 | //===-- LiveIntervalAnalysis.cpp - Live Interval Analysis -----------------===// |
Alkis Evlogimenos | ff0cbe1 | 2003-11-20 03:32:25 +0000 | [diff] [blame] | 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
Chris Lattner | 4ee451d | 2007-12-29 20:36:04 +0000 | [diff] [blame] | 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
Alkis Evlogimenos | ff0cbe1 | 2003-11-20 03:32:25 +0000 | [diff] [blame] | 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 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 | 3c3fe46 | 2005-09-21 04:19:09 +0000 | [diff] [blame] | 18 | #include "llvm/CodeGen/LiveIntervalAnalysis.h" |
Chandler Carruth | d04a8d4 | 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 | 6d69ba8 | 2008-07-25 00:02:30 +0000 | [diff] [blame] | 22 | #include "llvm/Analysis/AliasAnalysis.h" |
Alkis Evlogimenos | ff0cbe1 | 2003-11-20 03:32:25 +0000 | [diff] [blame] | 23 | #include "llvm/CodeGen/LiveVariables.h" |
Stephen Hines | 36b5688 | 2014-04-23 16:57:46 -0700 | [diff] [blame] | 24 | #include "llvm/CodeGen/MachineBlockFrequencyInfo.h" |
Jakob Stoklund Olesen | 34c6f98 | 2012-06-05 22:02:15 +0000 | [diff] [blame] | 25 | #include "llvm/CodeGen/MachineDominators.h" |
Alkis Evlogimenos | ff0cbe1 | 2003-11-20 03:32:25 +0000 | [diff] [blame] | 26 | #include "llvm/CodeGen/MachineInstr.h" |
Chris Lattner | 84bc542 | 2007-12-31 04:13:23 +0000 | [diff] [blame] | 27 | #include "llvm/CodeGen/MachineRegisterInfo.h" |
Alkis Evlogimenos | ff0cbe1 | 2003-11-20 03:32:25 +0000 | [diff] [blame] | 28 | #include "llvm/CodeGen/Passes.h" |
Jakob Stoklund Olesen | 1ead68d | 2012-11-28 19:13:06 +0000 | [diff] [blame] | 29 | #include "llvm/CodeGen/VirtRegMap.h" |
Chandler Carruth | 0b8c9a8 | 2013-01-02 11:36:10 +0000 | [diff] [blame] | 30 | #include "llvm/IR/Value.h" |
Benjamin Kramer | 4eed756 | 2013-06-17 19:00:36 +0000 | [diff] [blame] | 31 | #include "llvm/Support/BlockFrequency.h" |
Jakob Stoklund Olesen | 3dfa38a | 2012-07-27 20:58:46 +0000 | [diff] [blame] | 32 | #include "llvm/Support/CommandLine.h" |
Reid Spencer | 551ccae | 2004-09-01 22:55:40 +0000 | [diff] [blame] | 33 | #include "llvm/Support/Debug.h" |
Torok Edwin | 7d696d8 | 2009-07-11 13:10:19 +0000 | [diff] [blame] | 34 | #include "llvm/Support/ErrorHandling.h" |
| 35 | #include "llvm/Support/raw_ostream.h" |
Chandler Carruth | d04a8d4 | 2012-12-03 16:50:05 +0000 | [diff] [blame] | 36 | #include "llvm/Target/TargetInstrInfo.h" |
| 37 | #include "llvm/Target/TargetMachine.h" |
| 38 | #include "llvm/Target/TargetRegisterInfo.h" |
Alkis Evlogimenos | 20aa474 | 2004-09-03 18:19:51 +0000 | [diff] [blame] | 39 | #include <algorithm> |
Jeff Cohen | 97af751 | 2006-12-02 02:22:01 +0000 | [diff] [blame] | 40 | #include <cmath> |
Chandler Carruth | d04a8d4 | 2012-12-03 16:50:05 +0000 | [diff] [blame] | 41 | #include <limits> |
Alkis Evlogimenos | ff0cbe1 | 2003-11-20 03:32:25 +0000 | [diff] [blame] | 42 | using namespace llvm; |
| 43 | |
Stephen Hines | dce4a40 | 2014-05-29 02:49:00 -0700 | [diff] [blame^] | 44 | #define DEBUG_TYPE "regalloc" |
| 45 | |
Devang Patel | 1997473 | 2007-05-03 01:11:54 +0000 | [diff] [blame] | 46 | char LiveIntervals::ID = 0; |
Jakob Stoklund Olesen | dcc4436 | 2012-08-03 22:12:54 +0000 | [diff] [blame] | 47 | char &llvm::LiveIntervalsID = LiveIntervals::ID; |
Owen Anderson | 2ab36d3 | 2010-10-12 19:48:12 +0000 | [diff] [blame] | 48 | INITIALIZE_PASS_BEGIN(LiveIntervals, "liveintervals", |
| 49 | "Live Interval Analysis", false, false) |
Andrew Trick | 8dd2625 | 2012-02-10 04:10:36 +0000 | [diff] [blame] | 50 | INITIALIZE_AG_DEPENDENCY(AliasAnalysis) |
Owen Anderson | 2ab36d3 | 2010-10-12 19:48:12 +0000 | [diff] [blame] | 51 | INITIALIZE_PASS_DEPENDENCY(LiveVariables) |
Andrew Trick | 8dd2625 | 2012-02-10 04:10:36 +0000 | [diff] [blame] | 52 | INITIALIZE_PASS_DEPENDENCY(MachineDominatorTree) |
Owen Anderson | 2ab36d3 | 2010-10-12 19:48:12 +0000 | [diff] [blame] | 53 | INITIALIZE_PASS_DEPENDENCY(SlotIndexes) |
Owen Anderson | 2ab36d3 | 2010-10-12 19:48:12 +0000 | [diff] [blame] | 54 | INITIALIZE_PASS_END(LiveIntervals, "liveintervals", |
Owen Anderson | ce665bd | 2010-10-07 22:25:06 +0000 | [diff] [blame] | 55 | "Live Interval Analysis", false, false) |
Alkis Evlogimenos | ff0cbe1 | 2003-11-20 03:32:25 +0000 | [diff] [blame] | 56 | |
Andrew Trick | c6bae79 | 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 | |
Chris Lattner | f7da2c7 | 2006-08-24 22:43:55 +0000 | [diff] [blame] | 65 | void LiveIntervals::getAnalysisUsage(AnalysisUsage &AU) const { |
Dan Gohman | 845012e | 2009-07-31 23:37:33 +0000 | [diff] [blame] | 66 | AU.setPreservesCFG(); |
Dan Gohman | 6d69ba8 | 2008-07-25 00:02:30 +0000 | [diff] [blame] | 67 | AU.addRequired<AliasAnalysis>(); |
| 68 | AU.addPreserved<AliasAnalysis>(); |
Jakob Stoklund Olesen | ec7b25d | 2013-02-09 00:04:07 +0000 | [diff] [blame] | 69 | // LiveVariables isn't really required by this analysis, it is only required |
| 70 | // here to make sure it is live during TwoAddressInstructionPass and |
| 71 | // PHIElimination. This is temporary. |
Alkis Evlogimenos | 1a8ea01 | 2004-08-04 09:46:26 +0000 | [diff] [blame] | 72 | AU.addRequired<LiveVariables>(); |
Evan Cheng | 148341c | 2010-08-17 21:00:37 +0000 | [diff] [blame] | 73 | AU.addPreserved<LiveVariables>(); |
Andrew Trick | d35576b | 2012-02-13 20:44:42 +0000 | [diff] [blame] | 74 | AU.addPreservedID(MachineLoopInfoID); |
Jakob Stoklund Olesen | c411845 | 2012-06-20 23:31:34 +0000 | [diff] [blame] | 75 | AU.addRequiredTransitiveID(MachineDominatorsID); |
Bill Wendling | 67d65bb | 2008-01-04 20:54:55 +0000 | [diff] [blame] | 76 | AU.addPreservedID(MachineDominatorsID); |
Lang Hames | 233a60e | 2009-11-03 23:52:08 +0000 | [diff] [blame] | 77 | AU.addPreserved<SlotIndexes>(); |
| 78 | AU.addRequiredTransitive<SlotIndexes>(); |
Alkis Evlogimenos | 1a8ea01 | 2004-08-04 09:46:26 +0000 | [diff] [blame] | 79 | MachineFunctionPass::getAnalysisUsage(AU); |
Alkis Evlogimenos | ff0cbe1 | 2003-11-20 03:32:25 +0000 | [diff] [blame] | 80 | } |
| 81 | |
Jakob Stoklund Olesen | 34c6f98 | 2012-06-05 22:02:15 +0000 | [diff] [blame] | 82 | LiveIntervals::LiveIntervals() : MachineFunctionPass(ID), |
Stephen Hines | dce4a40 | 2014-05-29 02:49:00 -0700 | [diff] [blame^] | 83 | DomTree(nullptr), LRCalc(nullptr) { |
Jakob Stoklund Olesen | 34c6f98 | 2012-06-05 22:02:15 +0000 | [diff] [blame] | 84 | initializeLiveIntervalsPass(*PassRegistry::getPassRegistry()); |
| 85 | } |
| 86 | |
| 87 | LiveIntervals::~LiveIntervals() { |
| 88 | delete LRCalc; |
| 89 | } |
| 90 | |
Chris Lattner | f7da2c7 | 2006-08-24 22:43:55 +0000 | [diff] [blame] | 91 | void LiveIntervals::releaseMemory() { |
Owen Anderson | 03857b2 | 2008-08-13 21:49:13 +0000 | [diff] [blame] | 92 | // Free the live intervals themselves. |
Jakob Stoklund Olesen | 7fa6784 | 2012-06-22 20:37:52 +0000 | [diff] [blame] | 93 | for (unsigned i = 0, e = VirtRegIntervals.size(); i != e; ++i) |
| 94 | delete VirtRegIntervals[TargetRegisterInfo::index2VirtReg(i)]; |
| 95 | VirtRegIntervals.clear(); |
Jakob Stoklund Olesen | 3fd3a84 | 2012-02-08 17:33:45 +0000 | [diff] [blame] | 96 | RegMaskSlots.clear(); |
| 97 | RegMaskBits.clear(); |
Jakob Stoklund Olesen | 34e85d0 | 2012-02-10 01:26:29 +0000 | [diff] [blame] | 98 | RegMaskBlocks.clear(); |
Lang Hames | ffd1326 | 2009-07-09 03:57:02 +0000 | [diff] [blame] | 99 | |
Matthias Braun | 4f3b5e8 | 2013-10-10 21:29:02 +0000 | [diff] [blame] | 100 | for (unsigned i = 0, e = RegUnitRanges.size(); i != e; ++i) |
| 101 | delete RegUnitRanges[i]; |
| 102 | RegUnitRanges.clear(); |
Jakob Stoklund Olesen | 34c6f98 | 2012-06-05 22:02:15 +0000 | [diff] [blame] | 103 | |
Benjamin Kramer | ce9a20b | 2010-06-26 11:30:59 +0000 | [diff] [blame] | 104 | // Release VNInfo memory regions, VNInfo objects don't need to be dtor'd. |
| 105 | VNInfoAllocator.Reset(); |
Alkis Evlogimenos | 08cec00 | 2004-01-31 19:59:32 +0000 | [diff] [blame] | 106 | } |
| 107 | |
Jakob Stoklund Olesen | 2aeef00 | 2013-08-14 17:28:46 +0000 | [diff] [blame] | 108 | /// runOnMachineFunction - calculates LiveIntervals |
Owen Anderson | 80b3ce6 | 2008-05-28 20:54:50 +0000 | [diff] [blame] | 109 | /// |
| 110 | bool LiveIntervals::runOnMachineFunction(MachineFunction &fn) { |
Jakob Stoklund Olesen | 15f1d8c | 2012-06-04 22:39:14 +0000 | [diff] [blame] | 111 | MF = &fn; |
| 112 | MRI = &MF->getRegInfo(); |
| 113 | TM = &fn.getTarget(); |
| 114 | TRI = TM->getRegisterInfo(); |
| 115 | TII = TM->getInstrInfo(); |
| 116 | AA = &getAnalysis<AliasAnalysis>(); |
Jakob Stoklund Olesen | 15f1d8c | 2012-06-04 22:39:14 +0000 | [diff] [blame] | 117 | Indexes = &getAnalysis<SlotIndexes>(); |
Jakob Stoklund Olesen | c411845 | 2012-06-20 23:31:34 +0000 | [diff] [blame] | 118 | DomTree = &getAnalysis<MachineDominatorTree>(); |
| 119 | if (!LRCalc) |
Jakob Stoklund Olesen | 34c6f98 | 2012-06-05 22:02:15 +0000 | [diff] [blame] | 120 | LRCalc = new LiveRangeCalc(); |
Owen Anderson | 80b3ce6 | 2008-05-28 20:54:50 +0000 | [diff] [blame] | 121 | |
Jakob Stoklund Olesen | 3dfa38a | 2012-07-27 20:58:46 +0000 | [diff] [blame] | 122 | // Allocate space for all virtual registers. |
| 123 | VirtRegIntervals.resize(MRI->getNumVirtRegs()); |
| 124 | |
Jakob Stoklund Olesen | ec7b25d | 2013-02-09 00:04:07 +0000 | [diff] [blame] | 125 | computeVirtRegs(); |
| 126 | computeRegMasks(); |
Jakob Stoklund Olesen | c411845 | 2012-06-20 23:31:34 +0000 | [diff] [blame] | 127 | computeLiveInRegUnits(); |
Jakob Stoklund Olesen | 34c6f98 | 2012-06-05 22:02:15 +0000 | [diff] [blame] | 128 | |
Andrew Trick | c6bae79 | 2013-06-21 18:33:23 +0000 | [diff] [blame] | 129 | if (EnablePrecomputePhysRegs) { |
| 130 | // For stress testing, precompute live ranges of all physical register |
| 131 | // units, including reserved registers. |
| 132 | for (unsigned i = 0, e = TRI->getNumRegUnits(); i != e; ++i) |
| 133 | getRegUnit(i); |
| 134 | } |
Chris Lattner | 70ca358 | 2004-09-30 15:59:17 +0000 | [diff] [blame] | 135 | DEBUG(dump()); |
Alkis Evlogimenos | 1a8ea01 | 2004-08-04 09:46:26 +0000 | [diff] [blame] | 136 | return true; |
Alkis Evlogimenos | ff0cbe1 | 2003-11-20 03:32:25 +0000 | [diff] [blame] | 137 | } |
| 138 | |
Chris Lattner | 70ca358 | 2004-09-30 15:59:17 +0000 | [diff] [blame] | 139 | /// print - Implement the dump method. |
Chris Lattner | 45cfe54 | 2009-08-23 06:03:38 +0000 | [diff] [blame] | 140 | void LiveIntervals::print(raw_ostream &OS, const Module* ) const { |
Chris Lattner | 705e07f | 2009-08-23 03:41:05 +0000 | [diff] [blame] | 141 | OS << "********** INTERVALS **********\n"; |
Jakob Stoklund Olesen | f658af5 | 2012-02-14 23:46:21 +0000 | [diff] [blame] | 142 | |
Jakob Stoklund Olesen | 34c6f98 | 2012-06-05 22:02:15 +0000 | [diff] [blame] | 143 | // Dump the regunits. |
Matthias Braun | 4f3b5e8 | 2013-10-10 21:29:02 +0000 | [diff] [blame] | 144 | for (unsigned i = 0, e = RegUnitRanges.size(); i != e; ++i) |
| 145 | if (LiveRange *LR = RegUnitRanges[i]) |
Matthias Braun | 03d9609 | 2013-10-10 21:29:05 +0000 | [diff] [blame] | 146 | OS << PrintRegUnit(i, TRI) << ' ' << *LR << '\n'; |
Jakob Stoklund Olesen | 34c6f98 | 2012-06-05 22:02:15 +0000 | [diff] [blame] | 147 | |
Jakob Stoklund Olesen | f658af5 | 2012-02-14 23:46:21 +0000 | [diff] [blame] | 148 | // Dump the virtregs. |
Jakob Stoklund Olesen | 7fa6784 | 2012-06-22 20:37:52 +0000 | [diff] [blame] | 149 | for (unsigned i = 0, e = MRI->getNumVirtRegs(); i != e; ++i) { |
| 150 | unsigned Reg = TargetRegisterInfo::index2VirtReg(i); |
| 151 | if (hasInterval(Reg)) |
Matthias Braun | 03d9609 | 2013-10-10 21:29:05 +0000 | [diff] [blame] | 152 | OS << getInterval(Reg) << '\n'; |
Jakob Stoklund Olesen | 7fa6784 | 2012-06-22 20:37:52 +0000 | [diff] [blame] | 153 | } |
Chris Lattner | 70ca358 | 2004-09-30 15:59:17 +0000 | [diff] [blame] | 154 | |
Jakob Stoklund Olesen | 722c9a7 | 2012-11-09 19:18:49 +0000 | [diff] [blame] | 155 | OS << "RegMasks:"; |
| 156 | for (unsigned i = 0, e = RegMaskSlots.size(); i != e; ++i) |
| 157 | OS << ' ' << RegMaskSlots[i]; |
| 158 | OS << '\n'; |
| 159 | |
Evan Cheng | 752195e | 2009-09-14 21:33:42 +0000 | [diff] [blame] | 160 | printInstrs(OS); |
| 161 | } |
| 162 | |
| 163 | void LiveIntervals::printInstrs(raw_ostream &OS) const { |
Chris Lattner | 705e07f | 2009-08-23 03:41:05 +0000 | [diff] [blame] | 164 | OS << "********** MACHINEINSTRS **********\n"; |
Jakob Stoklund Olesen | 15f1d8c | 2012-06-04 22:39:14 +0000 | [diff] [blame] | 165 | MF->print(OS, Indexes); |
Chris Lattner | 70ca358 | 2004-09-30 15:59:17 +0000 | [diff] [blame] | 166 | } |
| 167 | |
Manman Ren | b720be6 | 2012-09-11 22:23:19 +0000 | [diff] [blame] | 168 | #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP) |
Evan Cheng | 752195e | 2009-09-14 21:33:42 +0000 | [diff] [blame] | 169 | void LiveIntervals::dumpInstrs() const { |
David Greene | 8a34229 | 2010-01-04 22:49:02 +0000 | [diff] [blame] | 170 | printInstrs(dbgs()); |
Evan Cheng | 752195e | 2009-09-14 21:33:42 +0000 | [diff] [blame] | 171 | } |
Manman Ren | 77e300e | 2012-09-06 19:06:06 +0000 | [diff] [blame] | 172 | #endif |
Evan Cheng | 752195e | 2009-09-14 21:33:42 +0000 | [diff] [blame] | 173 | |
Owen Anderson | 03857b2 | 2008-08-13 21:49:13 +0000 | [diff] [blame] | 174 | LiveInterval* LiveIntervals::createInterval(unsigned reg) { |
Aaron Ballman | eb36024 | 2013-11-13 00:15:44 +0000 | [diff] [blame] | 175 | float Weight = TargetRegisterInfo::isPhysicalRegister(reg) ? |
| 176 | llvm::huge_valf : 0.0F; |
Owen Anderson | 03857b2 | 2008-08-13 21:49:13 +0000 | [diff] [blame] | 177 | return new LiveInterval(reg, Weight); |
Alkis Evlogimenos | 9a8b490 | 2004-04-09 18:07:57 +0000 | [diff] [blame] | 178 | } |
Evan Cheng | f2fbca6 | 2007-11-12 06:35:08 +0000 | [diff] [blame] | 179 | |
Jakob Stoklund Olesen | 34c6f98 | 2012-06-05 22:02:15 +0000 | [diff] [blame] | 180 | |
Jakob Stoklund Olesen | 3dfa38a | 2012-07-27 20:58:46 +0000 | [diff] [blame] | 181 | /// computeVirtRegInterval - Compute the live interval of a virtual register, |
| 182 | /// based on defs and uses. |
Matthias Braun | e25dde5 | 2013-10-10 21:28:57 +0000 | [diff] [blame] | 183 | void LiveIntervals::computeVirtRegInterval(LiveInterval &LI) { |
Jakob Stoklund Olesen | 3dfa38a | 2012-07-27 20:58:46 +0000 | [diff] [blame] | 184 | assert(LRCalc && "LRCalc not initialized."); |
Matthias Braun | e25dde5 | 2013-10-10 21:28:57 +0000 | [diff] [blame] | 185 | assert(LI.empty() && "Should only compute empty intervals."); |
Jakob Stoklund Olesen | 3dfa38a | 2012-07-27 20:58:46 +0000 | [diff] [blame] | 186 | LRCalc->reset(MF, getSlotIndexes(), DomTree, &getVNInfoAllocator()); |
| 187 | LRCalc->createDeadDefs(LI); |
| 188 | LRCalc->extendToUses(LI); |
| 189 | } |
| 190 | |
Jakob Stoklund Olesen | c16bf79 | 2012-07-27 21:56:39 +0000 | [diff] [blame] | 191 | void LiveIntervals::computeVirtRegs() { |
| 192 | for (unsigned i = 0, e = MRI->getNumVirtRegs(); i != e; ++i) { |
| 193 | unsigned Reg = TargetRegisterInfo::index2VirtReg(i); |
| 194 | if (MRI->reg_nodbg_empty(Reg)) |
| 195 | continue; |
Mark Lacey | e742d68 | 2013-08-14 23:50:16 +0000 | [diff] [blame] | 196 | createAndComputeVirtRegInterval(Reg); |
Jakob Stoklund Olesen | c16bf79 | 2012-07-27 21:56:39 +0000 | [diff] [blame] | 197 | } |
| 198 | } |
| 199 | |
| 200 | void LiveIntervals::computeRegMasks() { |
| 201 | RegMaskBlocks.resize(MF->getNumBlockIDs()); |
| 202 | |
| 203 | // Find all instructions with regmask operands. |
| 204 | for (MachineFunction::iterator MBBI = MF->begin(), E = MF->end(); |
| 205 | MBBI != E; ++MBBI) { |
| 206 | MachineBasicBlock *MBB = MBBI; |
| 207 | std::pair<unsigned, unsigned> &RMB = RegMaskBlocks[MBB->getNumber()]; |
| 208 | RMB.first = RegMaskSlots.size(); |
| 209 | for (MachineBasicBlock::iterator MI = MBB->begin(), ME = MBB->end(); |
| 210 | MI != ME; ++MI) |
| 211 | for (MIOperands MO(MI); MO.isValid(); ++MO) { |
| 212 | if (!MO->isRegMask()) |
| 213 | continue; |
| 214 | RegMaskSlots.push_back(Indexes->getInstructionIndex(MI).getRegSlot()); |
| 215 | RegMaskBits.push_back(MO->getRegMask()); |
| 216 | } |
| 217 | // Compute the number of register mask instructions in this block. |
Dmitri Gribenko | 2de0572 | 2012-09-10 21:26:47 +0000 | [diff] [blame] | 218 | RMB.second = RegMaskSlots.size() - RMB.first; |
Jakob Stoklund Olesen | c16bf79 | 2012-07-27 21:56:39 +0000 | [diff] [blame] | 219 | } |
| 220 | } |
Jakob Stoklund Olesen | 3dfa38a | 2012-07-27 20:58:46 +0000 | [diff] [blame] | 221 | |
Jakob Stoklund Olesen | 34c6f98 | 2012-06-05 22:02:15 +0000 | [diff] [blame] | 222 | //===----------------------------------------------------------------------===// |
| 223 | // Register Unit Liveness |
| 224 | //===----------------------------------------------------------------------===// |
| 225 | // |
| 226 | // Fixed interference typically comes from ABI boundaries: Function arguments |
| 227 | // and return values are passed in fixed registers, and so are exception |
| 228 | // pointers entering landing pads. Certain instructions require values to be |
| 229 | // present in specific registers. That is also represented through fixed |
| 230 | // interference. |
| 231 | // |
| 232 | |
Matthias Braun | 4f3b5e8 | 2013-10-10 21:29:02 +0000 | [diff] [blame] | 233 | /// computeRegUnitInterval - Compute the live range of a register unit, based |
| 234 | /// on the uses and defs of aliasing registers. The range should be empty, |
Jakob Stoklund Olesen | 34c6f98 | 2012-06-05 22:02:15 +0000 | [diff] [blame] | 235 | /// or contain only dead phi-defs from ABI blocks. |
Matthias Braun | 4f3b5e8 | 2013-10-10 21:29:02 +0000 | [diff] [blame] | 236 | void LiveIntervals::computeRegUnitRange(LiveRange &LR, unsigned Unit) { |
Jakob Stoklund Olesen | 34c6f98 | 2012-06-05 22:02:15 +0000 | [diff] [blame] | 237 | assert(LRCalc && "LRCalc not initialized."); |
| 238 | LRCalc->reset(MF, getSlotIndexes(), DomTree, &getVNInfoAllocator()); |
| 239 | |
| 240 | // The physregs aliasing Unit are the roots and their super-registers. |
| 241 | // Create all values as dead defs before extending to uses. Note that roots |
| 242 | // may share super-registers. That's OK because createDeadDefs() is |
| 243 | // idempotent. It is very rare for a register unit to have multiple roots, so |
| 244 | // uniquing super-registers is probably not worthwhile. |
| 245 | for (MCRegUnitRootIterator Roots(Unit, TRI); Roots.isValid(); ++Roots) { |
Chad Rosier | b018bab | 2013-05-22 22:36:55 +0000 | [diff] [blame] | 246 | for (MCSuperRegIterator Supers(*Roots, TRI, /*IncludeSelf=*/true); |
| 247 | Supers.isValid(); ++Supers) { |
Jakob Stoklund Olesen | 34c6f98 | 2012-06-05 22:02:15 +0000 | [diff] [blame] | 248 | if (!MRI->reg_empty(*Supers)) |
Matthias Braun | 4f3b5e8 | 2013-10-10 21:29:02 +0000 | [diff] [blame] | 249 | LRCalc->createDeadDefs(LR, *Supers); |
Jakob Stoklund Olesen | 34c6f98 | 2012-06-05 22:02:15 +0000 | [diff] [blame] | 250 | } |
| 251 | } |
| 252 | |
Matthias Braun | 4f3b5e8 | 2013-10-10 21:29:02 +0000 | [diff] [blame] | 253 | // Now extend LR to reach all uses. |
Jakob Stoklund Olesen | 34c6f98 | 2012-06-05 22:02:15 +0000 | [diff] [blame] | 254 | // Ignore uses of reserved registers. We only track defs of those. |
| 255 | for (MCRegUnitRootIterator Roots(Unit, TRI); Roots.isValid(); ++Roots) { |
Chad Rosier | b018bab | 2013-05-22 22:36:55 +0000 | [diff] [blame] | 256 | for (MCSuperRegIterator Supers(*Roots, TRI, /*IncludeSelf=*/true); |
| 257 | Supers.isValid(); ++Supers) { |
Jakob Stoklund Olesen | 34c6f98 | 2012-06-05 22:02:15 +0000 | [diff] [blame] | 258 | unsigned Reg = *Supers; |
Jakob Stoklund Olesen | 7900476 | 2012-10-15 22:14:34 +0000 | [diff] [blame] | 259 | if (!MRI->isReserved(Reg) && !MRI->reg_empty(Reg)) |
Matthias Braun | 4f3b5e8 | 2013-10-10 21:29:02 +0000 | [diff] [blame] | 260 | LRCalc->extendToUses(LR, Reg); |
Jakob Stoklund Olesen | 34c6f98 | 2012-06-05 22:02:15 +0000 | [diff] [blame] | 261 | } |
| 262 | } |
| 263 | } |
| 264 | |
| 265 | |
| 266 | /// computeLiveInRegUnits - Precompute the live ranges of any register units |
| 267 | /// that are live-in to an ABI block somewhere. Register values can appear |
| 268 | /// without a corresponding def when entering the entry block or a landing pad. |
| 269 | /// |
| 270 | void LiveIntervals::computeLiveInRegUnits() { |
Matthias Braun | 4f3b5e8 | 2013-10-10 21:29:02 +0000 | [diff] [blame] | 271 | RegUnitRanges.resize(TRI->getNumRegUnits()); |
Jakob Stoklund Olesen | 34c6f98 | 2012-06-05 22:02:15 +0000 | [diff] [blame] | 272 | DEBUG(dbgs() << "Computing live-in reg-units in ABI blocks.\n"); |
| 273 | |
Matthias Braun | 4f3b5e8 | 2013-10-10 21:29:02 +0000 | [diff] [blame] | 274 | // Keep track of the live range sets allocated. |
| 275 | SmallVector<unsigned, 8> NewRanges; |
Jakob Stoklund Olesen | 34c6f98 | 2012-06-05 22:02:15 +0000 | [diff] [blame] | 276 | |
| 277 | // Check all basic blocks for live-ins. |
| 278 | for (MachineFunction::const_iterator MFI = MF->begin(), MFE = MF->end(); |
| 279 | MFI != MFE; ++MFI) { |
| 280 | const MachineBasicBlock *MBB = MFI; |
| 281 | |
| 282 | // We only care about ABI blocks: Entry + landing pads. |
| 283 | if ((MFI != MF->begin() && !MBB->isLandingPad()) || MBB->livein_empty()) |
| 284 | continue; |
| 285 | |
| 286 | // Create phi-defs at Begin for all live-in registers. |
| 287 | SlotIndex Begin = Indexes->getMBBStartIdx(MBB); |
| 288 | DEBUG(dbgs() << Begin << "\tBB#" << MBB->getNumber()); |
| 289 | for (MachineBasicBlock::livein_iterator LII = MBB->livein_begin(), |
| 290 | LIE = MBB->livein_end(); LII != LIE; ++LII) { |
| 291 | for (MCRegUnitIterator Units(*LII, TRI); Units.isValid(); ++Units) { |
| 292 | unsigned Unit = *Units; |
Matthias Braun | 4f3b5e8 | 2013-10-10 21:29:02 +0000 | [diff] [blame] | 293 | LiveRange *LR = RegUnitRanges[Unit]; |
| 294 | if (!LR) { |
| 295 | LR = RegUnitRanges[Unit] = new LiveRange(); |
| 296 | NewRanges.push_back(Unit); |
Jakob Stoklund Olesen | 34c6f98 | 2012-06-05 22:02:15 +0000 | [diff] [blame] | 297 | } |
Matthias Braun | 4f3b5e8 | 2013-10-10 21:29:02 +0000 | [diff] [blame] | 298 | VNInfo *VNI = LR->createDeadDef(Begin, getVNInfoAllocator()); |
Matt Beaumont-Gay | 05b46f0 | 2012-06-05 23:00:03 +0000 | [diff] [blame] | 299 | (void)VNI; |
Jakob Stoklund Olesen | 34c6f98 | 2012-06-05 22:02:15 +0000 | [diff] [blame] | 300 | DEBUG(dbgs() << ' ' << PrintRegUnit(Unit, TRI) << '#' << VNI->id); |
| 301 | } |
| 302 | } |
| 303 | DEBUG(dbgs() << '\n'); |
| 304 | } |
Matthias Braun | 4f3b5e8 | 2013-10-10 21:29:02 +0000 | [diff] [blame] | 305 | DEBUG(dbgs() << "Created " << NewRanges.size() << " new intervals.\n"); |
Jakob Stoklund Olesen | 34c6f98 | 2012-06-05 22:02:15 +0000 | [diff] [blame] | 306 | |
Matthias Braun | 4f3b5e8 | 2013-10-10 21:29:02 +0000 | [diff] [blame] | 307 | // Compute the 'normal' part of the ranges. |
| 308 | for (unsigned i = 0, e = NewRanges.size(); i != e; ++i) { |
| 309 | unsigned Unit = NewRanges[i]; |
| 310 | computeRegUnitRange(*RegUnitRanges[Unit], Unit); |
| 311 | } |
Jakob Stoklund Olesen | 34c6f98 | 2012-06-05 22:02:15 +0000 | [diff] [blame] | 312 | } |
| 313 | |
| 314 | |
Jakob Stoklund Olesen | 11513e5 | 2011-02-08 00:03:05 +0000 | [diff] [blame] | 315 | /// shrinkToUses - After removing some uses of a register, shrink its live |
| 316 | /// range to just the remaining uses. This method does not compute reaching |
| 317 | /// defs for new uses, and it doesn't remove dead defs. |
Jakob Stoklund Olesen | 6a3dbd3 | 2011-03-17 20:37:07 +0000 | [diff] [blame] | 318 | bool LiveIntervals::shrinkToUses(LiveInterval *li, |
Jakob Stoklund Olesen | 0d8ccaa | 2011-03-07 23:29:10 +0000 | [diff] [blame] | 319 | SmallVectorImpl<MachineInstr*> *dead) { |
Jakob Stoklund Olesen | 11513e5 | 2011-02-08 00:03:05 +0000 | [diff] [blame] | 320 | DEBUG(dbgs() << "Shrink: " << *li << '\n'); |
| 321 | assert(TargetRegisterInfo::isVirtualRegister(li->reg) |
Lang Hames | 567cdba | 2012-01-03 20:05:57 +0000 | [diff] [blame] | 322 | && "Can only shrink virtual registers"); |
Jakob Stoklund Olesen | 11513e5 | 2011-02-08 00:03:05 +0000 | [diff] [blame] | 323 | // Find all the values used, including PHI kills. |
| 324 | SmallVector<std::pair<SlotIndex, VNInfo*>, 16> WorkList; |
| 325 | |
Jakob Stoklund Olesen | 031432f | 2011-09-15 15:24:16 +0000 | [diff] [blame] | 326 | // Blocks that have already been added to WorkList as live-out. |
| 327 | SmallPtrSet<MachineBasicBlock*, 16> LiveOut; |
| 328 | |
Jakob Stoklund Olesen | 11513e5 | 2011-02-08 00:03:05 +0000 | [diff] [blame] | 329 | // Visit all instructions reading li->reg. |
Stephen Hines | 36b5688 | 2014-04-23 16:57:46 -0700 | [diff] [blame] | 330 | for (MachineRegisterInfo::reg_instr_iterator |
| 331 | I = MRI->reg_instr_begin(li->reg), E = MRI->reg_instr_end(); |
| 332 | I != E; ) { |
| 333 | MachineInstr *UseMI = &*(I++); |
Jakob Stoklund Olesen | 11513e5 | 2011-02-08 00:03:05 +0000 | [diff] [blame] | 334 | if (UseMI->isDebugValue() || !UseMI->readsVirtualRegister(li->reg)) |
| 335 | continue; |
Jakob Stoklund Olesen | 6c9cc21 | 2011-11-13 23:53:25 +0000 | [diff] [blame] | 336 | SlotIndex Idx = getInstructionIndex(UseMI).getRegSlot(); |
Matthias Braun | 5649e25 | 2013-10-10 21:28:52 +0000 | [diff] [blame] | 337 | LiveQueryResult LRQ = li->Query(Idx); |
Jakob Stoklund Olesen | 97769fc | 2012-05-20 02:54:52 +0000 | [diff] [blame] | 338 | VNInfo *VNI = LRQ.valueIn(); |
Jakob Stoklund Olesen | 9ef931e | 2011-03-18 03:06:04 +0000 | [diff] [blame] | 339 | if (!VNI) { |
| 340 | // This shouldn't happen: readsVirtualRegister returns true, but there is |
| 341 | // no live value. It is likely caused by a target getting <undef> flags |
| 342 | // wrong. |
| 343 | DEBUG(dbgs() << Idx << '\t' << *UseMI |
| 344 | << "Warning: Instr claims to read non-existent value in " |
| 345 | << *li << '\n'); |
| 346 | continue; |
| 347 | } |
Jakob Stoklund Olesen | f054e19 | 2011-11-14 18:45:38 +0000 | [diff] [blame] | 348 | // Special case: An early-clobber tied operand reads and writes the |
Jakob Stoklund Olesen | 97769fc | 2012-05-20 02:54:52 +0000 | [diff] [blame] | 349 | // register one slot early. |
| 350 | if (VNInfo *DefVNI = LRQ.valueDefined()) |
| 351 | Idx = DefVNI->def; |
| 352 | |
Jakob Stoklund Olesen | 11513e5 | 2011-02-08 00:03:05 +0000 | [diff] [blame] | 353 | WorkList.push_back(std::make_pair(Idx, VNI)); |
| 354 | } |
| 355 | |
Matthias Braun | 87a8605 | 2013-10-10 21:28:47 +0000 | [diff] [blame] | 356 | // Create new live ranges with only minimal live segments per def. |
| 357 | LiveRange NewLR; |
Jakob Stoklund Olesen | 11513e5 | 2011-02-08 00:03:05 +0000 | [diff] [blame] | 358 | for (LiveInterval::vni_iterator I = li->vni_begin(), E = li->vni_end(); |
| 359 | I != E; ++I) { |
| 360 | VNInfo *VNI = *I; |
| 361 | if (VNI->isUnused()) |
| 362 | continue; |
Matthias Braun | 87a8605 | 2013-10-10 21:28:47 +0000 | [diff] [blame] | 363 | NewLR.addSegment(LiveRange::Segment(VNI->def, VNI->def.getDeadSlot(), VNI)); |
Jakob Stoklund Olesen | 11513e5 | 2011-02-08 00:03:05 +0000 | [diff] [blame] | 364 | } |
| 365 | |
Jakob Stoklund Olesen | e0ab245 | 2011-03-02 00:33:03 +0000 | [diff] [blame] | 366 | // Keep track of the PHIs that are in use. |
| 367 | SmallPtrSet<VNInfo*, 8> UsedPHIs; |
| 368 | |
Jakob Stoklund Olesen | 11513e5 | 2011-02-08 00:03:05 +0000 | [diff] [blame] | 369 | // Extend intervals to reach all uses in WorkList. |
| 370 | while (!WorkList.empty()) { |
| 371 | SlotIndex Idx = WorkList.back().first; |
| 372 | VNInfo *VNI = WorkList.back().second; |
| 373 | WorkList.pop_back(); |
Jakob Stoklund Olesen | 6c9cc21 | 2011-11-13 23:53:25 +0000 | [diff] [blame] | 374 | const MachineBasicBlock *MBB = getMBBFromIndex(Idx.getPrevSlot()); |
Jakob Stoklund Olesen | 11513e5 | 2011-02-08 00:03:05 +0000 | [diff] [blame] | 375 | SlotIndex BlockStart = getMBBStartIdx(MBB); |
Jakob Stoklund Olesen | e0ab245 | 2011-03-02 00:33:03 +0000 | [diff] [blame] | 376 | |
| 377 | // Extend the live range for VNI to be live at Idx. |
Matthias Braun | 87a8605 | 2013-10-10 21:28:47 +0000 | [diff] [blame] | 378 | if (VNInfo *ExtVNI = NewLR.extendInBlock(BlockStart, Idx)) { |
Nick Lewycky | 4b11a70 | 2011-03-02 01:43:30 +0000 | [diff] [blame] | 379 | (void)ExtVNI; |
Jakob Stoklund Olesen | e0ab245 | 2011-03-02 00:33:03 +0000 | [diff] [blame] | 380 | assert(ExtVNI == VNI && "Unexpected existing value number"); |
| 381 | // Is this a PHIDef we haven't seen before? |
Jakob Stoklund Olesen | c29d9b3 | 2011-03-03 00:20:51 +0000 | [diff] [blame] | 382 | if (!VNI->isPHIDef() || VNI->def != BlockStart || !UsedPHIs.insert(VNI)) |
Jakob Stoklund Olesen | e0ab245 | 2011-03-02 00:33:03 +0000 | [diff] [blame] | 383 | continue; |
| 384 | // The PHI is live, make sure the predecessors are live-out. |
| 385 | for (MachineBasicBlock::const_pred_iterator PI = MBB->pred_begin(), |
| 386 | PE = MBB->pred_end(); PI != PE; ++PI) { |
Jakob Stoklund Olesen | 031432f | 2011-09-15 15:24:16 +0000 | [diff] [blame] | 387 | if (!LiveOut.insert(*PI)) |
| 388 | continue; |
Jakob Stoklund Olesen | 6c9cc21 | 2011-11-13 23:53:25 +0000 | [diff] [blame] | 389 | SlotIndex Stop = getMBBEndIdx(*PI); |
Jakob Stoklund Olesen | e0ab245 | 2011-03-02 00:33:03 +0000 | [diff] [blame] | 390 | // A predecessor is not required to have a live-out value for a PHI. |
Jakob Stoklund Olesen | 6c9cc21 | 2011-11-13 23:53:25 +0000 | [diff] [blame] | 391 | if (VNInfo *PVNI = li->getVNInfoBefore(Stop)) |
Jakob Stoklund Olesen | e0ab245 | 2011-03-02 00:33:03 +0000 | [diff] [blame] | 392 | WorkList.push_back(std::make_pair(Stop, PVNI)); |
Jakob Stoklund Olesen | 11513e5 | 2011-02-08 00:03:05 +0000 | [diff] [blame] | 393 | } |
Jakob Stoklund Olesen | 11513e5 | 2011-02-08 00:03:05 +0000 | [diff] [blame] | 394 | continue; |
| 395 | } |
| 396 | |
| 397 | // VNI is live-in to MBB. |
| 398 | DEBUG(dbgs() << " live-in at " << BlockStart << '\n'); |
Matthias Braun | 87a8605 | 2013-10-10 21:28:47 +0000 | [diff] [blame] | 399 | NewLR.addSegment(LiveRange::Segment(BlockStart, Idx, VNI)); |
Jakob Stoklund Olesen | 11513e5 | 2011-02-08 00:03:05 +0000 | [diff] [blame] | 400 | |
| 401 | // Make sure VNI is live-out from the predecessors. |
| 402 | for (MachineBasicBlock::const_pred_iterator PI = MBB->pred_begin(), |
| 403 | PE = MBB->pred_end(); PI != PE; ++PI) { |
Jakob Stoklund Olesen | 031432f | 2011-09-15 15:24:16 +0000 | [diff] [blame] | 404 | if (!LiveOut.insert(*PI)) |
| 405 | continue; |
Jakob Stoklund Olesen | 6c9cc21 | 2011-11-13 23:53:25 +0000 | [diff] [blame] | 406 | SlotIndex Stop = getMBBEndIdx(*PI); |
| 407 | assert(li->getVNInfoBefore(Stop) == VNI && |
| 408 | "Wrong value out of predecessor"); |
Jakob Stoklund Olesen | 11513e5 | 2011-02-08 00:03:05 +0000 | [diff] [blame] | 409 | WorkList.push_back(std::make_pair(Stop, VNI)); |
| 410 | } |
| 411 | } |
| 412 | |
| 413 | // Handle dead values. |
Jakob Stoklund Olesen | 6a3dbd3 | 2011-03-17 20:37:07 +0000 | [diff] [blame] | 414 | bool CanSeparate = false; |
Jakob Stoklund Olesen | 11513e5 | 2011-02-08 00:03:05 +0000 | [diff] [blame] | 415 | for (LiveInterval::vni_iterator I = li->vni_begin(), E = li->vni_end(); |
| 416 | I != E; ++I) { |
| 417 | VNInfo *VNI = *I; |
| 418 | if (VNI->isUnused()) |
| 419 | continue; |
Matthias Braun | 87a8605 | 2013-10-10 21:28:47 +0000 | [diff] [blame] | 420 | LiveRange::iterator LRI = NewLR.FindSegmentContaining(VNI->def); |
| 421 | assert(LRI != NewLR.end() && "Missing segment for PHI"); |
| 422 | if (LRI->end != VNI->def.getDeadSlot()) |
Jakob Stoklund Olesen | 11513e5 | 2011-02-08 00:03:05 +0000 | [diff] [blame] | 423 | continue; |
Jakob Stoklund Olesen | a4d3473 | 2011-03-02 00:33:01 +0000 | [diff] [blame] | 424 | if (VNI->isPHIDef()) { |
Jakob Stoklund Olesen | 11513e5 | 2011-02-08 00:03:05 +0000 | [diff] [blame] | 425 | // This is a dead PHI. Remove it. |
Jakob Stoklund Olesen | b2beac2 | 2012-08-03 20:59:32 +0000 | [diff] [blame] | 426 | VNI->markUnused(); |
Matthias Braun | 87a8605 | 2013-10-10 21:28:47 +0000 | [diff] [blame] | 427 | NewLR.removeSegment(LRI->start, LRI->end); |
Jakob Stoklund Olesen | 6a3dbd3 | 2011-03-17 20:37:07 +0000 | [diff] [blame] | 428 | DEBUG(dbgs() << "Dead PHI at " << VNI->def << " may separate interval\n"); |
| 429 | CanSeparate = true; |
Jakob Stoklund Olesen | 11513e5 | 2011-02-08 00:03:05 +0000 | [diff] [blame] | 430 | } else { |
| 431 | // This is a dead def. Make sure the instruction knows. |
| 432 | MachineInstr *MI = getInstructionFromIndex(VNI->def); |
| 433 | assert(MI && "No instruction defining live value"); |
Jakob Stoklund Olesen | 15f1d8c | 2012-06-04 22:39:14 +0000 | [diff] [blame] | 434 | MI->addRegisterDead(li->reg, TRI); |
Jakob Stoklund Olesen | 0d8ccaa | 2011-03-07 23:29:10 +0000 | [diff] [blame] | 435 | if (dead && MI->allDefsAreDead()) { |
Jakob Stoklund Olesen | c46570d | 2011-03-16 22:56:08 +0000 | [diff] [blame] | 436 | DEBUG(dbgs() << "All defs dead: " << VNI->def << '\t' << *MI); |
Jakob Stoklund Olesen | 0d8ccaa | 2011-03-07 23:29:10 +0000 | [diff] [blame] | 437 | dead->push_back(MI); |
| 438 | } |
Jakob Stoklund Olesen | 11513e5 | 2011-02-08 00:03:05 +0000 | [diff] [blame] | 439 | } |
| 440 | } |
| 441 | |
Matthias Braun | 331de11 | 2013-10-10 21:28:43 +0000 | [diff] [blame] | 442 | // Move the trimmed segments back. |
Matthias Braun | 87a8605 | 2013-10-10 21:28:47 +0000 | [diff] [blame] | 443 | li->segments.swap(NewLR.segments); |
Jakob Stoklund Olesen | c46570d | 2011-03-16 22:56:08 +0000 | [diff] [blame] | 444 | DEBUG(dbgs() << "Shrunk: " << *li << '\n'); |
Jakob Stoklund Olesen | 6a3dbd3 | 2011-03-17 20:37:07 +0000 | [diff] [blame] | 445 | return CanSeparate; |
Jakob Stoklund Olesen | 11513e5 | 2011-02-08 00:03:05 +0000 | [diff] [blame] | 446 | } |
| 447 | |
Matthias Braun | e25dde5 | 2013-10-10 21:28:57 +0000 | [diff] [blame] | 448 | void LiveIntervals::extendToIndices(LiveRange &LR, |
Jakob Stoklund Olesen | 87f7864 | 2012-09-17 23:03:25 +0000 | [diff] [blame] | 449 | ArrayRef<SlotIndex> Indices) { |
| 450 | assert(LRCalc && "LRCalc not initialized."); |
| 451 | LRCalc->reset(MF, getSlotIndexes(), DomTree, &getVNInfoAllocator()); |
| 452 | for (unsigned i = 0, e = Indices.size(); i != e; ++i) |
Matthias Braun | e25dde5 | 2013-10-10 21:28:57 +0000 | [diff] [blame] | 453 | LRCalc->extend(LR, Indices[i]); |
Jakob Stoklund Olesen | 87f7864 | 2012-09-17 23:03:25 +0000 | [diff] [blame] | 454 | } |
| 455 | |
| 456 | void LiveIntervals::pruneValue(LiveInterval *LI, SlotIndex Kill, |
| 457 | SmallVectorImpl<SlotIndex> *EndPoints) { |
Matthias Braun | 5649e25 | 2013-10-10 21:28:52 +0000 | [diff] [blame] | 458 | LiveQueryResult LRQ = LI->Query(Kill); |
Jakob Stoklund Olesen | 87f7864 | 2012-09-17 23:03:25 +0000 | [diff] [blame] | 459 | VNInfo *VNI = LRQ.valueOut(); |
| 460 | if (!VNI) |
| 461 | return; |
| 462 | |
| 463 | MachineBasicBlock *KillMBB = Indexes->getMBBFromIndex(Kill); |
| 464 | SlotIndex MBBStart, MBBEnd; |
Stephen Hines | 36b5688 | 2014-04-23 16:57:46 -0700 | [diff] [blame] | 465 | std::tie(MBBStart, MBBEnd) = Indexes->getMBBRange(KillMBB); |
Jakob Stoklund Olesen | 87f7864 | 2012-09-17 23:03:25 +0000 | [diff] [blame] | 466 | |
| 467 | // If VNI isn't live out from KillMBB, the value is trivially pruned. |
| 468 | if (LRQ.endPoint() < MBBEnd) { |
Matthias Braun | 331de11 | 2013-10-10 21:28:43 +0000 | [diff] [blame] | 469 | LI->removeSegment(Kill, LRQ.endPoint()); |
Jakob Stoklund Olesen | 87f7864 | 2012-09-17 23:03:25 +0000 | [diff] [blame] | 470 | if (EndPoints) EndPoints->push_back(LRQ.endPoint()); |
| 471 | return; |
| 472 | } |
| 473 | |
| 474 | // VNI is live out of KillMBB. |
Matthias Braun | 331de11 | 2013-10-10 21:28:43 +0000 | [diff] [blame] | 475 | LI->removeSegment(Kill, MBBEnd); |
Jakob Stoklund Olesen | 87f7864 | 2012-09-17 23:03:25 +0000 | [diff] [blame] | 476 | if (EndPoints) EndPoints->push_back(MBBEnd); |
| 477 | |
Jakob Stoklund Olesen | af89690 | 2012-10-13 16:15:31 +0000 | [diff] [blame] | 478 | // Find all blocks that are reachable from KillMBB without leaving VNI's live |
| 479 | // range. It is possible that KillMBB itself is reachable, so start a DFS |
| 480 | // from each successor. |
| 481 | typedef SmallPtrSet<MachineBasicBlock*, 9> VisitedTy; |
| 482 | VisitedTy Visited; |
| 483 | for (MachineBasicBlock::succ_iterator |
| 484 | SuccI = KillMBB->succ_begin(), SuccE = KillMBB->succ_end(); |
| 485 | SuccI != SuccE; ++SuccI) { |
| 486 | for (df_ext_iterator<MachineBasicBlock*, VisitedTy> |
| 487 | I = df_ext_begin(*SuccI, Visited), E = df_ext_end(*SuccI, Visited); |
| 488 | I != E;) { |
| 489 | MachineBasicBlock *MBB = *I; |
| 490 | |
| 491 | // Check if VNI is live in to MBB. |
Stephen Hines | 36b5688 | 2014-04-23 16:57:46 -0700 | [diff] [blame] | 492 | std::tie(MBBStart, MBBEnd) = Indexes->getMBBRange(MBB); |
Matthias Braun | 5649e25 | 2013-10-10 21:28:52 +0000 | [diff] [blame] | 493 | LiveQueryResult LRQ = LI->Query(MBBStart); |
Jakob Stoklund Olesen | af89690 | 2012-10-13 16:15:31 +0000 | [diff] [blame] | 494 | if (LRQ.valueIn() != VNI) { |
Matthias Braun | 331de11 | 2013-10-10 21:28:43 +0000 | [diff] [blame] | 495 | // This block isn't part of the VNI segment. Prune the search. |
Jakob Stoklund Olesen | af89690 | 2012-10-13 16:15:31 +0000 | [diff] [blame] | 496 | I.skipChildren(); |
| 497 | continue; |
| 498 | } |
| 499 | |
| 500 | // Prune the search if VNI is killed in MBB. |
| 501 | if (LRQ.endPoint() < MBBEnd) { |
Matthias Braun | 331de11 | 2013-10-10 21:28:43 +0000 | [diff] [blame] | 502 | LI->removeSegment(MBBStart, LRQ.endPoint()); |
Jakob Stoklund Olesen | af89690 | 2012-10-13 16:15:31 +0000 | [diff] [blame] | 503 | if (EndPoints) EndPoints->push_back(LRQ.endPoint()); |
| 504 | I.skipChildren(); |
| 505 | continue; |
| 506 | } |
| 507 | |
| 508 | // VNI is live through MBB. |
Matthias Braun | 331de11 | 2013-10-10 21:28:43 +0000 | [diff] [blame] | 509 | LI->removeSegment(MBBStart, MBBEnd); |
Jakob Stoklund Olesen | af89690 | 2012-10-13 16:15:31 +0000 | [diff] [blame] | 510 | if (EndPoints) EndPoints->push_back(MBBEnd); |
Jakob Stoklund Olesen | 87f7864 | 2012-09-17 23:03:25 +0000 | [diff] [blame] | 511 | ++I; |
Jakob Stoklund Olesen | 87f7864 | 2012-09-17 23:03:25 +0000 | [diff] [blame] | 512 | } |
Jakob Stoklund Olesen | 87f7864 | 2012-09-17 23:03:25 +0000 | [diff] [blame] | 513 | } |
| 514 | } |
Jakob Stoklund Olesen | 11513e5 | 2011-02-08 00:03:05 +0000 | [diff] [blame] | 515 | |
Evan Cheng | f2fbca6 | 2007-11-12 06:35:08 +0000 | [diff] [blame] | 516 | //===----------------------------------------------------------------------===// |
| 517 | // Register allocator hooks. |
| 518 | // |
| 519 | |
Jakob Stoklund Olesen | e617ccb | 2012-09-06 18:15:18 +0000 | [diff] [blame] | 520 | void LiveIntervals::addKillFlags(const VirtRegMap *VRM) { |
| 521 | // Keep track of regunit ranges. |
Matthias Braun | 4f3b5e8 | 2013-10-10 21:29:02 +0000 | [diff] [blame] | 522 | SmallVector<std::pair<LiveRange*, LiveRange::iterator>, 8> RU; |
Jakob Stoklund Olesen | e617ccb | 2012-09-06 18:15:18 +0000 | [diff] [blame] | 523 | |
Jakob Stoklund Olesen | 12a7be9 | 2012-06-20 23:23:59 +0000 | [diff] [blame] | 524 | for (unsigned i = 0, e = MRI->getNumVirtRegs(); i != e; ++i) { |
| 525 | unsigned Reg = TargetRegisterInfo::index2VirtReg(i); |
Jakob Stoklund Olesen | 15f1d8c | 2012-06-04 22:39:14 +0000 | [diff] [blame] | 526 | if (MRI->reg_nodbg_empty(Reg)) |
Jakob Stoklund Olesen | 8a61da8 | 2011-02-08 21:13:03 +0000 | [diff] [blame] | 527 | continue; |
Jakob Stoklund Olesen | 12a7be9 | 2012-06-20 23:23:59 +0000 | [diff] [blame] | 528 | LiveInterval *LI = &getInterval(Reg); |
Jakob Stoklund Olesen | e617ccb | 2012-09-06 18:15:18 +0000 | [diff] [blame] | 529 | if (LI->empty()) |
| 530 | continue; |
| 531 | |
| 532 | // Find the regunit intervals for the assigned register. They may overlap |
| 533 | // the virtual register live range, cancelling any kills. |
| 534 | RU.clear(); |
| 535 | for (MCRegUnitIterator Units(VRM->getPhys(Reg), TRI); Units.isValid(); |
| 536 | ++Units) { |
Matthias Braun | 4f3b5e8 | 2013-10-10 21:29:02 +0000 | [diff] [blame] | 537 | LiveRange &RURanges = getRegUnit(*Units); |
| 538 | if (RURanges.empty()) |
Jakob Stoklund Olesen | e617ccb | 2012-09-06 18:15:18 +0000 | [diff] [blame] | 539 | continue; |
Matthias Braun | 4f3b5e8 | 2013-10-10 21:29:02 +0000 | [diff] [blame] | 540 | RU.push_back(std::make_pair(&RURanges, RURanges.find(LI->begin()->end))); |
Jakob Stoklund Olesen | e617ccb | 2012-09-06 18:15:18 +0000 | [diff] [blame] | 541 | } |
Jakob Stoklund Olesen | 8a61da8 | 2011-02-08 21:13:03 +0000 | [diff] [blame] | 542 | |
Matthias Braun | 331de11 | 2013-10-10 21:28:43 +0000 | [diff] [blame] | 543 | // Every instruction that kills Reg corresponds to a segment range end |
| 544 | // point. |
Jakob Stoklund Olesen | 8a61da8 | 2011-02-08 21:13:03 +0000 | [diff] [blame] | 545 | for (LiveInterval::iterator RI = LI->begin(), RE = LI->end(); RI != RE; |
| 546 | ++RI) { |
Jakob Stoklund Olesen | 2debd48 | 2011-11-13 20:45:27 +0000 | [diff] [blame] | 547 | // A block index indicates an MBB edge. |
| 548 | if (RI->end.isBlock()) |
Jakob Stoklund Olesen | 8a61da8 | 2011-02-08 21:13:03 +0000 | [diff] [blame] | 549 | continue; |
| 550 | MachineInstr *MI = getInstructionFromIndex(RI->end); |
| 551 | if (!MI) |
| 552 | continue; |
Jakob Stoklund Olesen | e617ccb | 2012-09-06 18:15:18 +0000 | [diff] [blame] | 553 | |
Matthias Braun | b1aa5e4 | 2013-10-04 16:52:58 +0000 | [diff] [blame] | 554 | // Check if any of the regunits are live beyond the end of RI. That could |
Jakob Stoklund Olesen | e617ccb | 2012-09-06 18:15:18 +0000 | [diff] [blame] | 555 | // happen when a physreg is defined as a copy of a virtreg: |
| 556 | // |
| 557 | // %EAX = COPY %vreg5 |
| 558 | // FOO %vreg5 <--- MI, cancel kill because %EAX is live. |
| 559 | // BAR %EAX<kill> |
| 560 | // |
| 561 | // There should be no kill flag on FOO when %vreg5 is rewritten as %EAX. |
| 562 | bool CancelKill = false; |
| 563 | for (unsigned u = 0, e = RU.size(); u != e; ++u) { |
Matthias Braun | 4f3b5e8 | 2013-10-10 21:29:02 +0000 | [diff] [blame] | 564 | LiveRange &RRanges = *RU[u].first; |
| 565 | LiveRange::iterator &I = RU[u].second; |
| 566 | if (I == RRanges.end()) |
Jakob Stoklund Olesen | e617ccb | 2012-09-06 18:15:18 +0000 | [diff] [blame] | 567 | continue; |
Matthias Braun | 4f3b5e8 | 2013-10-10 21:29:02 +0000 | [diff] [blame] | 568 | I = RRanges.advanceTo(I, RI->end); |
| 569 | if (I == RRanges.end() || I->start >= RI->end) |
Jakob Stoklund Olesen | e617ccb | 2012-09-06 18:15:18 +0000 | [diff] [blame] | 570 | continue; |
| 571 | // I is overlapping RI. |
| 572 | CancelKill = true; |
| 573 | break; |
| 574 | } |
| 575 | if (CancelKill) |
Stephen Hines | dce4a40 | 2014-05-29 02:49:00 -0700 | [diff] [blame^] | 576 | MI->clearRegisterKills(Reg, nullptr); |
Jakob Stoklund Olesen | e617ccb | 2012-09-06 18:15:18 +0000 | [diff] [blame] | 577 | else |
Stephen Hines | dce4a40 | 2014-05-29 02:49:00 -0700 | [diff] [blame^] | 578 | MI->addRegisterKilled(Reg, nullptr); |
Jakob Stoklund Olesen | 8a61da8 | 2011-02-08 21:13:03 +0000 | [diff] [blame] | 579 | } |
| 580 | } |
| 581 | } |
| 582 | |
Jakob Stoklund Olesen | ebf2750 | 2012-02-10 01:23:55 +0000 | [diff] [blame] | 583 | MachineBasicBlock* |
| 584 | LiveIntervals::intervalIsInOneMBB(const LiveInterval &LI) const { |
| 585 | // A local live range must be fully contained inside the block, meaning it is |
| 586 | // defined and killed at instructions, not at block boundaries. It is not |
| 587 | // live in or or out of any block. |
| 588 | // |
| 589 | // It is technically possible to have a PHI-defined live range identical to a |
| 590 | // single block, but we are going to return false in that case. |
Lang Hames | 233a60e | 2009-11-03 23:52:08 +0000 | [diff] [blame] | 591 | |
Jakob Stoklund Olesen | ebf2750 | 2012-02-10 01:23:55 +0000 | [diff] [blame] | 592 | SlotIndex Start = LI.beginIndex(); |
| 593 | if (Start.isBlock()) |
Stephen Hines | dce4a40 | 2014-05-29 02:49:00 -0700 | [diff] [blame^] | 594 | return nullptr; |
Lang Hames | 233a60e | 2009-11-03 23:52:08 +0000 | [diff] [blame] | 595 | |
Jakob Stoklund Olesen | ebf2750 | 2012-02-10 01:23:55 +0000 | [diff] [blame] | 596 | SlotIndex Stop = LI.endIndex(); |
| 597 | if (Stop.isBlock()) |
Stephen Hines | dce4a40 | 2014-05-29 02:49:00 -0700 | [diff] [blame^] | 598 | return nullptr; |
Lang Hames | 233a60e | 2009-11-03 23:52:08 +0000 | [diff] [blame] | 599 | |
Jakob Stoklund Olesen | ebf2750 | 2012-02-10 01:23:55 +0000 | [diff] [blame] | 600 | // getMBBFromIndex doesn't need to search the MBB table when both indexes |
| 601 | // belong to proper instructions. |
Jakob Stoklund Olesen | 15f1d8c | 2012-06-04 22:39:14 +0000 | [diff] [blame] | 602 | MachineBasicBlock *MBB1 = Indexes->getMBBFromIndex(Start); |
| 603 | MachineBasicBlock *MBB2 = Indexes->getMBBFromIndex(Stop); |
Stephen Hines | dce4a40 | 2014-05-29 02:49:00 -0700 | [diff] [blame^] | 604 | return MBB1 == MBB2 ? MBB1 : nullptr; |
Evan Cheng | 81a0382 | 2007-11-17 00:40:40 +0000 | [diff] [blame] | 605 | } |
| 606 | |
Jakob Stoklund Olesen | 0ab7103 | 2012-08-03 20:10:24 +0000 | [diff] [blame] | 607 | bool |
| 608 | LiveIntervals::hasPHIKill(const LiveInterval &LI, const VNInfo *VNI) const { |
| 609 | for (LiveInterval::const_vni_iterator I = LI.vni_begin(), E = LI.vni_end(); |
| 610 | I != E; ++I) { |
| 611 | const VNInfo *PHI = *I; |
| 612 | if (PHI->isUnused() || !PHI->isPHIDef()) |
| 613 | continue; |
| 614 | const MachineBasicBlock *PHIMBB = getMBBFromIndex(PHI->def); |
| 615 | // Conservatively return true instead of scanning huge predecessor lists. |
| 616 | if (PHIMBB->pred_size() > 100) |
| 617 | return true; |
| 618 | for (MachineBasicBlock::const_pred_iterator |
| 619 | PI = PHIMBB->pred_begin(), PE = PHIMBB->pred_end(); PI != PE; ++PI) |
| 620 | if (VNI == LI.getVNInfoBefore(Indexes->getMBBEndIdx(*PI))) |
| 621 | return true; |
| 622 | } |
| 623 | return false; |
| 624 | } |
| 625 | |
Jakob Stoklund Olesen | e5d9041 | 2010-03-01 20:59:38 +0000 | [diff] [blame] | 626 | float |
Stephen Hines | 36b5688 | 2014-04-23 16:57:46 -0700 | [diff] [blame] | 627 | LiveIntervals::getSpillWeight(bool isDef, bool isUse, |
| 628 | const MachineBlockFrequencyInfo *MBFI, |
| 629 | const MachineInstr *MI) { |
| 630 | BlockFrequency Freq = MBFI->getBlockFreq(MI->getParent()); |
| 631 | const float Scale = 1.0f / MBFI->getEntryFreq(); |
| 632 | return (isDef + isUse) * (Freq.getFrequency() * Scale); |
Jakob Stoklund Olesen | e5d9041 | 2010-03-01 20:59:38 +0000 | [diff] [blame] | 633 | } |
| 634 | |
Matthias Braun | 87a8605 | 2013-10-10 21:28:47 +0000 | [diff] [blame] | 635 | LiveRange::Segment |
Matthias Braun | 331de11 | 2013-10-10 21:28:43 +0000 | [diff] [blame] | 636 | LiveIntervals::addSegmentToEndOfBlock(unsigned reg, MachineInstr* startInst) { |
Mark Lacey | e742d68 | 2013-08-14 23:50:16 +0000 | [diff] [blame] | 637 | LiveInterval& Interval = createEmptyInterval(reg); |
Owen Anderson | c4dc132 | 2008-06-05 17:15:43 +0000 | [diff] [blame] | 638 | VNInfo* VN = Interval.getNextValue( |
Jakob Stoklund Olesen | 2debd48 | 2011-11-13 20:45:27 +0000 | [diff] [blame] | 639 | SlotIndex(getInstructionIndex(startInst).getRegSlot()), |
Jakob Stoklund Olesen | 3b1088a | 2012-02-04 05:20:49 +0000 | [diff] [blame] | 640 | getVNInfoAllocator()); |
Matthias Braun | 87a8605 | 2013-10-10 21:28:47 +0000 | [diff] [blame] | 641 | LiveRange::Segment S( |
Jakob Stoklund Olesen | 2debd48 | 2011-11-13 20:45:27 +0000 | [diff] [blame] | 642 | SlotIndex(getInstructionIndex(startInst).getRegSlot()), |
Lang Hames | 74ab5ee | 2009-12-22 00:11:50 +0000 | [diff] [blame] | 643 | getMBBEndIdx(startInst->getParent()), VN); |
Matthias Braun | 331de11 | 2013-10-10 21:28:43 +0000 | [diff] [blame] | 644 | Interval.addSegment(S); |
Jakob Stoklund Olesen | 1b29320 | 2010-08-12 20:01:23 +0000 | [diff] [blame] | 645 | |
Matthias Braun | 331de11 | 2013-10-10 21:28:43 +0000 | [diff] [blame] | 646 | return S; |
Owen Anderson | c4dc132 | 2008-06-05 17:15:43 +0000 | [diff] [blame] | 647 | } |
Jakob Stoklund Olesen | 3fd3a84 | 2012-02-08 17:33:45 +0000 | [diff] [blame] | 648 | |
| 649 | |
| 650 | //===----------------------------------------------------------------------===// |
| 651 | // Register mask functions |
| 652 | //===----------------------------------------------------------------------===// |
| 653 | |
| 654 | bool LiveIntervals::checkRegMaskInterference(LiveInterval &LI, |
| 655 | BitVector &UsableRegs) { |
| 656 | if (LI.empty()) |
| 657 | return false; |
Jakob Stoklund Olesen | 9f10ac6 | 2012-02-10 01:31:31 +0000 | [diff] [blame] | 658 | LiveInterval::iterator LiveI = LI.begin(), LiveE = LI.end(); |
| 659 | |
| 660 | // Use a smaller arrays for local live ranges. |
| 661 | ArrayRef<SlotIndex> Slots; |
| 662 | ArrayRef<const uint32_t*> Bits; |
| 663 | if (MachineBasicBlock *MBB = intervalIsInOneMBB(LI)) { |
| 664 | Slots = getRegMaskSlotsInBlock(MBB->getNumber()); |
| 665 | Bits = getRegMaskBitsInBlock(MBB->getNumber()); |
| 666 | } else { |
| 667 | Slots = getRegMaskSlots(); |
| 668 | Bits = getRegMaskBits(); |
| 669 | } |
Jakob Stoklund Olesen | 3fd3a84 | 2012-02-08 17:33:45 +0000 | [diff] [blame] | 670 | |
| 671 | // We are going to enumerate all the register mask slots contained in LI. |
| 672 | // Start with a binary search of RegMaskSlots to find a starting point. |
Jakob Stoklund Olesen | 3fd3a84 | 2012-02-08 17:33:45 +0000 | [diff] [blame] | 673 | ArrayRef<SlotIndex>::iterator SlotI = |
| 674 | std::lower_bound(Slots.begin(), Slots.end(), LiveI->start); |
| 675 | ArrayRef<SlotIndex>::iterator SlotE = Slots.end(); |
| 676 | |
| 677 | // No slots in range, LI begins after the last call. |
| 678 | if (SlotI == SlotE) |
| 679 | return false; |
| 680 | |
| 681 | bool Found = false; |
| 682 | for (;;) { |
| 683 | assert(*SlotI >= LiveI->start); |
| 684 | // Loop over all slots overlapping this segment. |
| 685 | while (*SlotI < LiveI->end) { |
| 686 | // *SlotI overlaps LI. Collect mask bits. |
| 687 | if (!Found) { |
| 688 | // This is the first overlap. Initialize UsableRegs to all ones. |
| 689 | UsableRegs.clear(); |
Jakob Stoklund Olesen | 15f1d8c | 2012-06-04 22:39:14 +0000 | [diff] [blame] | 690 | UsableRegs.resize(TRI->getNumRegs(), true); |
Jakob Stoklund Olesen | 3fd3a84 | 2012-02-08 17:33:45 +0000 | [diff] [blame] | 691 | Found = true; |
| 692 | } |
| 693 | // Remove usable registers clobbered by this mask. |
Jakob Stoklund Olesen | 9f10ac6 | 2012-02-10 01:31:31 +0000 | [diff] [blame] | 694 | UsableRegs.clearBitsNotInMask(Bits[SlotI-Slots.begin()]); |
Jakob Stoklund Olesen | 3fd3a84 | 2012-02-08 17:33:45 +0000 | [diff] [blame] | 695 | if (++SlotI == SlotE) |
| 696 | return Found; |
| 697 | } |
| 698 | // *SlotI is beyond the current LI segment. |
| 699 | LiveI = LI.advanceTo(LiveI, *SlotI); |
| 700 | if (LiveI == LiveE) |
| 701 | return Found; |
| 702 | // Advance SlotI until it overlaps. |
| 703 | while (*SlotI < LiveI->start) |
| 704 | if (++SlotI == SlotE) |
| 705 | return Found; |
| 706 | } |
| 707 | } |
Lang Hames | 3dc7c51 | 2012-02-17 18:44:18 +0000 | [diff] [blame] | 708 | |
| 709 | //===----------------------------------------------------------------------===// |
| 710 | // IntervalUpdate class. |
| 711 | //===----------------------------------------------------------------------===// |
| 712 | |
Lang Hames | fd6d321 | 2012-02-21 00:00:36 +0000 | [diff] [blame] | 713 | // HMEditor is a toolkit used by handleMove to trim or extend live intervals. |
Lang Hames | 3dc7c51 | 2012-02-17 18:44:18 +0000 | [diff] [blame] | 714 | class LiveIntervals::HMEditor { |
| 715 | private: |
Lang Hames | ecb5062 | 2012-02-17 23:43:40 +0000 | [diff] [blame] | 716 | LiveIntervals& LIS; |
| 717 | const MachineRegisterInfo& MRI; |
| 718 | const TargetRegisterInfo& TRI; |
Jakob Stoklund Olesen | ad5e969 | 2012-10-12 21:31:57 +0000 | [diff] [blame] | 719 | SlotIndex OldIdx; |
Lang Hames | ecb5062 | 2012-02-17 23:43:40 +0000 | [diff] [blame] | 720 | SlotIndex NewIdx; |
Matthias Braun | 4f3b5e8 | 2013-10-10 21:29:02 +0000 | [diff] [blame] | 721 | SmallPtrSet<LiveRange*, 8> Updated; |
Andrew Trick | 27c28ce | 2012-10-16 00:22:51 +0000 | [diff] [blame] | 722 | bool UpdateFlags; |
Lang Hames | 6aceab1 | 2012-02-19 07:13:05 +0000 | [diff] [blame] | 723 | |
Lang Hames | 3dc7c51 | 2012-02-17 18:44:18 +0000 | [diff] [blame] | 724 | public: |
Lang Hames | ecb5062 | 2012-02-17 23:43:40 +0000 | [diff] [blame] | 725 | HMEditor(LiveIntervals& LIS, const MachineRegisterInfo& MRI, |
Jakob Stoklund Olesen | ad5e969 | 2012-10-12 21:31:57 +0000 | [diff] [blame] | 726 | const TargetRegisterInfo& TRI, |
Andrew Trick | 27c28ce | 2012-10-16 00:22:51 +0000 | [diff] [blame] | 727 | SlotIndex OldIdx, SlotIndex NewIdx, bool UpdateFlags) |
| 728 | : LIS(LIS), MRI(MRI), TRI(TRI), OldIdx(OldIdx), NewIdx(NewIdx), |
| 729 | UpdateFlags(UpdateFlags) {} |
| 730 | |
| 731 | // FIXME: UpdateFlags is a workaround that creates live intervals for all |
| 732 | // physregs, even those that aren't needed for regalloc, in order to update |
| 733 | // kill flags. This is wasteful. Eventually, LiveVariables will strip all kill |
| 734 | // flags, and postRA passes will use a live register utility instead. |
Matthias Braun | 4f3b5e8 | 2013-10-10 21:29:02 +0000 | [diff] [blame] | 735 | LiveRange *getRegUnitLI(unsigned Unit) { |
Andrew Trick | 27c28ce | 2012-10-16 00:22:51 +0000 | [diff] [blame] | 736 | if (UpdateFlags) |
| 737 | return &LIS.getRegUnit(Unit); |
| 738 | return LIS.getCachedRegUnit(Unit); |
| 739 | } |
Lang Hames | 3dc7c51 | 2012-02-17 18:44:18 +0000 | [diff] [blame] | 740 | |
Jakob Stoklund Olesen | ad5e969 | 2012-10-12 21:31:57 +0000 | [diff] [blame] | 741 | /// Update all live ranges touched by MI, assuming a move from OldIdx to |
| 742 | /// NewIdx. |
| 743 | void updateAllRanges(MachineInstr *MI) { |
| 744 | DEBUG(dbgs() << "handleMove " << OldIdx << " -> " << NewIdx << ": " << *MI); |
| 745 | bool hasRegMask = false; |
| 746 | for (MIOperands MO(MI); MO.isValid(); ++MO) { |
| 747 | if (MO->isRegMask()) |
| 748 | hasRegMask = true; |
| 749 | if (!MO->isReg()) |
Lang Hames | 4586d25 | 2012-02-21 22:29:38 +0000 | [diff] [blame] | 750 | continue; |
Jakob Stoklund Olesen | ad5e969 | 2012-10-12 21:31:57 +0000 | [diff] [blame] | 751 | // Aggressively clear all kill flags. |
| 752 | // They are reinserted by VirtRegRewriter. |
| 753 | if (MO->isUse()) |
| 754 | MO->setIsKill(false); |
| 755 | |
| 756 | unsigned Reg = MO->getReg(); |
| 757 | if (!Reg) |
| 758 | continue; |
| 759 | if (TargetRegisterInfo::isVirtualRegister(Reg)) { |
Matthias Braun | 4f3b5e8 | 2013-10-10 21:29:02 +0000 | [diff] [blame] | 760 | LiveInterval &LI = LIS.getInterval(Reg); |
| 761 | updateRange(LI, Reg); |
Jakob Stoklund Olesen | ad5e969 | 2012-10-12 21:31:57 +0000 | [diff] [blame] | 762 | continue; |
| 763 | } |
| 764 | |
| 765 | // For physregs, only update the regunits that actually have a |
| 766 | // precomputed live range. |
| 767 | for (MCRegUnitIterator Units(Reg, &TRI); Units.isValid(); ++Units) |
Matthias Braun | 4f3b5e8 | 2013-10-10 21:29:02 +0000 | [diff] [blame] | 768 | if (LiveRange *LR = getRegUnitLI(*Units)) |
| 769 | updateRange(*LR, *Units); |
Lang Hames | 4586d25 | 2012-02-21 22:29:38 +0000 | [diff] [blame] | 770 | } |
Jakob Stoklund Olesen | ad5e969 | 2012-10-12 21:31:57 +0000 | [diff] [blame] | 771 | if (hasRegMask) |
| 772 | updateRegMaskSlots(); |
Lang Hames | 6aceab1 | 2012-02-19 07:13:05 +0000 | [diff] [blame] | 773 | } |
| 774 | |
Lang Hames | 55fed62 | 2012-02-19 03:00:30 +0000 | [diff] [blame] | 775 | private: |
Jakob Stoklund Olesen | ad5e969 | 2012-10-12 21:31:57 +0000 | [diff] [blame] | 776 | /// Update a single live range, assuming an instruction has been moved from |
| 777 | /// OldIdx to NewIdx. |
Matthias Braun | 4f3b5e8 | 2013-10-10 21:29:02 +0000 | [diff] [blame] | 778 | void updateRange(LiveRange &LR, unsigned Reg) { |
| 779 | if (!Updated.insert(&LR)) |
Jakob Stoklund Olesen | ad5e969 | 2012-10-12 21:31:57 +0000 | [diff] [blame] | 780 | return; |
| 781 | DEBUG({ |
| 782 | dbgs() << " "; |
Matthias Braun | 4f3b5e8 | 2013-10-10 21:29:02 +0000 | [diff] [blame] | 783 | if (TargetRegisterInfo::isVirtualRegister(Reg)) |
| 784 | dbgs() << PrintReg(Reg); |
Jakob Stoklund Olesen | bf833f0 | 2012-06-19 23:50:18 +0000 | [diff] [blame] | 785 | else |
Matthias Braun | 4f3b5e8 | 2013-10-10 21:29:02 +0000 | [diff] [blame] | 786 | dbgs() << PrintRegUnit(Reg, &TRI); |
| 787 | dbgs() << ":\t" << LR << '\n'; |
Jakob Stoklund Olesen | ad5e969 | 2012-10-12 21:31:57 +0000 | [diff] [blame] | 788 | }); |
| 789 | if (SlotIndex::isEarlierInstr(OldIdx, NewIdx)) |
Matthias Braun | 4f3b5e8 | 2013-10-10 21:29:02 +0000 | [diff] [blame] | 790 | handleMoveDown(LR); |
Jakob Stoklund Olesen | ad5e969 | 2012-10-12 21:31:57 +0000 | [diff] [blame] | 791 | else |
Matthias Braun | 4f3b5e8 | 2013-10-10 21:29:02 +0000 | [diff] [blame] | 792 | handleMoveUp(LR, Reg); |
| 793 | DEBUG(dbgs() << " -->\t" << LR << '\n'); |
| 794 | LR.verify(); |
Lang Hames | 3dc7c51 | 2012-02-17 18:44:18 +0000 | [diff] [blame] | 795 | } |
| 796 | |
Matthias Braun | 4f3b5e8 | 2013-10-10 21:29:02 +0000 | [diff] [blame] | 797 | /// Update LR to reflect an instruction has been moved downwards from OldIdx |
Jakob Stoklund Olesen | ad5e969 | 2012-10-12 21:31:57 +0000 | [diff] [blame] | 798 | /// to NewIdx. |
| 799 | /// |
| 800 | /// 1. Live def at OldIdx: |
| 801 | /// Move def to NewIdx, assert endpoint after NewIdx. |
| 802 | /// |
| 803 | /// 2. Live def at OldIdx, killed at NewIdx: |
| 804 | /// Change to dead def at NewIdx. |
| 805 | /// (Happens when bundling def+kill together). |
| 806 | /// |
| 807 | /// 3. Dead def at OldIdx: |
| 808 | /// Move def to NewIdx, possibly across another live value. |
| 809 | /// |
| 810 | /// 4. Def at OldIdx AND at NewIdx: |
Matthias Braun | 331de11 | 2013-10-10 21:28:43 +0000 | [diff] [blame] | 811 | /// Remove segment [OldIdx;NewIdx) and value defined at OldIdx. |
Jakob Stoklund Olesen | ad5e969 | 2012-10-12 21:31:57 +0000 | [diff] [blame] | 812 | /// (Happens when bundling multiple defs together). |
| 813 | /// |
| 814 | /// 5. Value read at OldIdx, killed before NewIdx: |
| 815 | /// Extend kill to NewIdx. |
| 816 | /// |
Matthias Braun | 4f3b5e8 | 2013-10-10 21:29:02 +0000 | [diff] [blame] | 817 | void handleMoveDown(LiveRange &LR) { |
Jakob Stoklund Olesen | ad5e969 | 2012-10-12 21:31:57 +0000 | [diff] [blame] | 818 | // First look for a kill at OldIdx. |
Matthias Braun | 4f3b5e8 | 2013-10-10 21:29:02 +0000 | [diff] [blame] | 819 | LiveRange::iterator I = LR.find(OldIdx.getBaseIndex()); |
| 820 | LiveRange::iterator E = LR.end(); |
| 821 | // Is LR even live at OldIdx? |
Jakob Stoklund Olesen | ad5e969 | 2012-10-12 21:31:57 +0000 | [diff] [blame] | 822 | if (I == E || SlotIndex::isEarlierInstr(OldIdx, I->start)) |
| 823 | return; |
Lang Hames | 6aceab1 | 2012-02-19 07:13:05 +0000 | [diff] [blame] | 824 | |
Jakob Stoklund Olesen | ad5e969 | 2012-10-12 21:31:57 +0000 | [diff] [blame] | 825 | // Handle a live-in value. |
| 826 | if (!SlotIndex::isSameInstr(I->start, OldIdx)) { |
| 827 | bool isKill = SlotIndex::isSameInstr(OldIdx, I->end); |
| 828 | // If the live-in value already extends to NewIdx, there is nothing to do. |
| 829 | if (!SlotIndex::isEarlierInstr(I->end, NewIdx)) |
| 830 | return; |
| 831 | // Aggressively remove all kill flags from the old kill point. |
| 832 | // Kill flags shouldn't be used while live intervals exist, they will be |
| 833 | // reinserted by VirtRegRewriter. |
| 834 | if (MachineInstr *KillMI = LIS.getInstructionFromIndex(I->end)) |
| 835 | for (MIBundleOperands MO(KillMI); MO.isValid(); ++MO) |
| 836 | if (MO->isReg() && MO->isUse()) |
| 837 | MO->setIsKill(false); |
Matthias Braun | 4f3b5e8 | 2013-10-10 21:29:02 +0000 | [diff] [blame] | 838 | // Adjust I->end to reach NewIdx. This may temporarily make LR invalid by |
Jakob Stoklund Olesen | ad5e969 | 2012-10-12 21:31:57 +0000 | [diff] [blame] | 839 | // overlapping ranges. Case 5 above. |
| 840 | I->end = NewIdx.getRegSlot(I->end.isEarlyClobber()); |
| 841 | // If this was a kill, there may also be a def. Otherwise we're done. |
| 842 | if (!isKill) |
| 843 | return; |
| 844 | ++I; |
Lang Hames | 6aceab1 | 2012-02-19 07:13:05 +0000 | [diff] [blame] | 845 | } |
| 846 | |
Jakob Stoklund Olesen | ad5e969 | 2012-10-12 21:31:57 +0000 | [diff] [blame] | 847 | // Check for a def at OldIdx. |
| 848 | if (I == E || !SlotIndex::isSameInstr(OldIdx, I->start)) |
| 849 | return; |
| 850 | // We have a def at OldIdx. |
| 851 | VNInfo *DefVNI = I->valno; |
| 852 | assert(DefVNI->def == I->start && "Inconsistent def"); |
| 853 | DefVNI->def = NewIdx.getRegSlot(I->start.isEarlyClobber()); |
| 854 | // If the defined value extends beyond NewIdx, just move the def down. |
| 855 | // This is case 1 above. |
| 856 | if (SlotIndex::isEarlierInstr(NewIdx, I->end)) { |
| 857 | I->start = DefVNI->def; |
| 858 | return; |
| 859 | } |
| 860 | // The remaining possibilities are now: |
| 861 | // 2. Live def at OldIdx, killed at NewIdx: isSameInstr(I->end, NewIdx). |
| 862 | // 3. Dead def at OldIdx: I->end = OldIdx.getDeadSlot(). |
| 863 | // In either case, it is possible that there is an existing def at NewIdx. |
| 864 | assert((I->end == OldIdx.getDeadSlot() || |
| 865 | SlotIndex::isSameInstr(I->end, NewIdx)) && |
| 866 | "Cannot move def below kill"); |
Matthias Braun | 4f3b5e8 | 2013-10-10 21:29:02 +0000 | [diff] [blame] | 867 | LiveRange::iterator NewI = LR.advanceTo(I, NewIdx.getRegSlot()); |
Jakob Stoklund Olesen | ad5e969 | 2012-10-12 21:31:57 +0000 | [diff] [blame] | 868 | if (NewI != E && SlotIndex::isSameInstr(NewI->start, NewIdx)) { |
| 869 | // There is an existing def at NewIdx, case 4 above. The def at OldIdx is |
| 870 | // coalesced into that value. |
| 871 | assert(NewI->valno != DefVNI && "Multiple defs of value?"); |
Matthias Braun | 4f3b5e8 | 2013-10-10 21:29:02 +0000 | [diff] [blame] | 872 | LR.removeValNo(DefVNI); |
Jakob Stoklund Olesen | ad5e969 | 2012-10-12 21:31:57 +0000 | [diff] [blame] | 873 | return; |
| 874 | } |
| 875 | // There was no existing def at NewIdx. Turn *I into a dead def at NewIdx. |
Matthias Braun | 4f3b5e8 | 2013-10-10 21:29:02 +0000 | [diff] [blame] | 876 | // If the def at OldIdx was dead, we allow it to be moved across other LR |
Jakob Stoklund Olesen | ad5e969 | 2012-10-12 21:31:57 +0000 | [diff] [blame] | 877 | // values. The new range should be placed immediately before NewI, move any |
| 878 | // intermediate ranges up. |
| 879 | assert(NewI != I && "Inconsistent iterators"); |
Stephen Hines | 36b5688 | 2014-04-23 16:57:46 -0700 | [diff] [blame] | 880 | std::copy(std::next(I), NewI, I); |
| 881 | *std::prev(NewI) |
Matthias Braun | 87a8605 | 2013-10-10 21:28:47 +0000 | [diff] [blame] | 882 | = LiveRange::Segment(DefVNI->def, NewIdx.getDeadSlot(), DefVNI); |
Jakob Stoklund Olesen | ad5e969 | 2012-10-12 21:31:57 +0000 | [diff] [blame] | 883 | } |
| 884 | |
Matthias Braun | 4f3b5e8 | 2013-10-10 21:29:02 +0000 | [diff] [blame] | 885 | /// Update LR to reflect an instruction has been moved upwards from OldIdx |
Jakob Stoklund Olesen | ad5e969 | 2012-10-12 21:31:57 +0000 | [diff] [blame] | 886 | /// to NewIdx. |
| 887 | /// |
| 888 | /// 1. Live def at OldIdx: |
| 889 | /// Hoist def to NewIdx. |
| 890 | /// |
| 891 | /// 2. Dead def at OldIdx: |
| 892 | /// Hoist def+end to NewIdx, possibly move across other values. |
| 893 | /// |
| 894 | /// 3. Dead def at OldIdx AND existing def at NewIdx: |
| 895 | /// Remove value defined at OldIdx, coalescing it with existing value. |
| 896 | /// |
| 897 | /// 4. Live def at OldIdx AND existing def at NewIdx: |
| 898 | /// Remove value defined at NewIdx, hoist OldIdx def to NewIdx. |
| 899 | /// (Happens when bundling multiple defs together). |
| 900 | /// |
| 901 | /// 5. Value killed at OldIdx: |
| 902 | /// Hoist kill to NewIdx, then scan for last kill between NewIdx and |
| 903 | /// OldIdx. |
| 904 | /// |
Matthias Braun | 4f3b5e8 | 2013-10-10 21:29:02 +0000 | [diff] [blame] | 905 | void handleMoveUp(LiveRange &LR, unsigned Reg) { |
Jakob Stoklund Olesen | ad5e969 | 2012-10-12 21:31:57 +0000 | [diff] [blame] | 906 | // First look for a kill at OldIdx. |
Matthias Braun | 4f3b5e8 | 2013-10-10 21:29:02 +0000 | [diff] [blame] | 907 | LiveRange::iterator I = LR.find(OldIdx.getBaseIndex()); |
| 908 | LiveRange::iterator E = LR.end(); |
| 909 | // Is LR even live at OldIdx? |
Jakob Stoklund Olesen | ad5e969 | 2012-10-12 21:31:57 +0000 | [diff] [blame] | 910 | if (I == E || SlotIndex::isEarlierInstr(OldIdx, I->start)) |
| 911 | return; |
| 912 | |
| 913 | // Handle a live-in value. |
| 914 | if (!SlotIndex::isSameInstr(I->start, OldIdx)) { |
| 915 | // If the live-in value isn't killed here, there is nothing to do. |
| 916 | if (!SlotIndex::isSameInstr(OldIdx, I->end)) |
| 917 | return; |
| 918 | // Adjust I->end to end at NewIdx. If we are hoisting a kill above |
| 919 | // another use, we need to search for that use. Case 5 above. |
| 920 | I->end = NewIdx.getRegSlot(I->end.isEarlyClobber()); |
| 921 | ++I; |
| 922 | // If OldIdx also defines a value, there couldn't have been another use. |
| 923 | if (I == E || !SlotIndex::isSameInstr(I->start, OldIdx)) { |
| 924 | // No def, search for the new kill. |
| 925 | // This can never be an early clobber kill since there is no def. |
Stephen Hines | 36b5688 | 2014-04-23 16:57:46 -0700 | [diff] [blame] | 926 | std::prev(I)->end = findLastUseBefore(Reg).getRegSlot(); |
Jakob Stoklund Olesen | ad5e969 | 2012-10-12 21:31:57 +0000 | [diff] [blame] | 927 | return; |
Lang Hames | 6aceab1 | 2012-02-19 07:13:05 +0000 | [diff] [blame] | 928 | } |
| 929 | } |
| 930 | |
Jakob Stoklund Olesen | ad5e969 | 2012-10-12 21:31:57 +0000 | [diff] [blame] | 931 | // Now deal with the def at OldIdx. |
| 932 | assert(I != E && SlotIndex::isSameInstr(I->start, OldIdx) && "No def?"); |
| 933 | VNInfo *DefVNI = I->valno; |
| 934 | assert(DefVNI->def == I->start && "Inconsistent def"); |
| 935 | DefVNI->def = NewIdx.getRegSlot(I->start.isEarlyClobber()); |
| 936 | |
| 937 | // Check for an existing def at NewIdx. |
Matthias Braun | 4f3b5e8 | 2013-10-10 21:29:02 +0000 | [diff] [blame] | 938 | LiveRange::iterator NewI = LR.find(NewIdx.getRegSlot()); |
Jakob Stoklund Olesen | ad5e969 | 2012-10-12 21:31:57 +0000 | [diff] [blame] | 939 | if (SlotIndex::isSameInstr(NewI->start, NewIdx)) { |
| 940 | assert(NewI->valno != DefVNI && "Same value defined more than once?"); |
| 941 | // There is an existing def at NewIdx. |
| 942 | if (I->end.isDead()) { |
| 943 | // Case 3: Remove the dead def at OldIdx. |
Matthias Braun | 4f3b5e8 | 2013-10-10 21:29:02 +0000 | [diff] [blame] | 944 | LR.removeValNo(DefVNI); |
Jakob Stoklund Olesen | ad5e969 | 2012-10-12 21:31:57 +0000 | [diff] [blame] | 945 | return; |
| 946 | } |
| 947 | // Case 4: Replace def at NewIdx with live def at OldIdx. |
| 948 | I->start = DefVNI->def; |
Matthias Braun | 4f3b5e8 | 2013-10-10 21:29:02 +0000 | [diff] [blame] | 949 | LR.removeValNo(NewI->valno); |
Jakob Stoklund Olesen | ad5e969 | 2012-10-12 21:31:57 +0000 | [diff] [blame] | 950 | return; |
Lang Hames | 6aceab1 | 2012-02-19 07:13:05 +0000 | [diff] [blame] | 951 | } |
| 952 | |
Jakob Stoklund Olesen | ad5e969 | 2012-10-12 21:31:57 +0000 | [diff] [blame] | 953 | // There is no existing def at NewIdx. Hoist DefVNI. |
| 954 | if (!I->end.isDead()) { |
| 955 | // Leave the end point of a live def. |
| 956 | I->start = DefVNI->def; |
| 957 | return; |
| 958 | } |
| 959 | |
Matthias Braun | 4f3b5e8 | 2013-10-10 21:29:02 +0000 | [diff] [blame] | 960 | // DefVNI is a dead def. It may have been moved across other values in LR, |
Jakob Stoklund Olesen | ad5e969 | 2012-10-12 21:31:57 +0000 | [diff] [blame] | 961 | // so move I up to NewI. Slide [NewI;I) down one position. |
Stephen Hines | 36b5688 | 2014-04-23 16:57:46 -0700 | [diff] [blame] | 962 | std::copy_backward(NewI, I, std::next(I)); |
Matthias Braun | 87a8605 | 2013-10-10 21:28:47 +0000 | [diff] [blame] | 963 | *NewI = LiveRange::Segment(DefVNI->def, NewIdx.getDeadSlot(), DefVNI); |
Lang Hames | 6aceab1 | 2012-02-19 07:13:05 +0000 | [diff] [blame] | 964 | } |
| 965 | |
Jakob Stoklund Olesen | ad5e969 | 2012-10-12 21:31:57 +0000 | [diff] [blame] | 966 | void updateRegMaskSlots() { |
Lang Hames | ecb5062 | 2012-02-17 23:43:40 +0000 | [diff] [blame] | 967 | SmallVectorImpl<SlotIndex>::iterator RI = |
| 968 | std::lower_bound(LIS.RegMaskSlots.begin(), LIS.RegMaskSlots.end(), |
| 969 | OldIdx); |
Jakob Stoklund Olesen | 722c9a7 | 2012-11-09 19:18:49 +0000 | [diff] [blame] | 970 | assert(RI != LIS.RegMaskSlots.end() && *RI == OldIdx.getRegSlot() && |
| 971 | "No RegMask at OldIdx."); |
| 972 | *RI = NewIdx.getRegSlot(); |
| 973 | assert((RI == LIS.RegMaskSlots.begin() || |
Stephen Hines | 36b5688 | 2014-04-23 16:57:46 -0700 | [diff] [blame] | 974 | SlotIndex::isEarlierInstr(*std::prev(RI), *RI)) && |
| 975 | "Cannot move regmask instruction above another call"); |
| 976 | assert((std::next(RI) == LIS.RegMaskSlots.end() || |
| 977 | SlotIndex::isEarlierInstr(*RI, *std::next(RI))) && |
| 978 | "Cannot move regmask instruction below another call"); |
Lang Hames | fbc8dd3 | 2012-02-17 21:29:41 +0000 | [diff] [blame] | 979 | } |
Lang Hames | 55fed62 | 2012-02-19 03:00:30 +0000 | [diff] [blame] | 980 | |
| 981 | // Return the last use of reg between NewIdx and OldIdx. |
Jakob Stoklund Olesen | ad5e969 | 2012-10-12 21:31:57 +0000 | [diff] [blame] | 982 | SlotIndex findLastUseBefore(unsigned Reg) { |
Lang Hames | 6d742cc | 2012-09-12 06:56:16 +0000 | [diff] [blame] | 983 | |
| 984 | if (TargetRegisterInfo::isVirtualRegister(Reg)) { |
Jakob Stoklund Olesen | 778ef97 | 2013-03-08 18:08:57 +0000 | [diff] [blame] | 985 | SlotIndex LastUse = NewIdx; |
Stephen Hines | 36b5688 | 2014-04-23 16:57:46 -0700 | [diff] [blame] | 986 | for (MachineRegisterInfo::use_instr_nodbg_iterator |
| 987 | UI = MRI.use_instr_nodbg_begin(Reg), |
| 988 | UE = MRI.use_instr_nodbg_end(); |
| 989 | UI != UE; ++UI) { |
Lang Hames | 6d742cc | 2012-09-12 06:56:16 +0000 | [diff] [blame] | 990 | const MachineInstr* MI = &*UI; |
| 991 | SlotIndex InstSlot = LIS.getSlotIndexes()->getInstructionIndex(MI); |
| 992 | if (InstSlot > LastUse && InstSlot < OldIdx) |
| 993 | LastUse = InstSlot; |
| 994 | } |
Jakob Stoklund Olesen | 778ef97 | 2013-03-08 18:08:57 +0000 | [diff] [blame] | 995 | return LastUse; |
Lang Hames | 55fed62 | 2012-02-19 03:00:30 +0000 | [diff] [blame] | 996 | } |
Jakob Stoklund Olesen | 778ef97 | 2013-03-08 18:08:57 +0000 | [diff] [blame] | 997 | |
| 998 | // This is a regunit interval, so scanning the use list could be very |
| 999 | // expensive. Scan upwards from OldIdx instead. |
| 1000 | assert(NewIdx < OldIdx && "Expected upwards move"); |
| 1001 | SlotIndexes *Indexes = LIS.getSlotIndexes(); |
| 1002 | MachineBasicBlock *MBB = Indexes->getMBBFromIndex(NewIdx); |
| 1003 | |
| 1004 | // OldIdx may not correspond to an instruction any longer, so set MII to |
| 1005 | // point to the next instruction after OldIdx, or MBB->end(). |
| 1006 | MachineBasicBlock::iterator MII = MBB->end(); |
| 1007 | if (MachineInstr *MI = Indexes->getInstructionFromIndex( |
| 1008 | Indexes->getNextNonNullIndex(OldIdx))) |
| 1009 | if (MI->getParent() == MBB) |
| 1010 | MII = MI; |
| 1011 | |
| 1012 | MachineBasicBlock::iterator Begin = MBB->begin(); |
| 1013 | while (MII != Begin) { |
| 1014 | if ((--MII)->isDebugValue()) |
| 1015 | continue; |
| 1016 | SlotIndex Idx = Indexes->getInstructionIndex(MII); |
| 1017 | |
| 1018 | // Stop searching when NewIdx is reached. |
| 1019 | if (!SlotIndex::isEarlierInstr(NewIdx, Idx)) |
| 1020 | return NewIdx; |
| 1021 | |
| 1022 | // Check if MII uses Reg. |
| 1023 | for (MIBundleOperands MO(MII); MO.isValid(); ++MO) |
| 1024 | if (MO->isReg() && |
| 1025 | TargetRegisterInfo::isPhysicalRegister(MO->getReg()) && |
| 1026 | TRI.hasRegUnit(MO->getReg(), Reg)) |
| 1027 | return Idx; |
| 1028 | } |
| 1029 | // Didn't reach NewIdx. It must be the first instruction in the block. |
| 1030 | return NewIdx; |
Lang Hames | 55fed62 | 2012-02-19 03:00:30 +0000 | [diff] [blame] | 1031 | } |
Lang Hames | 3dc7c51 | 2012-02-17 18:44:18 +0000 | [diff] [blame] | 1032 | }; |
| 1033 | |
Andrew Trick | 27c28ce | 2012-10-16 00:22:51 +0000 | [diff] [blame] | 1034 | void LiveIntervals::handleMove(MachineInstr* MI, bool UpdateFlags) { |
Jakob Stoklund Olesen | ad5e969 | 2012-10-12 21:31:57 +0000 | [diff] [blame] | 1035 | assert(!MI->isBundled() && "Can't handle bundled instructions yet."); |
Jakob Stoklund Olesen | 15f1d8c | 2012-06-04 22:39:14 +0000 | [diff] [blame] | 1036 | SlotIndex OldIndex = Indexes->getInstructionIndex(MI); |
| 1037 | Indexes->removeMachineInstrFromMaps(MI); |
Jakob Stoklund Olesen | ad5e969 | 2012-10-12 21:31:57 +0000 | [diff] [blame] | 1038 | SlotIndex NewIndex = Indexes->insertMachineInstrInMaps(MI); |
Lang Hames | ecb5062 | 2012-02-17 23:43:40 +0000 | [diff] [blame] | 1039 | assert(getMBBStartIdx(MI->getParent()) <= OldIndex && |
| 1040 | OldIndex < getMBBEndIdx(MI->getParent()) && |
Lang Hames | 3dc7c51 | 2012-02-17 18:44:18 +0000 | [diff] [blame] | 1041 | "Cannot handle moves across basic block boundaries."); |
Lang Hames | 3dc7c51 | 2012-02-17 18:44:18 +0000 | [diff] [blame] | 1042 | |
Andrew Trick | 27c28ce | 2012-10-16 00:22:51 +0000 | [diff] [blame] | 1043 | HMEditor HME(*this, *MRI, *TRI, OldIndex, NewIndex, UpdateFlags); |
Jakob Stoklund Olesen | ad5e969 | 2012-10-12 21:31:57 +0000 | [diff] [blame] | 1044 | HME.updateAllRanges(MI); |
Lang Hames | 4586d25 | 2012-02-21 22:29:38 +0000 | [diff] [blame] | 1045 | } |
| 1046 | |
Jakob Stoklund Olesen | fa8becb | 2012-06-19 22:50:53 +0000 | [diff] [blame] | 1047 | void LiveIntervals::handleMoveIntoBundle(MachineInstr* MI, |
Andrew Trick | 27c28ce | 2012-10-16 00:22:51 +0000 | [diff] [blame] | 1048 | MachineInstr* BundleStart, |
| 1049 | bool UpdateFlags) { |
Jakob Stoklund Olesen | ad5e969 | 2012-10-12 21:31:57 +0000 | [diff] [blame] | 1050 | SlotIndex OldIndex = Indexes->getInstructionIndex(MI); |
Jakob Stoklund Olesen | 15f1d8c | 2012-06-04 22:39:14 +0000 | [diff] [blame] | 1051 | SlotIndex NewIndex = Indexes->getInstructionIndex(BundleStart); |
Andrew Trick | 27c28ce | 2012-10-16 00:22:51 +0000 | [diff] [blame] | 1052 | HMEditor HME(*this, *MRI, *TRI, OldIndex, NewIndex, UpdateFlags); |
Jakob Stoklund Olesen | ad5e969 | 2012-10-12 21:31:57 +0000 | [diff] [blame] | 1053 | HME.updateAllRanges(MI); |
Lang Hames | 3dc7c51 | 2012-02-17 18:44:18 +0000 | [diff] [blame] | 1054 | } |
Cameron Zwarich | f0b2535 | 2013-02-17 00:10:44 +0000 | [diff] [blame] | 1055 | |
| 1056 | void |
| 1057 | LiveIntervals::repairIntervalsInRange(MachineBasicBlock *MBB, |
Cameron Zwarich | 680c98f | 2013-02-17 11:09:00 +0000 | [diff] [blame] | 1058 | MachineBasicBlock::iterator Begin, |
| 1059 | MachineBasicBlock::iterator End, |
Cameron Zwarich | 7324d4e | 2013-02-17 03:48:23 +0000 | [diff] [blame] | 1060 | ArrayRef<unsigned> OrigRegs) { |
Cameron Zwarich | c5b6135 | 2013-02-20 22:10:00 +0000 | [diff] [blame] | 1061 | // Find anchor points, which are at the beginning/end of blocks or at |
| 1062 | // instructions that already have indexes. |
| 1063 | while (Begin != MBB->begin() && !Indexes->hasIndex(Begin)) |
| 1064 | --Begin; |
| 1065 | while (End != MBB->end() && !Indexes->hasIndex(End)) |
| 1066 | ++End; |
| 1067 | |
Cameron Zwarich | 9030fc2 | 2013-02-20 06:46:48 +0000 | [diff] [blame] | 1068 | SlotIndex endIdx; |
| 1069 | if (End == MBB->end()) |
| 1070 | endIdx = getMBBEndIdx(MBB).getPrevSlot(); |
Cameron Zwarich | 680c98f | 2013-02-17 11:09:00 +0000 | [diff] [blame] | 1071 | else |
Cameron Zwarich | 9030fc2 | 2013-02-20 06:46:48 +0000 | [diff] [blame] | 1072 | endIdx = getInstructionIndex(End); |
Cameron Zwarich | 680c98f | 2013-02-17 11:09:00 +0000 | [diff] [blame] | 1073 | |
Cameron Zwarich | 349cf34 | 2013-02-20 06:46:41 +0000 | [diff] [blame] | 1074 | Indexes->repairIndexesInRange(MBB, Begin, End); |
| 1075 | |
Cameron Zwarich | 9030fc2 | 2013-02-20 06:46:48 +0000 | [diff] [blame] | 1076 | for (MachineBasicBlock::iterator I = End; I != Begin;) { |
| 1077 | --I; |
| 1078 | MachineInstr *MI = I; |
Cameron Zwarich | 79f5ab1 | 2013-02-23 10:25:25 +0000 | [diff] [blame] | 1079 | if (MI->isDebugValue()) |
| 1080 | continue; |
Cameron Zwarich | 9030fc2 | 2013-02-20 06:46:48 +0000 | [diff] [blame] | 1081 | for (MachineInstr::const_mop_iterator MOI = MI->operands_begin(), |
| 1082 | MOE = MI->operands_end(); MOI != MOE; ++MOI) { |
| 1083 | if (MOI->isReg() && |
| 1084 | TargetRegisterInfo::isVirtualRegister(MOI->getReg()) && |
| 1085 | !hasInterval(MOI->getReg())) { |
Mark Lacey | e742d68 | 2013-08-14 23:50:16 +0000 | [diff] [blame] | 1086 | createAndComputeVirtRegInterval(MOI->getReg()); |
Cameron Zwarich | 9030fc2 | 2013-02-20 06:46:48 +0000 | [diff] [blame] | 1087 | } |
| 1088 | } |
| 1089 | } |
| 1090 | |
Cameron Zwarich | f0b2535 | 2013-02-17 00:10:44 +0000 | [diff] [blame] | 1091 | for (unsigned i = 0, e = OrigRegs.size(); i != e; ++i) { |
| 1092 | unsigned Reg = OrigRegs[i]; |
| 1093 | if (!TargetRegisterInfo::isVirtualRegister(Reg)) |
| 1094 | continue; |
| 1095 | |
| 1096 | LiveInterval &LI = getInterval(Reg); |
Cameron Zwarich | 0e827eb | 2013-02-20 22:09:57 +0000 | [diff] [blame] | 1097 | // FIXME: Should we support undefs that gain defs? |
| 1098 | if (!LI.hasAtLeastOneValue()) |
| 1099 | continue; |
| 1100 | |
| 1101 | LiveInterval::iterator LII = LI.find(endIdx); |
| 1102 | SlotIndex lastUseIdx; |
| 1103 | if (LII != LI.end() && LII->start < endIdx) |
| 1104 | lastUseIdx = LII->end; |
| 1105 | else |
| 1106 | --LII; |
Cameron Zwarich | 9030fc2 | 2013-02-20 06:46:48 +0000 | [diff] [blame] | 1107 | |
Cameron Zwarich | 680c98f | 2013-02-17 11:09:00 +0000 | [diff] [blame] | 1108 | for (MachineBasicBlock::iterator I = End; I != Begin;) { |
| 1109 | --I; |
| 1110 | MachineInstr *MI = I; |
Cameron Zwarich | 79f5ab1 | 2013-02-23 10:25:25 +0000 | [diff] [blame] | 1111 | if (MI->isDebugValue()) |
| 1112 | continue; |
Cameron Zwarich | f0b2535 | 2013-02-17 00:10:44 +0000 | [diff] [blame] | 1113 | |
Cameron Zwarich | 79f5ab1 | 2013-02-23 10:25:25 +0000 | [diff] [blame] | 1114 | SlotIndex instrIdx = getInstructionIndex(MI); |
Cameron Zwarich | 0e827eb | 2013-02-20 22:09:57 +0000 | [diff] [blame] | 1115 | bool isStartValid = getInstructionFromIndex(LII->start); |
| 1116 | bool isEndValid = getInstructionFromIndex(LII->end); |
| 1117 | |
| 1118 | // FIXME: This doesn't currently handle early-clobber or multiple removed |
| 1119 | // defs inside of the region to repair. |
Cameron Zwarich | f0b2535 | 2013-02-17 00:10:44 +0000 | [diff] [blame] | 1120 | for (MachineInstr::mop_iterator OI = MI->operands_begin(), |
| 1121 | OE = MI->operands_end(); OI != OE; ++OI) { |
| 1122 | const MachineOperand &MO = *OI; |
| 1123 | if (!MO.isReg() || MO.getReg() != Reg) |
| 1124 | continue; |
| 1125 | |
Cameron Zwarich | 9030fc2 | 2013-02-20 06:46:48 +0000 | [diff] [blame] | 1126 | if (MO.isDef()) { |
Cameron Zwarich | 0e827eb | 2013-02-20 22:09:57 +0000 | [diff] [blame] | 1127 | if (!isStartValid) { |
| 1128 | if (LII->end.isDead()) { |
| 1129 | SlotIndex prevStart; |
| 1130 | if (LII != LI.begin()) |
Stephen Hines | 36b5688 | 2014-04-23 16:57:46 -0700 | [diff] [blame] | 1131 | prevStart = std::prev(LII)->start; |
Cameron Zwarich | 0e827eb | 2013-02-20 22:09:57 +0000 | [diff] [blame] | 1132 | |
Matthias Braun | 331de11 | 2013-10-10 21:28:43 +0000 | [diff] [blame] | 1133 | // FIXME: This could be more efficient if there was a |
| 1134 | // removeSegment method that returned an iterator. |
| 1135 | LI.removeSegment(*LII, true); |
Cameron Zwarich | 0e827eb | 2013-02-20 22:09:57 +0000 | [diff] [blame] | 1136 | if (prevStart.isValid()) |
| 1137 | LII = LI.find(prevStart); |
| 1138 | else |
| 1139 | LII = LI.begin(); |
| 1140 | } else { |
| 1141 | LII->start = instrIdx.getRegSlot(); |
| 1142 | LII->valno->def = instrIdx.getRegSlot(); |
| 1143 | if (MO.getSubReg() && !MO.isUndef()) |
| 1144 | lastUseIdx = instrIdx.getRegSlot(); |
| 1145 | else |
| 1146 | lastUseIdx = SlotIndex(); |
| 1147 | continue; |
| 1148 | } |
| 1149 | } |
| 1150 | |
| 1151 | if (!lastUseIdx.isValid()) { |
| 1152 | VNInfo *VNI = LI.getNextValue(instrIdx.getRegSlot(), |
| 1153 | VNInfoAllocator); |
Matthias Braun | 87a8605 | 2013-10-10 21:28:47 +0000 | [diff] [blame] | 1154 | LiveRange::Segment S(instrIdx.getRegSlot(), |
| 1155 | instrIdx.getDeadSlot(), VNI); |
Matthias Braun | 331de11 | 2013-10-10 21:28:43 +0000 | [diff] [blame] | 1156 | LII = LI.addSegment(S); |
Cameron Zwarich | 9030fc2 | 2013-02-20 06:46:48 +0000 | [diff] [blame] | 1157 | } else if (LII->start != instrIdx.getRegSlot()) { |
Cameron Zwarich | 0e827eb | 2013-02-20 22:09:57 +0000 | [diff] [blame] | 1158 | VNInfo *VNI = LI.getNextValue(instrIdx.getRegSlot(), |
| 1159 | VNInfoAllocator); |
Matthias Braun | 87a8605 | 2013-10-10 21:28:47 +0000 | [diff] [blame] | 1160 | LiveRange::Segment S(instrIdx.getRegSlot(), lastUseIdx, VNI); |
Matthias Braun | 331de11 | 2013-10-10 21:28:43 +0000 | [diff] [blame] | 1161 | LII = LI.addSegment(S); |
Cameron Zwarich | 9030fc2 | 2013-02-20 06:46:48 +0000 | [diff] [blame] | 1162 | } |
Cameron Zwarich | f0b2535 | 2013-02-17 00:10:44 +0000 | [diff] [blame] | 1163 | |
Cameron Zwarich | 0e827eb | 2013-02-20 22:09:57 +0000 | [diff] [blame] | 1164 | if (MO.getSubReg() && !MO.isUndef()) |
| 1165 | lastUseIdx = instrIdx.getRegSlot(); |
| 1166 | else |
| 1167 | lastUseIdx = SlotIndex(); |
| 1168 | } else if (MO.isUse()) { |
| 1169 | // FIXME: This should probably be handled outside of this branch, |
| 1170 | // either as part of the def case (for defs inside of the region) or |
| 1171 | // after the loop over the region. |
| 1172 | if (!isEndValid && !LII->end.isBlock()) |
Cameron Zwarich | 9030fc2 | 2013-02-20 06:46:48 +0000 | [diff] [blame] | 1173 | LII->end = instrIdx.getRegSlot(); |
Cameron Zwarich | 0e827eb | 2013-02-20 22:09:57 +0000 | [diff] [blame] | 1174 | if (!lastUseIdx.isValid()) |
| 1175 | lastUseIdx = instrIdx.getRegSlot(); |
Cameron Zwarich | f0b2535 | 2013-02-17 00:10:44 +0000 | [diff] [blame] | 1176 | } |
| 1177 | } |
| 1178 | } |
| 1179 | } |
| 1180 | } |