blob: 8044a192385cadbce826ce852c3a1c58b04f06e3 [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"
Andrew Trick14e8d712010-10-22 23:09:15 +000041
42namespace llvm {
43
Andrew Tricke16eecc2010-10-26 18:34:01 +000044template<typename T> class SmallVectorImpl;
45class TargetRegisterInfo;
Andrew Trick14e8d712010-10-22 23:09:15 +000046class VirtRegMap;
Andrew Tricke16eecc2010-10-26 18:34:01 +000047class LiveIntervals;
Andrew Trickf4baeaf2010-11-10 19:18:47 +000048class Spiller;
Andrew Tricke16eecc2010-10-26 18:34:01 +000049
50// Heuristic that determines the priority of assigning virtual to physical
51// registers. The main impact of the heuristic is expected to be compile time.
52// The default is to simply compare spill weights.
53struct LessSpillWeightPriority
54 : public std::binary_function<LiveInterval,LiveInterval, bool> {
Andrew Trick18c57a82010-11-30 23:18:47 +000055 bool operator()(const LiveInterval *Left, const LiveInterval *Right) const {
56 return Left->weight < Right->weight;
Andrew Tricke16eecc2010-10-26 18:34:01 +000057 }
58};
59
60// Forward declare a priority queue of live virtual registers. If an
61// implementation needs to prioritize by anything other than spill weight, then
62// this will become an abstract base class with virtual calls to push/get.
63class LiveVirtRegQueue;
Andrew Trick14e8d712010-10-22 23:09:15 +000064
65/// RegAllocBase provides the register allocation driver and interface that can
66/// be extended to add interesting heuristics.
67///
Andrew Trick18c57a82010-11-30 23:18:47 +000068/// Register allocators must override the selectOrSplit() method to implement
69/// live range splitting. LessSpillWeightPriority is provided as a standard
70/// comparator, but we may add an interface to override it if necessary.
Andrew Trick14e8d712010-10-22 23:09:15 +000071class RegAllocBase {
72protected:
Andrew Trick14e8d712010-10-22 23:09:15 +000073 // Array of LiveIntervalUnions indexed by physical register.
Andrew Trick18c57a82010-11-30 23:18:47 +000074 class LiveUnionArray {
75 unsigned NumRegs;
76 OwningArrayPtr<LiveIntervalUnion> Array;
Andrew Trick14e8d712010-10-22 23:09:15 +000077 public:
Andrew Trick18c57a82010-11-30 23:18:47 +000078 LiveUnionArray(): NumRegs(0) {}
Andrew Trick14e8d712010-10-22 23:09:15 +000079
Andrew Trick18c57a82010-11-30 23:18:47 +000080 unsigned numRegs() const { return NumRegs; }
Andrew Trick14e8d712010-10-22 23:09:15 +000081
Andrew Trick18c57a82010-11-30 23:18:47 +000082 void init(unsigned NRegs);
Andrew Trick14e8d712010-10-22 23:09:15 +000083
84 void clear();
Andrew Trick18c57a82010-11-30 23:18:47 +000085
86 LiveIntervalUnion& operator[](unsigned PhysReg) {
87 assert(PhysReg < NumRegs && "physReg out of bounds");
88 return Array[PhysReg];
Andrew Trick14e8d712010-10-22 23:09:15 +000089 }
90 };
Andrew Trick18c57a82010-11-30 23:18:47 +000091
92 const TargetRegisterInfo *TRI;
93 VirtRegMap *VRM;
94 LiveIntervals *LIS;
95 LiveUnionArray PhysReg2LiveUnion;
Andrew Trick14e8d712010-10-22 23:09:15 +000096
Andrew Tricke141a492010-11-08 18:02:08 +000097 // Current queries, one per physreg. They must be reinitialized each time we
98 // query on a new live virtual register.
Andrew Trick18c57a82010-11-30 23:18:47 +000099 OwningArrayPtr<LiveIntervalUnion::Query> Queries;
Andrew Tricke141a492010-11-08 18:02:08 +0000100
Andrew Trick18c57a82010-11-30 23:18:47 +0000101 RegAllocBase(): TRI(0), VRM(0), LIS(0) {}
Andrew Trick14e8d712010-10-22 23:09:15 +0000102
Andrew Trickf4331062010-10-22 23:33:19 +0000103 virtual ~RegAllocBase() {}
104
Andrew Trick14e8d712010-10-22 23:09:15 +0000105 // A RegAlloc pass should call this before allocatePhysRegs.
106 void init(const TargetRegisterInfo &tri, VirtRegMap &vrm, LiveIntervals &lis);
107
Andrew Trick8a83d542010-11-11 17:46:29 +0000108 // Get an initialized query to check interferences between lvr and preg. Note
109 // that Query::init must be called at least once for each physical register
Andrew Trick18c57a82010-11-30 23:18:47 +0000110 // before querying a new live virtual register. This ties Queries and
111 // PhysReg2LiveUnion together.
112 LiveIntervalUnion::Query &query(LiveInterval &VirtReg, unsigned PhysReg) {
113 Queries[PhysReg].init(&VirtReg, &PhysReg2LiveUnion[PhysReg]);
114 return Queries[PhysReg];
Andrew Trick8a83d542010-11-11 17:46:29 +0000115 }
Andrew Trick18c57a82010-11-30 23:18:47 +0000116
Andrew Tricke16eecc2010-10-26 18:34:01 +0000117 // The top-level driver. The output is a VirtRegMap that us updated with
118 // physical register assignments.
119 //
120 // If an implementation wants to override the LiveInterval comparator, we
121 // should modify this interface to allow passing in an instance derived from
122 // LiveVirtRegQueue.
123 void allocatePhysRegs();
Andrew Trick14e8d712010-10-22 23:09:15 +0000124
Andrew Trickf4baeaf2010-11-10 19:18:47 +0000125 // Get a temporary reference to a Spiller instance.
126 virtual Spiller &spiller() = 0;
Andrew Trick18c57a82010-11-30 23:18:47 +0000127
Andrew Trick14e8d712010-10-22 23:09:15 +0000128 // A RegAlloc pass should override this to provide the allocation heuristics.
Andrew Tricke16eecc2010-10-26 18:34:01 +0000129 // Each call must guarantee forward progess by returning an available PhysReg
130 // or new set of split live virtual registers. It is up to the splitter to
Andrew Trick14e8d712010-10-22 23:09:15 +0000131 // converge quickly toward fully spilled live ranges.
Andrew Trick18c57a82010-11-30 23:18:47 +0000132 virtual unsigned selectOrSplit(LiveInterval &VirtReg,
Andrew Tricke16eecc2010-10-26 18:34:01 +0000133 SmallVectorImpl<LiveInterval*> &splitLVRs) = 0;
Andrew Trick14e8d712010-10-22 23:09:15 +0000134
135 // A RegAlloc pass should call this when PassManager releases its memory.
136 virtual void releaseMemory();
137
138 // Helper for checking interference between a live virtual register and a
Andrew Tricke141a492010-11-08 18:02:08 +0000139 // physical register, including all its register aliases. If an interference
140 // exists, return the interfering register, which may be preg or an alias.
Andrew Trick18c57a82010-11-30 23:18:47 +0000141 unsigned checkPhysRegInterference(LiveInterval& VirtReg, unsigned PhysReg);
Andrew Tricke141a492010-11-08 18:02:08 +0000142
Andrew Trickf4baeaf2010-11-10 19:18:47 +0000143 // Helper for spilling all live virtual registers currently unified under preg
144 // that interfere with the most recently queried lvr. Return true if spilling
145 // was successful, and append any new spilled/split intervals to splitLVRs.
Andrew Trick18c57a82010-11-30 23:18:47 +0000146 bool spillInterferences(LiveInterval &VirtReg, unsigned PhysReg,
147 SmallVectorImpl<LiveInterval*> &SplitVRegs);
Andrew Trickf4baeaf2010-11-10 19:18:47 +0000148
Andrew Trick071d1c02010-11-09 21:04:34 +0000149#ifndef NDEBUG
150 // Verify each LiveIntervalUnion.
151 void verify();
152#endif
Andrew Trickf4baeaf2010-11-10 19:18:47 +0000153
Andrew Trick18c57a82010-11-30 23:18:47 +0000154private:
155 void seedLiveVirtRegs(LiveVirtRegQueue &VirtRegQ);
156
157 void spillReg(LiveInterval &VirtReg, unsigned PhysReg,
158 SmallVectorImpl<LiveInterval*> &SplitVRegs);
Andrew Trick14e8d712010-10-22 23:09:15 +0000159};
160
Andrew Trick14e8d712010-10-22 23:09:15 +0000161} // end namespace llvm
162
163#endif // !defined(LLVM_CODEGEN_REGALLOCBASE)