xref: /freebsd-src/contrib/llvm-project/llvm/include/llvm/Transforms/IPO/HotColdSplitting.h (revision abd872540f24cfc7dbd1ea29b6918c7082a22108)
1 //===- HotColdSplitting.h ---- Outline Cold Regions -------------*- 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 // This pass outlines cold regions to a separate function.
9 //
10 //===----------------------------------------------------------------------===//
11 
12 #ifndef LLVM_TRANSFORMS_IPO_HOTCOLDSPLITTING_H
13 #define LLVM_TRANSFORMS_IPO_HOTCOLDSPLITTING_H
14 
15 #include "llvm/IR/PassManager.h"
16 #include "llvm/Support/BranchProbability.h"
17 
18 namespace llvm {
19 
20 class Module;
21 class ProfileSummaryInfo;
22 class BlockFrequencyInfo;
23 class TargetTransformInfo;
24 class OptimizationRemarkEmitter;
25 class AssumptionCache;
26 class DominatorTree;
27 class CodeExtractorAnalysisCache;
28 
29 /// A sequence of basic blocks.
30 ///
31 /// A 0-sized SmallVector is slightly cheaper to move than a std::vector.
32 using BlockSequence = SmallVector<BasicBlock *, 0>;
33 
34 class HotColdSplitting {
35 public:
36   HotColdSplitting(ProfileSummaryInfo *ProfSI,
37                    function_ref<BlockFrequencyInfo *(Function &)> GBFI,
38                    function_ref<TargetTransformInfo &(Function &)> GTTI,
39                    std::function<OptimizationRemarkEmitter &(Function &)> *GORE,
40                    function_ref<AssumptionCache *(Function &)> LAC)
41       : PSI(ProfSI), GetBFI(GBFI), GetTTI(GTTI), GetORE(GORE), LookupAC(LAC) {}
42   bool run(Module &M);
43 
44 private:
45   bool isFunctionCold(const Function &F) const;
46   bool isBasicBlockCold(BasicBlock* BB,
47                         BranchProbability ColdProbThresh,
48                         SmallPtrSetImpl<BasicBlock *> &ColdBlocks,
49                         SmallPtrSetImpl<BasicBlock *> &AnnotatedColdBlocks,
50                         BlockFrequencyInfo *BFI) const;
51   bool shouldOutlineFrom(const Function &F) const;
52   bool outlineColdRegions(Function &F, bool HasProfileSummary);
53   Function *extractColdRegion(const BlockSequence &Region,
54                               const CodeExtractorAnalysisCache &CEAC,
55                               DominatorTree &DT, BlockFrequencyInfo *BFI,
56                               TargetTransformInfo &TTI,
57                               OptimizationRemarkEmitter &ORE,
58                               AssumptionCache *AC, unsigned Count);
59   ProfileSummaryInfo *PSI;
60   function_ref<BlockFrequencyInfo *(Function &)> GetBFI;
61   function_ref<TargetTransformInfo &(Function &)> GetTTI;
62   std::function<OptimizationRemarkEmitter &(Function &)> *GetORE;
63   function_ref<AssumptionCache *(Function &)> LookupAC;
64 };
65 
66 /// Pass to outline cold regions.
67 class HotColdSplittingPass : public PassInfoMixin<HotColdSplittingPass> {
68 public:
69   PreservedAnalyses run(Module &M, ModuleAnalysisManager &AM);
70 };
71 
72 } // end namespace llvm
73 
74 #endif // LLVM_TRANSFORMS_IPO_HOTCOLDSPLITTING_H
75 
76