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/CallGraph.h" 20 #include "llvm/Analysis/CallGraphSCCPass.h" 21 #include "llvm/Analysis/LazyCallGraph.h" 22 23 namespace llvm { 24 25 /// Wrapper to unify "old style" CallGraph and "new style" LazyCallGraph. This 26 /// simplifies the interface and the call sites, e.g., new and old pass manager 27 /// passes can share the same code. 28 class CallGraphUpdater { 29 /// Containers for functions which we did replace or want to delete when 30 /// `finalize` is called. This can happen explicitly or as part of the 31 /// destructor. Dead functions in comdat sections are tracked separately 32 /// because a function with discardable linakage in a COMDAT should only 33 /// be dropped if the entire COMDAT is dropped, see git ac07703842cf. 34 ///{ 35 SmallPtrSet<Function *, 16> ReplacedFunctions; 36 SmallVector<Function *, 16> DeadFunctions; 37 SmallVector<Function *, 16> DeadFunctionsInComdats; 38 ///} 39 40 /// Old PM variables 41 ///{ 42 CallGraph *CG = nullptr; 43 CallGraphSCC *CGSCC = nullptr; 44 ///} 45 46 /// New PM variables 47 ///{ 48 LazyCallGraph *LCG = nullptr; 49 LazyCallGraph::SCC *SCC = nullptr; 50 CGSCCAnalysisManager *AM = nullptr; 51 CGSCCUpdateResult *UR = nullptr; 52 FunctionAnalysisManager *FAM = nullptr; 53 ///} 54 55 public: CallGraphUpdater()56 CallGraphUpdater() {} ~CallGraphUpdater()57 ~CallGraphUpdater() { finalize(); } 58 59 /// Initializers for usage outside of a CGSCC pass, inside a CGSCC pass in 60 /// the old and new pass manager (PM). 61 ///{ initialize(CallGraph & CG,CallGraphSCC & SCC)62 void initialize(CallGraph &CG, CallGraphSCC &SCC) { 63 this->CG = &CG; 64 this->CGSCC = &SCC; 65 } initialize(LazyCallGraph & LCG,LazyCallGraph::SCC & SCC,CGSCCAnalysisManager & AM,CGSCCUpdateResult & UR)66 void initialize(LazyCallGraph &LCG, LazyCallGraph::SCC &SCC, 67 CGSCCAnalysisManager &AM, CGSCCUpdateResult &UR) { 68 this->LCG = &LCG; 69 this->SCC = &SCC; 70 this->AM = &AM; 71 this->UR = &UR; 72 FAM = 73 &AM.getResult<FunctionAnalysisManagerCGSCCProxy>(SCC, LCG).getManager(); 74 } 75 ///} 76 77 /// Finalizer that will trigger actions like function removal from the CG. 78 bool finalize(); 79 80 /// Remove \p Fn from the call graph. 81 void removeFunction(Function &Fn); 82 83 /// After an CGSCC pass changes a function in ways that affect the call 84 /// graph, this method can be called to update it. 85 void reanalyzeFunction(Function &Fn); 86 87 /// If a new function was created by outlining, this method can be called 88 /// to update the call graph for the new function. Note that the old one 89 /// still needs to be re-analyzed or manually updated. 90 void registerOutlinedFunction(Function &NewFn); 91 92 /// Replace \p OldFn in the call graph (and SCC) with \p NewFn. The uses 93 /// outside the call graph and the function \p OldFn are not modified. 94 /// Note that \p OldFn is also removed from the call graph 95 /// (\see removeFunction). 96 void replaceFunctionWith(Function &OldFn, Function &NewFn); 97 98 /// Remove the call site \p CS from the call graph. 99 void removeCallSite(CallBase &CS); 100 101 /// Replace \p OldCS with the new call site \p NewCS. 102 /// \return True if the replacement was successful, otherwise False. In the 103 /// latter case the parent function of \p OldCB needs to be re-analyzed. 104 bool replaceCallSite(CallBase &OldCS, CallBase &NewCS); 105 }; 106 107 } // end namespace llvm 108 109 #endif // LLVM_TRANSFORMS_UTILS_CALLGRAPHUPDATER_H 110