Path: blob/main/contrib/llvm-project/llvm/lib/Transforms/Instrumentation/ValueProfileCollector.cpp
35266 views
//===- ValueProfileCollector.cpp - determine what to value profile --------===//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// The implementation of the ValueProfileCollector via ValueProfileCollectorImpl9//10//===----------------------------------------------------------------------===//1112#include "ValueProfileCollector.h"13#include "ValueProfilePlugins.inc"14#include "llvm/ProfileData/InstrProf.h"1516using namespace llvm;1718namespace {1920/// A plugin-based class that takes an arbitrary number of Plugin types.21/// Each plugin type must satisfy the following API:22/// 1) the constructor must take a `Function &f`. Typically, the plugin would23/// scan the function looking for candidates.24/// 2) contain a member function with the following signature and name:25/// void run(std::vector<CandidateInfo> &Candidates);26/// such that the plugin would append its result into the vector parameter.27///28/// Plugins are defined in ValueProfilePlugins.inc29template <class... Ts> class PluginChain;3031/// The type PluginChainFinal is the final chain of plugins that will be used by32/// ValueProfileCollectorImpl.33using PluginChainFinal = PluginChain<VP_PLUGIN_LIST>;3435template <> class PluginChain<> {36public:37PluginChain(Function &F, TargetLibraryInfo &TLI) {}38void get(InstrProfValueKind K, std::vector<CandidateInfo> &Candidates) {}39};4041template <class PluginT, class... Ts>42class PluginChain<PluginT, Ts...> : public PluginChain<Ts...> {43PluginT Plugin;44using Base = PluginChain<Ts...>;4546public:47PluginChain(Function &F, TargetLibraryInfo &TLI)48: PluginChain<Ts...>(F, TLI), Plugin(F, TLI) {}4950void get(InstrProfValueKind K, std::vector<CandidateInfo> &Candidates) {51if (K == PluginT::Kind)52Plugin.run(Candidates);53Base::get(K, Candidates);54}55};5657} // end anonymous namespace5859/// ValueProfileCollectorImpl inherits the API of PluginChainFinal.60class ValueProfileCollector::ValueProfileCollectorImpl : public PluginChainFinal {61public:62using PluginChainFinal::PluginChainFinal;63};6465ValueProfileCollector::ValueProfileCollector(Function &F,66TargetLibraryInfo &TLI)67: PImpl(new ValueProfileCollectorImpl(F, TLI)) {}6869ValueProfileCollector::~ValueProfileCollector() = default;7071std::vector<CandidateInfo>72ValueProfileCollector::get(InstrProfValueKind Kind) const {73std::vector<CandidateInfo> Result;74PImpl->get(Kind, Result);75return Result;76}777879