blob: 445e7b3bf18afb37c79948bb27ea49044901ca25 [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"
Andrew Trick14e8d712010-10-22 23:09:15 +000021#include <set>
22
23namespace llvm {
24
Andrew Trick071d1c02010-11-09 21:04:34 +000025#ifndef NDEBUG
26// forward declaration
27template <unsigned Element> class SparseBitVector;
Andrew Trick18c57a82010-11-30 23:18:47 +000028typedef SparseBitVector<128> LiveVirtRegBitSet;
Andrew Trick071d1c02010-11-09 21:04:34 +000029#endif
30
Andrew Tricke16eecc2010-10-26 18:34:01 +000031/// A LiveSegment is a copy of a LiveRange object used within
32/// LiveIntervalUnion. LiveSegment additionally contains a pointer to its
33/// original live virtual register (LiveInterval). This allows quick lookup of
34/// the live virtual register as we iterate over live segments in a union. Note
35/// that LiveRange is misnamed and actually represents only a single contiguous
36/// interval within a virtual register's liveness. To limit confusion, in this
37/// file we refer it as a live segment.
38///
Andrew Trick18c57a82010-11-30 23:18:47 +000039/// Note: This currently represents a half-open interval [Start,End).
Andrew Tricke16eecc2010-10-26 18:34:01 +000040/// If LiveRange is modified to represent a closed interval, so should this.
Andrew Trick14e8d712010-10-22 23:09:15 +000041struct LiveSegment {
Andrew Trick18c57a82010-11-30 23:18:47 +000042 SlotIndex Start;
43 SlotIndex End;
44 LiveInterval *VirtReg;
Andrew Trick14e8d712010-10-22 23:09:15 +000045
Andrew Trick18c57a82010-11-30 23:18:47 +000046 LiveSegment(const LiveRange& LR, LiveInterval *VReg)
47 : Start(LR.start), End(LR.end), VirtReg(VReg) {}
Andrew Trick14e8d712010-10-22 23:09:15 +000048
Andrew Trick18c57a82010-11-30 23:18:47 +000049 bool operator==(const LiveSegment &LS) const {
50 return Start == LS.Start && End == LS.End && VirtReg == LS.VirtReg;
Andrew Trick14e8d712010-10-22 23:09:15 +000051 }
52
Andrew Trick18c57a82010-11-30 23:18:47 +000053 bool operator!=(const LiveSegment &LS) const {
54 return !operator==(LS);
Andrew Trick14e8d712010-10-22 23:09:15 +000055 }
56
Andrew Tricke16eecc2010-10-26 18:34:01 +000057 // Order segments by starting point only--we expect them to be disjoint.
Andrew Trick18c57a82010-11-30 23:18:47 +000058 bool operator<(const LiveSegment &LS) const { return Start < LS.Start; }
Andrew Trick071d1c02010-11-09 21:04:34 +000059
60 void dump() const;
Andrew Trick18c57a82010-11-30 23:18:47 +000061 void print(raw_ostream &OS) const;
Andrew Trick14e8d712010-10-22 23:09:15 +000062};
63
Andrew Trick18c57a82010-11-30 23:18:47 +000064inline bool operator<(SlotIndex Idx, const LiveSegment &LS) {
65 return Idx < LS.Start;
Andrew Trick14e8d712010-10-22 23:09:15 +000066}
67
Andrew Trick18c57a82010-11-30 23:18:47 +000068inline bool operator<(const LiveSegment &LS, SlotIndex Idx) {
69 return LS.Start < Idx;
Andrew Trick14e8d712010-10-22 23:09:15 +000070}
71
Andrew Tricke16eecc2010-10-26 18:34:01 +000072/// Compare a live virtual register segment to a LiveIntervalUnion segment.
Andrew Trick18c57a82010-11-30 23:18:47 +000073inline bool overlap(const LiveRange &VirtRegSegment,
74 const LiveSegment &LiveUnionSegment) {
75 return VirtRegSegment.start < LiveUnionSegment.End &&
76 LiveUnionSegment.Start < VirtRegSegment.end;
Andrew Tricke16eecc2010-10-26 18:34:01 +000077}
78
Matt Beaumont-Gaye33daaa2010-11-09 19:56:25 +000079template <> struct isPodLike<LiveSegment> { static const bool value = true; };
80
Andrew Trick18c57a82010-11-30 23:18:47 +000081raw_ostream& operator<<(raw_ostream& OS, const LiveSegment &LS);
Matt Beaumont-Gaye33daaa2010-11-09 19:56:25 +000082
83/// Abstraction to provide info for the representative register.
84class AbstractRegisterDescription {
85public:
Andrew Trick18c57a82010-11-30 23:18:47 +000086 virtual const char *getName(unsigned Reg) const = 0;
Andrew Trick071d1c02010-11-09 21:04:34 +000087 virtual ~AbstractRegisterDescription() {}
Matt Beaumont-Gaye33daaa2010-11-09 19:56:25 +000088};
Andrew Trick071d1c02010-11-09 21:04:34 +000089
Andrew Trick14e8d712010-10-22 23:09:15 +000090/// Union of live intervals that are strong candidates for coalescing into a
91/// single register (either physical or virtual depending on the context). We
92/// expect the constituent live intervals to be disjoint, although we may
93/// eventually make exceptions to handle value-based interference.
94class LiveIntervalUnion {
95 // A set of live virtual register segments that supports fast insertion,
Andrew Trick18c57a82010-11-30 23:18:47 +000096 // intersection, and removal.
Andrew Trick14e8d712010-10-22 23:09:15 +000097 //
98 // FIXME: std::set is a placeholder until we decide how to
99 // efficiently represent it. Probably need to roll our own B-tree.
100 typedef std::set<LiveSegment> LiveSegments;
101
Andrew Trick14e8d712010-10-22 23:09:15 +0000102public:
103 // SegmentIter can advance to the next segment ordered by starting position
104 // which may belong to a different live virtual register. We also must be able
105 // to reach the current segment's containing virtual register.
106 typedef LiveSegments::iterator SegmentIter;
107
108 class InterferenceResult;
109 class Query;
110
111private:
Andrew Trick18c57a82010-11-30 23:18:47 +0000112 unsigned RepReg; // representative register number
113 LiveSegments Segments; // union of virtual reg segements
Andrew Trick14e8d712010-10-22 23:09:15 +0000114
115public:
116 // default ctor avoids placement new
Andrew Trick18c57a82010-11-30 23:18:47 +0000117 LiveIntervalUnion() : RepReg(0) {}
Andrew Tricke16eecc2010-10-26 18:34:01 +0000118
119 // Initialize the union by associating it with a representative register
120 // number.
Andrew Trick18c57a82010-11-30 23:18:47 +0000121 void init(unsigned Reg) { RepReg = Reg; }
Andrew Trick14e8d712010-10-22 23:09:15 +0000122
Andrew Tricke16eecc2010-10-26 18:34:01 +0000123 // Iterate over all segments in the union of live virtual registers ordered
124 // by their starting position.
Andrew Trick18c57a82010-11-30 23:18:47 +0000125 SegmentIter begin() { return Segments.begin(); }
126 SegmentIter end() { return Segments.end(); }
Andrew Trick14e8d712010-10-22 23:09:15 +0000127
Andrew Tricke141a492010-11-08 18:02:08 +0000128 // Return an iterator to the first segment after or including begin that
Andrew Trick18c57a82010-11-30 23:18:47 +0000129 // intersects with LS.
130 SegmentIter upperBound(SegmentIter SegBegin, const LiveSegment &LS);
Andrew Tricke141a492010-11-08 18:02:08 +0000131
Andrew Tricke16eecc2010-10-26 18:34:01 +0000132 // Add a live virtual register to this union and merge its segments.
Andrew Trick18c57a82010-11-30 23:18:47 +0000133 // Holds a nonconst reference to the VirtReg for later maniplution.
134 void unify(LiveInterval &VirtReg);
Andrew Trick14e8d712010-10-22 23:09:15 +0000135
Andrew Tricke141a492010-11-08 18:02:08 +0000136 // Remove a live virtual register's segments from this union.
Andrew Trick18c57a82010-11-30 23:18:47 +0000137 void extract(const LiveInterval &VirtReg);
Andrew Trick14e8d712010-10-22 23:09:15 +0000138
Andrew Trick18c57a82010-11-30 23:18:47 +0000139 void dump(const AbstractRegisterDescription *RegDesc) const;
Andrew Trick071d1c02010-11-09 21:04:34 +0000140
Andrew Trick18c57a82010-11-30 23:18:47 +0000141 // If tri != NULL, use it to decode RepReg
142 void print(raw_ostream &OS, const AbstractRegisterDescription *RegDesc) const;
143
Andrew Trick071d1c02010-11-09 21:04:34 +0000144#ifndef NDEBUG
145 // Verify the live intervals in this union and add them to the visited set.
Andrew Trick18c57a82010-11-30 23:18:47 +0000146 void verify(LiveVirtRegBitSet& VisitedVRegs);
Andrew Trick071d1c02010-11-09 21:04:34 +0000147#endif
148
Andrew Trick14e8d712010-10-22 23:09:15 +0000149 /// Cache a single interference test result in the form of two intersecting
150 /// segments. This allows efficiently iterating over the interferences. The
151 /// iteration logic is handled by LiveIntervalUnion::Query which may
152 /// filter interferences depending on the type of query.
153 class InterferenceResult {
154 friend class Query;
155
Andrew Trick18c57a82010-11-30 23:18:47 +0000156 LiveInterval::iterator VirtRegI; // current position in VirtReg
157 SegmentIter LiveUnionI; // current position in LiveUnion
158
Andrew Trick14e8d712010-10-22 23:09:15 +0000159 // Internal ctor.
Andrew Trick18c57a82010-11-30 23:18:47 +0000160 InterferenceResult(LiveInterval::iterator VRegI, SegmentIter UnionI)
161 : VirtRegI(VRegI), LiveUnionI(UnionI) {}
Andrew Trick14e8d712010-10-22 23:09:15 +0000162
163 public:
164 // Public default ctor.
Andrew Trick18c57a82010-11-30 23:18:47 +0000165 InterferenceResult(): VirtRegI(), LiveUnionI() {}
Andrew Trick14e8d712010-10-22 23:09:15 +0000166
167 // Note: this interface provides raw access to the iterators because the
168 // result has no way to tell if it's valid to dereference them.
169
Andrew Trick18c57a82010-11-30 23:18:47 +0000170 // Access the VirtReg segment.
171 LiveInterval::iterator virtRegPos() const { return VirtRegI; }
Andrew Trick14e8d712010-10-22 23:09:15 +0000172
Andrew Trick18c57a82010-11-30 23:18:47 +0000173 // Access the LiveUnion segment.
174 SegmentIter liveUnionPos() const { return LiveUnionI; }
Andrew Trick14e8d712010-10-22 23:09:15 +0000175
Andrew Trick18c57a82010-11-30 23:18:47 +0000176 bool operator==(const InterferenceResult &IR) const {
177 return VirtRegI == IR.VirtRegI && LiveUnionI == IR.LiveUnionI;
Andrew Trick14e8d712010-10-22 23:09:15 +0000178 }
Andrew Trick18c57a82010-11-30 23:18:47 +0000179 bool operator!=(const InterferenceResult &IR) const {
180 return !operator==(IR);
Andrew Trick14e8d712010-10-22 23:09:15 +0000181 }
182 };
183
184 /// Query interferences between a single live virtual register and a live
185 /// interval union.
186 class Query {
Andrew Trick18c57a82010-11-30 23:18:47 +0000187 LiveIntervalUnion *LiveUnion;
188 LiveInterval *VirtReg;
189 InterferenceResult FirstInterference;
190 SmallVector<LiveInterval*,4> InterferingVRegs;
191 bool SeenAllInterferences;
192 bool SeenUnspillableVReg;
Andrew Trick14e8d712010-10-22 23:09:15 +0000193
194 public:
Andrew Trick18c57a82010-11-30 23:18:47 +0000195 Query(): LiveUnion(), VirtReg() {}
Andrew Trick14e8d712010-10-22 23:09:15 +0000196
Andrew Trick18c57a82010-11-30 23:18:47 +0000197 Query(LiveInterval *VReg, LiveIntervalUnion *LIU):
198 LiveUnion(LIU), VirtReg(VReg), SeenAllInterferences(false),
199 SeenUnspillableVReg(false)
200 {}
Andrew Tricke141a492010-11-08 18:02:08 +0000201
202 void clear() {
Andrew Trick18c57a82010-11-30 23:18:47 +0000203 LiveUnion = NULL;
204 VirtReg = NULL;
205 FirstInterference = InterferenceResult();
206 InterferingVRegs.clear();
207 SeenAllInterferences = false;
208 SeenUnspillableVReg = false;
Andrew Tricke141a492010-11-08 18:02:08 +0000209 }
Andrew Trick18c57a82010-11-30 23:18:47 +0000210
211 void init(LiveInterval *VReg, LiveIntervalUnion *LIU) {
212 if (VirtReg == VReg) {
Andrew Tricke141a492010-11-08 18:02:08 +0000213 // We currently allow query objects to be reused acrossed live virtual
214 // registers, but always for the same live interval union.
Andrew Trick18c57a82010-11-30 23:18:47 +0000215 assert(LiveUnion == LIU && "inconsistent initialization");
Andrew Tricke141a492010-11-08 18:02:08 +0000216 // Retain cached results, e.g. firstInterference.
217 return;
218 }
Andrew Trick18c57a82010-11-30 23:18:47 +0000219 clear();
220 LiveUnion = LIU;
221 VirtReg = VReg;
Andrew Tricke141a492010-11-08 18:02:08 +0000222 }
223
Andrew Trick18c57a82010-11-30 23:18:47 +0000224 LiveInterval &virtReg() const {
225 assert(VirtReg && "uninitialized");
226 return *VirtReg;
227 }
Andrew Trick14e8d712010-10-22 23:09:15 +0000228
Andrew Trick18c57a82010-11-30 23:18:47 +0000229 bool isInterference(const InterferenceResult &IR) const {
230 if (IR.VirtRegI != VirtReg->end()) {
231 assert(overlap(*IR.VirtRegI, *IR.LiveUnionI) &&
Andrew Trick14e8d712010-10-22 23:09:15 +0000232 "invalid segment iterators");
233 return true;
234 }
235 return false;
236 }
237
Andrew Trick18c57a82010-11-30 23:18:47 +0000238 // Does this live virtual register interfere with the union?
Andrew Trick14e8d712010-10-22 23:09:15 +0000239 bool checkInterference() { return isInterference(firstInterference()); }
240
Andrew Tricke141a492010-11-08 18:02:08 +0000241 // Get the first pair of interfering segments, or a noninterfering result.
242 // This initializes the firstInterference_ cache.
Andrew Trick14e8d712010-10-22 23:09:15 +0000243 InterferenceResult firstInterference();
244
245 // Treat the result as an iterator and advance to the next interfering pair
246 // of segments. Visiting each unique interfering pairs means that the same
Andrew Trick18c57a82010-11-30 23:18:47 +0000247 // VirtReg or LiveUnion segment may be visited multiple times.
248 bool nextInterference(InterferenceResult &IR) const;
Andrew Trick14e8d712010-10-22 23:09:15 +0000249
Andrew Trickf4baeaf2010-11-10 19:18:47 +0000250 // Count the virtual registers in this union that interfere with this
251 // query's live virtual register, up to maxInterferingRegs.
Andrew Trick18c57a82010-11-30 23:18:47 +0000252 unsigned collectInterferingVRegs(unsigned MaxInterferingRegs = UINT_MAX);
Andrew Trickf4baeaf2010-11-10 19:18:47 +0000253
254 // Was this virtual register visited during collectInterferingVRegs?
Andrew Trick18c57a82010-11-30 23:18:47 +0000255 bool isSeenInterference(LiveInterval *VReg) const;
256
257 // Did collectInterferingVRegs collect all interferences?
258 bool seenAllInterferences() const { return SeenAllInterferences; }
Andrew Trickf4baeaf2010-11-10 19:18:47 +0000259
260 // Did collectInterferingVRegs encounter an unspillable vreg?
Andrew Trick18c57a82010-11-30 23:18:47 +0000261 bool seenUnspillableVReg() const { return SeenUnspillableVReg; }
Andrew Trickf4baeaf2010-11-10 19:18:47 +0000262
263 // Vector generated by collectInterferingVRegs.
264 const SmallVectorImpl<LiveInterval*> &interferingVRegs() const {
Andrew Trick18c57a82010-11-30 23:18:47 +0000265 return InterferingVRegs;
Andrew Trickf4baeaf2010-11-10 19:18:47 +0000266 }
Andrew Trick18c57a82010-11-30 23:18:47 +0000267
Andrew Trick14e8d712010-10-22 23:09:15 +0000268 private:
Andrew Trick8a83d542010-11-11 17:46:29 +0000269 Query(const Query&); // DO NOT IMPLEMENT
270 void operator=(const Query&); // DO NOT IMPLEMENT
Andrew Trick18c57a82010-11-30 23:18:47 +0000271
Andrew Trick14e8d712010-10-22 23:09:15 +0000272 // Private interface for queries
Andrew Trick18c57a82010-11-30 23:18:47 +0000273 void findIntersection(InterferenceResult &IR) const;
Andrew Trick14e8d712010-10-22 23:09:15 +0000274 };
275};
276
277} // end namespace llvm
278
279#endif // !defined(LLVM_CODEGEN_LIVEINTERVALUNION)