ValueProfileCollector.h 3.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. //===- ValueProfileCollector.h - 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. // This file contains a utility class, ValueProfileCollector, that is used to
  10. // determine what kind of llvm::Value's are worth value-profiling, at which
  11. // point in the program, and which instruction holds the Value Profile metadata.
  12. // Currently, the only users of this utility is the PGOInstrumentation[Gen|Use]
  13. // passes.
  14. //===----------------------------------------------------------------------===//
  15. #ifndef LLVM_ANALYSIS_PROFILE_GEN_ANALYSIS_H
  16. #define LLVM_ANALYSIS_PROFILE_GEN_ANALYSIS_H
  17. #include "llvm/Analysis/TargetLibraryInfo.h"
  18. #include "llvm/ProfileData/InstrProf.h"
  19. #include <memory>
  20. #include <vector>
  21. namespace llvm {
  22. class Function;
  23. class Instruction;
  24. class Value;
  25. /// Utility analysis that determines what values are worth profiling.
  26. /// The actual logic is inside the ValueProfileCollectorImpl, whose job is to
  27. /// populate the Candidates vector.
  28. ///
  29. /// Value profiling an expression means to track the values that this expression
  30. /// takes at runtime and the frequency of each value.
  31. /// It is important to distinguish between two sets of value profiles for a
  32. /// particular expression:
  33. /// 1) The set of values at the point of evaluation.
  34. /// 2) The set of values at the point of use.
  35. /// In some cases, the two sets are identical, but it's not unusual for the two
  36. /// to differ.
  37. ///
  38. /// To elaborate more, consider this C code, and focus on the expression `nn`:
  39. /// void foo(int nn, bool b) {
  40. /// if (b) memcpy(x, y, nn);
  41. /// }
  42. /// The point of evaluation can be as early as the start of the function, and
  43. /// let's say the value profile for `nn` is:
  44. /// total=100; (value,freq) set = {(8,10), (32,50)}
  45. /// The point of use is right before we call memcpy, and since we execute the
  46. /// memcpy conditionally, the value profile of `nn` can be:
  47. /// total=15; (value,freq) set = {(8,10), (4,5)}
  48. ///
  49. /// For this reason, a plugin is responsible for computing the insertion point
  50. /// for each value to be profiled. The `CandidateInfo` structure encapsulates
  51. /// all the information needed for each value profile site.
  52. class ValueProfileCollector {
  53. public:
  54. struct CandidateInfo {
  55. Value *V; // The value to profile.
  56. Instruction *InsertPt; // Insert the VP lib call before this instr.
  57. Instruction *AnnotatedInst; // Where metadata is attached.
  58. };
  59. ValueProfileCollector(Function &Fn, TargetLibraryInfo &TLI);
  60. ValueProfileCollector(ValueProfileCollector &&) = delete;
  61. ValueProfileCollector &operator=(ValueProfileCollector &&) = delete;
  62. ValueProfileCollector(const ValueProfileCollector &) = delete;
  63. ValueProfileCollector &operator=(const ValueProfileCollector &) = delete;
  64. ~ValueProfileCollector();
  65. /// returns a list of value profiling candidates of the given kind
  66. std::vector<CandidateInfo> get(InstrProfValueKind Kind) const;
  67. private:
  68. class ValueProfileCollectorImpl;
  69. std::unique_ptr<ValueProfileCollectorImpl> PImpl;
  70. };
  71. } // namespace llvm
  72. #endif