1 //===- SyntheticCountsUtils.h - utilities for count propagation--*- C++ -*-===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This file defines utilities for synthetic counts propagation.
11 //
12 //===----------------------------------------------------------------------===//
13 
14 #ifndef LLVM_ANALYSIS_SYNTHETIC_COUNTS_UTILS_H
15 #define LLVM_ANALYSIS_SYNTHETIC_COUNTS_UTILS_H
16 
17 #include "llvm/ADT/STLExtras.h"
18 #include "llvm/Analysis/CallGraph.h"
19 #include "llvm/Support/ScaledNumber.h"
20 
21 namespace llvm {
22 
23 class CallGraph;
24 class Function;
25 
26 /// Class with methods to propagate synthetic entry counts.
27 ///
28 /// This class is templated on the type of the call graph and designed to work
29 /// with the traditional per-module callgraph and the summary callgraphs used in
30 /// ThinLTO. This contains only static methods and alias templates.
31 template <typename CallGraphType> class SyntheticCountsUtils {
32 public:
33   using Scaled64 = ScaledNumber<uint64_t>;
34   using CGT = GraphTraits<CallGraphType>;
35   using NodeRef = typename CGT::NodeRef;
36   using EdgeRef = typename CGT::EdgeRef;
37   using SccTy = std::vector<NodeRef>;
38 
39   // Not all EdgeRef have information about the source of the edge. Hence
40   // NodeRef corresponding to the source of the EdgeRef is explicitly passed.
41   using GetProfCountTy = function_ref<Optional<Scaled64>(NodeRef, EdgeRef)>;
42   using AddCountTy = function_ref<void(NodeRef, Scaled64)>;
43 
44   static void propagate(const CallGraphType &CG, GetProfCountTy GetProfCount,
45                         AddCountTy AddCount);
46 
47 private:
48   static void propagateFromSCC(const SccTy &SCC, GetProfCountTy GetProfCount,
49                                AddCountTy AddCount);
50 };
51 } // namespace llvm
52 
53 #endif
54