2017-02-17 22:43:25 +01:00
|
|
|
//===- LiveRegUnits.cpp - Register Unit Set -------------------------------===//
|
2017-01-20 01:16:14 +01: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
|
2017-01-20 01:16:14 +01:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
/// \file This file imlements the LiveRegUnits set.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "llvm/CodeGen/LiveRegUnits.h"
|
2017-02-17 22:43:25 +01:00
|
|
|
#include "llvm/CodeGen/MachineBasicBlock.h"
|
2017-01-20 01:16:14 +01:00
|
|
|
#include "llvm/CodeGen/MachineFrameInfo.h"
|
|
|
|
#include "llvm/CodeGen/MachineFunction.h"
|
2017-02-17 22:43:25 +01:00
|
|
|
#include "llvm/CodeGen/MachineOperand.h"
|
2017-06-03 02:26:35 +02:00
|
|
|
#include "llvm/CodeGen/MachineRegisterInfo.h"
|
2017-02-17 22:43:25 +01:00
|
|
|
|
2017-01-20 01:16:14 +01:00
|
|
|
using namespace llvm;
|
|
|
|
|
|
|
|
void LiveRegUnits::removeRegsNotPreserved(const uint32_t *RegMask) {
|
|
|
|
for (unsigned U = 0, E = TRI->getNumRegUnits(); U != E; ++U) {
|
|
|
|
for (MCRegUnitRootIterator RootReg(U, TRI); RootReg.isValid(); ++RootReg) {
|
|
|
|
if (MachineOperand::clobbersPhysReg(RegMask, *RootReg))
|
|
|
|
Units.reset(U);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-01-21 03:21:04 +01:00
|
|
|
void LiveRegUnits::addRegsInMask(const uint32_t *RegMask) {
|
|
|
|
for (unsigned U = 0, E = TRI->getNumRegUnits(); U != E; ++U) {
|
|
|
|
for (MCRegUnitRootIterator RootReg(U, TRI); RootReg.isValid(); ++RootReg) {
|
|
|
|
if (MachineOperand::clobbersPhysReg(RegMask, *RootReg))
|
|
|
|
Units.set(U);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-01-20 01:16:14 +01:00
|
|
|
void LiveRegUnits::stepBackward(const MachineInstr &MI) {
|
|
|
|
// Remove defined registers and regmask kills from the set.
|
2019-12-11 10:27:01 +01:00
|
|
|
for (const MachineOperand &MOP : phys_regs_and_masks(MI)) {
|
|
|
|
if (MOP.isRegMask()) {
|
|
|
|
removeRegsNotPreserved(MOP.getRegMask());
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (MOP.isDef())
|
|
|
|
removeReg(MOP.getReg());
|
2017-01-20 01:16:14 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Add uses to the set.
|
2019-12-11 10:27:01 +01:00
|
|
|
for (const MachineOperand &MOP : phys_regs_and_masks(MI)) {
|
|
|
|
if (!MOP.isReg() || !MOP.readsReg())
|
2017-01-20 01:16:14 +01:00
|
|
|
continue;
|
2019-12-11 10:27:01 +01:00
|
|
|
addReg(MOP.getReg());
|
2017-01-20 01:16:14 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-07-07 05:02:17 +02:00
|
|
|
void LiveRegUnits::accumulate(const MachineInstr &MI) {
|
2017-01-21 03:21:04 +01:00
|
|
|
// Add defs, uses and regmask clobbers to the set.
|
2019-12-11 10:27:01 +01:00
|
|
|
for (const MachineOperand &MOP : phys_regs_and_masks(MI)) {
|
|
|
|
if (MOP.isRegMask()) {
|
|
|
|
addRegsInMask(MOP.getRegMask());
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (!MOP.isDef() && !MOP.readsReg())
|
|
|
|
continue;
|
|
|
|
addReg(MOP.getReg());
|
2017-01-21 03:21:04 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-01-20 01:16:14 +01:00
|
|
|
/// Add live-in registers of basic block \p MBB to \p LiveUnits.
|
2017-06-03 02:26:35 +02:00
|
|
|
static void addBlockLiveIns(LiveRegUnits &LiveUnits,
|
|
|
|
const MachineBasicBlock &MBB) {
|
2017-01-20 01:16:14 +01:00
|
|
|
for (const auto &LI : MBB.liveins())
|
|
|
|
LiveUnits.addRegMasked(LI.PhysReg, LI.LaneMask);
|
|
|
|
}
|
|
|
|
|
2017-06-03 02:26:35 +02:00
|
|
|
/// Adds all callee saved registers to \p LiveUnits.
|
|
|
|
static void addCalleeSavedRegs(LiveRegUnits &LiveUnits,
|
|
|
|
const MachineFunction &MF) {
|
|
|
|
const MachineRegisterInfo &MRI = MF.getRegInfo();
|
|
|
|
for (const MCPhysReg *CSR = MRI.getCalleeSavedRegs(); CSR && *CSR; ++CSR)
|
|
|
|
LiveUnits.addReg(*CSR);
|
2017-01-20 01:16:14 +01:00
|
|
|
}
|
|
|
|
|
2017-09-08 18:29:50 +02:00
|
|
|
void LiveRegUnits::addPristines(const MachineFunction &MF) {
|
2017-06-03 02:26:35 +02:00
|
|
|
const MachineFrameInfo &MFI = MF.getFrameInfo();
|
|
|
|
if (!MFI.isCalleeSavedInfoValid())
|
|
|
|
return;
|
2017-09-08 18:29:50 +02:00
|
|
|
/// This function will usually be called on an empty object, handle this
|
|
|
|
/// as a special case.
|
|
|
|
if (empty()) {
|
|
|
|
/// Add all callee saved regs, then remove the ones that are saved and
|
|
|
|
/// restored.
|
|
|
|
addCalleeSavedRegs(*this, MF);
|
|
|
|
/// Remove the ones that are not saved/restored; they are pristine.
|
|
|
|
for (const CalleeSavedInfo &Info : MFI.getCalleeSavedInfo())
|
|
|
|
removeReg(Info.getReg());
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
/// If a callee-saved register that is not pristine is already present
|
|
|
|
/// in the set, we should make sure that it stays in it. Precompute the
|
|
|
|
/// set of pristine registers in a separate object.
|
2017-06-03 02:26:35 +02:00
|
|
|
/// Add all callee saved regs, then remove the ones that are saved+restored.
|
2017-09-08 18:29:50 +02:00
|
|
|
LiveRegUnits Pristine(*TRI);
|
|
|
|
addCalleeSavedRegs(Pristine, MF);
|
2017-06-03 02:26:35 +02:00
|
|
|
/// Remove the ones that are not saved/restored; they are pristine.
|
2017-01-20 01:16:14 +01:00
|
|
|
for (const CalleeSavedInfo &Info : MFI.getCalleeSavedInfo())
|
2017-09-08 18:29:50 +02:00
|
|
|
Pristine.removeReg(Info.getReg());
|
|
|
|
addUnits(Pristine.getBitVector());
|
2017-01-20 01:16:14 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void LiveRegUnits::addLiveOuts(const MachineBasicBlock &MBB) {
|
|
|
|
const MachineFunction &MF = *MBB.getParent();
|
2019-02-01 10:23:51 +01:00
|
|
|
|
|
|
|
addPristines(MF);
|
|
|
|
|
|
|
|
// To get the live-outs we simply merge the live-ins of all successors.
|
|
|
|
for (const MachineBasicBlock *Succ : MBB.successors())
|
|
|
|
addBlockLiveIns(*this, *Succ);
|
|
|
|
|
|
|
|
// For the return block: Add all callee saved registers.
|
|
|
|
if (MBB.isReturnBlock()) {
|
2017-06-03 02:26:35 +02:00
|
|
|
const MachineFrameInfo &MFI = MF.getFrameInfo();
|
|
|
|
if (MFI.isCalleeSavedInfoValid())
|
|
|
|
addCalleeSavedRegs(*this, MF);
|
2017-01-20 01:16:14 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void LiveRegUnits::addLiveIns(const MachineBasicBlock &MBB) {
|
|
|
|
const MachineFunction &MF = *MBB.getParent();
|
2017-09-08 18:29:50 +02:00
|
|
|
addPristines(MF);
|
2017-06-03 02:26:35 +02:00
|
|
|
addBlockLiveIns(*this, MBB);
|
2017-01-20 01:16:14 +01:00
|
|
|
}
|