1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210
|
//===- MachineSizeOpts.cpp - code size optimization related code ----------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
//
// This file contains some shared machine IR code size optimization related
// code.
//
//===----------------------------------------------------------------------===//
#include "llvm/CodeGen/MachineSizeOpts.h"
#include "llvm/CodeGen/MBFIWrapper.h"
#include "llvm/Analysis/ProfileSummaryInfo.h"
#include "llvm/CodeGen/MachineBlockFrequencyInfo.h"
using namespace llvm;
extern cl::opt<bool> EnablePGSO;
extern cl::opt<bool> PGSOLargeWorkingSetSizeOnly;
extern cl::opt<bool> ForcePGSO;
extern cl::opt<int> PgsoCutoffInstrProf;
extern cl::opt<int> PgsoCutoffSampleProf;
namespace {
namespace machine_size_opts_detail {
/// Like ProfileSummaryInfo::isColdBlock but for MachineBasicBlock.
bool isColdBlock(const MachineBasicBlock *MBB,
ProfileSummaryInfo *PSI,
const MachineBlockFrequencyInfo *MBFI) {
auto Count = MBFI->getBlockProfileCount(MBB);
return Count && PSI->isColdCount(*Count);
}
bool isColdBlock(BlockFrequency BlockFreq,
ProfileSummaryInfo *PSI,
const MachineBlockFrequencyInfo *MBFI) {
auto Count = MBFI->getProfileCountFromFreq(BlockFreq.getFrequency());
return Count && PSI->isColdCount(*Count);
}
/// Like ProfileSummaryInfo::isHotBlockNthPercentile but for MachineBasicBlock.
static bool isHotBlockNthPercentile(int PercentileCutoff,
const MachineBasicBlock *MBB,
ProfileSummaryInfo *PSI,
const MachineBlockFrequencyInfo *MBFI) {
auto Count = MBFI->getBlockProfileCount(MBB);
return Count && PSI->isHotCountNthPercentile(PercentileCutoff, *Count);
}
static bool isHotBlockNthPercentile(int PercentileCutoff,
BlockFrequency BlockFreq,
ProfileSummaryInfo *PSI,
const MachineBlockFrequencyInfo *MBFI) {
auto Count = MBFI->getProfileCountFromFreq(BlockFreq.getFrequency());
return Count && PSI->isHotCountNthPercentile(PercentileCutoff, *Count);
}
static bool isColdBlockNthPercentile(int PercentileCutoff,
const MachineBasicBlock *MBB,
ProfileSummaryInfo *PSI,
const MachineBlockFrequencyInfo *MBFI) {
auto Count = MBFI->getBlockProfileCount(MBB);
return Count && PSI->isColdCountNthPercentile(PercentileCutoff, *Count);
}
static bool isColdBlockNthPercentile(int PercentileCutoff,
BlockFrequency BlockFreq,
ProfileSummaryInfo *PSI,
const MachineBlockFrequencyInfo *MBFI) {
auto Count = MBFI->getProfileCountFromFreq(BlockFreq.getFrequency());
return Count && PSI->isColdCountNthPercentile(PercentileCutoff, *Count);
}
/// Like ProfileSummaryInfo::isFunctionColdInCallGraph but for
/// MachineFunction.
bool isFunctionColdInCallGraph(
const MachineFunction *MF,
ProfileSummaryInfo *PSI,
const MachineBlockFrequencyInfo &MBFI) {
if (auto FunctionCount = MF->getFunction().getEntryCount())
if (!PSI->isColdCount(FunctionCount.getCount()))
return false;
for (const auto &MBB : *MF)
if (!isColdBlock(&MBB, PSI, &MBFI))
return false;
return true;
}
/// Like ProfileSummaryInfo::isFunctionHotInCallGraphNthPercentile but for
/// MachineFunction.
bool isFunctionHotInCallGraphNthPercentile(
int PercentileCutoff,
const MachineFunction *MF,
ProfileSummaryInfo *PSI,
const MachineBlockFrequencyInfo &MBFI) {
if (auto FunctionCount = MF->getFunction().getEntryCount())
if (PSI->isHotCountNthPercentile(PercentileCutoff,
FunctionCount.getCount()))
return true;
for (const auto &MBB : *MF)
if (isHotBlockNthPercentile(PercentileCutoff, &MBB, PSI, &MBFI))
return true;
return false;
}
bool isFunctionColdInCallGraphNthPercentile(
int PercentileCutoff, const MachineFunction *MF, ProfileSummaryInfo *PSI,
const MachineBlockFrequencyInfo &MBFI) {
if (auto FunctionCount = MF->getFunction().getEntryCount())
if (!PSI->isColdCountNthPercentile(PercentileCutoff,
FunctionCount.getCount()))
return false;
for (const auto &MBB : *MF)
if (!isColdBlockNthPercentile(PercentileCutoff, &MBB, PSI, &MBFI))
return false;
return true;
}
} // namespace machine_size_opts_detail
struct MachineBasicBlockBFIAdapter {
static bool isFunctionColdInCallGraph(const MachineFunction *MF,
ProfileSummaryInfo *PSI,
const MachineBlockFrequencyInfo &MBFI) {
return machine_size_opts_detail::isFunctionColdInCallGraph(MF, PSI, MBFI);
}
static bool isFunctionHotInCallGraphNthPercentile(
int CutOff,
const MachineFunction *MF,
ProfileSummaryInfo *PSI,
const MachineBlockFrequencyInfo &MBFI) {
return machine_size_opts_detail::isFunctionHotInCallGraphNthPercentile(
CutOff, MF, PSI, MBFI);
}
static bool isFunctionColdInCallGraphNthPercentile(
int CutOff, const MachineFunction *MF, ProfileSummaryInfo *PSI,
const MachineBlockFrequencyInfo &MBFI) {
return machine_size_opts_detail::isFunctionColdInCallGraphNthPercentile(
CutOff, MF, PSI, MBFI);
}
static bool isColdBlock(const MachineBasicBlock *MBB,
ProfileSummaryInfo *PSI,
const MachineBlockFrequencyInfo *MBFI) {
return machine_size_opts_detail::isColdBlock(MBB, PSI, MBFI);
}
static bool isColdBlock(BlockFrequency BlockFreq,
ProfileSummaryInfo *PSI,
const MachineBlockFrequencyInfo *MBFI) {
return machine_size_opts_detail::isColdBlock(BlockFreq, PSI, MBFI);
}
static bool isHotBlockNthPercentile(int CutOff,
const MachineBasicBlock *MBB,
ProfileSummaryInfo *PSI,
const MachineBlockFrequencyInfo *MBFI) {
return machine_size_opts_detail::isHotBlockNthPercentile(
CutOff, MBB, PSI, MBFI);
}
static bool isHotBlockNthPercentile(int CutOff,
BlockFrequency BlockFreq,
ProfileSummaryInfo *PSI,
const MachineBlockFrequencyInfo *MBFI) {
return machine_size_opts_detail::isHotBlockNthPercentile(
CutOff, BlockFreq, PSI, MBFI);
}
static bool isColdBlockNthPercentile(int CutOff, const MachineBasicBlock *MBB,
ProfileSummaryInfo *PSI,
const MachineBlockFrequencyInfo *MBFI) {
return machine_size_opts_detail::isColdBlockNthPercentile(CutOff, MBB, PSI,
MBFI);
}
static bool isColdBlockNthPercentile(int CutOff, BlockFrequency BlockFreq,
ProfileSummaryInfo *PSI,
const MachineBlockFrequencyInfo *MBFI) {
return machine_size_opts_detail::isColdBlockNthPercentile(CutOff, BlockFreq,
PSI, MBFI);
}
};
} // end anonymous namespace
bool llvm::shouldOptimizeForSize(const MachineFunction *MF,
ProfileSummaryInfo *PSI,
const MachineBlockFrequencyInfo *MBFI,
PGSOQueryType QueryType) {
return shouldFuncOptimizeForSizeImpl<MachineBasicBlockBFIAdapter>(
MF, PSI, MBFI, QueryType);
}
bool llvm::shouldOptimizeForSize(const MachineBasicBlock *MBB,
ProfileSummaryInfo *PSI,
const MachineBlockFrequencyInfo *MBFI,
PGSOQueryType QueryType) {
assert(MBB);
return shouldOptimizeForSizeImpl<MachineBasicBlockBFIAdapter>(
MBB, PSI, MBFI, QueryType);
}
bool llvm::shouldOptimizeForSize(const MachineBasicBlock *MBB,
ProfileSummaryInfo *PSI,
MBFIWrapper *MBFIW,
PGSOQueryType QueryType) {
assert(MBB);
if (!PSI || !MBFIW)
return false;
BlockFrequency BlockFreq = MBFIW->getBlockFreq(MBB);
return shouldOptimizeForSizeImpl<MachineBasicBlockBFIAdapter>(
BlockFreq, PSI, &MBFIW->getMBFI(), QueryType);
}
|