blob: 0df951dc99793a72a3d6e47236ba30a02f1b2126 [file] [log] [blame]
Eugene Zelenko618c5552017-09-13 21:15:20 +00001//===- RegAllocBase.cpp - Register Allocator Base Class -------------------===//
Jakob Stoklund Olesena818d802012-01-11 22:28:30 +00002//
Chandler Carruth2946cd72019-01-19 08:50:56 +00003// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
Jakob Stoklund Olesena818d802012-01-11 22:28:30 +00006//
7//===----------------------------------------------------------------------===//
8//
Manman Ren28671402014-02-22 19:31:28 +00009// This file defines the RegAllocBase class which provides common functionality
Jakob Stoklund Olesena818d802012-01-11 22:28:30 +000010// for LiveIntervalUnion-based register allocators.
11//
12//===----------------------------------------------------------------------===//
13
Jakob Stoklund Olesena818d802012-01-11 22:28:30 +000014#include "RegAllocBase.h"
Jakob Stoklund Olesena818d802012-01-11 22:28:30 +000015#include "Spiller.h"
Eugene Zelenko618c5552017-09-13 21:15:20 +000016#include "llvm/ADT/SmallVector.h"
Jakob Stoklund Olesena818d802012-01-11 22:28:30 +000017#include "llvm/ADT/Statistic.h"
Eugene Zelenko618c5552017-09-13 21:15:20 +000018#include "llvm/CodeGen/LiveInterval.h"
Matthias Braunf8422972017-12-13 02:51:04 +000019#include "llvm/CodeGen/LiveIntervals.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"
David Blaikieb3bde2e2017-11-17 01:07:10 +000023#include "llvm/CodeGen/TargetRegisterInfo.h"
Jakob Stoklund Olesen26c9d702012-11-28 19:13:06 +000024#include "llvm/CodeGen/VirtRegMap.h"
Eugene Zelenko618c5552017-09-13 21:15:20 +000025#include "llvm/Pass.h"
Jakob Stoklund Olesena818d802012-01-11 22:28:30 +000026#include "llvm/Support/CommandLine.h"
27#include "llvm/Support/Debug.h"
28#include "llvm/Support/ErrorHandling.h"
Jakob Stoklund Olesena818d802012-01-11 22:28:30 +000029#include "llvm/Support/Timer.h"
Chandler Carruth6bda14b2017-06-06 11:49:48 +000030#include "llvm/Support/raw_ostream.h"
Eugene Zelenko618c5552017-09-13 21:15:20 +000031#include <cassert>
Jakob Stoklund Olesena818d802012-01-11 22:28:30 +000032
33using namespace llvm;
34
Chandler Carruth1b9dde02014-04-22 02:02:50 +000035#define DEBUG_TYPE "regalloc"
36
Jakob Stoklund Olesena818d802012-01-11 22:28:30 +000037STATISTIC(NumNewQueued , "Number of new live ranges queued");
38
39// Temporary verification option until we can put verification inside
40// MachineVerifier.
41static cl::opt<bool, true>
Zachary Turner8065f0b2017-12-01 00:53:10 +000042 VerifyRegAlloc("verify-regalloc", cl::location(RegAllocBase::VerifyEnabled),
43 cl::Hidden, cl::desc("Verify during register allocation"));
Jakob Stoklund Olesena818d802012-01-11 22:28:30 +000044
Matthias Braun9f15a792016-11-18 19:43:18 +000045const char RegAllocBase::TimerGroupName[] = "regalloc";
46const char RegAllocBase::TimerGroupDescription[] = "Register Allocation";
Jakob Stoklund Olesena818d802012-01-11 22:28:30 +000047bool RegAllocBase::VerifyEnabled = false;
48
Jakob Stoklund Olesena818d802012-01-11 22:28:30 +000049//===----------------------------------------------------------------------===//
50// RegAllocBase Implementation
51//===----------------------------------------------------------------------===//
52
Juergen Ributzkad12ccbd2013-11-19 00:57:56 +000053// Pin the vtable to this file.
54void RegAllocBase::anchor() {}
55
Jakob Stoklund Olesen2d2dec92012-06-20 22:52:29 +000056void RegAllocBase::init(VirtRegMap &vrm,
57 LiveIntervals &lis,
58 LiveRegMatrix &mat) {
Jakob Stoklund Olesena818d802012-01-11 22:28:30 +000059 TRI = &vrm.getTargetRegInfo();
60 MRI = &vrm.getRegInfo();
61 VRM = &vrm;
62 LIS = &lis;
Jakob Stoklund Olesen2d2dec92012-06-20 22:52:29 +000063 Matrix = &mat;
Chad Rosiered119d52012-11-28 00:21:29 +000064 MRI->freezeReservedRegs(vrm.getMachineFunction());
Jakob Stoklund Olesena818d802012-01-11 22:28:30 +000065 RegClassInfo.runOnMachineFunction(vrm.getMachineFunction());
Jakob Stoklund Olesena818d802012-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() {
Matthias Braun9f15a792016-11-18 19:43:18 +000072 NamedRegionTimer T("seed", "Seed Live Regs", TimerGroupName,
73 TimerGroupDescription, TimePassesIsEnabled);
Jakob Stoklund Olesena1f43dc2012-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 Olesena818d802012-01-11 22:28:30 +000079 }
80}
81
Jakob Stoklund Olesena818d802012-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)) {
Nicola Zaghend34e60c2018-05-14 12:53:11 +000093 LLVM_DEBUG(dbgs() << "Dropping unused " << *VirtReg << '\n');
Quentin Colombeta799e2e2015-01-08 01:16:39 +000094 aboutToRemoveInterval(*VirtReg);
Jakob Stoklund Olesena818d802012-01-11 22:28:30 +000095 LIS->removeInterval(VirtReg->reg);
96 continue;
97 }
98
99 // Invalidate all interference queries, live ranges could have changed.
Jakob Stoklund Olesen2d2dec92012-06-20 22:52:29 +0000100 Matrix->invalidateVirtRegs();
Jakob Stoklund Olesena818d802012-01-11 22:28:30 +0000101
102 // selectOrSplit requests the allocator to return an available physical
103 // register if possible and populate a list of new live intervals that
104 // result from splitting.
Nicola Zaghend34e60c2018-05-14 12:53:11 +0000105 LLVM_DEBUG(dbgs() << "\nselectOrSplit "
106 << TRI->getRegClassName(MRI->getRegClass(VirtReg->reg))
107 << ':' << *VirtReg << " w=" << VirtReg->weight << '\n');
Eugene Zelenko618c5552017-09-13 21:15:20 +0000108
109 using VirtRegVec = SmallVector<unsigned, 4>;
110
Jakob Stoklund Olesena818d802012-01-11 22:28:30 +0000111 VirtRegVec SplitVRegs;
112 unsigned AvailablePhysReg = selectOrSplit(*VirtReg, SplitVRegs);
113
114 if (AvailablePhysReg == ~0u) {
115 // selectOrSplit failed to find a register!
Jakob Stoklund Olesena818d802012-01-11 22:28:30 +0000116 // Probably caused by an inline asm.
Craig Topperc0196b12014-04-14 00:51:57 +0000117 MachineInstr *MI = nullptr;
Owen Andersonabb90c92014-03-13 06:02:25 +0000118 for (MachineRegisterInfo::reg_instr_iterator
119 I = MRI->reg_instr_begin(VirtReg->reg), E = MRI->reg_instr_end();
120 I != E; ) {
121 MachineInstr *TmpMI = &*(I++);
122 if (TmpMI->isInlineAsm()) {
123 MI = TmpMI;
Jakob Stoklund Olesena818d802012-01-11 22:28:30 +0000124 break;
Owen Andersonabb90c92014-03-13 06:02:25 +0000125 }
126 }
Jakob Stoklund Olesena818d802012-01-11 22:28:30 +0000127 if (MI)
Benjamin Kramer7200a462013-10-05 19:33:37 +0000128 MI->emitError("inline assembly requires more registers than available");
Jakob Stoklund Olesena818d802012-01-11 22:28:30 +0000129 else
Benjamin Kramer7200a462013-10-05 19:33:37 +0000130 report_fatal_error("ran out of registers during register allocation");
Jakob Stoklund Olesena818d802012-01-11 22:28:30 +0000131 // Keep going after reporting the error.
132 VRM->assignVirt2Phys(VirtReg->reg,
133 RegClassInfo.getOrder(MRI->getRegClass(VirtReg->reg)).front());
134 continue;
135 }
136
137 if (AvailablePhysReg)
Jakob Stoklund Olesen2d2dec92012-06-20 22:52:29 +0000138 Matrix->assign(*VirtReg, AvailablePhysReg);
Jakob Stoklund Olesena818d802012-01-11 22:28:30 +0000139
Matt Arsenault5fbc8702017-07-24 18:07:55 +0000140 for (unsigned Reg : SplitVRegs) {
141 assert(LIS->hasInterval(Reg));
142
143 LiveInterval *SplitVirtReg = &LIS->getInterval(Reg);
Jakob Stoklund Olesena818d802012-01-11 22:28:30 +0000144 assert(!VRM->hasPhys(SplitVirtReg->reg) && "Register already assigned");
145 if (MRI->reg_nodbg_empty(SplitVirtReg->reg)) {
Matt Arsenault5fbc8702017-07-24 18:07:55 +0000146 assert(SplitVirtReg->empty() && "Non-empty but used interval");
Nicola Zaghend34e60c2018-05-14 12:53:11 +0000147 LLVM_DEBUG(dbgs() << "not queueing unused " << *SplitVirtReg << '\n');
Quentin Colombeta799e2e2015-01-08 01:16:39 +0000148 aboutToRemoveInterval(*SplitVirtReg);
Jakob Stoklund Olesena818d802012-01-11 22:28:30 +0000149 LIS->removeInterval(SplitVirtReg->reg);
150 continue;
151 }
Nicola Zaghend34e60c2018-05-14 12:53:11 +0000152 LLVM_DEBUG(dbgs() << "queuing new interval: " << *SplitVirtReg << "\n");
Jakob Stoklund Olesena818d802012-01-11 22:28:30 +0000153 assert(TargetRegisterInfo::isVirtualRegister(SplitVirtReg->reg) &&
154 "expect split value in virtual register");
155 enqueue(SplitVirtReg);
156 ++NumNewQueued;
157 }
158 }
159}
Wei Mi9a16d652016-04-13 03:08:27 +0000160
161void RegAllocBase::postOptimization() {
162 spiller().postOptimization();
163 for (auto DeadInst : DeadRemats) {
164 LIS->RemoveMachineInstrFromMaps(*DeadInst);
165 DeadInst->eraseFromParent();
166 }
167 DeadRemats.clear();
168}