xref: /llvm-project/llvm/lib/Analysis/CycleAnalysis.cpp (revision 236fda550d36d35a00785938c3e38b0f402aeda6)
1 //===- CycleAnalysis.cpp - Compute CycleInfo for LLVM 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/Analysis/CycleAnalysis.h"
10 #include "llvm/IR/CFG.h" // for successors found by ADL in GenericCycleImpl.h
11 #include "llvm/InitializePasses.h"
12 
13 using namespace llvm;
14 
15 namespace llvm {
16 class Module;
17 } // namespace llvm
18 
19 CycleInfo CycleAnalysis::run(Function &F, FunctionAnalysisManager &) {
20   CycleInfo CI;
21   CI.compute(F);
22   return CI;
23 }
24 
25 AnalysisKey CycleAnalysis::Key;
26 
27 CycleInfoPrinterPass::CycleInfoPrinterPass(raw_ostream &OS) : OS(OS) {}
28 
29 PreservedAnalyses CycleInfoPrinterPass::run(Function &F,
30                                             FunctionAnalysisManager &AM) {
31   OS << "CycleInfo for function: " << F.getName() << "\n";
32   AM.getResult<CycleAnalysis>(F).print(OS);
33 
34   return PreservedAnalyses::all();
35 }
36 
37 PreservedAnalyses CycleInfoVerifierPass::run(Function &F,
38                                              FunctionAnalysisManager &AM) {
39   CycleInfo &CI = AM.getResult<CycleAnalysis>(F);
40   CI.verify();
41   return PreservedAnalyses::all();
42 }
43 
44 //===----------------------------------------------------------------------===//
45 //  CycleInfoWrapperPass Implementation
46 //===----------------------------------------------------------------------===//
47 //
48 // The implementation details of the wrapper pass that holds a CycleInfo
49 // suitable for use with the legacy pass manager.
50 //
51 //===----------------------------------------------------------------------===//
52 
53 char CycleInfoWrapperPass::ID = 0;
54 
55 CycleInfoWrapperPass::CycleInfoWrapperPass() : FunctionPass(ID) {
56   initializeCycleInfoWrapperPassPass(*PassRegistry::getPassRegistry());
57 }
58 
59 INITIALIZE_PASS_BEGIN(CycleInfoWrapperPass, "cycles", "Cycle Info Analysis",
60                       true, true)
61 INITIALIZE_PASS_END(CycleInfoWrapperPass, "cycles", "Cycle Info Analysis", true,
62                     true)
63 
64 void CycleInfoWrapperPass::getAnalysisUsage(AnalysisUsage &AU) const {
65   AU.setPreservesAll();
66 }
67 
68 bool CycleInfoWrapperPass::runOnFunction(Function &Func) {
69   CI.clear();
70 
71   F = &Func;
72   CI.compute(Func);
73   return false;
74 }
75 
76 void CycleInfoWrapperPass::print(raw_ostream &OS, const Module *) const {
77   OS << "CycleInfo for function: " << F->getName() << "\n";
78   CI.print(OS);
79 }
80 
81 void CycleInfoWrapperPass::releaseMemory() {
82   CI.clear();
83   F = nullptr;
84 }
85