1 //=--- RegUsageInfoPropagate.cpp - Register Usage Informartion Propagation --=//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 /// This pass is required to take advantage of the interprocedural register
11 /// allocation infrastructure.
13 /// This pass iterates through MachineInstrs in a given MachineFunction and at
14 /// each callsite queries RegisterUsageInfo for RegMask (calculated based on
15 /// actual register allocation) of the callee function, if the RegMask detail
16 /// is available then this pass will update the RegMask of the call instruction.
17 /// This updated RegMask will be used by the register allocator while allocating
18 /// the current MachineFunction.
20 //===----------------------------------------------------------------------===//
22 #include "llvm/CodeGen/MachineBasicBlock.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/PassAnalysisSupport.h"
30 #include "llvm/Support/Debug.h"
31 #include "llvm/Support/raw_ostream.h"
32 #include "llvm/Target/TargetMachine.h"
37 void initializeRegUsageInfoPropagationPassPass(PassRegistry &);
42 #define DEBUG_TYPE "ip-regalloc"
44 #define RUIP_NAME "Register Usage Information Propagation"
47 class RegUsageInfoPropagationPass : public MachineFunctionPass {
50 RegUsageInfoPropagationPass() : MachineFunctionPass(ID) {
51 PassRegistry &Registry = *PassRegistry::getPassRegistry();
52 initializeRegUsageInfoPropagationPassPass(Registry);
55 const char *getPassName() const override { return RUIP_NAME; }
57 bool runOnMachineFunction(MachineFunction &MF) override;
59 void getAnalysisUsage(AnalysisUsage &AU) const override;
64 static void setRegMask(MachineInstr &MI, const uint32_t *RegMask) {
65 for (MachineOperand &MO : MI.operands()) {
67 MO.setRegMask(RegMask);
71 } // end of anonymous namespace
72 char RegUsageInfoPropagationPass::ID = 0;
74 INITIALIZE_PASS_BEGIN(RegUsageInfoPropagationPass, "reg-usage-propagation",
75 RUIP_NAME, false, false)
76 INITIALIZE_PASS_DEPENDENCY(PhysicalRegisterUsageInfo)
77 INITIALIZE_PASS_END(RegUsageInfoPropagationPass, "reg-usage-propagation",
78 RUIP_NAME, false, false)
80 FunctionPass *llvm::createRegUsageInfoPropPass() {
81 return new RegUsageInfoPropagationPass();
84 void RegUsageInfoPropagationPass::getAnalysisUsage(AnalysisUsage &AU) const {
85 AU.addRequired<PhysicalRegisterUsageInfo>();
87 MachineFunctionPass::getAnalysisUsage(AU);
90 bool RegUsageInfoPropagationPass::runOnMachineFunction(MachineFunction &MF) {
91 const Module *M = MF.getFunction()->getParent();
92 PhysicalRegisterUsageInfo *PRUI = &getAnalysis<PhysicalRegisterUsageInfo>();
94 DEBUG(dbgs() << " ++++++++++++++++++++ " << getPassName()
95 << " ++++++++++++++++++++ \n");
96 DEBUG(dbgs() << "MachineFunction : " << MF.getName() << "\n");
100 for (MachineBasicBlock &MBB : MF) {
101 for (MachineInstr &MI : MBB) {
105 << "Call Instruction Before Register Usage Info Propagation : \n");
106 DEBUG(dbgs() << MI << "\n");
108 auto UpdateRegMask = [&](const Function *F) {
109 const auto *RegMask = PRUI->getRegUsageInfo(F);
112 setRegMask(MI, &(*RegMask)[0]);
116 MachineOperand &Operand = MI.getOperand(0);
117 if (Operand.isGlobal())
118 UpdateRegMask(cast<Function>(Operand.getGlobal()));
119 else if (Operand.isSymbol())
120 UpdateRegMask(M->getFunction(Operand.getSymbolName()));
123 << "Call Instruction After Register Usage Info Propagation : \n");
124 DEBUG(dbgs() << MI << "\n");
128 DEBUG(dbgs() << " +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++"