2017-01-20 01:16:14 +01:00
|
|
|
//===- llvm/CodeGen/LiveRegUnits.h - Register Unit Set ----------*- C++ -*-===//
|
|
|
|
//
|
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
|
2017-01-20 01:16:14 +01:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
/// \file
|
|
|
|
/// A set of register units. It is intended for register liveness tracking.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#ifndef LLVM_CODEGEN_LIVEREGUNITS_H
|
|
|
|
#define LLVM_CODEGEN_LIVEREGUNITS_H
|
|
|
|
|
|
|
|
#include "llvm/ADT/BitVector.h"
|
2020-09-08 18:21:28 +02:00
|
|
|
#include "llvm/CodeGen/MachineInstrBundle.h"
|
2017-11-17 02:07:10 +01:00
|
|
|
#include "llvm/CodeGen/TargetRegisterInfo.h"
|
2017-02-17 22:43:25 +01:00
|
|
|
#include "llvm/MC/LaneBitmask.h"
|
|
|
|
#include "llvm/MC/MCRegisterInfo.h"
|
|
|
|
#include <cstdint>
|
2017-01-20 01:16:14 +01:00
|
|
|
|
|
|
|
namespace llvm {
|
|
|
|
|
|
|
|
class MachineInstr;
|
|
|
|
class MachineBasicBlock;
|
|
|
|
|
|
|
|
/// A set of register units used to track register liveness.
|
|
|
|
class LiveRegUnits {
|
2017-02-17 22:43:25 +01:00
|
|
|
const TargetRegisterInfo *TRI = nullptr;
|
2017-01-20 01:16:14 +01:00
|
|
|
BitVector Units;
|
|
|
|
|
|
|
|
public:
|
|
|
|
/// Constructs a new empty LiveRegUnits set.
|
2017-02-17 22:43:25 +01:00
|
|
|
LiveRegUnits() = default;
|
2017-01-20 01:16:14 +01:00
|
|
|
|
|
|
|
/// Constructs and initialize an empty LiveRegUnits set.
|
|
|
|
LiveRegUnits(const TargetRegisterInfo &TRI) {
|
|
|
|
init(TRI);
|
|
|
|
}
|
|
|
|
|
[CodeGen] Use RegUnits to track register aliases (NFC)
Summary: Use RegUnits to track register aliases in PostRASink and AArch64LoadStoreOptimizer.
Reviewers: thegameg, mcrosier, gberry, qcolombet, sebpop, MatzeB, t.p.northover, javed.absar
Reviewed By: thegameg, sebpop
Subscribers: javed.absar, llvm-commits, kristof.beyls
Differential Revision: https://reviews.llvm.org/D45695
llvm-svn: 331066
2018-04-27 20:44:37 +02:00
|
|
|
/// For a machine instruction \p MI, adds all register units used in
|
|
|
|
/// \p UsedRegUnits and defined or clobbered in \p ModifiedRegUnits. This is
|
|
|
|
/// useful when walking over a range of instructions to track registers
|
|
|
|
/// used or defined seperately.
|
|
|
|
static void accumulateUsedDefed(const MachineInstr &MI,
|
|
|
|
LiveRegUnits &ModifiedRegUnits,
|
|
|
|
LiveRegUnits &UsedRegUnits,
|
|
|
|
const TargetRegisterInfo *TRI) {
|
|
|
|
for (ConstMIBundleOperands O(MI); O.isValid(); ++O) {
|
|
|
|
if (O->isRegMask())
|
|
|
|
ModifiedRegUnits.addRegsInMask(O->getRegMask());
|
|
|
|
if (!O->isReg())
|
|
|
|
continue;
|
2019-08-13 02:55:24 +02:00
|
|
|
Register Reg = O->getReg();
|
|
|
|
if (!Reg.isPhysical())
|
[CodeGen] Use RegUnits to track register aliases (NFC)
Summary: Use RegUnits to track register aliases in PostRASink and AArch64LoadStoreOptimizer.
Reviewers: thegameg, mcrosier, gberry, qcolombet, sebpop, MatzeB, t.p.northover, javed.absar
Reviewed By: thegameg, sebpop
Subscribers: javed.absar, llvm-commits, kristof.beyls
Differential Revision: https://reviews.llvm.org/D45695
llvm-svn: 331066
2018-04-27 20:44:37 +02:00
|
|
|
continue;
|
|
|
|
if (O->isDef()) {
|
|
|
|
// Some architectures (e.g. AArch64 XZR/WZR) have registers that are
|
|
|
|
// constant and may be used as destinations to indicate the generated
|
|
|
|
// value is discarded. No need to track such case as a def.
|
|
|
|
if (!TRI->isConstantPhysReg(Reg))
|
|
|
|
ModifiedRegUnits.addReg(Reg);
|
|
|
|
} else {
|
|
|
|
assert(O->isUse() && "Reg operand not a def and not a use");
|
|
|
|
UsedRegUnits.addReg(Reg);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-01-20 01:16:14 +01:00
|
|
|
/// Initialize and clear the set.
|
|
|
|
void init(const TargetRegisterInfo &TRI) {
|
|
|
|
this->TRI = &TRI;
|
|
|
|
Units.reset();
|
|
|
|
Units.resize(TRI.getNumRegUnits());
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Clears the set.
|
|
|
|
void clear() { Units.reset(); }
|
|
|
|
|
|
|
|
/// Returns true if the set is empty.
|
2017-09-08 18:29:50 +02:00
|
|
|
bool empty() const { return Units.none(); }
|
2017-01-20 01:16:14 +01:00
|
|
|
|
|
|
|
/// Adds register units covered by physical register \p Reg.
|
2018-11-06 20:00:11 +01:00
|
|
|
void addReg(MCPhysReg Reg) {
|
2017-01-20 01:16:14 +01:00
|
|
|
for (MCRegUnitIterator Unit(Reg, TRI); Unit.isValid(); ++Unit)
|
|
|
|
Units.set(*Unit);
|
|
|
|
}
|
|
|
|
|
2018-05-01 17:54:18 +02:00
|
|
|
/// Adds register units covered by physical register \p Reg that are
|
2017-01-20 01:16:14 +01:00
|
|
|
/// part of the lanemask \p Mask.
|
2018-11-06 20:00:11 +01:00
|
|
|
void addRegMasked(MCPhysReg Reg, LaneBitmask Mask) {
|
2017-01-20 01:16:14 +01:00
|
|
|
for (MCRegUnitMaskIterator Unit(Reg, TRI); Unit.isValid(); ++Unit) {
|
|
|
|
LaneBitmask UnitMask = (*Unit).second;
|
|
|
|
if (UnitMask.none() || (UnitMask & Mask).any())
|
|
|
|
Units.set((*Unit).first);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Removes all register units covered by physical register \p Reg.
|
2018-11-06 20:00:11 +01:00
|
|
|
void removeReg(MCPhysReg Reg) {
|
2017-01-20 01:16:14 +01:00
|
|
|
for (MCRegUnitIterator Unit(Reg, TRI); Unit.isValid(); ++Unit)
|
|
|
|
Units.reset(*Unit);
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Removes register units not preserved by the regmask \p RegMask.
|
|
|
|
/// The regmask has the same format as the one in the RegMask machine operand.
|
|
|
|
void removeRegsNotPreserved(const uint32_t *RegMask);
|
|
|
|
|
2017-01-21 03:21:04 +01:00
|
|
|
/// Adds register units not preserved by the regmask \p RegMask.
|
|
|
|
/// The regmask has the same format as the one in the RegMask machine operand.
|
|
|
|
void addRegsInMask(const uint32_t *RegMask);
|
|
|
|
|
2017-01-20 01:16:14 +01:00
|
|
|
/// Returns true if no part of physical register \p Reg is live.
|
2018-11-06 20:00:11 +01:00
|
|
|
bool available(MCPhysReg Reg) const {
|
2017-01-20 01:16:14 +01:00
|
|
|
for (MCRegUnitIterator Unit(Reg, TRI); Unit.isValid(); ++Unit) {
|
|
|
|
if (Units.test(*Unit))
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Updates liveness when stepping backwards over the instruction \p MI.
|
2017-07-07 05:02:18 +02:00
|
|
|
/// This removes all register units defined or clobbered in \p MI and then
|
|
|
|
/// adds the units used (as in use operands) in \p MI.
|
2017-01-20 01:16:14 +01:00
|
|
|
void stepBackward(const MachineInstr &MI);
|
|
|
|
|
2017-07-07 05:02:17 +02:00
|
|
|
/// Adds all register units used, defined or clobbered in \p MI.
|
|
|
|
/// This is useful when walking over a range of instruction to find registers
|
|
|
|
/// unused over the whole range.
|
|
|
|
void accumulate(const MachineInstr &MI);
|
2017-01-21 03:21:04 +01:00
|
|
|
|
2017-01-20 01:16:14 +01:00
|
|
|
/// Adds registers living out of block \p MBB.
|
|
|
|
/// Live out registers are the union of the live-in registers of the successor
|
|
|
|
/// blocks and pristine registers. Live out registers of the end block are the
|
|
|
|
/// callee saved registers.
|
|
|
|
void addLiveOuts(const MachineBasicBlock &MBB);
|
|
|
|
|
|
|
|
/// Adds registers living into block \p MBB.
|
|
|
|
void addLiveIns(const MachineBasicBlock &MBB);
|
|
|
|
|
|
|
|
/// Adds all register units marked in the bitvector \p RegUnits.
|
|
|
|
void addUnits(const BitVector &RegUnits) {
|
|
|
|
Units |= RegUnits;
|
|
|
|
}
|
|
|
|
/// Removes all register units marked in the bitvector \p RegUnits.
|
|
|
|
void removeUnits(const BitVector &RegUnits) {
|
|
|
|
Units.reset(RegUnits);
|
|
|
|
}
|
|
|
|
/// Return the internal bitvector representation of the set.
|
|
|
|
const BitVector &getBitVector() const {
|
|
|
|
return Units;
|
|
|
|
}
|
2017-09-08 18:29:50 +02:00
|
|
|
|
|
|
|
private:
|
|
|
|
/// Adds pristine registers. Pristine registers are callee saved registers
|
|
|
|
/// that are unused in the function.
|
|
|
|
void addPristines(const MachineFunction &MF);
|
2017-01-20 01:16:14 +01:00
|
|
|
};
|
|
|
|
|
2019-12-11 10:27:01 +01:00
|
|
|
/// Returns an iterator range over all physical register and mask operands for
|
|
|
|
/// \p MI and bundled instructions. This also skips any debug operands.
|
|
|
|
inline iterator_range<filter_iterator<
|
|
|
|
ConstMIBundleOperands, std::function<bool(const MachineOperand &)>>>
|
|
|
|
phys_regs_and_masks(const MachineInstr &MI) {
|
|
|
|
std::function<bool(const MachineOperand &)> Pred =
|
|
|
|
[](const MachineOperand &MOP) {
|
|
|
|
return MOP.isRegMask() || (MOP.isReg() && !MOP.isDebug() &&
|
|
|
|
Register::isPhysicalRegister(MOP.getReg()));
|
|
|
|
};
|
|
|
|
return make_filter_range(const_mi_bundle_ops(MI), Pred);
|
|
|
|
}
|
|
|
|
|
2017-02-17 22:43:25 +01:00
|
|
|
} // end namespace llvm
|
2017-01-20 01:16:14 +01:00
|
|
|
|
2017-02-17 22:43:25 +01:00
|
|
|
#endif // LLVM_CODEGEN_LIVEREGUNITS_H
|