HeatUtils.cpp 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. //===-- HeatUtils.cpp - Utility for printing heat colors --------*- C++ -*-===//
  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. // Utility for printing heat colors based on heuristics or profiling
  10. // information.
  11. //
  12. //===----------------------------------------------------------------------===//
  13. #include "llvm/Analysis/HeatUtils.h"
  14. #include "llvm/Analysis/BlockFrequencyInfo.h"
  15. #include "llvm/IR/Instructions.h"
  16. #include <cmath>
  17. namespace llvm {
  18. static const unsigned heatSize = 100;
  19. static const char heatPalette[heatSize][8] = {
  20. "#3d50c3", "#4055c8", "#4358cb", "#465ecf", "#4961d2", "#4c66d6", "#4f69d9",
  21. "#536edd", "#5572df", "#5977e3", "#5b7ae5", "#5f7fe8", "#6282ea", "#6687ed",
  22. "#6a8bef", "#6c8ff1", "#7093f3", "#7396f5", "#779af7", "#7a9df8", "#7ea1fa",
  23. "#81a4fb", "#85a8fc", "#88abfd", "#8caffe", "#8fb1fe", "#93b5fe", "#96b7ff",
  24. "#9abbff", "#9ebeff", "#a1c0ff", "#a5c3fe", "#a7c5fe", "#abc8fd", "#aec9fc",
  25. "#b2ccfb", "#b5cdfa", "#b9d0f9", "#bbd1f8", "#bfd3f6", "#c1d4f4", "#c5d6f2",
  26. "#c7d7f0", "#cbd8ee", "#cedaeb", "#d1dae9", "#d4dbe6", "#d6dce4", "#d9dce1",
  27. "#dbdcde", "#dedcdb", "#e0dbd8", "#e3d9d3", "#e5d8d1", "#e8d6cc", "#ead5c9",
  28. "#ecd3c5", "#eed0c0", "#efcebd", "#f1ccb8", "#f2cab5", "#f3c7b1", "#f4c5ad",
  29. "#f5c1a9", "#f6bfa6", "#f7bca1", "#f7b99e", "#f7b599", "#f7b396", "#f7af91",
  30. "#f7ac8e", "#f7a889", "#f6a385", "#f5a081", "#f59c7d", "#f4987a", "#f39475",
  31. "#f29072", "#f08b6e", "#ef886b", "#ed8366", "#ec7f63", "#e97a5f", "#e8765c",
  32. "#e57058", "#e36c55", "#e16751", "#de614d", "#dc5d4a", "#d85646", "#d65244",
  33. "#d24b40", "#d0473d", "#cc403a", "#ca3b37", "#c53334", "#c32e31", "#be242e",
  34. "#bb1b2c", "#b70d28"};
  35. uint64_t
  36. getNumOfCalls(Function &callerFunction, Function &calledFunction) {
  37. uint64_t counter = 0;
  38. for (User *U : calledFunction.users()) {
  39. if (auto CI = dyn_cast<CallInst>(U)) {
  40. if (CI->getCaller() == (&callerFunction)) {
  41. counter += 1;
  42. }
  43. }
  44. }
  45. return counter;
  46. }
  47. uint64_t getMaxFreq(const Function &F, const BlockFrequencyInfo *BFI) {
  48. uint64_t maxFreq = 0;
  49. for (const BasicBlock &BB : F) {
  50. uint64_t freqVal = BFI->getBlockFreq(&BB).getFrequency();
  51. if (freqVal >= maxFreq)
  52. maxFreq = freqVal;
  53. }
  54. return maxFreq;
  55. }
  56. std::string getHeatColor(uint64_t freq, uint64_t maxFreq) {
  57. if (freq > maxFreq)
  58. freq = maxFreq;
  59. double percent = (freq > 0) ? log2(double(freq)) / log2(maxFreq) : 0;
  60. return getHeatColor(percent);
  61. }
  62. std::string getHeatColor(double percent) {
  63. if (percent > 1.0)
  64. percent = 1.0;
  65. if (percent < 0.0)
  66. percent = 0.0;
  67. unsigned colorId = unsigned(round(percent * (heatSize - 1.0)));
  68. return heatPalette[colorId];
  69. }
  70. } // namespace llvm