blob: 0276d14a14fe0df5967a7c59efeb6fb088b6aaaf [file] [log] [blame]
Easwaran Ramanbdf20262018-01-09 19:39:35 +00001//=- SyntheticCountsPropagation.cpp - Propagate function counts --*- 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 implements a transformation that synthesizes entry counts for
11// functions and attaches !prof metadata to functions with the synthesized
12// counts. The presence of !prof metadata with counter name set to
13// 'synthesized_function_entry_count' indicate that the value of the counter is
14// an estimation of the likely execution count of the function. This transform
15// is applied only in non PGO mode as functions get 'real' profile-based
16// function entry counts in the PGO mode.
17//
18// The transformation works by first assigning some initial values to the entry
19// counts of all functions and then doing a top-down traversal of the
20// callgraph-scc to propagate the counts. For each function the set of callsites
21// and their relative block frequency is gathered. The relative block frequency
22// multiplied by the entry count of the caller and added to the callee's entry
23// count. For non-trivial SCCs, the new counts are computed from the previous
24// counts and updated in one shot.
25//
26//===----------------------------------------------------------------------===//
27
28#include "llvm/Transforms/IPO/SyntheticCountsPropagation.h"
29#include "llvm/ADT/DenseSet.h"
30#include "llvm/ADT/STLExtras.h"
31#include "llvm/Analysis/BlockFrequencyInfo.h"
32#include "llvm/Analysis/CallGraph.h"
33#include "llvm/Analysis/SyntheticCountsUtils.h"
34#include "llvm/IR/CallSite.h"
35#include "llvm/IR/Function.h"
36#include "llvm/IR/Instructions.h"
37#include "llvm/IR/Module.h"
38#include "llvm/Support/CommandLine.h"
39#include "llvm/Support/Debug.h"
40#include "llvm/Support/raw_ostream.h"
41
42using namespace llvm;
43using Scaled64 = ScaledNumber<uint64_t>;
44
45#define DEBUG_TYPE "synthetic-counts-propagation"
46
47/// Initial synthetic count assigned to functions.
48static cl::opt<int>
49 InitialSyntheticCount("initial-synthetic-count", cl::Hidden, cl::init(10),
50 cl::ZeroOrMore,
51 cl::desc("Initial value of synthetic entry count."));
52
53/// Initial synthetic count assigned to inline functions.
54static cl::opt<int> InlineSyntheticCount(
55 "inline-synthetic-count", cl::Hidden, cl::init(15), cl::ZeroOrMore,
56 cl::desc("Initial synthetic entry count for inline functions."));
57
58/// Initial synthetic count assigned to cold functions.
59static cl::opt<int> ColdSyntheticCount(
60 "cold-synthetic-count", cl::Hidden, cl::init(5), cl::ZeroOrMore,
61 cl::desc("Initial synthetic entry count for cold functions."));
62
63// Assign initial synthetic entry counts to functions.
64static void
65initializeCounts(Module &M, function_ref<void(Function *, uint64_t)> SetCount) {
66 auto MayHaveIndirectCalls = [](Function &F) {
67 for (auto *U : F.users()) {
68 if (!isa<CallInst>(U) && !isa<InvokeInst>(U))
69 return true;
70 }
71 return false;
72 };
73
74 for (Function &F : M) {
75 uint64_t InitialCount = InitialSyntheticCount;
76 if (F.isDeclaration())
77 continue;
78 if (F.hasFnAttribute(Attribute::AlwaysInline) ||
79 F.hasFnAttribute(Attribute::InlineHint)) {
80 // Use a higher value for inline functions to account for the fact that
81 // these are usually beneficial to inline.
82 InitialCount = InlineSyntheticCount;
83 } else if (F.hasLocalLinkage() && !MayHaveIndirectCalls(F)) {
84 // Local functions without inline hints get counts only through
85 // propagation.
86 InitialCount = 0;
87 } else if (F.hasFnAttribute(Attribute::Cold) ||
88 F.hasFnAttribute(Attribute::NoInline)) {
89 // Use a lower value for noinline and cold functions.
90 InitialCount = ColdSyntheticCount;
91 }
92 SetCount(&F, InitialCount);
93 }
94}
95
96PreservedAnalyses SyntheticCountsPropagation::run(Module &M,
97 ModuleAnalysisManager &MAM) {
98 FunctionAnalysisManager &FAM =
99 MAM.getResult<FunctionAnalysisManagerModuleProxy>(M).getManager();
100 DenseMap<Function *, uint64_t> Counts;
101 // Set initial entry counts.
102 initializeCounts(M, [&](Function *F, uint64_t Count) { Counts[F] = Count; });
103
104 // Compute the relative block frequency for a callsite. Use scaled numbers
105 // and not integers since the relative block frequency could be less than 1.
106 auto GetCallSiteRelFreq = [&](CallSite CS) {
107 Function *Caller = CS.getCaller();
108 auto &BFI = FAM.getResult<BlockFrequencyAnalysis>(*Caller);
109 BasicBlock *CSBB = CS.getInstruction()->getParent();
110 Scaled64 EntryFreq(BFI.getEntryFreq(), 0);
111 Scaled64 BBFreq(BFI.getBlockFreq(CSBB).getFrequency(), 0);
112 BBFreq /= EntryFreq;
113 return BBFreq;
114 };
115
116 CallGraph CG(M);
117 // Propgate the entry counts on the callgraph.
118 propagateSyntheticCounts(
119 CG, GetCallSiteRelFreq, [&](Function *F) { return Counts[F]; },
120 [&](Function *F, uint64_t New) { Counts[F] += New; });
121
122 // Set the counts as metadata.
123 for (auto Entry : Counts)
124 Entry.first->setEntryCount(Entry.second, true);
125
126 return PreservedAnalyses::all();
127}