LLVM  10.0.0
MachineSizeOpts.cpp
Go to the documentation of this file.
1 //===- MachineSizeOpts.cpp - code size optimization related code ----------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // This file contains some shared machine IR code size optimization related
10 // code.
11 //
12 //===----------------------------------------------------------------------===//
13 
17 
18 using namespace llvm;
19 
25 
27 
28 /// Like ProfileSummaryInfo::isColdBlock but for MachineBasicBlock.
30  ProfileSummaryInfo *PSI,
31  const MachineBlockFrequencyInfo *MBFI) {
32  auto Count = MBFI->getBlockProfileCount(MBB);
33  return Count && PSI->isColdCount(*Count);
34 }
35 
36 /// Like ProfileSummaryInfo::isHotBlockNthPercentile but for MachineBasicBlock.
37 static bool isHotBlockNthPercentile(int PercentileCutoff,
38  const MachineBasicBlock *MBB,
39  ProfileSummaryInfo *PSI,
40  const MachineBlockFrequencyInfo *MBFI) {
41  auto Count = MBFI->getBlockProfileCount(MBB);
42  return Count && PSI->isHotCountNthPercentile(PercentileCutoff, *Count);
43 }
44 
45 /// Like ProfileSummaryInfo::isFunctionColdInCallGraph but for
46 /// MachineFunction.
48  const MachineFunction *MF,
49  ProfileSummaryInfo *PSI,
50  const MachineBlockFrequencyInfo &MBFI) {
51  if (auto FunctionCount = MF->getFunction().getEntryCount())
52  if (!PSI->isColdCount(FunctionCount.getCount()))
53  return false;
54  for (const auto &MBB : *MF)
55  if (!isColdBlock(&MBB, PSI, &MBFI))
56  return false;
57  return true;
58 }
59 
60 /// Like ProfileSummaryInfo::isFunctionHotInCallGraphNthPercentile but for
61 /// MachineFunction.
63  int PercentileCutoff,
64  const MachineFunction *MF,
65  ProfileSummaryInfo *PSI,
66  const MachineBlockFrequencyInfo &MBFI) {
67  if (auto FunctionCount = MF->getFunction().getEntryCount())
68  if (PSI->isHotCountNthPercentile(PercentileCutoff,
69  FunctionCount.getCount()))
70  return true;
71  for (const auto &MBB : *MF)
72  if (isHotBlockNthPercentile(PercentileCutoff, &MBB, PSI, &MBFI))
73  return true;
74  return false;
75 }
76 } // namespace machine_size_opts_detail
77 
78 namespace {
79 struct MachineBasicBlockBFIAdapter {
80  static bool isFunctionColdInCallGraph(const MachineFunction *MF,
81  ProfileSummaryInfo *PSI,
82  const MachineBlockFrequencyInfo &MBFI) {
84  }
86  int CutOff,
87  const MachineFunction *MF,
88  ProfileSummaryInfo *PSI,
89  const MachineBlockFrequencyInfo &MBFI) {
91  CutOff, MF, PSI, MBFI);
92  }
93  static bool isColdBlock(const MachineBasicBlock *MBB,
94  ProfileSummaryInfo *PSI,
95  const MachineBlockFrequencyInfo *MBFI) {
96  return machine_size_opts_detail::isColdBlock(MBB, PSI, MBFI);
97  }
98  static bool isHotBlockNthPercentile(int CutOff,
99  const MachineBasicBlock *MBB,
100  ProfileSummaryInfo *PSI,
101  const MachineBlockFrequencyInfo *MBFI) {
103  CutOff, MBB, PSI, MBFI);
104  }
105 };
106 } // end anonymous namespace
107 
109  ProfileSummaryInfo *PSI,
110  const MachineBlockFrequencyInfo *MBFI,
111  PGSOQueryType QueryType) {
112  return shouldFuncOptimizeForSizeImpl<MachineBasicBlockBFIAdapter>(
113  MF, PSI, MBFI, QueryType);
114 }
115 
117  ProfileSummaryInfo *PSI,
118  const MachineBlockFrequencyInfo *MBFI,
119  PGSOQueryType QueryType) {
120  return shouldOptimizeForSizeImpl<MachineBasicBlockBFIAdapter>(
121  MBB, PSI, MBFI, QueryType);
122 }
bool isFunctionColdInCallGraph(const MachineFunction *MF, ProfileSummaryInfo *PSI, const MachineBlockFrequencyInfo &MBFI)
Like ProfileSummaryInfo::isFunctionColdInCallGraph but for MachineFunction.
This class represents lattice values for constants.
Definition: AllocatorList.h:23
bool isColdCount(uint64_t C)
Returns true if count C is considered cold.
Analysis providing profile information.
MachineBlockFrequencyInfo pass uses BlockFrequencyInfoImpl implementation to estimate machine basic b...
static bool isHotBlockNthPercentile(int PercentileCutoff, const MachineBasicBlock *MBB, ProfileSummaryInfo *PSI, const MachineBlockFrequencyInfo *MBFI)
Like ProfileSummaryInfo::isHotBlockNthPercentile but for MachineBasicBlock.
Optional< uint64_t > getBlockProfileCount(const MachineBasicBlock *MBB) const
cl::opt< bool > EnablePGSO
ProfileCount getEntryCount(bool AllowSynthetic=false) const
Get the entry count for this function.
Definition: Function.cpp:1571
bool isHotCountNthPercentile(int PercentileCutoff, uint64_t C)
Returns true if count C is considered hot with regard to a given hot percentile cutoff value...
cl::opt< int > PgsoCutoffInstrProf
const Function & getFunction() const
Return the LLVM function that this machine code represents.
bool shouldOptimizeForSize(const MachineFunction *MF, ProfileSummaryInfo *PSI, const MachineBlockFrequencyInfo *BFI, PGSOQueryType QueryType=PGSOQueryType::Other)
Returns true if machine function MF is suggested to be size-optimized based on the profile...
cl::opt< bool > PGSOLargeWorkingSetSizeOnly
bool isFunctionHotInCallGraphNthPercentile(int PercentileCutoff, const MachineFunction *MF, ProfileSummaryInfo *PSI, const MachineBlockFrequencyInfo &MBFI)
Like ProfileSummaryInfo::isFunctionHotInCallGraphNthPercentile but for MachineFunction.
PGSOQueryType
Definition: SizeOpts.h:35
bool isColdBlock(const MachineBasicBlock *MBB, ProfileSummaryInfo *PSI, const MachineBlockFrequencyInfo *MBFI)
Like ProfileSummaryInfo::isColdBlock but for MachineBasicBlock.
cl::opt< bool > ForcePGSO
cl::opt< int > PgsoCutoffSampleProf