Remove some dead code.
[oota-llvm.git] / lib / CodeGen / LiveIntervalUnion.cpp
1 //===-- 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"
18 #include "llvm/ADT/SparseBitVector.h"
19 #include "llvm/CodeGen/MachineLoopRanges.h"
20 #include "llvm/Support/Debug.h"
21 #include "llvm/Support/raw_ostream.h"
22 #include "llvm/Target/TargetRegisterInfo.h"
23
24 using namespace llvm;
25
26
27 // Merge a LiveInterval's segments. Guarantee no overlaps.
28 void LiveIntervalUnion::unify(LiveInterval &VirtReg) {
29   if (VirtReg.empty())
30     return;
31   ++Tag;
32
33   // Insert each of the virtual register's live segments into the map.
34   LiveInterval::iterator RegPos = VirtReg.begin();
35   LiveInterval::iterator RegEnd = VirtReg.end();
36   SegmentIter SegPos = Segments.find(RegPos->start);
37
38   while (SegPos.valid()) {
39     SegPos.insert(RegPos->start, RegPos->end, &VirtReg);
40     if (++RegPos == RegEnd)
41       return;
42     SegPos.advanceTo(RegPos->start);
43   }
44
45   // We have reached the end of Segments, so it is no longer necessary to search
46   // for the insertion position.
47   // It is faster to insert the end first.
48   --RegEnd;
49   SegPos.insert(RegEnd->start, RegEnd->end, &VirtReg);
50   for (; RegPos != RegEnd; ++RegPos, ++SegPos)
51     SegPos.insert(RegPos->start, RegPos->end, &VirtReg);
52 }
53
54 // Remove a live virtual register's segments from this union.
55 void LiveIntervalUnion::extract(LiveInterval &VirtReg) {
56   if (VirtReg.empty())
57     return;
58   ++Tag;
59
60   // Remove each of the virtual register's live segments from the map.
61   LiveInterval::iterator RegPos = VirtReg.begin();
62   LiveInterval::iterator RegEnd = VirtReg.end();
63   SegmentIter SegPos = Segments.find(RegPos->start);
64
65   for (;;) {
66     assert(SegPos.value() == &VirtReg && "Inconsistent LiveInterval");
67     SegPos.erase();
68     if (!SegPos.valid())
69       return;
70
71     // Skip all segments that may have been coalesced.
72     RegPos = VirtReg.advanceTo(RegPos, SegPos.start());
73     if (RegPos == RegEnd)
74       return;
75
76     SegPos.advanceTo(RegPos->start);
77   }
78 }
79
80 void
81 LiveIntervalUnion::print(raw_ostream &OS, const TargetRegisterInfo *TRI) const {
82   OS << "LIU " << PrintReg(RepReg, TRI);
83   if (empty()) {
84     OS << " empty\n";
85     return;
86   }
87   for (LiveSegments::const_iterator SI = Segments.begin(); SI.valid(); ++SI) {
88     OS << " [" << SI.start() << ' ' << SI.stop() << "):"
89        << PrintReg(SI.value()->reg, TRI);
90   }
91   OS << '\n';
92 }
93
94 #ifndef NDEBUG
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);
99 }
100 #endif //!NDEBUG
101
102 // Private interface accessed by Query.
103 //
104 // Find a pair of segments that intersect, one in the live virtual register
105 // (LiveInterval), and the other in this LiveIntervalUnion. The caller (Query)
106 // is responsible for advancing the LiveIntervalUnion segments to find a
107 // "notable" intersection, which requires query-specific logic.
108 //
109 // This design assumes only a fast mechanism for intersecting a single live
110 // virtual register segment with a set of LiveIntervalUnion segments.  This may
111 // be ok since most virtual registers have very few segments.  If we had a data
112 // structure that optimizd MxN intersection of segments, then we would bypass
113 // the loop that advances within the LiveInterval.
114 //
115 // If no intersection exists, set VirtRegI = VirtRegEnd, and set SI to the first
116 // segment whose start point is greater than LiveInterval's end point.
117 //
118 // Assumes that segments are sorted by start position in both
119 // LiveInterval and LiveSegments.
120 void LiveIntervalUnion::Query::findIntersection(InterferenceResult &IR) const {
121   // Search until reaching the end of the LiveUnion segments.
122   LiveInterval::iterator VirtRegEnd = VirtReg->end();
123   if (IR.VirtRegI == VirtRegEnd)
124     return;
125   while (IR.LiveUnionI.valid()) {
126     // Slowly advance the live virtual reg iterator until we surpass the next
127     // segment in LiveUnion.
128     //
129     // Note: If this is ever used for coalescing of fixed registers and we have
130     // a live vreg with thousands of segments, then change this code to use
131     // upperBound instead.
132     IR.VirtRegI = VirtReg->advanceTo(IR.VirtRegI, IR.LiveUnionI.start());
133     if (IR.VirtRegI == VirtRegEnd)
134       break; // Retain current (nonoverlapping) LiveUnionI
135
136     // VirtRegI may have advanced far beyond LiveUnionI, catch up.
137     IR.LiveUnionI.advanceTo(IR.VirtRegI->start);
138
139     // Check if no LiveUnionI exists with VirtRegI->Start < LiveUnionI.end
140     if (!IR.LiveUnionI.valid())
141       break;
142     if (IR.LiveUnionI.start() < IR.VirtRegI->end) {
143       assert(overlap(*IR.VirtRegI, IR.LiveUnionI) &&
144              "upperBound postcondition");
145       break;
146     }
147   }
148   if (!IR.LiveUnionI.valid())
149     IR.VirtRegI = VirtRegEnd;
150 }
151
152 // Find the first intersection, and cache interference info
153 // (retain segment iterators into both VirtReg and LiveUnion).
154 const LiveIntervalUnion::InterferenceResult &
155 LiveIntervalUnion::Query::firstInterference() {
156   if (CheckedFirstInterference)
157     return FirstInterference;
158   CheckedFirstInterference = true;
159   InterferenceResult &IR = FirstInterference;
160   IR.LiveUnionI.setMap(LiveUnion->getMap());
161
162   // Quickly skip interference check for empty sets.
163   if (VirtReg->empty() || LiveUnion->empty()) {
164     IR.VirtRegI = VirtReg->end();
165   } else if (VirtReg->beginIndex() < LiveUnion->startIndex()) {
166     // VirtReg starts first, perform double binary search.
167     IR.VirtRegI = VirtReg->find(LiveUnion->startIndex());
168     if (IR.VirtRegI != VirtReg->end())
169       IR.LiveUnionI.find(IR.VirtRegI->start);
170   } else {
171     // LiveUnion starts first, perform double binary search.
172     IR.LiveUnionI.find(VirtReg->beginIndex());
173     if (IR.LiveUnionI.valid())
174       IR.VirtRegI = VirtReg->find(IR.LiveUnionI.start());
175     else
176       IR.VirtRegI = VirtReg->end();
177   }
178   findIntersection(FirstInterference);
179   assert((IR.VirtRegI == VirtReg->end() || IR.LiveUnionI.valid())
180          && "Uninitialized iterator");
181   return FirstInterference;
182 }
183
184 // Treat the result as an iterator and advance to the next interfering pair
185 // of segments. This is a plain iterator with no filter.
186 bool LiveIntervalUnion::Query::nextInterference(InterferenceResult &IR) const {
187   assert(isInterference(IR) && "iteration past end of interferences");
188
189   // Advance either the VirtReg or LiveUnion segment to ensure that we visit all
190   // unique overlapping pairs.
191   if (IR.VirtRegI->end < IR.LiveUnionI.stop()) {
192     if (++IR.VirtRegI == VirtReg->end())
193       return false;
194   }
195   else {
196     if (!(++IR.LiveUnionI).valid()) {
197       IR.VirtRegI = VirtReg->end();
198       return false;
199     }
200   }
201   // Short-circuit findIntersection() if possible.
202   if (overlap(*IR.VirtRegI, IR.LiveUnionI))
203     return true;
204
205   // Find the next intersection.
206   findIntersection(IR);
207   return isInterference(IR);
208 }
209
210 // Scan the vector of interfering virtual registers in this union. Assume it's
211 // quite small.
212 bool LiveIntervalUnion::Query::isSeenInterference(LiveInterval *VirtReg) const {
213   SmallVectorImpl<LiveInterval*>::const_iterator I =
214     std::find(InterferingVRegs.begin(), InterferingVRegs.end(), VirtReg);
215   return I != InterferingVRegs.end();
216 }
217
218 // Count the number of virtual registers in this union that interfere with this
219 // query's live virtual register.
220 //
221 // The number of times that we either advance IR.VirtRegI or call
222 // LiveUnion.upperBound() will be no more than the number of holes in
223 // VirtReg. So each invocation of collectInterferingVRegs() takes
224 // time proportional to |VirtReg Holes| * time(LiveUnion.upperBound()).
225 //
226 // For comments on how to speed it up, see Query::findIntersection().
227 unsigned LiveIntervalUnion::Query::
228 collectInterferingVRegs(unsigned MaxInterferingRegs) {
229   InterferenceResult IR = firstInterference();
230   LiveInterval::iterator VirtRegEnd = VirtReg->end();
231   LiveInterval *RecentInterferingVReg = NULL;
232   if (IR.VirtRegI != VirtRegEnd) while (IR.LiveUnionI.valid()) {
233     // Advance the union's iterator to reach an unseen interfering vreg.
234     do {
235       if (IR.LiveUnionI.value() == RecentInterferingVReg)
236         continue;
237
238       if (!isSeenInterference(IR.LiveUnionI.value()))
239         break;
240
241       // Cache the most recent interfering vreg to bypass isSeenInterference.
242       RecentInterferingVReg = IR.LiveUnionI.value();
243
244     } while ((++IR.LiveUnionI).valid());
245     if (!IR.LiveUnionI.valid())
246       break;
247
248     // Advance the VirtReg iterator until surpassing the next segment in
249     // LiveUnion.
250     IR.VirtRegI = VirtReg->advanceTo(IR.VirtRegI, IR.LiveUnionI.start());
251     if (IR.VirtRegI == VirtRegEnd)
252       break;
253
254     // Check for intersection with the union's segment.
255     if (overlap(*IR.VirtRegI, IR.LiveUnionI)) {
256
257       if (!IR.LiveUnionI.value()->isSpillable())
258         SeenUnspillableVReg = true;
259
260       if (InterferingVRegs.size() == MaxInterferingRegs)
261         // Leave SeenAllInterferences set to false to indicate that at least one
262         // interference exists beyond those we collected.
263         return MaxInterferingRegs;
264
265       InterferingVRegs.push_back(IR.LiveUnionI.value());
266
267       // Cache the most recent interfering vreg to bypass isSeenInterference.
268       RecentInterferingVReg = IR.LiveUnionI.value();
269       ++IR.LiveUnionI;
270
271       continue;
272     }
273     // VirtRegI may have advanced far beyond LiveUnionI,
274     // do a fast intersection test to "catch up"
275     IR.LiveUnionI.advanceTo(IR.VirtRegI->start);
276   }
277   SeenAllInterferences = true;
278   return InterferingVRegs.size();
279 }
280
281 bool LiveIntervalUnion::Query::checkLoopInterference(MachineLoopRange *Loop) {
282   // VirtReg is likely live throughout the loop, so start by checking LIU-Loop
283   // overlaps.
284   IntervalMapOverlaps<LiveIntervalUnion::Map, MachineLoopRange::Map>
285     Overlaps(LiveUnion->getMap(), Loop->getMap());
286   if (!Overlaps.valid())
287     return false;
288
289   // The loop is overlapping an LIU assignment. Check VirtReg as well.
290   LiveInterval::iterator VRI = VirtReg->find(Overlaps.start());
291
292   for (;;) {
293     if (VRI == VirtReg->end())
294       return false;
295     if (VRI->start < Overlaps.stop())
296       return true;
297
298     Overlaps.advanceTo(VRI->start);
299     if (!Overlaps.valid())
300       return false;
301     if (Overlaps.start() < VRI->end)
302       return true;
303
304     VRI = VirtReg->advanceTo(VRI, Overlaps.start());
305   }
306 }