1e8d8bef9SDimitry Andric //===-- MachineFunctionSplitter.cpp - Split machine functions //-----------===//
2e8d8bef9SDimitry Andric //
3e8d8bef9SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4e8d8bef9SDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
5e8d8bef9SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6e8d8bef9SDimitry Andric //
7e8d8bef9SDimitry Andric //===----------------------------------------------------------------------===//
8e8d8bef9SDimitry Andric //
9e8d8bef9SDimitry Andric // \file
10e8d8bef9SDimitry Andric // Uses profile information to split out cold blocks.
11e8d8bef9SDimitry Andric //
12e8d8bef9SDimitry Andric // This pass splits out cold machine basic blocks from the parent function. This
13e8d8bef9SDimitry Andric // implementation leverages the basic block section framework. Blocks marked
14e8d8bef9SDimitry Andric // cold by this pass are grouped together in a separate section prefixed with
15e8d8bef9SDimitry Andric // ".text.unlikely.*". The linker can then group these together as a cold
16e8d8bef9SDimitry Andric // section. The split part of the function is a contiguous region identified by
17e8d8bef9SDimitry Andric // the symbol "foo.cold". Grouping all cold blocks across functions together
18e8d8bef9SDimitry Andric // decreases fragmentation and improves icache and itlb utilization. Note that
19e8d8bef9SDimitry Andric // the overall changes to the binary size are negligible; only a small number of
20e8d8bef9SDimitry Andric // additional jump instructions may be introduced.
21e8d8bef9SDimitry Andric //
22e8d8bef9SDimitry Andric // For the original RFC of this pass please see
23e8d8bef9SDimitry Andric // https://groups.google.com/d/msg/llvm-dev/RUegaMg-iqc/wFAVxa6fCgAJ
24e8d8bef9SDimitry Andric //===----------------------------------------------------------------------===//
25e8d8bef9SDimitry Andric 
26e8d8bef9SDimitry Andric #include "llvm/ADT/Statistic.h"
27e8d8bef9SDimitry Andric #include "llvm/Analysis/ProfileSummaryInfo.h"
28e8d8bef9SDimitry Andric #include "llvm/CodeGen/BasicBlockSectionUtils.h"
29e8d8bef9SDimitry Andric #include "llvm/CodeGen/MachineBasicBlock.h"
30e8d8bef9SDimitry Andric #include "llvm/CodeGen/MachineBlockFrequencyInfo.h"
31e8d8bef9SDimitry Andric #include "llvm/CodeGen/MachineFunction.h"
32e8d8bef9SDimitry Andric #include "llvm/CodeGen/MachineFunctionPass.h"
33e8d8bef9SDimitry Andric #include "llvm/CodeGen/MachineModuleInfo.h"
34e8d8bef9SDimitry Andric #include "llvm/CodeGen/Passes.h"
35e8d8bef9SDimitry Andric #include "llvm/IR/Function.h"
36e8d8bef9SDimitry Andric #include "llvm/IR/Module.h"
37e8d8bef9SDimitry Andric #include "llvm/InitializePasses.h"
38e8d8bef9SDimitry Andric #include "llvm/Support/CommandLine.h"
39e8d8bef9SDimitry Andric 
40e8d8bef9SDimitry Andric using namespace llvm;
41e8d8bef9SDimitry Andric 
42e8d8bef9SDimitry Andric // FIXME: This cutoff value is CPU dependent and should be moved to
43e8d8bef9SDimitry Andric // TargetTransformInfo once we consider enabling this on other platforms.
44e8d8bef9SDimitry Andric // The value is expressed as a ProfileSummaryInfo integer percentile cutoff.
45e8d8bef9SDimitry Andric // Defaults to 999950, i.e. all blocks colder than 99.995 percentile are split.
46e8d8bef9SDimitry Andric // The default was empirically determined to be optimal when considering cutoff
47e8d8bef9SDimitry Andric // values between 99%-ile to 100%-ile with respect to iTLB and icache metrics on
48e8d8bef9SDimitry Andric // Intel CPUs.
49e8d8bef9SDimitry Andric static cl::opt<unsigned>
50e8d8bef9SDimitry Andric     PercentileCutoff("mfs-psi-cutoff",
51e8d8bef9SDimitry Andric                      cl::desc("Percentile profile summary cutoff used to "
52e8d8bef9SDimitry Andric                               "determine cold blocks. Unused if set to zero."),
53e8d8bef9SDimitry Andric                      cl::init(999950), cl::Hidden);
54e8d8bef9SDimitry Andric 
55e8d8bef9SDimitry Andric static cl::opt<unsigned> ColdCountThreshold(
56e8d8bef9SDimitry Andric     "mfs-count-threshold",
57e8d8bef9SDimitry Andric     cl::desc(
58e8d8bef9SDimitry Andric         "Minimum number of times a block must be executed to be retained."),
59e8d8bef9SDimitry Andric     cl::init(1), cl::Hidden);
60e8d8bef9SDimitry Andric 
61e8d8bef9SDimitry Andric namespace {
62e8d8bef9SDimitry Andric 
63e8d8bef9SDimitry Andric class MachineFunctionSplitter : public MachineFunctionPass {
64e8d8bef9SDimitry Andric public:
65e8d8bef9SDimitry Andric   static char ID;
66e8d8bef9SDimitry Andric   MachineFunctionSplitter() : MachineFunctionPass(ID) {
67e8d8bef9SDimitry Andric     initializeMachineFunctionSplitterPass(*PassRegistry::getPassRegistry());
68e8d8bef9SDimitry Andric   }
69e8d8bef9SDimitry Andric 
70e8d8bef9SDimitry Andric   StringRef getPassName() const override {
71e8d8bef9SDimitry Andric     return "Machine Function Splitter Transformation";
72e8d8bef9SDimitry Andric   }
73e8d8bef9SDimitry Andric 
74e8d8bef9SDimitry Andric   void getAnalysisUsage(AnalysisUsage &AU) const override;
75e8d8bef9SDimitry Andric 
76e8d8bef9SDimitry Andric   bool runOnMachineFunction(MachineFunction &F) override;
77e8d8bef9SDimitry Andric };
78e8d8bef9SDimitry Andric } // end anonymous namespace
79e8d8bef9SDimitry Andric 
80e8d8bef9SDimitry Andric static bool isColdBlock(MachineBasicBlock &MBB,
81e8d8bef9SDimitry Andric                         const MachineBlockFrequencyInfo *MBFI,
82e8d8bef9SDimitry Andric                         ProfileSummaryInfo *PSI) {
83e8d8bef9SDimitry Andric   Optional<uint64_t> Count = MBFI->getBlockProfileCount(&MBB);
84e8d8bef9SDimitry Andric   if (!Count.hasValue())
85e8d8bef9SDimitry Andric     return true;
86e8d8bef9SDimitry Andric 
87e8d8bef9SDimitry Andric   if (PercentileCutoff > 0) {
88e8d8bef9SDimitry Andric     return PSI->isColdCountNthPercentile(PercentileCutoff, *Count);
89e8d8bef9SDimitry Andric   }
90e8d8bef9SDimitry Andric   return (*Count < ColdCountThreshold);
91e8d8bef9SDimitry Andric }
92e8d8bef9SDimitry Andric 
93e8d8bef9SDimitry Andric bool MachineFunctionSplitter::runOnMachineFunction(MachineFunction &MF) {
94e8d8bef9SDimitry Andric   // TODO: We only target functions with profile data. Static information may
95e8d8bef9SDimitry Andric   // also be considered but we don't see performance improvements yet.
96e8d8bef9SDimitry Andric   if (!MF.getFunction().hasProfileData())
97e8d8bef9SDimitry Andric     return false;
98e8d8bef9SDimitry Andric 
99e8d8bef9SDimitry Andric   // TODO: We don't split functions where a section attribute has been set
100e8d8bef9SDimitry Andric   // since the split part may not be placed in a contiguous region. It may also
101e8d8bef9SDimitry Andric   // be more beneficial to augment the linker to ensure contiguous layout of
102e8d8bef9SDimitry Andric   // split functions within the same section as specified by the attribute.
103e8d8bef9SDimitry Andric   if (!MF.getFunction().getSection().empty())
104e8d8bef9SDimitry Andric     return false;
105e8d8bef9SDimitry Andric 
106e8d8bef9SDimitry Andric   // We don't want to proceed further for cold functions
107e8d8bef9SDimitry Andric   // or functions of unknown hotness. Lukewarm functions have no prefix.
108e8d8bef9SDimitry Andric   Optional<StringRef> SectionPrefix = MF.getFunction().getSectionPrefix();
109e8d8bef9SDimitry Andric   if (SectionPrefix.hasValue() &&
110e8d8bef9SDimitry Andric       (SectionPrefix.getValue().equals("unlikely") ||
111e8d8bef9SDimitry Andric        SectionPrefix.getValue().equals("unknown"))) {
112e8d8bef9SDimitry Andric     return false;
113e8d8bef9SDimitry Andric   }
114e8d8bef9SDimitry Andric 
115e8d8bef9SDimitry Andric   // Renumbering blocks here preserves the order of the blocks as
116e8d8bef9SDimitry Andric   // sortBasicBlocksAndUpdateBranches uses the numeric identifier to sort
117e8d8bef9SDimitry Andric   // blocks. Preserving the order of blocks is essential to retaining decisions
118e8d8bef9SDimitry Andric   // made by prior passes such as MachineBlockPlacement.
119e8d8bef9SDimitry Andric   MF.RenumberBlocks();
120e8d8bef9SDimitry Andric   MF.setBBSectionsType(BasicBlockSection::Preset);
121e8d8bef9SDimitry Andric   auto *MBFI = &getAnalysis<MachineBlockFrequencyInfo>();
122e8d8bef9SDimitry Andric   auto *PSI = &getAnalysis<ProfileSummaryInfoWrapperPass>().getPSI();
123e8d8bef9SDimitry Andric 
124e8d8bef9SDimitry Andric   for (auto &MBB : MF) {
125e8d8bef9SDimitry Andric     // FIXME: We retain the entry block and conservatively keep all landing pad
126e8d8bef9SDimitry Andric     // blocks as part of the original function. Once D73739 is submitted, we can
127e8d8bef9SDimitry Andric     // improve the handling of ehpads.
128e8d8bef9SDimitry Andric     if ((MBB.pred_empty() || MBB.isEHPad()))
129e8d8bef9SDimitry Andric       continue;
130e8d8bef9SDimitry Andric     if (isColdBlock(MBB, MBFI, PSI))
131e8d8bef9SDimitry Andric       MBB.setSectionID(MBBSectionID::ColdSectionID);
132e8d8bef9SDimitry Andric   }
133e8d8bef9SDimitry Andric 
134e8d8bef9SDimitry Andric   auto Comparator = [](const MachineBasicBlock &X, const MachineBasicBlock &Y) {
135e8d8bef9SDimitry Andric     return X.getSectionID().Type < Y.getSectionID().Type;
136e8d8bef9SDimitry Andric   };
137e8d8bef9SDimitry Andric   llvm::sortBasicBlocksAndUpdateBranches(MF, Comparator);
138e8d8bef9SDimitry Andric 
139e8d8bef9SDimitry Andric   return true;
140e8d8bef9SDimitry Andric }
141e8d8bef9SDimitry Andric 
142e8d8bef9SDimitry Andric void MachineFunctionSplitter::getAnalysisUsage(AnalysisUsage &AU) const {
143e8d8bef9SDimitry Andric   AU.addRequired<MachineModuleInfoWrapperPass>();
144e8d8bef9SDimitry Andric   AU.addRequired<MachineBlockFrequencyInfo>();
145e8d8bef9SDimitry Andric   AU.addRequired<ProfileSummaryInfoWrapperPass>();
146e8d8bef9SDimitry Andric }
147e8d8bef9SDimitry Andric 
148e8d8bef9SDimitry Andric char MachineFunctionSplitter::ID = 0;
149e8d8bef9SDimitry Andric INITIALIZE_PASS(MachineFunctionSplitter, "machine-function-splitter",
150e8d8bef9SDimitry Andric                 "Split machine functions using profile information", false,
151e8d8bef9SDimitry Andric                 false)
152e8d8bef9SDimitry Andric 
153e8d8bef9SDimitry Andric MachineFunctionPass *llvm::createMachineFunctionSplitterPass() {
154e8d8bef9SDimitry Andric   return new MachineFunctionSplitter();
155e8d8bef9SDimitry Andric }
156