1 //===- ValueProfileCollector.cpp - determine what to value profile --------===//
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
7 //===----------------------------------------------------------------------===//
9 // The implementation of the ValueProfileCollector via ValueProfileCollectorImpl
11 //===----------------------------------------------------------------------===//
13 #include "ValueProfilePlugins.inc"
14 #include "llvm/IR/Function.h"
15 #include "llvm/IR/InstIterator.h"
16 #include "llvm/IR/IntrinsicInst.h"
17 #include "llvm/InitializePasses.h"
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.
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
<> {
41 PluginChain(Function
&F
, TargetLibraryInfo
&TLI
) {}
42 void get(InstrProfValueKind K
, std::vector
<CandidateInfo
> &Candidates
) {}
45 template <class PluginT
, class... Ts
>
46 class PluginChain
<PluginT
, Ts
...> : public PluginChain
<Ts
...> {
48 using Base
= PluginChain
<Ts
...>;
51 PluginChain(Function
&F
, TargetLibraryInfo
&TLI
)
52 : PluginChain
<Ts
...>(F
, TLI
), Plugin(F
, TLI
) {}
54 void get(InstrProfValueKind K
, std::vector
<CandidateInfo
> &Candidates
) {
55 if (K
== PluginT::Kind
)
56 Plugin
.run(Candidates
);
57 Base::get(K
, Candidates
);
61 } // end anonymous namespace
63 /// ValueProfileCollectorImpl inherits the API of PluginChainFinal.
64 class ValueProfileCollector::ValueProfileCollectorImpl
: public PluginChainFinal
{
66 using PluginChainFinal::PluginChainFinal
;
69 ValueProfileCollector::ValueProfileCollector(Function
&F
,
70 TargetLibraryInfo
&TLI
)
71 : PImpl(new ValueProfileCollectorImpl(F
, TLI
)) {}
73 ValueProfileCollector::~ValueProfileCollector() = default;
75 std::vector
<CandidateInfo
>
76 ValueProfileCollector::get(InstrProfValueKind Kind
) const {
77 std::vector
<CandidateInfo
> Result
;
78 PImpl
->get(Kind
, Result
);