18b67853aSKirill Naumov //===-- HeatUtils.cpp - Utility for printing heat colors --------*- C++ -*-===//
28b67853aSKirill Naumov //
3c874dd53SChristopher Di Bella // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4c874dd53SChristopher Di Bella // See https://llvm.org/LICENSE.txt for license information.
5c874dd53SChristopher Di Bella // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
68b67853aSKirill Naumov //
78b67853aSKirill Naumov //===----------------------------------------------------------------------===//
88b67853aSKirill Naumov //
98b67853aSKirill Naumov // Utility for printing heat colors based on heuristics or profiling
108b67853aSKirill Naumov // information.
118b67853aSKirill Naumov //
128b67853aSKirill Naumov //===----------------------------------------------------------------------===//
138b67853aSKirill Naumov
148b67853aSKirill Naumov #include "llvm/Analysis/HeatUtils.h"
159b95186cSSimon Pilgrim #include "llvm/Analysis/BlockFrequencyInfo.h"
168b67853aSKirill Naumov #include "llvm/IR/Instructions.h"
178b67853aSKirill Naumov
18*16544cbeSserge-sans-paille #include <cmath>
19*16544cbeSserge-sans-paille
208b67853aSKirill Naumov namespace llvm {
218b67853aSKirill Naumov
228b67853aSKirill Naumov static const unsigned heatSize = 100;
23547b6da7SBenjamin Kramer static const char heatPalette[heatSize][8] = {
248b67853aSKirill Naumov "#3d50c3", "#4055c8", "#4358cb", "#465ecf", "#4961d2", "#4c66d6", "#4f69d9",
258b67853aSKirill Naumov "#536edd", "#5572df", "#5977e3", "#5b7ae5", "#5f7fe8", "#6282ea", "#6687ed",
268b67853aSKirill Naumov "#6a8bef", "#6c8ff1", "#7093f3", "#7396f5", "#779af7", "#7a9df8", "#7ea1fa",
278b67853aSKirill Naumov "#81a4fb", "#85a8fc", "#88abfd", "#8caffe", "#8fb1fe", "#93b5fe", "#96b7ff",
288b67853aSKirill Naumov "#9abbff", "#9ebeff", "#a1c0ff", "#a5c3fe", "#a7c5fe", "#abc8fd", "#aec9fc",
298b67853aSKirill Naumov "#b2ccfb", "#b5cdfa", "#b9d0f9", "#bbd1f8", "#bfd3f6", "#c1d4f4", "#c5d6f2",
308b67853aSKirill Naumov "#c7d7f0", "#cbd8ee", "#cedaeb", "#d1dae9", "#d4dbe6", "#d6dce4", "#d9dce1",
318b67853aSKirill Naumov "#dbdcde", "#dedcdb", "#e0dbd8", "#e3d9d3", "#e5d8d1", "#e8d6cc", "#ead5c9",
328b67853aSKirill Naumov "#ecd3c5", "#eed0c0", "#efcebd", "#f1ccb8", "#f2cab5", "#f3c7b1", "#f4c5ad",
338b67853aSKirill Naumov "#f5c1a9", "#f6bfa6", "#f7bca1", "#f7b99e", "#f7b599", "#f7b396", "#f7af91",
348b67853aSKirill Naumov "#f7ac8e", "#f7a889", "#f6a385", "#f5a081", "#f59c7d", "#f4987a", "#f39475",
358b67853aSKirill Naumov "#f29072", "#f08b6e", "#ef886b", "#ed8366", "#ec7f63", "#e97a5f", "#e8765c",
368b67853aSKirill Naumov "#e57058", "#e36c55", "#e16751", "#de614d", "#dc5d4a", "#d85646", "#d65244",
378b67853aSKirill Naumov "#d24b40", "#d0473d", "#cc403a", "#ca3b37", "#c53334", "#c32e31", "#be242e",
388b67853aSKirill Naumov "#bb1b2c", "#b70d28"};
398b67853aSKirill Naumov
40369d00dfSKirill Naumov uint64_t
getNumOfCalls(Function & callerFunction,Function & calledFunction)41369d00dfSKirill Naumov getNumOfCalls(Function &callerFunction, Function &calledFunction) {
42369d00dfSKirill Naumov uint64_t counter = 0;
43369d00dfSKirill Naumov for (User *U : calledFunction.users()) {
44369d00dfSKirill Naumov if (auto CI = dyn_cast<CallInst>(U)) {
45369d00dfSKirill Naumov if (CI->getCaller() == (&callerFunction)) {
46369d00dfSKirill Naumov counter += 1;
47369d00dfSKirill Naumov }
48369d00dfSKirill Naumov }
49369d00dfSKirill Naumov }
50369d00dfSKirill Naumov return counter;
51369d00dfSKirill Naumov }
52369d00dfSKirill Naumov
getMaxFreq(const Function & F,const BlockFrequencyInfo * BFI)538b67853aSKirill Naumov uint64_t getMaxFreq(const Function &F, const BlockFrequencyInfo *BFI) {
548b67853aSKirill Naumov uint64_t maxFreq = 0;
558b67853aSKirill Naumov for (const BasicBlock &BB : F) {
568b67853aSKirill Naumov uint64_t freqVal = BFI->getBlockFreq(&BB).getFrequency();
578b67853aSKirill Naumov if (freqVal >= maxFreq)
588b67853aSKirill Naumov maxFreq = freqVal;
598b67853aSKirill Naumov }
608b67853aSKirill Naumov return maxFreq;
618b67853aSKirill Naumov }
628b67853aSKirill Naumov
getHeatColor(uint64_t freq,uint64_t maxFreq)638b67853aSKirill Naumov std::string getHeatColor(uint64_t freq, uint64_t maxFreq) {
648b67853aSKirill Naumov if (freq > maxFreq)
658b67853aSKirill Naumov freq = maxFreq;
663847737fSKirill Bobyrev double percent = (freq > 0) ? log2(double(freq)) / log2(maxFreq) : 0;
678b67853aSKirill Naumov return getHeatColor(percent);
688b67853aSKirill Naumov }
698b67853aSKirill Naumov
getHeatColor(double percent)708b67853aSKirill Naumov std::string getHeatColor(double percent) {
718b67853aSKirill Naumov if (percent > 1.0)
728b67853aSKirill Naumov percent = 1.0;
738b67853aSKirill Naumov if (percent < 0.0)
748b67853aSKirill Naumov percent = 0.0;
758b67853aSKirill Naumov unsigned colorId = unsigned(round(percent * (heatSize - 1.0)));
768b67853aSKirill Naumov return heatPalette[colorId];
778b67853aSKirill Naumov }
788b67853aSKirill Naumov
798b67853aSKirill Naumov } // namespace llvm
80