2016-07-11 20:45:49 +02:00
|
|
|
//===-- RegUsageInfoCollector.cpp - Register Usage Information Collector --===//
|
2016-06-10 18:19:46 +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
|
2016-06-10 18:19:46 +02: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
|
|
|
|
///
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2016-07-14 01:39:34 +02:00
|
|
|
#include "llvm/ADT/Statistic.h"
|
2016-06-10 18:19:46 +02:00
|
|
|
#include "llvm/CodeGen/MachineBasicBlock.h"
|
|
|
|
#include "llvm/CodeGen/MachineFunctionPass.h"
|
|
|
|
#include "llvm/CodeGen/MachineInstr.h"
|
|
|
|
#include "llvm/CodeGen/MachineOperand.h"
|
|
|
|
#include "llvm/CodeGen/MachineRegisterInfo.h"
|
|
|
|
#include "llvm/CodeGen/Passes.h"
|
|
|
|
#include "llvm/CodeGen/RegisterUsageInfo.h"
|
|
|
|
#include "llvm/Support/Debug.h"
|
|
|
|
#include "llvm/Support/raw_ostream.h"
|
2017-11-03 23:32:11 +01:00
|
|
|
#include "llvm/CodeGen/TargetFrameLowering.h"
|
2016-06-10 18:19:46 +02:00
|
|
|
|
|
|
|
using namespace llvm;
|
|
|
|
|
|
|
|
#define DEBUG_TYPE "ip-regalloc"
|
|
|
|
|
2016-07-14 01:39:34 +02:00
|
|
|
STATISTIC(NumCSROpt,
|
|
|
|
"Number of functions optimized for callee saved registers");
|
|
|
|
|
2016-06-10 18:19:46 +02:00
|
|
|
namespace {
|
2018-07-26 02:27:51 +02:00
|
|
|
|
2016-06-10 18:19:46 +02:00
|
|
|
class RegUsageInfoCollector : public MachineFunctionPass {
|
|
|
|
public:
|
|
|
|
RegUsageInfoCollector() : MachineFunctionPass(ID) {
|
|
|
|
PassRegistry &Registry = *PassRegistry::getPassRegistry();
|
|
|
|
initializeRegUsageInfoCollectorPass(Registry);
|
|
|
|
}
|
|
|
|
|
2016-10-01 04:56:57 +02:00
|
|
|
StringRef getPassName() const override {
|
2016-06-10 18:19:46 +02:00
|
|
|
return "Register Usage Information Collector Pass";
|
|
|
|
}
|
|
|
|
|
2018-07-26 02:27:51 +02:00
|
|
|
void getAnalysisUsage(AnalysisUsage &AU) const override {
|
|
|
|
AU.addRequired<PhysicalRegisterUsageInfo>();
|
|
|
|
AU.setPreservesAll();
|
|
|
|
MachineFunctionPass::getAnalysisUsage(AU);
|
|
|
|
}
|
2016-06-10 18:19:46 +02:00
|
|
|
|
|
|
|
bool runOnMachineFunction(MachineFunction &MF) override;
|
|
|
|
|
2018-05-25 10:42:02 +02:00
|
|
|
// Call determineCalleeSaves and then also set the bits for subregs and
|
|
|
|
// fully saved superregs.
|
|
|
|
static void computeCalleeSavedRegs(BitVector &SavedRegs, MachineFunction &MF);
|
|
|
|
|
2016-06-10 18:19:46 +02:00
|
|
|
static char ID;
|
|
|
|
};
|
2018-07-26 02:27:51 +02:00
|
|
|
|
2016-06-10 18:19:46 +02:00
|
|
|
} // end of anonymous namespace
|
|
|
|
|
|
|
|
char RegUsageInfoCollector::ID = 0;
|
|
|
|
|
|
|
|
INITIALIZE_PASS_BEGIN(RegUsageInfoCollector, "RegUsageInfoCollector",
|
|
|
|
"Register Usage Information Collector", false, false)
|
|
|
|
INITIALIZE_PASS_DEPENDENCY(PhysicalRegisterUsageInfo)
|
|
|
|
INITIALIZE_PASS_END(RegUsageInfoCollector, "RegUsageInfoCollector",
|
|
|
|
"Register Usage Information Collector", false, false)
|
|
|
|
|
|
|
|
FunctionPass *llvm::createRegUsageInfoCollector() {
|
|
|
|
return new RegUsageInfoCollector();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool RegUsageInfoCollector::runOnMachineFunction(MachineFunction &MF) {
|
|
|
|
MachineRegisterInfo *MRI = &MF.getRegInfo();
|
2016-06-12 15:32:23 +02:00
|
|
|
const TargetRegisterInfo *TRI = MF.getSubtarget().getRegisterInfo();
|
2018-11-06 00:49:14 +01:00
|
|
|
const LLVMTargetMachine &TM = MF.getTarget();
|
2016-06-10 18:19:46 +02:00
|
|
|
|
2018-05-14 14:53:11 +02:00
|
|
|
LLVM_DEBUG(dbgs() << " -------------------- " << getPassName()
|
|
|
|
<< " -------------------- \n");
|
|
|
|
LLVM_DEBUG(dbgs() << "Function Name : " << MF.getName() << "\n");
|
2016-06-10 18:19:46 +02: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 02:27:47 +02:00
|
|
|
unsigned RegMaskSize = MachineOperand::getRegMaskSize(TRI->getNumRegs());
|
2018-07-26 02:27:51 +02:00
|
|
|
RegMask.resize(RegMaskSize, ~((uint32_t)0));
|
2016-06-10 18:19:46 +02:00
|
|
|
|
2017-12-15 23:22:58 +01:00
|
|
|
const Function &F = MF.getFunction();
|
2016-07-14 01:39:34 +02:00
|
|
|
|
2018-07-26 02:27:51 +02:00
|
|
|
PhysicalRegisterUsageInfo &PRUI = getAnalysis<PhysicalRegisterUsageInfo>();
|
|
|
|
PRUI.setTargetMachine(TM);
|
2016-06-10 18:19:46 +02:00
|
|
|
|
2018-05-14 14:53:11 +02:00
|
|
|
LLVM_DEBUG(dbgs() << "Clobbered Registers: ");
|
2016-07-11 20:45:49 +02:00
|
|
|
|
2018-05-25 10:42:02 +02:00
|
|
|
BitVector SavedRegs;
|
|
|
|
computeCalleeSavedRegs(SavedRegs, MF);
|
|
|
|
|
2017-03-13 22:42:53 +01:00
|
|
|
const BitVector &UsedPhysRegsMask = MRI->getUsedPhysRegsMask();
|
|
|
|
auto SetRegAsDefined = [&RegMask] (unsigned Reg) {
|
|
|
|
RegMask[Reg / 32] &= ~(1u << Reg % 32);
|
|
|
|
};
|
|
|
|
// 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.
|
|
|
|
for (unsigned PReg = 1, PRegE = TRI->getNumRegs(); PReg < PRegE; ++PReg) {
|
2018-05-25 10:42:02 +02:00
|
|
|
// Don't count registers that are saved and restored.
|
|
|
|
if (SavedRegs.test(PReg))
|
|
|
|
continue;
|
2017-03-13 22:42:53 +01:00
|
|
|
// If a register is defined by an instruction mark it as defined together
|
2018-05-25 10:42:02 +02:00
|
|
|
// with all it's unsaved aliases.
|
2017-03-13 22:42:53 +01:00
|
|
|
if (!MRI->def_empty(PReg)) {
|
|
|
|
for (MCRegAliasIterator AI(PReg, TRI, true); AI.isValid(); ++AI)
|
2018-05-25 10:42:02 +02:00
|
|
|
if (!SavedRegs.test(*AI))
|
|
|
|
SetRegAsDefined(*AI);
|
2018-05-04 09:50:05 +02:00
|
|
|
continue;
|
2017-03-13 22:42:53 +01:00
|
|
|
}
|
2018-05-04 09:50:05 +02: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 22:42:53 +01:00
|
|
|
}
|
2016-06-10 18:19:46 +02:00
|
|
|
|
2018-05-25 10:42:02 +02:00
|
|
|
if (TargetFrameLowering::isSafeForNoCSROpt(F)) {
|
2016-07-14 01:39:34 +02:00
|
|
|
++NumCSROpt;
|
2018-05-14 14:53:11 +02:00
|
|
|
LLVM_DEBUG(dbgs() << MF.getName()
|
|
|
|
<< " function optimized for not having CSR.\n");
|
2016-07-14 01:39:34 +02:00
|
|
|
}
|
2016-06-15 23:14:02 +02:00
|
|
|
|
|
|
|
for (unsigned PReg = 1, PRegE = TRI->getNumRegs(); PReg < PRegE; ++PReg)
|
2016-06-10 18:19:46 +02:00
|
|
|
if (MachineOperand::clobbersPhysReg(&(RegMask[0]), PReg))
|
2018-05-14 14:53:11 +02:00
|
|
|
LLVM_DEBUG(dbgs() << printReg(PReg, TRI) << " ");
|
2016-06-10 18:19:46 +02:00
|
|
|
|
2018-05-14 14:53:11 +02:00
|
|
|
LLVM_DEBUG(dbgs() << " \n----------------------------------------\n");
|
2016-06-10 18:19:46 +02:00
|
|
|
|
2018-07-26 02:27:51 +02:00
|
|
|
PRUI.storeUpdateRegUsageInfo(F, RegMask);
|
2016-06-10 18:19:46 +02:00
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
2018-05-25 10:42:02 +02:00
|
|
|
|
|
|
|
void RegUsageInfoCollector::
|
|
|
|
computeCalleeSavedRegs(BitVector &SavedRegs, MachineFunction &MF) {
|
2018-07-26 02:27:51 +02:00
|
|
|
const TargetFrameLowering &TFI = *MF.getSubtarget().getFrameLowering();
|
|
|
|
const TargetRegisterInfo &TRI = *MF.getSubtarget().getRegisterInfo();
|
2018-05-25 10:42:02 +02:00
|
|
|
|
|
|
|
// Target will return the set of registers that it saves/restores as needed.
|
|
|
|
SavedRegs.clear();
|
2018-07-26 02:27:51 +02:00
|
|
|
TFI.determineCalleeSaves(MF, SavedRegs);
|
2018-05-25 10:42:02 +02:00
|
|
|
|
|
|
|
// Insert subregs.
|
2018-07-26 02:27:51 +02:00
|
|
|
const MCPhysReg *CSRegs = TRI.getCalleeSavedRegs(&MF);
|
2018-05-25 10:42:02 +02:00
|
|
|
for (unsigned i = 0; CSRegs[i]; ++i) {
|
|
|
|
unsigned Reg = CSRegs[i];
|
|
|
|
if (SavedRegs.test(Reg))
|
2018-07-26 02:27:51 +02:00
|
|
|
for (MCSubRegIterator SR(Reg, &TRI, false); SR.isValid(); ++SR)
|
2018-05-25 10:42:02 +02:00
|
|
|
SavedRegs.set(*SR);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Insert any register fully saved via subregisters.
|
2018-08-30 01:12:42 +02:00
|
|
|
for (const TargetRegisterClass *RC : TRI.regclasses()) {
|
|
|
|
if (!RC->CoveredBySubRegs)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
for (unsigned PReg = 1, PRegE = TRI.getNumRegs(); PReg < PRegE; ++PReg) {
|
|
|
|
if (SavedRegs.test(PReg))
|
|
|
|
continue;
|
|
|
|
|
|
|
|
// Check if PReg is fully covered by its subregs.
|
|
|
|
if (!RC->contains(PReg))
|
|
|
|
continue;
|
|
|
|
|
|
|
|
// Add PReg to SavedRegs if all subregs are saved.
|
|
|
|
bool AllSubRegsSaved = true;
|
2019-05-29 01:43:12 +02:00
|
|
|
bool HasAtLeastOneSubreg = false;
|
|
|
|
for (MCSubRegIterator SR(PReg, &TRI, false); SR.isValid(); ++SR) {
|
|
|
|
HasAtLeastOneSubreg = true;
|
2018-08-30 01:12:42 +02:00
|
|
|
if (!SavedRegs.test(*SR)) {
|
|
|
|
AllSubRegsSaved = false;
|
|
|
|
break;
|
|
|
|
}
|
2019-05-29 01:43:12 +02:00
|
|
|
}
|
|
|
|
if (AllSubRegsSaved && HasAtLeastOneSubreg)
|
2018-08-30 01:12:42 +02:00
|
|
|
SavedRegs.set(PReg);
|
|
|
|
}
|
2018-05-25 10:42:02 +02:00
|
|
|
}
|
|
|
|
}
|