xref: /llvm-project/llvm/include/llvm/Analysis/SyntheticCountsUtils.h (revision 9a84afefa1ab98c5315ad2e7a78e8cdc6372e153)
1 //===- SyntheticCountsUtils.h - utilities for count propagation--*- 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 //
9 // This file defines utilities for synthetic counts propagation.
10 //
11 //===----------------------------------------------------------------------===//
12 
13 #ifndef LLVM_ANALYSIS_SYNTHETICCOUNTSUTILS_H
14 #define LLVM_ANALYSIS_SYNTHETICCOUNTSUTILS_H
15 
16 #include "llvm/ADT/GraphTraits.h"
17 #include "llvm/ADT/STLFunctionalExtras.h"
18 #include "llvm/Support/ScaledNumber.h"
19 #include <vector>
20 
21 namespace llvm {
22 
23 /// Class with methods to propagate synthetic entry counts.
24 ///
25 /// This class is templated on the type of the call graph and designed to work
26 /// with the traditional per-module callgraph and the summary callgraphs used in
27 /// ThinLTO. This contains only static methods and alias templates.
28 template <typename CallGraphType> class SyntheticCountsUtils {
29 public:
30   using Scaled64 = ScaledNumber<uint64_t>;
31   using CGT = GraphTraits<CallGraphType>;
32   using NodeRef = typename CGT::NodeRef;
33   using EdgeRef = typename CGT::EdgeRef;
34   using SccTy = std::vector<NodeRef>;
35 
36   // Not all EdgeRef have information about the source of the edge. Hence
37   // NodeRef corresponding to the source of the EdgeRef is explicitly passed.
38   using GetProfCountTy =
39       function_ref<std::optional<Scaled64>(NodeRef, EdgeRef)>;
40   using AddCountTy = function_ref<void(NodeRef, Scaled64)>;
41 
42   static void propagate(const CallGraphType &CG, GetProfCountTy GetProfCount,
43                         AddCountTy AddCount);
44 
45 private:
46   static void propagateFromSCC(const SccTy &SCC, GetProfCountTy GetProfCount,
47                                AddCountTy AddCount);
48 };
49 } // namespace llvm
50 
51 #endif
52