ProfDataUtils.h 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. #pragma once
  2. #ifdef __GNUC__
  3. #pragma GCC diagnostic push
  4. #pragma GCC diagnostic ignored "-Wunused-parameter"
  5. #endif
  6. //===- llvm/IR/ProfDataUtils.h - Profiling Metadata Utilities ---*- C++ -*-===//
  7. //
  8. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  9. // See https://llvm.org/LICENSE.txt for license information.
  10. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  11. //
  12. //===----------------------------------------------------------------------===//
  13. //
  14. /// @file
  15. /// This file contains the declarations for profiling metadata utility
  16. /// functions.
  17. //
  18. //===----------------------------------------------------------------------===//
  19. #ifndef LLVM_IR_PROFDATAUTILS_H
  20. #define LLVM_IR_PROFDATAUTILS_H
  21. #include "llvm/ADT/SmallVector.h"
  22. #include "llvm/ADT/Twine.h"
  23. #include "llvm/IR/Metadata.h"
  24. namespace llvm {
  25. /// Checks if an Instruction has MD_prof Metadata
  26. bool hasProfMD(const Instruction &I);
  27. /// Checks if an MDNode contains Branch Weight Metadata
  28. bool isBranchWeightMD(const MDNode *ProfileData);
  29. /// Checks if an instructions has Branch Weight Metadata
  30. ///
  31. /// \param I The instruction to check
  32. /// \returns True if I has an MD_prof node containing Branch Weights. False
  33. /// otherwise.
  34. bool hasBranchWeightMD(const Instruction &I);
  35. /// Checks if an instructions has valid Branch Weight Metadata
  36. ///
  37. /// \param I The instruction to check
  38. /// \returns True if I has an MD_prof node containing valid Branch Weights,
  39. /// i.e., one weight for each successor. False otherwise.
  40. bool hasValidBranchWeightMD(const Instruction &I);
  41. /// Get the branch weights metadata node
  42. ///
  43. /// \param I The Instruction to get the weights from.
  44. /// \returns A pointer to I's branch weights metadata node, if it exists.
  45. /// Nullptr otherwise.
  46. MDNode *getBranchWeightMDNode(const Instruction &I);
  47. /// Get the valid branch weights metadata node
  48. ///
  49. /// \param I The Instruction to get the weights from.
  50. /// \returns A pointer to I's valid branch weights metadata node, if it exists.
  51. /// Nullptr otherwise.
  52. MDNode *getValidBranchWeightMDNode(const Instruction &I);
  53. /// Extract branch weights from MD_prof metadata
  54. ///
  55. /// \param ProfileData A pointer to an MDNode.
  56. /// \param [out] Weights An output vector to fill with branch weights
  57. /// \returns True if weights were extracted, False otherwise. When false Weights
  58. /// will be cleared.
  59. bool extractBranchWeights(const MDNode *ProfileData,
  60. SmallVectorImpl<uint32_t> &Weights);
  61. /// Extract branch weights attatched to an Instruction
  62. ///
  63. /// \param I The Instruction to extract weights from.
  64. /// \param [out] Weights An output vector to fill with branch weights
  65. /// \returns True if weights were extracted, False otherwise. When false Weights
  66. /// will be cleared.
  67. bool extractBranchWeights(const Instruction &I,
  68. SmallVectorImpl<uint32_t> &Weights);
  69. /// Extract branch weights from a conditional branch or select Instruction.
  70. ///
  71. /// \param I The instruction to extract branch weights from.
  72. /// \param [out] TrueVal will contain the branch weight for the True branch
  73. /// \param [out] FalseVal will contain the branch weight for the False branch
  74. /// \returns True on success with profile weights filled in. False if no
  75. /// metadata or invalid metadata was found.
  76. bool extractBranchWeights(const Instruction &I, uint64_t &TrueVal,
  77. uint64_t &FalseVal);
  78. /// Retrieve the total of all weights from MD_prof data.
  79. ///
  80. /// \param ProfileData The profile data to extract the total weight from
  81. /// \param [out] TotalWeights input variable to fill with total weights
  82. /// \returns True on success with profile total weights filled in. False if no
  83. /// metadata was found.
  84. bool extractProfTotalWeight(const MDNode *ProfileData, uint64_t &TotalWeights);
  85. /// Retrieve the total of all weights from an instruction.
  86. ///
  87. /// \param I The instruction to extract the total weight from
  88. /// \param [out] TotalWeights input variable to fill with total weights
  89. /// \returns True on success with profile total weights filled in. False if no
  90. /// metadata was found.
  91. bool extractProfTotalWeight(const Instruction &I, uint64_t &TotalWeights);
  92. } // namespace llvm
  93. #endif
  94. #ifdef __GNUC__
  95. #pragma GCC diagnostic pop
  96. #endif