1097a140dSpatrick //===-- HeatUtils.cpp - Utility for printing heat colors --------*- C++ -*-===//
2097a140dSpatrick //
3*d415bd75Srobert // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4*d415bd75Srobert // See https://llvm.org/LICENSE.txt for license information.
5*d415bd75Srobert // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6097a140dSpatrick //
7097a140dSpatrick //===----------------------------------------------------------------------===//
8097a140dSpatrick //
9097a140dSpatrick // Utility for printing heat colors based on heuristics or profiling
10097a140dSpatrick // information.
11097a140dSpatrick //
12097a140dSpatrick //===----------------------------------------------------------------------===//
13097a140dSpatrick
14097a140dSpatrick #include "llvm/Analysis/HeatUtils.h"
15097a140dSpatrick #include "llvm/Analysis/BlockFrequencyInfo.h"
16097a140dSpatrick #include "llvm/IR/Instructions.h"
17097a140dSpatrick
18*d415bd75Srobert #include <cmath>
19*d415bd75Srobert
20097a140dSpatrick namespace llvm {
21097a140dSpatrick
22097a140dSpatrick static const unsigned heatSize = 100;
23097a140dSpatrick static const char heatPalette[heatSize][8] = {
24097a140dSpatrick "#3d50c3", "#4055c8", "#4358cb", "#465ecf", "#4961d2", "#4c66d6", "#4f69d9",
25097a140dSpatrick "#536edd", "#5572df", "#5977e3", "#5b7ae5", "#5f7fe8", "#6282ea", "#6687ed",
26097a140dSpatrick "#6a8bef", "#6c8ff1", "#7093f3", "#7396f5", "#779af7", "#7a9df8", "#7ea1fa",
27097a140dSpatrick "#81a4fb", "#85a8fc", "#88abfd", "#8caffe", "#8fb1fe", "#93b5fe", "#96b7ff",
28097a140dSpatrick "#9abbff", "#9ebeff", "#a1c0ff", "#a5c3fe", "#a7c5fe", "#abc8fd", "#aec9fc",
29097a140dSpatrick "#b2ccfb", "#b5cdfa", "#b9d0f9", "#bbd1f8", "#bfd3f6", "#c1d4f4", "#c5d6f2",
30097a140dSpatrick "#c7d7f0", "#cbd8ee", "#cedaeb", "#d1dae9", "#d4dbe6", "#d6dce4", "#d9dce1",
31097a140dSpatrick "#dbdcde", "#dedcdb", "#e0dbd8", "#e3d9d3", "#e5d8d1", "#e8d6cc", "#ead5c9",
32097a140dSpatrick "#ecd3c5", "#eed0c0", "#efcebd", "#f1ccb8", "#f2cab5", "#f3c7b1", "#f4c5ad",
33097a140dSpatrick "#f5c1a9", "#f6bfa6", "#f7bca1", "#f7b99e", "#f7b599", "#f7b396", "#f7af91",
34097a140dSpatrick "#f7ac8e", "#f7a889", "#f6a385", "#f5a081", "#f59c7d", "#f4987a", "#f39475",
35097a140dSpatrick "#f29072", "#f08b6e", "#ef886b", "#ed8366", "#ec7f63", "#e97a5f", "#e8765c",
36097a140dSpatrick "#e57058", "#e36c55", "#e16751", "#de614d", "#dc5d4a", "#d85646", "#d65244",
37097a140dSpatrick "#d24b40", "#d0473d", "#cc403a", "#ca3b37", "#c53334", "#c32e31", "#be242e",
38097a140dSpatrick "#bb1b2c", "#b70d28"};
39097a140dSpatrick
40097a140dSpatrick uint64_t
getNumOfCalls(Function & callerFunction,Function & calledFunction)41097a140dSpatrick getNumOfCalls(Function &callerFunction, Function &calledFunction) {
42097a140dSpatrick uint64_t counter = 0;
43097a140dSpatrick for (User *U : calledFunction.users()) {
44097a140dSpatrick if (auto CI = dyn_cast<CallInst>(U)) {
45097a140dSpatrick if (CI->getCaller() == (&callerFunction)) {
46097a140dSpatrick counter += 1;
47097a140dSpatrick }
48097a140dSpatrick }
49097a140dSpatrick }
50097a140dSpatrick return counter;
51097a140dSpatrick }
52097a140dSpatrick
getMaxFreq(const Function & F,const BlockFrequencyInfo * BFI)53097a140dSpatrick uint64_t getMaxFreq(const Function &F, const BlockFrequencyInfo *BFI) {
54097a140dSpatrick uint64_t maxFreq = 0;
55097a140dSpatrick for (const BasicBlock &BB : F) {
56097a140dSpatrick uint64_t freqVal = BFI->getBlockFreq(&BB).getFrequency();
57097a140dSpatrick if (freqVal >= maxFreq)
58097a140dSpatrick maxFreq = freqVal;
59097a140dSpatrick }
60097a140dSpatrick return maxFreq;
61097a140dSpatrick }
62097a140dSpatrick
getHeatColor(uint64_t freq,uint64_t maxFreq)63097a140dSpatrick std::string getHeatColor(uint64_t freq, uint64_t maxFreq) {
64097a140dSpatrick if (freq > maxFreq)
65097a140dSpatrick freq = maxFreq;
66097a140dSpatrick double percent = (freq > 0) ? log2(double(freq)) / log2(maxFreq) : 0;
67097a140dSpatrick return getHeatColor(percent);
68097a140dSpatrick }
69097a140dSpatrick
getHeatColor(double percent)70097a140dSpatrick std::string getHeatColor(double percent) {
71097a140dSpatrick if (percent > 1.0)
72097a140dSpatrick percent = 1.0;
73097a140dSpatrick if (percent < 0.0)
74097a140dSpatrick percent = 0.0;
75097a140dSpatrick unsigned colorId = unsigned(round(percent * (heatSize - 1.0)));
76097a140dSpatrick return heatPalette[colorId];
77097a140dSpatrick }
78097a140dSpatrick
79097a140dSpatrick } // namespace llvm
80