blob: 7f38c9b95a974ff6b744941aa2b18d3458ae8889 [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"
Andrew Trick14e8d712010-10-22 23:09:15 +000042
43namespace llvm {
44
Andrew Tricke16eecc2010-10-26 18:34:01 +000045template<typename T> class SmallVectorImpl;
46class TargetRegisterInfo;
Andrew Trick14e8d712010-10-22 23:09:15 +000047class VirtRegMap;
Andrew Tricke16eecc2010-10-26 18:34:01 +000048class LiveIntervals;
Andrew Trickf4baeaf2010-11-10 19:18:47 +000049class Spiller;
Andrew Tricke16eecc2010-10-26 18:34:01 +000050
51// Heuristic that determines the priority of assigning virtual to physical
52// registers. The main impact of the heuristic is expected to be compile time.
53// The default is to simply compare spill weights.
54struct LessSpillWeightPriority
55 : public std::binary_function<LiveInterval,LiveInterval, bool> {
Andrew Trick18c57a82010-11-30 23:18:47 +000056 bool operator()(const LiveInterval *Left, const LiveInterval *Right) const {
57 return Left->weight < Right->weight;
Andrew Tricke16eecc2010-10-26 18:34:01 +000058 }
59};
60
61// Forward declare a priority queue of live virtual registers. If an
62// implementation needs to prioritize by anything other than spill weight, then
63// this will become an abstract base class with virtual calls to push/get.
64class LiveVirtRegQueue;
Andrew Trick14e8d712010-10-22 23:09:15 +000065
66/// RegAllocBase provides the register allocation driver and interface that can
67/// be extended to add interesting heuristics.
68///
Andrew Trick18c57a82010-11-30 23:18:47 +000069/// Register allocators must override the selectOrSplit() method to implement
70/// live range splitting. LessSpillWeightPriority is provided as a standard
71/// comparator, but we may add an interface to override it if necessary.
Andrew Trick14e8d712010-10-22 23:09:15 +000072class RegAllocBase {
Jakob Stoklund Olesen953af2c2010-12-07 23:18:47 +000073 LiveIntervalUnion::Allocator UnionAllocator;
Andrew Trick14e8d712010-10-22 23:09:15 +000074protected:
Andrew Trick14e8d712010-10-22 23:09:15 +000075 // Array of LiveIntervalUnions indexed by physical register.
Andrew Trick18c57a82010-11-30 23:18:47 +000076 class LiveUnionArray {
77 unsigned NumRegs;
Jakob Stoklund Olesen953af2c2010-12-07 23:18:47 +000078 LiveIntervalUnion *Array;
Andrew Trick14e8d712010-10-22 23:09:15 +000079 public:
Jakob Stoklund Olesen953af2c2010-12-07 23:18:47 +000080 LiveUnionArray(): NumRegs(0), Array(0) {}
81 ~LiveUnionArray() { clear(); }
Andrew Trick14e8d712010-10-22 23:09:15 +000082
Andrew Trick18c57a82010-11-30 23:18:47 +000083 unsigned numRegs() const { return NumRegs; }
Andrew Trick14e8d712010-10-22 23:09:15 +000084
Jakob Stoklund Olesen953af2c2010-12-07 23:18:47 +000085 void init(LiveIntervalUnion::Allocator &, unsigned NRegs);
Andrew Trick14e8d712010-10-22 23:09:15 +000086
87 void clear();
Andrew Trick18c57a82010-11-30 23:18:47 +000088
89 LiveIntervalUnion& operator[](unsigned PhysReg) {
90 assert(PhysReg < NumRegs && "physReg out of bounds");
91 return Array[PhysReg];
Andrew Trick14e8d712010-10-22 23:09:15 +000092 }
93 };
Andrew Trick18c57a82010-11-30 23:18:47 +000094
95 const TargetRegisterInfo *TRI;
96 VirtRegMap *VRM;
97 LiveIntervals *LIS;
98 LiveUnionArray PhysReg2LiveUnion;
Andrew Trick14e8d712010-10-22 23:09:15 +000099
Andrew Tricke141a492010-11-08 18:02:08 +0000100 // Current queries, one per physreg. They must be reinitialized each time we
101 // query on a new live virtual register.
Andrew Trick18c57a82010-11-30 23:18:47 +0000102 OwningArrayPtr<LiveIntervalUnion::Query> Queries;
Andrew Tricke141a492010-11-08 18:02:08 +0000103
Andrew Trick18c57a82010-11-30 23:18:47 +0000104 RegAllocBase(): TRI(0), VRM(0), LIS(0) {}
Andrew Trick14e8d712010-10-22 23:09:15 +0000105
Andrew Trickf4331062010-10-22 23:33:19 +0000106 virtual ~RegAllocBase() {}
107
Andrew Trick14e8d712010-10-22 23:09:15 +0000108 // A RegAlloc pass should call this before allocatePhysRegs.
109 void init(const TargetRegisterInfo &tri, VirtRegMap &vrm, LiveIntervals &lis);
110
Andrew Trick8a83d542010-11-11 17:46:29 +0000111 // Get an initialized query to check interferences between lvr and preg. Note
112 // that Query::init must be called at least once for each physical register
Andrew Trick18c57a82010-11-30 23:18:47 +0000113 // before querying a new live virtual register. This ties Queries and
114 // PhysReg2LiveUnion together.
115 LiveIntervalUnion::Query &query(LiveInterval &VirtReg, unsigned PhysReg) {
116 Queries[PhysReg].init(&VirtReg, &PhysReg2LiveUnion[PhysReg]);
117 return Queries[PhysReg];
Andrew Trick8a83d542010-11-11 17:46:29 +0000118 }
Andrew Trick18c57a82010-11-30 23:18:47 +0000119
Andrew Tricke16eecc2010-10-26 18:34:01 +0000120 // The top-level driver. The output is a VirtRegMap that us updated with
121 // physical register assignments.
122 //
123 // If an implementation wants to override the LiveInterval comparator, we
124 // should modify this interface to allow passing in an instance derived from
125 // LiveVirtRegQueue.
126 void allocatePhysRegs();
Andrew Trick14e8d712010-10-22 23:09:15 +0000127
Andrew Trickf4baeaf2010-11-10 19:18:47 +0000128 // Get a temporary reference to a Spiller instance.
129 virtual Spiller &spiller() = 0;
Andrew Trick18c57a82010-11-30 23:18:47 +0000130
Andrew Trick14e8d712010-10-22 23:09:15 +0000131 // A RegAlloc pass should override this to provide the allocation heuristics.
Andrew Tricke16eecc2010-10-26 18:34:01 +0000132 // Each call must guarantee forward progess by returning an available PhysReg
133 // or new set of split live virtual registers. It is up to the splitter to
Andrew Trick14e8d712010-10-22 23:09:15 +0000134 // converge quickly toward fully spilled live ranges.
Andrew Trick18c57a82010-11-30 23:18:47 +0000135 virtual unsigned selectOrSplit(LiveInterval &VirtReg,
Andrew Tricke16eecc2010-10-26 18:34:01 +0000136 SmallVectorImpl<LiveInterval*> &splitLVRs) = 0;
Andrew Trick14e8d712010-10-22 23:09:15 +0000137
138 // A RegAlloc pass should call this when PassManager releases its memory.
139 virtual void releaseMemory();
140
141 // Helper for checking interference between a live virtual register and a
Andrew Tricke141a492010-11-08 18:02:08 +0000142 // physical register, including all its register aliases. If an interference
143 // exists, return the interfering register, which may be preg or an alias.
Andrew Trick18c57a82010-11-30 23:18:47 +0000144 unsigned checkPhysRegInterference(LiveInterval& VirtReg, unsigned PhysReg);
Andrew Tricke141a492010-11-08 18:02:08 +0000145
Andrew Trickf4baeaf2010-11-10 19:18:47 +0000146 // Helper for spilling all live virtual registers currently unified under preg
147 // that interfere with the most recently queried lvr. Return true if spilling
148 // was successful, and append any new spilled/split intervals to splitLVRs.
Andrew Trick18c57a82010-11-30 23:18:47 +0000149 bool spillInterferences(LiveInterval &VirtReg, unsigned PhysReg,
150 SmallVectorImpl<LiveInterval*> &SplitVRegs);
Andrew Trickf4baeaf2010-11-10 19:18:47 +0000151
Jakob Stoklund Olesen1b19dc12010-12-08 01:06:06 +0000152 /// addMBBLiveIns - Add physreg liveins to basic blocks.
153 void addMBBLiveIns(MachineFunction *);
154
Andrew Trick071d1c02010-11-09 21:04:34 +0000155#ifndef NDEBUG
156 // Verify each LiveIntervalUnion.
157 void verify();
158#endif
Andrew Trickf4baeaf2010-11-10 19:18:47 +0000159
Andrew Trick18c57a82010-11-30 23:18:47 +0000160private:
161 void seedLiveVirtRegs(LiveVirtRegQueue &VirtRegQ);
162
163 void spillReg(LiveInterval &VirtReg, unsigned PhysReg,
164 SmallVectorImpl<LiveInterval*> &SplitVRegs);
Andrew Trick14e8d712010-10-22 23:09:15 +0000165};
166
Andrew Trick14e8d712010-10-22 23:09:15 +0000167} // end namespace llvm
168
169#endif // !defined(LLVM_CODEGEN_REGALLOCBASE)