xref: /freebsd/contrib/llvm-project/llvm/lib/Analysis/SyntheticCountsUtils.cpp (revision 0b57cec536236d46e3dba9bd041533462f33dbb7)
1*0b57cec5SDimitry Andric //===--- SyntheticCountsUtils.cpp - synthetic counts propagation utils ---===//
2*0b57cec5SDimitry Andric //
3*0b57cec5SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4*0b57cec5SDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
5*0b57cec5SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6*0b57cec5SDimitry Andric //
7*0b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
8*0b57cec5SDimitry Andric //
9*0b57cec5SDimitry Andric // This file defines utilities for propagating synthetic counts.
10*0b57cec5SDimitry Andric //
11*0b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
12*0b57cec5SDimitry Andric 
13*0b57cec5SDimitry Andric #include "llvm/Analysis/SyntheticCountsUtils.h"
14*0b57cec5SDimitry Andric #include "llvm/ADT/DenseSet.h"
15*0b57cec5SDimitry Andric #include "llvm/ADT/SCCIterator.h"
16*0b57cec5SDimitry Andric #include "llvm/Analysis/CallGraph.h"
17*0b57cec5SDimitry Andric #include "llvm/IR/CallSite.h"
18*0b57cec5SDimitry Andric #include "llvm/IR/Function.h"
19*0b57cec5SDimitry Andric #include "llvm/IR/InstIterator.h"
20*0b57cec5SDimitry Andric #include "llvm/IR/Instructions.h"
21*0b57cec5SDimitry Andric #include "llvm/IR/ModuleSummaryIndex.h"
22*0b57cec5SDimitry Andric 
23*0b57cec5SDimitry Andric using namespace llvm;
24*0b57cec5SDimitry Andric 
25*0b57cec5SDimitry Andric // Given an SCC, propagate entry counts along the edge of the SCC nodes.
26*0b57cec5SDimitry Andric template <typename CallGraphType>
27*0b57cec5SDimitry Andric void SyntheticCountsUtils<CallGraphType>::propagateFromSCC(
28*0b57cec5SDimitry Andric     const SccTy &SCC, GetProfCountTy GetProfCount, AddCountTy AddCount) {
29*0b57cec5SDimitry Andric 
30*0b57cec5SDimitry Andric   DenseSet<NodeRef> SCCNodes;
31*0b57cec5SDimitry Andric   SmallVector<std::pair<NodeRef, EdgeRef>, 8> SCCEdges, NonSCCEdges;
32*0b57cec5SDimitry Andric 
33*0b57cec5SDimitry Andric   for (auto &Node : SCC)
34*0b57cec5SDimitry Andric     SCCNodes.insert(Node);
35*0b57cec5SDimitry Andric 
36*0b57cec5SDimitry Andric   // Partition the edges coming out of the SCC into those whose destination is
37*0b57cec5SDimitry Andric   // in the SCC and the rest.
38*0b57cec5SDimitry Andric   for (const auto &Node : SCCNodes) {
39*0b57cec5SDimitry Andric     for (auto &E : children_edges<CallGraphType>(Node)) {
40*0b57cec5SDimitry Andric       if (SCCNodes.count(CGT::edge_dest(E)))
41*0b57cec5SDimitry Andric         SCCEdges.emplace_back(Node, E);
42*0b57cec5SDimitry Andric       else
43*0b57cec5SDimitry Andric         NonSCCEdges.emplace_back(Node, E);
44*0b57cec5SDimitry Andric     }
45*0b57cec5SDimitry Andric   }
46*0b57cec5SDimitry Andric 
47*0b57cec5SDimitry Andric   // For nodes in the same SCC, update the counts in two steps:
48*0b57cec5SDimitry Andric   // 1. Compute the additional count for each node by propagating the counts
49*0b57cec5SDimitry Andric   // along all incoming edges to the node that originate from within the same
50*0b57cec5SDimitry Andric   // SCC and summing them up.
51*0b57cec5SDimitry Andric   // 2. Add the additional counts to the nodes in the SCC.
52*0b57cec5SDimitry Andric   // This ensures that the order of
53*0b57cec5SDimitry Andric   // traversal of nodes within the SCC doesn't affect the final result.
54*0b57cec5SDimitry Andric 
55*0b57cec5SDimitry Andric   DenseMap<NodeRef, Scaled64> AdditionalCounts;
56*0b57cec5SDimitry Andric   for (auto &E : SCCEdges) {
57*0b57cec5SDimitry Andric     auto OptProfCount = GetProfCount(E.first, E.second);
58*0b57cec5SDimitry Andric     if (!OptProfCount)
59*0b57cec5SDimitry Andric       continue;
60*0b57cec5SDimitry Andric     auto Callee = CGT::edge_dest(E.second);
61*0b57cec5SDimitry Andric     AdditionalCounts[Callee] += OptProfCount.getValue();
62*0b57cec5SDimitry Andric   }
63*0b57cec5SDimitry Andric 
64*0b57cec5SDimitry Andric   // Update the counts for the nodes in the SCC.
65*0b57cec5SDimitry Andric   for (auto &Entry : AdditionalCounts)
66*0b57cec5SDimitry Andric     AddCount(Entry.first, Entry.second);
67*0b57cec5SDimitry Andric 
68*0b57cec5SDimitry Andric   // Now update the counts for nodes outside the SCC.
69*0b57cec5SDimitry Andric   for (auto &E : NonSCCEdges) {
70*0b57cec5SDimitry Andric     auto OptProfCount = GetProfCount(E.first, E.second);
71*0b57cec5SDimitry Andric     if (!OptProfCount)
72*0b57cec5SDimitry Andric       continue;
73*0b57cec5SDimitry Andric     auto Callee = CGT::edge_dest(E.second);
74*0b57cec5SDimitry Andric     AddCount(Callee, OptProfCount.getValue());
75*0b57cec5SDimitry Andric   }
76*0b57cec5SDimitry Andric }
77*0b57cec5SDimitry Andric 
78*0b57cec5SDimitry Andric /// Propgate synthetic entry counts on a callgraph \p CG.
79*0b57cec5SDimitry Andric ///
80*0b57cec5SDimitry Andric /// This performs a reverse post-order traversal of the callgraph SCC. For each
81*0b57cec5SDimitry Andric /// SCC, it first propagates the entry counts to the nodes within the SCC
82*0b57cec5SDimitry Andric /// through call edges and updates them in one shot. Then the entry counts are
83*0b57cec5SDimitry Andric /// propagated to nodes outside the SCC. This requires \p GraphTraits
84*0b57cec5SDimitry Andric /// to have a specialization for \p CallGraphType.
85*0b57cec5SDimitry Andric 
86*0b57cec5SDimitry Andric template <typename CallGraphType>
87*0b57cec5SDimitry Andric void SyntheticCountsUtils<CallGraphType>::propagate(const CallGraphType &CG,
88*0b57cec5SDimitry Andric                                                     GetProfCountTy GetProfCount,
89*0b57cec5SDimitry Andric                                                     AddCountTy AddCount) {
90*0b57cec5SDimitry Andric   std::vector<SccTy> SCCs;
91*0b57cec5SDimitry Andric 
92*0b57cec5SDimitry Andric   // Collect all the SCCs.
93*0b57cec5SDimitry Andric   for (auto I = scc_begin(CG); !I.isAtEnd(); ++I)
94*0b57cec5SDimitry Andric     SCCs.push_back(*I);
95*0b57cec5SDimitry Andric 
96*0b57cec5SDimitry Andric   // The callgraph-scc needs to be visited in top-down order for propagation.
97*0b57cec5SDimitry Andric   // The scc iterator returns the scc in bottom-up order, so reverse the SCCs
98*0b57cec5SDimitry Andric   // and call propagateFromSCC.
99*0b57cec5SDimitry Andric   for (auto &SCC : reverse(SCCs))
100*0b57cec5SDimitry Andric     propagateFromSCC(SCC, GetProfCount, AddCount);
101*0b57cec5SDimitry Andric }
102*0b57cec5SDimitry Andric 
103*0b57cec5SDimitry Andric template class llvm::SyntheticCountsUtils<const CallGraph *>;
104*0b57cec5SDimitry Andric template class llvm::SyntheticCountsUtils<ModuleSummaryIndex *>;
105