xref: /freebsd/contrib/llvm-project/llvm/include/llvm/Transforms/Utils/CallGraphUpdater.h (revision 0fca6ea1d4eea4c934cfff25ac9ee8ad6fe95583)
1 //===- CallGraphUpdater.h - A (lazy) call graph update helper ---*- C++ -*-===//
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 /// \file
9 ///
10 /// This file provides interfaces used to manipulate a call graph, regardless
11 /// if it is a "old style" CallGraph or an "new style" LazyCallGraph.
12 ///
13 //===----------------------------------------------------------------------===//
14 
15 #ifndef LLVM_TRANSFORMS_UTILS_CALLGRAPHUPDATER_H
16 #define LLVM_TRANSFORMS_UTILS_CALLGRAPHUPDATER_H
17 
18 #include "llvm/Analysis/CGSCCPassManager.h"
19 #include "llvm/Analysis/LazyCallGraph.h"
20 
21 namespace llvm {
22 
23 class CallGraph;
24 class CallGraphSCC;
25 
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 
41   /// New PM variables
42   ///{
43   LazyCallGraph *LCG = nullptr;
44   LazyCallGraph::SCC *SCC = nullptr;
45   CGSCCAnalysisManager *AM = nullptr;
46   CGSCCUpdateResult *UR = nullptr;
47   FunctionAnalysisManager *FAM = nullptr;
48   ///}
49 
50 public:
51   CallGraphUpdater() = default;
~CallGraphUpdater()52   ~CallGraphUpdater() { finalize(); }
53 
54   /// Initializers for usage outside of a CGSCC pass, inside a CGSCC pass in
55   /// the old and new pass manager (PM).
56   ///{
initialize(LazyCallGraph & LCG,LazyCallGraph::SCC & SCC,CGSCCAnalysisManager & AM,CGSCCUpdateResult & UR)57   void initialize(LazyCallGraph &LCG, LazyCallGraph::SCC &SCC,
58                   CGSCCAnalysisManager &AM, CGSCCUpdateResult &UR) {
59     this->LCG = &LCG;
60     this->SCC = &SCC;
61     this->AM = &AM;
62     this->UR = &UR;
63     FAM =
64         &AM.getResult<FunctionAnalysisManagerCGSCCProxy>(SCC, LCG).getManager();
65   }
66   ///}
67 
68   /// Finalizer that will trigger actions like function removal from the CG.
69   bool finalize();
70 
71   /// Remove \p Fn from the call graph.
72   void removeFunction(Function &Fn);
73 
74   /// After an CGSCC pass changes a function in ways that affect the call
75   /// graph, this method can be called to update it.
76   void reanalyzeFunction(Function &Fn);
77 
78   /// If a new function was created by outlining, this method can be called
79   /// to update the call graph for the new function. Note that the old one
80   /// still needs to be re-analyzed or manually updated.
81   void registerOutlinedFunction(Function &OriginalFn, Function &NewFn);
82 
83   /// Replace \p OldFn in the call graph (and SCC) with \p NewFn. The uses
84   /// outside the call graph and the function \p OldFn are not modified.
85   /// Note that \p OldFn is also removed from the call graph
86   /// (\see removeFunction).
87   void replaceFunctionWith(Function &OldFn, Function &NewFn);
88 };
89 
90 } // end namespace llvm
91 
92 #endif // LLVM_TRANSFORMS_UTILS_CALLGRAPHUPDATER_H
93