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/SparseBitVector.h"
18 #include "llvm/Support/Debug.h"
19 #include "llvm/Support/raw_ostream.h"
20 #include "llvm/Target/TargetRegisterInfo.h"
25 #define DEBUG_TYPE "regalloc"
28 // Merge a LiveInterval's segments. Guarantee no overlaps.
29 void LiveIntervalUnion::unify(LiveInterval &VirtReg) {
34 // Insert each of the virtual register's live segments into the map.
35 LiveInterval::iterator RegPos = VirtReg.begin();
36 LiveInterval::iterator RegEnd = VirtReg.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) {
61 // Remove each of the virtual register's live segments from the map.
62 LiveInterval::iterator RegPos = VirtReg.begin();
63 LiveInterval::iterator RegEnd = VirtReg.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 = VirtReg.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 SmallVectorImpl<LiveInterval*>::const_iterator I =
106 std::find(InterferingVRegs.begin(), InterferingVRegs.end(), VirtReg);
107 return I != InterferingVRegs.end();
110 // Collect virtual registers in this union that interfere with this
111 // query's live virtual register.
113 // The query state is one of:
115 // 1. CheckedFirstInterference == false: Iterators are uninitialized.
116 // 2. SeenAllInterferences == true: InterferingVRegs complete, iterators unused.
117 // 3. Iterators left at the last seen intersection.
119 unsigned LiveIntervalUnion::Query::
120 collectInterferingVRegs(unsigned MaxInterferingRegs) {
121 // Fast path return if we already have the desired information.
122 if (SeenAllInterferences || InterferingVRegs.size() >= MaxInterferingRegs)
123 return InterferingVRegs.size();
125 // Set up iterators on the first call.
126 if (!CheckedFirstInterference) {
127 CheckedFirstInterference = true;
129 // Quickly skip interference check for empty sets.
130 if (VirtReg->empty() || LiveUnion->empty()) {
131 SeenAllInterferences = true;
135 // In most cases, the union will start before VirtReg.
136 VirtRegI = VirtReg->begin();
137 LiveUnionI.setMap(LiveUnion->getMap());
138 LiveUnionI.find(VirtRegI->start);
141 LiveInterval::iterator VirtRegEnd = VirtReg->end();
142 LiveInterval *RecentReg = nullptr;
143 while (LiveUnionI.valid()) {
144 assert(VirtRegI != VirtRegEnd && "Reached end of VirtReg");
146 // Check for overlapping interference.
147 while (VirtRegI->start < LiveUnionI.stop() &&
148 VirtRegI->end > LiveUnionI.start()) {
149 // This is an overlap, record the interfering register.
150 LiveInterval *VReg = LiveUnionI.value();
151 if (VReg != RecentReg && !isSeenInterference(VReg)) {
153 InterferingVRegs.push_back(VReg);
154 if (InterferingVRegs.size() >= MaxInterferingRegs)
155 return InterferingVRegs.size();
157 // This LiveUnion segment is no longer interesting.
158 if (!(++LiveUnionI).valid()) {
159 SeenAllInterferences = true;
160 return InterferingVRegs.size();
164 // The iterators are now not overlapping, LiveUnionI has been advanced
166 assert(VirtRegI->end <= LiveUnionI.start() && "Expected non-overlap");
168 // Advance the iterator that ends first.
169 VirtRegI = VirtReg->advanceTo(VirtRegI, LiveUnionI.start());
170 if (VirtRegI == VirtRegEnd)
173 // Detect overlap, handle above.
174 if (VirtRegI->start < LiveUnionI.stop())
177 // Still not overlapping. Catch up LiveUnionI.
178 LiveUnionI.advanceTo(VirtRegI->start);
180 SeenAllInterferences = true;
181 return InterferingVRegs.size();
184 void LiveIntervalUnion::Array::init(LiveIntervalUnion::Allocator &Alloc,
186 // Reuse existing allocation.
191 LIUs = static_cast<LiveIntervalUnion*>(
192 malloc(sizeof(LiveIntervalUnion)*NSize));
193 for (unsigned i = 0; i != Size; ++i)
194 new(LIUs + i) LiveIntervalUnion(Alloc);
197 void LiveIntervalUnion::Array::clear() {
200 for (unsigned i = 0; i != Size; ++i)
201 LIUs[i].~LiveIntervalUnion();