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