RegUsageInfoPropagate.cpp 5.0 KB

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