2017-02-17 22:43:25 +01:00
|
|
|
//===- LiveIntervalUnion.h - Live interval union data struct ---*- C++ -*--===//
|
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 is a union of live segments across multiple live virtual
|
|
|
|
// registers. This may be used during coalescing to represent a congruence
|
|
|
|
// class, or during register allocation to model liveness of a physical
|
|
|
|
// register.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2013-01-10 01:45:19 +01:00
|
|
|
#ifndef LLVM_CODEGEN_LIVEINTERVALUNION_H
|
|
|
|
#define LLVM_CODEGEN_LIVEINTERVALUNION_H
|
2010-10-23 01:09:15 +02:00
|
|
|
|
2010-12-08 00:18:47 +01:00
|
|
|
#include "llvm/ADT/IntervalMap.h"
|
2017-02-17 22:43:25 +01:00
|
|
|
#include "llvm/ADT/SmallVector.h"
|
2010-10-23 01:09:15 +02:00
|
|
|
#include "llvm/CodeGen/LiveInterval.h"
|
2017-02-17 22:43:25 +01:00
|
|
|
#include "llvm/CodeGen/SlotIndexes.h"
|
|
|
|
#include <cassert>
|
|
|
|
#include <limits>
|
2010-10-23 01:09:15 +02:00
|
|
|
|
|
|
|
namespace llvm {
|
|
|
|
|
2017-05-25 01:10:29 +02:00
|
|
|
class raw_ostream;
|
2010-12-14 19:53:47 +01:00
|
|
|
class TargetRegisterInfo;
|
|
|
|
|
2010-11-09 22:04:34 +01:00
|
|
|
#ifndef NDEBUG
|
|
|
|
// forward declaration
|
|
|
|
template <unsigned Element> class SparseBitVector;
|
2017-05-25 01:10:29 +02:00
|
|
|
|
|
|
|
using LiveVirtRegBitSet = SparseBitVector<128>;
|
2010-11-09 22:04:34 +01:00
|
|
|
#endif
|
|
|
|
|
2010-10-23 01:09:15 +02:00
|
|
|
/// Union of live intervals that are strong candidates for coalescing into a
|
|
|
|
/// single register (either physical or virtual depending on the context). We
|
|
|
|
/// expect the constituent live intervals to be disjoint, although we may
|
|
|
|
/// eventually make exceptions to handle value-based interference.
|
|
|
|
class LiveIntervalUnion {
|
|
|
|
// A set of live virtual register segments that supports fast insertion,
|
2010-12-01 00:18:47 +01:00
|
|
|
// intersection, and removal.
|
2010-12-08 00:18:47 +01:00
|
|
|
// Mapping SlotIndex intervals to virtual register numbers.
|
2017-05-25 01:10:29 +02:00
|
|
|
using LiveSegments = IntervalMap<SlotIndex, LiveInterval*>;
|
2010-10-23 01:09:15 +02:00
|
|
|
|
|
|
|
public:
|
|
|
|
// SegmentIter can advance to the next segment ordered by starting position
|
|
|
|
// which may belong to a different live virtual register. We also must be able
|
|
|
|
// to reach the current segment's containing virtual register.
|
2017-05-25 01:10:29 +02:00
|
|
|
using SegmentIter = LiveSegments::iterator;
|
2010-10-23 01:09:15 +02:00
|
|
|
|
2017-03-01 22:48:12 +01:00
|
|
|
/// Const version of SegmentIter.
|
2017-05-25 01:10:29 +02:00
|
|
|
using ConstSegmentIter = LiveSegments::const_iterator;
|
2017-03-01 22:48:12 +01:00
|
|
|
|
2010-12-08 00:18:47 +01:00
|
|
|
// LiveIntervalUnions share an external allocator.
|
2017-05-25 01:10:29 +02:00
|
|
|
using Allocator = LiveSegments::Allocator;
|
2010-12-08 00:18:47 +01:00
|
|
|
|
2010-10-23 01:09:15 +02:00
|
|
|
private:
|
2017-02-17 22:43:25 +01:00
|
|
|
unsigned Tag = 0; // unique tag for current contents.
|
2010-12-08 00:18:47 +01:00
|
|
|
LiveSegments Segments; // union of virtual reg segments
|
2010-10-23 01:09:15 +02:00
|
|
|
|
|
|
|
public:
|
2017-02-17 22:43:25 +01:00
|
|
|
explicit LiveIntervalUnion(Allocator &a) : Segments(a) {}
|
2010-10-23 01:09:15 +02:00
|
|
|
|
2010-10-26 20:34:01 +02:00
|
|
|
// Iterate over all segments in the union of live virtual registers ordered
|
|
|
|
// by their starting position.
|
2010-12-01 00:18:47 +01:00
|
|
|
SegmentIter begin() { return Segments.begin(); }
|
|
|
|
SegmentIter end() { return Segments.end(); }
|
2010-12-09 02:06:52 +01:00
|
|
|
SegmentIter find(SlotIndex x) { return Segments.find(x); }
|
2017-03-01 22:48:12 +01:00
|
|
|
ConstSegmentIter begin() const { return Segments.begin(); }
|
|
|
|
ConstSegmentIter end() const { return Segments.end(); }
|
|
|
|
ConstSegmentIter find(SlotIndex x) const { return Segments.find(x); }
|
|
|
|
|
2010-12-14 20:38:49 +01:00
|
|
|
bool empty() const { return Segments.empty(); }
|
|
|
|
SlotIndex startIndex() const { return Segments.start(); }
|
2019-10-28 17:15:52 +01:00
|
|
|
SlotIndex endIndex() const { return Segments.stop(); }
|
2010-10-23 01:09:15 +02:00
|
|
|
|
2010-12-17 05:09:47 +01:00
|
|
|
// Provide public access to the underlying map to allow overlap iteration.
|
2017-05-25 01:10:29 +02:00
|
|
|
using Map = LiveSegments;
|
2017-03-01 22:48:12 +01:00
|
|
|
const Map &getMap() const { return Segments; }
|
2010-12-17 05:09:47 +01:00
|
|
|
|
2011-02-09 22:52:03 +01:00
|
|
|
/// getTag - Return an opaque tag representing the current state of the union.
|
|
|
|
unsigned getTag() const { return Tag; }
|
|
|
|
|
|
|
|
/// changedSince - Return true if the union change since getTag returned tag.
|
|
|
|
bool changedSince(unsigned tag) const { return tag != Tag; }
|
|
|
|
|
2010-10-26 20:34:01 +02:00
|
|
|
// Add a live virtual register to this union and merge its segments.
|
2014-12-10 02:12:59 +01:00
|
|
|
void unify(LiveInterval &VirtReg, const LiveRange &Range);
|
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 extract(LiveInterval &VirtReg, const LiveRange &Range);
|
2010-10-23 01:09:15 +02:00
|
|
|
|
2011-04-12 01:57:14 +02:00
|
|
|
// Remove all inserted virtual registers.
|
|
|
|
void clear() { Segments.clear(); ++Tag; }
|
|
|
|
|
2010-12-14 19:53:47 +01:00
|
|
|
// Print union, using TRI to translate register names
|
|
|
|
void print(raw_ostream &OS, const TargetRegisterInfo *TRI) const;
|
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 verify(LiveVirtRegBitSet& VisitedVRegs);
|
2010-11-09 22:04:34 +01:00
|
|
|
#endif
|
|
|
|
|
2020-09-06 04:17:22 +02:00
|
|
|
// Get any virtual register that is assign to this physical unit
|
|
|
|
LiveInterval *getOneVReg() const;
|
|
|
|
|
2010-10-23 01:09:15 +02:00
|
|
|
/// Query interferences between a single live virtual register and a live
|
|
|
|
/// interval union.
|
|
|
|
class Query {
|
2017-03-01 22:48:12 +01:00
|
|
|
const LiveIntervalUnion *LiveUnion = nullptr;
|
|
|
|
const LiveRange *LR = nullptr;
|
|
|
|
LiveRange::const_iterator LRI; ///< current position in LR
|
|
|
|
ConstSegmentIter LiveUnionI; ///< current position in LiveUnion
|
2021-03-09 05:55:53 +01:00
|
|
|
Optional<SmallVector<LiveInterval *, 4>> InterferingVRegs;
|
2017-02-17 22:43:25 +01:00
|
|
|
bool CheckedFirstInterference = false;
|
|
|
|
bool SeenAllInterferences = false;
|
|
|
|
unsigned Tag = 0;
|
|
|
|
unsigned UserTag = 0;
|
2010-10-23 01:09:15 +02:00
|
|
|
|
2021-03-09 05:55:53 +01:00
|
|
|
public:
|
|
|
|
Query() = default;
|
|
|
|
Query(const LiveRange &LR, const LiveIntervalUnion &LIU)
|
|
|
|
: LiveUnion(&LIU), LR(&LR) {}
|
|
|
|
Query(const Query &) = delete;
|
|
|
|
Query &operator=(const Query &) = delete;
|
|
|
|
|
2017-03-01 22:48:12 +01:00
|
|
|
void reset(unsigned NewUserTag, const LiveRange &NewLR,
|
|
|
|
const LiveIntervalUnion &NewLiveUnion) {
|
|
|
|
LiveUnion = &NewLiveUnion;
|
|
|
|
LR = &NewLR;
|
2021-03-09 05:55:53 +01:00
|
|
|
InterferingVRegs = None;
|
2010-12-09 02:06:52 +01:00
|
|
|
CheckedFirstInterference = false;
|
2010-12-01 00:18:47 +01:00
|
|
|
SeenAllInterferences = false;
|
2017-03-01 22:48:12 +01:00
|
|
|
Tag = NewLiveUnion.getTag();
|
|
|
|
UserTag = NewUserTag;
|
2010-11-08 19:02:08 +01:00
|
|
|
}
|
2010-12-01 00:18:47 +01:00
|
|
|
|
2017-03-01 22:48:12 +01:00
|
|
|
void init(unsigned NewUserTag, const LiveRange &NewLR,
|
|
|
|
const LiveIntervalUnion &NewLiveUnion) {
|
|
|
|
if (UserTag == NewUserTag && LR == &NewLR && LiveUnion == &NewLiveUnion &&
|
|
|
|
!NewLiveUnion.changedSince(Tag)) {
|
2010-11-08 19:02:08 +01:00
|
|
|
// Retain cached results, e.g. firstInterference.
|
|
|
|
return;
|
|
|
|
}
|
2017-03-01 22:48:12 +01:00
|
|
|
reset(NewUserTag, NewLR, NewLiveUnion);
|
2010-11-08 19:02:08 +01:00
|
|
|
}
|
2010-10-23 01:09:15 +02:00
|
|
|
|
2010-12-01 00:18:47 +01:00
|
|
|
// Does this live virtual register interfere with the union?
|
2011-08-11 23:18:34 +02:00
|
|
|
bool checkInterference() { return collectInterferingVRegs(1); }
|
2010-10-23 01:09:15 +02:00
|
|
|
|
2010-11-10 20:18:47 +01:00
|
|
|
// Count the virtual registers in this union that interfere with this
|
|
|
|
// query's live virtual register, up to maxInterferingRegs.
|
2017-02-17 22:43:25 +01:00
|
|
|
unsigned collectInterferingVRegs(
|
|
|
|
unsigned MaxInterferingRegs = std::numeric_limits<unsigned>::max());
|
2010-11-10 20:18:47 +01:00
|
|
|
|
|
|
|
// Was this virtual register visited during collectInterferingVRegs?
|
2018-07-16 20:51:40 +02:00
|
|
|
bool isSeenInterference(LiveInterval *VirtReg) const;
|
2010-12-01 00:18:47 +01:00
|
|
|
|
|
|
|
// Did collectInterferingVRegs collect all interferences?
|
|
|
|
bool seenAllInterferences() const { return SeenAllInterferences; }
|
2010-11-10 20:18:47 +01:00
|
|
|
|
|
|
|
// Vector generated by collectInterferingVRegs.
|
|
|
|
const SmallVectorImpl<LiveInterval*> &interferingVRegs() const {
|
2021-03-09 05:55:53 +01:00
|
|
|
return *InterferingVRegs;
|
2010-11-10 20:18:47 +01:00
|
|
|
}
|
2010-10-23 01:09:15 +02:00
|
|
|
};
|
2012-06-06 01:57:30 +02:00
|
|
|
|
|
|
|
// Array of LiveIntervalUnions.
|
|
|
|
class Array {
|
2017-02-17 22:43:25 +01:00
|
|
|
unsigned Size = 0;
|
|
|
|
LiveIntervalUnion *LIUs = nullptr;
|
|
|
|
|
2012-06-06 01:57:30 +02:00
|
|
|
public:
|
2017-02-17 22:43:25 +01:00
|
|
|
Array() = default;
|
2012-06-06 01:57:30 +02:00
|
|
|
~Array() { clear(); }
|
|
|
|
|
|
|
|
// Initialize the array to have Size entries.
|
|
|
|
// Reuse an existing allocation if the size matches.
|
|
|
|
void init(LiveIntervalUnion::Allocator&, unsigned Size);
|
|
|
|
|
|
|
|
unsigned size() const { return Size; }
|
|
|
|
|
|
|
|
void clear();
|
|
|
|
|
|
|
|
LiveIntervalUnion& operator[](unsigned idx) {
|
|
|
|
assert(idx < Size && "idx out of bounds");
|
|
|
|
return LIUs[idx];
|
|
|
|
}
|
2015-07-14 19:38:17 +02:00
|
|
|
|
|
|
|
const LiveIntervalUnion& operator[](unsigned Idx) const {
|
|
|
|
assert(Idx < Size && "Idx out of bounds");
|
|
|
|
return LIUs[Idx];
|
|
|
|
}
|
2012-06-06 01:57:30 +02:00
|
|
|
};
|
2010-10-23 01:09:15 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
} // end namespace llvm
|
|
|
|
|
2017-02-17 22:43:25 +01:00
|
|
|
#endif // LLVM_CODEGEN_LIVEINTERVALUNION_H
|