blob: 894aee7a9917024b9fbba90585fe5fc1e8cef738 [file] [log] [blame]
Jakob Stoklund Olesenccc95812012-01-11 22:28:30 +00001//===-- 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//
Stephen Hines36b56882014-04-23 16:57:46 -070010// This file defines the RegAllocBase class which provides common functionality
Jakob Stoklund Olesenccc95812012-01-11 22:28:30 +000011// for LiveIntervalUnion-based register allocators.
12//
13//===----------------------------------------------------------------------===//
14
Jakob Stoklund Olesenccc95812012-01-11 22:28:30 +000015#include "RegAllocBase.h"
Jakob Stoklund Olesenccc95812012-01-11 22:28:30 +000016#include "Spiller.h"
Jakob Stoklund Olesenccc95812012-01-11 22:28:30 +000017#include "llvm/ADT/Statistic.h"
18#include "llvm/CodeGen/LiveIntervalAnalysis.h"
Pete Cooper789d5d82012-04-02 22:44:18 +000019#include "llvm/CodeGen/LiveRangeEdit.h"
Jakob Stoklund Olesen1ead68d2012-11-28 19:13:06 +000020#include "llvm/CodeGen/LiveRegMatrix.h"
Jakob Stoklund Olesenccc95812012-01-11 22:28:30 +000021#include "llvm/CodeGen/MachineInstr.h"
22#include "llvm/CodeGen/MachineRegisterInfo.h"
Jakob Stoklund Olesen1ead68d2012-11-28 19:13:06 +000023#include "llvm/CodeGen/VirtRegMap.h"
Jakob Stoklund Olesenccc95812012-01-11 22:28:30 +000024#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
35using namespace llvm;
36
Stephen Hinesdce4a402014-05-29 02:49:00 -070037#define DEBUG_TYPE "regalloc"
38
Jakob Stoklund Olesenccc95812012-01-11 22:28:30 +000039STATISTIC(NumNewQueued , "Number of new live ranges queued");
40
41// Temporary verification option until we can put verification inside
42// MachineVerifier.
43static cl::opt<bool, true>
44VerifyRegAlloc("verify-regalloc", cl::location(RegAllocBase::VerifyEnabled),
45 cl::desc("Verify during register allocation"));
46
Craig Topper8de25f02013-07-17 03:11:32 +000047const char RegAllocBase::TimerGroupName[] = "Register Allocation";
Jakob Stoklund Olesenccc95812012-01-11 22:28:30 +000048bool RegAllocBase::VerifyEnabled = false;
49
Jakob Stoklund Olesenccc95812012-01-11 22:28:30 +000050//===----------------------------------------------------------------------===//
51// RegAllocBase Implementation
52//===----------------------------------------------------------------------===//
53
Juergen Ributzka35436252013-11-19 00:57:56 +000054// Pin the vtable to this file.
55void RegAllocBase::anchor() {}
56
Jakob Stoklund Olesend4348a22012-06-20 22:52:29 +000057void RegAllocBase::init(VirtRegMap &vrm,
58 LiveIntervals &lis,
59 LiveRegMatrix &mat) {
Jakob Stoklund Olesenccc95812012-01-11 22:28:30 +000060 TRI = &vrm.getTargetRegInfo();
61 MRI = &vrm.getRegInfo();
62 VRM = &vrm;
63 LIS = &lis;
Jakob Stoklund Olesend4348a22012-06-20 22:52:29 +000064 Matrix = &mat;
Chad Rosier18bb0542012-11-28 00:21:29 +000065 MRI->freezeReservedRegs(vrm.getMachineFunction());
Jakob Stoklund Olesenccc95812012-01-11 22:28:30 +000066 RegClassInfo.runOnMachineFunction(vrm.getMachineFunction());
Jakob Stoklund Olesenccc95812012-01-11 22:28:30 +000067}
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.
72void RegAllocBase::seedLiveRegs() {
73 NamedRegionTimer T("Seed Live Regs", TimerGroupName, TimePassesIsEnabled);
Jakob Stoklund Olesend67582e2012-06-20 21:25:05 +000074 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 Olesenccc95812012-01-11 22:28:30 +000079 }
80}
81
Jakob Stoklund Olesenccc95812012-01-11 22:28:30 +000082// Top-level driver to manage the queue of unassigned VirtRegs and call the
83// selectOrSplit implementation.
84void 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');
94 LIS->removeInterval(VirtReg->reg);
95 continue;
96 }
97
98 // Invalidate all interference queries, live ranges could have changed.
Jakob Stoklund Olesend4348a22012-06-20 22:52:29 +000099 Matrix->invalidateVirtRegs();
Jakob Stoklund Olesenccc95812012-01-11 22:28:30 +0000100
101 // selectOrSplit requests the allocator to return an available physical
102 // register if possible and populate a list of new live intervals that
103 // result from splitting.
104 DEBUG(dbgs() << "\nselectOrSplit "
Stephen Hines36b56882014-04-23 16:57:46 -0700105 << MRI->getRegClass(VirtReg->reg)->getName()
106 << ':' << *VirtReg << " w=" << VirtReg->weight << '\n');
Mark Lacey1feb5852013-08-14 23:50:04 +0000107 typedef SmallVector<unsigned, 4> VirtRegVec;
Jakob Stoklund Olesenccc95812012-01-11 22:28:30 +0000108 VirtRegVec SplitVRegs;
109 unsigned AvailablePhysReg = selectOrSplit(*VirtReg, SplitVRegs);
110
111 if (AvailablePhysReg == ~0u) {
112 // selectOrSplit failed to find a register!
Jakob Stoklund Olesenccc95812012-01-11 22:28:30 +0000113 // Probably caused by an inline asm.
Stephen Hinesdce4a402014-05-29 02:49:00 -0700114 MachineInstr *MI = nullptr;
Stephen Hines36b56882014-04-23 16:57:46 -0700115 for (MachineRegisterInfo::reg_instr_iterator
116 I = MRI->reg_instr_begin(VirtReg->reg), E = MRI->reg_instr_end();
117 I != E; ) {
118 MachineInstr *TmpMI = &*(I++);
119 if (TmpMI->isInlineAsm()) {
120 MI = TmpMI;
Jakob Stoklund Olesenccc95812012-01-11 22:28:30 +0000121 break;
Stephen Hines36b56882014-04-23 16:57:46 -0700122 }
123 }
Jakob Stoklund Olesenccc95812012-01-11 22:28:30 +0000124 if (MI)
Benjamin Kramer87855d32013-10-05 19:33:37 +0000125 MI->emitError("inline assembly requires more registers than available");
Jakob Stoklund Olesenccc95812012-01-11 22:28:30 +0000126 else
Benjamin Kramer87855d32013-10-05 19:33:37 +0000127 report_fatal_error("ran out of registers during register allocation");
Jakob Stoklund Olesenccc95812012-01-11 22:28:30 +0000128 // Keep going after reporting the error.
129 VRM->assignVirt2Phys(VirtReg->reg,
130 RegClassInfo.getOrder(MRI->getRegClass(VirtReg->reg)).front());
131 continue;
132 }
133
134 if (AvailablePhysReg)
Jakob Stoklund Olesend4348a22012-06-20 22:52:29 +0000135 Matrix->assign(*VirtReg, AvailablePhysReg);
Jakob Stoklund Olesenccc95812012-01-11 22:28:30 +0000136
137 for (VirtRegVec::iterator I = SplitVRegs.begin(), E = SplitVRegs.end();
138 I != E; ++I) {
Mark Lacey1feb5852013-08-14 23:50:04 +0000139 LiveInterval *SplitVirtReg = &LIS->getInterval(*I);
Jakob Stoklund Olesenccc95812012-01-11 22:28:30 +0000140 assert(!VRM->hasPhys(SplitVirtReg->reg) && "Register already assigned");
141 if (MRI->reg_nodbg_empty(SplitVirtReg->reg)) {
142 DEBUG(dbgs() << "not queueing unused " << *SplitVirtReg << '\n');
143 LIS->removeInterval(SplitVirtReg->reg);
144 continue;
145 }
146 DEBUG(dbgs() << "queuing new interval: " << *SplitVirtReg << "\n");
147 assert(TargetRegisterInfo::isVirtualRegister(SplitVirtReg->reg) &&
148 "expect split value in virtual register");
149 enqueue(SplitVirtReg);
150 ++NumNewQueued;
151 }
152 }
153}