blob: cb07653368a73bf7c0c907037be5df90e2aefc8b [file] [log] [blame]
Andrew Trick14e8d712010-10-22 23:09:15 +00001//===-- LiveIntervalUnion.h - Live interval union data struct --*- 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// LiveIntervalUnion is a union of live segments across multiple live virtual
11// registers. This may be used during coalescing to represent a congruence
12// class, or during register allocation to model liveness of a physical
13// register.
14//
15//===----------------------------------------------------------------------===//
16
17#ifndef LLVM_CODEGEN_LIVEINTERVALUNION
18#define LLVM_CODEGEN_LIVEINTERVALUNION
19
20#include "llvm/CodeGen/LiveInterval.h"
21#include <vector>
22#include <set>
23
24namespace llvm {
25
Andrew Tricke16eecc2010-10-26 18:34:01 +000026/// A LiveSegment is a copy of a LiveRange object used within
27/// LiveIntervalUnion. LiveSegment additionally contains a pointer to its
28/// original live virtual register (LiveInterval). This allows quick lookup of
29/// the live virtual register as we iterate over live segments in a union. Note
30/// that LiveRange is misnamed and actually represents only a single contiguous
31/// interval within a virtual register's liveness. To limit confusion, in this
32/// file we refer it as a live segment.
33///
34/// Note: This currently represents a half-open interval [start,end).
35/// If LiveRange is modified to represent a closed interval, so should this.
Andrew Trick14e8d712010-10-22 23:09:15 +000036struct LiveSegment {
37 SlotIndex start;
38 SlotIndex end;
39 LiveInterval *liveVirtReg;
40
Andrew Tricke141a492010-11-08 18:02:08 +000041 LiveSegment(SlotIndex s, SlotIndex e, LiveInterval *lvr)
42 : start(s), end(e), liveVirtReg(lvr) {}
Andrew Trick14e8d712010-10-22 23:09:15 +000043
44 bool operator==(const LiveSegment &ls) const {
45 return start == ls.start && end == ls.end && liveVirtReg == ls.liveVirtReg;
46 }
47
48 bool operator!=(const LiveSegment &ls) const {
49 return !operator==(ls);
50 }
51
Andrew Tricke16eecc2010-10-26 18:34:01 +000052 // Order segments by starting point only--we expect them to be disjoint.
53 bool operator<(const LiveSegment &ls) const { return start < ls.start; }
Andrew Trick14e8d712010-10-22 23:09:15 +000054};
55
Andrew Trick14e8d712010-10-22 23:09:15 +000056inline bool operator<(SlotIndex V, const LiveSegment &ls) {
57 return V < ls.start;
58}
59
60inline bool operator<(const LiveSegment &ls, SlotIndex V) {
61 return ls.start < V;
62}
63
Andrew Tricke16eecc2010-10-26 18:34:01 +000064/// Compare a live virtual register segment to a LiveIntervalUnion segment.
65inline bool overlap(const LiveRange &lvrSeg, const LiveSegment &liuSeg) {
66 return lvrSeg.start < liuSeg.end && liuSeg.start < lvrSeg.end;
67}
68
Matt Beaumont-Gaye33daaa2010-11-09 19:56:25 +000069template <> struct isPodLike<LiveSegment> { static const bool value = true; };
70
71raw_ostream& operator<<(raw_ostream& os, const LiveSegment &ls);
72
73/// Abstraction to provide info for the representative register.
74class AbstractRegisterDescription {
75public:
76 virtual const char *getName(unsigned reg) const = 0;
77 virtual ~AbstractRegisterDescription() { }
78};
79
Andrew Trick14e8d712010-10-22 23:09:15 +000080/// Union of live intervals that are strong candidates for coalescing into a
81/// single register (either physical or virtual depending on the context). We
82/// expect the constituent live intervals to be disjoint, although we may
83/// eventually make exceptions to handle value-based interference.
84class LiveIntervalUnion {
85 // A set of live virtual register segments that supports fast insertion,
86 // intersection, and removal.
87 //
88 // FIXME: std::set is a placeholder until we decide how to
89 // efficiently represent it. Probably need to roll our own B-tree.
90 typedef std::set<LiveSegment> LiveSegments;
91
92 // A set of live virtual registers. Elements have type LiveInterval, where
93 // each element represents the liveness of a single live virtual register.
94 // This is traditionally known as a live range, but we refer is as a live
95 // virtual register to avoid confusing it with the misnamed LiveRange
96 // class.
97 typedef std::vector<LiveInterval*> LiveVirtRegs;
98
99public:
100 // SegmentIter can advance to the next segment ordered by starting position
101 // which may belong to a different live virtual register. We also must be able
102 // to reach the current segment's containing virtual register.
103 typedef LiveSegments::iterator SegmentIter;
104
105 class InterferenceResult;
106 class Query;
107
108private:
109 unsigned repReg_; // representative register number
110 LiveSegments segments_; // union of virtual reg segements
Andrew Trick14e8d712010-10-22 23:09:15 +0000111
112public:
113 // default ctor avoids placement new
114 LiveIntervalUnion() : repReg_(0) {}
Andrew Tricke16eecc2010-10-26 18:34:01 +0000115
116 // Initialize the union by associating it with a representative register
117 // number.
Andrew Trick14e8d712010-10-22 23:09:15 +0000118 void init(unsigned repReg) { repReg_ = repReg; }
119
Andrew Tricke16eecc2010-10-26 18:34:01 +0000120 // Iterate over all segments in the union of live virtual registers ordered
121 // by their starting position.
Andrew Trick14e8d712010-10-22 23:09:15 +0000122 SegmentIter begin() { return segments_.begin(); }
123 SegmentIter end() { return segments_.end(); }
124
Andrew Tricke141a492010-11-08 18:02:08 +0000125 // Return an iterator to the first segment after or including begin that
126 // intersects with lvrSeg.
127 SegmentIter upperBound(SegmentIter begin, const LiveSegment &seg);
128
Andrew Tricke16eecc2010-10-26 18:34:01 +0000129 // Add a live virtual register to this union and merge its segments.
130 // Holds a nonconst reference to the LVR for later maniplution.
Andrew Trick14e8d712010-10-22 23:09:15 +0000131 void unify(LiveInterval &lvr);
132
Andrew Tricke141a492010-11-08 18:02:08 +0000133 // Remove a live virtual register's segments from this union.
134 void extract(const LiveInterval &lvr);
Andrew Trick14e8d712010-10-22 23:09:15 +0000135
136 /// Cache a single interference test result in the form of two intersecting
137 /// segments. This allows efficiently iterating over the interferences. The
138 /// iteration logic is handled by LiveIntervalUnion::Query which may
139 /// filter interferences depending on the type of query.
140 class InterferenceResult {
141 friend class Query;
142
143 LiveInterval::iterator lvrSegI_; // current position in _lvr
144 SegmentIter liuSegI_; // current position in _liu
145
146 // Internal ctor.
147 InterferenceResult(LiveInterval::iterator lvrSegI, SegmentIter liuSegI)
148 : lvrSegI_(lvrSegI), liuSegI_(liuSegI) {}
149
150 public:
151 // Public default ctor.
152 InterferenceResult(): lvrSegI_(), liuSegI_() {}
153
154 // Note: this interface provides raw access to the iterators because the
155 // result has no way to tell if it's valid to dereference them.
156
157 // Access the lvr segment.
158 const LiveInterval::iterator &lvrSegPos() const { return lvrSegI_; }
159
160 // Access the liu segment.
Andrew Tricke141a492010-11-08 18:02:08 +0000161 const SegmentIter &liuSegPos() const { return liuSegI_; }
Andrew Trick14e8d712010-10-22 23:09:15 +0000162
163 bool operator==(const InterferenceResult &ir) const {
164 return lvrSegI_ == ir.lvrSegI_ && liuSegI_ == ir.liuSegI_;
165 }
166 bool operator!=(const InterferenceResult &ir) const {
167 return !operator==(ir);
168 }
169 };
170
171 /// Query interferences between a single live virtual register and a live
172 /// interval union.
173 class Query {
Andrew Tricke141a492010-11-08 18:02:08 +0000174 LiveIntervalUnion *liu_;
175 LiveInterval *lvr_;
Andrew Trick14e8d712010-10-22 23:09:15 +0000176 InterferenceResult firstInterference_;
177 // TBD: interfering vregs
178
179 public:
Andrew Tricke141a492010-11-08 18:02:08 +0000180 Query(): liu_(), lvr_() {}
Andrew Trick14e8d712010-10-22 23:09:15 +0000181
Andrew Tricke141a492010-11-08 18:02:08 +0000182 Query(LiveInterval *lvr, LiveIntervalUnion *liu): liu_(liu), lvr_(lvr) {}
183
184 void clear() {
185 liu_ = NULL;
186 lvr_ = NULL;
187 firstInterference_ = InterferenceResult();
188 }
189
190 void init(LiveInterval *lvr, LiveIntervalUnion *liu) {
191 if (lvr_ == lvr) {
192 // We currently allow query objects to be reused acrossed live virtual
193 // registers, but always for the same live interval union.
194 assert(liu_ == liu && "inconsistent initialization");
195 // Retain cached results, e.g. firstInterference.
196 return;
197 }
198 liu_ = liu;
199 lvr_ = lvr;
200 // Clear cached results.
201 firstInterference_ = InterferenceResult();
202 }
203
204 LiveInterval &lvr() const { assert(lvr_ && "uninitialized"); return *lvr_; }
Andrew Trick14e8d712010-10-22 23:09:15 +0000205
206 bool isInterference(const InterferenceResult &ir) const {
Andrew Tricke141a492010-11-08 18:02:08 +0000207 if (ir.lvrSegI_ != lvr_->end()) {
Andrew Trick14e8d712010-10-22 23:09:15 +0000208 assert(overlap(*ir.lvrSegI_, *ir.liuSegI_) &&
209 "invalid segment iterators");
210 return true;
211 }
212 return false;
213 }
214
215 // Does this live virtual register interfere with the union.
216 bool checkInterference() { return isInterference(firstInterference()); }
217
Andrew Tricke141a492010-11-08 18:02:08 +0000218 // Get the first pair of interfering segments, or a noninterfering result.
219 // This initializes the firstInterference_ cache.
Andrew Trick14e8d712010-10-22 23:09:15 +0000220 InterferenceResult firstInterference();
221
222 // Treat the result as an iterator and advance to the next interfering pair
223 // of segments. Visiting each unique interfering pairs means that the same
224 // lvr or liu segment may be visited multiple times.
225 bool nextInterference(InterferenceResult &ir) const;
226
227 // TBD: bool collectInterferingVirtRegs(unsigned maxInterference)
228
229 private:
230 // Private interface for queries
231 void findIntersection(InterferenceResult &ir) const;
232 };
233};
234
235} // end namespace llvm
236
237#endif // !defined(LLVM_CODEGEN_LIVEINTERVALUNION)