LLVM 19.0.0git
RegAllocBase.h
Go to the documentation of this file.
1//===- RegAllocBase.h - basic regalloc interface and driver -----*- C++ -*-===//
2//
3// 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
6//
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:
13//
14// - 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.
17//
18// - 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
32// quality trade-off without relying on a particular theoretical solver.
33//
34//===----------------------------------------------------------------------===//
35
36#ifndef LLVM_LIB_CODEGEN_REGALLOCBASE_H
37#define LLVM_LIB_CODEGEN_REGALLOCBASE_H
38
43
44namespace llvm {
45
46class LiveInterval;
47class LiveIntervals;
48class LiveRegMatrix;
49class MachineInstr;
50class MachineRegisterInfo;
51template<typename T> class SmallVectorImpl;
52class Spiller;
53class TargetRegisterInfo;
54class VirtRegMap;
55
56/// RegAllocBase provides the register allocation driver and interface that can
57/// be extended to add interesting heuristics.
58///
59/// Register allocators must override the selectOrSplit() method to implement
60/// live range splitting. They must also override enqueue/dequeue to provide an
61/// assignment order.
63 virtual void anchor();
64
65protected:
66 const TargetRegisterInfo *TRI = nullptr;
68 VirtRegMap *VRM = nullptr;
69 LiveIntervals *LIS = nullptr;
72
73private:
74 /// Private, callees should go through shouldAllocateRegister
75 const RegClassFilterFunc ShouldAllocateClass;
76
77protected:
78 /// Inst which is a def of an original reg and whose defs are already all
79 /// dead after remat is saved in DeadRemats. The deletion of such inst is
80 /// postponed till all the allocations are done, so its remat expr is
81 /// always available for the remat of all the siblings of the original reg.
83
84 RegAllocBase(const RegClassFilterFunc F = nullptr) : ShouldAllocateClass(F) {}
85
86 virtual ~RegAllocBase() = default;
87
88 // A RegAlloc pass should call this before allocatePhysRegs.
89 void init(VirtRegMap &vrm, LiveIntervals &lis, LiveRegMatrix &mat);
90
91 /// Get whether a given register should be allocated
93 if (!ShouldAllocateClass)
94 return true;
95 return ShouldAllocateClass(*TRI, *MRI->getRegClass(Reg));
96 }
97
98 // The top-level driver. The output is a VirtRegMap that us updated with
99 // physical register assignments.
100 void allocatePhysRegs();
101
102 // Include spiller post optimization and removing dead defs left because of
103 // rematerialization.
104 virtual void postOptimization();
105
106 // Get a temporary reference to a Spiller instance.
107 virtual Spiller &spiller() = 0;
108
109 /// enqueue - Add VirtReg to the priority queue of unassigned registers.
110 virtual void enqueueImpl(const LiveInterval *LI) = 0;
111
112 /// enqueue - Add VirtReg to the priority queue of unassigned registers.
113 void enqueue(const LiveInterval *LI);
114
115 /// dequeue - Return the next unassigned register, or NULL.
116 virtual const LiveInterval *dequeue() = 0;
117
118 // A RegAlloc pass should override this to provide the allocation heuristics.
119 // Each call must guarantee forward progess by returning an available PhysReg
120 // or new set of split live virtual registers. It is up to the splitter to
121 // converge quickly toward fully spilled live ranges.
122 virtual MCRegister selectOrSplit(const LiveInterval &VirtReg,
123 SmallVectorImpl<Register> &splitLVRs) = 0;
124
125 // Use this group name for NamedRegionTimer.
126 static const char TimerGroupName[];
127 static const char TimerGroupDescription[];
128
129 /// Method called when the allocator is about to remove a LiveInterval.
130 virtual void aboutToRemoveInterval(const LiveInterval &LI) {}
131
132public:
133 /// VerifyEnabled - True when -verify-regalloc is given.
134 static bool VerifyEnabled;
135
136private:
137 void seedLiveRegs();
138};
139
140} // end namespace llvm
141
142#endif // LLVM_LIB_CODEGEN_REGALLOCBASE_H
#define F(x, y, z)
Definition: MD5.cpp:55
unsigned Reg
This file defines the SmallPtrSet class.
LiveInterval - This class represents the liveness of a register, or stack slot.
Definition: LiveInterval.h:687
Wrapper class representing physical registers. Should be passed by value.
Definition: MCRegister.h:33
MachineRegisterInfo - Keep track of information for virtual and physical registers,...
const TargetRegisterClass * getRegClass(Register Reg) const
Return the register class of the specified virtual register.
RegAllocBase provides the register allocation driver and interface that can be extended to add intere...
Definition: RegAllocBase.h:62
virtual void aboutToRemoveInterval(const LiveInterval &LI)
Method called when the allocator is about to remove a LiveInterval.
Definition: RegAllocBase.h:130
virtual MCRegister selectOrSplit(const LiveInterval &VirtReg, SmallVectorImpl< Register > &splitLVRs)=0
void enqueue(const LiveInterval *LI)
enqueue - Add VirtReg to the priority queue of unassigned registers.
virtual ~RegAllocBase()=default
void init(VirtRegMap &vrm, LiveIntervals &lis, LiveRegMatrix &mat)
SmallPtrSet< MachineInstr *, 32 > DeadRemats
Inst which is a def of an original reg and whose defs are already all dead after remat is saved in De...
Definition: RegAllocBase.h:82
virtual Spiller & spiller()=0
const TargetRegisterInfo * TRI
Definition: RegAllocBase.h:66
LiveIntervals * LIS
Definition: RegAllocBase.h:69
static const char TimerGroupName[]
Definition: RegAllocBase.h:126
static const char TimerGroupDescription[]
Definition: RegAllocBase.h:127
LiveRegMatrix * Matrix
Definition: RegAllocBase.h:70
virtual const LiveInterval * dequeue()=0
dequeue - Return the next unassigned register, or NULL.
virtual void postOptimization()
VirtRegMap * VRM
Definition: RegAllocBase.h:68
RegisterClassInfo RegClassInfo
Definition: RegAllocBase.h:71
MachineRegisterInfo * MRI
Definition: RegAllocBase.h:67
RegAllocBase(const RegClassFilterFunc F=nullptr)
Definition: RegAllocBase.h:84
virtual void enqueueImpl(const LiveInterval *LI)=0
enqueue - Add VirtReg to the priority queue of unassigned registers.
bool shouldAllocateRegister(Register Reg)
Get whether a given register should be allocated.
Definition: RegAllocBase.h:92
static bool VerifyEnabled
VerifyEnabled - True when -verify-regalloc is given.
Definition: RegAllocBase.h:134
Wrapper class representing virtual and physical registers.
Definition: Register.h:19
SmallPtrSet - This class implements a set which is optimized for holding SmallSize or less elements.
Definition: SmallPtrSet.h:479
This class consists of common code factored out of the SmallVector class to reduce code duplication b...
Definition: SmallVector.h:586
Spiller interface.
Definition: Spiller.h:24
TargetRegisterInfo base class - We assume that the target defines a static array of TargetRegisterDes...
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
std::function< bool(const TargetRegisterInfo &TRI, const TargetRegisterClass &RC)> RegClassFilterFunc
Filter function for register classes during regalloc.