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