Coverage Report

Created: 2019-07-24 05:18

/Users/buildslave/jenkins/workspace/clang-stage2-coverage-R/llvm/lib/LTO/SummaryBasedOptimizations.cpp
Line
Count
Source (jump to first uncovered line)
1
//==-SummaryBasedOptimizations.cpp - Optimizations based on ThinLTO summary-==//
2
//
3
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4
// See https://llvm.org/LICENSE.txt for license information.
5
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6
//
7
//===----------------------------------------------------------------------===//
8
//
9
// This file implements optimizations that are based on the module summaries.
10
// These optimizations are performed during the thinlink phase of the
11
// compilation.
12
//
13
//===----------------------------------------------------------------------===//
14
15
#include "llvm/LTO/SummaryBasedOptimizations.h"
16
#include "llvm/Analysis/SyntheticCountsUtils.h"
17
#include "llvm/IR/ModuleSummaryIndex.h"
18
19
using namespace llvm;
20
21
cl::opt<bool> ThinLTOSynthesizeEntryCounts(
22
    "thinlto-synthesize-entry-counts", cl::init(false), cl::Hidden,
23
    cl::desc("Synthesize entry counts based on the summary"));
24
25
extern cl::opt<int> InitialSyntheticCount;
26
27
3
static void initializeCounts(ModuleSummaryIndex &Index) {
28
3
  auto Root = Index.calculateCallGraphRoot();
29
3
  // Root is a fake node. All its successors are the actual roots of the
30
3
  // callgraph.
31
3
  // FIXME: This initializes the entry counts of only the root nodes. This makes
32
3
  // sense when compiling a binary with ThinLTO, but for libraries any of the
33
3
  // non-root nodes could be called from outside.
34
3
  for (auto &C : Root.calls()) {
35
2
    auto &V = C.first;
36
2
    for (auto &GVS : V.getSummaryList()) {
37
2
      auto S = GVS.get()->getBaseObject();
38
2
      auto *F = cast<FunctionSummary>(S);
39
2
      F->setEntryCount(InitialSyntheticCount);
40
2
    }
41
2
  }
42
3
}
43
44
236
void llvm::computeSyntheticCounts(ModuleSummaryIndex &Index) {
45
236
  if (!ThinLTOSynthesizeEntryCounts)
46
233
    return;
47
3
48
3
  using Scaled64 = ScaledNumber<uint64_t>;
49
3
  initializeCounts(Index);
50
6
  auto GetCallSiteRelFreq = [](FunctionSummary::EdgeTy &Edge) {
51
6
    return Scaled64(Edge.second.RelBlockFreq, -CalleeInfo::ScaleShift);
52
6
  };
53
6
  auto GetEntryCount = [](ValueInfo V) {
54
6
    if (V.getSummaryList().size()) {
55
6
      auto S = V.getSummaryList().front().get()->getBaseObject();
56
6
      auto *F = cast<FunctionSummary>(S);
57
6
      return F->entryCount();
58
6
    } else {
59
0
      return UINT64_C(0);
60
0
    }
61
6
  };
62
6
  auto AddToEntryCount = [](ValueInfo V, Scaled64 New) {
63
6
    if (!V.getSummaryList().size())
64
0
      return;
65
6
    for (auto &GVS : V.getSummaryList()) {
66
6
      auto S = GVS.get()->getBaseObject();
67
6
      auto *F = cast<FunctionSummary>(S);
68
6
      F->setEntryCount(
69
6
          SaturatingAdd(F->entryCount(), New.template toInt<uint64_t>()));
70
6
    }
71
6
  };
72
3
73
6
  auto GetProfileCount = [&](ValueInfo V, FunctionSummary::EdgeTy &Edge) {
74
6
    auto RelFreq = GetCallSiteRelFreq(Edge);
75
6
    Scaled64 EC(GetEntryCount(V), 0);
76
6
    return RelFreq * EC;
77
6
  };
78
3
  // After initializing the counts in initializeCounts above, the counts have to
79
3
  // be propagated across the combined callgraph.
80
3
  // SyntheticCountsUtils::propagate takes care of this propagation on any
81
3
  // callgraph that specialized GraphTraits.
82
3
  SyntheticCountsUtils<ModuleSummaryIndex *>::propagate(&Index, GetProfileCount,
83
3
                                                        AddToEntryCount);
84
3
  Index.setHasSyntheticEntryCounts();
85
3
}