| 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 | |
| 14 | using namespace llvm; |
| 15 | |
| 16 | // Always verify cycle info if expensive checking is enabled. |
| 17 | #ifdef EXPENSIVE_CHECKS |
| 18 | bool llvm::VerifyCycleInfo = true; |
| 19 | #else |
| 20 | bool llvm::VerifyCycleInfo = false; |
| 21 | #endif |
| 22 | static 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 | |
| 27 | namespace llvm { |
| 28 | class Module; |
| 29 | } // namespace llvm |
| 30 | |
| 31 | CycleInfo CycleAnalysis::run(Function &F, FunctionAnalysisManager &) { |
| 32 | CycleInfo CI; |
| 33 | CI.compute(F); |
| 34 | return CI; |
| 35 | } |
| 36 | |
| 37 | bool 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 | |
| 46 | AnalysisKey CycleAnalysis::Key; |
| 47 | |
| 48 | CycleInfoPrinterPass::CycleInfoPrinterPass(raw_ostream &OS) : OS(OS) {} |
| 49 | |
| 50 | PreservedAnalyses 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 | |
| 58 | PreservedAnalyses 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 | |
| 74 | char CycleInfoWrapperPass::ID = 0; |
| 75 | |
| 76 | CycleInfoWrapperPass::CycleInfoWrapperPass() : FunctionPass(ID) {} |
| 77 | |
| 78 | INITIALIZE_PASS_BEGIN(CycleInfoWrapperPass, "cycles" , "Cycle Info Analysis" , |
| 79 | true, true) |
| 80 | INITIALIZE_PASS_END(CycleInfoWrapperPass, "cycles" , "Cycle Info Analysis" , true, |
| 81 | true) |
| 82 | |
| 83 | void CycleInfoWrapperPass::getAnalysisUsage(AnalysisUsage &AU) const { |
| 84 | AU.setPreservesAll(); |
| 85 | } |
| 86 | |
| 87 | bool CycleInfoWrapperPass::runOnFunction(Function &Func) { |
| 88 | CI.clear(); |
| 89 | |
| 90 | F = &Func; |
| 91 | CI.compute(F&: Func); |
| 92 | return false; |
| 93 | } |
| 94 | |
| 95 | void CycleInfoWrapperPass::print(raw_ostream &OS, const Module *) const { |
| 96 | OS << "CycleInfo for function: " << F->getName() << "\n" ; |
| 97 | CI.print(Out&: OS); |
| 98 | } |
| 99 | |
| 100 | void CycleInfoWrapperPass::verifyAnalysis() const { |
| 101 | if (VerifyCycleInfo) |
| 102 | CI.verify(); |
| 103 | } |
| 104 | |
| 105 | void CycleInfoWrapperPass::releaseMemory() { |
| 106 | CI.clear(); |
| 107 | F = nullptr; |
| 108 | } |
| 109 | |