LLVM 23.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"
32#include "llvm/Pass.h"
33#include "llvm/Support/Debug.h"
35
36using namespace llvm;
37
38#define DEBUG_TYPE "ip-regalloc"
39
40#define RUIP_NAME "Register Usage Information Propagation"
41
42namespace {
43
44class RegUsageInfoPropagation {
45public:
46 explicit RegUsageInfoPropagation(PhysicalRegisterUsageInfo *PRUI)
47 : PRUI(PRUI) {}
48
49 bool run(MachineFunction &MF);
50
51private:
53
54 static void setRegMask(MachineInstr &MI, ArrayRef<uint32_t> RegMask) {
55 assert(RegMask.size() ==
56 MachineOperand::getRegMaskSize(MI.getParent()->getParent()
57 ->getRegInfo().getTargetRegisterInfo()
58 ->getNumRegs())
59 && "expected register mask size");
60 for (MachineOperand &MO : MI.operands()) {
61 if (MO.isRegMask())
62 MO.setRegMask(RegMask.data());
63 }
64 }
65};
66
67class RegUsageInfoPropagationLegacy : public MachineFunctionPass {
68public:
69 static char ID;
70 RegUsageInfoPropagationLegacy() : MachineFunctionPass(ID) {}
71
72 StringRef getPassName() const override { return RUIP_NAME; }
73
74 bool runOnMachineFunction(MachineFunction &MF) override;
75
76 void getAnalysisUsage(AnalysisUsage &AU) const override {
78 AU.setPreservesAll();
80 }
81};
82
83} // end of anonymous namespace
84
85INITIALIZE_PASS_BEGIN(RegUsageInfoPropagationLegacy, "reg-usage-propagation",
86 RUIP_NAME, false, false)
88INITIALIZE_PASS_END(RegUsageInfoPropagationLegacy, "reg-usage-propagation",
90
91char RegUsageInfoPropagationLegacy::ID = 0;
92
93// Assumes call instructions have a single reference to a function.
96 for (const MachineOperand &MO : MI.operands()) {
97 if (MO.isGlobal())
98 return dyn_cast<const Function>(MO.getGlobal());
99
100 if (MO.isSymbol())
101 return M.getFunction(MO.getSymbolName());
102 }
103
104 return nullptr;
105}
106
107bool RegUsageInfoPropagationLegacy::runOnMachineFunction(MachineFunction &MF) {
108 PhysicalRegisterUsageInfo *PRUI =
109 &getAnalysis<PhysicalRegisterUsageInfoWrapperLegacy>().getPRUI();
110
111 RegUsageInfoPropagation RUIP(PRUI);
112 return RUIP.run(MF);
113}
114
115PreservedAnalyses
118 Module &MFA = *MF.getFunction().getParent();
120 .getCachedResult<PhysicalRegisterUsageAnalysis>(MFA);
121 assert(PRUI && "PhysicalRegisterUsageAnalysis not available");
122 RegUsageInfoPropagation(PRUI).run(MF);
123 return PreservedAnalyses::all();
124}
125
126bool RegUsageInfoPropagation::run(MachineFunction &MF) {
127 const Module &M = *MF.getFunction().getParent();
128
129 LLVM_DEBUG(dbgs() << " ++++++++++++++++++++ " << RUIP_NAME
130 << " ++++++++++++++++++++ \n");
131 LLVM_DEBUG(dbgs() << "MachineFunction : " << MF.getName() << "\n");
132
133 const MachineFrameInfo &MFI = MF.getFrameInfo();
134 if (!MFI.hasCalls() && !MFI.hasTailCall())
135 return false;
136
137 bool Changed = false;
138
139 for (MachineBasicBlock &MBB : MF) {
140 for (MachineInstr &MI : MBB) {
141 if (!MI.isCall())
142 continue;
144 dbgs()
145 << "Call Instruction Before Register Usage Info Propagation : \n"
146 << MI << "\n");
147
148 auto UpdateRegMask = [&](const Function &F) {
149 const ArrayRef<uint32_t> RegMask = PRUI->getRegUsageInfo(F);
150 if (RegMask.empty())
151 return;
152 setRegMask(MI, RegMask);
153 Changed = true;
154 };
155
156 if (const Function *F = findCalledFunction(M, MI)) {
157 if (F->isDefinitionExact()) {
158 UpdateRegMask(*F);
159 } else {
160 LLVM_DEBUG(dbgs() << "Function definition is not exact\n");
161 }
162 } else {
163 LLVM_DEBUG(dbgs() << "Failed to find call target function\n");
164 }
165
167 dbgs()
168 << "Call Instruction After Register Usage Info Propagation : \n"
169 << MI << '\n');
170 }
171 }
172
174 dbgs() << " +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++"
175 "++++++ \n");
176 return Changed;
177}
178
180 return new RegUsageInfoPropagationLegacy();
181}
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
aarch64 promote const
MachineBasicBlock & MBB
IRTranslator LLVM IR MI
Module.h This file contains the declarations for the Module class.
#define F(x, y, z)
Definition MD5.cpp:54
#define INITIALIZE_PASS_DEPENDENCY(depName)
Definition PassSupport.h:42
#define INITIALIZE_PASS_END(passName, arg, name, cfg, analysis)
Definition PassSupport.h:44
#define INITIALIZE_PASS_BEGIN(passName, arg, name, cfg, analysis)
Definition PassSupport.h:39
#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.
#define LLVM_DEBUG(...)
Definition Debug.h:114
PassT::Result & getResult(IRUnitT &IR, ExtraArgTs... ExtraArgs)
Get the result of an analysis pass for a given IR unit.
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:40
size_t size() const
size - Get the array size.
Definition ArrayRef.h:142
bool empty() const
empty - Check if the array is empty.
Definition ArrayRef.h:137
const T * data() const
Definition ArrayRef.h:139
FunctionPass class - This class is used to implement most global optimizations.
Definition Pass.h:314
Module * getParent()
Get the module that this global value is contained inside of...
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.
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.
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:67
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...
static PreservedAnalyses all()
Construct a special preserved set that preserves all passes.
Definition Analysis.h:118
PreservedAnalyses run(MachineFunction &MF, MachineFunctionAnalysisManager &MFAM)
StringRef - Represent a constant reference to a string, i.e.
Definition StringRef.h:55
Changed
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 Types.h:26
OuterAnalysisManagerProxy< ModuleAnalysisManager, MachineFunction > ModuleAnalysisManagerMachineFunctionProxy
Provide the ModuleAnalysisManager to Function proxy.
AnalysisManager< MachineFunction > MachineFunctionAnalysisManager
LLVM_ABI raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
Definition Debug.cpp:207
LLVM_ABI FunctionPass * createRegUsageInfoPropPass()
Return a MachineFunction pass that identifies call sites and propagates register usage information of...