xref: /openbsd-src/gnu/llvm/llvm/lib/Analysis/SyntheticCountsUtils.cpp (revision d415bd752c734aee168c4ee86ff32e8cc249eb16)
109467b48Spatrick //===--- SyntheticCountsUtils.cpp - synthetic counts propagation utils ---===//
209467b48Spatrick //
309467b48Spatrick // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
409467b48Spatrick // See https://llvm.org/LICENSE.txt for license information.
509467b48Spatrick // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
609467b48Spatrick //
709467b48Spatrick //===----------------------------------------------------------------------===//
809467b48Spatrick //
909467b48Spatrick // This file defines utilities for propagating synthetic counts.
1009467b48Spatrick //
1109467b48Spatrick //===----------------------------------------------------------------------===//
1209467b48Spatrick 
1309467b48Spatrick #include "llvm/Analysis/SyntheticCountsUtils.h"
1409467b48Spatrick #include "llvm/ADT/DenseSet.h"
1509467b48Spatrick #include "llvm/ADT/SCCIterator.h"
1609467b48Spatrick #include "llvm/Analysis/CallGraph.h"
1709467b48Spatrick #include "llvm/IR/ModuleSummaryIndex.h"
1809467b48Spatrick 
1909467b48Spatrick using namespace llvm;
2009467b48Spatrick 
2109467b48Spatrick // Given an SCC, propagate entry counts along the edge of the SCC nodes.
2209467b48Spatrick template <typename CallGraphType>
propagateFromSCC(const SccTy & SCC,GetProfCountTy GetProfCount,AddCountTy AddCount)2309467b48Spatrick void SyntheticCountsUtils<CallGraphType>::propagateFromSCC(
2409467b48Spatrick     const SccTy &SCC, GetProfCountTy GetProfCount, AddCountTy AddCount) {
2509467b48Spatrick 
2609467b48Spatrick   DenseSet<NodeRef> SCCNodes;
2709467b48Spatrick   SmallVector<std::pair<NodeRef, EdgeRef>, 8> SCCEdges, NonSCCEdges;
2809467b48Spatrick 
2909467b48Spatrick   for (auto &Node : SCC)
3009467b48Spatrick     SCCNodes.insert(Node);
3109467b48Spatrick 
3209467b48Spatrick   // Partition the edges coming out of the SCC into those whose destination is
3309467b48Spatrick   // in the SCC and the rest.
3409467b48Spatrick   for (const auto &Node : SCCNodes) {
3509467b48Spatrick     for (auto &E : children_edges<CallGraphType>(Node)) {
3609467b48Spatrick       if (SCCNodes.count(CGT::edge_dest(E)))
3709467b48Spatrick         SCCEdges.emplace_back(Node, E);
3809467b48Spatrick       else
3909467b48Spatrick         NonSCCEdges.emplace_back(Node, E);
4009467b48Spatrick     }
4109467b48Spatrick   }
4209467b48Spatrick 
4309467b48Spatrick   // For nodes in the same SCC, update the counts in two steps:
4409467b48Spatrick   // 1. Compute the additional count for each node by propagating the counts
4509467b48Spatrick   // along all incoming edges to the node that originate from within the same
4609467b48Spatrick   // SCC and summing them up.
4709467b48Spatrick   // 2. Add the additional counts to the nodes in the SCC.
4809467b48Spatrick   // This ensures that the order of
4909467b48Spatrick   // traversal of nodes within the SCC doesn't affect the final result.
5009467b48Spatrick 
5109467b48Spatrick   DenseMap<NodeRef, Scaled64> AdditionalCounts;
5209467b48Spatrick   for (auto &E : SCCEdges) {
5309467b48Spatrick     auto OptProfCount = GetProfCount(E.first, E.second);
5409467b48Spatrick     if (!OptProfCount)
5509467b48Spatrick       continue;
5609467b48Spatrick     auto Callee = CGT::edge_dest(E.second);
57*d415bd75Srobert     AdditionalCounts[Callee] += *OptProfCount;
5809467b48Spatrick   }
5909467b48Spatrick 
6009467b48Spatrick   // Update the counts for the nodes in the SCC.
6109467b48Spatrick   for (auto &Entry : AdditionalCounts)
6209467b48Spatrick     AddCount(Entry.first, Entry.second);
6309467b48Spatrick 
6409467b48Spatrick   // Now update the counts for nodes outside the SCC.
6509467b48Spatrick   for (auto &E : NonSCCEdges) {
6609467b48Spatrick     auto OptProfCount = GetProfCount(E.first, E.second);
6709467b48Spatrick     if (!OptProfCount)
6809467b48Spatrick       continue;
6909467b48Spatrick     auto Callee = CGT::edge_dest(E.second);
70*d415bd75Srobert     AddCount(Callee, *OptProfCount);
7109467b48Spatrick   }
7209467b48Spatrick }
7309467b48Spatrick 
7409467b48Spatrick /// Propgate synthetic entry counts on a callgraph \p CG.
7509467b48Spatrick ///
7609467b48Spatrick /// This performs a reverse post-order traversal of the callgraph SCC. For each
7709467b48Spatrick /// SCC, it first propagates the entry counts to the nodes within the SCC
7809467b48Spatrick /// through call edges and updates them in one shot. Then the entry counts are
7909467b48Spatrick /// propagated to nodes outside the SCC. This requires \p GraphTraits
8009467b48Spatrick /// to have a specialization for \p CallGraphType.
8109467b48Spatrick 
8209467b48Spatrick template <typename CallGraphType>
propagate(const CallGraphType & CG,GetProfCountTy GetProfCount,AddCountTy AddCount)8309467b48Spatrick void SyntheticCountsUtils<CallGraphType>::propagate(const CallGraphType &CG,
8409467b48Spatrick                                                     GetProfCountTy GetProfCount,
8509467b48Spatrick                                                     AddCountTy AddCount) {
8609467b48Spatrick   std::vector<SccTy> SCCs;
8709467b48Spatrick 
8809467b48Spatrick   // Collect all the SCCs.
8909467b48Spatrick   for (auto I = scc_begin(CG); !I.isAtEnd(); ++I)
9009467b48Spatrick     SCCs.push_back(*I);
9109467b48Spatrick 
9209467b48Spatrick   // The callgraph-scc needs to be visited in top-down order for propagation.
9309467b48Spatrick   // The scc iterator returns the scc in bottom-up order, so reverse the SCCs
9409467b48Spatrick   // and call propagateFromSCC.
9509467b48Spatrick   for (auto &SCC : reverse(SCCs))
9609467b48Spatrick     propagateFromSCC(SCC, GetProfCount, AddCount);
9709467b48Spatrick }
9809467b48Spatrick 
9909467b48Spatrick template class llvm::SyntheticCountsUtils<const CallGraph *>;
10009467b48Spatrick template class llvm::SyntheticCountsUtils<ModuleSummaryIndex *>;
101