CallGraphUpdater.h 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. #pragma once
  2. #ifdef __GNUC__
  3. #pragma GCC diagnostic push
  4. #pragma GCC diagnostic ignored "-Wunused-parameter"
  5. #endif
  6. //===- CallGraphUpdater.h - A (lazy) call graph update helper ---*- C++ -*-===//
  7. //
  8. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  9. // See https://llvm.org/LICENSE.txt for license information.
  10. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  11. //
  12. //===----------------------------------------------------------------------===//
  13. /// \file
  14. ///
  15. /// This file provides interfaces used to manipulate a call graph, regardless
  16. /// if it is a "old style" CallGraph or an "new style" LazyCallGraph.
  17. ///
  18. //===----------------------------------------------------------------------===//
  19. #ifndef LLVM_TRANSFORMS_UTILS_CALLGRAPHUPDATER_H
  20. #define LLVM_TRANSFORMS_UTILS_CALLGRAPHUPDATER_H
  21. #include "llvm/Analysis/CGSCCPassManager.h"
  22. #include "llvm/Analysis/LazyCallGraph.h"
  23. namespace llvm {
  24. class CallGraph;
  25. class CallGraphSCC;
  26. /// Wrapper to unify "old style" CallGraph and "new style" LazyCallGraph. This
  27. /// simplifies the interface and the call sites, e.g., new and old pass manager
  28. /// passes can share the same code.
  29. class CallGraphUpdater {
  30. /// Containers for functions which we did replace or want to delete when
  31. /// `finalize` is called. This can happen explicitly or as part of the
  32. /// destructor. Dead functions in comdat sections are tracked separately
  33. /// because a function with discardable linakage in a COMDAT should only
  34. /// be dropped if the entire COMDAT is dropped, see git ac07703842cf.
  35. ///{
  36. SmallPtrSet<Function *, 16> ReplacedFunctions;
  37. SmallVector<Function *, 16> DeadFunctions;
  38. SmallVector<Function *, 16> DeadFunctionsInComdats;
  39. ///}
  40. /// Old PM variables
  41. ///{
  42. CallGraph *CG = nullptr;
  43. CallGraphSCC *CGSCC = nullptr;
  44. ///}
  45. /// New PM variables
  46. ///{
  47. LazyCallGraph *LCG = nullptr;
  48. LazyCallGraph::SCC *SCC = nullptr;
  49. CGSCCAnalysisManager *AM = nullptr;
  50. CGSCCUpdateResult *UR = nullptr;
  51. FunctionAnalysisManager *FAM = nullptr;
  52. ///}
  53. public:
  54. CallGraphUpdater() = default;
  55. ~CallGraphUpdater() { finalize(); }
  56. /// Initializers for usage outside of a CGSCC pass, inside a CGSCC pass in
  57. /// the old and new pass manager (PM).
  58. ///{
  59. void initialize(CallGraph &CG, CallGraphSCC &SCC) {
  60. this->CG = &CG;
  61. this->CGSCC = &SCC;
  62. }
  63. void initialize(LazyCallGraph &LCG, LazyCallGraph::SCC &SCC,
  64. CGSCCAnalysisManager &AM, CGSCCUpdateResult &UR) {
  65. this->LCG = &LCG;
  66. this->SCC = &SCC;
  67. this->AM = &AM;
  68. this->UR = &UR;
  69. FAM =
  70. &AM.getResult<FunctionAnalysisManagerCGSCCProxy>(SCC, LCG).getManager();
  71. }
  72. ///}
  73. /// Finalizer that will trigger actions like function removal from the CG.
  74. bool finalize();
  75. /// Remove \p Fn from the call graph.
  76. void removeFunction(Function &Fn);
  77. /// After an CGSCC pass changes a function in ways that affect the call
  78. /// graph, this method can be called to update it.
  79. void reanalyzeFunction(Function &Fn);
  80. /// If a new function was created by outlining, this method can be called
  81. /// to update the call graph for the new function. Note that the old one
  82. /// still needs to be re-analyzed or manually updated.
  83. void registerOutlinedFunction(Function &OriginalFn, Function &NewFn);
  84. /// Replace \p OldFn in the call graph (and SCC) with \p NewFn. The uses
  85. /// outside the call graph and the function \p OldFn are not modified.
  86. /// Note that \p OldFn is also removed from the call graph
  87. /// (\see removeFunction).
  88. void replaceFunctionWith(Function &OldFn, Function &NewFn);
  89. /// Remove the call site \p CS from the call graph.
  90. void removeCallSite(CallBase &CS);
  91. /// Replace \p OldCS with the new call site \p NewCS.
  92. /// \return True if the replacement was successful, otherwise False. In the
  93. /// latter case the parent function of \p OldCB needs to be re-analyzed.
  94. bool replaceCallSite(CallBase &OldCS, CallBase &NewCS);
  95. };
  96. } // end namespace llvm
  97. #endif // LLVM_TRANSFORMS_UTILS_CALLGRAPHUPDATER_H
  98. #ifdef __GNUC__
  99. #pragma GCC diagnostic pop
  100. #endif