1 //===- LiveIntervalUnion.cpp - Live interval union data structure ---------===//
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 represents a coalesced set of live intervals. This may be
11 // used during coalescing to represent a congruence class, or during register
12 // allocation to model liveness of a physical register.
14 //===----------------------------------------------------------------------===//
16 #include "llvm/CodeGen/LiveIntervalUnion.h"
17 #include "llvm/ADT/STLExtras.h"
18 #include "llvm/ADT/SparseBitVector.h"
19 #include "llvm/CodeGen/LiveInterval.h"
20 #include "llvm/CodeGen/TargetRegisterInfo.h"
21 #include "llvm/Support/raw_ostream.h"
27 #define DEBUG_TYPE "regalloc"
29 // Merge a LiveInterval's segments. Guarantee no overlaps.
30 void LiveIntervalUnion::unify(LiveInterval
&VirtReg
, const LiveRange
&Range
) {
35 // Insert each of the virtual register's live segments into the map.
36 LiveRange::const_iterator RegPos
= Range
.begin();
37 LiveRange::const_iterator RegEnd
= Range
.end();
38 SegmentIter SegPos
= Segments
.find(RegPos
->start
);
40 while (SegPos
.valid()) {
41 SegPos
.insert(RegPos
->start
, RegPos
->end
, &VirtReg
);
42 if (++RegPos
== RegEnd
)
44 SegPos
.advanceTo(RegPos
->start
);
47 // We have reached the end of Segments, so it is no longer necessary to search
48 // for the insertion position.
49 // It is faster to insert the end first.
51 SegPos
.insert(RegEnd
->start
, RegEnd
->end
, &VirtReg
);
52 for (; RegPos
!= RegEnd
; ++RegPos
, ++SegPos
)
53 SegPos
.insert(RegPos
->start
, RegPos
->end
, &VirtReg
);
56 // Remove a live virtual register's segments from this union.
57 void LiveIntervalUnion::extract(LiveInterval
&VirtReg
, const LiveRange
&Range
) {
62 // Remove each of the virtual register's live segments from the map.
63 LiveRange::const_iterator RegPos
= Range
.begin();
64 LiveRange::const_iterator RegEnd
= Range
.end();
65 SegmentIter SegPos
= Segments
.find(RegPos
->start
);
68 assert(SegPos
.value() == &VirtReg
&& "Inconsistent LiveInterval");
73 // Skip all segments that may have been coalesced.
74 RegPos
= Range
.advanceTo(RegPos
, SegPos
.start());
78 SegPos
.advanceTo(RegPos
->start
);
83 LiveIntervalUnion::print(raw_ostream
&OS
, const TargetRegisterInfo
*TRI
) const {
88 for (LiveSegments::const_iterator SI
= Segments
.begin(); SI
.valid(); ++SI
) {
89 OS
<< " [" << SI
.start() << ' ' << SI
.stop() << "):"
90 << printReg(SI
.value()->reg
, TRI
);
96 // Verify the live intervals in this union and add them to the visited set.
97 void LiveIntervalUnion::verify(LiveVirtRegBitSet
& VisitedVRegs
) {
98 for (SegmentIter SI
= Segments
.begin(); SI
.valid(); ++SI
)
99 VisitedVRegs
.set(SI
.value()->reg
);
103 // Scan the vector of interfering virtual registers in this union. Assume it's
105 bool LiveIntervalUnion::Query::isSeenInterference(LiveInterval
*VirtReg
) const {
106 return is_contained(InterferingVRegs
, VirtReg
);
109 // Collect virtual registers in this union that interfere with this
110 // query's live virtual register.
112 // The query state is one of:
114 // 1. CheckedFirstInterference == false: Iterators are uninitialized.
115 // 2. SeenAllInterferences == true: InterferingVRegs complete, iterators unused.
116 // 3. Iterators left at the last seen intersection.
118 unsigned LiveIntervalUnion::Query::
119 collectInterferingVRegs(unsigned MaxInterferingRegs
) {
120 // Fast path return if we already have the desired information.
121 if (SeenAllInterferences
|| InterferingVRegs
.size() >= MaxInterferingRegs
)
122 return InterferingVRegs
.size();
124 // Set up iterators on the first call.
125 if (!CheckedFirstInterference
) {
126 CheckedFirstInterference
= true;
128 // Quickly skip interference check for empty sets.
129 if (LR
->empty() || LiveUnion
->empty()) {
130 SeenAllInterferences
= true;
134 // In most cases, the union will start before LR.
136 LiveUnionI
.setMap(LiveUnion
->getMap());
137 LiveUnionI
.find(LRI
->start
);
140 LiveRange::const_iterator LREnd
= LR
->end();
141 LiveInterval
*RecentReg
= nullptr;
142 while (LiveUnionI
.valid()) {
143 assert(LRI
!= LREnd
&& "Reached end of LR");
145 // Check for overlapping interference.
146 while (LRI
->start
< LiveUnionI
.stop() && LRI
->end
> LiveUnionI
.start()) {
147 // This is an overlap, record the interfering register.
148 LiveInterval
*VReg
= LiveUnionI
.value();
149 if (VReg
!= RecentReg
&& !isSeenInterference(VReg
)) {
151 InterferingVRegs
.push_back(VReg
);
152 if (InterferingVRegs
.size() >= MaxInterferingRegs
)
153 return InterferingVRegs
.size();
155 // This LiveUnion segment is no longer interesting.
156 if (!(++LiveUnionI
).valid()) {
157 SeenAllInterferences
= true;
158 return InterferingVRegs
.size();
162 // The iterators are now not overlapping, LiveUnionI has been advanced
164 assert(LRI
->end
<= LiveUnionI
.start() && "Expected non-overlap");
166 // Advance the iterator that ends first.
167 LRI
= LR
->advanceTo(LRI
, LiveUnionI
.start());
171 // Detect overlap, handle above.
172 if (LRI
->start
< LiveUnionI
.stop())
175 // Still not overlapping. Catch up LiveUnionI.
176 LiveUnionI
.advanceTo(LRI
->start
);
178 SeenAllInterferences
= true;
179 return InterferingVRegs
.size();
182 void LiveIntervalUnion::Array::init(LiveIntervalUnion::Allocator
&Alloc
,
184 // Reuse existing allocation.
189 LIUs
= static_cast<LiveIntervalUnion
*>(
190 safe_malloc(sizeof(LiveIntervalUnion
)*NSize
));
191 for (unsigned i
= 0; i
!= Size
; ++i
)
192 new(LIUs
+ i
) LiveIntervalUnion(Alloc
);
195 void LiveIntervalUnion::Array::clear() {
198 for (unsigned i
= 0; i
!= Size
; ++i
)
199 LIUs
[i
].~LiveIntervalUnion();