1*0b57cec5SDimitry Andric //=--- RegUsageInfoPropagate.cpp - Register Usage Informartion Propagation --=//
2*0b57cec5SDimitry Andric //
3*0b57cec5SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4*0b57cec5SDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
5*0b57cec5SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6*0b57cec5SDimitry Andric //
7*0b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
8*0b57cec5SDimitry Andric ///
9*0b57cec5SDimitry Andric /// This pass is required to take advantage of the interprocedural register
10*0b57cec5SDimitry Andric /// allocation infrastructure.
11*0b57cec5SDimitry Andric ///
12*0b57cec5SDimitry Andric /// This pass iterates through MachineInstrs in a given MachineFunction and at
13*0b57cec5SDimitry Andric /// each callsite queries RegisterUsageInfo for RegMask (calculated based on
14*0b57cec5SDimitry Andric /// actual register allocation) of the callee function, if the RegMask detail
15*0b57cec5SDimitry Andric /// is available then this pass will update the RegMask of the call instruction.
16*0b57cec5SDimitry Andric /// This updated RegMask will be used by the register allocator while allocating
17*0b57cec5SDimitry Andric /// the current MachineFunction.
18*0b57cec5SDimitry Andric ///
19*0b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
20*0b57cec5SDimitry Andric 
21*0b57cec5SDimitry Andric #include "llvm/CodeGen/MachineBasicBlock.h"
22*0b57cec5SDimitry Andric #include "llvm/CodeGen/MachineFrameInfo.h"
23*0b57cec5SDimitry Andric #include "llvm/CodeGen/MachineFunctionPass.h"
24*0b57cec5SDimitry Andric #include "llvm/CodeGen/MachineInstr.h"
25*0b57cec5SDimitry Andric #include "llvm/CodeGen/MachineRegisterInfo.h"
26*0b57cec5SDimitry Andric #include "llvm/CodeGen/Passes.h"
27*0b57cec5SDimitry Andric #include "llvm/CodeGen/RegisterUsageInfo.h"
28*0b57cec5SDimitry Andric #include "llvm/IR/Module.h"
29*0b57cec5SDimitry Andric #include "llvm/Pass.h"
30*0b57cec5SDimitry Andric #include "llvm/Support/Debug.h"
31*0b57cec5SDimitry Andric #include "llvm/Support/raw_ostream.h"
32*0b57cec5SDimitry Andric 
33*0b57cec5SDimitry Andric using namespace llvm;
34*0b57cec5SDimitry Andric 
35*0b57cec5SDimitry Andric #define DEBUG_TYPE "ip-regalloc"
36*0b57cec5SDimitry Andric 
37*0b57cec5SDimitry Andric #define RUIP_NAME "Register Usage Information Propagation"
38*0b57cec5SDimitry Andric 
39*0b57cec5SDimitry Andric namespace {
40*0b57cec5SDimitry Andric 
41*0b57cec5SDimitry Andric class RegUsageInfoPropagation : public MachineFunctionPass {
42*0b57cec5SDimitry Andric public:
RegUsageInfoPropagation()43*0b57cec5SDimitry Andric   RegUsageInfoPropagation() : MachineFunctionPass(ID) {
44*0b57cec5SDimitry Andric     PassRegistry &Registry = *PassRegistry::getPassRegistry();
45*0b57cec5SDimitry Andric     initializeRegUsageInfoPropagationPass(Registry);
46*0b57cec5SDimitry Andric   }
47*0b57cec5SDimitry Andric 
getPassName() const48*0b57cec5SDimitry Andric   StringRef getPassName() const override { return RUIP_NAME; }
49*0b57cec5SDimitry Andric 
50*0b57cec5SDimitry Andric   bool runOnMachineFunction(MachineFunction &MF) override;
51*0b57cec5SDimitry Andric 
getAnalysisUsage(AnalysisUsage & AU) const52*0b57cec5SDimitry Andric   void getAnalysisUsage(AnalysisUsage &AU) const override {
53*0b57cec5SDimitry Andric     AU.addRequired<PhysicalRegisterUsageInfo>();
54*0b57cec5SDimitry Andric     AU.setPreservesAll();
55*0b57cec5SDimitry Andric     MachineFunctionPass::getAnalysisUsage(AU);
56*0b57cec5SDimitry Andric   }
57*0b57cec5SDimitry Andric 
58*0b57cec5SDimitry Andric   static char ID;
59*0b57cec5SDimitry Andric 
60*0b57cec5SDimitry Andric private:
setRegMask(MachineInstr & MI,ArrayRef<uint32_t> RegMask)61*0b57cec5SDimitry Andric   static void setRegMask(MachineInstr &MI, ArrayRef<uint32_t> RegMask) {
62*0b57cec5SDimitry Andric     assert(RegMask.size() ==
63*0b57cec5SDimitry Andric            MachineOperand::getRegMaskSize(MI.getParent()->getParent()
64*0b57cec5SDimitry Andric                                           ->getRegInfo().getTargetRegisterInfo()
65*0b57cec5SDimitry Andric                                           ->getNumRegs())
66*0b57cec5SDimitry Andric            && "expected register mask size");
67*0b57cec5SDimitry Andric     for (MachineOperand &MO : MI.operands()) {
68*0b57cec5SDimitry Andric       if (MO.isRegMask())
69*0b57cec5SDimitry Andric         MO.setRegMask(RegMask.data());
70*0b57cec5SDimitry Andric     }
71*0b57cec5SDimitry Andric   }
72*0b57cec5SDimitry Andric };
73*0b57cec5SDimitry Andric 
74*0b57cec5SDimitry Andric } // end of anonymous namespace
75*0b57cec5SDimitry Andric 
76*0b57cec5SDimitry Andric INITIALIZE_PASS_BEGIN(RegUsageInfoPropagation, "reg-usage-propagation",
77*0b57cec5SDimitry Andric                       RUIP_NAME, false, false)
78*0b57cec5SDimitry Andric INITIALIZE_PASS_DEPENDENCY(PhysicalRegisterUsageInfo)
79*0b57cec5SDimitry Andric INITIALIZE_PASS_END(RegUsageInfoPropagation, "reg-usage-propagation",
80*0b57cec5SDimitry Andric                     RUIP_NAME, false, false)
81*0b57cec5SDimitry Andric 
82*0b57cec5SDimitry Andric char RegUsageInfoPropagation::ID = 0;
83*0b57cec5SDimitry Andric 
84*0b57cec5SDimitry Andric // Assumes call instructions have a single reference to a function.
findCalledFunction(const Module & M,const MachineInstr & MI)85*0b57cec5SDimitry Andric static const Function *findCalledFunction(const Module &M,
86*0b57cec5SDimitry Andric                                           const MachineInstr &MI) {
87*0b57cec5SDimitry Andric   for (const MachineOperand &MO : MI.operands()) {
88*0b57cec5SDimitry Andric     if (MO.isGlobal())
89*0b57cec5SDimitry Andric       return dyn_cast<const Function>(MO.getGlobal());
90*0b57cec5SDimitry Andric 
91*0b57cec5SDimitry Andric     if (MO.isSymbol())
92*0b57cec5SDimitry Andric       return M.getFunction(MO.getSymbolName());
93*0b57cec5SDimitry Andric   }
94*0b57cec5SDimitry Andric 
95*0b57cec5SDimitry Andric   return nullptr;
96*0b57cec5SDimitry Andric }
97*0b57cec5SDimitry Andric 
runOnMachineFunction(MachineFunction & MF)98*0b57cec5SDimitry Andric bool RegUsageInfoPropagation::runOnMachineFunction(MachineFunction &MF) {
99*0b57cec5SDimitry Andric   const Module &M = *MF.getFunction().getParent();
100*0b57cec5SDimitry Andric   PhysicalRegisterUsageInfo *PRUI = &getAnalysis<PhysicalRegisterUsageInfo>();
101*0b57cec5SDimitry Andric 
102*0b57cec5SDimitry Andric   LLVM_DEBUG(dbgs() << " ++++++++++++++++++++ " << getPassName()
103*0b57cec5SDimitry Andric                     << " ++++++++++++++++++++  \n");
104*0b57cec5SDimitry Andric   LLVM_DEBUG(dbgs() << "MachineFunction : " << MF.getName() << "\n");
105*0b57cec5SDimitry Andric 
106*0b57cec5SDimitry Andric   const MachineFrameInfo &MFI = MF.getFrameInfo();
107*0b57cec5SDimitry Andric   if (!MFI.hasCalls() && !MFI.hasTailCall())
108*0b57cec5SDimitry Andric     return false;
109*0b57cec5SDimitry Andric 
110*0b57cec5SDimitry Andric   bool Changed = false;
111*0b57cec5SDimitry Andric 
112*0b57cec5SDimitry Andric   for (MachineBasicBlock &MBB : MF) {
113*0b57cec5SDimitry Andric     for (MachineInstr &MI : MBB) {
114*0b57cec5SDimitry Andric       if (!MI.isCall())
115*0b57cec5SDimitry Andric         continue;
116*0b57cec5SDimitry Andric       LLVM_DEBUG(
117*0b57cec5SDimitry Andric           dbgs()
118*0b57cec5SDimitry Andric           << "Call Instruction Before Register Usage Info Propagation : \n"
119*0b57cec5SDimitry Andric           << MI << "\n");
120*0b57cec5SDimitry Andric 
121*0b57cec5SDimitry Andric       auto UpdateRegMask = [&](const Function &F) {
122*0b57cec5SDimitry Andric         const ArrayRef<uint32_t> RegMask = PRUI->getRegUsageInfo(F);
123*0b57cec5SDimitry Andric         if (RegMask.empty())
124*0b57cec5SDimitry Andric           return;
125*0b57cec5SDimitry Andric         setRegMask(MI, RegMask);
126*0b57cec5SDimitry Andric         Changed = true;
127*0b57cec5SDimitry Andric       };
128*0b57cec5SDimitry Andric 
129*0b57cec5SDimitry Andric       if (const Function *F = findCalledFunction(M, MI)) {
130*0b57cec5SDimitry Andric         if (F->isDefinitionExact()) {
131*0b57cec5SDimitry Andric           UpdateRegMask(*F);
132*0b57cec5SDimitry Andric         } else {
133*0b57cec5SDimitry Andric           LLVM_DEBUG(dbgs() << "Function definition is not exact\n");
134*0b57cec5SDimitry Andric         }
135*0b57cec5SDimitry Andric       } else {
136*0b57cec5SDimitry Andric         LLVM_DEBUG(dbgs() << "Failed to find call target function\n");
137*0b57cec5SDimitry Andric       }
138*0b57cec5SDimitry Andric 
139*0b57cec5SDimitry Andric       LLVM_DEBUG(
140*0b57cec5SDimitry Andric           dbgs()
141*0b57cec5SDimitry Andric           << "Call Instruction After Register Usage Info Propagation : \n"
142*0b57cec5SDimitry Andric           << MI << '\n');
143*0b57cec5SDimitry Andric     }
144*0b57cec5SDimitry Andric   }
145*0b57cec5SDimitry Andric 
146*0b57cec5SDimitry Andric   LLVM_DEBUG(
147*0b57cec5SDimitry Andric       dbgs() << " +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++"
148*0b57cec5SDimitry Andric                 "++++++ \n");
149*0b57cec5SDimitry Andric   return Changed;
150*0b57cec5SDimitry Andric }
151*0b57cec5SDimitry Andric 
createRegUsageInfoPropPass()152*0b57cec5SDimitry Andric FunctionPass *llvm::createRegUsageInfoPropPass() {
153   return new RegUsageInfoPropagation();
154 }
155