blob: 686ffc36e0493e1305ce8063d22b2eb39e044ec2 [file] [log] [blame]
Eugene Zelenko16ffaf82017-09-13 21:15:20 +00001//===- RegAllocBase.h - basic regalloc interface and driver -----*- C++ -*-===//
Andrew Trick14e8d712010-10-22 23:09:15 +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//
10// This file defines the RegAllocBase class, which is the skeleton of a basic
11// register allocation algorithm and interface for extending it. It provides the
12// building blocks on which to construct other experimental allocators and test
13// the validity of two principles:
Andrew Trick18c57a82010-11-30 23:18:47 +000014//
Andrew Trick14e8d712010-10-22 23:09:15 +000015// - If virtual and physical register liveness is modeled using intervals, then
16// on-the-fly interference checking is cheap. Furthermore, interferences can be
17// lazily cached and reused.
Andrew Trick18c57a82010-11-30 23:18:47 +000018//
Andrew Trick14e8d712010-10-22 23:09:15 +000019// - Register allocation complexity, and generated code performance is
20// determined by the effectiveness of live range splitting rather than optimal
21// coloring.
22//
23// Following the first principle, interfering checking revolves around the
24// LiveIntervalUnion data structure.
25//
26// To fulfill the second principle, the basic allocator provides a driver for
27// incremental splitting. It essentially punts on the problem of register
28// coloring, instead driving the assignment of virtual to physical registers by
29// the cost of splitting. The basic allocator allows for heuristic reassignment
30// of registers, if a more sophisticated allocator chooses to do that.
31//
32// This framework provides a way to engineer the compile time vs. code
Cameron Zwarich7fb95d42010-12-29 04:42:39 +000033// quality trade-off without relying on a particular theoretical solver.
Andrew Trick14e8d712010-10-22 23:09:15 +000034//
35//===----------------------------------------------------------------------===//
36
Benjamin Kramer00e08fc2014-08-13 16:26:38 +000037#ifndef LLVM_LIB_CODEGEN_REGALLOCBASE_H
38#define LLVM_LIB_CODEGEN_REGALLOCBASE_H
Andrew Trick14e8d712010-10-22 23:09:15 +000039
Eugene Zelenko16ffaf82017-09-13 21:15:20 +000040#include "llvm/ADT/SmallPtrSet.h"
Chandler Carrutha1514e22012-12-04 07:12:27 +000041#include "llvm/CodeGen/RegisterClassInfo.h"
Andrew Trick14e8d712010-10-22 23:09:15 +000042
43namespace llvm {
44
Eugene Zelenko16ffaf82017-09-13 21:15:20 +000045class LiveInterval;
Andrew Tricke16eecc2010-10-26 18:34:01 +000046class LiveIntervals;
Jakob Stoklund Olesen812cda92012-06-20 22:52:24 +000047class LiveRegMatrix;
Eugene Zelenko16ffaf82017-09-13 21:15:20 +000048class MachineInstr;
49class MachineRegisterInfo;
50template<typename T> class SmallVectorImpl;
Andrew Trickf4baeaf2010-11-10 19:18:47 +000051class Spiller;
Eugene Zelenko16ffaf82017-09-13 21:15:20 +000052class TargetRegisterInfo;
53class VirtRegMap;
Andrew Tricke16eecc2010-10-26 18:34:01 +000054
Andrew Trick14e8d712010-10-22 23:09:15 +000055/// RegAllocBase provides the register allocation driver and interface that can
56/// be extended to add interesting heuristics.
57///
Andrew Trick18c57a82010-11-30 23:18:47 +000058/// Register allocators must override the selectOrSplit() method to implement
Jakob Stoklund Olesen98d96482011-02-22 23:01:52 +000059/// live range splitting. They must also override enqueue/dequeue to provide an
60/// assignment order.
Benjamin Kramer55c06ae2013-09-11 18:05:11 +000061class RegAllocBase {
Juergen Ributzka35436252013-11-19 00:57:56 +000062 virtual void anchor();
Eugene Zelenko16ffaf82017-09-13 21:15:20 +000063
Jakob Stoklund Olesen93841112012-01-11 23:19:08 +000064protected:
Eugene Zelenko16ffaf82017-09-13 21:15:20 +000065 const TargetRegisterInfo *TRI = nullptr;
66 MachineRegisterInfo *MRI = nullptr;
67 VirtRegMap *VRM = nullptr;
68 LiveIntervals *LIS = nullptr;
69 LiveRegMatrix *Matrix = nullptr;
Jakob Stoklund Olesen93841112012-01-11 23:19:08 +000070 RegisterClassInfo RegClassInfo;
71
Wei Mi815b02e2016-04-13 03:08:27 +000072 /// Inst which is a def of an original reg and whose defs are already all
73 /// dead after remat is saved in DeadRemats. The deletion of such inst is
74 /// postponed till all the allocations are done, so its remat expr is
75 /// always available for the remat of all the siblings of the original reg.
76 SmallPtrSet<MachineInstr *, 32> DeadRemats;
77
Eugene Zelenko16ffaf82017-09-13 21:15:20 +000078 RegAllocBase() = default;
79 virtual ~RegAllocBase() = default;
Andrew Trickf4331062010-10-22 23:33:19 +000080
Andrew Trick14e8d712010-10-22 23:09:15 +000081 // A RegAlloc pass should call this before allocatePhysRegs.
Jakob Stoklund Olesend4348a22012-06-20 22:52:29 +000082 void init(VirtRegMap &vrm, LiveIntervals &lis, LiveRegMatrix &mat);
Jakob Stoklund Olesenbdda37d2011-05-10 17:37:41 +000083
Andrew Tricke16eecc2010-10-26 18:34:01 +000084 // The top-level driver. The output is a VirtRegMap that us updated with
85 // physical register assignments.
Andrew Tricke16eecc2010-10-26 18:34:01 +000086 void allocatePhysRegs();
Andrew Trick14e8d712010-10-22 23:09:15 +000087
Wei Mi815b02e2016-04-13 03:08:27 +000088 // Include spiller post optimization and removing dead defs left because of
89 // rematerialization.
90 virtual void postOptimization();
91
Andrew Trickf4baeaf2010-11-10 19:18:47 +000092 // Get a temporary reference to a Spiller instance.
93 virtual Spiller &spiller() = 0;
Andrew Trick18c57a82010-11-30 23:18:47 +000094
Jakob Stoklund Olesen98d96482011-02-22 23:01:52 +000095 /// enqueue - Add VirtReg to the priority queue of unassigned registers.
96 virtual void enqueue(LiveInterval *LI) = 0;
97
98 /// dequeue - Return the next unassigned register, or NULL.
99 virtual LiveInterval *dequeue() = 0;
Jakob Stoklund Olesend0bec3e2010-12-08 22:22:41 +0000100
Andrew Trick14e8d712010-10-22 23:09:15 +0000101 // A RegAlloc pass should override this to provide the allocation heuristics.
Andrew Tricke16eecc2010-10-26 18:34:01 +0000102 // Each call must guarantee forward progess by returning an available PhysReg
103 // or new set of split live virtual registers. It is up to the splitter to
Andrew Trick14e8d712010-10-22 23:09:15 +0000104 // converge quickly toward fully spilled live ranges.
Andrew Trick18c57a82010-11-30 23:18:47 +0000105 virtual unsigned selectOrSplit(LiveInterval &VirtReg,
Mark Lacey1feb5852013-08-14 23:50:04 +0000106 SmallVectorImpl<unsigned> &splitLVRs) = 0;
Andrew Trick14e8d712010-10-22 23:09:15 +0000107
Jakob Stoklund Olesen533f58e2010-12-11 00:19:56 +0000108 // Use this group name for NamedRegionTimer.
Craig Topper8de25f02013-07-17 03:11:32 +0000109 static const char TimerGroupName[];
Matthias Braun9262f002016-11-18 19:43:18 +0000110 static const char TimerGroupDescription[];
Jakob Stoklund Olesen533f58e2010-12-11 00:19:56 +0000111
Quentin Colombet9d60e0f2015-01-08 01:16:39 +0000112 /// Method called when the allocator is about to remove a LiveInterval.
113 virtual void aboutToRemoveInterval(LiveInterval &LI) {}
114
Jakob Stoklund Olesenaf249642010-12-17 23:16:35 +0000115public:
116 /// VerifyEnabled - True when -verify-regalloc is given.
117 static bool VerifyEnabled;
118
Andrew Trick18c57a82010-11-30 23:18:47 +0000119private:
Jakob Stoklund Olesen98d96482011-02-22 23:01:52 +0000120 void seedLiveRegs();
Andrew Trick14e8d712010-10-22 23:09:15 +0000121};
122
Andrew Trick14e8d712010-10-22 23:09:15 +0000123} // end namespace llvm
124
Eugene Zelenko16ffaf82017-09-13 21:15:20 +0000125#endif // LLVM_LIB_CODEGEN_REGALLOCBASE_H