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