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