MachineFunctionPass.cpp 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  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/OptimizationRemarkEmitter.h"
  20. #include "llvm/Analysis/ScalarEvolution.h"
  21. #include "llvm/Analysis/ScalarEvolutionAliasAnalysis.h"
  22. #include "llvm/CodeGen/MachineFunction.h"
  23. #include "llvm/CodeGen/MachineModuleInfo.h"
  24. #include "llvm/CodeGen/MachineOptimizationRemarkEmitter.h"
  25. #include "llvm/CodeGen/Passes.h"
  26. #include "llvm/IR/Dominators.h"
  27. #include "llvm/IR/Function.h"
  28. #include "llvm/IR/PrintPasses.h"
  29. using namespace llvm;
  30. using namespace ore;
  31. Pass *MachineFunctionPass::createPrinterPass(raw_ostream &O,
  32. const std::string &Banner) const {
  33. return createMachineFunctionPrinterPass(O, Banner);
  34. }
  35. bool MachineFunctionPass::runOnFunction(Function &F) {
  36. // Do not codegen any 'available_externally' functions at all, they have
  37. // definitions outside the translation unit.
  38. if (F.hasAvailableExternallyLinkage())
  39. return false;
  40. MachineModuleInfo &MMI = getAnalysis<MachineModuleInfoWrapperPass>().getMMI();
  41. MachineFunction &MF = MMI.getOrCreateMachineFunction(F);
  42. MachineFunctionProperties &MFProps = MF.getProperties();
  43. #ifndef NDEBUG
  44. if (!MFProps.verifyRequiredProperties(RequiredProperties)) {
  45. errs() << "MachineFunctionProperties required by " << getPassName()
  46. << " pass are not met by function " << F.getName() << ".\n"
  47. << "Required properties: ";
  48. RequiredProperties.print(errs());
  49. errs() << "\nCurrent properties: ";
  50. MFProps.print(errs());
  51. errs() << "\n";
  52. llvm_unreachable("MachineFunctionProperties check failed");
  53. }
  54. #endif
  55. // Collect the MI count of the function before the pass.
  56. unsigned CountBefore, CountAfter;
  57. // Check if the user asked for size remarks.
  58. bool ShouldEmitSizeRemarks =
  59. F.getParent()->shouldEmitInstrCountChangedRemark();
  60. // If we want size remarks, collect the number of MachineInstrs in our
  61. // MachineFunction before the pass runs.
  62. if (ShouldEmitSizeRemarks)
  63. CountBefore = MF.getInstructionCount();
  64. // For --print-changed, if the function name is a candidate, save the
  65. // serialized MF to be compared later.
  66. SmallString<0> BeforeStr, AfterStr;
  67. StringRef PassID;
  68. if (PrintChanged != ChangePrinter::None) {
  69. if (const PassInfo *PI = Pass::lookupPassInfo(getPassID()))
  70. PassID = PI->getPassArgument();
  71. }
  72. const bool IsInterestingPass = isPassInPrintList(PassID);
  73. const bool ShouldPrintChanged = PrintChanged != ChangePrinter::None &&
  74. IsInterestingPass &&
  75. isFunctionInPrintList(MF.getName());
  76. if (ShouldPrintChanged) {
  77. raw_svector_ostream OS(BeforeStr);
  78. MF.print(OS);
  79. }
  80. bool RV = runOnMachineFunction(MF);
  81. if (ShouldEmitSizeRemarks) {
  82. // We wanted size remarks. Check if there was a change to the number of
  83. // MachineInstrs in the module. Emit a remark if there was a change.
  84. CountAfter = MF.getInstructionCount();
  85. if (CountBefore != CountAfter) {
  86. MachineOptimizationRemarkEmitter MORE(MF, nullptr);
  87. MORE.emit([&]() {
  88. int64_t Delta = static_cast<int64_t>(CountAfter) -
  89. static_cast<int64_t>(CountBefore);
  90. MachineOptimizationRemarkAnalysis R("size-info", "FunctionMISizeChange",
  91. MF.getFunction().getSubprogram(),
  92. &MF.front());
  93. R << NV("Pass", getPassName())
  94. << ": Function: " << NV("Function", F.getName()) << ": "
  95. << "MI Instruction count changed from "
  96. << NV("MIInstrsBefore", CountBefore) << " to "
  97. << NV("MIInstrsAfter", CountAfter)
  98. << "; Delta: " << NV("Delta", Delta);
  99. return R;
  100. });
  101. }
  102. }
  103. MFProps.set(SetProperties);
  104. MFProps.reset(ClearedProperties);
  105. // For --print-changed, print if the serialized MF has changed. Modes other
  106. // than quiet/verbose are unimplemented and treated the same as 'quiet'.
  107. if (ShouldPrintChanged || !IsInterestingPass) {
  108. if (ShouldPrintChanged) {
  109. raw_svector_ostream OS(AfterStr);
  110. MF.print(OS);
  111. }
  112. if (IsInterestingPass && BeforeStr != AfterStr) {
  113. errs() << ("*** IR Dump After " + getPassName() + " (" + PassID +
  114. ") on " + MF.getName() + " ***\n");
  115. switch (PrintChanged) {
  116. case ChangePrinter::None:
  117. llvm_unreachable("");
  118. case ChangePrinter::Quiet:
  119. case ChangePrinter::Verbose:
  120. case ChangePrinter::DotCfgQuiet: // unimplemented
  121. case ChangePrinter::DotCfgVerbose: // unimplemented
  122. errs() << AfterStr;
  123. break;
  124. case ChangePrinter::DiffQuiet:
  125. case ChangePrinter::DiffVerbose:
  126. case ChangePrinter::ColourDiffQuiet:
  127. case ChangePrinter::ColourDiffVerbose: {
  128. bool Color = llvm::is_contained(
  129. {ChangePrinter::ColourDiffQuiet, ChangePrinter::ColourDiffVerbose},
  130. PrintChanged.getValue());
  131. StringRef Removed = Color ? "\033[31m-%l\033[0m\n" : "-%l\n";
  132. StringRef Added = Color ? "\033[32m+%l\033[0m\n" : "+%l\n";
  133. StringRef NoChange = " %l\n";
  134. errs() << doSystemDiff(BeforeStr, AfterStr, Removed, Added, NoChange);
  135. break;
  136. }
  137. }
  138. } else if (llvm::is_contained({ChangePrinter::Verbose,
  139. ChangePrinter::DiffVerbose,
  140. ChangePrinter::ColourDiffVerbose},
  141. PrintChanged.getValue())) {
  142. const char *Reason =
  143. IsInterestingPass ? " omitted because no change" : " filtered out";
  144. errs() << "*** IR Dump After " << getPassName();
  145. if (!PassID.empty())
  146. errs() << " (" << PassID << ")";
  147. errs() << " on " << MF.getName() + Reason + " ***\n";
  148. }
  149. }
  150. return RV;
  151. }
  152. void MachineFunctionPass::getAnalysisUsage(AnalysisUsage &AU) const {
  153. AU.addRequired<MachineModuleInfoWrapperPass>();
  154. AU.addPreserved<MachineModuleInfoWrapperPass>();
  155. // MachineFunctionPass preserves all LLVM IR passes, but there's no
  156. // high-level way to express this. Instead, just list a bunch of
  157. // passes explicitly. This does not include setPreservesCFG,
  158. // because CodeGen overloads that to mean preserving the MachineBasicBlock
  159. // CFG in addition to the LLVM IR CFG.
  160. AU.addPreserved<BasicAAWrapperPass>();
  161. AU.addPreserved<DominanceFrontierWrapperPass>();
  162. AU.addPreserved<DominatorTreeWrapperPass>();
  163. AU.addPreserved<AAResultsWrapperPass>();
  164. AU.addPreserved<GlobalsAAWrapperPass>();
  165. AU.addPreserved<IVUsersWrapperPass>();
  166. AU.addPreserved<LoopInfoWrapperPass>();
  167. AU.addPreserved<MemoryDependenceWrapperPass>();
  168. AU.addPreserved<ScalarEvolutionWrapperPass>();
  169. AU.addPreserved<SCEVAAWrapperPass>();
  170. FunctionPass::getAnalysisUsage(AU);
  171. }