blob: 153ee2d6691544038aabce67354868f7ab40589b [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;
Andrew Trick18c57a82010-11-30 23:18:47 +000029typedef SparseBitVector<128> LiveVirtRegBitSet;
Andrew Trick071d1c02010-11-09 21:04:34 +000030#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///
Andrew Trick18c57a82010-11-30 23:18:47 +000040/// Note: This currently represents a half-open interval [Start,End).
Andrew Tricke16eecc2010-10-26 18:34:01 +000041/// If LiveRange is modified to represent a closed interval, so should this.
Andrew Trick14e8d712010-10-22 23:09:15 +000042struct LiveSegment {
Andrew Trick18c57a82010-11-30 23:18:47 +000043 SlotIndex Start;
44 SlotIndex End;
45 LiveInterval *VirtReg;
Andrew Trick14e8d712010-10-22 23:09:15 +000046
Andrew Trick18c57a82010-11-30 23:18:47 +000047 LiveSegment(const LiveRange& LR, LiveInterval *VReg)
48 : Start(LR.start), End(LR.end), VirtReg(VReg) {}
Andrew Trick14e8d712010-10-22 23:09:15 +000049
Andrew Trick18c57a82010-11-30 23:18:47 +000050 bool operator==(const LiveSegment &LS) const {
51 return Start == LS.Start && End == LS.End && VirtReg == LS.VirtReg;
Andrew Trick14e8d712010-10-22 23:09:15 +000052 }
53
Andrew Trick18c57a82010-11-30 23:18:47 +000054 bool operator!=(const LiveSegment &LS) const {
55 return !operator==(LS);
Andrew Trick14e8d712010-10-22 23:09:15 +000056 }
57
Andrew Tricke16eecc2010-10-26 18:34:01 +000058 // Order segments by starting point only--we expect them to be disjoint.
Andrew Trick18c57a82010-11-30 23:18:47 +000059 bool operator<(const LiveSegment &LS) const { return Start < LS.Start; }
Andrew Trick071d1c02010-11-09 21:04:34 +000060
61 void dump() const;
Andrew Trick18c57a82010-11-30 23:18:47 +000062 void print(raw_ostream &OS) const;
Andrew Trick14e8d712010-10-22 23:09:15 +000063};
64
Andrew Trick18c57a82010-11-30 23:18:47 +000065inline bool operator<(SlotIndex Idx, const LiveSegment &LS) {
66 return Idx < LS.Start;
Andrew Trick14e8d712010-10-22 23:09:15 +000067}
68
Andrew Trick18c57a82010-11-30 23:18:47 +000069inline bool operator<(const LiveSegment &LS, SlotIndex Idx) {
70 return LS.Start < Idx;
Andrew Trick14e8d712010-10-22 23:09:15 +000071}
72
Andrew Tricke16eecc2010-10-26 18:34:01 +000073/// Compare a live virtual register segment to a LiveIntervalUnion segment.
Andrew Trick18c57a82010-11-30 23:18:47 +000074inline bool overlap(const LiveRange &VirtRegSegment,
75 const LiveSegment &LiveUnionSegment) {
76 return VirtRegSegment.start < LiveUnionSegment.End &&
77 LiveUnionSegment.Start < VirtRegSegment.end;
Andrew Tricke16eecc2010-10-26 18:34:01 +000078}
79
Matt Beaumont-Gaye33daaa2010-11-09 19:56:25 +000080template <> struct isPodLike<LiveSegment> { static const bool value = true; };
81
Andrew Trick18c57a82010-11-30 23:18:47 +000082raw_ostream& operator<<(raw_ostream& OS, const LiveSegment &LS);
Matt Beaumont-Gaye33daaa2010-11-09 19:56:25 +000083
84/// Abstraction to provide info for the representative register.
85class AbstractRegisterDescription {
86public:
Andrew Trick18c57a82010-11-30 23:18:47 +000087 virtual const char *getName(unsigned Reg) const = 0;
Andrew Trick071d1c02010-11-09 21:04:34 +000088 virtual ~AbstractRegisterDescription() {}
Matt Beaumont-Gaye33daaa2010-11-09 19:56:25 +000089};
Andrew Trick071d1c02010-11-09 21:04:34 +000090
Andrew Trick14e8d712010-10-22 23:09:15 +000091/// Union of live intervals that are strong candidates for coalescing into a
92/// single register (either physical or virtual depending on the context). We
93/// expect the constituent live intervals to be disjoint, although we may
94/// eventually make exceptions to handle value-based interference.
95class LiveIntervalUnion {
96 // A set of live virtual register segments that supports fast insertion,
Andrew Trick18c57a82010-11-30 23:18:47 +000097 // intersection, and removal.
Andrew Trick14e8d712010-10-22 23:09:15 +000098 //
99 // FIXME: std::set is a placeholder until we decide how to
100 // efficiently represent it. Probably need to roll our own B-tree.
101 typedef std::set<LiveSegment> LiveSegments;
102
103 // A set of live virtual registers. Elements have type LiveInterval, where
104 // each element represents the liveness of a single live virtual register.
105 // This is traditionally known as a live range, but we refer is as a live
106 // virtual register to avoid confusing it with the misnamed LiveRange
107 // class.
Andrew Trick18c57a82010-11-30 23:18:47 +0000108 typedef std::vector<LiveInterval*> LiveVRegs;
Andrew Trick14e8d712010-10-22 23:09:15 +0000109
110public:
111 // SegmentIter can advance to the next segment ordered by starting position
112 // which may belong to a different live virtual register. We also must be able
113 // to reach the current segment's containing virtual register.
114 typedef LiveSegments::iterator SegmentIter;
115
116 class InterferenceResult;
117 class Query;
118
119private:
Andrew Trick18c57a82010-11-30 23:18:47 +0000120 unsigned RepReg; // representative register number
121 LiveSegments Segments; // union of virtual reg segements
Andrew Trick14e8d712010-10-22 23:09:15 +0000122
123public:
124 // default ctor avoids placement new
Andrew Trick18c57a82010-11-30 23:18:47 +0000125 LiveIntervalUnion() : RepReg(0) {}
Andrew Tricke16eecc2010-10-26 18:34:01 +0000126
127 // Initialize the union by associating it with a representative register
128 // number.
Andrew Trick18c57a82010-11-30 23:18:47 +0000129 void init(unsigned Reg) { RepReg = Reg; }
Andrew Trick14e8d712010-10-22 23:09:15 +0000130
Andrew Tricke16eecc2010-10-26 18:34:01 +0000131 // Iterate over all segments in the union of live virtual registers ordered
132 // by their starting position.
Andrew Trick18c57a82010-11-30 23:18:47 +0000133 SegmentIter begin() { return Segments.begin(); }
134 SegmentIter end() { return Segments.end(); }
Andrew Trick14e8d712010-10-22 23:09:15 +0000135
Andrew Tricke141a492010-11-08 18:02:08 +0000136 // Return an iterator to the first segment after or including begin that
Andrew Trick18c57a82010-11-30 23:18:47 +0000137 // intersects with LS.
138 SegmentIter upperBound(SegmentIter SegBegin, const LiveSegment &LS);
Andrew Tricke141a492010-11-08 18:02:08 +0000139
Andrew Tricke16eecc2010-10-26 18:34:01 +0000140 // Add a live virtual register to this union and merge its segments.
Andrew Trick18c57a82010-11-30 23:18:47 +0000141 // Holds a nonconst reference to the VirtReg for later maniplution.
142 void unify(LiveInterval &VirtReg);
Andrew Trick14e8d712010-10-22 23:09:15 +0000143
Andrew Tricke141a492010-11-08 18:02:08 +0000144 // Remove a live virtual register's segments from this union.
Andrew Trick18c57a82010-11-30 23:18:47 +0000145 void extract(const LiveInterval &VirtReg);
Andrew Trick14e8d712010-10-22 23:09:15 +0000146
Andrew Trick18c57a82010-11-30 23:18:47 +0000147 void dump(const AbstractRegisterDescription *RegDesc) const;
Andrew Trick071d1c02010-11-09 21:04:34 +0000148
Andrew Trick18c57a82010-11-30 23:18:47 +0000149 // If tri != NULL, use it to decode RepReg
150 void print(raw_ostream &OS, const AbstractRegisterDescription *RegDesc) const;
151
Andrew Trick071d1c02010-11-09 21:04:34 +0000152#ifndef NDEBUG
153 // Verify the live intervals in this union and add them to the visited set.
Andrew Trick18c57a82010-11-30 23:18:47 +0000154 void verify(LiveVirtRegBitSet& VisitedVRegs);
Andrew Trick071d1c02010-11-09 21:04:34 +0000155#endif
156
Andrew Trick14e8d712010-10-22 23:09:15 +0000157 /// Cache a single interference test result in the form of two intersecting
158 /// segments. This allows efficiently iterating over the interferences. The
159 /// iteration logic is handled by LiveIntervalUnion::Query which may
160 /// filter interferences depending on the type of query.
161 class InterferenceResult {
162 friend class Query;
163
Andrew Trick18c57a82010-11-30 23:18:47 +0000164 LiveInterval::iterator VirtRegI; // current position in VirtReg
165 SegmentIter LiveUnionI; // current position in LiveUnion
166
Andrew Trick14e8d712010-10-22 23:09:15 +0000167 // Internal ctor.
Andrew Trick18c57a82010-11-30 23:18:47 +0000168 InterferenceResult(LiveInterval::iterator VRegI, SegmentIter UnionI)
169 : VirtRegI(VRegI), LiveUnionI(UnionI) {}
Andrew Trick14e8d712010-10-22 23:09:15 +0000170
171 public:
172 // Public default ctor.
Andrew Trick18c57a82010-11-30 23:18:47 +0000173 InterferenceResult(): VirtRegI(), LiveUnionI() {}
Andrew Trick14e8d712010-10-22 23:09:15 +0000174
175 // Note: this interface provides raw access to the iterators because the
176 // result has no way to tell if it's valid to dereference them.
177
Andrew Trick18c57a82010-11-30 23:18:47 +0000178 // Access the VirtReg segment.
179 LiveInterval::iterator virtRegPos() const { return VirtRegI; }
Andrew Trick14e8d712010-10-22 23:09:15 +0000180
Andrew Trick18c57a82010-11-30 23:18:47 +0000181 // Access the LiveUnion segment.
182 SegmentIter liveUnionPos() const { return LiveUnionI; }
Andrew Trick14e8d712010-10-22 23:09:15 +0000183
Andrew Trick18c57a82010-11-30 23:18:47 +0000184 bool operator==(const InterferenceResult &IR) const {
185 return VirtRegI == IR.VirtRegI && LiveUnionI == IR.LiveUnionI;
Andrew Trick14e8d712010-10-22 23:09:15 +0000186 }
Andrew Trick18c57a82010-11-30 23:18:47 +0000187 bool operator!=(const InterferenceResult &IR) const {
188 return !operator==(IR);
Andrew Trick14e8d712010-10-22 23:09:15 +0000189 }
190 };
191
192 /// Query interferences between a single live virtual register and a live
193 /// interval union.
194 class Query {
Andrew Trick18c57a82010-11-30 23:18:47 +0000195 LiveIntervalUnion *LiveUnion;
196 LiveInterval *VirtReg;
197 InterferenceResult FirstInterference;
198 SmallVector<LiveInterval*,4> InterferingVRegs;
199 bool SeenAllInterferences;
200 bool SeenUnspillableVReg;
Andrew Trick14e8d712010-10-22 23:09:15 +0000201
202 public:
Andrew Trick18c57a82010-11-30 23:18:47 +0000203 Query(): LiveUnion(), VirtReg() {}
Andrew Trick14e8d712010-10-22 23:09:15 +0000204
Andrew Trick18c57a82010-11-30 23:18:47 +0000205 Query(LiveInterval *VReg, LiveIntervalUnion *LIU):
206 LiveUnion(LIU), VirtReg(VReg), SeenAllInterferences(false),
207 SeenUnspillableVReg(false)
208 {}
Andrew Tricke141a492010-11-08 18:02:08 +0000209
210 void clear() {
Andrew Trick18c57a82010-11-30 23:18:47 +0000211 LiveUnion = NULL;
212 VirtReg = NULL;
213 FirstInterference = InterferenceResult();
214 InterferingVRegs.clear();
215 SeenAllInterferences = false;
216 SeenUnspillableVReg = false;
Andrew Tricke141a492010-11-08 18:02:08 +0000217 }
Andrew Trick18c57a82010-11-30 23:18:47 +0000218
219 void init(LiveInterval *VReg, LiveIntervalUnion *LIU) {
220 if (VirtReg == VReg) {
Andrew Tricke141a492010-11-08 18:02:08 +0000221 // We currently allow query objects to be reused acrossed live virtual
222 // registers, but always for the same live interval union.
Andrew Trick18c57a82010-11-30 23:18:47 +0000223 assert(LiveUnion == LIU && "inconsistent initialization");
Andrew Tricke141a492010-11-08 18:02:08 +0000224 // Retain cached results, e.g. firstInterference.
225 return;
226 }
Andrew Trick18c57a82010-11-30 23:18:47 +0000227 clear();
228 LiveUnion = LIU;
229 VirtReg = VReg;
Andrew Tricke141a492010-11-08 18:02:08 +0000230 }
231
Andrew Trick18c57a82010-11-30 23:18:47 +0000232 LiveInterval &virtReg() const {
233 assert(VirtReg && "uninitialized");
234 return *VirtReg;
235 }
Andrew Trick14e8d712010-10-22 23:09:15 +0000236
Andrew Trick18c57a82010-11-30 23:18:47 +0000237 bool isInterference(const InterferenceResult &IR) const {
238 if (IR.VirtRegI != VirtReg->end()) {
239 assert(overlap(*IR.VirtRegI, *IR.LiveUnionI) &&
Andrew Trick14e8d712010-10-22 23:09:15 +0000240 "invalid segment iterators");
241 return true;
242 }
243 return false;
244 }
245
Andrew Trick18c57a82010-11-30 23:18:47 +0000246 // Does this live virtual register interfere with the union?
Andrew Trick14e8d712010-10-22 23:09:15 +0000247 bool checkInterference() { return isInterference(firstInterference()); }
248
Andrew Tricke141a492010-11-08 18:02:08 +0000249 // Get the first pair of interfering segments, or a noninterfering result.
250 // This initializes the firstInterference_ cache.
Andrew Trick14e8d712010-10-22 23:09:15 +0000251 InterferenceResult firstInterference();
252
253 // Treat the result as an iterator and advance to the next interfering pair
254 // of segments. Visiting each unique interfering pairs means that the same
Andrew Trick18c57a82010-11-30 23:18:47 +0000255 // VirtReg or LiveUnion segment may be visited multiple times.
256 bool nextInterference(InterferenceResult &IR) const;
Andrew Trick14e8d712010-10-22 23:09:15 +0000257
Andrew Trickf4baeaf2010-11-10 19:18:47 +0000258 // Count the virtual registers in this union that interfere with this
259 // query's live virtual register, up to maxInterferingRegs.
Andrew Trick18c57a82010-11-30 23:18:47 +0000260 unsigned collectInterferingVRegs(unsigned MaxInterferingRegs = UINT_MAX);
Andrew Trickf4baeaf2010-11-10 19:18:47 +0000261
262 // Was this virtual register visited during collectInterferingVRegs?
Andrew Trick18c57a82010-11-30 23:18:47 +0000263 bool isSeenInterference(LiveInterval *VReg) const;
264
265 // Did collectInterferingVRegs collect all interferences?
266 bool seenAllInterferences() const { return SeenAllInterferences; }
Andrew Trickf4baeaf2010-11-10 19:18:47 +0000267
268 // Did collectInterferingVRegs encounter an unspillable vreg?
Andrew Trick18c57a82010-11-30 23:18:47 +0000269 bool seenUnspillableVReg() const { return SeenUnspillableVReg; }
Andrew Trickf4baeaf2010-11-10 19:18:47 +0000270
271 // Vector generated by collectInterferingVRegs.
272 const SmallVectorImpl<LiveInterval*> &interferingVRegs() const {
Andrew Trick18c57a82010-11-30 23:18:47 +0000273 return InterferingVRegs;
Andrew Trickf4baeaf2010-11-10 19:18:47 +0000274 }
Andrew Trick18c57a82010-11-30 23:18:47 +0000275
Andrew Trick14e8d712010-10-22 23:09:15 +0000276 private:
Andrew Trick8a83d542010-11-11 17:46:29 +0000277 Query(const Query&); // DO NOT IMPLEMENT
278 void operator=(const Query&); // DO NOT IMPLEMENT
Andrew Trick18c57a82010-11-30 23:18:47 +0000279
Andrew Trick14e8d712010-10-22 23:09:15 +0000280 // Private interface for queries
Andrew Trick18c57a82010-11-30 23:18:47 +0000281 void findIntersection(InterferenceResult &IR) const;
Andrew Trick14e8d712010-10-22 23:09:15 +0000282 };
283};
284
285} // end namespace llvm
286
287#endif // !defined(LLVM_CODEGEN_LIVEINTERVALUNION)