LazyBranchProbabilityInfo.cpp 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. //===- LazyBranchProbabilityInfo.cpp - Lazy Branch Probability 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 BranchProbabilityInfoWrapperPass.
  10. // The difference is that with this pass the branch probabilities are not
  11. // computed when the analysis pass is executed but rather when the BPI results
  12. // is explicitly requested by the analysis client.
  13. //
  14. //===----------------------------------------------------------------------===//
  15. #include "llvm/Analysis/LazyBranchProbabilityInfo.h"
  16. #include "llvm/Analysis/LoopInfo.h"
  17. #include "llvm/Analysis/TargetLibraryInfo.h"
  18. #include "llvm/IR/Dominators.h"
  19. #include "llvm/InitializePasses.h"
  20. using namespace llvm;
  21. #define DEBUG_TYPE "lazy-branch-prob"
  22. INITIALIZE_PASS_BEGIN(LazyBranchProbabilityInfoPass, DEBUG_TYPE,
  23. "Lazy Branch Probability Analysis", true, true)
  24. INITIALIZE_PASS_DEPENDENCY(LoopInfoWrapperPass)
  25. INITIALIZE_PASS_DEPENDENCY(TargetLibraryInfoWrapperPass)
  26. INITIALIZE_PASS_END(LazyBranchProbabilityInfoPass, DEBUG_TYPE,
  27. "Lazy Branch Probability Analysis", true, true)
  28. char LazyBranchProbabilityInfoPass::ID = 0;
  29. LazyBranchProbabilityInfoPass::LazyBranchProbabilityInfoPass()
  30. : FunctionPass(ID) {
  31. initializeLazyBranchProbabilityInfoPassPass(*PassRegistry::getPassRegistry());
  32. }
  33. void LazyBranchProbabilityInfoPass::print(raw_ostream &OS,
  34. const Module *) const {
  35. LBPI->getCalculated().print(OS);
  36. }
  37. void LazyBranchProbabilityInfoPass::getAnalysisUsage(AnalysisUsage &AU) const {
  38. // We require DT so it's available when LI is available. The LI updating code
  39. // asserts that DT is also present so if we don't make sure that we have DT
  40. // here, that assert will trigger.
  41. AU.addRequiredTransitive<DominatorTreeWrapperPass>();
  42. AU.addRequiredTransitive<LoopInfoWrapperPass>();
  43. AU.addRequiredTransitive<TargetLibraryInfoWrapperPass>();
  44. AU.setPreservesAll();
  45. }
  46. void LazyBranchProbabilityInfoPass::releaseMemory() { LBPI.reset(); }
  47. bool LazyBranchProbabilityInfoPass::runOnFunction(Function &F) {
  48. LoopInfo &LI = getAnalysis<LoopInfoWrapperPass>().getLoopInfo();
  49. TargetLibraryInfo &TLI =
  50. getAnalysis<TargetLibraryInfoWrapperPass>().getTLI(F);
  51. LBPI = std::make_unique<LazyBranchProbabilityInfo>(&F, &LI, &TLI);
  52. return false;
  53. }
  54. void LazyBranchProbabilityInfoPass::getLazyBPIAnalysisUsage(AnalysisUsage &AU) {
  55. AU.addRequiredTransitive<LazyBranchProbabilityInfoPass>();
  56. AU.addRequiredTransitive<LoopInfoWrapperPass>();
  57. AU.addRequiredTransitive<TargetLibraryInfoWrapperPass>();
  58. }
  59. void llvm::initializeLazyBPIPassPass(PassRegistry &Registry) {
  60. INITIALIZE_PASS_DEPENDENCY(LazyBranchProbabilityInfoPass);
  61. INITIALIZE_PASS_DEPENDENCY(LoopInfoWrapperPass);
  62. INITIALIZE_PASS_DEPENDENCY(TargetLibraryInfoWrapperPass);
  63. }