blob: b6eaa71cfaa41d16dd7f5e249e3fa86ffd091293 [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 Trick071d1c02010-11-09 21:04:34 +000026#ifndef NDEBUG
27// forward declaration
28template <unsigned Element> class SparseBitVector;
29typedef SparseBitVector<128> LvrBitSet;
30#endif
31
Andrew Tricke16eecc2010-10-26 18:34:01 +000032/// A LiveSegment is a copy of a LiveRange object used within
33/// LiveIntervalUnion. LiveSegment additionally contains a pointer to its
34/// original live virtual register (LiveInterval). This allows quick lookup of
35/// the live virtual register as we iterate over live segments in a union. Note
36/// that LiveRange is misnamed and actually represents only a single contiguous
37/// interval within a virtual register's liveness. To limit confusion, in this
38/// file we refer it as a live segment.
39///
40/// Note: This currently represents a half-open interval [start,end).
41/// If LiveRange is modified to represent a closed interval, so should this.
Andrew Trick14e8d712010-10-22 23:09:15 +000042struct LiveSegment {
43 SlotIndex start;
44 SlotIndex end;
45 LiveInterval *liveVirtReg;
46
Andrew Tricke141a492010-11-08 18:02:08 +000047 LiveSegment(SlotIndex s, SlotIndex e, LiveInterval *lvr)
48 : start(s), end(e), liveVirtReg(lvr) {}
Andrew Trick14e8d712010-10-22 23:09:15 +000049
50 bool operator==(const LiveSegment &ls) const {
51 return start == ls.start && end == ls.end && liveVirtReg == ls.liveVirtReg;
52 }
53
54 bool operator!=(const LiveSegment &ls) const {
55 return !operator==(ls);
56 }
57
Andrew Tricke16eecc2010-10-26 18:34:01 +000058 // Order segments by starting point only--we expect them to be disjoint.
59 bool operator<(const LiveSegment &ls) const { return start < ls.start; }
Andrew Trick071d1c02010-11-09 21:04:34 +000060
61 void dump() const;
62 void print(raw_ostream &os) const;
Andrew Trick14e8d712010-10-22 23:09:15 +000063};
64
Andrew Trick14e8d712010-10-22 23:09:15 +000065inline bool operator<(SlotIndex V, const LiveSegment &ls) {
66 return V < ls.start;
67}
68
69inline bool operator<(const LiveSegment &ls, SlotIndex V) {
70 return ls.start < V;
71}
72
Andrew Tricke16eecc2010-10-26 18:34:01 +000073/// Compare a live virtual register segment to a LiveIntervalUnion segment.
74inline bool overlap(const LiveRange &lvrSeg, const LiveSegment &liuSeg) {
75 return lvrSeg.start < liuSeg.end && liuSeg.start < lvrSeg.end;
76}
77
Matt Beaumont-Gaye33daaa2010-11-09 19:56:25 +000078template <> struct isPodLike<LiveSegment> { static const bool value = true; };
79
80raw_ostream& operator<<(raw_ostream& os, const LiveSegment &ls);
81
82/// Abstraction to provide info for the representative register.
83class AbstractRegisterDescription {
84public:
85 virtual const char *getName(unsigned reg) const = 0;
Andrew Trick071d1c02010-11-09 21:04:34 +000086 virtual ~AbstractRegisterDescription() {}
Matt Beaumont-Gaye33daaa2010-11-09 19:56:25 +000087};
Andrew Trick071d1c02010-11-09 21:04:34 +000088
Andrew Trick14e8d712010-10-22 23:09:15 +000089/// Union of live intervals that are strong candidates for coalescing into a
90/// single register (either physical or virtual depending on the context). We
91/// expect the constituent live intervals to be disjoint, although we may
92/// eventually make exceptions to handle value-based interference.
93class LiveIntervalUnion {
94 // A set of live virtual register segments that supports fast insertion,
95 // intersection, and removal.
96 //
97 // FIXME: std::set is a placeholder until we decide how to
98 // efficiently represent it. Probably need to roll our own B-tree.
99 typedef std::set<LiveSegment> LiveSegments;
100
101 // A set of live virtual registers. Elements have type LiveInterval, where
102 // each element represents the liveness of a single live virtual register.
103 // This is traditionally known as a live range, but we refer is as a live
104 // virtual register to avoid confusing it with the misnamed LiveRange
105 // class.
106 typedef std::vector<LiveInterval*> LiveVirtRegs;
107
108public:
109 // SegmentIter can advance to the next segment ordered by starting position
110 // which may belong to a different live virtual register. We also must be able
111 // to reach the current segment's containing virtual register.
112 typedef LiveSegments::iterator SegmentIter;
113
114 class InterferenceResult;
115 class Query;
116
117private:
118 unsigned repReg_; // representative register number
119 LiveSegments segments_; // union of virtual reg segements
Andrew Trick14e8d712010-10-22 23:09:15 +0000120
121public:
122 // default ctor avoids placement new
123 LiveIntervalUnion() : repReg_(0) {}
Andrew Tricke16eecc2010-10-26 18:34:01 +0000124
125 // Initialize the union by associating it with a representative register
126 // number.
Andrew Trick14e8d712010-10-22 23:09:15 +0000127 void init(unsigned repReg) { repReg_ = repReg; }
128
Andrew Tricke16eecc2010-10-26 18:34:01 +0000129 // Iterate over all segments in the union of live virtual registers ordered
130 // by their starting position.
Andrew Trick14e8d712010-10-22 23:09:15 +0000131 SegmentIter begin() { return segments_.begin(); }
132 SegmentIter end() { return segments_.end(); }
133
Andrew Tricke141a492010-11-08 18:02:08 +0000134 // Return an iterator to the first segment after or including begin that
135 // intersects with lvrSeg.
136 SegmentIter upperBound(SegmentIter begin, const LiveSegment &seg);
137
Andrew Tricke16eecc2010-10-26 18:34:01 +0000138 // Add a live virtual register to this union and merge its segments.
139 // Holds a nonconst reference to the LVR for later maniplution.
Andrew Trick14e8d712010-10-22 23:09:15 +0000140 void unify(LiveInterval &lvr);
141
Andrew Tricke141a492010-11-08 18:02:08 +0000142 // Remove a live virtual register's segments from this union.
143 void extract(const LiveInterval &lvr);
Andrew Trick14e8d712010-10-22 23:09:15 +0000144
Andrew Trick071d1c02010-11-09 21:04:34 +0000145 void dump(const AbstractRegisterDescription *regInfo) const;
146
147 // If tri != NULL, use it to decode repReg_
148 void print(raw_ostream &os, const AbstractRegisterDescription *rdesc) const;
149
150#ifndef NDEBUG
151 // Verify the live intervals in this union and add them to the visited set.
152 void verify(LvrBitSet& visitedVRegs);
153#endif
154
Andrew Trick14e8d712010-10-22 23:09:15 +0000155 /// Cache a single interference test result in the form of two intersecting
156 /// segments. This allows efficiently iterating over the interferences. The
157 /// iteration logic is handled by LiveIntervalUnion::Query which may
158 /// filter interferences depending on the type of query.
159 class InterferenceResult {
160 friend class Query;
161
162 LiveInterval::iterator lvrSegI_; // current position in _lvr
163 SegmentIter liuSegI_; // current position in _liu
164
165 // Internal ctor.
166 InterferenceResult(LiveInterval::iterator lvrSegI, SegmentIter liuSegI)
167 : lvrSegI_(lvrSegI), liuSegI_(liuSegI) {}
168
169 public:
170 // Public default ctor.
171 InterferenceResult(): lvrSegI_(), liuSegI_() {}
172
173 // Note: this interface provides raw access to the iterators because the
174 // result has no way to tell if it's valid to dereference them.
175
176 // Access the lvr segment.
Andrew Trickf4baeaf2010-11-10 19:18:47 +0000177 LiveInterval::iterator lvrSegPos() const { return lvrSegI_; }
Andrew Trick14e8d712010-10-22 23:09:15 +0000178
179 // Access the liu segment.
Andrew Trickf4baeaf2010-11-10 19:18:47 +0000180 SegmentIter liuSegPos() const { return liuSegI_; }
Andrew Trick14e8d712010-10-22 23:09:15 +0000181
182 bool operator==(const InterferenceResult &ir) const {
183 return lvrSegI_ == ir.lvrSegI_ && liuSegI_ == ir.liuSegI_;
184 }
185 bool operator!=(const InterferenceResult &ir) const {
186 return !operator==(ir);
187 }
188 };
189
190 /// Query interferences between a single live virtual register and a live
191 /// interval union.
192 class Query {
Andrew Tricke141a492010-11-08 18:02:08 +0000193 LiveIntervalUnion *liu_;
194 LiveInterval *lvr_;
Andrew Trick14e8d712010-10-22 23:09:15 +0000195 InterferenceResult firstInterference_;
Andrew Trickf4baeaf2010-11-10 19:18:47 +0000196 SmallVector<LiveInterval*,4> interferingVRegs_;
197 bool seenUnspillableVReg_;
Andrew Trick14e8d712010-10-22 23:09:15 +0000198
199 public:
Andrew Tricke141a492010-11-08 18:02:08 +0000200 Query(): liu_(), lvr_() {}
Andrew Trick14e8d712010-10-22 23:09:15 +0000201
Andrew Trickf4baeaf2010-11-10 19:18:47 +0000202 Query(LiveInterval *lvr, LiveIntervalUnion *liu):
203 liu_(liu), lvr_(lvr), seenUnspillableVReg_(false) {}
Andrew Tricke141a492010-11-08 18:02:08 +0000204
205 void clear() {
206 liu_ = NULL;
207 lvr_ = NULL;
208 firstInterference_ = InterferenceResult();
Andrew Trickf4baeaf2010-11-10 19:18:47 +0000209 interferingVRegs_.clear();
210 seenUnspillableVReg_ = false;
Andrew Tricke141a492010-11-08 18:02:08 +0000211 }
212
213 void init(LiveInterval *lvr, LiveIntervalUnion *liu) {
214 if (lvr_ == lvr) {
215 // We currently allow query objects to be reused acrossed live virtual
216 // registers, but always for the same live interval union.
217 assert(liu_ == liu && "inconsistent initialization");
218 // Retain cached results, e.g. firstInterference.
219 return;
220 }
221 liu_ = liu;
222 lvr_ = lvr;
223 // Clear cached results.
224 firstInterference_ = InterferenceResult();
Andrew Trickf4baeaf2010-11-10 19:18:47 +0000225 interferingVRegs_.clear();
226 seenUnspillableVReg_ = false;
Andrew Tricke141a492010-11-08 18:02:08 +0000227 }
228
229 LiveInterval &lvr() const { assert(lvr_ && "uninitialized"); return *lvr_; }
Andrew Trick14e8d712010-10-22 23:09:15 +0000230
231 bool isInterference(const InterferenceResult &ir) const {
Andrew Tricke141a492010-11-08 18:02:08 +0000232 if (ir.lvrSegI_ != lvr_->end()) {
Andrew Trick14e8d712010-10-22 23:09:15 +0000233 assert(overlap(*ir.lvrSegI_, *ir.liuSegI_) &&
234 "invalid segment iterators");
235 return true;
236 }
237 return false;
238 }
239
240 // Does this live virtual register interfere with the union.
241 bool checkInterference() { return isInterference(firstInterference()); }
242
Andrew Tricke141a492010-11-08 18:02:08 +0000243 // Get the first pair of interfering segments, or a noninterfering result.
244 // This initializes the firstInterference_ cache.
Andrew Trick14e8d712010-10-22 23:09:15 +0000245 InterferenceResult firstInterference();
246
247 // Treat the result as an iterator and advance to the next interfering pair
248 // of segments. Visiting each unique interfering pairs means that the same
249 // lvr or liu segment may be visited multiple times.
250 bool nextInterference(InterferenceResult &ir) const;
Andrew Trick14e8d712010-10-22 23:09:15 +0000251
Andrew Trickf4baeaf2010-11-10 19:18:47 +0000252 // Count the virtual registers in this union that interfere with this
253 // query's live virtual register, up to maxInterferingRegs.
254 unsigned collectInterferingVRegs(unsigned maxInterferingRegs = UINT_MAX);
255
256 // Was this virtual register visited during collectInterferingVRegs?
257 bool isSeenInterference(LiveInterval *lvr) const;
258
259 // Did collectInterferingVRegs encounter an unspillable vreg?
260 bool seenUnspillableVReg() const {
261 return seenUnspillableVReg_;
262 }
263
264 // Vector generated by collectInterferingVRegs.
265 const SmallVectorImpl<LiveInterval*> &interferingVRegs() const {
266 return interferingVRegs_;
267 }
268
Andrew Trick14e8d712010-10-22 23:09:15 +0000269 private:
270 // Private interface for queries
271 void findIntersection(InterferenceResult &ir) const;
272 };
273};
274
275} // end namespace llvm
276
277#endif // !defined(LLVM_CODEGEN_LIVEINTERVALUNION)