MachineCycleAnalysis.cpp 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. //===- MachineCycleAnalysis.cpp - Compute CycleInfo for Machine IR --------===//
  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. #include "llvm/CodeGen/MachineCycleAnalysis.h"
  9. #include "llvm/ADT/GenericCycleImpl.h"
  10. #include "llvm/CodeGen/MachineFunctionPass.h"
  11. #include "llvm/CodeGen/MachineSSAContext.h"
  12. #include "llvm/InitializePasses.h"
  13. using namespace llvm;
  14. template class llvm::GenericCycleInfo<llvm::MachineSSAContext>;
  15. template class llvm::GenericCycle<llvm::MachineSSAContext>;
  16. namespace {
  17. /// Legacy analysis pass which computes a \ref MachineCycleInfo.
  18. class MachineCycleInfoWrapperPass : public MachineFunctionPass {
  19. MachineFunction *F = nullptr;
  20. MachineCycleInfo CI;
  21. public:
  22. static char ID;
  23. MachineCycleInfoWrapperPass();
  24. MachineCycleInfo &getCycleInfo() { return CI; }
  25. const MachineCycleInfo &getCycleInfo() const { return CI; }
  26. bool runOnMachineFunction(MachineFunction &F) override;
  27. void getAnalysisUsage(AnalysisUsage &AU) const override;
  28. void releaseMemory() override;
  29. void print(raw_ostream &OS, const Module *M = nullptr) const override;
  30. // TODO: verify analysis
  31. };
  32. class MachineCycleInfoPrinterPass : public MachineFunctionPass {
  33. public:
  34. static char ID;
  35. MachineCycleInfoPrinterPass();
  36. bool runOnMachineFunction(MachineFunction &F) override;
  37. void getAnalysisUsage(AnalysisUsage &AU) const override;
  38. };
  39. } // namespace
  40. char MachineCycleInfoWrapperPass::ID = 0;
  41. MachineCycleInfoWrapperPass::MachineCycleInfoWrapperPass()
  42. : MachineFunctionPass(ID) {
  43. initializeMachineCycleInfoWrapperPassPass(*PassRegistry::getPassRegistry());
  44. }
  45. INITIALIZE_PASS_BEGIN(MachineCycleInfoWrapperPass, "machine-cycles",
  46. "Machine Cycle Info Analysis", true, true)
  47. INITIALIZE_PASS_END(MachineCycleInfoWrapperPass, "machine-cycles",
  48. "Machine Cycle Info Analysis", true, true)
  49. void MachineCycleInfoWrapperPass::getAnalysisUsage(AnalysisUsage &AU) const {
  50. AU.setPreservesAll();
  51. MachineFunctionPass::getAnalysisUsage(AU);
  52. }
  53. bool MachineCycleInfoWrapperPass::runOnMachineFunction(MachineFunction &Func) {
  54. CI.clear();
  55. F = &Func;
  56. CI.compute(Func);
  57. return false;
  58. }
  59. void MachineCycleInfoWrapperPass::print(raw_ostream &OS, const Module *) const {
  60. OS << "MachineCycleInfo for function: " << F->getName() << "\n";
  61. CI.print(OS);
  62. }
  63. void MachineCycleInfoWrapperPass::releaseMemory() {
  64. CI.clear();
  65. F = nullptr;
  66. }
  67. char MachineCycleInfoPrinterPass::ID = 0;
  68. MachineCycleInfoPrinterPass::MachineCycleInfoPrinterPass()
  69. : MachineFunctionPass(ID) {
  70. initializeMachineCycleInfoPrinterPassPass(*PassRegistry::getPassRegistry());
  71. }
  72. INITIALIZE_PASS_BEGIN(MachineCycleInfoPrinterPass, "print-machine-cycles",
  73. "Print Machine Cycle Info Analysis", true, true)
  74. INITIALIZE_PASS_DEPENDENCY(MachineCycleInfoWrapperPass)
  75. INITIALIZE_PASS_END(MachineCycleInfoPrinterPass, "print-machine-cycles",
  76. "Print Machine Cycle Info Analysis", true, true)
  77. void MachineCycleInfoPrinterPass::getAnalysisUsage(AnalysisUsage &AU) const {
  78. AU.setPreservesAll();
  79. AU.addRequired<MachineCycleInfoWrapperPass>();
  80. MachineFunctionPass::getAnalysisUsage(AU);
  81. }
  82. bool MachineCycleInfoPrinterPass::runOnMachineFunction(MachineFunction &F) {
  83. auto &CI = getAnalysis<MachineCycleInfoWrapperPass>();
  84. CI.print(errs());
  85. return false;
  86. }