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 //===----------------------------------------------------------------------===//
21 // MachineCycleInfoWrapperPass Implementation
22 //===----------------------------------------------------------------------===//
23 //
24 // The implementation details of the wrapper pass that holds a MachineCycleInfo
25 // suitable for use with the legacy pass manager.
26 //
27 //===----------------------------------------------------------------------===//
28 
29 char MachineCycleInfoWrapperPass::ID = 0;
30 
31 MachineCycleInfoWrapperPass::MachineCycleInfoWrapperPass()
32     : MachineFunctionPass(ID) {
33   initializeMachineCycleInfoWrapperPassPass(*PassRegistry::getPassRegistry());
34 }
35 
36 INITIALIZE_PASS_BEGIN(MachineCycleInfoWrapperPass, "machine-cycles",
37                       "Machine Cycle Info Analysis", true, true)
38 INITIALIZE_PASS_END(MachineCycleInfoWrapperPass, "machine-cycles",
39                     "Machine Cycle Info Analysis", true, true)
40 
41 void MachineCycleInfoWrapperPass::getAnalysisUsage(AnalysisUsage &AU) const {
42   AU.setPreservesAll();
43   MachineFunctionPass::getAnalysisUsage(AU);
44 }
45 
46 bool MachineCycleInfoWrapperPass::runOnMachineFunction(MachineFunction &Func) {
47   CI.clear();
48 
49   F = &Func;
50   CI.compute(Func);
51   return false;
52 }
53 
54 void MachineCycleInfoWrapperPass::print(raw_ostream &OS, const Module *) const {
55   OS << "MachineCycleInfo for function: " << F->getName() << "\n";
56   CI.print(OS);
57 }
58 
59 void MachineCycleInfoWrapperPass::releaseMemory() {
60   CI.clear();
61   F = nullptr;
62 }
63 
64 char MachineCycleInfoPrinterPass::ID = 0;
65 
66 MachineCycleInfoPrinterPass::MachineCycleInfoPrinterPass()
67     : MachineFunctionPass(ID) {
68   initializeMachineCycleInfoPrinterPassPass(*PassRegistry::getPassRegistry());
69 }
70 
71 INITIALIZE_PASS_BEGIN(MachineCycleInfoPrinterPass, "print-machine-cycles",
72                       "Print Machine Cycle Info Analysis", true, true)
73 INITIALIZE_PASS_DEPENDENCY(MachineCycleInfoWrapperPass)
74 INITIALIZE_PASS_END(MachineCycleInfoPrinterPass, "print-machine-cycles",
75                     "Print Machine Cycle Info Analysis", true, true)
76 
77 void MachineCycleInfoPrinterPass::getAnalysisUsage(AnalysisUsage &AU) const {
78   AU.setPreservesAll();
79   AU.addRequired<MachineCycleInfoWrapperPass>();
80   MachineFunctionPass::getAnalysisUsage(AU);
81 }
82 
83 bool MachineCycleInfoPrinterPass::runOnMachineFunction(MachineFunction &F) {
84   auto &CI = getAnalysis<MachineCycleInfoWrapperPass>();
85   CI.print(errs());
86   return false;
87 }
88