LLVM  16.0.0git
CallGraphUpdater.h
Go to the documentation of this file.
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 
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.
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  /// Old PM variables
42  ///{
43  CallGraph *CG = nullptr;
44  CallGraphSCC *CGSCC = nullptr;
45  ///}
46 
47  /// New PM variables
48  ///{
49  LazyCallGraph *LCG = nullptr;
50  LazyCallGraph::SCC *SCC = nullptr;
51  CGSCCAnalysisManager *AM = nullptr;
52  CGSCCUpdateResult *UR = nullptr;
53  FunctionAnalysisManager *FAM = nullptr;
54  ///}
55 
56 public:
57  CallGraphUpdater() = default;
59 
60  /// Initializers for usage outside of a CGSCC pass, inside a CGSCC pass in
61  /// the old and new pass manager (PM).
62  ///{
63  void initialize(CallGraph &CG, CallGraphSCC &SCC) {
64  this->CG = &CG;
65  this->CGSCC = &SCC;
66  }
69  this->LCG = &LCG;
70  this->SCC = &SCC;
71  this->AM = &AM;
72  this->UR = &UR;
73  FAM =
74  &AM.getResult<FunctionAnalysisManagerCGSCCProxy>(SCC, LCG).getManager();
75  }
76  ///}
77 
78  /// Finalizer that will trigger actions like function removal from the CG.
79  bool finalize();
80 
81  /// Remove \p Fn from the call graph.
82  void removeFunction(Function &Fn);
83 
84  /// After an CGSCC pass changes a function in ways that affect the call
85  /// graph, this method can be called to update it.
86  void reanalyzeFunction(Function &Fn);
87 
88  /// If a new function was created by outlining, this method can be called
89  /// to update the call graph for the new function. Note that the old one
90  /// still needs to be re-analyzed or manually updated.
91  void registerOutlinedFunction(Function &OriginalFn, Function &NewFn);
92 
93  /// Replace \p OldFn in the call graph (and SCC) with \p NewFn. The uses
94  /// outside the call graph and the function \p OldFn are not modified.
95  /// Note that \p OldFn is also removed from the call graph
96  /// (\see removeFunction).
97  void replaceFunctionWith(Function &OldFn, Function &NewFn);
98 
99  /// Remove the call site \p CS from the call graph.
100  void removeCallSite(CallBase &CS);
101 
102  /// Replace \p OldCS with the new call site \p NewCS.
103  /// \return True if the replacement was successful, otherwise False. In the
104  /// latter case the parent function of \p OldCB needs to be re-analyzed.
105  bool replaceCallSite(CallBase &OldCS, CallBase &NewCS);
106 };
107 
108 } // end namespace llvm
109 
110 #endif // LLVM_TRANSFORMS_UTILS_CALLGRAPHUPDATER_H
llvm::CallGraphUpdater
Wrapper to unify "old style" CallGraph and "new style" LazyCallGraph.
Definition: CallGraphUpdater.h:29
llvm
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
llvm::CallGraphUpdater::CallGraphUpdater
CallGraphUpdater()=default
}
llvm::AnalysisManager::getResult
PassT::Result & getResult(IRUnitT &IR, ExtraArgTs... ExtraArgs)
Get the result of an analysis pass for a given IR unit.
Definition: PassManager.h:774
llvm::Function
Definition: Function.h:60
llvm::CallGraphUpdater::replaceCallSite
bool replaceCallSite(CallBase &OldCS, CallBase &NewCS)
Replace OldCS with the new call site NewCS.
Definition: CallGraphUpdater.cpp:145
llvm::SmallVector
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
Definition: SmallVector.h:1199
llvm::CallGraph
The basic data container for the call graph of a Module of IR.
Definition: CallGraph.h:72
FAM
FunctionAnalysisManager FAM
Definition: PassBuilderBindings.cpp:59
llvm::SmallPtrSet
SmallPtrSet - This class implements a set which is optimized for holding SmallSize or less elements.
Definition: SmallPtrSet.h:450
llvm::CallGraphUpdater::finalize
bool finalize()
}
Definition: CallGraphUpdater.cpp:24
llvm::CallGraphSCC
CallGraphSCC - This is a single SCC that a CallGraphSCCPass is run on.
Definition: CallGraphSCCPass.h:87
llvm::LazyCallGraph::SCC
An SCC of the call graph.
Definition: LazyCallGraph.h:420
llvm::CallGraphUpdater::removeCallSite
void removeCallSite(CallBase &CS)
Remove the call site CS from the call graph.
Definition: CallGraphUpdater.cpp:162
LazyCallGraph.h
llvm::CallGraphUpdater::~CallGraphUpdater
~CallGraphUpdater()
Definition: CallGraphUpdater.h:58
CGSCCPassManager.h
llvm::CallGraphUpdater::registerOutlinedFunction
void registerOutlinedFunction(Function &OriginalFn, Function &NewFn)
If a new function was created by outlining, this method can be called to update the call graph for th...
Definition: CallGraphUpdater.cpp:101
llvm::CGSCCUpdateResult
Support structure for SCC passes to communicate updates the call graph back to the CGSCC pass manager...
Definition: CGSCCPassManager.h:232
llvm::CallGraphUpdater::replaceFunctionWith
void replaceFunctionWith(Function &OldFn, Function &NewFn)
Replace OldFn in the call graph (and SCC) with NewFn.
Definition: CallGraphUpdater.cpp:125
llvm::CallGraphUpdater::removeFunction
void removeFunction(Function &Fn)
Remove Fn from the call graph.
Definition: CallGraphUpdater.cpp:109
llvm::CallGraphUpdater::initialize
void initialize(LazyCallGraph &LCG, LazyCallGraph::SCC &SCC, CGSCCAnalysisManager &AM, CGSCCUpdateResult &UR)
Definition: CallGraphUpdater.h:67
llvm::CallBase
Base class for all callable instructions (InvokeInst and CallInst) Holds everything related to callin...
Definition: InstrTypes.h:1175
llvm::AnalysisManager
A container for analyses that lazily runs them and caches their results.
Definition: InstructionSimplify.h:42
llvm::LazyCallGraph
A lazily constructed view of the call graph of a module.
Definition: LazyCallGraph.h:111
llvm::CallGraphUpdater::initialize
void initialize(CallGraph &CG, CallGraphSCC &SCC)
Initializers for usage outside of a CGSCC pass, inside a CGSCC pass in the old and new pass manager (...
Definition: CallGraphUpdater.h:63
llvm::CallGraphUpdater::reanalyzeFunction
void reanalyzeFunction(Function &Fn)
After an CGSCC pass changes a function in ways that affect the call graph, this method can be called ...
Definition: CallGraphUpdater.cpp:89
llvm::FunctionAnalysisManagerCGSCCProxy
A proxy from a FunctionAnalysisManager to an SCC.
Definition: CGSCCPassManager.h:392