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