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-12-17 04:09:47 +00:00
|
|
|
#include "llvm/CodeGen/MachineLoopRanges.h"
|
2010-10-22 23:09:15 +00:00
|
|
|
#include "llvm/Support/Debug.h"
|
|
|
|
#include "llvm/Support/raw_ostream.h"
|
2010-12-14 18:53:47 +00:00
|
|
|
#include "llvm/Target/TargetRegisterInfo.h"
|
|
|
|
|
2010-10-22 23:09:15 +00:00
|
|
|
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;
|
2011-02-09 21:52:03 +00:00
|
|
|
++Tag;
|
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);
|
|
|
|
|
2011-04-11 15:00:44 +00:00
|
|
|
while (SegPos.valid()) {
|
2010-12-07 23:18:47 +00:00
|
|
|
SegPos.insert(RegPos->start, RegPos->end, &VirtReg);
|
|
|
|
if (++RegPos == RegEnd)
|
|
|
|
return;
|
|
|
|
SegPos.advanceTo(RegPos->start);
|
2010-10-22 23:09:15 +00:00
|
|
|
}
|
2011-04-11 15:00:44 +00:00
|
|
|
|
|
|
|
// We have reached the end of Segments, so it is no longer necessary to search
|
|
|
|
// for the insertion position.
|
|
|
|
// It is faster to insert the end first.
|
|
|
|
--RegEnd;
|
|
|
|
SegPos.insert(RegEnd->start, RegEnd->end, &VirtReg);
|
|
|
|
for (; RegPos != RegEnd; ++RegPos, ++SegPos)
|
|
|
|
SegPos.insert(RegPos->start, RegPos->end, &VirtReg);
|
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;
|
2011-02-09 21:52:03 +00:00
|
|
|
++Tag;
|
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-12-14 18:53:47 +00:00
|
|
|
LiveIntervalUnion::print(raw_ostream &OS, const TargetRegisterInfo *TRI) const {
|
2011-01-09 03:05:53 +00:00
|
|
|
OS << "LIU " << PrintReg(RepReg, TRI);
|
2010-12-14 19:38:49 +00:00
|
|
|
if (empty()) {
|
|
|
|
OS << " empty\n";
|
|
|
|
return;
|
|
|
|
}
|
2010-12-14 18:53:47 +00:00
|
|
|
for (LiveSegments::const_iterator SI = Segments.begin(); SI.valid(); ++SI) {
|
2011-01-09 03:05:53 +00:00
|
|
|
OS << " [" << SI.start() << ' ' << SI.stop() << "):"
|
|
|
|
<< PrintReg(SI.value()->reg, TRI);
|
2010-11-09 21:04:34 +00:00
|
|
|
}
|
2010-12-14 19:38:49 +00:00
|
|
|
OS << '\n';
|
|
|
|
}
|
|
|
|
|
|
|
|
void LiveIntervalUnion::InterferenceResult::print(raw_ostream &OS,
|
|
|
|
const TargetRegisterInfo *TRI) const {
|
2011-01-09 03:05:53 +00:00
|
|
|
OS << '[' << start() << ';' << stop() << "):"
|
|
|
|
<< PrintReg(interference()->reg, TRI);
|
2010-12-14 19:38:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void LiveIntervalUnion::Query::print(raw_ostream &OS,
|
|
|
|
const TargetRegisterInfo *TRI) {
|
|
|
|
OS << "Interferences with ";
|
|
|
|
LiveUnion->print(OS, TRI);
|
|
|
|
InterferenceResult IR = firstInterference();
|
|
|
|
while (isInterference(IR)) {
|
|
|
|
OS << " ";
|
|
|
|
IR.print(OS, TRI);
|
|
|
|
OS << '\n';
|
|
|
|
nextInterference(IR);
|
|
|
|
}
|
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;
|
2011-04-11 21:47:01 +00:00
|
|
|
IR.LiveUnionI.setMap(LiveUnion->getMap());
|
2010-12-09 01:06:52 +00:00
|
|
|
|
|
|
|
// 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())
|
2011-04-11 21:47:01 +00:00
|
|
|
IR.LiveUnionI.find(IR.VirtRegI->start);
|
2010-12-09 01:06:52 +00:00
|
|
|
} else {
|
|
|
|
// LiveUnion starts first, perform double binary search.
|
2011-04-11 21:47:01 +00:00
|
|
|
IR.LiveUnionI.find(VirtReg->beginIndex());
|
2010-12-09 01:06:52 +00:00
|
|
|
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);
|
2010-12-09 21:20:44 +00:00
|
|
|
assert((IR.VirtRegI == VirtReg->end() || IR.LiveUnionI.valid())
|
|
|
|
&& "Uninitialized iterator");
|
2010-11-30 23:18:47 +00:00
|
|
|
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::
|
2011-04-11 21:47:01 +00:00
|
|
|
collectInterferingVRegs(unsigned MaxInterferingRegs, float MaxWeight) {
|
2010-11-30 23:18:47 +00:00
|
|
|
InterferenceResult IR = firstInterference();
|
|
|
|
LiveInterval::iterator VirtRegEnd = VirtReg->end();
|
|
|
|
LiveInterval *RecentInterferingVReg = NULL;
|
2010-12-17 23:16:38 +00:00
|
|
|
if (IR.VirtRegI != VirtRegEnd) 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;
|
|
|
|
|
|
|
|
if (InterferingVRegs.size() == MaxInterferingRegs)
|
2010-12-09 18:15:21 +00:00
|
|
|
// Leave SeenAllInterferences set to false to indicate that at least one
|
|
|
|
// interference exists beyond those we collected.
|
2010-11-30 23:18:47 +00:00
|
|
|
return MaxInterferingRegs;
|
2010-11-10 19:18:47 +00:00
|
|
|
|
2010-12-09 18:15:21 +00:00
|
|
|
InterferingVRegs.push_back(IR.LiveUnionI.value());
|
|
|
|
|
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;
|
2011-04-11 21:47:01 +00:00
|
|
|
|
|
|
|
// Stop collecting when the max weight is exceeded.
|
|
|
|
if (RecentInterferingVReg->weight >= MaxWeight)
|
|
|
|
return InterferingVRegs.size();
|
|
|
|
|
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
|
|
|
}
|
2010-12-17 04:09:47 +00:00
|
|
|
|
|
|
|
bool LiveIntervalUnion::Query::checkLoopInterference(MachineLoopRange *Loop) {
|
|
|
|
// VirtReg is likely live throughout the loop, so start by checking LIU-Loop
|
|
|
|
// overlaps.
|
|
|
|
IntervalMapOverlaps<LiveIntervalUnion::Map, MachineLoopRange::Map>
|
|
|
|
Overlaps(LiveUnion->getMap(), Loop->getMap());
|
|
|
|
if (!Overlaps.valid())
|
|
|
|
return false;
|
|
|
|
|
|
|
|
// The loop is overlapping an LIU assignment. Check VirtReg as well.
|
|
|
|
LiveInterval::iterator VRI = VirtReg->find(Overlaps.start());
|
|
|
|
|
|
|
|
for (;;) {
|
|
|
|
if (VRI == VirtReg->end())
|
|
|
|
return false;
|
|
|
|
if (VRI->start < Overlaps.stop())
|
|
|
|
return true;
|
|
|
|
|
|
|
|
Overlaps.advanceTo(VRI->start);
|
|
|
|
if (!Overlaps.valid())
|
|
|
|
return false;
|
|
|
|
if (Overlaps.start() < VRI->end)
|
|
|
|
return true;
|
|
|
|
|
|
|
|
VRI = VirtReg->advanceTo(VRI, Overlaps.start());
|
|
|
|
}
|
|
|
|
}
|