RegUsageInfoPropagate.cpp 5.1 KB

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