Path: blob/main/contrib/llvm-project/llvm/lib/LTO/SummaryBasedOptimizations.cpp
35234 views
//==-SummaryBasedOptimizations.cpp - Optimizations based on ThinLTO summary-==//1//2// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.3// See https://llvm.org/LICENSE.txt for license information.4// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception5//6//===----------------------------------------------------------------------===//7//8// This file implements optimizations that are based on the module summaries.9// These optimizations are performed during the thinlink phase of the10// compilation.11//12//===----------------------------------------------------------------------===//1314#include "llvm/LTO/SummaryBasedOptimizations.h"15#include "llvm/Analysis/SyntheticCountsUtils.h"16#include "llvm/IR/ModuleSummaryIndex.h"17#include "llvm/Support/CommandLine.h"1819using namespace llvm;2021static cl::opt<bool> ThinLTOSynthesizeEntryCounts(22"thinlto-synthesize-entry-counts", cl::init(false), cl::Hidden,23cl::desc("Synthesize entry counts based on the summary"));2425namespace llvm {26extern cl::opt<int> InitialSyntheticCount;27}2829static void initializeCounts(ModuleSummaryIndex &Index) {30auto Root = Index.calculateCallGraphRoot();31// Root is a fake node. All its successors are the actual roots of the32// callgraph.33// FIXME: This initializes the entry counts of only the root nodes. This makes34// sense when compiling a binary with ThinLTO, but for libraries any of the35// non-root nodes could be called from outside.36for (auto &C : Root.calls()) {37auto &V = C.first;38for (auto &GVS : V.getSummaryList()) {39auto S = GVS.get()->getBaseObject();40auto *F = cast<FunctionSummary>(S);41F->setEntryCount(InitialSyntheticCount);42}43}44}4546void llvm::computeSyntheticCounts(ModuleSummaryIndex &Index) {47if (!ThinLTOSynthesizeEntryCounts)48return;4950using Scaled64 = ScaledNumber<uint64_t>;51initializeCounts(Index);52auto GetCallSiteRelFreq = [](FunctionSummary::EdgeTy &Edge) {53return Scaled64(Edge.second.RelBlockFreq, -CalleeInfo::ScaleShift);54};55auto GetEntryCount = [](ValueInfo V) {56if (V.getSummaryList().size()) {57auto S = V.getSummaryList().front()->getBaseObject();58auto *F = cast<FunctionSummary>(S);59return F->entryCount();60} else {61return UINT64_C(0);62}63};64auto AddToEntryCount = [](ValueInfo V, Scaled64 New) {65if (!V.getSummaryList().size())66return;67for (auto &GVS : V.getSummaryList()) {68auto S = GVS.get()->getBaseObject();69auto *F = cast<FunctionSummary>(S);70F->setEntryCount(71SaturatingAdd(F->entryCount(), New.template toInt<uint64_t>()));72}73};7475auto GetProfileCount = [&](ValueInfo V, FunctionSummary::EdgeTy &Edge) {76auto RelFreq = GetCallSiteRelFreq(Edge);77Scaled64 EC(GetEntryCount(V), 0);78return RelFreq * EC;79};80// After initializing the counts in initializeCounts above, the counts have to81// be propagated across the combined callgraph.82// SyntheticCountsUtils::propagate takes care of this propagation on any83// callgraph that specialized GraphTraits.84SyntheticCountsUtils<ModuleSummaryIndex *>::propagate(&Index, GetProfileCount,85AddToEntryCount);86Index.setHasSyntheticEntryCounts();87}888990