xref: /freebsd/contrib/llvm-project/llvm/lib/Transforms/Instrumentation/ValueProfileCollector.cpp (revision 62cfcf62f627e5093fb37026a6d8c98e4d2ef04c)
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
10 //
11 //===----------------------------------------------------------------------===//
12 
13 #include "ValueProfilePlugins.inc"
14 #include "llvm/IR/InstIterator.h"
15 #include "llvm/IR/IntrinsicInst.h"
16 #include "llvm/InitializePasses.h"
17 
18 #include <cassert>
19 
20 using namespace llvm;
21 
22 namespace {
23 
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;
34 
35 /// The type PluginChainFinal is the final chain of plugins that will be used by
36 /// ValueProfileCollectorImpl.
37 using PluginChainFinal = PluginChain<VP_PLUGIN_LIST>;
38 
39 template <> class PluginChain<> {
40 public:
41   PluginChain(Function &F) {}
42   void get(InstrProfValueKind K, std::vector<CandidateInfo> &Candidates) {}
43 };
44 
45 template <class PluginT, class... Ts>
46 class PluginChain<PluginT, Ts...> : public PluginChain<Ts...> {
47   PluginT Plugin;
48   using Base = PluginChain<Ts...>;
49 
50 public:
51   PluginChain(Function &F) : PluginChain<Ts...>(F), Plugin(F) {}
52 
53   void get(InstrProfValueKind K, std::vector<CandidateInfo> &Candidates) {
54     if (K == PluginT::Kind)
55       Plugin.run(Candidates);
56     Base::get(K, Candidates);
57   }
58 };
59 
60 } // end anonymous namespace
61 
62 /// ValueProfileCollectorImpl inherits the API of PluginChainFinal.
63 class ValueProfileCollector::ValueProfileCollectorImpl : public PluginChainFinal {
64 public:
65   using PluginChainFinal::PluginChainFinal;
66 };
67 
68 ValueProfileCollector::ValueProfileCollector(Function &F)
69     : PImpl(new ValueProfileCollectorImpl(F)) {}
70 
71 ValueProfileCollector::~ValueProfileCollector() = default;
72 
73 std::vector<CandidateInfo>
74 ValueProfileCollector::get(InstrProfValueKind Kind) const {
75   std::vector<CandidateInfo> Result;
76   PImpl->get(Kind, Result);
77   return Result;
78 }
79