LazyBlockFrequencyInfo.cpp 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. //===- LazyBlockFrequencyInfo.cpp - Lazy Block Frequency Analysis ---------===//
  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. // This is an alternative analysis pass to BlockFrequencyInfoWrapperPass. The
  10. // difference is that with this pass the block frequencies are not computed when
  11. // the analysis pass is executed but rather when the BFI result is explicitly
  12. // requested by the analysis client.
  13. //
  14. //===----------------------------------------------------------------------===//
  15. #include "llvm/Analysis/LazyBlockFrequencyInfo.h"
  16. #include "llvm/Analysis/LazyBranchProbabilityInfo.h"
  17. #include "llvm/Analysis/LoopInfo.h"
  18. #include "llvm/IR/Dominators.h"
  19. #include "llvm/InitializePasses.h"
  20. using namespace llvm;
  21. #define DEBUG_TYPE "lazy-block-freq"
  22. INITIALIZE_PASS_BEGIN(LazyBlockFrequencyInfoPass, DEBUG_TYPE,
  23. "Lazy Block Frequency Analysis", true, true)
  24. INITIALIZE_PASS_DEPENDENCY(LazyBPIPass)
  25. INITIALIZE_PASS_DEPENDENCY(LoopInfoWrapperPass)
  26. INITIALIZE_PASS_END(LazyBlockFrequencyInfoPass, DEBUG_TYPE,
  27. "Lazy Block Frequency Analysis", true, true)
  28. char LazyBlockFrequencyInfoPass::ID = 0;
  29. LazyBlockFrequencyInfoPass::LazyBlockFrequencyInfoPass() : FunctionPass(ID) {
  30. initializeLazyBlockFrequencyInfoPassPass(*PassRegistry::getPassRegistry());
  31. }
  32. void LazyBlockFrequencyInfoPass::print(raw_ostream &OS, const Module *) const {
  33. LBFI.getCalculated().print(OS);
  34. }
  35. void LazyBlockFrequencyInfoPass::getAnalysisUsage(AnalysisUsage &AU) const {
  36. LazyBranchProbabilityInfoPass::getLazyBPIAnalysisUsage(AU);
  37. // We require DT so it's available when LI is available. The LI updating code
  38. // asserts that DT is also present so if we don't make sure that we have DT
  39. // here, that assert will trigger.
  40. AU.addRequiredTransitive<DominatorTreeWrapperPass>();
  41. AU.addRequiredTransitive<LoopInfoWrapperPass>();
  42. AU.setPreservesAll();
  43. }
  44. void LazyBlockFrequencyInfoPass::releaseMemory() { LBFI.releaseMemory(); }
  45. bool LazyBlockFrequencyInfoPass::runOnFunction(Function &F) {
  46. auto &BPIPass = getAnalysis<LazyBranchProbabilityInfoPass>();
  47. LoopInfo &LI = getAnalysis<LoopInfoWrapperPass>().getLoopInfo();
  48. LBFI.setAnalysis(&F, &BPIPass, &LI);
  49. return false;
  50. }
  51. void LazyBlockFrequencyInfoPass::getLazyBFIAnalysisUsage(AnalysisUsage &AU) {
  52. LazyBranchProbabilityInfoPass::getLazyBPIAnalysisUsage(AU);
  53. AU.addRequiredTransitive<LazyBlockFrequencyInfoPass>();
  54. AU.addRequiredTransitive<LoopInfoWrapperPass>();
  55. }
  56. void llvm::initializeLazyBFIPassPass(PassRegistry &Registry) {
  57. initializeLazyBPIPassPass(Registry);
  58. INITIALIZE_PASS_DEPENDENCY(LazyBlockFrequencyInfoPass);
  59. INITIALIZE_PASS_DEPENDENCY(LoopInfoWrapperPass);
  60. }