blob: 438a7d17baefee1d7712da0a4545a004a5f38b24 [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
33// quality trade-off without relying a particular theoretical solver.
34//
35//===----------------------------------------------------------------------===//
36
37#ifndef LLVM_CODEGEN_REGALLOCBASE
38#define LLVM_CODEGEN_REGALLOCBASE
39
Andrew Trick14e8d712010-10-22 23:09:15 +000040#include "llvm/ADT/OwningPtr.h"
Jakob Stoklund Olesen953af2c2010-12-07 23:18:47 +000041#include "LiveIntervalUnion.h"
Jakob Stoklund Olesend0bec3e2010-12-08 22:22:41 +000042#include <queue>
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;
Andrew Trickf4baeaf2010-11-10 19:18:47 +000050class Spiller;
Andrew Tricke16eecc2010-10-26 18:34:01 +000051
Andrew Tricke16eecc2010-10-26 18:34:01 +000052// Forward declare a priority queue of live virtual registers. If an
53// implementation needs to prioritize by anything other than spill weight, then
54// this will become an abstract base class with virtual calls to push/get.
55class LiveVirtRegQueue;
Andrew Trick14e8d712010-10-22 23:09:15 +000056
57/// RegAllocBase provides the register allocation driver and interface that can
58/// be extended to add interesting heuristics.
59///
Andrew Trick18c57a82010-11-30 23:18:47 +000060/// Register allocators must override the selectOrSplit() method to implement
Andrew Trickb853e6c2010-12-09 18:15:21 +000061/// live range splitting. They may also override getPriority() which otherwise
62/// defaults to the spill weight computed by CalculateSpillWeights.
Andrew Trick14e8d712010-10-22 23:09:15 +000063class RegAllocBase {
Jakob Stoklund Olesen953af2c2010-12-07 23:18:47 +000064 LiveIntervalUnion::Allocator UnionAllocator;
Andrew Trick14e8d712010-10-22 23:09:15 +000065protected:
Andrew Trick14e8d712010-10-22 23:09:15 +000066 // Array of LiveIntervalUnions indexed by physical register.
Andrew Trick18c57a82010-11-30 23:18:47 +000067 class LiveUnionArray {
68 unsigned NumRegs;
Jakob Stoklund Olesen953af2c2010-12-07 23:18:47 +000069 LiveIntervalUnion *Array;
Andrew Trick14e8d712010-10-22 23:09:15 +000070 public:
Jakob Stoklund Olesen953af2c2010-12-07 23:18:47 +000071 LiveUnionArray(): NumRegs(0), Array(0) {}
72 ~LiveUnionArray() { clear(); }
Andrew Trick14e8d712010-10-22 23:09:15 +000073
Andrew Trick18c57a82010-11-30 23:18:47 +000074 unsigned numRegs() const { return NumRegs; }
Andrew Trick14e8d712010-10-22 23:09:15 +000075
Jakob Stoklund Olesen953af2c2010-12-07 23:18:47 +000076 void init(LiveIntervalUnion::Allocator &, unsigned NRegs);
Andrew Trick14e8d712010-10-22 23:09:15 +000077
78 void clear();
Andrew Trick18c57a82010-11-30 23:18:47 +000079
80 LiveIntervalUnion& operator[](unsigned PhysReg) {
81 assert(PhysReg < NumRegs && "physReg out of bounds");
82 return Array[PhysReg];
Andrew Trick14e8d712010-10-22 23:09:15 +000083 }
84 };
Andrew Trick18c57a82010-11-30 23:18:47 +000085
86 const TargetRegisterInfo *TRI;
Jakob Stoklund Olesen4680dec2010-12-10 23:49:00 +000087 MachineRegisterInfo *MRI;
Andrew Trick18c57a82010-11-30 23:18:47 +000088 VirtRegMap *VRM;
89 LiveIntervals *LIS;
90 LiveUnionArray PhysReg2LiveUnion;
Andrew Trick14e8d712010-10-22 23:09:15 +000091
Andrew Tricke141a492010-11-08 18:02:08 +000092 // Current queries, one per physreg. They must be reinitialized each time we
93 // query on a new live virtual register.
Andrew Trick18c57a82010-11-30 23:18:47 +000094 OwningArrayPtr<LiveIntervalUnion::Query> Queries;
Andrew Tricke141a492010-11-08 18:02:08 +000095
Jakob Stoklund Olesen4680dec2010-12-10 23:49:00 +000096 RegAllocBase(): TRI(0), MRI(0), VRM(0), LIS(0) {}
Andrew Trick14e8d712010-10-22 23:09:15 +000097
Andrew Trickf4331062010-10-22 23:33:19 +000098 virtual ~RegAllocBase() {}
99
Andrew Trick14e8d712010-10-22 23:09:15 +0000100 // A RegAlloc pass should call this before allocatePhysRegs.
Jakob Stoklund Olesen4680dec2010-12-10 23:49:00 +0000101 void init(VirtRegMap &vrm, LiveIntervals &lis);
Andrew Trick14e8d712010-10-22 23:09:15 +0000102
Andrew Trick8a83d542010-11-11 17:46:29 +0000103 // Get an initialized query to check interferences between lvr and preg. Note
104 // that Query::init must be called at least once for each physical register
Andrew Trick18c57a82010-11-30 23:18:47 +0000105 // before querying a new live virtual register. This ties Queries and
106 // PhysReg2LiveUnion together.
107 LiveIntervalUnion::Query &query(LiveInterval &VirtReg, unsigned PhysReg) {
108 Queries[PhysReg].init(&VirtReg, &PhysReg2LiveUnion[PhysReg]);
109 return Queries[PhysReg];
Andrew Trick8a83d542010-11-11 17:46:29 +0000110 }
Andrew Trick18c57a82010-11-30 23:18:47 +0000111
Andrew Tricke16eecc2010-10-26 18:34:01 +0000112 // The top-level driver. The output is a VirtRegMap that us updated with
113 // physical register assignments.
114 //
115 // If an implementation wants to override the LiveInterval comparator, we
116 // should modify this interface to allow passing in an instance derived from
117 // LiveVirtRegQueue.
118 void allocatePhysRegs();
Andrew Trick14e8d712010-10-22 23:09:15 +0000119
Andrew Trickf4baeaf2010-11-10 19:18:47 +0000120 // Get a temporary reference to a Spiller instance.
121 virtual Spiller &spiller() = 0;
Andrew Trick18c57a82010-11-30 23:18:47 +0000122
Jakob Stoklund Olesend0bec3e2010-12-08 22:22:41 +0000123 // getPriority - Calculate the allocation priority for VirtReg.
124 // Virtual registers with higher priorities are allocated first.
125 virtual float getPriority(LiveInterval *LI) = 0;
126
Andrew Trick14e8d712010-10-22 23:09:15 +0000127 // A RegAlloc pass should override this to provide the allocation heuristics.
Andrew Tricke16eecc2010-10-26 18:34:01 +0000128 // Each call must guarantee forward progess by returning an available PhysReg
129 // or new set of split live virtual registers. It is up to the splitter to
Andrew Trick14e8d712010-10-22 23:09:15 +0000130 // converge quickly toward fully spilled live ranges.
Andrew Trick18c57a82010-11-30 23:18:47 +0000131 virtual unsigned selectOrSplit(LiveInterval &VirtReg,
Andrew Tricke16eecc2010-10-26 18:34:01 +0000132 SmallVectorImpl<LiveInterval*> &splitLVRs) = 0;
Andrew Trick14e8d712010-10-22 23:09:15 +0000133
134 // A RegAlloc pass should call this when PassManager releases its memory.
135 virtual void releaseMemory();
136
137 // Helper for checking interference between a live virtual register and a
Andrew Tricke141a492010-11-08 18:02:08 +0000138 // physical register, including all its register aliases. If an interference
139 // exists, return the interfering register, which may be preg or an alias.
Andrew Trick18c57a82010-11-30 23:18:47 +0000140 unsigned checkPhysRegInterference(LiveInterval& VirtReg, unsigned PhysReg);
Andrew Tricke141a492010-11-08 18:02:08 +0000141
Andrew Trickf4baeaf2010-11-10 19:18:47 +0000142 // Helper for spilling all live virtual registers currently unified under preg
143 // that interfere with the most recently queried lvr. Return true if spilling
144 // was successful, and append any new spilled/split intervals to splitLVRs.
Andrew Trick18c57a82010-11-30 23:18:47 +0000145 bool spillInterferences(LiveInterval &VirtReg, unsigned PhysReg,
146 SmallVectorImpl<LiveInterval*> &SplitVRegs);
Andrew Trickf4baeaf2010-11-10 19:18:47 +0000147
Jakob Stoklund Olesen1b19dc12010-12-08 01:06:06 +0000148 /// addMBBLiveIns - Add physreg liveins to basic blocks.
149 void addMBBLiveIns(MachineFunction *);
150
Andrew Trick071d1c02010-11-09 21:04:34 +0000151#ifndef NDEBUG
152 // Verify each LiveIntervalUnion.
153 void verify();
154#endif
Andrew Trickf4baeaf2010-11-10 19:18:47 +0000155
Jakob Stoklund Olesen533f58e2010-12-11 00:19:56 +0000156 // Use this group name for NamedRegionTimer.
157 static const char *TimerGroupName;
158
Andrew Trick18c57a82010-11-30 23:18:47 +0000159private:
Jakob Stoklund Olesend0bec3e2010-12-08 22:22:41 +0000160 void seedLiveVirtRegs(std::priority_queue<std::pair<float, unsigned> >&);
Andrew Trick18c57a82010-11-30 23:18:47 +0000161
162 void spillReg(LiveInterval &VirtReg, unsigned PhysReg,
163 SmallVectorImpl<LiveInterval*> &SplitVRegs);
Andrew Trick14e8d712010-10-22 23:09:15 +0000164};
165
Andrew Trick14e8d712010-10-22 23:09:15 +0000166} // end namespace llvm
167
168#endif // !defined(LLVM_CODEGEN_REGALLOCBASE)