MachineFunctionPass.cpp 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. //===-- MachineFunctionPass.cpp -------------------------------------------===//
  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 file contains the definitions of the MachineFunctionPass members.
  10. //
  11. //===----------------------------------------------------------------------===//
  12. #include "llvm/CodeGen/MachineFunctionPass.h"
  13. #include "llvm/Analysis/BasicAliasAnalysis.h"
  14. #include "llvm/Analysis/DominanceFrontier.h"
  15. #include "llvm/Analysis/GlobalsModRef.h"
  16. #include "llvm/Analysis/IVUsers.h"
  17. #include "llvm/Analysis/LoopInfo.h"
  18. #include "llvm/Analysis/MemoryDependenceAnalysis.h"
  19. #include "llvm/Analysis/ScalarEvolution.h"
  20. #include "llvm/Analysis/ScalarEvolutionAliasAnalysis.h"
  21. #include "llvm/CodeGen/MachineFunction.h"
  22. #include "llvm/CodeGen/MachineModuleInfo.h"
  23. #include "llvm/CodeGen/MachineOptimizationRemarkEmitter.h"
  24. #include "llvm/CodeGen/Passes.h"
  25. #include "llvm/IR/Dominators.h"
  26. #include "llvm/IR/Function.h"
  27. using namespace llvm;
  28. using namespace ore;
  29. Pass *MachineFunctionPass::createPrinterPass(raw_ostream &O,
  30. const std::string &Banner) const {
  31. return createMachineFunctionPrinterPass(O, Banner);
  32. }
  33. bool MachineFunctionPass::runOnFunction(Function &F) {
  34. // Do not codegen any 'available_externally' functions at all, they have
  35. // definitions outside the translation unit.
  36. if (F.hasAvailableExternallyLinkage())
  37. return false;
  38. MachineModuleInfo &MMI = getAnalysis<MachineModuleInfoWrapperPass>().getMMI();
  39. MachineFunction &MF = MMI.getOrCreateMachineFunction(F);
  40. MachineFunctionProperties &MFProps = MF.getProperties();
  41. #ifndef NDEBUG
  42. if (!MFProps.verifyRequiredProperties(RequiredProperties)) {
  43. errs() << "MachineFunctionProperties required by " << getPassName()
  44. << " pass are not met by function " << F.getName() << ".\n"
  45. << "Required properties: ";
  46. RequiredProperties.print(errs());
  47. errs() << "\nCurrent properties: ";
  48. MFProps.print(errs());
  49. errs() << "\n";
  50. llvm_unreachable("MachineFunctionProperties check failed");
  51. }
  52. #endif
  53. // Collect the MI count of the function before the pass.
  54. unsigned CountBefore, CountAfter;
  55. // Check if the user asked for size remarks.
  56. bool ShouldEmitSizeRemarks =
  57. F.getParent()->shouldEmitInstrCountChangedRemark();
  58. // If we want size remarks, collect the number of MachineInstrs in our
  59. // MachineFunction before the pass runs.
  60. if (ShouldEmitSizeRemarks)
  61. CountBefore = MF.getInstructionCount();
  62. bool RV = runOnMachineFunction(MF);
  63. if (ShouldEmitSizeRemarks) {
  64. // We wanted size remarks. Check if there was a change to the number of
  65. // MachineInstrs in the module. Emit a remark if there was a change.
  66. CountAfter = MF.getInstructionCount();
  67. if (CountBefore != CountAfter) {
  68. MachineOptimizationRemarkEmitter MORE(MF, nullptr);
  69. MORE.emit([&]() {
  70. int64_t Delta = static_cast<int64_t>(CountAfter) -
  71. static_cast<int64_t>(CountBefore);
  72. MachineOptimizationRemarkAnalysis R("size-info", "FunctionMISizeChange",
  73. MF.getFunction().getSubprogram(),
  74. &MF.front());
  75. R << NV("Pass", getPassName())
  76. << ": Function: " << NV("Function", F.getName()) << ": "
  77. << "MI Instruction count changed from "
  78. << NV("MIInstrsBefore", CountBefore) << " to "
  79. << NV("MIInstrsAfter", CountAfter)
  80. << "; Delta: " << NV("Delta", Delta);
  81. return R;
  82. });
  83. }
  84. }
  85. MFProps.set(SetProperties);
  86. MFProps.reset(ClearedProperties);
  87. return RV;
  88. }
  89. void MachineFunctionPass::getAnalysisUsage(AnalysisUsage &AU) const {
  90. AU.addRequired<MachineModuleInfoWrapperPass>();
  91. AU.addPreserved<MachineModuleInfoWrapperPass>();
  92. // MachineFunctionPass preserves all LLVM IR passes, but there's no
  93. // high-level way to express this. Instead, just list a bunch of
  94. // passes explicitly. This does not include setPreservesCFG,
  95. // because CodeGen overloads that to mean preserving the MachineBasicBlock
  96. // CFG in addition to the LLVM IR CFG.
  97. AU.addPreserved<BasicAAWrapperPass>();
  98. AU.addPreserved<DominanceFrontierWrapperPass>();
  99. AU.addPreserved<DominatorTreeWrapperPass>();
  100. AU.addPreserved<AAResultsWrapperPass>();
  101. AU.addPreserved<GlobalsAAWrapperPass>();
  102. AU.addPreserved<IVUsersWrapperPass>();
  103. AU.addPreserved<LoopInfoWrapperPass>();
  104. AU.addPreserved<MemoryDependenceWrapperPass>();
  105. AU.addPreserved<ScalarEvolutionWrapperPass>();
  106. AU.addPreserved<SCEVAAWrapperPass>();
  107. FunctionPass::getAnalysisUsage(AU);
  108. }