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/ADT/IntervalMap.h"
21 #include "llvm/CodeGen/LiveInterval.h"
25 class MachineLoopRange;
26 class TargetRegisterInfo;
29 // forward declaration
30 template <unsigned Element> class SparseBitVector;
31 typedef SparseBitVector<128> LiveVirtRegBitSet;
34 /// Compare a live virtual register segment to a LiveIntervalUnion segment.
36 overlap(const LiveRange &VRSeg,
37 const IntervalMap<SlotIndex, LiveInterval*>::const_iterator &LUSeg) {
38 return VRSeg.start < LUSeg.stop() && LUSeg.start() < VRSeg.end;
41 /// Union of live intervals that are strong candidates for coalescing into a
42 /// single register (either physical or virtual depending on the context). We
43 /// expect the constituent live intervals to be disjoint, although we may
44 /// eventually make exceptions to handle value-based interference.
45 class LiveIntervalUnion {
46 // A set of live virtual register segments that supports fast insertion,
47 // intersection, and removal.
48 // Mapping SlotIndex intervals to virtual register numbers.
49 typedef IntervalMap<SlotIndex, LiveInterval*> LiveSegments;
52 // SegmentIter can advance to the next segment ordered by starting position
53 // which may belong to a different live virtual register. We also must be able
54 // to reach the current segment's containing virtual register.
55 typedef LiveSegments::iterator SegmentIter;
57 // LiveIntervalUnions share an external allocator.
58 typedef LiveSegments::Allocator Allocator;
63 unsigned Tag; // unique tag for current contents.
64 LiveSegments Segments; // union of virtual reg segments
67 explicit LiveIntervalUnion(Allocator &a) : Tag(0), Segments(a) {}
69 // Iterate over all segments in the union of live virtual registers ordered
70 // by their starting position.
71 SegmentIter begin() { return Segments.begin(); }
72 SegmentIter end() { return Segments.end(); }
73 SegmentIter find(SlotIndex x) { return Segments.find(x); }
74 bool empty() const { return Segments.empty(); }
75 SlotIndex startIndex() const { return Segments.start(); }
77 // Provide public access to the underlying map to allow overlap iteration.
78 typedef LiveSegments Map;
79 const Map &getMap() { return Segments; }
81 /// getTag - Return an opaque tag representing the current state of the union.
82 unsigned getTag() const { return Tag; }
84 /// changedSince - Return true if the union change since getTag returned tag.
85 bool changedSince(unsigned tag) const { return tag != Tag; }
87 // Add a live virtual register to this union and merge its segments.
88 void unify(LiveInterval &VirtReg);
90 // Remove a live virtual register's segments from this union.
91 void extract(LiveInterval &VirtReg);
93 // Remove all inserted virtual registers.
94 void clear() { Segments.clear(); ++Tag; }
96 // Print union, using TRI to translate register names
97 void print(raw_ostream &OS, const TargetRegisterInfo *TRI) const;
100 // Verify the live intervals in this union and add them to the visited set.
101 void verify(LiveVirtRegBitSet& VisitedVRegs);
104 /// Query interferences between a single live virtual register and a live
107 LiveIntervalUnion *LiveUnion;
108 LiveInterval *VirtReg;
109 LiveInterval::iterator VirtRegI; // current position in VirtReg
110 SegmentIter LiveUnionI; // current position in LiveUnion
111 SmallVector<LiveInterval*,4> InterferingVRegs;
112 bool CheckedFirstInterference;
113 bool SeenAllInterferences;
114 bool SeenUnspillableVReg;
115 unsigned Tag, UserTag;
118 Query(): LiveUnion(), VirtReg(), Tag(0), UserTag(0) {}
120 Query(LiveInterval *VReg, LiveIntervalUnion *LIU):
121 LiveUnion(LIU), VirtReg(VReg), CheckedFirstInterference(false),
122 SeenAllInterferences(false), SeenUnspillableVReg(false)
128 InterferingVRegs.clear();
129 CheckedFirstInterference = false;
130 SeenAllInterferences = false;
131 SeenUnspillableVReg = false;
136 void init(unsigned UTag, LiveInterval *VReg, LiveIntervalUnion *LIU) {
137 assert(VReg && LIU && "Invalid arguments");
138 if (UserTag == UTag && VirtReg == VReg &&
139 LiveUnion == LIU && !LIU->changedSince(Tag)) {
140 // Retain cached results, e.g. firstInterference.
150 LiveInterval &virtReg() const {
151 assert(VirtReg && "uninitialized");
155 // Does this live virtual register interfere with the union?
156 bool checkInterference() { return collectInterferingVRegs(1); }
158 // Count the virtual registers in this union that interfere with this
159 // query's live virtual register, up to maxInterferingRegs.
160 unsigned collectInterferingVRegs(unsigned MaxInterferingRegs = UINT_MAX);
162 // Was this virtual register visited during collectInterferingVRegs?
163 bool isSeenInterference(LiveInterval *VReg) const;
165 // Did collectInterferingVRegs collect all interferences?
166 bool seenAllInterferences() const { return SeenAllInterferences; }
168 // Did collectInterferingVRegs encounter an unspillable vreg?
169 bool seenUnspillableVReg() const { return SeenUnspillableVReg; }
171 // Vector generated by collectInterferingVRegs.
172 const SmallVectorImpl<LiveInterval*> &interferingVRegs() const {
173 return InterferingVRegs;
176 /// checkLoopInterference - Return true if there is interference overlapping
178 bool checkLoopInterference(MachineLoopRange*);
181 Query(const Query&) LLVM_DELETED_FUNCTION;
182 void operator=(const Query&) LLVM_DELETED_FUNCTION;
185 // Array of LiveIntervalUnions.
188 LiveIntervalUnion *LIUs;
190 Array() : Size(0), LIUs(0) {}
191 ~Array() { clear(); }
193 // Initialize the array to have Size entries.
194 // Reuse an existing allocation if the size matches.
195 void init(LiveIntervalUnion::Allocator&, unsigned Size);
197 unsigned size() const { return Size; }
201 LiveIntervalUnion& operator[](unsigned idx) {
202 assert(idx < Size && "idx out of bounds");
208 } // end namespace llvm
210 #endif // !defined(LLVM_CODEGEN_LIVEINTERVALUNION)