2017-02-17 22:43:25 +01:00
|
|
|
//===- LiveIntervalUnion.cpp - Live interval union data structure ---------===//
|
2010-10-23 01:09:15 +02:00
|
|
|
//
|
2019-01-19 09:50:56 +01:00
|
|
|
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
|
|
|
|
// See https://llvm.org/LICENSE.txt for license information.
|
|
|
|
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
2010-10-23 01:09:15 +02:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// 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.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2017-06-06 13:49:48 +02:00
|
|
|
#include "llvm/CodeGen/LiveIntervalUnion.h"
|
2017-02-17 22:43:25 +01:00
|
|
|
#include "llvm/ADT/STLExtras.h"
|
2017-06-06 13:49:48 +02:00
|
|
|
#include "llvm/ADT/SparseBitVector.h"
|
2017-02-17 22:43:25 +01:00
|
|
|
#include "llvm/CodeGen/LiveInterval.h"
|
2017-11-17 02:07:10 +01:00
|
|
|
#include "llvm/CodeGen/TargetRegisterInfo.h"
|
2010-10-23 01:09:15 +02:00
|
|
|
#include "llvm/Support/raw_ostream.h"
|
2017-02-17 22:43:25 +01:00
|
|
|
#include <cassert>
|
|
|
|
#include <cstdlib>
|
2011-12-21 21:16:11 +01:00
|
|
|
|
2010-10-23 01:09:15 +02:00
|
|
|
using namespace llvm;
|
|
|
|
|
2014-04-22 04:02:50 +02:00
|
|
|
#define DEBUG_TYPE "regalloc"
|
|
|
|
|
2010-10-23 01:09:15 +02:00
|
|
|
// Merge a LiveInterval's segments. Guarantee no overlaps.
|
2014-12-10 02:12:59 +01:00
|
|
|
void LiveIntervalUnion::unify(LiveInterval &VirtReg, const LiveRange &Range) {
|
|
|
|
if (Range.empty())
|
2010-12-08 00:18:47 +01:00
|
|
|
return;
|
2011-02-09 22:52:03 +01:00
|
|
|
++Tag;
|
2010-12-01 00:18:47 +01:00
|
|
|
|
|
|
|
// Insert each of the virtual register's live segments into the map.
|
2014-12-10 02:12:59 +01:00
|
|
|
LiveRange::const_iterator RegPos = Range.begin();
|
|
|
|
LiveRange::const_iterator RegEnd = Range.end();
|
2010-12-08 00:18:47 +01:00
|
|
|
SegmentIter SegPos = Segments.find(RegPos->start);
|
|
|
|
|
2011-04-11 17:00:44 +02:00
|
|
|
while (SegPos.valid()) {
|
2010-12-08 00:18:47 +01:00
|
|
|
SegPos.insert(RegPos->start, RegPos->end, &VirtReg);
|
|
|
|
if (++RegPos == RegEnd)
|
|
|
|
return;
|
|
|
|
SegPos.advanceTo(RegPos->start);
|
2010-10-23 01:09:15 +02:00
|
|
|
}
|
2011-04-11 17:00:44 +02: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-23 01:09:15 +02:00
|
|
|
}
|
|
|
|
|
2010-11-08 19:02:08 +01:00
|
|
|
// Remove a live virtual register's segments from this union.
|
2014-12-10 02:12:59 +01:00
|
|
|
void LiveIntervalUnion::extract(LiveInterval &VirtReg, const LiveRange &Range) {
|
|
|
|
if (Range.empty())
|
2010-12-08 00:18:47 +01:00
|
|
|
return;
|
2011-02-09 22:52:03 +01:00
|
|
|
++Tag;
|
2010-12-01 00:18:47 +01:00
|
|
|
|
2010-11-08 19:02:08 +01:00
|
|
|
// Remove each of the virtual register's live segments from the map.
|
2014-12-10 02:12:59 +01:00
|
|
|
LiveRange::const_iterator RegPos = Range.begin();
|
|
|
|
LiveRange::const_iterator RegEnd = Range.end();
|
2010-12-08 00:18:47 +01:00
|
|
|
SegmentIter SegPos = Segments.find(RegPos->start);
|
|
|
|
|
2017-02-17 22:43:25 +01:00
|
|
|
while (true) {
|
2010-12-08 00:18:47 +01:00
|
|
|
assert(SegPos.value() == &VirtReg && "Inconsistent LiveInterval");
|
|
|
|
SegPos.erase();
|
|
|
|
if (!SegPos.valid())
|
|
|
|
return;
|
|
|
|
|
|
|
|
// Skip all segments that may have been coalesced.
|
2014-12-10 02:12:59 +01:00
|
|
|
RegPos = Range.advanceTo(RegPos, SegPos.start());
|
2010-12-08 00:18:47 +01:00
|
|
|
if (RegPos == RegEnd)
|
|
|
|
return;
|
|
|
|
|
|
|
|
SegPos.advanceTo(RegPos->start);
|
2010-10-23 01:09:15 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-11-09 22:04:34 +01:00
|
|
|
void
|
2010-12-14 19:53:47 +01:00
|
|
|
LiveIntervalUnion::print(raw_ostream &OS, const TargetRegisterInfo *TRI) const {
|
2010-12-14 20:38:49 +01:00
|
|
|
if (empty()) {
|
|
|
|
OS << " empty\n";
|
|
|
|
return;
|
|
|
|
}
|
2010-12-14 19:53:47 +01:00
|
|
|
for (LiveSegments::const_iterator SI = Segments.begin(); SI.valid(); ++SI) {
|
2011-01-09 04:05:53 +01:00
|
|
|
OS << " [" << SI.start() << ' ' << SI.stop() << "):"
|
2017-11-28 13:42:37 +01:00
|
|
|
<< printReg(SI.value()->reg, TRI);
|
2010-11-09 22:04:34 +01:00
|
|
|
}
|
2010-12-14 20:38:49 +01:00
|
|
|
OS << '\n';
|
|
|
|
}
|
|
|
|
|
2010-11-09 22:04:34 +01:00
|
|
|
#ifndef NDEBUG
|
|
|
|
// Verify the live intervals in this union and add them to the visited set.
|
2010-12-01 00:18:47 +01:00
|
|
|
void LiveIntervalUnion::verify(LiveVirtRegBitSet& VisitedVRegs) {
|
2010-12-08 00:18:47 +01:00
|
|
|
for (SegmentIter SI = Segments.begin(); SI.valid(); ++SI)
|
|
|
|
VisitedVRegs.set(SI.value()->reg);
|
2010-11-09 22:04:34 +01:00
|
|
|
}
|
|
|
|
#endif //!NDEBUG
|
|
|
|
|
2010-12-01 00:18:47 +01:00
|
|
|
// Scan the vector of interfering virtual registers in this union. Assume it's
|
2010-11-10 20:18:47 +01:00
|
|
|
// quite small.
|
2010-12-01 00:18:47 +01:00
|
|
|
bool LiveIntervalUnion::Query::isSeenInterference(LiveInterval *VirtReg) const {
|
2016-08-12 00:21:41 +02:00
|
|
|
return is_contained(InterferingVRegs, VirtReg);
|
2010-11-10 20:18:47 +01:00
|
|
|
}
|
|
|
|
|
2011-08-12 02:22:04 +02:00
|
|
|
// Collect virtual registers in this union that interfere with this
|
2010-12-01 00:18:47 +01:00
|
|
|
// query's live virtual register.
|
|
|
|
//
|
2011-08-12 02:22:04 +02: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 20:18:47 +01:00
|
|
|
//
|
|
|
|
unsigned LiveIntervalUnion::Query::
|
2011-07-08 22:46:18 +02:00
|
|
|
collectInterferingVRegs(unsigned MaxInterferingRegs) {
|
2011-08-12 00:46:04 +02:00
|
|
|
// Fast path return if we already have the desired information.
|
|
|
|
if (SeenAllInterferences || InterferingVRegs.size() >= MaxInterferingRegs)
|
2011-08-11 23:18:34 +02:00
|
|
|
return InterferingVRegs.size();
|
2011-08-12 00:46:04 +02:00
|
|
|
|
|
|
|
// Set up iterators on the first call.
|
|
|
|
if (!CheckedFirstInterference) {
|
|
|
|
CheckedFirstInterference = true;
|
|
|
|
|
|
|
|
// Quickly skip interference check for empty sets.
|
2017-03-01 22:48:12 +01:00
|
|
|
if (LR->empty() || LiveUnion->empty()) {
|
2011-08-12 02:22:04 +02:00
|
|
|
SeenAllInterferences = true;
|
|
|
|
return 0;
|
2011-08-12 00:46:04 +02:00
|
|
|
}
|
2011-08-12 02:22:04 +02:00
|
|
|
|
2017-03-01 22:48:12 +01:00
|
|
|
// In most cases, the union will start before LR.
|
|
|
|
LRI = LR->begin();
|
2011-08-12 02:22:04 +02:00
|
|
|
LiveUnionI.setMap(LiveUnion->getMap());
|
2017-03-01 22:48:12 +01:00
|
|
|
LiveUnionI.find(LRI->start);
|
2011-08-12 00:46:04 +02:00
|
|
|
}
|
|
|
|
|
2017-03-01 22:48:12 +01:00
|
|
|
LiveRange::const_iterator LREnd = LR->end();
|
2014-04-14 02:51:57 +02:00
|
|
|
LiveInterval *RecentReg = nullptr;
|
2011-08-12 02:22:04 +02:00
|
|
|
while (LiveUnionI.valid()) {
|
2017-03-01 22:48:12 +01:00
|
|
|
assert(LRI != LREnd && "Reached end of LR");
|
2011-08-12 02:22:04 +02:00
|
|
|
|
|
|
|
// Check for overlapping interference.
|
2017-03-01 22:48:12 +01:00
|
|
|
while (LRI->start < LiveUnionI.stop() && LRI->end > LiveUnionI.start()) {
|
2011-08-12 02:22:04 +02:00
|
|
|
// 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 20:18:47 +01:00
|
|
|
|
2011-08-12 02:22:04 +02:00
|
|
|
// The iterators are now not overlapping, LiveUnionI has been advanced
|
2017-03-01 22:48:12 +01:00
|
|
|
// beyond LRI.
|
|
|
|
assert(LRI->end <= LiveUnionI.start() && "Expected non-overlap");
|
2010-11-10 20:18:47 +01:00
|
|
|
|
2011-08-12 02:22:04 +02:00
|
|
|
// Advance the iterator that ends first.
|
2017-03-01 22:48:12 +01:00
|
|
|
LRI = LR->advanceTo(LRI, LiveUnionI.start());
|
|
|
|
if (LRI == LREnd)
|
2010-11-10 20:18:47 +01:00
|
|
|
break;
|
|
|
|
|
2011-08-12 02:22:04 +02:00
|
|
|
// Detect overlap, handle above.
|
2017-03-01 22:48:12 +01:00
|
|
|
if (LRI->start < LiveUnionI.stop())
|
2010-11-10 20:18:47 +01:00
|
|
|
continue;
|
2011-08-12 02:22:04 +02:00
|
|
|
|
|
|
|
// Still not overlapping. Catch up LiveUnionI.
|
2017-03-01 22:48:12 +01:00
|
|
|
LiveUnionI.advanceTo(LRI->start);
|
2010-11-10 20:18:47 +01:00
|
|
|
}
|
2010-12-01 00:18:47 +01:00
|
|
|
SeenAllInterferences = true;
|
|
|
|
return InterferingVRegs.size();
|
2010-11-10 20:18:47 +01:00
|
|
|
}
|
2010-12-17 05:09:47 +01:00
|
|
|
|
2012-06-06 01:57:30 +02:00
|
|
|
void LiveIntervalUnion::Array::init(LiveIntervalUnion::Allocator &Alloc,
|
|
|
|
unsigned NSize) {
|
|
|
|
// Reuse existing allocation.
|
|
|
|
if (NSize == Size)
|
|
|
|
return;
|
|
|
|
clear();
|
|
|
|
Size = NSize;
|
|
|
|
LIUs = static_cast<LiveIntervalUnion*>(
|
Report fatal error in the case of out of memory
This is the second part of recommit of r325224. The previous part was
committed in r325426, which deals with C++ memory allocation. Solution
for C memory allocation involved functions `llvm::malloc` and similar.
This was a fragile solution because it caused ambiguity errors in some
cases. In this commit the new functions have names like `llvm::safe_malloc`.
The relevant part of original comment is below, updated for new function
names.
Analysis of fails in the case of out of memory errors can be tricky on
Windows. Such error emerges at the point where memory allocation function
fails, but manifests itself when null pointer is used. These two points
may be distant from each other. Besides, next runs may not exhibit
allocation error.
In some cases memory is allocated by a call to some of C allocation
functions, malloc, calloc and realloc. They are used for interoperability
with C code, when allocated object has variable size and when it is
necessary to avoid call of constructors. In many calls the result is not
checked for null pointer. To simplify checks, new functions are defined
in the namespace 'llvm': `safe_malloc`, `safe_calloc` and `safe_realloc`.
They behave as corresponding standard functions but produce fatal error if
allocation fails. This change replaces the standard functions like 'malloc'
in the cases when the result of the allocation function is not checked
for null pointer.
Finally, there are plain C code, that uses malloc and similar functions. If
the result is not checked, assert statement is added.
Differential Revision: https://reviews.llvm.org/D43010
llvm-svn: 325551
2018-02-20 06:41:26 +01:00
|
|
|
safe_malloc(sizeof(LiveIntervalUnion)*NSize));
|
2012-06-06 01:57:30 +02:00
|
|
|
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;
|
2014-04-14 02:51:57 +02:00
|
|
|
LIUs = nullptr;
|
2012-06-06 01:57:30 +02:00
|
|
|
}
|