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
21 #include "llvm/CodeGen/MachineBasicBlock.h"
22 #include "llvm/CodeGen/MachineFunctionPass.h"
23 #include "llvm/CodeGen/MachineFrameInfo.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"
32 #include "llvm/Target/TargetMachine.h"
33
34 using namespace llvm;
35
36 #define DEBUG_TYPE "ip-regalloc"
37
38 #define RUIP_NAME "Register Usage Information Propagation"
39
40 namespace {
41
42 class RegUsageInfoPropagation : public MachineFunctionPass {
43 public:
RegUsageInfoPropagation()44 RegUsageInfoPropagation() : MachineFunctionPass(ID) {
45 PassRegistry &Registry = *PassRegistry::getPassRegistry();
46 initializeRegUsageInfoPropagationPass(Registry);
47 }
48
getPassName() const49 StringRef getPassName() const override { return RUIP_NAME; }
50
51 bool runOnMachineFunction(MachineFunction &MF) override;
52
getAnalysisUsage(AnalysisUsage & AU) const53 void getAnalysisUsage(AnalysisUsage &AU) const override {
54 AU.addRequired<PhysicalRegisterUsageInfo>();
55 AU.setPreservesAll();
56 MachineFunctionPass::getAnalysisUsage(AU);
57 }
58
59 static char ID;
60
61 private:
setRegMask(MachineInstr & MI,ArrayRef<uint32_t> RegMask)62 static void setRegMask(MachineInstr &MI, ArrayRef<uint32_t> RegMask) {
63 assert(RegMask.size() ==
64 MachineOperand::getRegMaskSize(MI.getParent()->getParent()
65 ->getRegInfo().getTargetRegisterInfo()
66 ->getNumRegs())
67 && "expected register mask size");
68 for (MachineOperand &MO : MI.operands()) {
69 if (MO.isRegMask())
70 MO.setRegMask(RegMask.data());
71 }
72 }
73 };
74
75 } // end of anonymous namespace
76
77 INITIALIZE_PASS_BEGIN(RegUsageInfoPropagation, "reg-usage-propagation",
78 RUIP_NAME, false, false)
79 INITIALIZE_PASS_DEPENDENCY(PhysicalRegisterUsageInfo)
80 INITIALIZE_PASS_END(RegUsageInfoPropagation, "reg-usage-propagation",
81 RUIP_NAME, false, false)
82
83 char RegUsageInfoPropagation::ID = 0;
84
85 // Assumes call instructions have a single reference to a function.
findCalledFunction(const Module & M,const MachineInstr & MI)86 static const Function *findCalledFunction(const Module &M,
87 const MachineInstr &MI) {
88 for (const MachineOperand &MO : MI.operands()) {
89 if (MO.isGlobal())
90 return dyn_cast<const Function>(MO.getGlobal());
91
92 if (MO.isSymbol())
93 return M.getFunction(MO.getSymbolName());
94 }
95
96 return nullptr;
97 }
98
runOnMachineFunction(MachineFunction & MF)99 bool RegUsageInfoPropagation::runOnMachineFunction(MachineFunction &MF) {
100 const Module &M = *MF.getFunction().getParent();
101 PhysicalRegisterUsageInfo *PRUI = &getAnalysis<PhysicalRegisterUsageInfo>();
102
103 LLVM_DEBUG(dbgs() << " ++++++++++++++++++++ " << getPassName()
104 << " ++++++++++++++++++++ \n");
105 LLVM_DEBUG(dbgs() << "MachineFunction : " << MF.getName() << "\n");
106
107 const MachineFrameInfo &MFI = MF.getFrameInfo();
108 if (!MFI.hasCalls() && !MFI.hasTailCall())
109 return false;
110
111 bool Changed = false;
112
113 for (MachineBasicBlock &MBB : MF) {
114 for (MachineInstr &MI : MBB) {
115 if (!MI.isCall())
116 continue;
117 LLVM_DEBUG(
118 dbgs()
119 << "Call Instruction Before Register Usage Info Propagation : \n"
120 << MI << "\n");
121
122 auto UpdateRegMask = [&](const Function &F) {
123 const ArrayRef<uint32_t> RegMask = PRUI->getRegUsageInfo(F);
124 if (RegMask.empty())
125 return;
126 setRegMask(MI, RegMask);
127 Changed = true;
128 };
129
130 if (const Function *F = findCalledFunction(M, MI)) {
131 if (F->isDefinitionExact()) {
132 UpdateRegMask(*F);
133 } else {
134 LLVM_DEBUG(dbgs() << "Function definition is not exact\n");
135 }
136 } else {
137 LLVM_DEBUG(dbgs() << "Failed to find call target function\n");
138 }
139
140 LLVM_DEBUG(
141 dbgs()
142 << "Call Instruction After Register Usage Info Propagation : \n"
143 << MI << '\n');
144 }
145 }
146
147 LLVM_DEBUG(
148 dbgs() << " +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++"
149 "++++++ \n");
150 return Changed;
151 }
152
createRegUsageInfoPropPass()153 FunctionPass *llvm::createRegUsageInfoPropPass() {
154 return new RegUsageInfoPropagation();
155 }
156