blob: fa595fed8b52e6f2ee5a376d35920d6c5c378a6d [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:
14//
15// - 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.
18//
19// - 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;
48
49// Heuristic that determines the priority of assigning virtual to physical
50// registers. The main impact of the heuristic is expected to be compile time.
51// The default is to simply compare spill weights.
52struct LessSpillWeightPriority
53 : public std::binary_function<LiveInterval,LiveInterval, bool> {
54 bool operator()(const LiveInterval *left, const LiveInterval *right) const {
55 return left->weight < right->weight;
56 }
57};
58
59// Forward declare a priority queue of live virtual registers. If an
60// implementation needs to prioritize by anything other than spill weight, then
61// this will become an abstract base class with virtual calls to push/get.
62class LiveVirtRegQueue;
Andrew Trick14e8d712010-10-22 23:09:15 +000063
64/// RegAllocBase provides the register allocation driver and interface that can
65/// be extended to add interesting heuristics.
66///
67/// More sophisticated allocators must override the selectOrSplit() method to
68/// implement live range splitting and must specify a comparator to determine
69/// register assignment priority. LessSpillWeightPriority is provided as a
70/// standard comparator.
71class RegAllocBase {
72protected:
Andrew Trick14e8d712010-10-22 23:09:15 +000073 // Array of LiveIntervalUnions indexed by physical register.
74 class LIUArray {
75 unsigned nRegs_;
76 OwningArrayPtr<LiveIntervalUnion> array_;
77 public:
78 LIUArray(): nRegs_(0) {}
79
80 unsigned numRegs() const { return nRegs_; }
81
82 void init(unsigned nRegs);
83
84 void clear();
85
86 LiveIntervalUnion& operator[](unsigned physReg) {
87 assert(physReg < nRegs_ && "physReg out of bounds");
88 return array_[physReg];
89 }
90 };
91
92 const TargetRegisterInfo *tri_;
93 VirtRegMap *vrm_;
94 LiveIntervals *lis_;
95 LIUArray physReg2liu_;
96
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.
99 OwningArrayPtr<LiveIntervalUnion::Query> queries_;
100
Andrew Trick14e8d712010-10-22 23:09:15 +0000101 RegAllocBase(): tri_(0), vrm_(0), lis_(0) {}
102
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 Tricke16eecc2010-10-26 18:34:01 +0000108 // The top-level driver. The output is a VirtRegMap that us updated with
109 // physical register assignments.
110 //
111 // If an implementation wants to override the LiveInterval comparator, we
112 // should modify this interface to allow passing in an instance derived from
113 // LiveVirtRegQueue.
114 void allocatePhysRegs();
Andrew Trick14e8d712010-10-22 23:09:15 +0000115
116 // A RegAlloc pass should override this to provide the allocation heuristics.
Andrew Tricke16eecc2010-10-26 18:34:01 +0000117 // Each call must guarantee forward progess by returning an available PhysReg
118 // or new set of split live virtual registers. It is up to the splitter to
Andrew Trick14e8d712010-10-22 23:09:15 +0000119 // converge quickly toward fully spilled live ranges.
120 virtual unsigned selectOrSplit(LiveInterval &lvr,
Andrew Tricke16eecc2010-10-26 18:34:01 +0000121 SmallVectorImpl<LiveInterval*> &splitLVRs) = 0;
Andrew Trick14e8d712010-10-22 23:09:15 +0000122
123 // A RegAlloc pass should call this when PassManager releases its memory.
124 virtual void releaseMemory();
125
126 // Helper for checking interference between a live virtual register and a
Andrew Tricke141a492010-11-08 18:02:08 +0000127 // physical register, including all its register aliases. If an interference
128 // exists, return the interfering register, which may be preg or an alias.
129 unsigned checkPhysRegInterference(LiveInterval& lvr, unsigned preg);
130
Andrew Trick071d1c02010-11-09 21:04:34 +0000131#ifndef NDEBUG
132 // Verify each LiveIntervalUnion.
133 void verify();
134#endif
135
Andrew Tricke141a492010-11-08 18:02:08 +0000136 // Helper that spills all live virtual registers currently unified under preg
137 // that interfere with the most recently queried lvr.
138 void spillInterferences(unsigned preg,
139 SmallVectorImpl<LiveInterval*> &splitLVRs);
140
Andrew Trick14e8d712010-10-22 23:09:15 +0000141private:
Andrew Tricke16eecc2010-10-26 18:34:01 +0000142 void seedLiveVirtRegs(LiveVirtRegQueue &lvrQ);
Andrew Trick14e8d712010-10-22 23:09:15 +0000143};
144
Andrew Trick14e8d712010-10-22 23:09:15 +0000145} // end namespace llvm
146
147#endif // !defined(LLVM_CODEGEN_REGALLOCBASE)