blob: bc28a054c680a4e47ea990bb37f359656d6de969 [file] [log] [blame]
Eugene Zelenko16ffaf82017-09-13 21:15:20 +00001//===- RegAllocBase.cpp - Register Allocator Base Class -------------------===//
Jakob Stoklund Olesenccc95812012-01-11 22:28:30 +00002//
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 Ren2938fa42014-02-22 19:31:28 +000010// 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"
Eugene Zelenko16ffaf82017-09-13 21:15:20 +000017#include "llvm/ADT/SmallVector.h"
Jakob Stoklund Olesenccc95812012-01-11 22:28:30 +000018#include "llvm/ADT/Statistic.h"
Eugene Zelenko16ffaf82017-09-13 21:15:20 +000019#include "llvm/CodeGen/LiveInterval.h"
Matthias Braunfa621d22017-12-13 02:51:04 +000020#include "llvm/CodeGen/LiveIntervals.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"
David Blaikiee3a9b4c2017-11-17 01:07:10 +000024#include "llvm/CodeGen/TargetRegisterInfo.h"
Jakob Stoklund Olesen1ead68d2012-11-28 19:13:06 +000025#include "llvm/CodeGen/VirtRegMap.h"
Eugene Zelenko16ffaf82017-09-13 21:15:20 +000026#include "llvm/Pass.h"
Jakob Stoklund Olesenccc95812012-01-11 22:28:30 +000027#include "llvm/Support/CommandLine.h"
28#include "llvm/Support/Debug.h"
29#include "llvm/Support/ErrorHandling.h"
Jakob Stoklund Olesenccc95812012-01-11 22:28:30 +000030#include "llvm/Support/Timer.h"
Chandler Carruthe3e43d92017-06-06 11:49:48 +000031#include "llvm/Support/raw_ostream.h"
Eugene Zelenko16ffaf82017-09-13 21:15:20 +000032#include <cassert>
Jakob Stoklund Olesenccc95812012-01-11 22:28:30 +000033
34using namespace llvm;
35
Chandler Carruth8677f2f2014-04-22 02:02:50 +000036#define DEBUG_TYPE "regalloc"
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>
Zachary Turner9a4e15c2017-12-01 00:53:10 +000043 VerifyRegAlloc("verify-regalloc", cl::location(RegAllocBase::VerifyEnabled),
44 cl::Hidden, cl::desc("Verify during register allocation"));
Jakob Stoklund Olesenccc95812012-01-11 22:28:30 +000045
Matthias Braun9262f002016-11-18 19:43:18 +000046const char RegAllocBase::TimerGroupName[] = "regalloc";
47const char RegAllocBase::TimerGroupDescription[] = "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() {
Matthias Braun9262f002016-11-18 19:43:18 +000073 NamedRegionTimer T("seed", "Seed Live Regs", TimerGroupName,
74 TimerGroupDescription, TimePassesIsEnabled);
Jakob Stoklund Olesend67582e2012-06-20 21:25:05 +000075 for (unsigned i = 0, e = MRI->getNumVirtRegs(); i != e; ++i) {
76 unsigned Reg = TargetRegisterInfo::index2VirtReg(i);
77 if (MRI->reg_nodbg_empty(Reg))
78 continue;
79 enqueue(&LIS->getInterval(Reg));
Jakob Stoklund Olesenccc95812012-01-11 22:28:30 +000080 }
81}
82
Jakob Stoklund Olesenccc95812012-01-11 22:28:30 +000083// Top-level driver to manage the queue of unassigned VirtRegs and call the
84// selectOrSplit implementation.
85void RegAllocBase::allocatePhysRegs() {
86 seedLiveRegs();
87
88 // Continue assigning vregs one at a time to available physical registers.
89 while (LiveInterval *VirtReg = dequeue()) {
90 assert(!VRM->hasPhys(VirtReg->reg) && "Register already assigned");
91
92 // Unused registers can appear when the spiller coalesces snippets.
93 if (MRI->reg_nodbg_empty(VirtReg->reg)) {
Nicola Zaghen0818e782018-05-14 12:53:11 +000094 LLVM_DEBUG(dbgs() << "Dropping unused " << *VirtReg << '\n');
Quentin Colombet9d60e0f2015-01-08 01:16:39 +000095 aboutToRemoveInterval(*VirtReg);
Jakob Stoklund Olesenccc95812012-01-11 22:28:30 +000096 LIS->removeInterval(VirtReg->reg);
97 continue;
98 }
99
100 // Invalidate all interference queries, live ranges could have changed.
Jakob Stoklund Olesend4348a22012-06-20 22:52:29 +0000101 Matrix->invalidateVirtRegs();
Jakob Stoklund Olesenccc95812012-01-11 22:28:30 +0000102
103 // selectOrSplit requests the allocator to return an available physical
104 // register if possible and populate a list of new live intervals that
105 // result from splitting.
Nicola Zaghen0818e782018-05-14 12:53:11 +0000106 LLVM_DEBUG(dbgs() << "\nselectOrSplit "
107 << TRI->getRegClassName(MRI->getRegClass(VirtReg->reg))
108 << ':' << *VirtReg << " w=" << VirtReg->weight << '\n');
Eugene Zelenko16ffaf82017-09-13 21:15:20 +0000109
110 using VirtRegVec = SmallVector<unsigned, 4>;
111
Jakob Stoklund Olesenccc95812012-01-11 22:28:30 +0000112 VirtRegVec SplitVRegs;
113 unsigned AvailablePhysReg = selectOrSplit(*VirtReg, SplitVRegs);
114
115 if (AvailablePhysReg == ~0u) {
116 // selectOrSplit failed to find a register!
Jakob Stoklund Olesenccc95812012-01-11 22:28:30 +0000117 // Probably caused by an inline asm.
Craig Topper4ba84432014-04-14 00:51:57 +0000118 MachineInstr *MI = nullptr;
Owen Anderson76604af2014-03-13 06:02:25 +0000119 for (MachineRegisterInfo::reg_instr_iterator
120 I = MRI->reg_instr_begin(VirtReg->reg), E = MRI->reg_instr_end();
121 I != E; ) {
122 MachineInstr *TmpMI = &*(I++);
123 if (TmpMI->isInlineAsm()) {
124 MI = TmpMI;
Jakob Stoklund Olesenccc95812012-01-11 22:28:30 +0000125 break;
Owen Anderson76604af2014-03-13 06:02:25 +0000126 }
127 }
Jakob Stoklund Olesenccc95812012-01-11 22:28:30 +0000128 if (MI)
Benjamin Kramer87855d32013-10-05 19:33:37 +0000129 MI->emitError("inline assembly requires more registers than available");
Jakob Stoklund Olesenccc95812012-01-11 22:28:30 +0000130 else
Benjamin Kramer87855d32013-10-05 19:33:37 +0000131 report_fatal_error("ran out of registers during register allocation");
Jakob Stoklund Olesenccc95812012-01-11 22:28:30 +0000132 // Keep going after reporting the error.
133 VRM->assignVirt2Phys(VirtReg->reg,
134 RegClassInfo.getOrder(MRI->getRegClass(VirtReg->reg)).front());
135 continue;
136 }
137
138 if (AvailablePhysReg)
Jakob Stoklund Olesend4348a22012-06-20 22:52:29 +0000139 Matrix->assign(*VirtReg, AvailablePhysReg);
Jakob Stoklund Olesenccc95812012-01-11 22:28:30 +0000140
Matt Arsenault07713762017-07-24 18:07:55 +0000141 for (unsigned Reg : SplitVRegs) {
142 assert(LIS->hasInterval(Reg));
143
144 LiveInterval *SplitVirtReg = &LIS->getInterval(Reg);
Jakob Stoklund Olesenccc95812012-01-11 22:28:30 +0000145 assert(!VRM->hasPhys(SplitVirtReg->reg) && "Register already assigned");
146 if (MRI->reg_nodbg_empty(SplitVirtReg->reg)) {
Matt Arsenault07713762017-07-24 18:07:55 +0000147 assert(SplitVirtReg->empty() && "Non-empty but used interval");
Nicola Zaghen0818e782018-05-14 12:53:11 +0000148 LLVM_DEBUG(dbgs() << "not queueing unused " << *SplitVirtReg << '\n');
Quentin Colombet9d60e0f2015-01-08 01:16:39 +0000149 aboutToRemoveInterval(*SplitVirtReg);
Jakob Stoklund Olesenccc95812012-01-11 22:28:30 +0000150 LIS->removeInterval(SplitVirtReg->reg);
151 continue;
152 }
Nicola Zaghen0818e782018-05-14 12:53:11 +0000153 LLVM_DEBUG(dbgs() << "queuing new interval: " << *SplitVirtReg << "\n");
Jakob Stoklund Olesenccc95812012-01-11 22:28:30 +0000154 assert(TargetRegisterInfo::isVirtualRegister(SplitVirtReg->reg) &&
155 "expect split value in virtual register");
156 enqueue(SplitVirtReg);
157 ++NumNewQueued;
158 }
159 }
160}
Wei Mi815b02e2016-04-13 03:08:27 +0000161
162void RegAllocBase::postOptimization() {
163 spiller().postOptimization();
164 for (auto DeadInst : DeadRemats) {
165 LIS->RemoveMachineInstrFromMaps(*DeadInst);
166 DeadInst->eraseFromParent();
167 }
168 DeadRemats.clear();
169}