Jakob Stoklund Olesen | a818d80 | 2012-01-11 22:28:30 +0000 | [diff] [blame] | 1 | //===-- RegAllocBase.cpp - Register Allocator Base Class ------------------===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
Manman Ren | 2867140 | 2014-02-22 19:31:28 +0000 | [diff] [blame] | 10 | // This file defines the RegAllocBase class which provides common functionality |
Jakob Stoklund Olesen | a818d80 | 2012-01-11 22:28:30 +0000 | [diff] [blame] | 11 | // for LiveIntervalUnion-based register allocators. |
| 12 | // |
| 13 | //===----------------------------------------------------------------------===// |
| 14 | |
Jakob Stoklund Olesen | a818d80 | 2012-01-11 22:28:30 +0000 | [diff] [blame] | 15 | #include "RegAllocBase.h" |
Jakob Stoklund Olesen | a818d80 | 2012-01-11 22:28:30 +0000 | [diff] [blame] | 16 | #include "Spiller.h" |
Jakob Stoklund Olesen | a818d80 | 2012-01-11 22:28:30 +0000 | [diff] [blame] | 17 | #include "llvm/ADT/Statistic.h" |
| 18 | #include "llvm/CodeGen/LiveIntervalAnalysis.h" |
Pete Cooper | 3ca96f9 | 2012-04-02 22:44:18 +0000 | [diff] [blame] | 19 | #include "llvm/CodeGen/LiveRangeEdit.h" |
Jakob Stoklund Olesen | 26c9d70 | 2012-11-28 19:13:06 +0000 | [diff] [blame] | 20 | #include "llvm/CodeGen/LiveRegMatrix.h" |
Jakob Stoklund Olesen | a818d80 | 2012-01-11 22:28:30 +0000 | [diff] [blame] | 21 | #include "llvm/CodeGen/MachineInstr.h" |
| 22 | #include "llvm/CodeGen/MachineRegisterInfo.h" |
Jakob Stoklund Olesen | 26c9d70 | 2012-11-28 19:13:06 +0000 | [diff] [blame] | 23 | #include "llvm/CodeGen/VirtRegMap.h" |
Jakob Stoklund Olesen | a818d80 | 2012-01-11 22:28:30 +0000 | [diff] [blame] | 24 | #include "llvm/Target/TargetRegisterInfo.h" |
| 25 | #ifndef NDEBUG |
| 26 | #include "llvm/ADT/SparseBitVector.h" |
| 27 | #endif |
| 28 | #include "llvm/Support/CommandLine.h" |
| 29 | #include "llvm/Support/Debug.h" |
Benjamin Kramer | 799003b | 2015-03-23 19:32:43 +0000 | [diff] [blame] | 30 | #include "llvm/Support/raw_ostream.h" |
Jakob Stoklund Olesen | a818d80 | 2012-01-11 22:28:30 +0000 | [diff] [blame] | 31 | #include "llvm/Support/ErrorHandling.h" |
| 32 | #include "llvm/Support/raw_ostream.h" |
| 33 | #include "llvm/Support/Timer.h" |
| 34 | |
| 35 | using namespace llvm; |
| 36 | |
Chandler Carruth | 1b9dde0 | 2014-04-22 02:02:50 +0000 | [diff] [blame] | 37 | #define DEBUG_TYPE "regalloc" |
| 38 | |
Jakob Stoklund Olesen | a818d80 | 2012-01-11 22:28:30 +0000 | [diff] [blame] | 39 | STATISTIC(NumNewQueued , "Number of new live ranges queued"); |
| 40 | |
| 41 | // Temporary verification option until we can put verification inside |
| 42 | // MachineVerifier. |
| 43 | static cl::opt<bool, true> |
| 44 | VerifyRegAlloc("verify-regalloc", cl::location(RegAllocBase::VerifyEnabled), |
| 45 | cl::desc("Verify during register allocation")); |
| 46 | |
Craig Topper | 9fdc70e | 2013-07-17 03:11:32 +0000 | [diff] [blame] | 47 | const char RegAllocBase::TimerGroupName[] = "Register Allocation"; |
Jakob Stoklund Olesen | a818d80 | 2012-01-11 22:28:30 +0000 | [diff] [blame] | 48 | bool RegAllocBase::VerifyEnabled = false; |
| 49 | |
Jakob Stoklund Olesen | a818d80 | 2012-01-11 22:28:30 +0000 | [diff] [blame] | 50 | //===----------------------------------------------------------------------===// |
| 51 | // RegAllocBase Implementation |
| 52 | //===----------------------------------------------------------------------===// |
| 53 | |
Juergen Ributzka | d12ccbd | 2013-11-19 00:57:56 +0000 | [diff] [blame] | 54 | // Pin the vtable to this file. |
| 55 | void RegAllocBase::anchor() {} |
| 56 | |
Jakob Stoklund Olesen | 2d2dec9 | 2012-06-20 22:52:29 +0000 | [diff] [blame] | 57 | void RegAllocBase::init(VirtRegMap &vrm, |
| 58 | LiveIntervals &lis, |
| 59 | LiveRegMatrix &mat) { |
Jakob Stoklund Olesen | a818d80 | 2012-01-11 22:28:30 +0000 | [diff] [blame] | 60 | TRI = &vrm.getTargetRegInfo(); |
| 61 | MRI = &vrm.getRegInfo(); |
| 62 | VRM = &vrm; |
| 63 | LIS = &lis; |
Jakob Stoklund Olesen | 2d2dec9 | 2012-06-20 22:52:29 +0000 | [diff] [blame] | 64 | Matrix = &mat; |
Chad Rosier | ed119d5 | 2012-11-28 00:21:29 +0000 | [diff] [blame] | 65 | MRI->freezeReservedRegs(vrm.getMachineFunction()); |
Jakob Stoklund Olesen | a818d80 | 2012-01-11 22:28:30 +0000 | [diff] [blame] | 66 | RegClassInfo.runOnMachineFunction(vrm.getMachineFunction()); |
Jakob Stoklund Olesen | a818d80 | 2012-01-11 22:28:30 +0000 | [diff] [blame] | 67 | } |
| 68 | |
| 69 | // Visit all the live registers. If they are already assigned to a physical |
| 70 | // register, unify them with the corresponding LiveIntervalUnion, otherwise push |
| 71 | // them on the priority queue for later assignment. |
| 72 | void RegAllocBase::seedLiveRegs() { |
| 73 | NamedRegionTimer T("Seed Live Regs", TimerGroupName, TimePassesIsEnabled); |
Jakob Stoklund Olesen | a1f43dc | 2012-06-20 21:25:05 +0000 | [diff] [blame] | 74 | for (unsigned i = 0, e = MRI->getNumVirtRegs(); i != e; ++i) { |
| 75 | unsigned Reg = TargetRegisterInfo::index2VirtReg(i); |
| 76 | if (MRI->reg_nodbg_empty(Reg)) |
| 77 | continue; |
| 78 | enqueue(&LIS->getInterval(Reg)); |
Jakob Stoklund Olesen | a818d80 | 2012-01-11 22:28:30 +0000 | [diff] [blame] | 79 | } |
| 80 | } |
| 81 | |
Jakob Stoklund Olesen | a818d80 | 2012-01-11 22:28:30 +0000 | [diff] [blame] | 82 | // Top-level driver to manage the queue of unassigned VirtRegs and call the |
| 83 | // selectOrSplit implementation. |
| 84 | void RegAllocBase::allocatePhysRegs() { |
| 85 | seedLiveRegs(); |
| 86 | |
| 87 | // Continue assigning vregs one at a time to available physical registers. |
| 88 | while (LiveInterval *VirtReg = dequeue()) { |
| 89 | assert(!VRM->hasPhys(VirtReg->reg) && "Register already assigned"); |
| 90 | |
| 91 | // Unused registers can appear when the spiller coalesces snippets. |
| 92 | if (MRI->reg_nodbg_empty(VirtReg->reg)) { |
| 93 | DEBUG(dbgs() << "Dropping unused " << *VirtReg << '\n'); |
Quentin Colombet | a799e2e | 2015-01-08 01:16:39 +0000 | [diff] [blame] | 94 | aboutToRemoveInterval(*VirtReg); |
Jakob Stoklund Olesen | a818d80 | 2012-01-11 22:28:30 +0000 | [diff] [blame] | 95 | LIS->removeInterval(VirtReg->reg); |
| 96 | continue; |
| 97 | } |
| 98 | |
| 99 | // Invalidate all interference queries, live ranges could have changed. |
Jakob Stoklund Olesen | 2d2dec9 | 2012-06-20 22:52:29 +0000 | [diff] [blame] | 100 | Matrix->invalidateVirtRegs(); |
Jakob Stoklund Olesen | a818d80 | 2012-01-11 22:28:30 +0000 | [diff] [blame] | 101 | |
| 102 | // selectOrSplit requests the allocator to return an available physical |
| 103 | // register if possible and populate a list of new live intervals that |
| 104 | // result from splitting. |
| 105 | DEBUG(dbgs() << "\nselectOrSplit " |
Craig Topper | cf0444b | 2014-11-17 05:50:14 +0000 | [diff] [blame] | 106 | << TRI->getRegClassName(MRI->getRegClass(VirtReg->reg)) |
Andrew Trick | 059e800 | 2013-11-22 19:07:42 +0000 | [diff] [blame] | 107 | << ':' << *VirtReg << " w=" << VirtReg->weight << '\n'); |
Mark Lacey | f9ea885 | 2013-08-14 23:50:04 +0000 | [diff] [blame] | 108 | typedef SmallVector<unsigned, 4> VirtRegVec; |
Jakob Stoklund Olesen | a818d80 | 2012-01-11 22:28:30 +0000 | [diff] [blame] | 109 | VirtRegVec SplitVRegs; |
| 110 | unsigned AvailablePhysReg = selectOrSplit(*VirtReg, SplitVRegs); |
| 111 | |
| 112 | if (AvailablePhysReg == ~0u) { |
| 113 | // selectOrSplit failed to find a register! |
Jakob Stoklund Olesen | a818d80 | 2012-01-11 22:28:30 +0000 | [diff] [blame] | 114 | // Probably caused by an inline asm. |
Craig Topper | c0196b1 | 2014-04-14 00:51:57 +0000 | [diff] [blame] | 115 | MachineInstr *MI = nullptr; |
Owen Anderson | abb90c9 | 2014-03-13 06:02:25 +0000 | [diff] [blame] | 116 | for (MachineRegisterInfo::reg_instr_iterator |
| 117 | I = MRI->reg_instr_begin(VirtReg->reg), E = MRI->reg_instr_end(); |
| 118 | I != E; ) { |
| 119 | MachineInstr *TmpMI = &*(I++); |
| 120 | if (TmpMI->isInlineAsm()) { |
| 121 | MI = TmpMI; |
Jakob Stoklund Olesen | a818d80 | 2012-01-11 22:28:30 +0000 | [diff] [blame] | 122 | break; |
Owen Anderson | abb90c9 | 2014-03-13 06:02:25 +0000 | [diff] [blame] | 123 | } |
| 124 | } |
Jakob Stoklund Olesen | a818d80 | 2012-01-11 22:28:30 +0000 | [diff] [blame] | 125 | if (MI) |
Benjamin Kramer | 7200a46 | 2013-10-05 19:33:37 +0000 | [diff] [blame] | 126 | MI->emitError("inline assembly requires more registers than available"); |
Jakob Stoklund Olesen | a818d80 | 2012-01-11 22:28:30 +0000 | [diff] [blame] | 127 | else |
Benjamin Kramer | 7200a46 | 2013-10-05 19:33:37 +0000 | [diff] [blame] | 128 | report_fatal_error("ran out of registers during register allocation"); |
Jakob Stoklund Olesen | a818d80 | 2012-01-11 22:28:30 +0000 | [diff] [blame] | 129 | // Keep going after reporting the error. |
| 130 | VRM->assignVirt2Phys(VirtReg->reg, |
| 131 | RegClassInfo.getOrder(MRI->getRegClass(VirtReg->reg)).front()); |
| 132 | continue; |
| 133 | } |
| 134 | |
| 135 | if (AvailablePhysReg) |
Jakob Stoklund Olesen | 2d2dec9 | 2012-06-20 22:52:29 +0000 | [diff] [blame] | 136 | Matrix->assign(*VirtReg, AvailablePhysReg); |
Jakob Stoklund Olesen | a818d80 | 2012-01-11 22:28:30 +0000 | [diff] [blame] | 137 | |
| 138 | for (VirtRegVec::iterator I = SplitVRegs.begin(), E = SplitVRegs.end(); |
| 139 | I != E; ++I) { |
Mark Lacey | f9ea885 | 2013-08-14 23:50:04 +0000 | [diff] [blame] | 140 | LiveInterval *SplitVirtReg = &LIS->getInterval(*I); |
Jakob Stoklund Olesen | a818d80 | 2012-01-11 22:28:30 +0000 | [diff] [blame] | 141 | assert(!VRM->hasPhys(SplitVirtReg->reg) && "Register already assigned"); |
| 142 | if (MRI->reg_nodbg_empty(SplitVirtReg->reg)) { |
| 143 | DEBUG(dbgs() << "not queueing unused " << *SplitVirtReg << '\n'); |
Quentin Colombet | a799e2e | 2015-01-08 01:16:39 +0000 | [diff] [blame] | 144 | aboutToRemoveInterval(*SplitVirtReg); |
Jakob Stoklund Olesen | a818d80 | 2012-01-11 22:28:30 +0000 | [diff] [blame] | 145 | LIS->removeInterval(SplitVirtReg->reg); |
| 146 | continue; |
| 147 | } |
| 148 | DEBUG(dbgs() << "queuing new interval: " << *SplitVirtReg << "\n"); |
| 149 | assert(TargetRegisterInfo::isVirtualRegister(SplitVirtReg->reg) && |
| 150 | "expect split value in virtual register"); |
| 151 | enqueue(SplitVirtReg); |
| 152 | ++NumNewQueued; |
| 153 | } |
| 154 | } |
| 155 | } |