blob: 7b4fbace2c1c1afc8151209b5509b915b6f1c35a [file] [log] [blame]
Jakob Stoklund Olesena818d802012-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//
Manman Ren28671402014-02-22 19:31:28 +000010// This file defines the RegAllocBase class which provides common functionality
Jakob Stoklund Olesena818d802012-01-11 22:28:30 +000011// for LiveIntervalUnion-based register allocators.
12//
13//===----------------------------------------------------------------------===//
14
Jakob Stoklund Olesena818d802012-01-11 22:28:30 +000015#include "RegAllocBase.h"
Jakob Stoklund Olesena818d802012-01-11 22:28:30 +000016#include "Spiller.h"
Jakob Stoklund Olesena818d802012-01-11 22:28:30 +000017#include "llvm/ADT/Statistic.h"
18#include "llvm/CodeGen/LiveIntervalAnalysis.h"
Pete Cooper3ca96f92012-04-02 22:44:18 +000019#include "llvm/CodeGen/LiveRangeEdit.h"
Jakob Stoklund Olesen26c9d702012-11-28 19:13:06 +000020#include "llvm/CodeGen/LiveRegMatrix.h"
Jakob Stoklund Olesena818d802012-01-11 22:28:30 +000021#include "llvm/CodeGen/MachineInstr.h"
22#include "llvm/CodeGen/MachineRegisterInfo.h"
Jakob Stoklund Olesen26c9d702012-11-28 19:13:06 +000023#include "llvm/CodeGen/VirtRegMap.h"
Jakob Stoklund Olesena818d802012-01-11 22:28:30 +000024#include "llvm/Support/CommandLine.h"
25#include "llvm/Support/Debug.h"
26#include "llvm/Support/ErrorHandling.h"
Jakob Stoklund Olesena818d802012-01-11 22:28:30 +000027#include "llvm/Support/Timer.h"
Chandler Carruth6bda14b2017-06-06 11:49:48 +000028#include "llvm/Support/raw_ostream.h"
29#include "llvm/Target/TargetRegisterInfo.h"
Jakob Stoklund Olesena818d802012-01-11 22:28:30 +000030
31using namespace llvm;
32
Chandler Carruth1b9dde02014-04-22 02:02:50 +000033#define DEBUG_TYPE "regalloc"
34
Jakob Stoklund Olesena818d802012-01-11 22:28:30 +000035STATISTIC(NumNewQueued , "Number of new live ranges queued");
36
37// Temporary verification option until we can put verification inside
38// MachineVerifier.
39static cl::opt<bool, true>
40VerifyRegAlloc("verify-regalloc", cl::location(RegAllocBase::VerifyEnabled),
41 cl::desc("Verify during register allocation"));
42
Matthias Braun9f15a792016-11-18 19:43:18 +000043const char RegAllocBase::TimerGroupName[] = "regalloc";
44const char RegAllocBase::TimerGroupDescription[] = "Register Allocation";
Jakob Stoklund Olesena818d802012-01-11 22:28:30 +000045bool RegAllocBase::VerifyEnabled = false;
46
Jakob Stoklund Olesena818d802012-01-11 22:28:30 +000047//===----------------------------------------------------------------------===//
48// RegAllocBase Implementation
49//===----------------------------------------------------------------------===//
50
Juergen Ributzkad12ccbd2013-11-19 00:57:56 +000051// Pin the vtable to this file.
52void RegAllocBase::anchor() {}
53
Jakob Stoklund Olesen2d2dec92012-06-20 22:52:29 +000054void RegAllocBase::init(VirtRegMap &vrm,
55 LiveIntervals &lis,
56 LiveRegMatrix &mat) {
Jakob Stoklund Olesena818d802012-01-11 22:28:30 +000057 TRI = &vrm.getTargetRegInfo();
58 MRI = &vrm.getRegInfo();
59 VRM = &vrm;
60 LIS = &lis;
Jakob Stoklund Olesen2d2dec92012-06-20 22:52:29 +000061 Matrix = &mat;
Chad Rosiered119d52012-11-28 00:21:29 +000062 MRI->freezeReservedRegs(vrm.getMachineFunction());
Jakob Stoklund Olesena818d802012-01-11 22:28:30 +000063 RegClassInfo.runOnMachineFunction(vrm.getMachineFunction());
Jakob Stoklund Olesena818d802012-01-11 22:28:30 +000064}
65
66// Visit all the live registers. If they are already assigned to a physical
67// register, unify them with the corresponding LiveIntervalUnion, otherwise push
68// them on the priority queue for later assignment.
69void RegAllocBase::seedLiveRegs() {
Matthias Braun9f15a792016-11-18 19:43:18 +000070 NamedRegionTimer T("seed", "Seed Live Regs", TimerGroupName,
71 TimerGroupDescription, TimePassesIsEnabled);
Jakob Stoklund Olesena1f43dc2012-06-20 21:25:05 +000072 for (unsigned i = 0, e = MRI->getNumVirtRegs(); i != e; ++i) {
73 unsigned Reg = TargetRegisterInfo::index2VirtReg(i);
74 if (MRI->reg_nodbg_empty(Reg))
75 continue;
76 enqueue(&LIS->getInterval(Reg));
Jakob Stoklund Olesena818d802012-01-11 22:28:30 +000077 }
78}
79
Jakob Stoklund Olesena818d802012-01-11 22:28:30 +000080// Top-level driver to manage the queue of unassigned VirtRegs and call the
81// selectOrSplit implementation.
82void RegAllocBase::allocatePhysRegs() {
83 seedLiveRegs();
84
85 // Continue assigning vregs one at a time to available physical registers.
86 while (LiveInterval *VirtReg = dequeue()) {
87 assert(!VRM->hasPhys(VirtReg->reg) && "Register already assigned");
88
89 // Unused registers can appear when the spiller coalesces snippets.
90 if (MRI->reg_nodbg_empty(VirtReg->reg)) {
91 DEBUG(dbgs() << "Dropping unused " << *VirtReg << '\n');
Quentin Colombeta799e2e2015-01-08 01:16:39 +000092 aboutToRemoveInterval(*VirtReg);
Jakob Stoklund Olesena818d802012-01-11 22:28:30 +000093 LIS->removeInterval(VirtReg->reg);
94 continue;
95 }
96
97 // Invalidate all interference queries, live ranges could have changed.
Jakob Stoklund Olesen2d2dec92012-06-20 22:52:29 +000098 Matrix->invalidateVirtRegs();
Jakob Stoklund Olesena818d802012-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 "
Craig Toppercf0444b2014-11-17 05:50:14 +0000104 << TRI->getRegClassName(MRI->getRegClass(VirtReg->reg))
Andrew Trick059e8002013-11-22 19:07:42 +0000105 << ':' << *VirtReg << " w=" << VirtReg->weight << '\n');
Mark Laceyf9ea8852013-08-14 23:50:04 +0000106 typedef SmallVector<unsigned, 4> VirtRegVec;
Jakob Stoklund Olesena818d802012-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 Olesena818d802012-01-11 22:28:30 +0000112 // Probably caused by an inline asm.
Craig Topperc0196b12014-04-14 00:51:57 +0000113 MachineInstr *MI = nullptr;
Owen Andersonabb90c92014-03-13 06:02:25 +0000114 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 Olesena818d802012-01-11 22:28:30 +0000120 break;
Owen Andersonabb90c92014-03-13 06:02:25 +0000121 }
122 }
Jakob Stoklund Olesena818d802012-01-11 22:28:30 +0000123 if (MI)
Benjamin Kramer7200a462013-10-05 19:33:37 +0000124 MI->emitError("inline assembly requires more registers than available");
Jakob Stoklund Olesena818d802012-01-11 22:28:30 +0000125 else
Benjamin Kramer7200a462013-10-05 19:33:37 +0000126 report_fatal_error("ran out of registers during register allocation");
Jakob Stoklund Olesena818d802012-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 Olesen2d2dec92012-06-20 22:52:29 +0000134 Matrix->assign(*VirtReg, AvailablePhysReg);
Jakob Stoklund Olesena818d802012-01-11 22:28:30 +0000135
Matt Arsenault5fbc8702017-07-24 18:07:55 +0000136 for (unsigned Reg : SplitVRegs) {
137 assert(LIS->hasInterval(Reg));
138
139 LiveInterval *SplitVirtReg = &LIS->getInterval(Reg);
Jakob Stoklund Olesena818d802012-01-11 22:28:30 +0000140 assert(!VRM->hasPhys(SplitVirtReg->reg) && "Register already assigned");
141 if (MRI->reg_nodbg_empty(SplitVirtReg->reg)) {
Matt Arsenault5fbc8702017-07-24 18:07:55 +0000142 assert(SplitVirtReg->empty() && "Non-empty but used interval");
Jakob Stoklund Olesena818d802012-01-11 22:28:30 +0000143 DEBUG(dbgs() << "not queueing unused " << *SplitVirtReg << '\n');
Quentin Colombeta799e2e2015-01-08 01:16:39 +0000144 aboutToRemoveInterval(*SplitVirtReg);
Jakob Stoklund Olesena818d802012-01-11 22:28:30 +0000145 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}
Wei Mi9a16d652016-04-13 03:08:27 +0000156
157void RegAllocBase::postOptimization() {
158 spiller().postOptimization();
159 for (auto DeadInst : DeadRemats) {
160 LIS->RemoveMachineInstrFromMaps(*DeadInst);
161 DeadInst->eraseFromParent();
162 }
163 DeadRemats.clear();
164}