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" 2706c3fb27SDimitry Andric #include "llvm/Analysis/BlockFrequencyInfo.h" 2806c3fb27SDimitry Andric #include "llvm/Analysis/BranchProbabilityInfo.h" 2906c3fb27SDimitry Andric #include "llvm/Analysis/EHUtils.h" 30e8d8bef9SDimitry Andric #include "llvm/Analysis/ProfileSummaryInfo.h" 31e8d8bef9SDimitry Andric #include "llvm/CodeGen/BasicBlockSectionUtils.h" 32e8d8bef9SDimitry Andric #include "llvm/CodeGen/MachineBasicBlock.h" 33e8d8bef9SDimitry Andric #include "llvm/CodeGen/MachineBlockFrequencyInfo.h" 34e8d8bef9SDimitry Andric #include "llvm/CodeGen/MachineFunction.h" 35e8d8bef9SDimitry Andric #include "llvm/CodeGen/MachineFunctionPass.h" 36e8d8bef9SDimitry Andric #include "llvm/CodeGen/MachineModuleInfo.h" 37e8d8bef9SDimitry Andric #include "llvm/CodeGen/Passes.h" 385f757f3fSDimitry Andric #include "llvm/CodeGen/TargetInstrInfo.h" 39e8d8bef9SDimitry Andric #include "llvm/IR/Function.h" 40e8d8bef9SDimitry Andric #include "llvm/InitializePasses.h" 41e8d8bef9SDimitry Andric #include "llvm/Support/CommandLine.h" 42bdd1243dSDimitry Andric #include <optional> 43e8d8bef9SDimitry Andric 44e8d8bef9SDimitry Andric using namespace llvm; 45e8d8bef9SDimitry Andric 46e8d8bef9SDimitry Andric // FIXME: This cutoff value is CPU dependent and should be moved to 47e8d8bef9SDimitry Andric // TargetTransformInfo once we consider enabling this on other platforms. 48e8d8bef9SDimitry Andric // The value is expressed as a ProfileSummaryInfo integer percentile cutoff. 49e8d8bef9SDimitry Andric // Defaults to 999950, i.e. all blocks colder than 99.995 percentile are split. 50e8d8bef9SDimitry Andric // The default was empirically determined to be optimal when considering cutoff 51e8d8bef9SDimitry Andric // values between 99%-ile to 100%-ile with respect to iTLB and icache metrics on 52e8d8bef9SDimitry Andric // Intel CPUs. 53e8d8bef9SDimitry Andric static cl::opt<unsigned> 54e8d8bef9SDimitry Andric PercentileCutoff("mfs-psi-cutoff", 55e8d8bef9SDimitry Andric cl::desc("Percentile profile summary cutoff used to " 56e8d8bef9SDimitry Andric "determine cold blocks. Unused if set to zero."), 57e8d8bef9SDimitry Andric cl::init(999950), cl::Hidden); 58e8d8bef9SDimitry Andric 59e8d8bef9SDimitry Andric static cl::opt<unsigned> ColdCountThreshold( 60e8d8bef9SDimitry Andric "mfs-count-threshold", 61e8d8bef9SDimitry Andric cl::desc( 62e8d8bef9SDimitry Andric "Minimum number of times a block must be executed to be retained."), 63e8d8bef9SDimitry Andric cl::init(1), cl::Hidden); 64e8d8bef9SDimitry Andric 65bdd1243dSDimitry Andric static cl::opt<bool> SplitAllEHCode( 66bdd1243dSDimitry Andric "mfs-split-ehcode", 67bdd1243dSDimitry Andric cl::desc("Splits all EH code and it's descendants by default."), 68bdd1243dSDimitry Andric cl::init(false), cl::Hidden); 69bdd1243dSDimitry Andric 70e8d8bef9SDimitry Andric namespace { 71e8d8bef9SDimitry Andric 72e8d8bef9SDimitry Andric class MachineFunctionSplitter : public MachineFunctionPass { 73e8d8bef9SDimitry Andric public: 74e8d8bef9SDimitry Andric static char ID; 75e8d8bef9SDimitry Andric MachineFunctionSplitter() : MachineFunctionPass(ID) { 76e8d8bef9SDimitry Andric initializeMachineFunctionSplitterPass(*PassRegistry::getPassRegistry()); 77e8d8bef9SDimitry Andric } 78e8d8bef9SDimitry Andric 79e8d8bef9SDimitry Andric StringRef getPassName() const override { 80e8d8bef9SDimitry Andric return "Machine Function Splitter Transformation"; 81e8d8bef9SDimitry Andric } 82e8d8bef9SDimitry Andric 83e8d8bef9SDimitry Andric void getAnalysisUsage(AnalysisUsage &AU) const override; 84e8d8bef9SDimitry Andric 85e8d8bef9SDimitry Andric bool runOnMachineFunction(MachineFunction &F) override; 86e8d8bef9SDimitry Andric }; 87e8d8bef9SDimitry Andric } // end anonymous namespace 88e8d8bef9SDimitry Andric 89bdd1243dSDimitry Andric /// setDescendantEHBlocksCold - This splits all EH pads and blocks reachable 9006c3fb27SDimitry Andric /// only by EH pad as cold. This will help mark EH pads statically cold 9106c3fb27SDimitry Andric /// instead of relying on profile data. 9206c3fb27SDimitry Andric static void setDescendantEHBlocksCold(MachineFunction &MF) { 9306c3fb27SDimitry Andric DenseSet<MachineBasicBlock *> EHBlocks; 9406c3fb27SDimitry Andric computeEHOnlyBlocks(MF, EHBlocks); 9506c3fb27SDimitry Andric for (auto Block : EHBlocks) { 9606c3fb27SDimitry Andric Block->setSectionID(MBBSectionID::ColdSectionID); 9706c3fb27SDimitry Andric } 9806c3fb27SDimitry Andric } 99bdd1243dSDimitry Andric 10006c3fb27SDimitry Andric static void finishAdjustingBasicBlocksAndLandingPads(MachineFunction &MF) { 10106c3fb27SDimitry Andric auto Comparator = [](const MachineBasicBlock &X, const MachineBasicBlock &Y) { 10206c3fb27SDimitry Andric return X.getSectionID().Type < Y.getSectionID().Type; 103bdd1243dSDimitry Andric }; 10406c3fb27SDimitry Andric llvm::sortBasicBlocksAndUpdateBranches(MF, Comparator); 10506c3fb27SDimitry Andric llvm::avoidZeroOffsetLandingPad(MF); 106bdd1243dSDimitry Andric } 107bdd1243dSDimitry Andric 108fe6060f1SDimitry Andric static bool isColdBlock(const MachineBasicBlock &MBB, 109e8d8bef9SDimitry Andric const MachineBlockFrequencyInfo *MBFI, 110e8d8bef9SDimitry Andric ProfileSummaryInfo *PSI) { 111bdd1243dSDimitry Andric std::optional<uint64_t> Count = MBFI->getBlockProfileCount(&MBB); 11206c3fb27SDimitry Andric // For instrumentation profiles and sample profiles, we use different ways 11306c3fb27SDimitry Andric // to judge whether a block is cold and should be split. 11406c3fb27SDimitry Andric if (PSI->hasInstrumentationProfile() || PSI->hasCSInstrumentationProfile()) { 11506c3fb27SDimitry Andric // If using instrument profile, which is deemed "accurate", no count means 11606c3fb27SDimitry Andric // cold. 11781ad6265SDimitry Andric if (!Count) 118e8d8bef9SDimitry Andric return true; 11906c3fb27SDimitry Andric if (PercentileCutoff > 0) 120e8d8bef9SDimitry Andric return PSI->isColdCountNthPercentile(PercentileCutoff, *Count); 12106c3fb27SDimitry Andric // Fallthrough to end of function. 12206c3fb27SDimitry Andric } else if (PSI->hasSampleProfile()) { 12306c3fb27SDimitry Andric // For sample profile, no count means "do not judege coldness". 12406c3fb27SDimitry Andric if (!Count) 12506c3fb27SDimitry Andric return false; 126e8d8bef9SDimitry Andric } 12706c3fb27SDimitry Andric 128e8d8bef9SDimitry Andric return (*Count < ColdCountThreshold); 129e8d8bef9SDimitry Andric } 130e8d8bef9SDimitry Andric 131e8d8bef9SDimitry Andric bool MachineFunctionSplitter::runOnMachineFunction(MachineFunction &MF) { 132bdd1243dSDimitry Andric // We target functions with profile data. Static information in the form 133bdd1243dSDimitry Andric // of exception handling code may be split to cold if user passes the 134bdd1243dSDimitry Andric // mfs-split-ehcode flag. 135bdd1243dSDimitry Andric bool UseProfileData = MF.getFunction().hasProfileData(); 136bdd1243dSDimitry Andric if (!UseProfileData && !SplitAllEHCode) 137e8d8bef9SDimitry Andric return false; 138e8d8bef9SDimitry Andric 1395f757f3fSDimitry Andric const TargetInstrInfo &TII = *MF.getSubtarget().getInstrInfo(); 1405f757f3fSDimitry Andric if (!TII.isFunctionSafeToSplit(MF)) 141e8d8bef9SDimitry Andric return false; 142e8d8bef9SDimitry Andric 143e8d8bef9SDimitry Andric // Renumbering blocks here preserves the order of the blocks as 144e8d8bef9SDimitry Andric // sortBasicBlocksAndUpdateBranches uses the numeric identifier to sort 145e8d8bef9SDimitry Andric // blocks. Preserving the order of blocks is essential to retaining decisions 146e8d8bef9SDimitry Andric // made by prior passes such as MachineBlockPlacement. 147e8d8bef9SDimitry Andric MF.RenumberBlocks(); 148e8d8bef9SDimitry Andric MF.setBBSectionsType(BasicBlockSection::Preset); 149bdd1243dSDimitry Andric 150bdd1243dSDimitry Andric MachineBlockFrequencyInfo *MBFI = nullptr; 151bdd1243dSDimitry Andric ProfileSummaryInfo *PSI = nullptr; 152bdd1243dSDimitry Andric if (UseProfileData) { 153*0fca6ea1SDimitry Andric MBFI = &getAnalysis<MachineBlockFrequencyInfoWrapperPass>().getMBFI(); 154bdd1243dSDimitry Andric PSI = &getAnalysis<ProfileSummaryInfoWrapperPass>().getPSI(); 15506c3fb27SDimitry Andric // If we don't have a good profile (sample profile is not deemed 15606c3fb27SDimitry Andric // as a "good profile") and the function is not hot, then early 15706c3fb27SDimitry Andric // return. (Because we can only trust hot functions when profile 15806c3fb27SDimitry Andric // quality is not good.) 15906c3fb27SDimitry Andric if (PSI->hasSampleProfile() && !PSI->isFunctionHotInCallGraph(&MF, *MBFI)) { 16006c3fb27SDimitry Andric // Split all EH code and it's descendant statically by default. 16106c3fb27SDimitry Andric if (SplitAllEHCode) 16206c3fb27SDimitry Andric setDescendantEHBlocksCold(MF); 16306c3fb27SDimitry Andric finishAdjustingBasicBlocksAndLandingPads(MF); 16406c3fb27SDimitry Andric return true; 16506c3fb27SDimitry Andric } 166bdd1243dSDimitry Andric } 167e8d8bef9SDimitry Andric 168fe6060f1SDimitry Andric SmallVector<MachineBasicBlock *, 2> LandingPads; 169e8d8bef9SDimitry Andric for (auto &MBB : MF) { 170fe6060f1SDimitry Andric if (MBB.isEntryBlock()) 171e8d8bef9SDimitry Andric continue; 172fe6060f1SDimitry Andric 173fe6060f1SDimitry Andric if (MBB.isEHPad()) 174fe6060f1SDimitry Andric LandingPads.push_back(&MBB); 175*0fca6ea1SDimitry Andric else if (UseProfileData && isColdBlock(MBB, MBFI, PSI) && 176*0fca6ea1SDimitry Andric TII.isMBBSafeToSplitToCold(MBB) && !SplitAllEHCode) 177e8d8bef9SDimitry Andric MBB.setSectionID(MBBSectionID::ColdSectionID); 178e8d8bef9SDimitry Andric } 179e8d8bef9SDimitry Andric 180bdd1243dSDimitry Andric // Split all EH code and it's descendant statically by default. 181bdd1243dSDimitry Andric if (SplitAllEHCode) 18206c3fb27SDimitry Andric setDescendantEHBlocksCold(MF); 183fe6060f1SDimitry Andric // We only split out eh pads if all of them are cold. 184bdd1243dSDimitry Andric else { 18506c3fb27SDimitry Andric // Here we have UseProfileData == true. 186fe6060f1SDimitry Andric bool HasHotLandingPads = false; 187fe6060f1SDimitry Andric for (const MachineBasicBlock *LP : LandingPads) { 188*0fca6ea1SDimitry Andric if (!isColdBlock(*LP, MBFI, PSI) || !TII.isMBBSafeToSplitToCold(*LP)) 189fe6060f1SDimitry Andric HasHotLandingPads = true; 190fe6060f1SDimitry Andric } 191fe6060f1SDimitry Andric if (!HasHotLandingPads) { 192fe6060f1SDimitry Andric for (MachineBasicBlock *LP : LandingPads) 193fe6060f1SDimitry Andric LP->setSectionID(MBBSectionID::ColdSectionID); 194fe6060f1SDimitry Andric } 195bdd1243dSDimitry Andric } 19606c3fb27SDimitry Andric 19706c3fb27SDimitry Andric finishAdjustingBasicBlocksAndLandingPads(MF); 198e8d8bef9SDimitry Andric return true; 199e8d8bef9SDimitry Andric } 200e8d8bef9SDimitry Andric 201e8d8bef9SDimitry Andric void MachineFunctionSplitter::getAnalysisUsage(AnalysisUsage &AU) const { 202e8d8bef9SDimitry Andric AU.addRequired<MachineModuleInfoWrapperPass>(); 203*0fca6ea1SDimitry Andric AU.addRequired<MachineBlockFrequencyInfoWrapperPass>(); 204e8d8bef9SDimitry Andric AU.addRequired<ProfileSummaryInfoWrapperPass>(); 205e8d8bef9SDimitry Andric } 206e8d8bef9SDimitry Andric 207e8d8bef9SDimitry Andric char MachineFunctionSplitter::ID = 0; 208e8d8bef9SDimitry Andric INITIALIZE_PASS(MachineFunctionSplitter, "machine-function-splitter", 209e8d8bef9SDimitry Andric "Split machine functions using profile information", false, 210e8d8bef9SDimitry Andric false) 211e8d8bef9SDimitry Andric 212e8d8bef9SDimitry Andric MachineFunctionPass *llvm::createMachineFunctionSplitterPass() { 213e8d8bef9SDimitry Andric return new MachineFunctionSplitter(); 214e8d8bef9SDimitry Andric } 215