Jakob Stoklund Olesen | ccc9581 | 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 | // |
| 10 | // This file defines the RegAllocBase class which provides comon functionality |
| 11 | // for LiveIntervalUnion-based register allocators. |
| 12 | // |
| 13 | //===----------------------------------------------------------------------===// |
| 14 | |
| 15 | #define DEBUG_TYPE "regalloc" |
| 16 | #include "RegAllocBase.h" |
Jakob Stoklund Olesen | ccc9581 | 2012-01-11 22:28:30 +0000 | [diff] [blame] | 17 | #include "Spiller.h" |
| 18 | #include "VirtRegMap.h" |
| 19 | #include "llvm/ADT/Statistic.h" |
| 20 | #include "llvm/CodeGen/LiveIntervalAnalysis.h" |
Pete Cooper | 789d5d8 | 2012-04-02 22:44:18 +0000 | [diff] [blame] | 21 | #include "llvm/CodeGen/LiveRangeEdit.h" |
Jakob Stoklund Olesen | ccc9581 | 2012-01-11 22:28:30 +0000 | [diff] [blame] | 22 | #include "llvm/CodeGen/MachineInstr.h" |
| 23 | #include "llvm/CodeGen/MachineRegisterInfo.h" |
| 24 | #include "llvm/Target/TargetMachine.h" |
| 25 | #include "llvm/Target/TargetRegisterInfo.h" |
| 26 | #ifndef NDEBUG |
| 27 | #include "llvm/ADT/SparseBitVector.h" |
| 28 | #endif |
| 29 | #include "llvm/Support/CommandLine.h" |
| 30 | #include "llvm/Support/Debug.h" |
| 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 | |
| 37 | STATISTIC(NumAssigned , "Number of registers assigned"); |
| 38 | STATISTIC(NumUnassigned , "Number of registers unassigned"); |
| 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 | |
| 47 | const char *RegAllocBase::TimerGroupName = "Register Allocation"; |
| 48 | bool RegAllocBase::VerifyEnabled = false; |
| 49 | |
| 50 | #ifndef NDEBUG |
| 51 | // Verify each LiveIntervalUnion. |
| 52 | void RegAllocBase::verify() { |
| 53 | LiveVirtRegBitSet VisitedVRegs; |
| 54 | OwningArrayPtr<LiveVirtRegBitSet> |
Jakob Stoklund Olesen | 0e5a60b | 2012-06-05 23:57:30 +0000 | [diff] [blame] | 55 | unionVRegs(new LiveVirtRegBitSet[TRI->getNumRegs()]); |
Jakob Stoklund Olesen | ccc9581 | 2012-01-11 22:28:30 +0000 | [diff] [blame] | 56 | |
| 57 | // Verify disjoint unions. |
Jakob Stoklund Olesen | 0e5a60b | 2012-06-05 23:57:30 +0000 | [diff] [blame] | 58 | for (unsigned PhysReg = 0, NumRegs = TRI->getNumRegs(); PhysReg != NumRegs; |
| 59 | ++PhysReg) { |
Jakob Stoklund Olesen | ccc9581 | 2012-01-11 22:28:30 +0000 | [diff] [blame] | 60 | DEBUG(PhysReg2LiveUnion[PhysReg].print(dbgs(), TRI)); |
| 61 | LiveVirtRegBitSet &VRegs = unionVRegs[PhysReg]; |
| 62 | PhysReg2LiveUnion[PhysReg].verify(VRegs); |
| 63 | // Union + intersection test could be done efficiently in one pass, but |
| 64 | // don't add a method to SparseBitVector unless we really need it. |
| 65 | assert(!VisitedVRegs.intersects(VRegs) && "vreg in multiple unions"); |
| 66 | VisitedVRegs |= VRegs; |
| 67 | } |
| 68 | |
| 69 | // Verify vreg coverage. |
Jakob Stoklund Olesen | d67582e | 2012-06-20 21:25:05 +0000 | [diff] [blame^] | 70 | for (unsigned i = 0, e = MRI->getNumVirtRegs(); i != e; ++i) { |
| 71 | unsigned Reg = TargetRegisterInfo::index2VirtReg(i); |
| 72 | if (MRI->reg_nodbg_empty(Reg)) |
| 73 | continue; |
| 74 | if (!VRM->hasPhys(Reg)) continue; // spilled? |
| 75 | LiveInterval &LI = LIS->getInterval(Reg); |
| 76 | if (LI.empty()) continue; // unionVRegs will only be filled if li is |
| 77 | // non-empty |
| 78 | unsigned PhysReg = VRM->getPhys(Reg); |
| 79 | if (!unionVRegs[PhysReg].test(Reg)) { |
| 80 | dbgs() << "LiveVirtReg " << PrintReg(Reg, TRI) << " not in union " |
| 81 | << TRI->getName(PhysReg) << "\n"; |
Jakob Stoklund Olesen | ccc9581 | 2012-01-11 22:28:30 +0000 | [diff] [blame] | 82 | llvm_unreachable("unallocated live vreg"); |
| 83 | } |
| 84 | } |
| 85 | // FIXME: I'm not sure how to verify spilled intervals. |
| 86 | } |
| 87 | #endif //!NDEBUG |
| 88 | |
| 89 | //===----------------------------------------------------------------------===// |
| 90 | // RegAllocBase Implementation |
| 91 | //===----------------------------------------------------------------------===// |
| 92 | |
Jakob Stoklund Olesen | ccc9581 | 2012-01-11 22:28:30 +0000 | [diff] [blame] | 93 | void RegAllocBase::init(VirtRegMap &vrm, LiveIntervals &lis) { |
| 94 | NamedRegionTimer T("Initialize", TimerGroupName, TimePassesIsEnabled); |
| 95 | TRI = &vrm.getTargetRegInfo(); |
| 96 | MRI = &vrm.getRegInfo(); |
| 97 | VRM = &vrm; |
| 98 | LIS = &lis; |
| 99 | MRI->freezeReservedRegs(vrm.getMachineFunction()); |
| 100 | RegClassInfo.runOnMachineFunction(vrm.getMachineFunction()); |
| 101 | |
| 102 | const unsigned NumRegs = TRI->getNumRegs(); |
Jakob Stoklund Olesen | 0e5a60b | 2012-06-05 23:57:30 +0000 | [diff] [blame] | 103 | if (NumRegs != PhysReg2LiveUnion.size()) { |
Jakob Stoklund Olesen | ccc9581 | 2012-01-11 22:28:30 +0000 | [diff] [blame] | 104 | PhysReg2LiveUnion.init(UnionAllocator, NumRegs); |
| 105 | // Cache an interferece query for each physical reg |
Jakob Stoklund Olesen | 0e5a60b | 2012-06-05 23:57:30 +0000 | [diff] [blame] | 106 | Queries.reset(new LiveIntervalUnion::Query[NumRegs]); |
Jakob Stoklund Olesen | ccc9581 | 2012-01-11 22:28:30 +0000 | [diff] [blame] | 107 | } |
| 108 | } |
| 109 | |
Jakob Stoklund Olesen | ccc9581 | 2012-01-11 22:28:30 +0000 | [diff] [blame] | 110 | void RegAllocBase::releaseMemory() { |
Jakob Stoklund Olesen | 0e5a60b | 2012-06-05 23:57:30 +0000 | [diff] [blame] | 111 | for (unsigned r = 0, e = PhysReg2LiveUnion.size(); r != e; ++r) |
Jakob Stoklund Olesen | ccc9581 | 2012-01-11 22:28:30 +0000 | [diff] [blame] | 112 | PhysReg2LiveUnion[r].clear(); |
| 113 | } |
| 114 | |
| 115 | // Visit all the live registers. If they are already assigned to a physical |
| 116 | // register, unify them with the corresponding LiveIntervalUnion, otherwise push |
| 117 | // them on the priority queue for later assignment. |
| 118 | void RegAllocBase::seedLiveRegs() { |
| 119 | NamedRegionTimer T("Seed Live Regs", TimerGroupName, TimePassesIsEnabled); |
Jakob Stoklund Olesen | d67582e | 2012-06-20 21:25:05 +0000 | [diff] [blame^] | 120 | // Physregs. |
| 121 | for (unsigned Reg = 1, e = TRI->getNumRegs(); Reg != e; ++Reg) { |
| 122 | if (!LIS->hasInterval(Reg)) |
| 123 | continue; |
| 124 | PhysReg2LiveUnion[Reg].unify(LIS->getInterval(Reg)); |
| 125 | } |
| 126 | |
| 127 | // Virtregs. |
| 128 | for (unsigned i = 0, e = MRI->getNumVirtRegs(); i != e; ++i) { |
| 129 | unsigned Reg = TargetRegisterInfo::index2VirtReg(i); |
| 130 | if (MRI->reg_nodbg_empty(Reg)) |
| 131 | continue; |
| 132 | enqueue(&LIS->getInterval(Reg)); |
Jakob Stoklund Olesen | ccc9581 | 2012-01-11 22:28:30 +0000 | [diff] [blame] | 133 | } |
| 134 | } |
| 135 | |
| 136 | void RegAllocBase::assign(LiveInterval &VirtReg, unsigned PhysReg) { |
| 137 | DEBUG(dbgs() << "assigning " << PrintReg(VirtReg.reg, TRI) |
| 138 | << " to " << PrintReg(PhysReg, TRI) << '\n'); |
| 139 | assert(!VRM->hasPhys(VirtReg.reg) && "Duplicate VirtReg assignment"); |
| 140 | VRM->assignVirt2Phys(VirtReg.reg, PhysReg); |
| 141 | MRI->setPhysRegUsed(PhysReg); |
| 142 | PhysReg2LiveUnion[PhysReg].unify(VirtReg); |
| 143 | ++NumAssigned; |
| 144 | } |
| 145 | |
| 146 | void RegAllocBase::unassign(LiveInterval &VirtReg, unsigned PhysReg) { |
| 147 | DEBUG(dbgs() << "unassigning " << PrintReg(VirtReg.reg, TRI) |
| 148 | << " from " << PrintReg(PhysReg, TRI) << '\n'); |
| 149 | assert(VRM->getPhys(VirtReg.reg) == PhysReg && "Inconsistent unassign"); |
| 150 | PhysReg2LiveUnion[PhysReg].extract(VirtReg); |
| 151 | VRM->clearVirt(VirtReg.reg); |
| 152 | ++NumUnassigned; |
| 153 | } |
| 154 | |
| 155 | // Top-level driver to manage the queue of unassigned VirtRegs and call the |
| 156 | // selectOrSplit implementation. |
| 157 | void RegAllocBase::allocatePhysRegs() { |
| 158 | seedLiveRegs(); |
| 159 | |
| 160 | // Continue assigning vregs one at a time to available physical registers. |
| 161 | while (LiveInterval *VirtReg = dequeue()) { |
| 162 | assert(!VRM->hasPhys(VirtReg->reg) && "Register already assigned"); |
| 163 | |
| 164 | // Unused registers can appear when the spiller coalesces snippets. |
| 165 | if (MRI->reg_nodbg_empty(VirtReg->reg)) { |
| 166 | DEBUG(dbgs() << "Dropping unused " << *VirtReg << '\n'); |
| 167 | LIS->removeInterval(VirtReg->reg); |
| 168 | continue; |
| 169 | } |
| 170 | |
| 171 | // Invalidate all interference queries, live ranges could have changed. |
| 172 | invalidateVirtRegs(); |
| 173 | |
| 174 | // selectOrSplit requests the allocator to return an available physical |
| 175 | // register if possible and populate a list of new live intervals that |
| 176 | // result from splitting. |
| 177 | DEBUG(dbgs() << "\nselectOrSplit " |
| 178 | << MRI->getRegClass(VirtReg->reg)->getName() |
Jakob Stoklund Olesen | b77ec7d | 2012-06-05 22:51:54 +0000 | [diff] [blame] | 179 | << ':' << PrintReg(VirtReg->reg) << ' ' << *VirtReg << '\n'); |
Jakob Stoklund Olesen | ccc9581 | 2012-01-11 22:28:30 +0000 | [diff] [blame] | 180 | typedef SmallVector<LiveInterval*, 4> VirtRegVec; |
| 181 | VirtRegVec SplitVRegs; |
| 182 | unsigned AvailablePhysReg = selectOrSplit(*VirtReg, SplitVRegs); |
| 183 | |
| 184 | if (AvailablePhysReg == ~0u) { |
| 185 | // selectOrSplit failed to find a register! |
| 186 | const char *Msg = "ran out of registers during register allocation"; |
| 187 | // Probably caused by an inline asm. |
| 188 | MachineInstr *MI; |
| 189 | for (MachineRegisterInfo::reg_iterator I = MRI->reg_begin(VirtReg->reg); |
| 190 | (MI = I.skipInstruction());) |
| 191 | if (MI->isInlineAsm()) |
| 192 | break; |
| 193 | if (MI) |
| 194 | MI->emitError(Msg); |
| 195 | else |
| 196 | report_fatal_error(Msg); |
| 197 | // Keep going after reporting the error. |
| 198 | VRM->assignVirt2Phys(VirtReg->reg, |
| 199 | RegClassInfo.getOrder(MRI->getRegClass(VirtReg->reg)).front()); |
| 200 | continue; |
| 201 | } |
| 202 | |
| 203 | if (AvailablePhysReg) |
| 204 | assign(*VirtReg, AvailablePhysReg); |
| 205 | |
| 206 | for (VirtRegVec::iterator I = SplitVRegs.begin(), E = SplitVRegs.end(); |
| 207 | I != E; ++I) { |
| 208 | LiveInterval *SplitVirtReg = *I; |
| 209 | assert(!VRM->hasPhys(SplitVirtReg->reg) && "Register already assigned"); |
| 210 | if (MRI->reg_nodbg_empty(SplitVirtReg->reg)) { |
| 211 | DEBUG(dbgs() << "not queueing unused " << *SplitVirtReg << '\n'); |
| 212 | LIS->removeInterval(SplitVirtReg->reg); |
| 213 | continue; |
| 214 | } |
| 215 | DEBUG(dbgs() << "queuing new interval: " << *SplitVirtReg << "\n"); |
| 216 | assert(TargetRegisterInfo::isVirtualRegister(SplitVirtReg->reg) && |
| 217 | "expect split value in virtual register"); |
| 218 | enqueue(SplitVirtReg); |
| 219 | ++NumNewQueued; |
| 220 | } |
| 221 | } |
| 222 | } |
| 223 | |
| 224 | // Check if this live virtual register interferes with a physical register. If |
| 225 | // not, then check for interference on each register that aliases with the |
| 226 | // physical register. Return the interfering register. |
| 227 | unsigned RegAllocBase::checkPhysRegInterference(LiveInterval &VirtReg, |
| 228 | unsigned PhysReg) { |
Jakob Stoklund Olesen | 396618b | 2012-06-01 23:28:30 +0000 | [diff] [blame] | 229 | for (MCRegAliasIterator AI(PhysReg, TRI, true); AI.isValid(); ++AI) |
| 230 | if (query(VirtReg, *AI).checkInterference()) |
| 231 | return *AI; |
Jakob Stoklund Olesen | ccc9581 | 2012-01-11 22:28:30 +0000 | [diff] [blame] | 232 | return 0; |
| 233 | } |