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