2016-06-11 02:37:21 +08:00
|
|
|
//=--- RegUsageInfoPropagate.cpp - Register Usage Informartion Propagation --=//
|
|
|
|
//
|
2019-01-19 16:50:56 +08: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-11 02:37:21 +08:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
///
|
|
|
|
/// This pass is required to take advantage of the interprocedural register
|
|
|
|
/// allocation infrastructure.
|
|
|
|
///
|
|
|
|
/// This pass iterates through MachineInstrs in a given MachineFunction and at
|
|
|
|
/// each callsite queries RegisterUsageInfo for RegMask (calculated based on
|
|
|
|
/// actual register allocation) of the callee function, if the RegMask detail
|
|
|
|
/// is available then this pass will update the RegMask of the call instruction.
|
|
|
|
/// This updated RegMask will be used by the register allocator while allocating
|
|
|
|
/// the current MachineFunction.
|
|
|
|
///
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "llvm/CodeGen/MachineBasicBlock.h"
|
2017-08-24 15:55:13 +08:00
|
|
|
#include "llvm/CodeGen/MachineFrameInfo.h"
|
2022-03-08 07:25:52 +08:00
|
|
|
#include "llvm/CodeGen/MachineFunctionPass.h"
|
2016-06-11 02:37:21 +08:00
|
|
|
#include "llvm/CodeGen/MachineInstr.h"
|
|
|
|
#include "llvm/CodeGen/MachineRegisterInfo.h"
|
|
|
|
#include "llvm/CodeGen/Passes.h"
|
|
|
|
#include "llvm/CodeGen/RegisterUsageInfo.h"
|
|
|
|
#include "llvm/IR/Module.h"
|
2020-04-26 19:57:57 +08:00
|
|
|
#include "llvm/Pass.h"
|
2016-06-11 02:37:21 +08:00
|
|
|
#include "llvm/Support/Debug.h"
|
|
|
|
#include "llvm/Support/raw_ostream.h"
|
|
|
|
|
|
|
|
using namespace llvm;
|
|
|
|
|
|
|
|
#define DEBUG_TYPE "ip-regalloc"
|
|
|
|
|
|
|
|
#define RUIP_NAME "Register Usage Information Propagation"
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
2018-07-26 08:27:51 +08:00
|
|
|
class RegUsageInfoPropagation : public MachineFunctionPass {
|
2016-06-11 02:37:21 +08:00
|
|
|
public:
|
2018-07-26 08:27:51 +08:00
|
|
|
RegUsageInfoPropagation() : MachineFunctionPass(ID) {
|
2016-06-11 02:37:21 +08:00
|
|
|
PassRegistry &Registry = *PassRegistry::getPassRegistry();
|
2018-07-26 08:27:51 +08:00
|
|
|
initializeRegUsageInfoPropagationPass(Registry);
|
2016-06-11 02:37:21 +08:00
|
|
|
}
|
|
|
|
|
2016-10-01 10:56:57 +08:00
|
|
|
StringRef getPassName() const override { return RUIP_NAME; }
|
2016-06-11 02:37:21 +08:00
|
|
|
|
|
|
|
bool runOnMachineFunction(MachineFunction &MF) override;
|
|
|
|
|
2018-07-26 08:27:51 +08:00
|
|
|
void getAnalysisUsage(AnalysisUsage &AU) const override {
|
|
|
|
AU.addRequired<PhysicalRegisterUsageInfo>();
|
|
|
|
AU.setPreservesAll();
|
|
|
|
MachineFunctionPass::getAnalysisUsage(AU);
|
|
|
|
}
|
2016-06-11 02:37:21 +08:00
|
|
|
|
|
|
|
static char ID;
|
|
|
|
|
|
|
|
private:
|
2018-07-26 08:27:51 +08:00
|
|
|
static void setRegMask(MachineInstr &MI, ArrayRef<uint32_t> RegMask) {
|
|
|
|
assert(RegMask.size() ==
|
|
|
|
MachineOperand::getRegMaskSize(MI.getParent()->getParent()
|
|
|
|
->getRegInfo().getTargetRegisterInfo()
|
|
|
|
->getNumRegs())
|
|
|
|
&& "expected register mask size");
|
2016-06-11 02:37:21 +08:00
|
|
|
for (MachineOperand &MO : MI.operands()) {
|
|
|
|
if (MO.isRegMask())
|
2018-07-26 08:27:51 +08:00
|
|
|
MO.setRegMask(RegMask.data());
|
2016-06-11 02:37:21 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
2018-07-26 08:27:51 +08:00
|
|
|
|
2016-06-11 02:37:21 +08:00
|
|
|
} // end of anonymous namespace
|
|
|
|
|
2018-07-26 08:27:51 +08:00
|
|
|
INITIALIZE_PASS_BEGIN(RegUsageInfoPropagation, "reg-usage-propagation",
|
2016-06-11 02:37:21 +08:00
|
|
|
RUIP_NAME, false, false)
|
|
|
|
INITIALIZE_PASS_DEPENDENCY(PhysicalRegisterUsageInfo)
|
2018-07-26 08:27:51 +08:00
|
|
|
INITIALIZE_PASS_END(RegUsageInfoPropagation, "reg-usage-propagation",
|
2016-06-11 02:37:21 +08:00
|
|
|
RUIP_NAME, false, false)
|
|
|
|
|
2018-07-26 08:27:51 +08:00
|
|
|
char RegUsageInfoPropagation::ID = 0;
|
2016-06-11 02:37:21 +08:00
|
|
|
|
2017-08-24 15:55:15 +08:00
|
|
|
// Assumes call instructions have a single reference to a function.
|
2018-07-26 08:27:51 +08:00
|
|
|
static const Function *findCalledFunction(const Module &M,
|
|
|
|
const MachineInstr &MI) {
|
|
|
|
for (const MachineOperand &MO : MI.operands()) {
|
2017-08-24 15:55:15 +08:00
|
|
|
if (MO.isGlobal())
|
2018-07-26 08:27:51 +08:00
|
|
|
return dyn_cast<const Function>(MO.getGlobal());
|
2017-08-24 15:55:15 +08:00
|
|
|
|
|
|
|
if (MO.isSymbol())
|
|
|
|
return M.getFunction(MO.getSymbolName());
|
|
|
|
}
|
|
|
|
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2018-07-26 08:27:51 +08:00
|
|
|
bool RegUsageInfoPropagation::runOnMachineFunction(MachineFunction &MF) {
|
|
|
|
const Module &M = *MF.getFunction().getParent();
|
2016-06-11 02:37:21 +08:00
|
|
|
PhysicalRegisterUsageInfo *PRUI = &getAnalysis<PhysicalRegisterUsageInfo>();
|
|
|
|
|
2018-05-14 20:53:11 +08:00
|
|
|
LLVM_DEBUG(dbgs() << " ++++++++++++++++++++ " << getPassName()
|
|
|
|
<< " ++++++++++++++++++++ \n");
|
|
|
|
LLVM_DEBUG(dbgs() << "MachineFunction : " << MF.getName() << "\n");
|
2016-06-11 02:37:21 +08:00
|
|
|
|
2017-08-24 15:55:13 +08:00
|
|
|
const MachineFrameInfo &MFI = MF.getFrameInfo();
|
|
|
|
if (!MFI.hasCalls() && !MFI.hasTailCall())
|
|
|
|
return false;
|
|
|
|
|
2016-06-11 02:37:21 +08:00
|
|
|
bool Changed = false;
|
|
|
|
|
|
|
|
for (MachineBasicBlock &MBB : MF) {
|
|
|
|
for (MachineInstr &MI : MBB) {
|
|
|
|
if (!MI.isCall())
|
|
|
|
continue;
|
2018-05-14 20:53:11 +08:00
|
|
|
LLVM_DEBUG(
|
|
|
|
dbgs()
|
2020-03-24 10:55:30 +08:00
|
|
|
<< "Call Instruction Before Register Usage Info Propagation : \n"
|
|
|
|
<< MI << "\n");
|
2016-06-11 02:37:21 +08:00
|
|
|
|
2018-07-26 08:27:51 +08:00
|
|
|
auto UpdateRegMask = [&](const Function &F) {
|
|
|
|
const ArrayRef<uint32_t> RegMask = PRUI->getRegUsageInfo(F);
|
|
|
|
if (RegMask.empty())
|
2016-06-11 02:37:21 +08:00
|
|
|
return;
|
2018-07-26 08:27:51 +08:00
|
|
|
setRegMask(MI, RegMask);
|
2016-06-11 02:37:21 +08:00
|
|
|
Changed = true;
|
|
|
|
};
|
|
|
|
|
2018-07-26 08:27:51 +08:00
|
|
|
if (const Function *F = findCalledFunction(M, MI)) {
|
2019-07-19 17:59:26 +08:00
|
|
|
if (F->isDefinitionExact()) {
|
|
|
|
UpdateRegMask(*F);
|
|
|
|
} else {
|
|
|
|
LLVM_DEBUG(dbgs() << "Function definition is not exact\n");
|
|
|
|
}
|
2017-08-24 15:55:15 +08:00
|
|
|
} else {
|
2018-05-14 20:53:11 +08:00
|
|
|
LLVM_DEBUG(dbgs() << "Failed to find call target function\n");
|
2017-08-24 15:55:15 +08:00
|
|
|
}
|
2016-06-11 02:37:21 +08:00
|
|
|
|
2018-05-14 20:53:11 +08:00
|
|
|
LLVM_DEBUG(
|
2020-03-24 10:55:30 +08:00
|
|
|
dbgs()
|
|
|
|
<< "Call Instruction After Register Usage Info Propagation : \n"
|
|
|
|
<< MI << '\n');
|
2016-06-11 02:37:21 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-05-14 20:53:11 +08:00
|
|
|
LLVM_DEBUG(
|
|
|
|
dbgs() << " +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++"
|
|
|
|
"++++++ \n");
|
2016-06-11 02:37:21 +08:00
|
|
|
return Changed;
|
|
|
|
}
|
2018-07-26 08:27:51 +08:00
|
|
|
|
|
|
|
FunctionPass *llvm::createRegUsageInfoPropPass() {
|
|
|
|
return new RegUsageInfoPropagation();
|
|
|
|
}
|