blob: 367efa4f6a43df83934ec1eb9ce8858585c6f020 [file] [log] [blame]
Andrew Trick14e8d712010-10-22 23:09:15 +00001//===-- RegAllocBase.h - basic regalloc interface and driver --*- C++ -*---===//
2//
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
37#ifndef LLVM_CODEGEN_REGALLOCBASE
38#define LLVM_CODEGEN_REGALLOCBASE
39
Jakob Stoklund Olesen953af2c2010-12-07 23:18:47 +000040#include "LiveIntervalUnion.h"
Andrew Trick15252602012-06-06 20:29:31 +000041#include "llvm/CodeGen/RegisterClassInfo.h"
42#include "llvm/ADT/OwningPtr.h"
Andrew Trick14e8d712010-10-22 23:09:15 +000043
44namespace llvm {
45
Andrew Tricke16eecc2010-10-26 18:34:01 +000046template<typename T> class SmallVectorImpl;
47class TargetRegisterInfo;
Andrew Trick14e8d712010-10-22 23:09:15 +000048class VirtRegMap;
Andrew Tricke16eecc2010-10-26 18:34:01 +000049class LiveIntervals;
Jakob Stoklund Olesen812cda92012-06-20 22:52:24 +000050class LiveRegMatrix;
Andrew Trickf4baeaf2010-11-10 19:18:47 +000051class Spiller;
Andrew Tricke16eecc2010-10-26 18:34:01 +000052
Andrew Trick14e8d712010-10-22 23:09:15 +000053/// RegAllocBase provides the register allocation driver and interface that can
54/// be extended to add interesting heuristics.
55///
Andrew Trick18c57a82010-11-30 23:18:47 +000056/// Register allocators must override the selectOrSplit() method to implement
Jakob Stoklund Olesen98d96482011-02-22 23:01:52 +000057/// live range splitting. They must also override enqueue/dequeue to provide an
58/// assignment order.
Andrew Trick14e8d712010-10-22 23:09:15 +000059class RegAllocBase {
Jakob Stoklund Olesen953af2c2010-12-07 23:18:47 +000060 LiveIntervalUnion::Allocator UnionAllocator;
Jakob Stoklund Olesen29267332011-03-16 22:56:11 +000061
62 // Cache tag for PhysReg2LiveUnion entries. Increment whenever virtual
63 // registers may have changed.
64 unsigned UserTag;
65
Jakob Stoklund Olesen0e5a60b2012-06-05 23:57:30 +000066 LiveIntervalUnion::Array PhysReg2LiveUnion;
Andrew Trick14e8d712010-10-22 23:09:15 +000067
Andrew Tricke141a492010-11-08 18:02:08 +000068 // Current queries, one per physreg. They must be reinitialized each time we
69 // query on a new live virtual register.
Andrew Trick18c57a82010-11-30 23:18:47 +000070 OwningArrayPtr<LiveIntervalUnion::Query> Queries;
Andrew Tricke141a492010-11-08 18:02:08 +000071
Jakob Stoklund Olesen93841112012-01-11 23:19:08 +000072protected:
73 const TargetRegisterInfo *TRI;
74 MachineRegisterInfo *MRI;
75 VirtRegMap *VRM;
76 LiveIntervals *LIS;
Jakob Stoklund Olesen812cda92012-06-20 22:52:24 +000077 LiveRegMatrix *Matrix;
Jakob Stoklund Olesen93841112012-01-11 23:19:08 +000078 RegisterClassInfo RegClassInfo;
79
Jakob Stoklund Olesen812cda92012-06-20 22:52:24 +000080 RegAllocBase(): UserTag(0), TRI(0), MRI(0), VRM(0), LIS(0), Matrix(0) {}
Andrew Trick14e8d712010-10-22 23:09:15 +000081
Andrew Trickf4331062010-10-22 23:33:19 +000082 virtual ~RegAllocBase() {}
83
Andrew Trick14e8d712010-10-22 23:09:15 +000084 // A RegAlloc pass should call this before allocatePhysRegs.
Jakob Stoklund Olesen4680dec2010-12-10 23:49:00 +000085 void init(VirtRegMap &vrm, LiveIntervals &lis);
Andrew Trick14e8d712010-10-22 23:09:15 +000086
Andrew Trick8a83d542010-11-11 17:46:29 +000087 // Get an initialized query to check interferences between lvr and preg. Note
88 // that Query::init must be called at least once for each physical register
Andrew Trick18c57a82010-11-30 23:18:47 +000089 // before querying a new live virtual register. This ties Queries and
90 // PhysReg2LiveUnion together.
91 LiveIntervalUnion::Query &query(LiveInterval &VirtReg, unsigned PhysReg) {
Jakob Stoklund Olesen29267332011-03-16 22:56:11 +000092 Queries[PhysReg].init(UserTag, &VirtReg, &PhysReg2LiveUnion[PhysReg]);
Andrew Trick18c57a82010-11-30 23:18:47 +000093 return Queries[PhysReg];
Andrew Trick8a83d542010-11-11 17:46:29 +000094 }
Andrew Trick18c57a82010-11-30 23:18:47 +000095
Jakob Stoklund Olesen93841112012-01-11 23:19:08 +000096 // Get direct access to the underlying LiveIntervalUnion for PhysReg.
97 LiveIntervalUnion &getLiveUnion(unsigned PhysReg) {
98 return PhysReg2LiveUnion[PhysReg];
99 }
100
Jakob Stoklund Olesenbdda37d2011-05-10 17:37:41 +0000101 // Invalidate all cached information about virtual registers - live ranges may
102 // have changed.
103 void invalidateVirtRegs() { ++UserTag; }
104
Andrew Tricke16eecc2010-10-26 18:34:01 +0000105 // The top-level driver. The output is a VirtRegMap that us updated with
106 // physical register assignments.
Andrew Tricke16eecc2010-10-26 18:34:01 +0000107 void allocatePhysRegs();
Andrew Trick14e8d712010-10-22 23:09:15 +0000108
Andrew Trickf4baeaf2010-11-10 19:18:47 +0000109 // Get a temporary reference to a Spiller instance.
110 virtual Spiller &spiller() = 0;
Andrew Trick18c57a82010-11-30 23:18:47 +0000111
Jakob Stoklund Olesen98d96482011-02-22 23:01:52 +0000112 /// enqueue - Add VirtReg to the priority queue of unassigned registers.
113 virtual void enqueue(LiveInterval *LI) = 0;
114
115 /// dequeue - Return the next unassigned register, or NULL.
116 virtual LiveInterval *dequeue() = 0;
Jakob Stoklund Olesend0bec3e2010-12-08 22:22:41 +0000117
Andrew Trick14e8d712010-10-22 23:09:15 +0000118 // A RegAlloc pass should override this to provide the allocation heuristics.
Andrew Tricke16eecc2010-10-26 18:34:01 +0000119 // 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
Andrew Trick14e8d712010-10-22 23:09:15 +0000121 // converge quickly toward fully spilled live ranges.
Andrew Trick18c57a82010-11-30 23:18:47 +0000122 virtual unsigned selectOrSplit(LiveInterval &VirtReg,
Andrew Tricke16eecc2010-10-26 18:34:01 +0000123 SmallVectorImpl<LiveInterval*> &splitLVRs) = 0;
Andrew Trick14e8d712010-10-22 23:09:15 +0000124
125 // A RegAlloc pass should call this when PassManager releases its memory.
126 virtual void releaseMemory();
127
128 // Helper for checking interference between a live virtual register and a
Andrew Tricke141a492010-11-08 18:02:08 +0000129 // physical register, including all its register aliases. If an interference
130 // exists, return the interfering register, which may be preg or an alias.
Andrew Trick18c57a82010-11-30 23:18:47 +0000131 unsigned checkPhysRegInterference(LiveInterval& VirtReg, unsigned PhysReg);
Andrew Tricke141a492010-11-08 18:02:08 +0000132
Jakob Stoklund Olesen27106382011-02-09 01:14:03 +0000133 /// assign - Assign VirtReg to PhysReg.
134 /// This should not be called from selectOrSplit for the current register.
135 void assign(LiveInterval &VirtReg, unsigned PhysReg);
136
137 /// unassign - Undo a previous assignment of VirtReg to PhysReg.
138 /// This can be invoked from selectOrSplit, but be careful to guarantee that
139 /// allocation is making progress.
140 void unassign(LiveInterval &VirtReg, unsigned PhysReg);
141
Andrew Trick071d1c02010-11-09 21:04:34 +0000142#ifndef NDEBUG
143 // Verify each LiveIntervalUnion.
144 void verify();
145#endif
Andrew Trickf4baeaf2010-11-10 19:18:47 +0000146
Jakob Stoklund Olesen533f58e2010-12-11 00:19:56 +0000147 // Use this group name for NamedRegionTimer.
148 static const char *TimerGroupName;
149
Jakob Stoklund Olesenaf249642010-12-17 23:16:35 +0000150public:
151 /// VerifyEnabled - True when -verify-regalloc is given.
152 static bool VerifyEnabled;
153
Andrew Trick18c57a82010-11-30 23:18:47 +0000154private:
Jakob Stoklund Olesen98d96482011-02-22 23:01:52 +0000155 void seedLiveRegs();
Andrew Trick14e8d712010-10-22 23:09:15 +0000156};
157
Andrew Trick14e8d712010-10-22 23:09:15 +0000158} // end namespace llvm
159
160#endif // !defined(LLVM_CODEGEN_REGALLOCBASE)