LLVM 20.0.0git
RegUsageInfoPropagate.cpp
Go to the documentation of this file.
1//=--- RegUsageInfoPropagate.cpp - Register Usage Informartion Propagation --=//
2//
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
6//
7//===----------------------------------------------------------------------===//
8///
9/// This pass is required to take advantage of the interprocedural register
10/// allocation infrastructure.
11///
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.
18///
19//===----------------------------------------------------------------------===//
20
27#include "llvm/CodeGen/Passes.h"
29#include "llvm/IR/Analysis.h"
30#include "llvm/IR/Module.h"
31#include "llvm/Pass.h"
32#include "llvm/Support/Debug.h"
34
35using namespace llvm;
36
37#define DEBUG_TYPE "ip-regalloc"
38
39#define RUIP_NAME "Register Usage Information Propagation"
40
41namespace {
42
43class RegUsageInfoPropagation {
44public:
45 explicit RegUsageInfoPropagation(PhysicalRegisterUsageInfo *PRUI)
46 : PRUI(PRUI) {}
47
48 bool run(MachineFunction &MF);
49
50private:
52
53 static void setRegMask(MachineInstr &MI, ArrayRef<uint32_t> RegMask) {
54 assert(RegMask.size() ==
55 MachineOperand::getRegMaskSize(MI.getParent()->getParent()
56 ->getRegInfo().getTargetRegisterInfo()
57 ->getNumRegs())
58 && "expected register mask size");
59 for (MachineOperand &MO : MI.operands()) {
60 if (MO.isRegMask())
61 MO.setRegMask(RegMask.data());
62 }
63 }
64};
65
66class RegUsageInfoPropagationLegacy : public MachineFunctionPass {
67public:
68 static char ID;
69 RegUsageInfoPropagationLegacy() : MachineFunctionPass(ID) {
72 }
73
74 StringRef getPassName() const override { return RUIP_NAME; }
75
76 bool runOnMachineFunction(MachineFunction &MF) override;
77
78 void getAnalysisUsage(AnalysisUsage &AU) const override {
80 AU.setPreservesAll();
82 }
83};
84
85} // end of anonymous namespace
86
87INITIALIZE_PASS_BEGIN(RegUsageInfoPropagationLegacy, "reg-usage-propagation",
88 RUIP_NAME, false, false)
90INITIALIZE_PASS_END(RegUsageInfoPropagationLegacy, "reg-usage-propagation",
92
93char RegUsageInfoPropagationLegacy::ID = 0;
94
95// Assumes call instructions have a single reference to a function.
98 for (const MachineOperand &MO : MI.operands()) {
99 if (MO.isGlobal())
100 return dyn_cast<const Function>(MO.getGlobal());
101
102 if (MO.isSymbol())
103 return M.getFunction(MO.getSymbolName());
104 }
105
106 return nullptr;
107}
108
109bool RegUsageInfoPropagationLegacy::runOnMachineFunction(MachineFunction &MF) {
111 &getAnalysis<PhysicalRegisterUsageInfoWrapperLegacy>().getPRUI();
112
113 RegUsageInfoPropagation RUIP(PRUI);
114 return RUIP.run(MF);
115}
116
120 Module &MFA = *MF.getFunction().getParent();
122 .getCachedResult<PhysicalRegisterUsageAnalysis>(MFA);
123 assert(PRUI && "PhysicalRegisterUsageAnalysis not available");
124 RegUsageInfoPropagation(PRUI).run(MF);
125 return PreservedAnalyses::all();
126}
127
128bool RegUsageInfoPropagation::run(MachineFunction &MF) {
129 const Module &M = *MF.getFunction().getParent();
130
131 LLVM_DEBUG(dbgs() << " ++++++++++++++++++++ " << RUIP_NAME
132 << " ++++++++++++++++++++ \n");
133 LLVM_DEBUG(dbgs() << "MachineFunction : " << MF.getName() << "\n");
134
135 const MachineFrameInfo &MFI = MF.getFrameInfo();
136 if (!MFI.hasCalls() && !MFI.hasTailCall())
137 return false;
138
139 bool Changed = false;
140
141 for (MachineBasicBlock &MBB : MF) {
142 for (MachineInstr &MI : MBB) {
143 if (!MI.isCall())
144 continue;
146 dbgs()
147 << "Call Instruction Before Register Usage Info Propagation : \n"
148 << MI << "\n");
149
150 auto UpdateRegMask = [&](const Function &F) {
151 const ArrayRef<uint32_t> RegMask = PRUI->getRegUsageInfo(F);
152 if (RegMask.empty())
153 return;
154 setRegMask(MI, RegMask);
155 Changed = true;
156 };
157
158 if (const Function *F = findCalledFunction(M, MI)) {
159 if (F->isDefinitionExact()) {
160 UpdateRegMask(*F);
161 } else {
162 LLVM_DEBUG(dbgs() << "Function definition is not exact\n");
163 }
164 } else {
165 LLVM_DEBUG(dbgs() << "Failed to find call target function\n");
166 }
167
169 dbgs()
170 << "Call Instruction After Register Usage Info Propagation : \n"
171 << MI << '\n');
172 }
173 }
174
176 dbgs() << " +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++"
177 "++++++ \n");
178 return Changed;
179}
180
182 return new RegUsageInfoPropagationLegacy();
183}
aarch64 promote const
MachineBasicBlock & MBB
#define LLVM_DEBUG(...)
Definition: Debug.h:106
IRTranslator LLVM IR MI
Module.h This file contains the declarations for the Module class.
#define F(x, y, z)
Definition: MD5.cpp:55
#define INITIALIZE_PASS_DEPENDENCY(depName)
Definition: PassSupport.h:55
#define INITIALIZE_PASS_END(passName, arg, name, cfg, analysis)
Definition: PassSupport.h:57
#define INITIALIZE_PASS_BEGIN(passName, arg, name, cfg, analysis)
Definition: PassSupport.h:52
reg usage propagation
#define RUIP_NAME
static const Function * findCalledFunction(const Module &M, const MachineInstr &MI)
This pass is required to take advantage of the interprocedural register allocation infrastructure.
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
A container for analyses that lazily runs them and caches their results.
Definition: PassManager.h:253
PassT::Result & getResult(IRUnitT &IR, ExtraArgTs... ExtraArgs)
Get the result of an analysis pass for a given IR unit.
Definition: PassManager.h:410
Represent the analysis usage information of a pass.
AnalysisUsage & addRequired()
void setPreservesAll()
Set by analyses that do not transform their input at all.
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory),...
Definition: ArrayRef.h:41
size_t size() const
size - Get the array size.
Definition: ArrayRef.h:168
bool empty() const
empty - Check if the array is empty.
Definition: ArrayRef.h:163
const T * data() const
Definition: ArrayRef.h:165
FunctionPass class - This class is used to implement most global optimizations.
Definition: Pass.h:310
Module * getParent()
Get the module that this global value is contained inside of...
Definition: GlobalValue.h:656
The MachineFrameInfo class represents an abstract stack frame until prolog/epilog code is inserted.
bool hasCalls() const
Return true if the current function has any function calls.
bool hasTailCall() const
Returns true if the function contains a tail call.
MachineFunctionPass - This class adapts the FunctionPass interface to allow convenient creation of pa...
void getAnalysisUsage(AnalysisUsage &AU) const override
getAnalysisUsage - Subclasses that override getAnalysisUsage must call this.
virtual bool runOnMachineFunction(MachineFunction &MF)=0
runOnMachineFunction - This method must be overloaded to perform the desired machine code transformat...
StringRef getName() const
getName - Return the name of the corresponding LLVM function.
MachineFrameInfo & getFrameInfo()
getFrameInfo - Return the frame info object for the current function.
Function & getFunction()
Return the LLVM function that this machine code represents.
Representation of each machine instruction.
Definition: MachineInstr.h:69
MachineOperand class - Representation of each machine instruction operand.
static unsigned getRegMaskSize(unsigned NumRegs)
Returns number of elements needed for a regmask array.
A Module instance is used to store all the information related to an LLVM module.
Definition: Module.h:65
An analysis over an "inner" IR unit that provides access to an analysis manager over a "outer" IR uni...
Definition: PassManager.h:692
PassRegistry - This class manages the registration and intitialization of the pass subsystem as appli...
Definition: PassRegistry.h:37
static PassRegistry * getPassRegistry()
getPassRegistry - Access the global registry object, which is automatically initialized at applicatio...
virtual StringRef getPassName() const
getPassName - Return a nice clean name for a pass.
Definition: Pass.cpp:81
ArrayRef< uint32_t > getRegUsageInfo(const Function &FP)
To query stored RegMask for given Function *, it will returns ane empty array if function is not know...
A set of analyses that are preserved following a run of a transformation pass.
Definition: Analysis.h:111
static PreservedAnalyses all()
Construct a special preserved set that preserves all passes.
Definition: Analysis.h:117
PreservedAnalyses run(MachineFunction &MF, MachineFunctionAnalysisManager &MFAM)
A global registry used in conjunction with static constructors to make pluggable components (like tar...
Definition: Registry.h:44
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:51
Pass manager infrastructure for declaring and invalidating analyses.
unsigned ID
LLVM IR allows to use arbitrary numbers as calling convention identifiers.
Definition: CallingConv.h:24
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
Definition: Debug.cpp:163
FunctionPass * createRegUsageInfoPropPass()
Return a MachineFunction pass that identifies call sites and propagates register usage information of...
void initializeRegUsageInfoPropagationLegacyPass(PassRegistry &)