2016-07-11 18:45:49 +00:00
|
|
|
//===-- RegUsageInfoCollector.cpp - Register Usage Information Collector --===//
|
2016-06-10 16:19:46 +00:00
|
|
|
//
|
2019-01-19 08:50:56 +00: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
|
2016-06-10 16:19:46 +00:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
///
|
|
|
|
/// This pass is required to take advantage of the interprocedural register
|
|
|
|
/// allocation infrastructure.
|
|
|
|
///
|
|
|
|
/// This pass is simple MachineFunction pass which collects register usage
|
|
|
|
/// details by iterating through each physical registers and checking
|
|
|
|
/// MRI::isPhysRegUsed() then creates a RegMask based on this details.
|
|
|
|
/// The pass then stores this RegMask in PhysicalRegisterUsageInfo.cpp
|
|
|
|
///
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2024-11-15 12:00:09 +05:30
|
|
|
#include "llvm/CodeGen/RegUsageInfoCollector.h"
|
2016-07-13 23:39:34 +00:00
|
|
|
#include "llvm/ADT/Statistic.h"
|
2016-06-10 16:19:46 +00:00
|
|
|
#include "llvm/CodeGen/MachineFunctionPass.h"
|
|
|
|
#include "llvm/CodeGen/MachineOperand.h"
|
2024-11-15 12:00:09 +05:30
|
|
|
#include "llvm/CodeGen/MachinePassManager.h"
|
2016-06-10 16:19:46 +00:00
|
|
|
#include "llvm/CodeGen/MachineRegisterInfo.h"
|
|
|
|
#include "llvm/CodeGen/Passes.h"
|
|
|
|
#include "llvm/CodeGen/RegisterUsageInfo.h"
|
2022-03-15 10:54:19 +01:00
|
|
|
#include "llvm/CodeGen/TargetFrameLowering.h"
|
|
|
|
#include "llvm/IR/Function.h"
|
2016-06-10 16:19:46 +00:00
|
|
|
#include "llvm/Support/Debug.h"
|
|
|
|
#include "llvm/Support/raw_ostream.h"
|
|
|
|
|
|
|
|
using namespace llvm;
|
|
|
|
|
|
|
|
#define DEBUG_TYPE "ip-regalloc"
|
|
|
|
|
2016-07-13 23:39:34 +00:00
|
|
|
STATISTIC(NumCSROpt,
|
|
|
|
"Number of functions optimized for callee saved registers");
|
|
|
|
|
2016-06-10 16:19:46 +00:00
|
|
|
namespace {
|
2018-07-26 00:27:51 +00:00
|
|
|
|
2024-11-15 12:00:09 +05:30
|
|
|
class RegUsageInfoCollector {
|
|
|
|
PhysicalRegisterUsageInfo &PRUI;
|
|
|
|
|
|
|
|
public:
|
|
|
|
RegUsageInfoCollector(PhysicalRegisterUsageInfo &PRUI) : PRUI(PRUI) {}
|
|
|
|
bool run(MachineFunction &MF);
|
|
|
|
|
|
|
|
// Call getCalleeSaves and then also set the bits for subregs and
|
|
|
|
// fully saved superregs.
|
|
|
|
static void computeCalleeSavedRegs(BitVector &SavedRegs, MachineFunction &MF);
|
|
|
|
};
|
|
|
|
|
|
|
|
class RegUsageInfoCollectorLegacy : public MachineFunctionPass {
|
2016-06-10 16:19:46 +00:00
|
|
|
public:
|
2024-11-15 12:00:09 +05:30
|
|
|
static char ID;
|
|
|
|
RegUsageInfoCollectorLegacy() : MachineFunctionPass(ID) {
|
|
|
|
initializeRegUsageInfoCollectorLegacyPass(*PassRegistry::getPassRegistry());
|
2016-06-10 16:19:46 +00:00
|
|
|
}
|
|
|
|
|
2016-10-01 02:56:57 +00:00
|
|
|
StringRef getPassName() const override {
|
2016-06-10 16:19:46 +00:00
|
|
|
return "Register Usage Information Collector Pass";
|
|
|
|
}
|
|
|
|
|
2018-07-26 00:27:51 +00:00
|
|
|
void getAnalysisUsage(AnalysisUsage &AU) const override {
|
2024-11-15 10:49:00 +05:30
|
|
|
AU.addRequired<PhysicalRegisterUsageInfoWrapperLegacy>();
|
2018-07-26 00:27:51 +00:00
|
|
|
AU.setPreservesAll();
|
|
|
|
MachineFunctionPass::getAnalysisUsage(AU);
|
|
|
|
}
|
2016-06-10 16:19:46 +00:00
|
|
|
|
|
|
|
bool runOnMachineFunction(MachineFunction &MF) override;
|
|
|
|
};
|
|
|
|
} // end of anonymous namespace
|
|
|
|
|
2024-11-15 12:00:09 +05:30
|
|
|
char RegUsageInfoCollectorLegacy::ID = 0;
|
2016-06-10 16:19:46 +00:00
|
|
|
|
2024-11-15 12:00:09 +05:30
|
|
|
INITIALIZE_PASS_BEGIN(RegUsageInfoCollectorLegacy, "RegUsageInfoCollector",
|
2016-06-10 16:19:46 +00:00
|
|
|
"Register Usage Information Collector", false, false)
|
2024-11-15 10:49:00 +05:30
|
|
|
INITIALIZE_PASS_DEPENDENCY(PhysicalRegisterUsageInfoWrapperLegacy)
|
2024-11-15 12:00:09 +05:30
|
|
|
INITIALIZE_PASS_END(RegUsageInfoCollectorLegacy, "RegUsageInfoCollector",
|
2016-06-10 16:19:46 +00:00
|
|
|
"Register Usage Information Collector", false, false)
|
|
|
|
|
|
|
|
FunctionPass *llvm::createRegUsageInfoCollector() {
|
2024-11-15 12:00:09 +05:30
|
|
|
return new RegUsageInfoCollectorLegacy();
|
2016-06-10 16:19:46 +00:00
|
|
|
}
|
|
|
|
|
2019-07-05 23:33:43 +00:00
|
|
|
// TODO: Move to hook somwehere?
|
|
|
|
|
|
|
|
// Return true if it is useful to track the used registers for IPRA / no CSR
|
|
|
|
// optimizations. This is not useful for entry points, and computing the
|
|
|
|
// register usage information is expensive.
|
|
|
|
static bool isCallableFunction(const MachineFunction &MF) {
|
|
|
|
switch (MF.getFunction().getCallingConv()) {
|
|
|
|
case CallingConv::AMDGPU_VS:
|
|
|
|
case CallingConv::AMDGPU_GS:
|
|
|
|
case CallingConv::AMDGPU_PS:
|
|
|
|
case CallingConv::AMDGPU_CS:
|
2019-07-11 14:41:40 +00:00
|
|
|
case CallingConv::AMDGPU_HS:
|
|
|
|
case CallingConv::AMDGPU_ES:
|
|
|
|
case CallingConv::AMDGPU_LS:
|
2019-07-05 23:33:43 +00:00
|
|
|
case CallingConv::AMDGPU_KERNEL:
|
|
|
|
return false;
|
|
|
|
default:
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-11-15 12:00:09 +05:30
|
|
|
PreservedAnalyses
|
|
|
|
RegUsageInfoCollectorPass::run(MachineFunction &MF,
|
|
|
|
MachineFunctionAnalysisManager &MFAM) {
|
|
|
|
Module &MFA = *MF.getFunction().getParent();
|
|
|
|
auto *PRUI = MFAM.getResult<ModuleAnalysisManagerMachineFunctionProxy>(MF)
|
|
|
|
.getCachedResult<PhysicalRegisterUsageAnalysis>(MFA);
|
|
|
|
assert(PRUI && "PhysicalRegisterUsageAnalysis not available");
|
|
|
|
RegUsageInfoCollector(*PRUI).run(MF);
|
|
|
|
return PreservedAnalyses::all();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool RegUsageInfoCollectorLegacy::runOnMachineFunction(MachineFunction &MF) {
|
|
|
|
PhysicalRegisterUsageInfo &PRUI =
|
|
|
|
getAnalysis<PhysicalRegisterUsageInfoWrapperLegacy>().getPRUI();
|
|
|
|
return RegUsageInfoCollector(PRUI).run(MF);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool RegUsageInfoCollector::run(MachineFunction &MF) {
|
2016-06-10 16:19:46 +00:00
|
|
|
MachineRegisterInfo *MRI = &MF.getRegInfo();
|
2016-06-12 13:32:23 +00:00
|
|
|
const TargetRegisterInfo *TRI = MF.getSubtarget().getRegisterInfo();
|
Overhaul the TargetMachine and LLVMTargetMachine Classes (#111234)
Following discussions in #110443, and the following earlier discussions
in https://lists.llvm.org/pipermail/llvm-dev/2017-October/117907.html,
https://reviews.llvm.org/D38482, https://reviews.llvm.org/D38489, this
PR attempts to overhaul the `TargetMachine` and `LLVMTargetMachine`
interface classes. More specifically:
1. Makes `TargetMachine` the only class implemented under
`TargetMachine.h` in the `Target` library.
2. `TargetMachine` contains target-specific interface functions that
relate to IR/CodeGen/MC constructs, whereas before (at least on paper)
it was supposed to have only IR/MC constructs. Any Target that doesn't
want to use the independent code generator simply does not implement
them, and returns either `false` or `nullptr`.
3. Renames `LLVMTargetMachine` to `CodeGenCommonTMImpl`. This renaming
aims to make the purpose of `LLVMTargetMachine` clearer. Its interface
was moved under the CodeGen library, to further emphasis its usage in
Targets that use CodeGen directly.
4. Makes `TargetMachine` the only interface used across LLVM and its
projects. With these changes, `CodeGenCommonTMImpl` is simply a set of
shared function implementations of `TargetMachine`, and CodeGen users
don't need to static cast to `LLVMTargetMachine` every time they need a
CodeGen-specific feature of the `TargetMachine`.
5. More importantly, does not change any requirements regarding library
linking.
cc @arsenm @aeubanks
2024-11-14 21:30:05 +00:00
|
|
|
const TargetMachine &TM = MF.getTarget();
|
2016-06-10 16:19:46 +00:00
|
|
|
|
2024-11-15 12:00:09 +05:30
|
|
|
LLVM_DEBUG(
|
|
|
|
dbgs()
|
|
|
|
<< " -------------------- Register Usage Information Collector Pass"
|
|
|
|
<< " -------------------- \nFunction Name : " << MF.getName() << '\n');
|
2019-07-05 23:33:43 +00:00
|
|
|
|
|
|
|
// Analyzing the register usage may be expensive on some targets.
|
|
|
|
if (!isCallableFunction(MF)) {
|
|
|
|
LLVM_DEBUG(dbgs() << "Not analyzing non-callable function\n");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// If there are no callers, there's no point in computing more precise
|
|
|
|
// register usage here.
|
|
|
|
if (MF.getFunction().use_empty()) {
|
|
|
|
LLVM_DEBUG(dbgs() << "Not analyzing function with no callers\n");
|
|
|
|
return false;
|
|
|
|
}
|
2016-06-10 16:19:46 +00:00
|
|
|
|
|
|
|
std::vector<uint32_t> RegMask;
|
|
|
|
|
|
|
|
// Compute the size of the bit vector to represent all the registers.
|
|
|
|
// The bit vector is broken into 32-bit chunks, thus takes the ceil of
|
|
|
|
// the number of registers divided by 32 for the size.
|
2018-07-26 00:27:47 +00:00
|
|
|
unsigned RegMaskSize = MachineOperand::getRegMaskSize(TRI->getNumRegs());
|
2018-07-26 00:27:51 +00:00
|
|
|
RegMask.resize(RegMaskSize, ~((uint32_t)0));
|
2016-06-10 16:19:46 +00:00
|
|
|
|
2017-12-15 22:22:58 +00:00
|
|
|
const Function &F = MF.getFunction();
|
2016-07-13 23:39:34 +00:00
|
|
|
|
2018-07-26 00:27:51 +00:00
|
|
|
PRUI.setTargetMachine(TM);
|
2016-06-10 16:19:46 +00:00
|
|
|
|
2018-05-14 12:53:11 +00:00
|
|
|
LLVM_DEBUG(dbgs() << "Clobbered Registers: ");
|
2016-07-11 18:45:49 +00:00
|
|
|
|
2018-05-25 08:42:02 +00:00
|
|
|
BitVector SavedRegs;
|
|
|
|
computeCalleeSavedRegs(SavedRegs, MF);
|
|
|
|
|
2017-03-13 21:42:53 +00:00
|
|
|
const BitVector &UsedPhysRegsMask = MRI->getUsedPhysRegsMask();
|
|
|
|
auto SetRegAsDefined = [&RegMask] (unsigned Reg) {
|
|
|
|
RegMask[Reg / 32] &= ~(1u << Reg % 32);
|
|
|
|
};
|
2019-08-05 09:04:10 +00:00
|
|
|
|
2024-06-17 13:42:03 +01:00
|
|
|
// Don't include $noreg in any regmasks.
|
|
|
|
SetRegAsDefined(MCRegister::NoRegister);
|
|
|
|
|
2019-08-05 09:04:10 +00:00
|
|
|
// Some targets can clobber registers "inside" a call, typically in
|
|
|
|
// linker-generated code.
|
|
|
|
for (const MCPhysReg Reg : TRI->getIntraCallClobberedRegs(&MF))
|
|
|
|
for (MCRegAliasIterator AI(Reg, TRI, true); AI.isValid(); ++AI)
|
|
|
|
SetRegAsDefined(*AI);
|
|
|
|
|
2017-03-13 21:42:53 +00:00
|
|
|
// Scan all the physical registers. When a register is defined in the current
|
|
|
|
// function set it and all the aliasing registers as defined in the regmask.
|
2019-07-05 23:33:43 +00:00
|
|
|
// FIXME: Rewrite to use regunits.
|
2017-03-13 21:42:53 +00:00
|
|
|
for (unsigned PReg = 1, PRegE = TRI->getNumRegs(); PReg < PRegE; ++PReg) {
|
2018-05-25 08:42:02 +00:00
|
|
|
// Don't count registers that are saved and restored.
|
|
|
|
if (SavedRegs.test(PReg))
|
|
|
|
continue;
|
2017-03-13 21:42:53 +00:00
|
|
|
// If a register is defined by an instruction mark it as defined together
|
2018-05-25 08:42:02 +00:00
|
|
|
// with all it's unsaved aliases.
|
2017-03-13 21:42:53 +00:00
|
|
|
if (!MRI->def_empty(PReg)) {
|
|
|
|
for (MCRegAliasIterator AI(PReg, TRI, true); AI.isValid(); ++AI)
|
2018-05-25 08:42:02 +00:00
|
|
|
if (!SavedRegs.test(*AI))
|
|
|
|
SetRegAsDefined(*AI);
|
2018-05-04 07:50:05 +00:00
|
|
|
continue;
|
2017-03-13 21:42:53 +00:00
|
|
|
}
|
2018-05-04 07:50:05 +00:00
|
|
|
// If a register is in the UsedPhysRegsMask set then mark it as defined.
|
|
|
|
// All clobbered aliases will also be in the set, so we can skip setting
|
|
|
|
// as defined all the aliases here.
|
|
|
|
if (UsedPhysRegsMask.test(PReg))
|
|
|
|
SetRegAsDefined(PReg);
|
2017-03-13 21:42:53 +00:00
|
|
|
}
|
2016-06-10 16:19:46 +00:00
|
|
|
|
2019-08-02 10:23:17 +00:00
|
|
|
if (TargetFrameLowering::isSafeForNoCSROpt(F) &&
|
|
|
|
MF.getSubtarget().getFrameLowering()->isProfitableForNoCSROpt(F)) {
|
2016-07-13 23:39:34 +00:00
|
|
|
++NumCSROpt;
|
2018-05-14 12:53:11 +00:00
|
|
|
LLVM_DEBUG(dbgs() << MF.getName()
|
|
|
|
<< " function optimized for not having CSR.\n");
|
2016-07-13 23:39:34 +00:00
|
|
|
}
|
2016-06-15 21:14:02 +00:00
|
|
|
|
2019-07-05 23:33:43 +00:00
|
|
|
LLVM_DEBUG(
|
|
|
|
for (unsigned PReg = 1, PRegE = TRI->getNumRegs(); PReg < PRegE; ++PReg) {
|
|
|
|
if (MachineOperand::clobbersPhysReg(&(RegMask[0]), PReg))
|
|
|
|
dbgs() << printReg(PReg, TRI) << " ";
|
|
|
|
}
|
2016-06-10 16:19:46 +00:00
|
|
|
|
2019-07-05 23:33:43 +00:00
|
|
|
dbgs() << " \n----------------------------------------\n";
|
|
|
|
);
|
2016-06-10 16:19:46 +00:00
|
|
|
|
2018-07-26 00:27:51 +00:00
|
|
|
PRUI.storeUpdateRegUsageInfo(F, RegMask);
|
2016-06-10 16:19:46 +00:00
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
2018-05-25 08:42:02 +00:00
|
|
|
|
|
|
|
void RegUsageInfoCollector::
|
|
|
|
computeCalleeSavedRegs(BitVector &SavedRegs, MachineFunction &MF) {
|
2018-07-26 00:27:51 +00:00
|
|
|
const TargetFrameLowering &TFI = *MF.getSubtarget().getFrameLowering();
|
|
|
|
const TargetRegisterInfo &TRI = *MF.getSubtarget().getRegisterInfo();
|
2018-05-25 08:42:02 +00:00
|
|
|
|
|
|
|
// Target will return the set of registers that it saves/restores as needed.
|
|
|
|
SavedRegs.clear();
|
2019-10-29 12:49:34 +00:00
|
|
|
TFI.getCalleeSaves(MF, SavedRegs);
|
2019-07-08 18:48:42 +00:00
|
|
|
if (SavedRegs.none())
|
|
|
|
return;
|
2018-05-25 08:42:02 +00:00
|
|
|
|
|
|
|
// Insert subregs.
|
2018-07-26 00:27:51 +00:00
|
|
|
const MCPhysReg *CSRegs = TRI.getCalleeSavedRegs(&MF);
|
2018-05-25 08:42:02 +00:00
|
|
|
for (unsigned i = 0; CSRegs[i]; ++i) {
|
2019-07-08 18:48:42 +00:00
|
|
|
MCPhysReg Reg = CSRegs[i];
|
|
|
|
if (SavedRegs.test(Reg)) {
|
|
|
|
// Save subregisters
|
2023-04-17 21:09:19 +01:00
|
|
|
for (MCPhysReg SR : TRI.subregs(Reg))
|
|
|
|
SavedRegs.set(SR);
|
2018-08-29 23:12:42 +00:00
|
|
|
}
|
2018-05-25 08:42:02 +00:00
|
|
|
}
|
|
|
|
}
|