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
13using namespace llvm;
14
15namespace llvm {
16class Module;
17} // namespace llvm
18
19CycleInfo CycleAnalysis::run(Function &F, FunctionAnalysisManager &) {
20 CycleInfo CI;
21 CI.compute(F);
22 return CI;
23}
24
25bool CycleAnalysis::invalidate(Function &F, const PreservedAnalyses &PA,
26 FunctionAnalysisManager::Invalidator &) {
27 // Check whether the analysis, all analyses on functions, or the function's
28 // CFG have been preserved.
29 auto PAC = PA.getChecker<CycleAnalysis>();
30 return !(PAC.preserved() || PAC.preservedSet<AllAnalysesOn<Function>>() ||
31 PAC.preservedSet<CFGAnalyses>());
32}
33
34AnalysisKey CycleAnalysis::Key;
35
36CycleInfoPrinterPass::CycleInfoPrinterPass(raw_ostream &OS) : OS(OS) {}
37
38PreservedAnalyses CycleInfoPrinterPass::run(Function &F,
39 FunctionAnalysisManager &AM) {
40 OS << "CycleInfo for function: " << F.getName() << "\n";
41 AM.getResult<CycleAnalysis>(IR&: F).print(Out&: OS);
42
43 return PreservedAnalyses::all();
44}
45
46PreservedAnalyses CycleInfoVerifierPass::run(Function &F,
47 FunctionAnalysisManager &AM) {
48 CycleInfo &CI = AM.getResult<CycleAnalysis>(IR&: F);
49 CI.verify();
50 return PreservedAnalyses::all();
51}
52
53//===----------------------------------------------------------------------===//
54// CycleInfoWrapperPass Implementation
55//===----------------------------------------------------------------------===//
56//
57// The implementation details of the wrapper pass that holds a CycleInfo
58// suitable for use with the legacy pass manager.
59//
60//===----------------------------------------------------------------------===//
61
62char CycleInfoWrapperPass::ID = 0;
63
64CycleInfoWrapperPass::CycleInfoWrapperPass() : FunctionPass(ID) {}
65
66INITIALIZE_PASS_BEGIN(CycleInfoWrapperPass, "cycles", "Cycle Info Analysis",
67 true, true)
68INITIALIZE_PASS_END(CycleInfoWrapperPass, "cycles", "Cycle Info Analysis", true,
69 true)
70
71void CycleInfoWrapperPass::getAnalysisUsage(AnalysisUsage &AU) const {
72 AU.setPreservesAll();
73}
74
75bool CycleInfoWrapperPass::runOnFunction(Function &Func) {
76 CI.clear();
77
78 F = &Func;
79 CI.compute(F&: Func);
80 return false;
81}
82
83void CycleInfoWrapperPass::print(raw_ostream &OS, const Module *) const {
84 OS << "CycleInfo for function: " << F->getName() << "\n";
85 CI.print(Out&: OS);
86}
87
88void CycleInfoWrapperPass::releaseMemory() {
89 CI.clear();
90 F = nullptr;
91}
92