Recommit r373598 "[yaml2obj/obj2yaml] - Add support for SHT_LLVM_ADDRSIG sections."
[llvm-complete.git] / lib / Transforms / Instrumentation / ValueProfileCollector.cpp
blob604726d4f40fccaca784f83618c144dd1f6cb8e0
1 //===- ValueProfileCollector.cpp - determine what to value profile --------===//
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 // The implementation of the ValueProfileCollector via ValueProfileCollectorImpl
11 //===----------------------------------------------------------------------===//
13 #include "ValueProfilePlugins.inc"
14 #include "llvm/IR/InstIterator.h"
15 #include "llvm/IR/IntrinsicInst.h"
16 #include "llvm/InitializePasses.h"
18 #include <cassert>
20 using namespace llvm;
22 namespace {
24 /// A plugin-based class that takes an arbitrary number of Plugin types.
25 /// Each plugin type must satisfy the following API:
26 /// 1) the constructor must take a `Function &f`. Typically, the plugin would
27 /// scan the function looking for candidates.
28 /// 2) contain a member function with the following signature and name:
29 /// void run(std::vector<CandidateInfo> &Candidates);
30 /// such that the plugin would append its result into the vector parameter.
31 ///
32 /// Plugins are defined in ValueProfilePlugins.inc
33 template <class... Ts> class PluginChain;
35 /// The type PluginChainFinal is the final chain of plugins that will be used by
36 /// ValueProfileCollectorImpl.
37 using PluginChainFinal = PluginChain<VP_PLUGIN_LIST>;
39 template <> class PluginChain<> {
40 public:
41 PluginChain(Function &F) {}
42 void get(InstrProfValueKind K, std::vector<CandidateInfo> &Candidates) {}
45 template <class PluginT, class... Ts>
46 class PluginChain<PluginT, Ts...> : public PluginChain<Ts...> {
47 PluginT Plugin;
48 using Base = PluginChain<Ts...>;
50 public:
51 PluginChain(Function &F) : PluginChain<Ts...>(F), Plugin(F) {}
53 void get(InstrProfValueKind K, std::vector<CandidateInfo> &Candidates) {
54 if (K == PluginT::Kind)
55 Plugin.run(Candidates);
56 Base::get(K, Candidates);
60 } // end anonymous namespace
62 /// ValueProfileCollectorImpl inherits the API of PluginChainFinal.
63 class ValueProfileCollector::ValueProfileCollectorImpl : public PluginChainFinal {
64 public:
65 using PluginChainFinal::PluginChainFinal;
68 ValueProfileCollector::ValueProfileCollector(Function &F)
69 : PImpl(new ValueProfileCollectorImpl(F)) {}
71 ValueProfileCollector::~ValueProfileCollector() = default;
73 std::vector<CandidateInfo>
74 ValueProfileCollector::get(InstrProfValueKind Kind) const {
75 std::vector<CandidateInfo> Result;
76 PImpl->get(Kind, Result);
77 return Result;