2016-07-12 02:45:49 +08:00
|
|
|
//===-- RegUsageInfoCollector.cpp - Register Usage Information Collector --===//
|
2016-06-11 00:19:46 +08:00
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
///
|
|
|
|
/// 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 07:39:34 +08:00
|
|
|
#include "llvm/ADT/Statistic.h"
|
2016-06-11 00:19:46 +08: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-04 06:32:11 +08:00
|
|
|
#include "llvm/CodeGen/TargetFrameLowering.h"
|
2016-06-11 00:19:46 +08:00
|
|
|
|
|
|
|
using namespace llvm;
|
|
|
|
|
|
|
|
#define DEBUG_TYPE "ip-regalloc"
|
|
|
|
|
2016-07-14 07:39:34 +08:00
|
|
|
STATISTIC(NumCSROpt,
|
|
|
|
"Number of functions optimized for callee saved registers");
|
|
|
|
|
2016-06-11 00:19:46 +08:00
|
|
|
namespace llvm {
|
|
|
|
void initializeRegUsageInfoCollectorPass(PassRegistry &);
|
|
|
|
}
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
class RegUsageInfoCollector : public MachineFunctionPass {
|
|
|
|
public:
|
|
|
|
RegUsageInfoCollector() : MachineFunctionPass(ID) {
|
|
|
|
PassRegistry &Registry = *PassRegistry::getPassRegistry();
|
|
|
|
initializeRegUsageInfoCollectorPass(Registry);
|
|
|
|
}
|
|
|
|
|
2016-10-01 10:56:57 +08:00
|
|
|
StringRef getPassName() const override {
|
2016-06-11 00:19:46 +08:00
|
|
|
return "Register Usage Information Collector Pass";
|
|
|
|
}
|
|
|
|
|
|
|
|
void getAnalysisUsage(AnalysisUsage &AU) const override;
|
|
|
|
|
|
|
|
bool runOnMachineFunction(MachineFunction &MF) override;
|
|
|
|
|
|
|
|
static char ID;
|
|
|
|
};
|
|
|
|
} // 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();
|
|
|
|
}
|
|
|
|
|
|
|
|
void RegUsageInfoCollector::getAnalysisUsage(AnalysisUsage &AU) const {
|
|
|
|
AU.addRequired<PhysicalRegisterUsageInfo>();
|
|
|
|
AU.setPreservesAll();
|
|
|
|
MachineFunctionPass::getAnalysisUsage(AU);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool RegUsageInfoCollector::runOnMachineFunction(MachineFunction &MF) {
|
|
|
|
MachineRegisterInfo *MRI = &MF.getRegInfo();
|
2016-06-12 21:32:23 +08:00
|
|
|
const TargetRegisterInfo *TRI = MF.getSubtarget().getRegisterInfo();
|
2016-06-11 00:19:46 +08:00
|
|
|
const TargetMachine &TM = MF.getTarget();
|
|
|
|
|
|
|
|
DEBUG(dbgs() << " -------------------- " << getPassName()
|
|
|
|
<< " -------------------- \n");
|
|
|
|
DEBUG(dbgs() << "Function Name : " << MF.getName() << "\n");
|
|
|
|
|
|
|
|
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.
|
2016-06-16 05:14:02 +08:00
|
|
|
unsigned RegMaskSize = (TRI->getNumRegs() + 31) / 32;
|
|
|
|
RegMask.resize(RegMaskSize, 0xFFFFFFFF);
|
2016-06-11 00:19:46 +08:00
|
|
|
|
2017-12-16 06:22:58 +08:00
|
|
|
const Function &F = MF.getFunction();
|
2016-07-14 07:39:34 +08:00
|
|
|
|
2016-06-11 00:19:46 +08:00
|
|
|
PhysicalRegisterUsageInfo *PRUI = &getAnalysis<PhysicalRegisterUsageInfo>();
|
|
|
|
|
|
|
|
PRUI->setTargetMachine(&TM);
|
|
|
|
|
|
|
|
DEBUG(dbgs() << "Clobbered Registers: ");
|
2016-07-12 02:45:49 +08:00
|
|
|
|
2017-03-14 05:42:53 +08: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) {
|
|
|
|
// If a register is in the UsedPhysRegsMask set then mark it as defined.
|
|
|
|
// All it's aliases will also be in the set, so we can skip setting
|
|
|
|
// as defined all the aliases here.
|
|
|
|
if (UsedPhysRegsMask.test(PReg)) {
|
|
|
|
SetRegAsDefined(PReg);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
// If a register is defined by an instruction mark it as defined together
|
|
|
|
// with all it's aliases.
|
|
|
|
if (!MRI->def_empty(PReg)) {
|
|
|
|
for (MCRegAliasIterator AI(PReg, TRI, true); AI.isValid(); ++AI)
|
|
|
|
SetRegAsDefined(*AI);
|
|
|
|
}
|
|
|
|
}
|
2016-06-11 00:19:46 +08:00
|
|
|
|
2016-07-14 07:39:34 +08:00
|
|
|
if (!TargetFrameLowering::isSafeForNoCSROpt(F)) {
|
|
|
|
const uint32_t *CallPreservedMask =
|
2017-12-16 06:22:58 +08:00
|
|
|
TRI->getCallPreservedMask(MF, F.getCallingConv());
|
2017-08-05 15:50:18 +08:00
|
|
|
if (CallPreservedMask) {
|
|
|
|
// Set callee saved register as preserved.
|
|
|
|
for (unsigned i = 0; i < RegMaskSize; ++i)
|
|
|
|
RegMask[i] = RegMask[i] | CallPreservedMask[i];
|
|
|
|
}
|
2016-07-14 07:39:34 +08:00
|
|
|
} else {
|
|
|
|
++NumCSROpt;
|
|
|
|
DEBUG(dbgs() << MF.getName()
|
|
|
|
<< " function optimized for not having CSR.\n");
|
|
|
|
}
|
2016-06-16 05:14:02 +08:00
|
|
|
|
|
|
|
for (unsigned PReg = 1, PRegE = TRI->getNumRegs(); PReg < PRegE; ++PReg)
|
2016-06-11 00:19:46 +08:00
|
|
|
if (MachineOperand::clobbersPhysReg(&(RegMask[0]), PReg))
|
2017-12-01 00:12:24 +08:00
|
|
|
DEBUG(dbgs() << printReg(PReg, TRI) << " ");
|
2016-06-11 00:19:46 +08:00
|
|
|
|
|
|
|
DEBUG(dbgs() << " \n----------------------------------------\n");
|
|
|
|
|
2017-12-16 06:22:58 +08:00
|
|
|
PRUI->storeUpdateRegUsageInfo(&F, std::move(RegMask));
|
2016-06-11 00:19:46 +08:00
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|