blob: c2d7bb11a4cf2213a5848f5229ae09a623955117 [file] [log] [blame]
Easwaran Raman283d9782018-01-09 19:39:35 +00001//===--- SyntheticCountsUtils.cpp - synthetic counts propagation utils ---===//
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 propagating synthetic counts.
11//
12//===----------------------------------------------------------------------===//
13
14#include "llvm/Analysis/SyntheticCountsUtils.h"
15#include "llvm/ADT/DenseSet.h"
16#include "llvm/ADT/SCCIterator.h"
Easwaran Raman283d9782018-01-09 19:39:35 +000017#include "llvm/Analysis/CallGraph.h"
18#include "llvm/IR/CallSite.h"
19#include "llvm/IR/Function.h"
20#include "llvm/IR/InstIterator.h"
21#include "llvm/IR/Instructions.h"
Easwaran Ramanf1f1adc2018-12-13 19:54:27 +000022#include "llvm/IR/ModuleSummaryIndex.h"
Easwaran Raman283d9782018-01-09 19:39:35 +000023
24using namespace llvm;
25
Easwaran Ramanefb87172018-01-25 22:02:29 +000026// Given an SCC, propagate entry counts along the edge of the SCC nodes.
27template <typename CallGraphType>
28void SyntheticCountsUtils<CallGraphType>::propagateFromSCC(
Easwaran Raman116e0802019-01-09 20:10:27 +000029 const SccTy &SCC, GetProfCountTy GetProfCount, AddCountTy AddCount) {
Easwaran Raman283d9782018-01-09 19:39:35 +000030
Easwaran Ramanf1f1adc2018-12-13 19:54:27 +000031 DenseSet<NodeRef> SCCNodes;
Easwaran Ramanefb87172018-01-25 22:02:29 +000032 SmallVector<std::pair<NodeRef, EdgeRef>, 8> SCCEdges, NonSCCEdges;
Easwaran Raman283d9782018-01-09 19:39:35 +000033
Easwaran Ramanefb87172018-01-25 22:02:29 +000034 for (auto &Node : SCC)
35 SCCNodes.insert(Node);
36
37 // Partition the edges coming out of the SCC into those whose destination is
38 // in the SCC and the rest.
39 for (const auto &Node : SCCNodes) {
Easwaran Raman51c93412018-02-01 19:40:35 +000040 for (auto &E : children_edges<CallGraphType>(Node)) {
Easwaran Ramanefb87172018-01-25 22:02:29 +000041 if (SCCNodes.count(CGT::edge_dest(E)))
42 SCCEdges.emplace_back(Node, E);
43 else
44 NonSCCEdges.emplace_back(Node, E);
Easwaran Raman283d9782018-01-09 19:39:35 +000045 }
Easwaran Ramanefb87172018-01-25 22:02:29 +000046 }
Easwaran Raman283d9782018-01-09 19:39:35 +000047
Easwaran Ramanefb87172018-01-25 22:02:29 +000048 // For nodes in the same SCC, update the counts in two steps:
49 // 1. Compute the additional count for each node by propagating the counts
50 // along all incoming edges to the node that originate from within the same
51 // SCC and summing them up.
52 // 2. Add the additional counts to the nodes in the SCC.
Easwaran Raman283d9782018-01-09 19:39:35 +000053 // This ensures that the order of
Easwaran Ramanefb87172018-01-25 22:02:29 +000054 // traversal of nodes within the SCC doesn't affect the final result.
Easwaran Raman283d9782018-01-09 19:39:35 +000055
Easwaran Raman116e0802019-01-09 20:10:27 +000056 DenseMap<NodeRef, Scaled64> AdditionalCounts;
Easwaran Ramanefb87172018-01-25 22:02:29 +000057 for (auto &E : SCCEdges) {
Easwaran Raman116e0802019-01-09 20:10:27 +000058 auto OptProfCount = GetProfCount(E.first, E.second);
59 if (!OptProfCount)
Easwaran Ramanefb87172018-01-25 22:02:29 +000060 continue;
Easwaran Ramanefb87172018-01-25 22:02:29 +000061 auto Callee = CGT::edge_dest(E.second);
Easwaran Raman116e0802019-01-09 20:10:27 +000062 AdditionalCounts[Callee] += OptProfCount.getValue();
Easwaran Raman283d9782018-01-09 19:39:35 +000063 }
64
Easwaran Ramanefb87172018-01-25 22:02:29 +000065 // Update the counts for the nodes in the SCC.
Easwaran Raman283d9782018-01-09 19:39:35 +000066 for (auto &Entry : AdditionalCounts)
Easwaran Ramanefb87172018-01-25 22:02:29 +000067 AddCount(Entry.first, Entry.second);
Easwaran Raman283d9782018-01-09 19:39:35 +000068
Easwaran Ramanefb87172018-01-25 22:02:29 +000069 // Now update the counts for nodes outside the SCC.
70 for (auto &E : NonSCCEdges) {
Easwaran Raman116e0802019-01-09 20:10:27 +000071 auto OptProfCount = GetProfCount(E.first, E.second);
72 if (!OptProfCount)
Easwaran Ramanefb87172018-01-25 22:02:29 +000073 continue;
Easwaran Ramanefb87172018-01-25 22:02:29 +000074 auto Callee = CGT::edge_dest(E.second);
Easwaran Raman116e0802019-01-09 20:10:27 +000075 AddCount(Callee, OptProfCount.getValue());
Easwaran Raman283d9782018-01-09 19:39:35 +000076 }
77}
78
Easwaran Ramanefb87172018-01-25 22:02:29 +000079/// Propgate synthetic entry counts on a callgraph \p CG.
Easwaran Raman283d9782018-01-09 19:39:35 +000080///
81/// This performs a reverse post-order traversal of the callgraph SCC. For each
Easwaran Ramanefb87172018-01-25 22:02:29 +000082/// SCC, it first propagates the entry counts to the nodes within the SCC
Easwaran Raman283d9782018-01-09 19:39:35 +000083/// through call edges and updates them in one shot. Then the entry counts are
Easwaran Raman51c93412018-02-01 19:40:35 +000084/// propagated to nodes outside the SCC. This requires \p GraphTraits
Easwaran Ramanefb87172018-01-25 22:02:29 +000085/// to have a specialization for \p CallGraphType.
Easwaran Raman283d9782018-01-09 19:39:35 +000086
Easwaran Ramanefb87172018-01-25 22:02:29 +000087template <typename CallGraphType>
88void SyntheticCountsUtils<CallGraphType>::propagate(const CallGraphType &CG,
Easwaran Raman116e0802019-01-09 20:10:27 +000089 GetProfCountTy GetProfCount,
Easwaran Ramanefb87172018-01-25 22:02:29 +000090 AddCountTy AddCount) {
91 std::vector<SccTy> SCCs;
Easwaran Raman283d9782018-01-09 19:39:35 +000092
Easwaran Ramanefb87172018-01-25 22:02:29 +000093 // Collect all the SCCs.
94 for (auto I = scc_begin(CG); !I.isAtEnd(); ++I)
95 SCCs.push_back(*I);
Easwaran Raman283d9782018-01-09 19:39:35 +000096
Easwaran Ramanefb87172018-01-25 22:02:29 +000097 // The callgraph-scc needs to be visited in top-down order for propagation.
98 // The scc iterator returns the scc in bottom-up order, so reverse the SCCs
99 // and call propagateFromSCC.
100 for (auto &SCC : reverse(SCCs))
Easwaran Raman116e0802019-01-09 20:10:27 +0000101 propagateFromSCC(SCC, GetProfCount, AddCount);
Easwaran Raman283d9782018-01-09 19:39:35 +0000102}
Easwaran Ramanefb87172018-01-25 22:02:29 +0000103
104template class llvm::SyntheticCountsUtils<const CallGraph *>;
Easwaran Ramanf1f1adc2018-12-13 19:54:27 +0000105template class llvm::SyntheticCountsUtils<ModuleSummaryIndex *>;