1 //===- LiveIntervalUnion.cpp - Live interval union data structure ---------===//
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7 //===----------------------------------------------------------------------===//
9 // LiveIntervalUnion represents a coalesced set of live intervals. This may be
10 // used during coalescing to represent a congruence class, or during register
11 // allocation to model liveness of a physical register.
13 //===----------------------------------------------------------------------===//
15 #include "llvm/CodeGen/LiveIntervalUnion.h"
16 #include "llvm/ADT/STLExtras.h"
17 #include "llvm/ADT/SparseBitVector.h"
18 #include "llvm/CodeGen/LiveInterval.h"
19 #include "llvm/CodeGen/TargetRegisterInfo.h"
20 #include "llvm/Support/raw_ostream.h"
26 #define DEBUG_TYPE "regalloc"
28 // Merge a LiveInterval's segments. Guarantee no overlaps.
29 void LiveIntervalUnion::unify(LiveInterval
&VirtReg
, const LiveRange
&Range
) {
34 // Insert each of the virtual register's live segments into the map.
35 LiveRange::const_iterator RegPos
= Range
.begin();
36 LiveRange::const_iterator RegEnd
= Range
.end();
37 SegmentIter SegPos
= Segments
.find(RegPos
->start
);
39 while (SegPos
.valid()) {
40 SegPos
.insert(RegPos
->start
, RegPos
->end
, &VirtReg
);
41 if (++RegPos
== RegEnd
)
43 SegPos
.advanceTo(RegPos
->start
);
46 // We have reached the end of Segments, so it is no longer necessary to search
47 // for the insertion position.
48 // It is faster to insert the end first.
50 SegPos
.insert(RegEnd
->start
, RegEnd
->end
, &VirtReg
);
51 for (; RegPos
!= RegEnd
; ++RegPos
, ++SegPos
)
52 SegPos
.insert(RegPos
->start
, RegPos
->end
, &VirtReg
);
55 // Remove a live virtual register's segments from this union.
56 void LiveIntervalUnion::extract(LiveInterval
&VirtReg
, const LiveRange
&Range
) {
61 // Remove each of the virtual register's live segments from the map.
62 LiveRange::const_iterator RegPos
= Range
.begin();
63 LiveRange::const_iterator RegEnd
= Range
.end();
64 SegmentIter SegPos
= Segments
.find(RegPos
->start
);
67 assert(SegPos
.value() == &VirtReg
&& "Inconsistent LiveInterval");
72 // Skip all segments that may have been coalesced.
73 RegPos
= Range
.advanceTo(RegPos
, SegPos
.start());
77 SegPos
.advanceTo(RegPos
->start
);
82 LiveIntervalUnion::print(raw_ostream
&OS
, const TargetRegisterInfo
*TRI
) const {
87 for (LiveSegments::const_iterator SI
= Segments
.begin(); SI
.valid(); ++SI
) {
88 OS
<< " [" << SI
.start() << ' ' << SI
.stop() << "):"
89 << printReg(SI
.value()->reg
, TRI
);
95 // Verify the live intervals in this union and add them to the visited set.
96 void LiveIntervalUnion::verify(LiveVirtRegBitSet
& VisitedVRegs
) {
97 for (SegmentIter SI
= Segments
.begin(); SI
.valid(); ++SI
)
98 VisitedVRegs
.set(SI
.value()->reg
);
102 // Scan the vector of interfering virtual registers in this union. Assume it's
104 bool LiveIntervalUnion::Query::isSeenInterference(LiveInterval
*VirtReg
) const {
105 return is_contained(InterferingVRegs
, VirtReg
);
108 // Collect virtual registers in this union that interfere with this
109 // query's live virtual register.
111 // The query state is one of:
113 // 1. CheckedFirstInterference == false: Iterators are uninitialized.
114 // 2. SeenAllInterferences == true: InterferingVRegs complete, iterators unused.
115 // 3. Iterators left at the last seen intersection.
117 unsigned LiveIntervalUnion::Query::
118 collectInterferingVRegs(unsigned MaxInterferingRegs
) {
119 // Fast path return if we already have the desired information.
120 if (SeenAllInterferences
|| InterferingVRegs
.size() >= MaxInterferingRegs
)
121 return InterferingVRegs
.size();
123 // Set up iterators on the first call.
124 if (!CheckedFirstInterference
) {
125 CheckedFirstInterference
= true;
127 // Quickly skip interference check for empty sets.
128 if (LR
->empty() || LiveUnion
->empty()) {
129 SeenAllInterferences
= true;
133 // In most cases, the union will start before LR.
135 LiveUnionI
.setMap(LiveUnion
->getMap());
136 LiveUnionI
.find(LRI
->start
);
139 LiveRange::const_iterator LREnd
= LR
->end();
140 LiveInterval
*RecentReg
= nullptr;
141 while (LiveUnionI
.valid()) {
142 assert(LRI
!= LREnd
&& "Reached end of LR");
144 // Check for overlapping interference.
145 while (LRI
->start
< LiveUnionI
.stop() && LRI
->end
> LiveUnionI
.start()) {
146 // This is an overlap, record the interfering register.
147 LiveInterval
*VReg
= LiveUnionI
.value();
148 if (VReg
!= RecentReg
&& !isSeenInterference(VReg
)) {
150 InterferingVRegs
.push_back(VReg
);
151 if (InterferingVRegs
.size() >= MaxInterferingRegs
)
152 return InterferingVRegs
.size();
154 // This LiveUnion segment is no longer interesting.
155 if (!(++LiveUnionI
).valid()) {
156 SeenAllInterferences
= true;
157 return InterferingVRegs
.size();
161 // The iterators are now not overlapping, LiveUnionI has been advanced
163 assert(LRI
->end
<= LiveUnionI
.start() && "Expected non-overlap");
165 // Advance the iterator that ends first.
166 LRI
= LR
->advanceTo(LRI
, LiveUnionI
.start());
170 // Detect overlap, handle above.
171 if (LRI
->start
< LiveUnionI
.stop())
174 // Still not overlapping. Catch up LiveUnionI.
175 LiveUnionI
.advanceTo(LRI
->start
);
177 SeenAllInterferences
= true;
178 return InterferingVRegs
.size();
181 void LiveIntervalUnion::Array::init(LiveIntervalUnion::Allocator
&Alloc
,
183 // Reuse existing allocation.
188 LIUs
= static_cast<LiveIntervalUnion
*>(
189 safe_malloc(sizeof(LiveIntervalUnion
)*NSize
));
190 for (unsigned i
= 0; i
!= Size
; ++i
)
191 new(LIUs
+ i
) LiveIntervalUnion(Alloc
);
194 void LiveIntervalUnion::Array::clear() {
197 for (unsigned i
= 0; i
!= Size
; ++i
)
198 LIUs
[i
].~LiveIntervalUnion();