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