2019-10-29 03:35:34 +08:00
|
|
|
//===- 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"
|
2020-07-24 19:40:34 +08:00
|
|
|
#include "llvm/CodeGen/MBFIWrapper.h"
|
2020-07-24 20:02:33 +08:00
|
|
|
#include "llvm/Analysis/ProfileSummaryInfo.h"
|
2019-10-29 03:35:34 +08:00
|
|
|
#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;
|
|
|
|
|
2020-02-18 00:55:03 +08:00
|
|
|
namespace {
|
2019-10-29 03:35:34 +08:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2020-01-30 01:36:31 +08:00
|
|
|
bool isColdBlock(BlockFrequency BlockFreq,
|
|
|
|
ProfileSummaryInfo *PSI,
|
|
|
|
const MachineBlockFrequencyInfo *MBFI) {
|
|
|
|
auto Count = MBFI->getProfileCountFromFreq(BlockFreq.getFrequency());
|
|
|
|
return Count && PSI->isColdCount(*Count);
|
|
|
|
}
|
|
|
|
|
2019-10-29 03:35:34 +08:00
|
|
|
/// 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);
|
|
|
|
}
|
|
|
|
|
2020-01-30 01:36:31 +08:00
|
|
|
static bool isHotBlockNthPercentile(int PercentileCutoff,
|
|
|
|
BlockFrequency BlockFreq,
|
|
|
|
ProfileSummaryInfo *PSI,
|
|
|
|
const MachineBlockFrequencyInfo *MBFI) {
|
|
|
|
auto Count = MBFI->getProfileCountFromFreq(BlockFreq.getFrequency());
|
|
|
|
return Count && PSI->isHotCountNthPercentile(PercentileCutoff, *Count);
|
|
|
|
}
|
|
|
|
|
2020-03-04 01:53:07 +08:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2019-10-29 03:35:34 +08:00
|
|
|
/// 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;
|
|
|
|
}
|
2020-03-04 01:53:07 +08:00
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
2019-10-29 03:35:34 +08:00
|
|
|
} // 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);
|
|
|
|
}
|
2020-03-04 01:53:07 +08:00
|
|
|
static bool isFunctionColdInCallGraphNthPercentile(
|
|
|
|
int CutOff, const MachineFunction *MF, ProfileSummaryInfo *PSI,
|
|
|
|
const MachineBlockFrequencyInfo &MBFI) {
|
|
|
|
return machine_size_opts_detail::isFunctionColdInCallGraphNthPercentile(
|
|
|
|
CutOff, MF, PSI, MBFI);
|
|
|
|
}
|
2019-10-29 03:35:34 +08:00
|
|
|
static bool isColdBlock(const MachineBasicBlock *MBB,
|
|
|
|
ProfileSummaryInfo *PSI,
|
|
|
|
const MachineBlockFrequencyInfo *MBFI) {
|
|
|
|
return machine_size_opts_detail::isColdBlock(MBB, PSI, MBFI);
|
|
|
|
}
|
2020-01-30 01:36:31 +08:00
|
|
|
static bool isColdBlock(BlockFrequency BlockFreq,
|
|
|
|
ProfileSummaryInfo *PSI,
|
|
|
|
const MachineBlockFrequencyInfo *MBFI) {
|
|
|
|
return machine_size_opts_detail::isColdBlock(BlockFreq, PSI, MBFI);
|
|
|
|
}
|
2019-10-29 03:35:34 +08:00
|
|
|
static bool isHotBlockNthPercentile(int CutOff,
|
|
|
|
const MachineBasicBlock *MBB,
|
|
|
|
ProfileSummaryInfo *PSI,
|
|
|
|
const MachineBlockFrequencyInfo *MBFI) {
|
|
|
|
return machine_size_opts_detail::isHotBlockNthPercentile(
|
|
|
|
CutOff, MBB, PSI, MBFI);
|
|
|
|
}
|
2020-01-30 01:36:31 +08:00
|
|
|
static bool isHotBlockNthPercentile(int CutOff,
|
|
|
|
BlockFrequency BlockFreq,
|
|
|
|
ProfileSummaryInfo *PSI,
|
|
|
|
const MachineBlockFrequencyInfo *MBFI) {
|
|
|
|
return machine_size_opts_detail::isHotBlockNthPercentile(
|
|
|
|
CutOff, BlockFreq, PSI, MBFI);
|
|
|
|
}
|
2020-03-04 01:53:07 +08:00
|
|
|
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);
|
|
|
|
}
|
2019-10-29 03:35:34 +08:00
|
|
|
};
|
|
|
|
} // end anonymous namespace
|
|
|
|
|
|
|
|
bool llvm::shouldOptimizeForSize(const MachineFunction *MF,
|
|
|
|
ProfileSummaryInfo *PSI,
|
2019-11-21 05:08:07 +08:00
|
|
|
const MachineBlockFrequencyInfo *MBFI,
|
|
|
|
PGSOQueryType QueryType) {
|
2019-10-29 03:35:34 +08:00
|
|
|
return shouldFuncOptimizeForSizeImpl<MachineBasicBlockBFIAdapter>(
|
2019-11-21 05:08:07 +08:00
|
|
|
MF, PSI, MBFI, QueryType);
|
2019-10-29 03:35:34 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
bool llvm::shouldOptimizeForSize(const MachineBasicBlock *MBB,
|
|
|
|
ProfileSummaryInfo *PSI,
|
2019-11-21 05:08:07 +08:00
|
|
|
const MachineBlockFrequencyInfo *MBFI,
|
|
|
|
PGSOQueryType QueryType) {
|
2020-01-30 01:36:31 +08:00
|
|
|
assert(MBB);
|
2019-10-29 03:35:34 +08:00
|
|
|
return shouldOptimizeForSizeImpl<MachineBasicBlockBFIAdapter>(
|
2019-11-21 05:08:07 +08:00
|
|
|
MBB, PSI, MBFI, QueryType);
|
2019-10-29 03:35:34 +08:00
|
|
|
}
|
2020-01-30 01:36:31 +08:00
|
|
|
|
|
|
|
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);
|
|
|
|
}
|