blob: 5af0ce79acf7ce58777034717f913974839f21be [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
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
Andrew Tricke16eecc2010-10-26 18:34:01 +000051// Forward declare a priority queue of live virtual registers. If an
52// implementation needs to prioritize by anything other than spill weight, then
53// this will become an abstract base class with virtual calls to push/get.
54class LiveVirtRegQueue;
Andrew Trick14e8d712010-10-22 23:09:15 +000055
56/// RegAllocBase provides the register allocation driver and interface that can
57/// be extended to add interesting heuristics.
58///
Andrew Trick18c57a82010-11-30 23:18:47 +000059/// Register allocators must override the selectOrSplit() method to implement
Jakob Stoklund Olesen98d96482011-02-22 23:01:52 +000060/// live range splitting. They must also override enqueue/dequeue to provide an
61/// assignment order.
Andrew Trick14e8d712010-10-22 23:09:15 +000062class RegAllocBase {
Jakob Stoklund Olesen953af2c2010-12-07 23:18:47 +000063 LiveIntervalUnion::Allocator UnionAllocator;
Andrew Trick14e8d712010-10-22 23:09:15 +000064protected:
Andrew Trick14e8d712010-10-22 23:09:15 +000065 // Array of LiveIntervalUnions indexed by physical register.
Andrew Trick18c57a82010-11-30 23:18:47 +000066 class LiveUnionArray {
67 unsigned NumRegs;
Jakob Stoklund Olesen953af2c2010-12-07 23:18:47 +000068 LiveIntervalUnion *Array;
Andrew Trick14e8d712010-10-22 23:09:15 +000069 public:
Jakob Stoklund Olesen953af2c2010-12-07 23:18:47 +000070 LiveUnionArray(): NumRegs(0), Array(0) {}
71 ~LiveUnionArray() { clear(); }
Andrew Trick14e8d712010-10-22 23:09:15 +000072
Andrew Trick18c57a82010-11-30 23:18:47 +000073 unsigned numRegs() const { return NumRegs; }
Andrew Trick14e8d712010-10-22 23:09:15 +000074
Jakob Stoklund Olesen953af2c2010-12-07 23:18:47 +000075 void init(LiveIntervalUnion::Allocator &, unsigned NRegs);
Andrew Trick14e8d712010-10-22 23:09:15 +000076
77 void clear();
Andrew Trick18c57a82010-11-30 23:18:47 +000078
79 LiveIntervalUnion& operator[](unsigned PhysReg) {
80 assert(PhysReg < NumRegs && "physReg out of bounds");
81 return Array[PhysReg];
Andrew Trick14e8d712010-10-22 23:09:15 +000082 }
83 };
Andrew Trick18c57a82010-11-30 23:18:47 +000084
85 const TargetRegisterInfo *TRI;
Jakob Stoklund Olesen4680dec2010-12-10 23:49:00 +000086 MachineRegisterInfo *MRI;
Andrew Trick18c57a82010-11-30 23:18:47 +000087 VirtRegMap *VRM;
88 LiveIntervals *LIS;
89 LiveUnionArray PhysReg2LiveUnion;
Andrew Trick14e8d712010-10-22 23:09:15 +000090
Andrew Tricke141a492010-11-08 18:02:08 +000091 // Current queries, one per physreg. They must be reinitialized each time we
92 // query on a new live virtual register.
Andrew Trick18c57a82010-11-30 23:18:47 +000093 OwningArrayPtr<LiveIntervalUnion::Query> Queries;
Andrew Tricke141a492010-11-08 18:02:08 +000094
Jakob Stoklund Olesen4680dec2010-12-10 23:49:00 +000095 RegAllocBase(): TRI(0), MRI(0), VRM(0), LIS(0) {}
Andrew Trick14e8d712010-10-22 23:09:15 +000096
Andrew Trickf4331062010-10-22 23:33:19 +000097 virtual ~RegAllocBase() {}
98
Andrew Trick14e8d712010-10-22 23:09:15 +000099 // A RegAlloc pass should call this before allocatePhysRegs.
Jakob Stoklund Olesen4680dec2010-12-10 23:49:00 +0000100 void init(VirtRegMap &vrm, LiveIntervals &lis);
Andrew Trick14e8d712010-10-22 23:09:15 +0000101
Andrew Trick8a83d542010-11-11 17:46:29 +0000102 // Get an initialized query to check interferences between lvr and preg. Note
103 // that Query::init must be called at least once for each physical register
Andrew Trick18c57a82010-11-30 23:18:47 +0000104 // before querying a new live virtual register. This ties Queries and
105 // PhysReg2LiveUnion together.
106 LiveIntervalUnion::Query &query(LiveInterval &VirtReg, unsigned PhysReg) {
107 Queries[PhysReg].init(&VirtReg, &PhysReg2LiveUnion[PhysReg]);
108 return Queries[PhysReg];
Andrew Trick8a83d542010-11-11 17:46:29 +0000109 }
Andrew Trick18c57a82010-11-30 23:18:47 +0000110
Andrew Tricke16eecc2010-10-26 18:34:01 +0000111 // The top-level driver. The output is a VirtRegMap that us updated with
112 // physical register assignments.
113 //
114 // If an implementation wants to override the LiveInterval comparator, we
115 // should modify this interface to allow passing in an instance derived from
116 // LiveVirtRegQueue.
117 void allocatePhysRegs();
Andrew Trick14e8d712010-10-22 23:09:15 +0000118
Andrew Trickf4baeaf2010-11-10 19:18:47 +0000119 // Get a temporary reference to a Spiller instance.
120 virtual Spiller &spiller() = 0;
Andrew Trick18c57a82010-11-30 23:18:47 +0000121
Jakob Stoklund Olesen98d96482011-02-22 23:01:52 +0000122 /// enqueue - Add VirtReg to the priority queue of unassigned registers.
123 virtual void enqueue(LiveInterval *LI) = 0;
124
125 /// dequeue - Return the next unassigned register, or NULL.
126 virtual LiveInterval *dequeue() = 0;
Jakob Stoklund Olesend0bec3e2010-12-08 22:22:41 +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
Jakob Stoklund Olesen27106382011-02-09 01:14:03 +0000143 /// assign - Assign VirtReg to PhysReg.
144 /// This should not be called from selectOrSplit for the current register.
145 void assign(LiveInterval &VirtReg, unsigned PhysReg);
146
147 /// unassign - Undo a previous assignment of VirtReg to PhysReg.
148 /// This can be invoked from selectOrSplit, but be careful to guarantee that
149 /// allocation is making progress.
150 void unassign(LiveInterval &VirtReg, unsigned PhysReg);
151
Andrew Trickf4baeaf2010-11-10 19:18:47 +0000152 // Helper for spilling all live virtual registers currently unified under preg
153 // that interfere with the most recently queried lvr. Return true if spilling
154 // was successful, and append any new spilled/split intervals to splitLVRs.
Andrew Trick18c57a82010-11-30 23:18:47 +0000155 bool spillInterferences(LiveInterval &VirtReg, unsigned PhysReg,
156 SmallVectorImpl<LiveInterval*> &SplitVRegs);
Andrew Trickf4baeaf2010-11-10 19:18:47 +0000157
Jakob Stoklund Olesen1b19dc12010-12-08 01:06:06 +0000158 /// addMBBLiveIns - Add physreg liveins to basic blocks.
159 void addMBBLiveIns(MachineFunction *);
160
Andrew Trick071d1c02010-11-09 21:04:34 +0000161#ifndef NDEBUG
162 // Verify each LiveIntervalUnion.
163 void verify();
164#endif
Andrew Trickf4baeaf2010-11-10 19:18:47 +0000165
Jakob Stoklund Olesen533f58e2010-12-11 00:19:56 +0000166 // Use this group name for NamedRegionTimer.
167 static const char *TimerGroupName;
168
Jakob Stoklund Olesenaf249642010-12-17 23:16:35 +0000169public:
170 /// VerifyEnabled - True when -verify-regalloc is given.
171 static bool VerifyEnabled;
172
Andrew Trick18c57a82010-11-30 23:18:47 +0000173private:
Jakob Stoklund Olesen98d96482011-02-22 23:01:52 +0000174 void seedLiveRegs();
Andrew Trick18c57a82010-11-30 23:18:47 +0000175
176 void spillReg(LiveInterval &VirtReg, unsigned PhysReg,
177 SmallVectorImpl<LiveInterval*> &SplitVRegs);
Andrew Trick14e8d712010-10-22 23:09:15 +0000178};
179
Andrew Trick14e8d712010-10-22 23:09:15 +0000180} // end namespace llvm
181
182#endif // !defined(LLVM_CODEGEN_REGALLOCBASE)