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