blob: 33584f868060f1e82b0f3d1024369b2e0d161886 [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
15#define DEBUG_TYPE "regalloc"
16#include "RegAllocBase.h"
Jakob Stoklund Olesenccc95812012-01-11 22:28:30 +000017#include "Spiller.h"
Jakob Stoklund Olesenccc95812012-01-11 22:28:30 +000018#include "llvm/ADT/Statistic.h"
19#include "llvm/CodeGen/LiveIntervalAnalysis.h"
Pete Cooper789d5d82012-04-02 22:44:18 +000020#include "llvm/CodeGen/LiveRangeEdit.h"
Jakob Stoklund Olesen1ead68d2012-11-28 19:13:06 +000021#include "llvm/CodeGen/LiveRegMatrix.h"
Jakob Stoklund Olesenccc95812012-01-11 22:28:30 +000022#include "llvm/CodeGen/MachineInstr.h"
23#include "llvm/CodeGen/MachineRegisterInfo.h"
Jakob Stoklund Olesen1ead68d2012-11-28 19:13:06 +000024#include "llvm/CodeGen/VirtRegMap.h"
Jakob Stoklund Olesenccc95812012-01-11 22:28:30 +000025#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
36using namespace llvm;
37
Jakob Stoklund Olesenccc95812012-01-11 22:28:30 +000038STATISTIC(NumNewQueued , "Number of new live ranges queued");
39
40// Temporary verification option until we can put verification inside
41// MachineVerifier.
42static cl::opt<bool, true>
43VerifyRegAlloc("verify-regalloc", cl::location(RegAllocBase::VerifyEnabled),
44 cl::desc("Verify during register allocation"));
45
Craig Topper8de25f02013-07-17 03:11:32 +000046const char RegAllocBase::TimerGroupName[] = "Register Allocation";
Jakob Stoklund Olesenccc95812012-01-11 22:28:30 +000047bool RegAllocBase::VerifyEnabled = false;
48
Jakob Stoklund Olesenccc95812012-01-11 22:28:30 +000049//===----------------------------------------------------------------------===//
50// RegAllocBase Implementation
51//===----------------------------------------------------------------------===//
52
Juergen Ributzka35436252013-11-19 00:57:56 +000053// Pin the vtable to this file.
54void RegAllocBase::anchor() {}
55
Jakob Stoklund Olesend4348a22012-06-20 22:52:29 +000056void RegAllocBase::init(VirtRegMap &vrm,
57 LiveIntervals &lis,
58 LiveRegMatrix &mat) {
Jakob Stoklund Olesenccc95812012-01-11 22:28:30 +000059 TRI = &vrm.getTargetRegInfo();
60 MRI = &vrm.getRegInfo();
61 VRM = &vrm;
62 LIS = &lis;
Jakob Stoklund Olesend4348a22012-06-20 22:52:29 +000063 Matrix = &mat;
Chad Rosier18bb0542012-11-28 00:21:29 +000064 MRI->freezeReservedRegs(vrm.getMachineFunction());
Jakob Stoklund Olesenccc95812012-01-11 22:28:30 +000065 RegClassInfo.runOnMachineFunction(vrm.getMachineFunction());
Jakob Stoklund Olesenccc95812012-01-11 22:28:30 +000066}
67
68// Visit all the live registers. If they are already assigned to a physical
69// register, unify them with the corresponding LiveIntervalUnion, otherwise push
70// them on the priority queue for later assignment.
71void RegAllocBase::seedLiveRegs() {
72 NamedRegionTimer T("Seed Live Regs", TimerGroupName, TimePassesIsEnabled);
Jakob Stoklund Olesend67582e2012-06-20 21:25:05 +000073 for (unsigned i = 0, e = MRI->getNumVirtRegs(); i != e; ++i) {
74 unsigned Reg = TargetRegisterInfo::index2VirtReg(i);
75 if (MRI->reg_nodbg_empty(Reg))
76 continue;
77 enqueue(&LIS->getInterval(Reg));
Jakob Stoklund Olesenccc95812012-01-11 22:28:30 +000078 }
79}
80
Jakob Stoklund Olesenccc95812012-01-11 22:28:30 +000081// Top-level driver to manage the queue of unassigned VirtRegs and call the
82// selectOrSplit implementation.
83void RegAllocBase::allocatePhysRegs() {
84 seedLiveRegs();
85
86 // Continue assigning vregs one at a time to available physical registers.
87 while (LiveInterval *VirtReg = dequeue()) {
88 assert(!VRM->hasPhys(VirtReg->reg) && "Register already assigned");
89
90 // Unused registers can appear when the spiller coalesces snippets.
91 if (MRI->reg_nodbg_empty(VirtReg->reg)) {
92 DEBUG(dbgs() << "Dropping unused " << *VirtReg << '\n');
93 LIS->removeInterval(VirtReg->reg);
94 continue;
95 }
96
97 // Invalidate all interference queries, live ranges could have changed.
Jakob Stoklund Olesend4348a22012-06-20 22:52:29 +000098 Matrix->invalidateVirtRegs();
Jakob Stoklund Olesenccc95812012-01-11 22:28:30 +000099
100 // selectOrSplit requests the allocator to return an available physical
101 // register if possible and populate a list of new live intervals that
102 // result from splitting.
103 DEBUG(dbgs() << "\nselectOrSplit "
Stephen Hines36b56882014-04-23 16:57:46 -0700104 << MRI->getRegClass(VirtReg->reg)->getName()
105 << ':' << *VirtReg << " w=" << VirtReg->weight << '\n');
Mark Lacey1feb5852013-08-14 23:50:04 +0000106 typedef SmallVector<unsigned, 4> VirtRegVec;
Jakob Stoklund Olesenccc95812012-01-11 22:28:30 +0000107 VirtRegVec SplitVRegs;
108 unsigned AvailablePhysReg = selectOrSplit(*VirtReg, SplitVRegs);
109
110 if (AvailablePhysReg == ~0u) {
111 // selectOrSplit failed to find a register!
Jakob Stoklund Olesenccc95812012-01-11 22:28:30 +0000112 // Probably caused by an inline asm.
Stephen Hines36b56882014-04-23 16:57:46 -0700113 MachineInstr *MI = 0;
114 for (MachineRegisterInfo::reg_instr_iterator
115 I = MRI->reg_instr_begin(VirtReg->reg), E = MRI->reg_instr_end();
116 I != E; ) {
117 MachineInstr *TmpMI = &*(I++);
118 if (TmpMI->isInlineAsm()) {
119 MI = TmpMI;
Jakob Stoklund Olesenccc95812012-01-11 22:28:30 +0000120 break;
Stephen Hines36b56882014-04-23 16:57:46 -0700121 }
122 }
Jakob Stoklund Olesenccc95812012-01-11 22:28:30 +0000123 if (MI)
Benjamin Kramer87855d32013-10-05 19:33:37 +0000124 MI->emitError("inline assembly requires more registers than available");
Jakob Stoklund Olesenccc95812012-01-11 22:28:30 +0000125 else
Benjamin Kramer87855d32013-10-05 19:33:37 +0000126 report_fatal_error("ran out of registers during register allocation");
Jakob Stoklund Olesenccc95812012-01-11 22:28:30 +0000127 // Keep going after reporting the error.
128 VRM->assignVirt2Phys(VirtReg->reg,
129 RegClassInfo.getOrder(MRI->getRegClass(VirtReg->reg)).front());
130 continue;
131 }
132
133 if (AvailablePhysReg)
Jakob Stoklund Olesend4348a22012-06-20 22:52:29 +0000134 Matrix->assign(*VirtReg, AvailablePhysReg);
Jakob Stoklund Olesenccc95812012-01-11 22:28:30 +0000135
136 for (VirtRegVec::iterator I = SplitVRegs.begin(), E = SplitVRegs.end();
137 I != E; ++I) {
Mark Lacey1feb5852013-08-14 23:50:04 +0000138 LiveInterval *SplitVirtReg = &LIS->getInterval(*I);
Jakob Stoklund Olesenccc95812012-01-11 22:28:30 +0000139 assert(!VRM->hasPhys(SplitVirtReg->reg) && "Register already assigned");
140 if (MRI->reg_nodbg_empty(SplitVirtReg->reg)) {
141 DEBUG(dbgs() << "not queueing unused " << *SplitVirtReg << '\n');
142 LIS->removeInterval(SplitVirtReg->reg);
143 continue;
144 }
145 DEBUG(dbgs() << "queuing new interval: " << *SplitVirtReg << "\n");
146 assert(TargetRegisterInfo::isVirtualRegister(SplitVirtReg->reg) &&
147 "expect split value in virtual register");
148 enqueue(SplitVirtReg);
149 ++NumNewQueued;
150 }
151 }
152}