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