2010-10-22 23:09:15 +00:00
|
|
|
//===-- LiveIntervalUnion.cpp - Live interval union data structure --------===//
|
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// LiveIntervalUnion represents a coalesced set of live intervals. This may be
|
|
|
|
// used during coalescing to represent a congruence class, or during register
|
|
|
|
// allocation to model liveness of a physical register.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#define DEBUG_TYPE "regalloc"
|
|
|
|
#include "LiveIntervalUnion.h"
|
2010-11-09 21:04:34 +00:00
|
|
|
#include "llvm/ADT/SparseBitVector.h"
|
2010-10-22 23:09:15 +00:00
|
|
|
#include "llvm/Support/Debug.h"
|
|
|
|
#include "llvm/Support/raw_ostream.h"
|
|
|
|
#include <algorithm>
|
|
|
|
using namespace llvm;
|
|
|
|
|
2010-11-08 18:02:08 +00:00
|
|
|
|
2010-10-22 23:09:15 +00:00
|
|
|
// Merge a LiveInterval's segments. Guarantee no overlaps.
|
2010-11-30 23:18:47 +00:00
|
|
|
void LiveIntervalUnion::unify(LiveInterval &VirtReg) {
|
2010-12-07 23:18:47 +00:00
|
|
|
if (VirtReg.empty())
|
|
|
|
return;
|
2010-11-30 23:18:47 +00:00
|
|
|
|
|
|
|
// Insert each of the virtual register's live segments into the map.
|
2010-12-07 23:18:47 +00:00
|
|
|
LiveInterval::iterator RegPos = VirtReg.begin();
|
|
|
|
LiveInterval::iterator RegEnd = VirtReg.end();
|
|
|
|
SegmentIter SegPos = Segments.find(RegPos->start);
|
|
|
|
|
|
|
|
for (;;) {
|
|
|
|
SegPos.insert(RegPos->start, RegPos->end, &VirtReg);
|
|
|
|
if (++RegPos == RegEnd)
|
|
|
|
return;
|
|
|
|
SegPos.advanceTo(RegPos->start);
|
2010-10-22 23:09:15 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-11-08 18:02:08 +00:00
|
|
|
// Remove a live virtual register's segments from this union.
|
2010-12-07 23:18:47 +00:00
|
|
|
void LiveIntervalUnion::extract(LiveInterval &VirtReg) {
|
|
|
|
if (VirtReg.empty())
|
|
|
|
return;
|
2010-11-30 23:18:47 +00:00
|
|
|
|
2010-11-08 18:02:08 +00:00
|
|
|
// Remove each of the virtual register's live segments from the map.
|
2010-12-07 23:18:47 +00:00
|
|
|
LiveInterval::iterator RegPos = VirtReg.begin();
|
|
|
|
LiveInterval::iterator RegEnd = VirtReg.end();
|
|
|
|
SegmentIter SegPos = Segments.find(RegPos->start);
|
|
|
|
|
|
|
|
for (;;) {
|
|
|
|
assert(SegPos.value() == &VirtReg && "Inconsistent LiveInterval");
|
|
|
|
SegPos.erase();
|
|
|
|
if (!SegPos.valid())
|
|
|
|
return;
|
|
|
|
|
|
|
|
// Skip all segments that may have been coalesced.
|
|
|
|
RegPos = VirtReg.advanceTo(RegPos, SegPos.start());
|
|
|
|
if (RegPos == RegEnd)
|
|
|
|
return;
|
|
|
|
|
|
|
|
SegPos.advanceTo(RegPos->start);
|
2010-10-22 23:09:15 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-11-09 21:04:34 +00:00
|
|
|
void
|
2010-11-30 23:18:47 +00:00
|
|
|
LiveIntervalUnion::print(raw_ostream &OS,
|
|
|
|
const AbstractRegisterDescription *RegDesc) const {
|
|
|
|
OS << "LIU ";
|
|
|
|
if (RegDesc != NULL)
|
|
|
|
OS << RegDesc->getName(RepReg);
|
2010-11-09 21:04:34 +00:00
|
|
|
else {
|
2010-11-30 23:18:47 +00:00
|
|
|
OS << RepReg;
|
2010-11-09 21:04:34 +00:00
|
|
|
}
|
2010-12-07 23:18:47 +00:00
|
|
|
for (LiveSegments::const_iterator SI = Segments.begin(); SI.valid(); ++SI)
|
|
|
|
dbgs() << " [" << SI.start() << ' ' << SI.stop() << "):%reg"
|
|
|
|
<< SI.value()->reg;
|
2010-11-30 23:18:47 +00:00
|
|
|
OS << "\n";
|
2010-11-09 21:04:34 +00:00
|
|
|
}
|
|
|
|
|
2010-11-30 23:18:47 +00:00
|
|
|
void LiveIntervalUnion::dump(const AbstractRegisterDescription *RegDesc) const {
|
|
|
|
print(dbgs(), RegDesc);
|
2010-11-09 21:04:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#ifndef NDEBUG
|
|
|
|
// Verify the live intervals in this union and add them to the visited set.
|
2010-11-30 23:18:47 +00:00
|
|
|
void LiveIntervalUnion::verify(LiveVirtRegBitSet& VisitedVRegs) {
|
2010-12-07 23:18:47 +00:00
|
|
|
for (SegmentIter SI = Segments.begin(); SI.valid(); ++SI)
|
|
|
|
VisitedVRegs.set(SI.value()->reg);
|
2010-11-09 21:04:34 +00:00
|
|
|
}
|
|
|
|
#endif //!NDEBUG
|
|
|
|
|
2010-10-22 23:09:15 +00:00
|
|
|
// Private interface accessed by Query.
|
|
|
|
//
|
|
|
|
// Find a pair of segments that intersect, one in the live virtual register
|
|
|
|
// (LiveInterval), and the other in this LiveIntervalUnion. The caller (Query)
|
|
|
|
// is responsible for advancing the LiveIntervalUnion segments to find a
|
|
|
|
// "notable" intersection, which requires query-specific logic.
|
2010-11-30 23:18:47 +00:00
|
|
|
//
|
2010-10-22 23:09:15 +00:00
|
|
|
// This design assumes only a fast mechanism for intersecting a single live
|
|
|
|
// virtual register segment with a set of LiveIntervalUnion segments. This may
|
2010-11-30 23:59:50 +00:00
|
|
|
// be ok since most virtual registers have very few segments. If we had a data
|
2010-10-22 23:09:15 +00:00
|
|
|
// structure that optimizd MxN intersection of segments, then we would bypass
|
|
|
|
// the loop that advances within the LiveInterval.
|
|
|
|
//
|
2010-11-30 23:18:47 +00:00
|
|
|
// If no intersection exists, set VirtRegI = VirtRegEnd, and set SI to the first
|
2010-10-22 23:09:15 +00:00
|
|
|
// segment whose start point is greater than LiveInterval's end point.
|
|
|
|
//
|
|
|
|
// Assumes that segments are sorted by start position in both
|
|
|
|
// LiveInterval and LiveSegments.
|
2010-11-30 23:18:47 +00:00
|
|
|
void LiveIntervalUnion::Query::findIntersection(InterferenceResult &IR) const {
|
|
|
|
// Search until reaching the end of the LiveUnion segments.
|
|
|
|
LiveInterval::iterator VirtRegEnd = VirtReg->end();
|
2010-12-08 23:51:35 +00:00
|
|
|
if (IR.VirtRegI == VirtRegEnd)
|
|
|
|
return;
|
2010-12-07 23:18:47 +00:00
|
|
|
while (IR.LiveUnionI.valid()) {
|
2010-10-22 23:09:15 +00:00
|
|
|
// Slowly advance the live virtual reg iterator until we surpass the next
|
2010-11-30 23:18:47 +00:00
|
|
|
// segment in LiveUnion.
|
|
|
|
//
|
|
|
|
// Note: If this is ever used for coalescing of fixed registers and we have
|
|
|
|
// a live vreg with thousands of segments, then change this code to use
|
|
|
|
// upperBound instead.
|
2010-12-07 23:18:47 +00:00
|
|
|
IR.VirtRegI = VirtReg->advanceTo(IR.VirtRegI, IR.LiveUnionI.start());
|
2010-11-30 23:18:47 +00:00
|
|
|
if (IR.VirtRegI == VirtRegEnd)
|
|
|
|
break; // Retain current (nonoverlapping) LiveUnionI
|
|
|
|
|
2010-12-07 23:18:47 +00:00
|
|
|
// VirtRegI may have advanced far beyond LiveUnionI, catch up.
|
|
|
|
IR.LiveUnionI.advanceTo(IR.VirtRegI->start);
|
2010-11-30 23:18:47 +00:00
|
|
|
|
|
|
|
// Check if no LiveUnionI exists with VirtRegI->Start < LiveUnionI.end
|
2010-12-07 23:18:47 +00:00
|
|
|
if (!IR.LiveUnionI.valid())
|
2010-10-22 23:09:15 +00:00
|
|
|
break;
|
2010-12-07 23:18:47 +00:00
|
|
|
if (IR.LiveUnionI.start() < IR.VirtRegI->end) {
|
|
|
|
assert(overlap(*IR.VirtRegI, IR.LiveUnionI) &&
|
2010-11-30 23:18:47 +00:00
|
|
|
"upperBound postcondition");
|
2010-10-22 23:09:15 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2010-12-07 23:18:47 +00:00
|
|
|
if (!IR.LiveUnionI.valid())
|
2010-11-30 23:18:47 +00:00
|
|
|
IR.VirtRegI = VirtRegEnd;
|
2010-10-22 23:09:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Find the first intersection, and cache interference info
|
2010-11-30 23:18:47 +00:00
|
|
|
// (retain segment iterators into both VirtReg and LiveUnion).
|
2010-12-09 01:06:52 +00:00
|
|
|
const LiveIntervalUnion::InterferenceResult &
|
2010-10-22 23:09:15 +00:00
|
|
|
LiveIntervalUnion::Query::firstInterference() {
|
2010-12-09 01:06:52 +00:00
|
|
|
if (CheckedFirstInterference)
|
2010-11-30 23:18:47 +00:00
|
|
|
return FirstInterference;
|
2010-12-09 01:06:52 +00:00
|
|
|
CheckedFirstInterference = true;
|
|
|
|
InterferenceResult &IR = FirstInterference;
|
|
|
|
|
|
|
|
// Quickly skip interference check for empty sets.
|
|
|
|
if (VirtReg->empty() || LiveUnion->empty()) {
|
|
|
|
IR.VirtRegI = VirtReg->end();
|
|
|
|
} else if (VirtReg->beginIndex() < LiveUnion->startIndex()) {
|
|
|
|
// VirtReg starts first, perform double binary search.
|
|
|
|
IR.VirtRegI = VirtReg->find(LiveUnion->startIndex());
|
|
|
|
if (IR.VirtRegI != VirtReg->end())
|
|
|
|
IR.LiveUnionI = LiveUnion->find(IR.VirtRegI->start);
|
|
|
|
} else {
|
|
|
|
// LiveUnion starts first, perform double binary search.
|
|
|
|
IR.LiveUnionI = LiveUnion->find(VirtReg->beginIndex());
|
|
|
|
if (IR.LiveUnionI.valid())
|
|
|
|
IR.VirtRegI = VirtReg->find(IR.LiveUnionI.start());
|
|
|
|
else
|
|
|
|
IR.VirtRegI = VirtReg->end();
|
2010-10-22 23:09:15 +00:00
|
|
|
}
|
2010-11-30 23:18:47 +00:00
|
|
|
findIntersection(FirstInterference);
|
|
|
|
return FirstInterference;
|
2010-10-22 23:09:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Treat the result as an iterator and advance to the next interfering pair
|
|
|
|
// of segments. This is a plain iterator with no filter.
|
2010-11-30 23:18:47 +00:00
|
|
|
bool LiveIntervalUnion::Query::nextInterference(InterferenceResult &IR) const {
|
|
|
|
assert(isInterference(IR) && "iteration past end of interferences");
|
|
|
|
|
|
|
|
// Advance either the VirtReg or LiveUnion segment to ensure that we visit all
|
|
|
|
// unique overlapping pairs.
|
2010-12-07 23:18:47 +00:00
|
|
|
if (IR.VirtRegI->end < IR.LiveUnionI.stop()) {
|
2010-11-30 23:18:47 +00:00
|
|
|
if (++IR.VirtRegI == VirtReg->end())
|
2010-10-22 23:09:15 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
else {
|
2010-12-07 23:18:47 +00:00
|
|
|
if (!(++IR.LiveUnionI).valid()) {
|
2010-11-30 23:18:47 +00:00
|
|
|
IR.VirtRegI = VirtReg->end();
|
2010-10-22 23:09:15 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
2010-11-30 23:18:47 +00:00
|
|
|
// Short-circuit findIntersection() if possible.
|
2010-12-07 23:18:47 +00:00
|
|
|
if (overlap(*IR.VirtRegI, IR.LiveUnionI))
|
2010-10-22 23:09:15 +00:00
|
|
|
return true;
|
2010-11-30 23:18:47 +00:00
|
|
|
|
|
|
|
// Find the next intersection.
|
|
|
|
findIntersection(IR);
|
|
|
|
return isInterference(IR);
|
2010-10-22 23:09:15 +00:00
|
|
|
}
|
2010-11-10 19:18:47 +00:00
|
|
|
|
2010-11-30 23:18:47 +00:00
|
|
|
// Scan the vector of interfering virtual registers in this union. Assume it's
|
2010-11-10 19:18:47 +00:00
|
|
|
// quite small.
|
2010-11-30 23:18:47 +00:00
|
|
|
bool LiveIntervalUnion::Query::isSeenInterference(LiveInterval *VirtReg) const {
|
2010-11-10 19:18:47 +00:00
|
|
|
SmallVectorImpl<LiveInterval*>::const_iterator I =
|
2010-11-30 23:18:47 +00:00
|
|
|
std::find(InterferingVRegs.begin(), InterferingVRegs.end(), VirtReg);
|
|
|
|
return I != InterferingVRegs.end();
|
2010-11-10 19:18:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Count the number of virtual registers in this union that interfere with this
|
2010-11-30 23:18:47 +00:00
|
|
|
// query's live virtual register.
|
|
|
|
//
|
|
|
|
// The number of times that we either advance IR.VirtRegI or call
|
|
|
|
// LiveUnion.upperBound() will be no more than the number of holes in
|
|
|
|
// VirtReg. So each invocation of collectInterferingVRegs() takes
|
|
|
|
// time proportional to |VirtReg Holes| * time(LiveUnion.upperBound()).
|
2010-11-10 19:18:47 +00:00
|
|
|
//
|
|
|
|
// For comments on how to speed it up, see Query::findIntersection().
|
|
|
|
unsigned LiveIntervalUnion::Query::
|
2010-11-30 23:18:47 +00:00
|
|
|
collectInterferingVRegs(unsigned MaxInterferingRegs) {
|
|
|
|
InterferenceResult IR = firstInterference();
|
|
|
|
LiveInterval::iterator VirtRegEnd = VirtReg->end();
|
|
|
|
LiveInterval *RecentInterferingVReg = NULL;
|
2010-12-07 23:18:47 +00:00
|
|
|
while (IR.LiveUnionI.valid()) {
|
2010-11-10 19:18:47 +00:00
|
|
|
// Advance the union's iterator to reach an unseen interfering vreg.
|
|
|
|
do {
|
2010-12-07 23:18:47 +00:00
|
|
|
if (IR.LiveUnionI.value() == RecentInterferingVReg)
|
2010-11-10 19:18:47 +00:00
|
|
|
continue;
|
|
|
|
|
2010-12-07 23:18:47 +00:00
|
|
|
if (!isSeenInterference(IR.LiveUnionI.value()))
|
2010-11-10 19:18:47 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
// Cache the most recent interfering vreg to bypass isSeenInterference.
|
2010-12-07 23:18:47 +00:00
|
|
|
RecentInterferingVReg = IR.LiveUnionI.value();
|
2010-11-10 19:18:47 +00:00
|
|
|
|
2010-12-07 23:18:47 +00:00
|
|
|
} while ((++IR.LiveUnionI).valid());
|
|
|
|
if (!IR.LiveUnionI.valid())
|
2010-11-10 19:18:47 +00:00
|
|
|
break;
|
|
|
|
|
2010-11-30 23:18:47 +00:00
|
|
|
// Advance the VirtReg iterator until surpassing the next segment in
|
|
|
|
// LiveUnion.
|
2010-12-07 23:18:47 +00:00
|
|
|
IR.VirtRegI = VirtReg->advanceTo(IR.VirtRegI, IR.LiveUnionI.start());
|
2010-11-30 23:18:47 +00:00
|
|
|
if (IR.VirtRegI == VirtRegEnd)
|
2010-11-10 19:18:47 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
// Check for intersection with the union's segment.
|
2010-12-07 23:18:47 +00:00
|
|
|
if (overlap(*IR.VirtRegI, IR.LiveUnionI)) {
|
2010-11-30 23:18:47 +00:00
|
|
|
|
2010-12-07 23:18:47 +00:00
|
|
|
if (!IR.LiveUnionI.value()->isSpillable())
|
2010-11-30 23:18:47 +00:00
|
|
|
SeenUnspillableVReg = true;
|
|
|
|
|
2010-12-07 23:18:47 +00:00
|
|
|
InterferingVRegs.push_back(IR.LiveUnionI.value());
|
2010-11-30 23:18:47 +00:00
|
|
|
if (InterferingVRegs.size() == MaxInterferingRegs)
|
|
|
|
return MaxInterferingRegs;
|
2010-11-10 19:18:47 +00:00
|
|
|
|
|
|
|
// Cache the most recent interfering vreg to bypass isSeenInterference.
|
2010-12-07 23:18:47 +00:00
|
|
|
RecentInterferingVReg = IR.LiveUnionI.value();
|
2010-11-30 23:18:47 +00:00
|
|
|
++IR.LiveUnionI;
|
2010-11-10 19:18:47 +00:00
|
|
|
continue;
|
|
|
|
}
|
2010-11-30 23:18:47 +00:00
|
|
|
// VirtRegI may have advanced far beyond LiveUnionI,
|
2010-11-10 19:18:47 +00:00
|
|
|
// do a fast intersection test to "catch up"
|
2010-12-07 23:18:47 +00:00
|
|
|
IR.LiveUnionI.advanceTo(IR.VirtRegI->start);
|
2010-11-10 19:18:47 +00:00
|
|
|
}
|
2010-11-30 23:18:47 +00:00
|
|
|
SeenAllInterferences = true;
|
|
|
|
return InterferingVRegs.size();
|
2010-11-10 19:18:47 +00:00
|
|
|
}
|