1 //=--- RegUsageInfoPropagate.cpp - Register Usage Informartion Propagation --=//
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7 //===----------------------------------------------------------------------===//
9 /// This pass is required to take advantage of the interprocedural register
10 /// allocation infrastructure.
12 /// This pass iterates through MachineInstrs in a given MachineFunction and at
13 /// each callsite queries RegisterUsageInfo for RegMask (calculated based on
14 /// actual register allocation) of the callee function, if the RegMask detail
15 /// is available then this pass will update the RegMask of the call instruction.
16 /// This updated RegMask will be used by the register allocator while allocating
17 /// the current MachineFunction.
19 //===----------------------------------------------------------------------===//
21 #include "llvm/CodeGen/RegUsageInfoPropagate.h"
22 #include "llvm/CodeGen/MachineBasicBlock.h"
23 #include "llvm/CodeGen/MachineFrameInfo.h"
24 #include "llvm/CodeGen/MachineFunctionPass.h"
25 #include "llvm/CodeGen/MachineInstr.h"
26 #include "llvm/CodeGen/MachineRegisterInfo.h"
27 #include "llvm/CodeGen/Passes.h"
28 #include "llvm/CodeGen/RegisterUsageInfo.h"
29 #include "llvm/IR/Analysis.h"
30 #include "llvm/IR/Module.h"
31 #include "llvm/Pass.h"
32 #include "llvm/Support/Debug.h"
33 #include "llvm/Support/raw_ostream.h"
37 #define DEBUG_TYPE "ip-regalloc"
39 #define RUIP_NAME "Register Usage Information Propagation"
43 class RegUsageInfoPropagation
{
45 explicit RegUsageInfoPropagation(PhysicalRegisterUsageInfo
*PRUI
)
48 bool run(MachineFunction
&MF
);
51 PhysicalRegisterUsageInfo
*PRUI
;
53 static void setRegMask(MachineInstr
&MI
, ArrayRef
<uint32_t> RegMask
) {
54 assert(RegMask
.size() ==
55 MachineOperand::getRegMaskSize(MI
.getParent()->getParent()
56 ->getRegInfo().getTargetRegisterInfo()
58 && "expected register mask size");
59 for (MachineOperand
&MO
: MI
.operands()) {
61 MO
.setRegMask(RegMask
.data());
66 class RegUsageInfoPropagationLegacy
: public MachineFunctionPass
{
69 RegUsageInfoPropagationLegacy() : MachineFunctionPass(ID
) {
70 PassRegistry
&Registry
= *PassRegistry::getPassRegistry();
71 initializeRegUsageInfoPropagationLegacyPass(Registry
);
74 StringRef
getPassName() const override
{ return RUIP_NAME
; }
76 bool runOnMachineFunction(MachineFunction
&MF
) override
;
78 void getAnalysisUsage(AnalysisUsage
&AU
) const override
{
79 AU
.addRequired
<PhysicalRegisterUsageInfoWrapperLegacy
>();
81 MachineFunctionPass::getAnalysisUsage(AU
);
85 } // end of anonymous namespace
87 INITIALIZE_PASS_BEGIN(RegUsageInfoPropagationLegacy
, "reg-usage-propagation",
88 RUIP_NAME
, false, false)
89 INITIALIZE_PASS_DEPENDENCY(PhysicalRegisterUsageInfoWrapperLegacy
)
90 INITIALIZE_PASS_END(RegUsageInfoPropagationLegacy
, "reg-usage-propagation",
91 RUIP_NAME
, false, false)
93 char RegUsageInfoPropagationLegacy::ID
= 0;
95 // Assumes call instructions have a single reference to a function.
96 static const Function
*findCalledFunction(const Module
&M
,
97 const MachineInstr
&MI
) {
98 for (const MachineOperand
&MO
: MI
.operands()) {
100 return dyn_cast
<const Function
>(MO
.getGlobal());
103 return M
.getFunction(MO
.getSymbolName());
109 bool RegUsageInfoPropagationLegacy::runOnMachineFunction(MachineFunction
&MF
) {
110 PhysicalRegisterUsageInfo
*PRUI
=
111 &getAnalysis
<PhysicalRegisterUsageInfoWrapperLegacy
>().getPRUI();
113 RegUsageInfoPropagation
RUIP(PRUI
);
118 RegUsageInfoPropagationPass::run(MachineFunction
&MF
,
119 MachineFunctionAnalysisManager
&MFAM
) {
120 Module
&MFA
= *MF
.getFunction().getParent();
121 auto *PRUI
= MFAM
.getResult
<ModuleAnalysisManagerMachineFunctionProxy
>(MF
)
122 .getCachedResult
<PhysicalRegisterUsageAnalysis
>(MFA
);
123 assert(PRUI
&& "PhysicalRegisterUsageAnalysis not available");
124 RegUsageInfoPropagation(PRUI
).run(MF
);
125 return PreservedAnalyses::all();
128 bool RegUsageInfoPropagation::run(MachineFunction
&MF
) {
129 const Module
&M
= *MF
.getFunction().getParent();
131 LLVM_DEBUG(dbgs() << " ++++++++++++++++++++ " << RUIP_NAME
132 << " ++++++++++++++++++++ \n");
133 LLVM_DEBUG(dbgs() << "MachineFunction : " << MF
.getName() << "\n");
135 const MachineFrameInfo
&MFI
= MF
.getFrameInfo();
136 if (!MFI
.hasCalls() && !MFI
.hasTailCall())
139 bool Changed
= false;
141 for (MachineBasicBlock
&MBB
: MF
) {
142 for (MachineInstr
&MI
: MBB
) {
147 << "Call Instruction Before Register Usage Info Propagation : \n"
150 auto UpdateRegMask
= [&](const Function
&F
) {
151 const ArrayRef
<uint32_t> RegMask
= PRUI
->getRegUsageInfo(F
);
154 setRegMask(MI
, RegMask
);
158 if (const Function
*F
= findCalledFunction(M
, MI
)) {
159 if (F
->isDefinitionExact()) {
162 LLVM_DEBUG(dbgs() << "Function definition is not exact\n");
165 LLVM_DEBUG(dbgs() << "Failed to find call target function\n");
170 << "Call Instruction After Register Usage Info Propagation : \n"
176 dbgs() << " +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++"
181 FunctionPass
*llvm::createRegUsageInfoPropPass() {
182 return new RegUsageInfoPropagationLegacy();