Run DCE after a LoopFlatten test to reduce spurious output [nfc]
[llvm-project.git] / llvm / lib / CodeGen / RegUsageInfoPropagate.cpp
blobd356962e0d78a24ad353c2b7fb137cd3e5fb38a0
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 //===----------------------------------------------------------------------===//
21 #include "llvm/CodeGen/MachineBasicBlock.h"
22 #include "llvm/CodeGen/MachineFrameInfo.h"
23 #include "llvm/CodeGen/MachineFunctionPass.h"
24 #include "llvm/CodeGen/MachineInstr.h"
25 #include "llvm/CodeGen/MachineRegisterInfo.h"
26 #include "llvm/CodeGen/Passes.h"
27 #include "llvm/CodeGen/RegisterUsageInfo.h"
28 #include "llvm/IR/Module.h"
29 #include "llvm/Pass.h"
30 #include "llvm/Support/Debug.h"
31 #include "llvm/Support/raw_ostream.h"
33 using namespace llvm;
35 #define DEBUG_TYPE "ip-regalloc"
37 #define RUIP_NAME "Register Usage Information Propagation"
39 namespace {
41 class RegUsageInfoPropagation : public MachineFunctionPass {
42 public:
43 RegUsageInfoPropagation() : MachineFunctionPass(ID) {
44 PassRegistry &Registry = *PassRegistry::getPassRegistry();
45 initializeRegUsageInfoPropagationPass(Registry);
48 StringRef getPassName() const override { return RUIP_NAME; }
50 bool runOnMachineFunction(MachineFunction &MF) override;
52 void getAnalysisUsage(AnalysisUsage &AU) const override {
53 AU.addRequired<PhysicalRegisterUsageInfo>();
54 AU.setPreservesAll();
55 MachineFunctionPass::getAnalysisUsage(AU);
58 static char ID;
60 private:
61 static void setRegMask(MachineInstr &MI, ArrayRef<uint32_t> RegMask) {
62 assert(RegMask.size() ==
63 MachineOperand::getRegMaskSize(MI.getParent()->getParent()
64 ->getRegInfo().getTargetRegisterInfo()
65 ->getNumRegs())
66 && "expected register mask size");
67 for (MachineOperand &MO : MI.operands()) {
68 if (MO.isRegMask())
69 MO.setRegMask(RegMask.data());
74 } // end of anonymous namespace
76 INITIALIZE_PASS_BEGIN(RegUsageInfoPropagation, "reg-usage-propagation",
77 RUIP_NAME, false, false)
78 INITIALIZE_PASS_DEPENDENCY(PhysicalRegisterUsageInfo)
79 INITIALIZE_PASS_END(RegUsageInfoPropagation, "reg-usage-propagation",
80 RUIP_NAME, false, false)
82 char RegUsageInfoPropagation::ID = 0;
84 // Assumes call instructions have a single reference to a function.
85 static const Function *findCalledFunction(const Module &M,
86 const MachineInstr &MI) {
87 for (const MachineOperand &MO : MI.operands()) {
88 if (MO.isGlobal())
89 return dyn_cast<const Function>(MO.getGlobal());
91 if (MO.isSymbol())
92 return M.getFunction(MO.getSymbolName());
95 return nullptr;
98 bool RegUsageInfoPropagation::runOnMachineFunction(MachineFunction &MF) {
99 const Module &M = *MF.getFunction().getParent();
100 PhysicalRegisterUsageInfo *PRUI = &getAnalysis<PhysicalRegisterUsageInfo>();
102 LLVM_DEBUG(dbgs() << " ++++++++++++++++++++ " << getPassName()
103 << " ++++++++++++++++++++ \n");
104 LLVM_DEBUG(dbgs() << "MachineFunction : " << MF.getName() << "\n");
106 const MachineFrameInfo &MFI = MF.getFrameInfo();
107 if (!MFI.hasCalls() && !MFI.hasTailCall())
108 return false;
110 bool Changed = false;
112 for (MachineBasicBlock &MBB : MF) {
113 for (MachineInstr &MI : MBB) {
114 if (!MI.isCall())
115 continue;
116 LLVM_DEBUG(
117 dbgs()
118 << "Call Instruction Before Register Usage Info Propagation : \n"
119 << MI << "\n");
121 auto UpdateRegMask = [&](const Function &F) {
122 const ArrayRef<uint32_t> RegMask = PRUI->getRegUsageInfo(F);
123 if (RegMask.empty())
124 return;
125 setRegMask(MI, RegMask);
126 Changed = true;
129 if (const Function *F = findCalledFunction(M, MI)) {
130 if (F->isDefinitionExact()) {
131 UpdateRegMask(*F);
132 } else {
133 LLVM_DEBUG(dbgs() << "Function definition is not exact\n");
135 } else {
136 LLVM_DEBUG(dbgs() << "Failed to find call target function\n");
139 LLVM_DEBUG(
140 dbgs()
141 << "Call Instruction After Register Usage Info Propagation : \n"
142 << MI << '\n');
146 LLVM_DEBUG(
147 dbgs() << " +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++"
148 "++++++ \n");
149 return Changed;
152 FunctionPass *llvm::createRegUsageInfoPropPass() {
153 return new RegUsageInfoPropagation();