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"
|
2012-11-28 19:13:06 +00:00
|
|
|
#include "llvm/CodeGen/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"
|
2010-12-14 18:53:47 +00:00
|
|
|
#include "llvm/Target/TargetRegisterInfo.h"
|
2011-12-21 20:16:11 +00:00
|
|
|
#include <algorithm>
|
|
|
|
|
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 {
|
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';
|
|
|
|
}
|
|
|
|
|
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-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
|
|
|
}
|
|
|
|
|
2011-08-12 00:22:04 +00:00
|
|
|
// Collect virtual registers in this union that interfere with this
|
2010-11-30 23:18:47 +00:00
|
|
|
// query's live virtual register.
|
|
|
|
//
|
2011-08-12 00:22:04 +00:00
|
|
|
// The query state is one of:
|
|
|
|
//
|
|
|
|
// 1. CheckedFirstInterference == false: Iterators are uninitialized.
|
|
|
|
// 2. SeenAllInterferences == true: InterferingVRegs complete, iterators unused.
|
|
|
|
// 3. Iterators left at the last seen intersection.
|
2010-11-10 19:18:47 +00:00
|
|
|
//
|
|
|
|
unsigned LiveIntervalUnion::Query::
|
2011-07-08 20:46:18 +00:00
|
|
|
collectInterferingVRegs(unsigned MaxInterferingRegs) {
|
2011-08-11 22:46:04 +00:00
|
|
|
// Fast path return if we already have the desired information.
|
|
|
|
if (SeenAllInterferences || InterferingVRegs.size() >= MaxInterferingRegs)
|
2011-08-11 21:18:34 +00:00
|
|
|
return InterferingVRegs.size();
|
2011-08-11 22:46:04 +00:00
|
|
|
|
|
|
|
// Set up iterators on the first call.
|
|
|
|
if (!CheckedFirstInterference) {
|
|
|
|
CheckedFirstInterference = true;
|
|
|
|
|
|
|
|
// Quickly skip interference check for empty sets.
|
|
|
|
if (VirtReg->empty() || LiveUnion->empty()) {
|
2011-08-12 00:22:04 +00:00
|
|
|
SeenAllInterferences = true;
|
|
|
|
return 0;
|
2011-08-11 22:46:04 +00:00
|
|
|
}
|
2011-08-12 00:22:04 +00:00
|
|
|
|
|
|
|
// In most cases, the union will start before VirtReg.
|
|
|
|
VirtRegI = VirtReg->begin();
|
|
|
|
LiveUnionI.setMap(LiveUnion->getMap());
|
|
|
|
LiveUnionI.find(VirtRegI->start);
|
2011-08-11 22:46:04 +00:00
|
|
|
}
|
|
|
|
|
2010-11-30 23:18:47 +00:00
|
|
|
LiveInterval::iterator VirtRegEnd = VirtReg->end();
|
2011-08-12 00:22:04 +00:00
|
|
|
LiveInterval *RecentReg = 0;
|
|
|
|
while (LiveUnionI.valid()) {
|
|
|
|
assert(VirtRegI != VirtRegEnd && "Reached end of VirtReg");
|
|
|
|
|
|
|
|
// Check for overlapping interference.
|
|
|
|
while (VirtRegI->start < LiveUnionI.stop() &&
|
|
|
|
VirtRegI->end > LiveUnionI.start()) {
|
|
|
|
// This is an overlap, record the interfering register.
|
|
|
|
LiveInterval *VReg = LiveUnionI.value();
|
|
|
|
if (VReg != RecentReg && !isSeenInterference(VReg)) {
|
|
|
|
RecentReg = VReg;
|
|
|
|
InterferingVRegs.push_back(VReg);
|
|
|
|
if (InterferingVRegs.size() >= MaxInterferingRegs)
|
|
|
|
return InterferingVRegs.size();
|
|
|
|
}
|
|
|
|
// This LiveUnion segment is no longer interesting.
|
|
|
|
if (!(++LiveUnionI).valid()) {
|
|
|
|
SeenAllInterferences = true;
|
|
|
|
return InterferingVRegs.size();
|
|
|
|
}
|
|
|
|
}
|
2010-11-10 19:18:47 +00:00
|
|
|
|
2011-08-12 00:22:04 +00:00
|
|
|
// The iterators are now not overlapping, LiveUnionI has been advanced
|
|
|
|
// beyond VirtRegI.
|
|
|
|
assert(VirtRegI->end <= LiveUnionI.start() && "Expected non-overlap");
|
2010-11-10 19:18:47 +00:00
|
|
|
|
2011-08-12 00:22:04 +00:00
|
|
|
// Advance the iterator that ends first.
|
2011-08-11 22:46:04 +00:00
|
|
|
VirtRegI = VirtReg->advanceTo(VirtRegI, LiveUnionI.start());
|
|
|
|
if (VirtRegI == VirtRegEnd)
|
2010-11-10 19:18:47 +00:00
|
|
|
break;
|
|
|
|
|
2011-08-12 00:22:04 +00:00
|
|
|
// Detect overlap, handle above.
|
|
|
|
if (VirtRegI->start < LiveUnionI.stop())
|
2010-11-10 19:18:47 +00:00
|
|
|
continue;
|
2011-08-12 00:22:04 +00:00
|
|
|
|
|
|
|
// Still not overlapping. Catch up LiveUnionI.
|
2011-08-11 22:46:04 +00:00
|
|
|
LiveUnionI.advanceTo(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
|
|
|
|
2012-06-05 23:57:30 +00:00
|
|
|
void LiveIntervalUnion::Array::init(LiveIntervalUnion::Allocator &Alloc,
|
|
|
|
unsigned NSize) {
|
|
|
|
// Reuse existing allocation.
|
|
|
|
if (NSize == Size)
|
|
|
|
return;
|
|
|
|
clear();
|
|
|
|
Size = NSize;
|
|
|
|
LIUs = static_cast<LiveIntervalUnion*>(
|
|
|
|
malloc(sizeof(LiveIntervalUnion)*NSize));
|
|
|
|
for (unsigned i = 0; i != Size; ++i)
|
|
|
|
new(LIUs + i) LiveIntervalUnion(Alloc);
|
|
|
|
}
|
|
|
|
|
|
|
|
void LiveIntervalUnion::Array::clear() {
|
|
|
|
if (!LIUs)
|
|
|
|
return;
|
|
|
|
for (unsigned i = 0; i != Size; ++i)
|
|
|
|
LIUs[i].~LiveIntervalUnion();
|
|
|
|
free(LIUs);
|
|
|
|
Size = 0;
|
|
|
|
LIUs = 0;
|
|
|
|
}
|