blob: 6a7cc5ba430841df829430addc6744f8d4c18b8c [file] [log] [blame]
Eugene Zelenko618c5552017-09-13 21:15:20 +00001//===- RegAllocBase.h - basic regalloc interface and driver -----*- C++ -*-===//
Andrew Trick1c246052010-10-22 23:09:15 +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
Andrew Trick1c246052010-10-22 23:09:15 +00006//
7//===----------------------------------------------------------------------===//
8//
9// This file defines the RegAllocBase class, which is the skeleton of a basic
10// register allocation algorithm and interface for extending it. It provides the
11// building blocks on which to construct other experimental allocators and test
12// the validity of two principles:
Andrew Trickfce64c92010-11-30 23:18:47 +000013//
Andrew Trick1c246052010-10-22 23:09:15 +000014// - If virtual and physical register liveness is modeled using intervals, then
15// on-the-fly interference checking is cheap. Furthermore, interferences can be
16// lazily cached and reused.
Andrew Trickfce64c92010-11-30 23:18:47 +000017//
Andrew Trick1c246052010-10-22 23:09:15 +000018// - Register allocation complexity, and generated code performance is
19// determined by the effectiveness of live range splitting rather than optimal
20// coloring.
21//
22// Following the first principle, interfering checking revolves around the
23// LiveIntervalUnion data structure.
24//
25// To fulfill the second principle, the basic allocator provides a driver for
26// incremental splitting. It essentially punts on the problem of register
27// coloring, instead driving the assignment of virtual to physical registers by
28// the cost of splitting. The basic allocator allows for heuristic reassignment
29// of registers, if a more sophisticated allocator chooses to do that.
30//
31// This framework provides a way to engineer the compile time vs. code
Cameron Zwarichbfef0752010-12-29 04:42:39 +000032// quality trade-off without relying on a particular theoretical solver.
Andrew Trick1c246052010-10-22 23:09:15 +000033//
34//===----------------------------------------------------------------------===//
35
Benjamin Kramera7c40ef2014-08-13 16:26:38 +000036#ifndef LLVM_LIB_CODEGEN_REGALLOCBASE_H
37#define LLVM_LIB_CODEGEN_REGALLOCBASE_H
Andrew Trick1c246052010-10-22 23:09:15 +000038
Eugene Zelenko618c5552017-09-13 21:15:20 +000039#include "llvm/ADT/SmallPtrSet.h"
Chandler Carruth802d7552012-12-04 07:12:27 +000040#include "llvm/CodeGen/RegisterClassInfo.h"
Andrew Trick1c246052010-10-22 23:09:15 +000041
42namespace llvm {
43
Eugene Zelenko618c5552017-09-13 21:15:20 +000044class LiveInterval;
Andrew Trick84aef492010-10-26 18:34:01 +000045class LiveIntervals;
Jakob Stoklund Olesen03b87d52012-06-20 22:52:24 +000046class LiveRegMatrix;
Eugene Zelenko618c5552017-09-13 21:15:20 +000047class MachineInstr;
48class MachineRegisterInfo;
49template<typename T> class SmallVectorImpl;
Andrew Trick89eb6a82010-11-10 19:18:47 +000050class Spiller;
Eugene Zelenko618c5552017-09-13 21:15:20 +000051class TargetRegisterInfo;
52class VirtRegMap;
Andrew Trick84aef492010-10-26 18:34:01 +000053
Andrew Trick1c246052010-10-22 23:09:15 +000054/// RegAllocBase provides the register allocation driver and interface that can
55/// be extended to add interesting heuristics.
56///
Andrew Trickfce64c92010-11-30 23:18:47 +000057/// Register allocators must override the selectOrSplit() method to implement
Jakob Stoklund Olesen2329c542011-02-22 23:01:52 +000058/// live range splitting. They must also override enqueue/dequeue to provide an
59/// assignment order.
Benjamin Kramer079b96e2013-09-11 18:05:11 +000060class RegAllocBase {
Juergen Ributzkad12ccbd2013-11-19 00:57:56 +000061 virtual void anchor();
Eugene Zelenko618c5552017-09-13 21:15:20 +000062
Jakob Stoklund Olesen20f19eb2012-01-11 23:19:08 +000063protected:
Eugene Zelenko618c5552017-09-13 21:15:20 +000064 const TargetRegisterInfo *TRI = nullptr;
65 MachineRegisterInfo *MRI = nullptr;
66 VirtRegMap *VRM = nullptr;
67 LiveIntervals *LIS = nullptr;
68 LiveRegMatrix *Matrix = nullptr;
Jakob Stoklund Olesen20f19eb2012-01-11 23:19:08 +000069 RegisterClassInfo RegClassInfo;
70
Wei Mi9a16d652016-04-13 03:08:27 +000071 /// Inst which is a def of an original reg and whose defs are already all
72 /// dead after remat is saved in DeadRemats. The deletion of such inst is
73 /// postponed till all the allocations are done, so its remat expr is
74 /// always available for the remat of all the siblings of the original reg.
75 SmallPtrSet<MachineInstr *, 32> DeadRemats;
76
Eugene Zelenko618c5552017-09-13 21:15:20 +000077 RegAllocBase() = default;
78 virtual ~RegAllocBase() = default;
Andrew Tricke8719c52010-10-22 23:33:19 +000079
Andrew Trick1c246052010-10-22 23:09:15 +000080 // A RegAlloc pass should call this before allocatePhysRegs.
Jakob Stoklund Olesen2d2dec92012-06-20 22:52:29 +000081 void init(VirtRegMap &vrm, LiveIntervals &lis, LiveRegMatrix &mat);
Jakob Stoklund Olesen50215af2011-05-10 17:37:41 +000082
Andrew Trick84aef492010-10-26 18:34:01 +000083 // The top-level driver. The output is a VirtRegMap that us updated with
84 // physical register assignments.
Andrew Trick84aef492010-10-26 18:34:01 +000085 void allocatePhysRegs();
Andrew Trick1c246052010-10-22 23:09:15 +000086
Wei Mi9a16d652016-04-13 03:08:27 +000087 // Include spiller post optimization and removing dead defs left because of
88 // rematerialization.
89 virtual void postOptimization();
90
Andrew Trick89eb6a82010-11-10 19:18:47 +000091 // Get a temporary reference to a Spiller instance.
92 virtual Spiller &spiller() = 0;
Andrew Trickfce64c92010-11-30 23:18:47 +000093
Jakob Stoklund Olesen2329c542011-02-22 23:01:52 +000094 /// enqueue - Add VirtReg to the priority queue of unassigned registers.
95 virtual void enqueue(LiveInterval *LI) = 0;
96
97 /// dequeue - Return the next unassigned register, or NULL.
98 virtual LiveInterval *dequeue() = 0;
Jakob Stoklund Olesene0df7862010-12-08 22:22:41 +000099
Andrew Trick1c246052010-10-22 23:09:15 +0000100 // A RegAlloc pass should override this to provide the allocation heuristics.
Andrew Trick84aef492010-10-26 18:34:01 +0000101 // Each call must guarantee forward progess by returning an available PhysReg
102 // or new set of split live virtual registers. It is up to the splitter to
Andrew Trick1c246052010-10-22 23:09:15 +0000103 // converge quickly toward fully spilled live ranges.
Andrew Trickfce64c92010-11-30 23:18:47 +0000104 virtual unsigned selectOrSplit(LiveInterval &VirtReg,
Mark Laceyf9ea8852013-08-14 23:50:04 +0000105 SmallVectorImpl<unsigned> &splitLVRs) = 0;
Andrew Trick1c246052010-10-22 23:09:15 +0000106
Jakob Stoklund Olesen92da7052010-12-11 00:19:56 +0000107 // Use this group name for NamedRegionTimer.
Craig Topper9fdc70e2013-07-17 03:11:32 +0000108 static const char TimerGroupName[];
Matthias Braun9f15a792016-11-18 19:43:18 +0000109 static const char TimerGroupDescription[];
Jakob Stoklund Olesen92da7052010-12-11 00:19:56 +0000110
Quentin Colombeta799e2e2015-01-08 01:16:39 +0000111 /// Method called when the allocator is about to remove a LiveInterval.
112 virtual void aboutToRemoveInterval(LiveInterval &LI) {}
113
Jakob Stoklund Olesen2e98ee32010-12-17 23:16:35 +0000114public:
115 /// VerifyEnabled - True when -verify-regalloc is given.
116 static bool VerifyEnabled;
117
Andrew Trickfce64c92010-11-30 23:18:47 +0000118private:
Jakob Stoklund Olesen2329c542011-02-22 23:01:52 +0000119 void seedLiveRegs();
Andrew Trick1c246052010-10-22 23:09:15 +0000120};
121
Andrew Trick1c246052010-10-22 23:09:15 +0000122} // end namespace llvm
123
Eugene Zelenko618c5552017-09-13 21:15:20 +0000124#endif // LLVM_LIB_CODEGEN_REGALLOCBASE_H