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