1 //===- ModuleSummaryAnalysis.h - Module summary index builder ---*- 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 /// This is the interface to build a ModuleSummaryIndex for a module. 10 /// 11 //===----------------------------------------------------------------------===// 12 13 #ifndef LLVM_ANALYSIS_MODULESUMMARYANALYSIS_H 14 #define LLVM_ANALYSIS_MODULESUMMARYANALYSIS_H 15 16 #include "llvm/IR/ModuleSummaryIndex.h" 17 #include "llvm/IR/PassManager.h" 18 #include "llvm/Pass.h" 19 #include "llvm/Support/Compiler.h" 20 #include <functional> 21 #include <optional> 22 23 namespace llvm { 24 25 class BlockFrequencyInfo; 26 class Function; 27 class Module; 28 class ProfileSummaryInfo; 29 class StackSafetyInfo; 30 31 /// Direct function to compute a \c ModuleSummaryIndex from a given module. 32 /// 33 /// If operating within a pass manager which has defined ways to compute the \c 34 /// BlockFrequencyInfo for a given function, that can be provided via 35 /// a std::function callback. Otherwise, this routine will manually construct 36 /// that information. 37 LLVM_ABI ModuleSummaryIndex buildModuleSummaryIndex( 38 const Module &M, 39 std::function<BlockFrequencyInfo *(const Function &F)> GetBFICallback, 40 ProfileSummaryInfo *PSI, 41 std::function<const StackSafetyInfo *(const Function &F)> GetSSICallback = 42 [](const Function &F) -> const StackSafetyInfo * { return nullptr; }); 43 44 /// Analysis pass to provide the ModuleSummaryIndex object. 45 class ModuleSummaryIndexAnalysis 46 : public AnalysisInfoMixin<ModuleSummaryIndexAnalysis> { 47 friend AnalysisInfoMixin<ModuleSummaryIndexAnalysis>; 48 49 LLVM_ABI static AnalysisKey Key; 50 51 public: 52 using Result = ModuleSummaryIndex; 53 54 LLVM_ABI Result run(Module &M, ModuleAnalysisManager &AM); 55 }; 56 57 /// Legacy wrapper pass to provide the ModuleSummaryIndex object. 58 class LLVM_ABI ModuleSummaryIndexWrapperPass : public ModulePass { 59 std::optional<ModuleSummaryIndex> Index; 60 61 public: 62 static char ID; 63 64 ModuleSummaryIndexWrapperPass(); 65 66 /// Get the index built by pass getIndex()67 ModuleSummaryIndex &getIndex() { return *Index; } getIndex()68 const ModuleSummaryIndex &getIndex() const { return *Index; } 69 70 bool runOnModule(Module &M) override; 71 bool doFinalization(Module &M) override; 72 void getAnalysisUsage(AnalysisUsage &AU) const override; 73 }; 74 75 //===--------------------------------------------------------------------===// 76 // 77 // createModuleSummaryIndexWrapperPass - This pass builds a ModuleSummaryIndex 78 // object for the module, to be written to bitcode or LLVM assembly. 79 // 80 LLVM_ABI ModulePass *createModuleSummaryIndexWrapperPass(); 81 82 /// Legacy wrapper pass to provide the ModuleSummaryIndex object. 83 class LLVM_ABI ImmutableModuleSummaryIndexWrapperPass : public ImmutablePass { 84 const ModuleSummaryIndex *Index; 85 86 public: 87 static char ID; 88 89 ImmutableModuleSummaryIndexWrapperPass( 90 const ModuleSummaryIndex *Index = nullptr); getIndex()91 const ModuleSummaryIndex *getIndex() const { return Index; } 92 void getAnalysisUsage(AnalysisUsage &AU) const override; 93 }; 94 95 //===--------------------------------------------------------------------===// 96 // 97 // ImmutableModuleSummaryIndexWrapperPass - This pass wrap provided 98 // ModuleSummaryIndex object for the module, to be used by other passes. 99 // 100 LLVM_ABI ImmutablePass * 101 createImmutableModuleSummaryIndexWrapperPass(const ModuleSummaryIndex *Index); 102 103 /// Returns true if the instruction could have memprof metadata, used to ensure 104 /// consistency between summary analysis and the ThinLTO backend processing. 105 LLVM_ABI bool mayHaveMemprofSummary(const CallBase *CB); 106 107 } // end namespace llvm 108 109 #endif // LLVM_ANALYSIS_MODULESUMMARYANALYSIS_H 110