18bcb0991SDimitry Andric //===- ValueProfileCollector.cpp - determine what to value profile --------===//
28bcb0991SDimitry Andric //
38bcb0991SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
48bcb0991SDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
58bcb0991SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
68bcb0991SDimitry Andric //
78bcb0991SDimitry Andric //===----------------------------------------------------------------------===//
88bcb0991SDimitry Andric //
98bcb0991SDimitry Andric // The implementation of the ValueProfileCollector via ValueProfileCollectorImpl
108bcb0991SDimitry Andric //
118bcb0991SDimitry Andric //===----------------------------------------------------------------------===//
128bcb0991SDimitry Andric
13*81ad6265SDimitry Andric #include "ValueProfileCollector.h"
148bcb0991SDimitry Andric #include "ValueProfilePlugins.inc"
15*81ad6265SDimitry Andric #include "llvm/ProfileData/InstrProf.h"
168bcb0991SDimitry Andric
178bcb0991SDimitry Andric using namespace llvm;
188bcb0991SDimitry Andric
198bcb0991SDimitry Andric namespace {
208bcb0991SDimitry Andric
218bcb0991SDimitry Andric /// A plugin-based class that takes an arbitrary number of Plugin types.
228bcb0991SDimitry Andric /// Each plugin type must satisfy the following API:
238bcb0991SDimitry Andric /// 1) the constructor must take a `Function &f`. Typically, the plugin would
248bcb0991SDimitry Andric /// scan the function looking for candidates.
258bcb0991SDimitry Andric /// 2) contain a member function with the following signature and name:
268bcb0991SDimitry Andric /// void run(std::vector<CandidateInfo> &Candidates);
278bcb0991SDimitry Andric /// such that the plugin would append its result into the vector parameter.
288bcb0991SDimitry Andric ///
298bcb0991SDimitry Andric /// Plugins are defined in ValueProfilePlugins.inc
308bcb0991SDimitry Andric template <class... Ts> class PluginChain;
318bcb0991SDimitry Andric
328bcb0991SDimitry Andric /// The type PluginChainFinal is the final chain of plugins that will be used by
338bcb0991SDimitry Andric /// ValueProfileCollectorImpl.
348bcb0991SDimitry Andric using PluginChainFinal = PluginChain<VP_PLUGIN_LIST>;
358bcb0991SDimitry Andric
368bcb0991SDimitry Andric template <> class PluginChain<> {
378bcb0991SDimitry Andric public:
PluginChain(Function & F,TargetLibraryInfo & TLI)385ffd83dbSDimitry Andric PluginChain(Function &F, TargetLibraryInfo &TLI) {}
get(InstrProfValueKind K,std::vector<CandidateInfo> & Candidates)398bcb0991SDimitry Andric void get(InstrProfValueKind K, std::vector<CandidateInfo> &Candidates) {}
408bcb0991SDimitry Andric };
418bcb0991SDimitry Andric
428bcb0991SDimitry Andric template <class PluginT, class... Ts>
438bcb0991SDimitry Andric class PluginChain<PluginT, Ts...> : public PluginChain<Ts...> {
448bcb0991SDimitry Andric PluginT Plugin;
458bcb0991SDimitry Andric using Base = PluginChain<Ts...>;
468bcb0991SDimitry Andric
478bcb0991SDimitry Andric public:
PluginChain(Function & F,TargetLibraryInfo & TLI)485ffd83dbSDimitry Andric PluginChain(Function &F, TargetLibraryInfo &TLI)
495ffd83dbSDimitry Andric : PluginChain<Ts...>(F, TLI), Plugin(F, TLI) {}
508bcb0991SDimitry Andric
get(InstrProfValueKind K,std::vector<CandidateInfo> & Candidates)518bcb0991SDimitry Andric void get(InstrProfValueKind K, std::vector<CandidateInfo> &Candidates) {
528bcb0991SDimitry Andric if (K == PluginT::Kind)
538bcb0991SDimitry Andric Plugin.run(Candidates);
548bcb0991SDimitry Andric Base::get(K, Candidates);
558bcb0991SDimitry Andric }
568bcb0991SDimitry Andric };
578bcb0991SDimitry Andric
588bcb0991SDimitry Andric } // end anonymous namespace
598bcb0991SDimitry Andric
608bcb0991SDimitry Andric /// ValueProfileCollectorImpl inherits the API of PluginChainFinal.
618bcb0991SDimitry Andric class ValueProfileCollector::ValueProfileCollectorImpl : public PluginChainFinal {
628bcb0991SDimitry Andric public:
638bcb0991SDimitry Andric using PluginChainFinal::PluginChainFinal;
648bcb0991SDimitry Andric };
658bcb0991SDimitry Andric
ValueProfileCollector(Function & F,TargetLibraryInfo & TLI)665ffd83dbSDimitry Andric ValueProfileCollector::ValueProfileCollector(Function &F,
675ffd83dbSDimitry Andric TargetLibraryInfo &TLI)
685ffd83dbSDimitry Andric : PImpl(new ValueProfileCollectorImpl(F, TLI)) {}
698bcb0991SDimitry Andric
708bcb0991SDimitry Andric ValueProfileCollector::~ValueProfileCollector() = default;
718bcb0991SDimitry Andric
728bcb0991SDimitry Andric std::vector<CandidateInfo>
get(InstrProfValueKind Kind) const738bcb0991SDimitry Andric ValueProfileCollector::get(InstrProfValueKind Kind) const {
748bcb0991SDimitry Andric std::vector<CandidateInfo> Result;
758bcb0991SDimitry Andric PImpl->get(Kind, Result);
768bcb0991SDimitry Andric return Result;
778bcb0991SDimitry Andric }
78