xref: /openbsd-src/gnu/llvm/llvm/lib/Transforms/Instrumentation/ValueProfileCollector.cpp (revision d415bd752c734aee168c4ee86ff32e8cc249eb16)
109467b48Spatrick //===- ValueProfileCollector.cpp - determine what to value profile --------===//
209467b48Spatrick //
309467b48Spatrick // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
409467b48Spatrick // See https://llvm.org/LICENSE.txt for license information.
509467b48Spatrick // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
609467b48Spatrick //
709467b48Spatrick //===----------------------------------------------------------------------===//
809467b48Spatrick //
909467b48Spatrick // The implementation of the ValueProfileCollector via ValueProfileCollectorImpl
1009467b48Spatrick //
1109467b48Spatrick //===----------------------------------------------------------------------===//
1209467b48Spatrick 
13*d415bd75Srobert #include "ValueProfileCollector.h"
1409467b48Spatrick #include "ValueProfilePlugins.inc"
15*d415bd75Srobert #include "llvm/ProfileData/InstrProf.h"
1609467b48Spatrick 
1709467b48Spatrick using namespace llvm;
1809467b48Spatrick 
1909467b48Spatrick namespace {
2009467b48Spatrick 
2109467b48Spatrick /// A plugin-based class that takes an arbitrary number of Plugin types.
2209467b48Spatrick /// Each plugin type must satisfy the following API:
2309467b48Spatrick ///  1) the constructor must take a `Function &f`. Typically, the plugin would
2409467b48Spatrick ///     scan the function looking for candidates.
2509467b48Spatrick ///  2) contain a member function with the following signature and name:
2609467b48Spatrick ///        void run(std::vector<CandidateInfo> &Candidates);
2709467b48Spatrick ///    such that the plugin would append its result into the vector parameter.
2809467b48Spatrick ///
2909467b48Spatrick /// Plugins are defined in ValueProfilePlugins.inc
3009467b48Spatrick template <class... Ts> class PluginChain;
3109467b48Spatrick 
3209467b48Spatrick /// The type PluginChainFinal is the final chain of plugins that will be used by
3309467b48Spatrick /// ValueProfileCollectorImpl.
3409467b48Spatrick using PluginChainFinal = PluginChain<VP_PLUGIN_LIST>;
3509467b48Spatrick 
3609467b48Spatrick template <> class PluginChain<> {
3709467b48Spatrick public:
PluginChain(Function & F,TargetLibraryInfo & TLI)38097a140dSpatrick   PluginChain(Function &F, TargetLibraryInfo &TLI) {}
get(InstrProfValueKind K,std::vector<CandidateInfo> & Candidates)3909467b48Spatrick   void get(InstrProfValueKind K, std::vector<CandidateInfo> &Candidates) {}
4009467b48Spatrick };
4109467b48Spatrick 
4209467b48Spatrick template <class PluginT, class... Ts>
4309467b48Spatrick class PluginChain<PluginT, Ts...> : public PluginChain<Ts...> {
4409467b48Spatrick   PluginT Plugin;
4509467b48Spatrick   using Base = PluginChain<Ts...>;
4609467b48Spatrick 
4709467b48Spatrick public:
PluginChain(Function & F,TargetLibraryInfo & TLI)48097a140dSpatrick   PluginChain(Function &F, TargetLibraryInfo &TLI)
49097a140dSpatrick       : PluginChain<Ts...>(F, TLI), Plugin(F, TLI) {}
5009467b48Spatrick 
get(InstrProfValueKind K,std::vector<CandidateInfo> & Candidates)5109467b48Spatrick   void get(InstrProfValueKind K, std::vector<CandidateInfo> &Candidates) {
5209467b48Spatrick     if (K == PluginT::Kind)
5309467b48Spatrick       Plugin.run(Candidates);
5409467b48Spatrick     Base::get(K, Candidates);
5509467b48Spatrick   }
5609467b48Spatrick };
5709467b48Spatrick 
5809467b48Spatrick } // end anonymous namespace
5909467b48Spatrick 
6009467b48Spatrick /// ValueProfileCollectorImpl inherits the API of PluginChainFinal.
6109467b48Spatrick class ValueProfileCollector::ValueProfileCollectorImpl : public PluginChainFinal {
6209467b48Spatrick public:
6309467b48Spatrick   using PluginChainFinal::PluginChainFinal;
6409467b48Spatrick };
6509467b48Spatrick 
ValueProfileCollector(Function & F,TargetLibraryInfo & TLI)66097a140dSpatrick ValueProfileCollector::ValueProfileCollector(Function &F,
67097a140dSpatrick                                              TargetLibraryInfo &TLI)
68097a140dSpatrick     : PImpl(new ValueProfileCollectorImpl(F, TLI)) {}
6909467b48Spatrick 
7009467b48Spatrick ValueProfileCollector::~ValueProfileCollector() = default;
7109467b48Spatrick 
7209467b48Spatrick std::vector<CandidateInfo>
get(InstrProfValueKind Kind) const7309467b48Spatrick ValueProfileCollector::get(InstrProfValueKind Kind) const {
7409467b48Spatrick   std::vector<CandidateInfo> Result;
7509467b48Spatrick   PImpl->get(Kind, Result);
7609467b48Spatrick   return Result;
7709467b48Spatrick }
78