ValueProfileCollector.cpp 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  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. #include "ValueProfilePlugins.inc"
  13. #include "llvm/IR/Function.h"
  14. #include "llvm/IR/InstIterator.h"
  15. #include "llvm/IR/IntrinsicInst.h"
  16. #include "llvm/InitializePasses.h"
  17. #include <cassert>
  18. using namespace llvm;
  19. namespace {
  20. /// A plugin-based class that takes an arbitrary number of Plugin types.
  21. /// Each plugin type must satisfy the following API:
  22. /// 1) the constructor must take a `Function &f`. Typically, the plugin would
  23. /// scan the function looking for candidates.
  24. /// 2) contain a member function with the following signature and name:
  25. /// void run(std::vector<CandidateInfo> &Candidates);
  26. /// such that the plugin would append its result into the vector parameter.
  27. ///
  28. /// Plugins are defined in ValueProfilePlugins.inc
  29. template <class... Ts> class PluginChain;
  30. /// The type PluginChainFinal is the final chain of plugins that will be used by
  31. /// ValueProfileCollectorImpl.
  32. using PluginChainFinal = PluginChain<VP_PLUGIN_LIST>;
  33. template <> class PluginChain<> {
  34. public:
  35. PluginChain(Function &F, TargetLibraryInfo &TLI) {}
  36. void get(InstrProfValueKind K, std::vector<CandidateInfo> &Candidates) {}
  37. };
  38. template <class PluginT, class... Ts>
  39. class PluginChain<PluginT, Ts...> : public PluginChain<Ts...> {
  40. PluginT Plugin;
  41. using Base = PluginChain<Ts...>;
  42. public:
  43. PluginChain(Function &F, TargetLibraryInfo &TLI)
  44. : PluginChain<Ts...>(F, TLI), Plugin(F, TLI) {}
  45. void get(InstrProfValueKind K, std::vector<CandidateInfo> &Candidates) {
  46. if (K == PluginT::Kind)
  47. Plugin.run(Candidates);
  48. Base::get(K, Candidates);
  49. }
  50. };
  51. } // end anonymous namespace
  52. /// ValueProfileCollectorImpl inherits the API of PluginChainFinal.
  53. class ValueProfileCollector::ValueProfileCollectorImpl : public PluginChainFinal {
  54. public:
  55. using PluginChainFinal::PluginChainFinal;
  56. };
  57. ValueProfileCollector::ValueProfileCollector(Function &F,
  58. TargetLibraryInfo &TLI)
  59. : PImpl(new ValueProfileCollectorImpl(F, TLI)) {}
  60. ValueProfileCollector::~ValueProfileCollector() = default;
  61. std::vector<CandidateInfo>
  62. ValueProfileCollector::get(InstrProfValueKind Kind) const {
  63. std::vector<CandidateInfo> Result;
  64. PImpl->get(Kind, Result);
  65. return Result;
  66. }