xref: /freebsd-src/contrib/llvm-project/llvm/lib/CodeGen/MachineFunctionSplitter.cpp (revision 81ad626541db97eb356e2c1d4a20eb2a26a766ab)
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 
26fe6060f1SDimitry Andric #include "llvm/ADT/SmallVector.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/InitializePasses.h"
37e8d8bef9SDimitry Andric #include "llvm/Support/CommandLine.h"
38e8d8bef9SDimitry Andric 
39e8d8bef9SDimitry Andric using namespace llvm;
40e8d8bef9SDimitry Andric 
41e8d8bef9SDimitry Andric // FIXME: This cutoff value is CPU dependent and should be moved to
42e8d8bef9SDimitry Andric // TargetTransformInfo once we consider enabling this on other platforms.
43e8d8bef9SDimitry Andric // The value is expressed as a ProfileSummaryInfo integer percentile cutoff.
44e8d8bef9SDimitry Andric // Defaults to 999950, i.e. all blocks colder than 99.995 percentile are split.
45e8d8bef9SDimitry Andric // The default was empirically determined to be optimal when considering cutoff
46e8d8bef9SDimitry Andric // values between 99%-ile to 100%-ile with respect to iTLB and icache metrics on
47e8d8bef9SDimitry Andric // Intel CPUs.
48e8d8bef9SDimitry Andric static cl::opt<unsigned>
49e8d8bef9SDimitry Andric     PercentileCutoff("mfs-psi-cutoff",
50e8d8bef9SDimitry Andric                      cl::desc("Percentile profile summary cutoff used to "
51e8d8bef9SDimitry Andric                               "determine cold blocks. Unused if set to zero."),
52e8d8bef9SDimitry Andric                      cl::init(999950), cl::Hidden);
53e8d8bef9SDimitry Andric 
54e8d8bef9SDimitry Andric static cl::opt<unsigned> ColdCountThreshold(
55e8d8bef9SDimitry Andric     "mfs-count-threshold",
56e8d8bef9SDimitry Andric     cl::desc(
57e8d8bef9SDimitry Andric         "Minimum number of times a block must be executed to be retained."),
58e8d8bef9SDimitry Andric     cl::init(1), cl::Hidden);
59e8d8bef9SDimitry Andric 
60e8d8bef9SDimitry Andric namespace {
61e8d8bef9SDimitry Andric 
62e8d8bef9SDimitry Andric class MachineFunctionSplitter : public MachineFunctionPass {
63e8d8bef9SDimitry Andric public:
64e8d8bef9SDimitry Andric   static char ID;
65e8d8bef9SDimitry Andric   MachineFunctionSplitter() : MachineFunctionPass(ID) {
66e8d8bef9SDimitry Andric     initializeMachineFunctionSplitterPass(*PassRegistry::getPassRegistry());
67e8d8bef9SDimitry Andric   }
68e8d8bef9SDimitry Andric 
69e8d8bef9SDimitry Andric   StringRef getPassName() const override {
70e8d8bef9SDimitry Andric     return "Machine Function Splitter Transformation";
71e8d8bef9SDimitry Andric   }
72e8d8bef9SDimitry Andric 
73e8d8bef9SDimitry Andric   void getAnalysisUsage(AnalysisUsage &AU) const override;
74e8d8bef9SDimitry Andric 
75e8d8bef9SDimitry Andric   bool runOnMachineFunction(MachineFunction &F) override;
76e8d8bef9SDimitry Andric };
77e8d8bef9SDimitry Andric } // end anonymous namespace
78e8d8bef9SDimitry Andric 
79fe6060f1SDimitry Andric static bool isColdBlock(const MachineBasicBlock &MBB,
80e8d8bef9SDimitry Andric                         const MachineBlockFrequencyInfo *MBFI,
81e8d8bef9SDimitry Andric                         ProfileSummaryInfo *PSI) {
82e8d8bef9SDimitry Andric   Optional<uint64_t> Count = MBFI->getBlockProfileCount(&MBB);
83*81ad6265SDimitry Andric   if (!Count)
84e8d8bef9SDimitry Andric     return true;
85e8d8bef9SDimitry Andric 
86e8d8bef9SDimitry Andric   if (PercentileCutoff > 0) {
87e8d8bef9SDimitry Andric     return PSI->isColdCountNthPercentile(PercentileCutoff, *Count);
88e8d8bef9SDimitry Andric   }
89e8d8bef9SDimitry Andric   return (*Count < ColdCountThreshold);
90e8d8bef9SDimitry Andric }
91e8d8bef9SDimitry Andric 
92e8d8bef9SDimitry Andric bool MachineFunctionSplitter::runOnMachineFunction(MachineFunction &MF) {
93e8d8bef9SDimitry Andric   // TODO: We only target functions with profile data. Static information may
94e8d8bef9SDimitry Andric   // also be considered but we don't see performance improvements yet.
95e8d8bef9SDimitry Andric   if (!MF.getFunction().hasProfileData())
96e8d8bef9SDimitry Andric     return false;
97e8d8bef9SDimitry Andric 
98e8d8bef9SDimitry Andric   // TODO: We don't split functions where a section attribute has been set
99e8d8bef9SDimitry Andric   // since the split part may not be placed in a contiguous region. It may also
100e8d8bef9SDimitry Andric   // be more beneficial to augment the linker to ensure contiguous layout of
101e8d8bef9SDimitry Andric   // split functions within the same section as specified by the attribute.
102fe6060f1SDimitry Andric   if (MF.getFunction().hasSection() ||
103fe6060f1SDimitry Andric       MF.getFunction().hasFnAttribute("implicit-section-name"))
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();
109*81ad6265SDimitry Andric   if (SectionPrefix && (SectionPrefix.getValue().equals("unlikely") ||
110e8d8bef9SDimitry Andric                         SectionPrefix.getValue().equals("unknown"))) {
111e8d8bef9SDimitry Andric     return false;
112e8d8bef9SDimitry Andric   }
113e8d8bef9SDimitry Andric 
114e8d8bef9SDimitry Andric   // Renumbering blocks here preserves the order of the blocks as
115e8d8bef9SDimitry Andric   // sortBasicBlocksAndUpdateBranches uses the numeric identifier to sort
116e8d8bef9SDimitry Andric   // blocks. Preserving the order of blocks is essential to retaining decisions
117e8d8bef9SDimitry Andric   // made by prior passes such as MachineBlockPlacement.
118e8d8bef9SDimitry Andric   MF.RenumberBlocks();
119e8d8bef9SDimitry Andric   MF.setBBSectionsType(BasicBlockSection::Preset);
120e8d8bef9SDimitry Andric   auto *MBFI = &getAnalysis<MachineBlockFrequencyInfo>();
121e8d8bef9SDimitry Andric   auto *PSI = &getAnalysis<ProfileSummaryInfoWrapperPass>().getPSI();
122e8d8bef9SDimitry Andric 
123fe6060f1SDimitry Andric   SmallVector<MachineBasicBlock *, 2> LandingPads;
124e8d8bef9SDimitry Andric   for (auto &MBB : MF) {
125fe6060f1SDimitry Andric     if (MBB.isEntryBlock())
126e8d8bef9SDimitry Andric       continue;
127fe6060f1SDimitry Andric 
128fe6060f1SDimitry Andric     if (MBB.isEHPad())
129fe6060f1SDimitry Andric       LandingPads.push_back(&MBB);
130fe6060f1SDimitry Andric     else if (isColdBlock(MBB, MBFI, PSI))
131e8d8bef9SDimitry Andric       MBB.setSectionID(MBBSectionID::ColdSectionID);
132e8d8bef9SDimitry Andric   }
133e8d8bef9SDimitry Andric 
134fe6060f1SDimitry Andric   // We only split out eh pads if all of them are cold.
135fe6060f1SDimitry Andric   bool HasHotLandingPads = false;
136fe6060f1SDimitry Andric   for (const MachineBasicBlock *LP : LandingPads) {
137fe6060f1SDimitry Andric     if (!isColdBlock(*LP, MBFI, PSI))
138fe6060f1SDimitry Andric       HasHotLandingPads = true;
139fe6060f1SDimitry Andric   }
140fe6060f1SDimitry Andric   if (!HasHotLandingPads) {
141fe6060f1SDimitry Andric     for (MachineBasicBlock *LP : LandingPads)
142fe6060f1SDimitry Andric       LP->setSectionID(MBBSectionID::ColdSectionID);
143fe6060f1SDimitry Andric   }
144fe6060f1SDimitry Andric 
145e8d8bef9SDimitry Andric   auto Comparator = [](const MachineBasicBlock &X, const MachineBasicBlock &Y) {
146e8d8bef9SDimitry Andric     return X.getSectionID().Type < Y.getSectionID().Type;
147e8d8bef9SDimitry Andric   };
148e8d8bef9SDimitry Andric   llvm::sortBasicBlocksAndUpdateBranches(MF, Comparator);
149e8d8bef9SDimitry Andric 
150e8d8bef9SDimitry Andric   return true;
151e8d8bef9SDimitry Andric }
152e8d8bef9SDimitry Andric 
153e8d8bef9SDimitry Andric void MachineFunctionSplitter::getAnalysisUsage(AnalysisUsage &AU) const {
154e8d8bef9SDimitry Andric   AU.addRequired<MachineModuleInfoWrapperPass>();
155e8d8bef9SDimitry Andric   AU.addRequired<MachineBlockFrequencyInfo>();
156e8d8bef9SDimitry Andric   AU.addRequired<ProfileSummaryInfoWrapperPass>();
157e8d8bef9SDimitry Andric }
158e8d8bef9SDimitry Andric 
159e8d8bef9SDimitry Andric char MachineFunctionSplitter::ID = 0;
160e8d8bef9SDimitry Andric INITIALIZE_PASS(MachineFunctionSplitter, "machine-function-splitter",
161e8d8bef9SDimitry Andric                 "Split machine functions using profile information", false,
162e8d8bef9SDimitry Andric                 false)
163e8d8bef9SDimitry Andric 
164e8d8bef9SDimitry Andric MachineFunctionPass *llvm::createMachineFunctionSplitterPass() {
165e8d8bef9SDimitry Andric   return new MachineFunctionSplitter();
166e8d8bef9SDimitry Andric }
167