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