2017-08-25 05:21:39 +08:00
|
|
|
//===- LiveRangeCalc.h - Calculate live ranges ------------------*- C++ -*-===//
|
2011-09-13 09:34:21 +08:00
|
|
|
//
|
2019-01-19 16:50:56 +08: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
|
2011-09-13 09:34:21 +08:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// The LiveRangeCalc class can be used to compute live ranges from scratch. It
|
|
|
|
// caches information about values in the CFG to speed up repeated operations
|
|
|
|
// on the same live range. The cache can be shared by non-overlapping live
|
|
|
|
// ranges. SplitKit uses that when computing the live range of split products.
|
|
|
|
//
|
|
|
|
// A low-level interface is available to clients that know where a variable is
|
|
|
|
// live, but don't know which value it has as every point. LiveRangeCalc will
|
|
|
|
// propagate values down the dominator tree, and even insert PHI-defs where
|
|
|
|
// needed. SplitKit uses this faster interface when possible.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2014-08-14 00:26:38 +08:00
|
|
|
#ifndef LLVM_LIB_CODEGEN_LIVERANGECALC_H
|
|
|
|
#define LLVM_LIB_CODEGEN_LIVERANGECALC_H
|
2011-09-13 09:34:21 +08:00
|
|
|
|
2016-08-24 21:37:55 +08:00
|
|
|
#include "llvm/ADT/ArrayRef.h"
|
2011-09-13 09:34:21 +08:00
|
|
|
#include "llvm/ADT/BitVector.h"
|
2017-06-28 02:05:26 +08:00
|
|
|
#include "llvm/ADT/DenseMap.h"
|
2011-09-13 09:34:21 +08:00
|
|
|
#include "llvm/ADT/IndexedMap.h"
|
2017-08-25 05:21:39 +08:00
|
|
|
#include "llvm/ADT/SmallVector.h"
|
2011-09-13 09:34:21 +08:00
|
|
|
#include "llvm/CodeGen/LiveInterval.h"
|
2017-08-25 05:21:39 +08:00
|
|
|
#include "llvm/CodeGen/MachineBasicBlock.h"
|
|
|
|
#include "llvm/CodeGen/SlotIndexes.h"
|
|
|
|
#include "llvm/MC/LaneBitmask.h"
|
|
|
|
#include <utility>
|
2011-09-13 09:34:21 +08:00
|
|
|
|
|
|
|
namespace llvm {
|
|
|
|
|
|
|
|
template <class NodeT> class DomTreeNodeBase;
|
2017-08-25 05:21:39 +08:00
|
|
|
class MachineDominatorTree;
|
|
|
|
class MachineFunction;
|
|
|
|
class MachineRegisterInfo;
|
|
|
|
|
|
|
|
using MachineDomTreeNode = DomTreeNodeBase<MachineBasicBlock>;
|
2011-09-13 09:34:21 +08:00
|
|
|
|
2013-09-12 02:05:11 +08:00
|
|
|
class LiveRangeCalc {
|
2017-08-25 05:21:39 +08:00
|
|
|
const MachineFunction *MF = nullptr;
|
|
|
|
const MachineRegisterInfo *MRI = nullptr;
|
|
|
|
SlotIndexes *Indexes = nullptr;
|
|
|
|
MachineDominatorTree *DomTree = nullptr;
|
|
|
|
VNInfo::Allocator *Alloc = nullptr;
|
2012-06-05 02:21:16 +08:00
|
|
|
|
2011-09-13 09:34:21 +08:00
|
|
|
/// LiveOutPair - A value and the block that defined it. The domtree node is
|
|
|
|
/// redundant, it can be computed as: MDT[Indexes.getMBBFromIndex(VNI->def)].
|
2017-08-25 05:21:39 +08:00
|
|
|
using LiveOutPair = std::pair<VNInfo *, MachineDomTreeNode *>;
|
2011-09-13 09:34:21 +08:00
|
|
|
|
|
|
|
/// LiveOutMap - Map basic blocks to the value leaving the block.
|
2017-08-25 05:21:39 +08:00
|
|
|
using LiveOutMap = IndexedMap<LiveOutPair, MBB2NumberFunctor>;
|
2011-09-13 09:34:21 +08:00
|
|
|
|
2014-12-16 12:03:38 +08:00
|
|
|
/// Bit vector of active entries in LiveOut, also used as a visited set by
|
|
|
|
/// findReachingDefs. One entry per basic block, indexed by block number.
|
|
|
|
/// This is kept as a separate bit vector because it can be cleared quickly
|
|
|
|
/// when switching live ranges.
|
|
|
|
BitVector Seen;
|
|
|
|
|
2016-08-24 21:37:55 +08:00
|
|
|
/// Map LiveRange to sets of blocks (represented by bit vectors) that
|
|
|
|
/// in the live range are defined on entry and undefined on entry.
|
|
|
|
/// A block is defined on entry if there is a path from at least one of
|
|
|
|
/// the defs in the live range to the entry of the block, and conversely,
|
|
|
|
/// a block is undefined on entry, if there is no such path (i.e. no
|
|
|
|
/// definition reaches the entry of the block). A single LiveRangeCalc
|
|
|
|
/// object is used to track live-out information for multiple registers
|
|
|
|
/// in live range splitting (which is ok, since the live ranges of these
|
|
|
|
/// registers do not overlap), but the defined/undefined information must
|
|
|
|
/// be kept separate for each individual range.
|
|
|
|
/// By convention, EntryInfoMap[&LR] = { Defined, Undefined }.
|
2017-08-25 05:21:39 +08:00
|
|
|
using EntryInfoMap = DenseMap<LiveRange *, std::pair<BitVector, BitVector>>;
|
2017-06-28 02:05:26 +08:00
|
|
|
EntryInfoMap EntryInfos;
|
2016-08-24 21:37:55 +08:00
|
|
|
|
2014-12-16 12:03:38 +08:00
|
|
|
/// Map each basic block where a live range is live out to the live-out value
|
|
|
|
/// and its defining block.
|
|
|
|
///
|
|
|
|
/// For every basic block, MBB, one of these conditions shall be true:
|
|
|
|
///
|
|
|
|
/// 1. !Seen.count(MBB->getNumber())
|
|
|
|
/// Blocks without a Seen bit are ignored.
|
|
|
|
/// 2. LiveOut[MBB].second.getNode() == MBB
|
|
|
|
/// The live-out value is defined in MBB.
|
|
|
|
/// 3. forall P in preds(MBB): LiveOut[P] == LiveOut[MBB]
|
|
|
|
/// The live-out value passses through MBB. All predecessors must carry
|
|
|
|
/// the same value.
|
|
|
|
///
|
|
|
|
/// The domtree node may be null, it can be computed.
|
|
|
|
///
|
|
|
|
/// The map can be shared by multiple live ranges as long as no two are
|
|
|
|
/// live-out of the same block.
|
|
|
|
LiveOutMap Map;
|
2011-09-13 09:34:21 +08:00
|
|
|
|
|
|
|
/// LiveInBlock - Information about a basic block where a live range is known
|
|
|
|
/// to be live-in, but the value has not yet been determined.
|
|
|
|
struct LiveInBlock {
|
2013-10-11 05:28:57 +08:00
|
|
|
// The live range set that is live-in to this block. The algorithms can
|
2011-09-13 09:34:21 +08:00
|
|
|
// handle multiple non-overlapping live ranges simultaneously.
|
2013-10-11 05:28:57 +08:00
|
|
|
LiveRange &LR;
|
2011-09-13 09:34:21 +08:00
|
|
|
|
|
|
|
// DomNode - Dominator tree node for the block.
|
|
|
|
// Cleared when the final value has been determined and LI has been updated.
|
|
|
|
MachineDomTreeNode *DomNode;
|
|
|
|
|
|
|
|
// Position in block where the live-in range ends, or SlotIndex() if the
|
|
|
|
// range passes through the block. When the final value has been
|
|
|
|
// determined, the range from the block start to Kill will be added to LI.
|
|
|
|
SlotIndex Kill;
|
|
|
|
|
|
|
|
// Live-in value filled in by updateSSA once it is known.
|
2017-08-25 05:21:39 +08:00
|
|
|
VNInfo *Value = nullptr;
|
2011-09-13 09:34:21 +08:00
|
|
|
|
2013-10-11 05:28:57 +08:00
|
|
|
LiveInBlock(LiveRange &LR, MachineDomTreeNode *node, SlotIndex kill)
|
2017-08-25 05:21:39 +08:00
|
|
|
: LR(LR), DomNode(node), Kill(kill) {}
|
2011-09-13 09:34:21 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
/// LiveIn - Work list of blocks where the live-in value has yet to be
|
|
|
|
/// determined. This list is typically computed by findReachingDefs() and
|
|
|
|
/// used as a work list by updateSSA(). The low-level interface may also be
|
|
|
|
/// used to add entries directly.
|
|
|
|
SmallVector<LiveInBlock, 16> LiveIn;
|
|
|
|
|
2016-08-24 21:37:55 +08:00
|
|
|
/// Check if the entry to block @p MBB can be reached by any of the defs
|
|
|
|
/// in @p LR. Return true if none of the defs reach the entry to @p MBB.
|
|
|
|
bool isDefOnEntry(LiveRange &LR, ArrayRef<SlotIndex> Undefs,
|
|
|
|
MachineBasicBlock &MBB, BitVector &DefOnEntry,
|
|
|
|
BitVector &UndefOnEntry);
|
|
|
|
|
|
|
|
/// Find the set of defs that can reach @p Kill. @p Kill must belong to
|
|
|
|
/// @p UseMBB.
|
2013-02-21 07:08:26 +08:00
|
|
|
///
|
2016-08-24 21:37:55 +08:00
|
|
|
/// If exactly one def can reach @p UseMBB, and the def dominates @p Kill,
|
|
|
|
/// all paths from the def to @p UseMBB are added to @p LR, and the function
|
|
|
|
/// returns true.
|
2013-02-21 07:08:26 +08:00
|
|
|
///
|
2015-02-18 09:50:52 +08:00
|
|
|
/// If multiple values can reach @p UseMBB, the blocks that need @p LR to be
|
|
|
|
/// live in are added to the LiveIn array, and the function returns false.
|
2012-07-14 07:39:05 +08:00
|
|
|
///
|
2016-08-24 21:37:55 +08:00
|
|
|
/// The array @p Undef provides the locations where the range @p LR becomes
|
|
|
|
/// undefined by <def,read-undef> operands on other subranges. If @p Undef
|
|
|
|
/// is non-empty and @p Kill is jointly dominated only by the entries of
|
|
|
|
/// @p Undef, the function returns false.
|
|
|
|
///
|
2012-07-14 07:39:05 +08:00
|
|
|
/// PhysReg, when set, is used to verify live-in lists on basic blocks.
|
2015-02-18 09:50:52 +08:00
|
|
|
bool findReachingDefs(LiveRange &LR, MachineBasicBlock &UseMBB,
|
2018-07-17 02:51:40 +08:00
|
|
|
SlotIndex Use, unsigned PhysReg,
|
2016-08-24 21:37:55 +08:00
|
|
|
ArrayRef<SlotIndex> Undefs);
|
2011-09-13 09:34:21 +08:00
|
|
|
|
|
|
|
/// updateSSA - Compute the values that will be live in to all requested
|
|
|
|
/// blocks in LiveIn. Create PHI-def values as required to preserve SSA form.
|
|
|
|
///
|
|
|
|
/// Every live-in block must be jointly dominated by the added live-out
|
|
|
|
/// blocks. No values are read from the live ranges.
|
2014-12-16 12:03:38 +08:00
|
|
|
void updateSSA();
|
2011-09-13 09:34:21 +08:00
|
|
|
|
2014-12-10 09:12:12 +08:00
|
|
|
/// Transfer information from the LiveIn vector to the live ranges and update
|
|
|
|
/// the given @p LiveOuts.
|
2014-12-16 12:03:38 +08:00
|
|
|
void updateFromLiveIns();
|
|
|
|
|
|
|
|
/// Extend the live range of @p LR to reach all uses of Reg.
|
|
|
|
///
|
2016-08-24 21:37:55 +08:00
|
|
|
/// If @p LR is a main range, or if @p LI is null, then all uses must be
|
|
|
|
/// jointly dominated by the definitions from @p LR. If @p LR is a subrange
|
|
|
|
/// of the live interval @p LI, corresponding to lane mask @p LaneMask,
|
|
|
|
/// all uses must be jointly dominated by the definitions from @p LR
|
|
|
|
/// together with definitions of other lanes where @p LR becomes undefined
|
|
|
|
/// (via <def,read-undef> operands).
|
2016-12-15 22:36:06 +08:00
|
|
|
/// If @p LR is a main range, the @p LaneMask should be set to ~0, i.e.
|
|
|
|
/// LaneBitmask::getAll().
|
2016-08-24 21:37:55 +08:00
|
|
|
void extendToUses(LiveRange &LR, unsigned Reg, LaneBitmask LaneMask,
|
|
|
|
LiveInterval *LI = nullptr);
|
2014-12-16 12:03:38 +08:00
|
|
|
|
|
|
|
/// Reset Map and Seen fields.
|
|
|
|
void resetLiveOutMap();
|
2011-09-13 09:34:21 +08:00
|
|
|
|
|
|
|
public:
|
2017-08-25 05:21:39 +08:00
|
|
|
LiveRangeCalc() = default;
|
2012-06-05 02:21:16 +08:00
|
|
|
|
2011-09-13 09:34:21 +08:00
|
|
|
//===--------------------------------------------------------------------===//
|
|
|
|
// High-level interface.
|
|
|
|
//===--------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// Calculate live ranges from scratch.
|
|
|
|
//
|
|
|
|
|
|
|
|
/// reset - Prepare caches for a new set of non-overlapping live ranges. The
|
|
|
|
/// caches must be reset before attempting calculations with a live range
|
|
|
|
/// that may overlap a previously computed live range, and before the first
|
|
|
|
/// live range in a function. If live ranges are not known to be
|
|
|
|
/// non-overlapping, call reset before each.
|
2017-08-25 05:21:39 +08:00
|
|
|
void reset(const MachineFunction *mf, SlotIndexes *SI,
|
|
|
|
MachineDominatorTree *MDT, VNInfo::Allocator *VNIA);
|
2011-09-13 09:34:21 +08:00
|
|
|
|
|
|
|
//===--------------------------------------------------------------------===//
|
|
|
|
// Mid-level interface.
|
|
|
|
//===--------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// Modify existing live ranges.
|
|
|
|
//
|
|
|
|
|
2015-02-18 09:50:52 +08:00
|
|
|
/// Extend the live range of @p LR to reach @p Use.
|
2011-09-13 09:34:21 +08:00
|
|
|
///
|
2015-02-18 09:50:52 +08:00
|
|
|
/// The existing values in @p LR must be live so they jointly dominate @p Use.
|
|
|
|
/// If @p Use is not dominated by a single existing value, PHI-defs are
|
|
|
|
/// inserted as required to preserve SSA form.
|
2012-07-14 07:39:05 +08:00
|
|
|
///
|
|
|
|
/// PhysReg, when set, is used to verify live-in lists on basic blocks.
|
2016-08-24 21:37:55 +08:00
|
|
|
void extend(LiveRange &LR, SlotIndex Use, unsigned PhysReg,
|
|
|
|
ArrayRef<SlotIndex> Undefs);
|
2014-12-16 05:36:35 +08:00
|
|
|
|
|
|
|
/// createDeadDefs - Create a dead def in LI for every def operand of Reg.
|
|
|
|
/// Each instruction defining Reg gets a new VNInfo with a corresponding
|
|
|
|
/// minimal live range.
|
|
|
|
void createDeadDefs(LiveRange &LR, unsigned Reg);
|
2012-06-06 05:54:09 +08:00
|
|
|
|
2014-12-16 12:03:38 +08:00
|
|
|
/// Extend the live range of @p LR to reach all uses of Reg.
|
2014-12-16 05:36:35 +08:00
|
|
|
///
|
|
|
|
/// All uses must be jointly dominated by existing liveness. PHI-defs are
|
|
|
|
/// inserted as needed to preserve SSA form.
|
2014-12-16 12:03:38 +08:00
|
|
|
void extendToUses(LiveRange &LR, unsigned PhysReg) {
|
2016-12-15 22:36:06 +08:00
|
|
|
extendToUses(LR, PhysReg, LaneBitmask::getAll());
|
2014-12-16 12:03:38 +08:00
|
|
|
}
|
2014-12-16 05:36:35 +08:00
|
|
|
|
2014-12-16 12:03:38 +08:00
|
|
|
/// Calculates liveness for the register specified in live interval @p LI.
|
|
|
|
/// Creates subregister live ranges as needed if subreg liveness tracking is
|
|
|
|
/// enabled.
|
2015-03-19 08:21:58 +08:00
|
|
|
void calculate(LiveInterval &LI, bool TrackSubRegs);
|
2011-09-13 09:34:21 +08:00
|
|
|
|
2016-05-21 07:14:56 +08:00
|
|
|
/// For live interval \p LI with correct SubRanges construct matching
|
|
|
|
/// information for the main live range. Expects the main live range to not
|
|
|
|
/// have any segments or value numbers.
|
|
|
|
void constructMainRangeFromSubranges(LiveInterval &LI);
|
|
|
|
|
2011-09-13 09:34:21 +08:00
|
|
|
//===--------------------------------------------------------------------===//
|
|
|
|
// Low-level interface.
|
|
|
|
//===--------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// These functions can be used to compute live ranges where the live-in and
|
|
|
|
// live-out blocks are already known, but the SSA value in each block is
|
|
|
|
// unknown.
|
|
|
|
//
|
|
|
|
// After calling reset(), add known live-out values and known live-in blocks.
|
|
|
|
// Then call calculateValues() to compute the actual value that is
|
|
|
|
// live-in to each block, and add liveness to the live ranges.
|
|
|
|
//
|
|
|
|
|
|
|
|
/// setLiveOutValue - Indicate that VNI is live out from MBB. The
|
|
|
|
/// calculateValues() function will not add liveness for MBB, the caller
|
|
|
|
/// should take care of that.
|
|
|
|
///
|
|
|
|
/// VNI may be null only if MBB is a live-through block also passed to
|
|
|
|
/// addLiveInBlock().
|
|
|
|
void setLiveOutValue(MachineBasicBlock *MBB, VNInfo *VNI) {
|
2014-12-16 12:03:38 +08:00
|
|
|
Seen.set(MBB->getNumber());
|
|
|
|
Map[MBB] = LiveOutPair(VNI, nullptr);
|
2011-09-13 09:34:21 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/// addLiveInBlock - Add a block with an unknown live-in value. This
|
|
|
|
/// function can only be called once per basic block. Once the live-in value
|
|
|
|
/// has been determined, calculateValues() will add liveness to LI.
|
|
|
|
///
|
2013-10-11 12:52:03 +08:00
|
|
|
/// @param LR The live range that is live-in to the block.
|
2011-09-13 09:34:21 +08:00
|
|
|
/// @param DomNode The domtree node for the block.
|
|
|
|
/// @param Kill Index in block where LI is killed. If the value is
|
|
|
|
/// live-through, set Kill = SLotIndex() and also call
|
|
|
|
/// setLiveOutValue(MBB, 0).
|
2013-10-11 05:28:57 +08:00
|
|
|
void addLiveInBlock(LiveRange &LR,
|
2011-09-13 09:34:21 +08:00
|
|
|
MachineDomTreeNode *DomNode,
|
|
|
|
SlotIndex Kill = SlotIndex()) {
|
2013-10-11 05:28:57 +08:00
|
|
|
LiveIn.push_back(LiveInBlock(LR, DomNode, Kill));
|
2011-09-13 09:34:21 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/// calculateValues - Calculate the value that will be live-in to each block
|
|
|
|
/// added with addLiveInBlock. Add PHI-def values as needed to preserve SSA
|
|
|
|
/// form. Add liveness to all live-in blocks up to the Kill point, or the
|
|
|
|
/// whole block for live-through blocks.
|
|
|
|
///
|
|
|
|
/// Every predecessor of a live-in block must have been given a value with
|
|
|
|
/// setLiveOutValue, the value may be null for live-trough blocks.
|
2014-12-16 12:03:38 +08:00
|
|
|
void calculateValues();
|
2018-06-26 22:37:16 +08:00
|
|
|
|
|
|
|
/// A diagnostic function to check if the end of the block @p MBB is
|
|
|
|
/// jointly dominated by the blocks corresponding to the slot indices
|
|
|
|
/// in @p Defs. This function is mainly for use in self-verification
|
|
|
|
/// checks.
|
|
|
|
LLVM_ATTRIBUTE_UNUSED
|
|
|
|
static bool isJointlyDominated(const MachineBasicBlock *MBB,
|
|
|
|
ArrayRef<SlotIndex> Defs,
|
|
|
|
const SlotIndexes &Indexes);
|
2011-09-13 09:34:21 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
} // end namespace llvm
|
|
|
|
|
2017-08-25 05:21:39 +08:00
|
|
|
#endif // LLVM_LIB_CODEGEN_LIVERANGECALC_H
|