2016-06-11 02:37:21 +08:00
|
|
|
//=--- RegUsageInfoPropagate.cpp - Register Usage Informartion Propagation --=//
|
|
|
|
//
|
|
|
|
// 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 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"
|
|
|
|
#include "llvm/CodeGen/MachineFunctionPass.h"
|
2017-08-24 15:55:13 +08:00
|
|
|
#include "llvm/CodeGen/MachineFrameInfo.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"
|
|
|
|
#include "llvm/PassAnalysisSupport.h"
|
|
|
|
#include "llvm/Support/Debug.h"
|
|
|
|
#include "llvm/Support/raw_ostream.h"
|
|
|
|
#include "llvm/Target/TargetMachine.h"
|
|
|
|
#include <map>
|
|
|
|
#include <string>
|
|
|
|
|
|
|
|
namespace llvm {
|
|
|
|
void initializeRegUsageInfoPropagationPassPass(PassRegistry &);
|
|
|
|
}
|
|
|
|
|
|
|
|
using namespace llvm;
|
|
|
|
|
|
|
|
#define DEBUG_TYPE "ip-regalloc"
|
|
|
|
|
|
|
|
#define RUIP_NAME "Register Usage Information Propagation"
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
class RegUsageInfoPropagationPass : public MachineFunctionPass {
|
|
|
|
|
|
|
|
public:
|
|
|
|
RegUsageInfoPropagationPass() : MachineFunctionPass(ID) {
|
|
|
|
PassRegistry &Registry = *PassRegistry::getPassRegistry();
|
|
|
|
initializeRegUsageInfoPropagationPassPass(Registry);
|
|
|
|
}
|
|
|
|
|
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;
|
|
|
|
|
|
|
|
void getAnalysisUsage(AnalysisUsage &AU) const override;
|
|
|
|
|
|
|
|
static char ID;
|
|
|
|
|
|
|
|
private:
|
|
|
|
static void setRegMask(MachineInstr &MI, const uint32_t *RegMask) {
|
|
|
|
for (MachineOperand &MO : MI.operands()) {
|
|
|
|
if (MO.isRegMask())
|
|
|
|
MO.setRegMask(RegMask);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
} // end of anonymous namespace
|
|
|
|
char RegUsageInfoPropagationPass::ID = 0;
|
|
|
|
|
|
|
|
INITIALIZE_PASS_BEGIN(RegUsageInfoPropagationPass, "reg-usage-propagation",
|
|
|
|
RUIP_NAME, false, false)
|
|
|
|
INITIALIZE_PASS_DEPENDENCY(PhysicalRegisterUsageInfo)
|
|
|
|
INITIALIZE_PASS_END(RegUsageInfoPropagationPass, "reg-usage-propagation",
|
|
|
|
RUIP_NAME, false, false)
|
|
|
|
|
|
|
|
FunctionPass *llvm::createRegUsageInfoPropPass() {
|
|
|
|
return new RegUsageInfoPropagationPass();
|
|
|
|
}
|
|
|
|
|
|
|
|
void RegUsageInfoPropagationPass::getAnalysisUsage(AnalysisUsage &AU) const {
|
|
|
|
AU.addRequired<PhysicalRegisterUsageInfo>();
|
|
|
|
AU.setPreservesAll();
|
|
|
|
MachineFunctionPass::getAnalysisUsage(AU);
|
|
|
|
}
|
|
|
|
|
2017-08-24 15:55:15 +08:00
|
|
|
// Assumes call instructions have a single reference to a function.
|
|
|
|
static const Function *findCalledFunction(const Module &M, MachineInstr &MI) {
|
|
|
|
for (MachineOperand &MO : MI.operands()) {
|
|
|
|
if (MO.isGlobal())
|
|
|
|
return dyn_cast<Function>(MO.getGlobal());
|
|
|
|
|
|
|
|
if (MO.isSymbol())
|
|
|
|
return M.getFunction(MO.getSymbolName());
|
|
|
|
}
|
|
|
|
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2016-06-11 02:37:21 +08:00
|
|
|
bool RegUsageInfoPropagationPass::runOnMachineFunction(MachineFunction &MF) {
|
2017-12-16 06:22:58 +08:00
|
|
|
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()
|
|
|
|
<< "Call Instruction Before Register Usage Info Propagation : \n");
|
|
|
|
LLVM_DEBUG(dbgs() << MI << "\n");
|
2016-06-11 02:37:21 +08:00
|
|
|
|
|
|
|
auto UpdateRegMask = [&](const Function *F) {
|
|
|
|
const auto *RegMask = PRUI->getRegUsageInfo(F);
|
|
|
|
if (!RegMask)
|
|
|
|
return;
|
|
|
|
setRegMask(MI, &(*RegMask)[0]);
|
|
|
|
Changed = true;
|
|
|
|
};
|
|
|
|
|
2017-08-24 15:55:15 +08:00
|
|
|
if (const Function *F = findCalledFunction(*M, MI)) {
|
|
|
|
UpdateRegMask(F);
|
|
|
|
} 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(
|
|
|
|
dbgs() << "Call Instruction After Register Usage Info Propagation : "
|
|
|
|
<< 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;
|
|
|
|
}
|