xref: /aosp_15_r20/external/llvm/lib/CodeGen/RegAllocBase.h (revision 9880d6810fe72a1726cb53787c6711e909410d58)
1*9880d681SAndroid Build Coastguard Worker //===-- RegAllocBase.h - basic regalloc interface and driver --*- C++ -*---===//
2*9880d681SAndroid Build Coastguard Worker //
3*9880d681SAndroid Build Coastguard Worker //                     The LLVM Compiler Infrastructure
4*9880d681SAndroid Build Coastguard Worker //
5*9880d681SAndroid Build Coastguard Worker // This file is distributed under the University of Illinois Open Source
6*9880d681SAndroid Build Coastguard Worker // License. See LICENSE.TXT for details.
7*9880d681SAndroid Build Coastguard Worker //
8*9880d681SAndroid Build Coastguard Worker //===----------------------------------------------------------------------===//
9*9880d681SAndroid Build Coastguard Worker //
10*9880d681SAndroid Build Coastguard Worker // This file defines the RegAllocBase class, which is the skeleton of a basic
11*9880d681SAndroid Build Coastguard Worker // register allocation algorithm and interface for extending it. It provides the
12*9880d681SAndroid Build Coastguard Worker // building blocks on which to construct other experimental allocators and test
13*9880d681SAndroid Build Coastguard Worker // the validity of two principles:
14*9880d681SAndroid Build Coastguard Worker //
15*9880d681SAndroid Build Coastguard Worker // - If virtual and physical register liveness is modeled using intervals, then
16*9880d681SAndroid Build Coastguard Worker // on-the-fly interference checking is cheap. Furthermore, interferences can be
17*9880d681SAndroid Build Coastguard Worker // lazily cached and reused.
18*9880d681SAndroid Build Coastguard Worker //
19*9880d681SAndroid Build Coastguard Worker // - Register allocation complexity, and generated code performance is
20*9880d681SAndroid Build Coastguard Worker // determined by the effectiveness of live range splitting rather than optimal
21*9880d681SAndroid Build Coastguard Worker // coloring.
22*9880d681SAndroid Build Coastguard Worker //
23*9880d681SAndroid Build Coastguard Worker // Following the first principle, interfering checking revolves around the
24*9880d681SAndroid Build Coastguard Worker // LiveIntervalUnion data structure.
25*9880d681SAndroid Build Coastguard Worker //
26*9880d681SAndroid Build Coastguard Worker // To fulfill the second principle, the basic allocator provides a driver for
27*9880d681SAndroid Build Coastguard Worker // incremental splitting. It essentially punts on the problem of register
28*9880d681SAndroid Build Coastguard Worker // coloring, instead driving the assignment of virtual to physical registers by
29*9880d681SAndroid Build Coastguard Worker // the cost of splitting. The basic allocator allows for heuristic reassignment
30*9880d681SAndroid Build Coastguard Worker // of registers, if a more sophisticated allocator chooses to do that.
31*9880d681SAndroid Build Coastguard Worker //
32*9880d681SAndroid Build Coastguard Worker // This framework provides a way to engineer the compile time vs. code
33*9880d681SAndroid Build Coastguard Worker // quality trade-off without relying on a particular theoretical solver.
34*9880d681SAndroid Build Coastguard Worker //
35*9880d681SAndroid Build Coastguard Worker //===----------------------------------------------------------------------===//
36*9880d681SAndroid Build Coastguard Worker 
37*9880d681SAndroid Build Coastguard Worker #ifndef LLVM_LIB_CODEGEN_REGALLOCBASE_H
38*9880d681SAndroid Build Coastguard Worker #define LLVM_LIB_CODEGEN_REGALLOCBASE_H
39*9880d681SAndroid Build Coastguard Worker 
40*9880d681SAndroid Build Coastguard Worker #include "llvm/CodeGen/LiveInterval.h"
41*9880d681SAndroid Build Coastguard Worker #include "llvm/CodeGen/RegisterClassInfo.h"
42*9880d681SAndroid Build Coastguard Worker 
43*9880d681SAndroid Build Coastguard Worker namespace llvm {
44*9880d681SAndroid Build Coastguard Worker 
45*9880d681SAndroid Build Coastguard Worker template<typename T> class SmallVectorImpl;
46*9880d681SAndroid Build Coastguard Worker class TargetRegisterInfo;
47*9880d681SAndroid Build Coastguard Worker class VirtRegMap;
48*9880d681SAndroid Build Coastguard Worker class LiveIntervals;
49*9880d681SAndroid Build Coastguard Worker class LiveRegMatrix;
50*9880d681SAndroid Build Coastguard Worker class Spiller;
51*9880d681SAndroid Build Coastguard Worker 
52*9880d681SAndroid Build Coastguard Worker /// RegAllocBase provides the register allocation driver and interface that can
53*9880d681SAndroid Build Coastguard Worker /// be extended to add interesting heuristics.
54*9880d681SAndroid Build Coastguard Worker ///
55*9880d681SAndroid Build Coastguard Worker /// Register allocators must override the selectOrSplit() method to implement
56*9880d681SAndroid Build Coastguard Worker /// live range splitting. They must also override enqueue/dequeue to provide an
57*9880d681SAndroid Build Coastguard Worker /// assignment order.
58*9880d681SAndroid Build Coastguard Worker class RegAllocBase {
59*9880d681SAndroid Build Coastguard Worker   virtual void anchor();
60*9880d681SAndroid Build Coastguard Worker protected:
61*9880d681SAndroid Build Coastguard Worker   const TargetRegisterInfo *TRI;
62*9880d681SAndroid Build Coastguard Worker   MachineRegisterInfo *MRI;
63*9880d681SAndroid Build Coastguard Worker   VirtRegMap *VRM;
64*9880d681SAndroid Build Coastguard Worker   LiveIntervals *LIS;
65*9880d681SAndroid Build Coastguard Worker   LiveRegMatrix *Matrix;
66*9880d681SAndroid Build Coastguard Worker   RegisterClassInfo RegClassInfo;
67*9880d681SAndroid Build Coastguard Worker 
68*9880d681SAndroid Build Coastguard Worker   /// Inst which is a def of an original reg and whose defs are already all
69*9880d681SAndroid Build Coastguard Worker   /// dead after remat is saved in DeadRemats. The deletion of such inst is
70*9880d681SAndroid Build Coastguard Worker   /// postponed till all the allocations are done, so its remat expr is
71*9880d681SAndroid Build Coastguard Worker   /// always available for the remat of all the siblings of the original reg.
72*9880d681SAndroid Build Coastguard Worker   SmallPtrSet<MachineInstr *, 32> DeadRemats;
73*9880d681SAndroid Build Coastguard Worker 
RegAllocBase()74*9880d681SAndroid Build Coastguard Worker   RegAllocBase()
75*9880d681SAndroid Build Coastguard Worker     : TRI(nullptr), MRI(nullptr), VRM(nullptr), LIS(nullptr), Matrix(nullptr) {}
76*9880d681SAndroid Build Coastguard Worker 
~RegAllocBase()77*9880d681SAndroid Build Coastguard Worker   virtual ~RegAllocBase() {}
78*9880d681SAndroid Build Coastguard Worker 
79*9880d681SAndroid Build Coastguard Worker   // A RegAlloc pass should call this before allocatePhysRegs.
80*9880d681SAndroid Build Coastguard Worker   void init(VirtRegMap &vrm, LiveIntervals &lis, LiveRegMatrix &mat);
81*9880d681SAndroid Build Coastguard Worker 
82*9880d681SAndroid Build Coastguard Worker   // The top-level driver. The output is a VirtRegMap that us updated with
83*9880d681SAndroid Build Coastguard Worker   // physical register assignments.
84*9880d681SAndroid Build Coastguard Worker   void allocatePhysRegs();
85*9880d681SAndroid Build Coastguard Worker 
86*9880d681SAndroid Build Coastguard Worker   // Include spiller post optimization and removing dead defs left because of
87*9880d681SAndroid Build Coastguard Worker   // rematerialization.
88*9880d681SAndroid Build Coastguard Worker   virtual void postOptimization();
89*9880d681SAndroid Build Coastguard Worker 
90*9880d681SAndroid Build Coastguard Worker   // Get a temporary reference to a Spiller instance.
91*9880d681SAndroid Build Coastguard Worker   virtual Spiller &spiller() = 0;
92*9880d681SAndroid Build Coastguard Worker 
93*9880d681SAndroid Build Coastguard Worker   /// enqueue - Add VirtReg to the priority queue of unassigned registers.
94*9880d681SAndroid Build Coastguard Worker   virtual void enqueue(LiveInterval *LI) = 0;
95*9880d681SAndroid Build Coastguard Worker 
96*9880d681SAndroid Build Coastguard Worker   /// dequeue - Return the next unassigned register, or NULL.
97*9880d681SAndroid Build Coastguard Worker   virtual LiveInterval *dequeue() = 0;
98*9880d681SAndroid Build Coastguard Worker 
99*9880d681SAndroid Build Coastguard Worker   // A RegAlloc pass should override this to provide the allocation heuristics.
100*9880d681SAndroid Build Coastguard Worker   // Each call must guarantee forward progess by returning an available PhysReg
101*9880d681SAndroid Build Coastguard Worker   // or new set of split live virtual registers. It is up to the splitter to
102*9880d681SAndroid Build Coastguard Worker   // converge quickly toward fully spilled live ranges.
103*9880d681SAndroid Build Coastguard Worker   virtual unsigned selectOrSplit(LiveInterval &VirtReg,
104*9880d681SAndroid Build Coastguard Worker                                  SmallVectorImpl<unsigned> &splitLVRs) = 0;
105*9880d681SAndroid Build Coastguard Worker 
106*9880d681SAndroid Build Coastguard Worker   // Use this group name for NamedRegionTimer.
107*9880d681SAndroid Build Coastguard Worker   static const char TimerGroupName[];
108*9880d681SAndroid Build Coastguard Worker 
109*9880d681SAndroid Build Coastguard Worker   /// Method called when the allocator is about to remove a LiveInterval.
aboutToRemoveInterval(LiveInterval & LI)110*9880d681SAndroid Build Coastguard Worker   virtual void aboutToRemoveInterval(LiveInterval &LI) {}
111*9880d681SAndroid Build Coastguard Worker 
112*9880d681SAndroid Build Coastguard Worker public:
113*9880d681SAndroid Build Coastguard Worker   /// VerifyEnabled - True when -verify-regalloc is given.
114*9880d681SAndroid Build Coastguard Worker   static bool VerifyEnabled;
115*9880d681SAndroid Build Coastguard Worker 
116*9880d681SAndroid Build Coastguard Worker private:
117*9880d681SAndroid Build Coastguard Worker   void seedLiveRegs();
118*9880d681SAndroid Build Coastguard Worker };
119*9880d681SAndroid Build Coastguard Worker 
120*9880d681SAndroid Build Coastguard Worker } // end namespace llvm
121*9880d681SAndroid Build Coastguard Worker 
122*9880d681SAndroid Build Coastguard Worker #endif
123