DominanceFrontier.cpp 3.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. //===- DominanceFrontier.cpp - Dominance Frontier Calculation -------------===//
  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. #include "llvm/Analysis/DominanceFrontier.h"
  9. #include "llvm/Analysis/DominanceFrontierImpl.h"
  10. #include "llvm/Config/llvm-config.h"
  11. #include "llvm/IR/Dominators.h"
  12. #include "llvm/IR/Function.h"
  13. #include "llvm/IR/PassManager.h"
  14. #include "llvm/InitializePasses.h"
  15. #include "llvm/Pass.h"
  16. #include "llvm/Support/Compiler.h"
  17. #include "llvm/Support/Debug.h"
  18. #include "llvm/Support/raw_ostream.h"
  19. using namespace llvm;
  20. namespace llvm {
  21. template class DominanceFrontierBase<BasicBlock, false>;
  22. template class DominanceFrontierBase<BasicBlock, true>;
  23. template class ForwardDominanceFrontierBase<BasicBlock>;
  24. } // end namespace llvm
  25. char DominanceFrontierWrapperPass::ID = 0;
  26. INITIALIZE_PASS_BEGIN(DominanceFrontierWrapperPass, "domfrontier",
  27. "Dominance Frontier Construction", true, true)
  28. INITIALIZE_PASS_DEPENDENCY(DominatorTreeWrapperPass)
  29. INITIALIZE_PASS_END(DominanceFrontierWrapperPass, "domfrontier",
  30. "Dominance Frontier Construction", true, true)
  31. DominanceFrontierWrapperPass::DominanceFrontierWrapperPass()
  32. : FunctionPass(ID) {
  33. initializeDominanceFrontierWrapperPassPass(*PassRegistry::getPassRegistry());
  34. }
  35. void DominanceFrontierWrapperPass::releaseMemory() {
  36. DF.releaseMemory();
  37. }
  38. bool DominanceFrontierWrapperPass::runOnFunction(Function &) {
  39. releaseMemory();
  40. DF.analyze(getAnalysis<DominatorTreeWrapperPass>().getDomTree());
  41. return false;
  42. }
  43. void DominanceFrontierWrapperPass::getAnalysisUsage(AnalysisUsage &AU) const {
  44. AU.setPreservesAll();
  45. AU.addRequired<DominatorTreeWrapperPass>();
  46. }
  47. void DominanceFrontierWrapperPass::print(raw_ostream &OS, const Module *) const {
  48. DF.print(OS);
  49. }
  50. #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
  51. LLVM_DUMP_METHOD void DominanceFrontierWrapperPass::dump() const {
  52. print(dbgs());
  53. }
  54. #endif
  55. /// Handle invalidation explicitly.
  56. bool DominanceFrontier::invalidate(Function &F, const PreservedAnalyses &PA,
  57. FunctionAnalysisManager::Invalidator &) {
  58. // Check whether the analysis, all analyses on functions, or the function's
  59. // CFG have been preserved.
  60. auto PAC = PA.getChecker<DominanceFrontierAnalysis>();
  61. return !(PAC.preserved() || PAC.preservedSet<AllAnalysesOn<Function>>() ||
  62. PAC.preservedSet<CFGAnalyses>());
  63. }
  64. AnalysisKey DominanceFrontierAnalysis::Key;
  65. DominanceFrontier DominanceFrontierAnalysis::run(Function &F,
  66. FunctionAnalysisManager &AM) {
  67. DominanceFrontier DF;
  68. DF.analyze(AM.getResult<DominatorTreeAnalysis>(F));
  69. return DF;
  70. }
  71. DominanceFrontierPrinterPass::DominanceFrontierPrinterPass(raw_ostream &OS)
  72. : OS(OS) {}
  73. PreservedAnalyses
  74. DominanceFrontierPrinterPass::run(Function &F, FunctionAnalysisManager &AM) {
  75. OS << "DominanceFrontier for function: " << F.getName() << "\n";
  76. AM.getResult<DominanceFrontierAnalysis>(F).print(OS);
  77. return PreservedAnalyses::all();
  78. }