blob: dadd02bfc65439d0041c5faf91996f31572dac9b [file] [log] [blame]
Andrew Trick14e8d712010-10-22 23:09:15 +00001//===-- LiveIntervalUnion.cpp - Live interval union data structure --------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
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.
13//
14//===----------------------------------------------------------------------===//
15
16#define DEBUG_TYPE "regalloc"
17#include "LiveIntervalUnion.h"
Andrew Trick071d1c02010-11-09 21:04:34 +000018#include "llvm/ADT/SparseBitVector.h"
Jakob Stoklund Olesenff2e9b42010-12-17 04:09:47 +000019#include "llvm/CodeGen/MachineLoopRanges.h"
Andrew Trick14e8d712010-10-22 23:09:15 +000020#include "llvm/Support/Debug.h"
21#include "llvm/Support/raw_ostream.h"
Jakob Stoklund Olesen4a84cce2010-12-14 18:53:47 +000022#include "llvm/Target/TargetRegisterInfo.h"
23
Lang Hamesb638c782011-12-21 20:16:11 +000024#include <algorithm>
25
Andrew Trick14e8d712010-10-22 23:09:15 +000026using namespace llvm;
27
Andrew Tricke141a492010-11-08 18:02:08 +000028
Andrew Trick14e8d712010-10-22 23:09:15 +000029// Merge a LiveInterval's segments. Guarantee no overlaps.
Andrew Trick18c57a82010-11-30 23:18:47 +000030void LiveIntervalUnion::unify(LiveInterval &VirtReg) {
Jakob Stoklund Olesen953af2c2010-12-07 23:18:47 +000031 if (VirtReg.empty())
32 return;
Jakob Stoklund Olesen4f6364f2011-02-09 21:52:03 +000033 ++Tag;
Andrew Trick18c57a82010-11-30 23:18:47 +000034
35 // Insert each of the virtual register's live segments into the map.
Jakob Stoklund Olesen953af2c2010-12-07 23:18:47 +000036 LiveInterval::iterator RegPos = VirtReg.begin();
37 LiveInterval::iterator RegEnd = VirtReg.end();
38 SegmentIter SegPos = Segments.find(RegPos->start);
Andrew Trick18c57a82010-11-30 23:18:47 +000039
Jakob Stoklund Olesen11983cd2011-04-11 15:00:44 +000040 while (SegPos.valid()) {
Jakob Stoklund Olesen953af2c2010-12-07 23:18:47 +000041 SegPos.insert(RegPos->start, RegPos->end, &VirtReg);
42 if (++RegPos == RegEnd)
43 return;
44 SegPos.advanceTo(RegPos->start);
Andrew Trick14e8d712010-10-22 23:09:15 +000045 }
Jakob Stoklund Olesen11983cd2011-04-11 15:00:44 +000046
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.
50 --RegEnd;
51 SegPos.insert(RegEnd->start, RegEnd->end, &VirtReg);
52 for (; RegPos != RegEnd; ++RegPos, ++SegPos)
53 SegPos.insert(RegPos->start, RegPos->end, &VirtReg);
Andrew Trick14e8d712010-10-22 23:09:15 +000054}
55
Andrew Tricke141a492010-11-08 18:02:08 +000056// Remove a live virtual register's segments from this union.
Jakob Stoklund Olesen953af2c2010-12-07 23:18:47 +000057void LiveIntervalUnion::extract(LiveInterval &VirtReg) {
58 if (VirtReg.empty())
59 return;
Jakob Stoklund Olesen4f6364f2011-02-09 21:52:03 +000060 ++Tag;
Andrew Trick18c57a82010-11-30 23:18:47 +000061
Andrew Tricke141a492010-11-08 18:02:08 +000062 // Remove each of the virtual register's live segments from the map.
Jakob Stoklund Olesen953af2c2010-12-07 23:18:47 +000063 LiveInterval::iterator RegPos = VirtReg.begin();
64 LiveInterval::iterator RegEnd = VirtReg.end();
65 SegmentIter SegPos = Segments.find(RegPos->start);
Andrew Trick18c57a82010-11-30 23:18:47 +000066
Jakob Stoklund Olesen953af2c2010-12-07 23:18:47 +000067 for (;;) {
68 assert(SegPos.value() == &VirtReg && "Inconsistent LiveInterval");
69 SegPos.erase();
70 if (!SegPos.valid())
71 return;
Andrew Trick18c57a82010-11-30 23:18:47 +000072
Jakob Stoklund Olesen953af2c2010-12-07 23:18:47 +000073 // Skip all segments that may have been coalesced.
74 RegPos = VirtReg.advanceTo(RegPos, SegPos.start());
75 if (RegPos == RegEnd)
76 return;
77
78 SegPos.advanceTo(RegPos->start);
Andrew Trick14e8d712010-10-22 23:09:15 +000079 }
Andrew Trick14e8d712010-10-22 23:09:15 +000080}
Andrew Trick14e8d712010-10-22 23:09:15 +000081
Andrew Trick071d1c02010-11-09 21:04:34 +000082void
Jakob Stoklund Olesen4a84cce2010-12-14 18:53:47 +000083LiveIntervalUnion::print(raw_ostream &OS, const TargetRegisterInfo *TRI) const {
Jakob Stoklund Olesenbfce6782010-12-14 19:38:49 +000084 if (empty()) {
85 OS << " empty\n";
86 return;
87 }
Jakob Stoklund Olesen4a84cce2010-12-14 18:53:47 +000088 for (LiveSegments::const_iterator SI = Segments.begin(); SI.valid(); ++SI) {
Jakob Stoklund Olesen43142682011-01-09 03:05:53 +000089 OS << " [" << SI.start() << ' ' << SI.stop() << "):"
90 << PrintReg(SI.value()->reg, TRI);
Andrew Trick071d1c02010-11-09 21:04:34 +000091 }
Jakob Stoklund Olesenbfce6782010-12-14 19:38:49 +000092 OS << '\n';
93}
94
Andrew Trick071d1c02010-11-09 21:04:34 +000095#ifndef NDEBUG
96// Verify the live intervals in this union and add them to the visited set.
Andrew Trick18c57a82010-11-30 23:18:47 +000097void LiveIntervalUnion::verify(LiveVirtRegBitSet& VisitedVRegs) {
Jakob Stoklund Olesen953af2c2010-12-07 23:18:47 +000098 for (SegmentIter SI = Segments.begin(); SI.valid(); ++SI)
99 VisitedVRegs.set(SI.value()->reg);
Andrew Trick071d1c02010-11-09 21:04:34 +0000100}
101#endif //!NDEBUG
102
Andrew Trick18c57a82010-11-30 23:18:47 +0000103// Scan the vector of interfering virtual registers in this union. Assume it's
Andrew Trickf4baeaf2010-11-10 19:18:47 +0000104// quite small.
Andrew Trick18c57a82010-11-30 23:18:47 +0000105bool LiveIntervalUnion::Query::isSeenInterference(LiveInterval *VirtReg) const {
Andrew Trickf4baeaf2010-11-10 19:18:47 +0000106 SmallVectorImpl<LiveInterval*>::const_iterator I =
Andrew Trick18c57a82010-11-30 23:18:47 +0000107 std::find(InterferingVRegs.begin(), InterferingVRegs.end(), VirtReg);
108 return I != InterferingVRegs.end();
Andrew Trickf4baeaf2010-11-10 19:18:47 +0000109}
110
Jakob Stoklund Olesen9b7ff122011-08-12 00:22:04 +0000111// Collect virtual registers in this union that interfere with this
Andrew Trick18c57a82010-11-30 23:18:47 +0000112// query's live virtual register.
113//
Jakob Stoklund Olesen9b7ff122011-08-12 00:22:04 +0000114// The query state is one of:
Andrew Trickf4baeaf2010-11-10 19:18:47 +0000115//
Jakob Stoklund Olesen9b7ff122011-08-12 00:22:04 +0000116// 1. CheckedFirstInterference == false: Iterators are uninitialized.
117// 2. SeenAllInterferences == true: InterferingVRegs complete, iterators unused.
118// 3. Iterators left at the last seen intersection.
119//
Andrew Trickf4baeaf2010-11-10 19:18:47 +0000120unsigned LiveIntervalUnion::Query::
Jakob Stoklund Olesen51458ed2011-07-08 20:46:18 +0000121collectInterferingVRegs(unsigned MaxInterferingRegs) {
Jakob Stoklund Olesenfe026e1822011-08-11 22:46:04 +0000122 // Fast path return if we already have the desired information.
123 if (SeenAllInterferences || InterferingVRegs.size() >= MaxInterferingRegs)
Jakob Stoklund Olesen9942ba92011-08-11 21:18:34 +0000124 return InterferingVRegs.size();
Jakob Stoklund Olesenfe026e1822011-08-11 22:46:04 +0000125
126 // Set up iterators on the first call.
127 if (!CheckedFirstInterference) {
128 CheckedFirstInterference = true;
Jakob Stoklund Olesenfe026e1822011-08-11 22:46:04 +0000129
130 // Quickly skip interference check for empty sets.
131 if (VirtReg->empty() || LiveUnion->empty()) {
Jakob Stoklund Olesen9b7ff122011-08-12 00:22:04 +0000132 SeenAllInterferences = true;
133 return 0;
Jakob Stoklund Olesenfe026e1822011-08-11 22:46:04 +0000134 }
Jakob Stoklund Olesen9b7ff122011-08-12 00:22:04 +0000135
136 // In most cases, the union will start before VirtReg.
137 VirtRegI = VirtReg->begin();
138 LiveUnionI.setMap(LiveUnion->getMap());
139 LiveUnionI.find(VirtRegI->start);
Jakob Stoklund Olesenfe026e1822011-08-11 22:46:04 +0000140 }
141
Andrew Trick18c57a82010-11-30 23:18:47 +0000142 LiveInterval::iterator VirtRegEnd = VirtReg->end();
Jakob Stoklund Olesen9b7ff122011-08-12 00:22:04 +0000143 LiveInterval *RecentReg = 0;
144 while (LiveUnionI.valid()) {
145 assert(VirtRegI != VirtRegEnd && "Reached end of VirtReg");
Andrew Trickf4baeaf2010-11-10 19:18:47 +0000146
Jakob Stoklund Olesen9b7ff122011-08-12 00:22:04 +0000147 // Check for overlapping interference.
148 while (VirtRegI->start < LiveUnionI.stop() &&
149 VirtRegI->end > LiveUnionI.start()) {
150 // This is an overlap, record the interfering register.
151 LiveInterval *VReg = LiveUnionI.value();
152 if (VReg != RecentReg && !isSeenInterference(VReg)) {
153 RecentReg = VReg;
154 InterferingVRegs.push_back(VReg);
155 if (InterferingVRegs.size() >= MaxInterferingRegs)
156 return InterferingVRegs.size();
157 }
158 // This LiveUnion segment is no longer interesting.
159 if (!(++LiveUnionI).valid()) {
160 SeenAllInterferences = true;
161 return InterferingVRegs.size();
162 }
163 }
Andrew Trickf4baeaf2010-11-10 19:18:47 +0000164
Jakob Stoklund Olesen9b7ff122011-08-12 00:22:04 +0000165 // The iterators are now not overlapping, LiveUnionI has been advanced
166 // beyond VirtRegI.
167 assert(VirtRegI->end <= LiveUnionI.start() && "Expected non-overlap");
Andrew Trickf4baeaf2010-11-10 19:18:47 +0000168
Jakob Stoklund Olesen9b7ff122011-08-12 00:22:04 +0000169 // Advance the iterator that ends first.
Jakob Stoklund Olesenfe026e1822011-08-11 22:46:04 +0000170 VirtRegI = VirtReg->advanceTo(VirtRegI, LiveUnionI.start());
171 if (VirtRegI == VirtRegEnd)
Andrew Trickf4baeaf2010-11-10 19:18:47 +0000172 break;
173
Jakob Stoklund Olesen9b7ff122011-08-12 00:22:04 +0000174 // Detect overlap, handle above.
175 if (VirtRegI->start < LiveUnionI.stop())
Andrew Trickf4baeaf2010-11-10 19:18:47 +0000176 continue;
Jakob Stoklund Olesen9b7ff122011-08-12 00:22:04 +0000177
178 // Still not overlapping. Catch up LiveUnionI.
Jakob Stoklund Olesenfe026e1822011-08-11 22:46:04 +0000179 LiveUnionI.advanceTo(VirtRegI->start);
Andrew Trickf4baeaf2010-11-10 19:18:47 +0000180 }
Andrew Trick18c57a82010-11-30 23:18:47 +0000181 SeenAllInterferences = true;
182 return InterferingVRegs.size();
Andrew Trickf4baeaf2010-11-10 19:18:47 +0000183}
Jakob Stoklund Olesenff2e9b42010-12-17 04:09:47 +0000184
185bool LiveIntervalUnion::Query::checkLoopInterference(MachineLoopRange *Loop) {
186 // VirtReg is likely live throughout the loop, so start by checking LIU-Loop
187 // overlaps.
188 IntervalMapOverlaps<LiveIntervalUnion::Map, MachineLoopRange::Map>
189 Overlaps(LiveUnion->getMap(), Loop->getMap());
190 if (!Overlaps.valid())
191 return false;
192
193 // The loop is overlapping an LIU assignment. Check VirtReg as well.
194 LiveInterval::iterator VRI = VirtReg->find(Overlaps.start());
195
196 for (;;) {
197 if (VRI == VirtReg->end())
198 return false;
199 if (VRI->start < Overlaps.stop())
200 return true;
201
202 Overlaps.advanceTo(VRI->start);
203 if (!Overlaps.valid())
204 return false;
205 if (Overlaps.start() < VRI->end)
206 return true;
207
208 VRI = VirtReg->advanceTo(VRI, Overlaps.start());
209 }
210}
Jakob Stoklund Olesen0e5a60b2012-06-05 23:57:30 +0000211
212void LiveIntervalUnion::Array::init(LiveIntervalUnion::Allocator &Alloc,
213 unsigned NSize) {
214 // Reuse existing allocation.
215 if (NSize == Size)
216 return;
217 clear();
218 Size = NSize;
219 LIUs = static_cast<LiveIntervalUnion*>(
220 malloc(sizeof(LiveIntervalUnion)*NSize));
221 for (unsigned i = 0; i != Size; ++i)
222 new(LIUs + i) LiveIntervalUnion(Alloc);
223}
224
225void LiveIntervalUnion::Array::clear() {
226 if (!LIUs)
227 return;
228 for (unsigned i = 0; i != Size; ++i)
229 LIUs[i].~LiveIntervalUnion();
230 free(LIUs);
231 Size = 0;
232 LIUs = 0;
233}