1 //===-- LiveIntervalUnion.h - Live interval union data struct --*- C++ -*--===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
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
15 //===----------------------------------------------------------------------===//
17 #ifndef LLVM_CODEGEN_LIVEINTERVALUNION
18 #define LLVM_CODEGEN_LIVEINTERVALUNION
20 #include "llvm/CodeGen/LiveInterval.h"
27 // forward declaration
28 template <unsigned Element> class SparseBitVector;
29 typedef SparseBitVector<128> LiveVirtRegBitSet;
32 /// 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.
40 /// Note: This currently represents a half-open interval [Start,End).
41 /// If LiveRange is modified to represent a closed interval, so should this.
45 LiveInterval *VirtReg;
47 LiveSegment(const LiveRange& LR, LiveInterval *VReg)
48 : Start(LR.start), End(LR.end), VirtReg(VReg) {}
50 bool operator==(const LiveSegment &LS) const {
51 return Start == LS.Start && End == LS.End && VirtReg == LS.VirtReg;
54 bool operator!=(const LiveSegment &LS) const {
55 return !operator==(LS);
58 // Order segments by starting point only--we expect them to be disjoint.
59 bool operator<(const LiveSegment &LS) const { return Start < LS.Start; }
62 void print(raw_ostream &OS) const;
65 inline bool operator<(SlotIndex Idx, const LiveSegment &LS) {
66 return Idx < LS.Start;
69 inline bool operator<(const LiveSegment &LS, SlotIndex Idx) {
70 return LS.Start < Idx;
73 /// Compare a live virtual register segment to a LiveIntervalUnion segment.
74 inline bool overlap(const LiveRange &VirtRegSegment,
75 const LiveSegment &LiveUnionSegment) {
76 return VirtRegSegment.start < LiveUnionSegment.End &&
77 LiveUnionSegment.Start < VirtRegSegment.end;
80 template <> struct isPodLike<LiveSegment> { static const bool value = true; };
82 raw_ostream& operator<<(raw_ostream& OS, const LiveSegment &LS);
84 /// Abstraction to provide info for the representative register.
85 class AbstractRegisterDescription {
87 virtual const char *getName(unsigned Reg) const = 0;
88 virtual ~AbstractRegisterDescription() {}
91 /// 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.
95 class LiveIntervalUnion {
96 // A set of live virtual register segments that supports fast insertion,
97 // intersection, and removal.
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;
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
108 typedef std::vector<LiveInterval*> LiveVRegs;
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;
116 class InterferenceResult;
120 unsigned RepReg; // representative register number
121 LiveSegments Segments; // union of virtual reg segements
124 // default ctor avoids placement new
125 LiveIntervalUnion() : RepReg(0) {}
127 // Initialize the union by associating it with a representative register
129 void init(unsigned Reg) { RepReg = Reg; }
131 // Iterate over all segments in the union of live virtual registers ordered
132 // by their starting position.
133 SegmentIter begin() { return Segments.begin(); }
134 SegmentIter end() { return Segments.end(); }
136 // Return an iterator to the first segment after or including begin that
137 // intersects with LS.
138 SegmentIter upperBound(SegmentIter SegBegin, const LiveSegment &LS);
140 // Add a live virtual register to this union and merge its segments.
141 // Holds a nonconst reference to the VirtReg for later maniplution.
142 void unify(LiveInterval &VirtReg);
144 // Remove a live virtual register's segments from this union.
145 void extract(const LiveInterval &VirtReg);
147 void dump(const AbstractRegisterDescription *RegDesc) const;
149 // If tri != NULL, use it to decode RepReg
150 void print(raw_ostream &OS, const AbstractRegisterDescription *RegDesc) const;
153 // Verify the live intervals in this union and add them to the visited set.
154 void verify(LiveVirtRegBitSet& VisitedVRegs);
157 /// 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 {
164 LiveInterval::iterator VirtRegI; // current position in VirtReg
165 SegmentIter LiveUnionI; // current position in LiveUnion
168 InterferenceResult(LiveInterval::iterator VRegI, SegmentIter UnionI)
169 : VirtRegI(VRegI), LiveUnionI(UnionI) {}
172 // Public default ctor.
173 InterferenceResult(): VirtRegI(), LiveUnionI() {}
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.
178 // Access the VirtReg segment.
179 LiveInterval::iterator virtRegPos() const { return VirtRegI; }
181 // Access the LiveUnion segment.
182 SegmentIter liveUnionPos() const { return LiveUnionI; }
184 bool operator==(const InterferenceResult &IR) const {
185 return VirtRegI == IR.VirtRegI && LiveUnionI == IR.LiveUnionI;
187 bool operator!=(const InterferenceResult &IR) const {
188 return !operator==(IR);
192 /// Query interferences between a single live virtual register and a live
195 LiveIntervalUnion *LiveUnion;
196 LiveInterval *VirtReg;
197 InterferenceResult FirstInterference;
198 SmallVector<LiveInterval*,4> InterferingVRegs;
199 bool SeenAllInterferences;
200 bool SeenUnspillableVReg;
203 Query(): LiveUnion(), VirtReg() {}
205 Query(LiveInterval *VReg, LiveIntervalUnion *LIU):
206 LiveUnion(LIU), VirtReg(VReg), SeenAllInterferences(false),
207 SeenUnspillableVReg(false)
213 FirstInterference = InterferenceResult();
214 InterferingVRegs.clear();
215 SeenAllInterferences = false;
216 SeenUnspillableVReg = false;
219 void init(LiveInterval *VReg, LiveIntervalUnion *LIU) {
220 if (VirtReg == VReg) {
221 // We currently allow query objects to be reused acrossed live virtual
222 // registers, but always for the same live interval union.
223 assert(LiveUnion == LIU && "inconsistent initialization");
224 // Retain cached results, e.g. firstInterference.
232 LiveInterval &virtReg() const {
233 assert(VirtReg && "uninitialized");
237 bool isInterference(const InterferenceResult &IR) const {
238 if (IR.VirtRegI != VirtReg->end()) {
239 assert(overlap(*IR.VirtRegI, *IR.LiveUnionI) &&
240 "invalid segment iterators");
246 // Does this live virtual register interfere with the union?
247 bool checkInterference() { return isInterference(firstInterference()); }
249 // Get the first pair of interfering segments, or a noninterfering result.
250 // This initializes the firstInterference_ cache.
251 InterferenceResult firstInterference();
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
255 // VirtReg or LiveUnion segment may be visited multiple times.
256 bool nextInterference(InterferenceResult &IR) const;
258 // Count the virtual registers in this union that interfere with this
259 // query's live virtual register, up to maxInterferingRegs.
260 unsigned collectInterferingVRegs(unsigned MaxInterferingRegs = UINT_MAX);
262 // Was this virtual register visited during collectInterferingVRegs?
263 bool isSeenInterference(LiveInterval *VReg) const;
265 // Did collectInterferingVRegs collect all interferences?
266 bool seenAllInterferences() const { return SeenAllInterferences; }
268 // Did collectInterferingVRegs encounter an unspillable vreg?
269 bool seenUnspillableVReg() const { return SeenUnspillableVReg; }
271 // Vector generated by collectInterferingVRegs.
272 const SmallVectorImpl<LiveInterval*> &interferingVRegs() const {
273 return InterferingVRegs;
277 Query(const Query&); // DO NOT IMPLEMENT
278 void operator=(const Query&); // DO NOT IMPLEMENT
280 // Private interface for queries
281 void findIntersection(InterferenceResult &IR) const;
285 } // end namespace llvm
287 #endif // !defined(LLVM_CODEGEN_LIVEINTERVALUNION)