diff options
author | Ellis Hoag <ellis.sparky.hoag@gmail.com> | 2024-11-12 09:53:27 -0800 |
---|---|---|
committer | GitHub <noreply@github.com> | 2024-11-12 09:53:27 -0800 |
commit | 57c33acac8c74eb071ede35d819918d8bd00e45b (patch) | |
tree | 0f67c80ba01ea0a69f9f347cfc1faa4ed5eaec25 /llvm/lib/CodeGen/MachineSink.cpp | |
parent | b8d6659bff25458693c99a7c53372afcf6d66d7d (diff) | |
download | llvm-57c33acac8c74eb071ede35d819918d8bd00e45b.zip llvm-57c33acac8c74eb071ede35d819918d8bd00e45b.tar.gz llvm-57c33acac8c74eb071ede35d819918d8bd00e45b.tar.bz2 |
[MachineSink] Sink into consistent blocks for optsize funcs (#115367)
Do not consider profile data when choosing a successor block to sink
into for optsize functions. This should result in more consistent
instruction sequences which will improve outlining and ICF. We've
observed a slight codesize improvement in a large binary. This is
similar reasoning to https://github.com/llvm/llvm-project/pull/114607.
Using profile data to select a block to sink into was original added in
https://github.com/llvm/llvm-project/commit/d04f7596e79d7c5cf7e4249ad62690afaecd01ec.
Diffstat (limited to 'llvm/lib/CodeGen/MachineSink.cpp')
-rw-r--r-- | llvm/lib/CodeGen/MachineSink.cpp | 14 |
1 files changed, 10 insertions, 4 deletions
diff --git a/llvm/lib/CodeGen/MachineSink.cpp b/llvm/lib/CodeGen/MachineSink.cpp index 658ebd4..3293f7c 100644 --- a/llvm/lib/CodeGen/MachineSink.cpp +++ b/llvm/lib/CodeGen/MachineSink.cpp @@ -26,6 +26,7 @@ #include "llvm/ADT/Statistic.h" #include "llvm/Analysis/AliasAnalysis.h" #include "llvm/Analysis/CFG.h" +#include "llvm/Analysis/ProfileSummaryInfo.h" #include "llvm/CodeGen/MachineBasicBlock.h" #include "llvm/CodeGen/MachineBlockFrequencyInfo.h" #include "llvm/CodeGen/MachineBranchProbabilityInfo.h" @@ -38,6 +39,7 @@ #include "llvm/CodeGen/MachineOperand.h" #include "llvm/CodeGen/MachinePostDominators.h" #include "llvm/CodeGen/MachineRegisterInfo.h" +#include "llvm/CodeGen/MachineSizeOpts.h" #include "llvm/CodeGen/RegisterClassInfo.h" #include "llvm/CodeGen/RegisterPressure.h" #include "llvm/CodeGen/TargetInstrInfo.h" @@ -122,6 +124,7 @@ namespace { MachineDominatorTree *DT = nullptr; // Machine dominator tree MachinePostDominatorTree *PDT = nullptr; // Machine post dominator tree MachineCycleInfo *CI = nullptr; + ProfileSummaryInfo *PSI = nullptr; MachineBlockFrequencyInfo *MBFI = nullptr; const MachineBranchProbabilityInfo *MBPI = nullptr; AliasAnalysis *AA = nullptr; @@ -198,6 +201,7 @@ namespace { AU.addRequired<MachineBranchProbabilityInfoWrapperPass>(); AU.addPreserved<MachineCycleInfoWrapperPass>(); AU.addPreserved<MachineLoopInfoWrapperPass>(); + AU.addRequired<ProfileSummaryInfoWrapperPass>(); if (UseBlockFreqInfo) AU.addRequired<MachineBlockFrequencyInfoWrapperPass>(); AU.addRequired<TargetPassConfig>(); @@ -284,6 +288,7 @@ char &llvm::MachineSinkingID = MachineSinking::ID; INITIALIZE_PASS_BEGIN(MachineSinking, DEBUG_TYPE, "Machine code sinking", false, false) +INITIALIZE_PASS_DEPENDENCY(ProfileSummaryInfoWrapperPass) INITIALIZE_PASS_DEPENDENCY(MachineBranchProbabilityInfoWrapperPass) INITIALIZE_PASS_DEPENDENCY(MachineDominatorTreeWrapperPass) INITIALIZE_PASS_DEPENDENCY(MachineCycleInfoWrapperPass) @@ -722,6 +727,7 @@ bool MachineSinking::runOnMachineFunction(MachineFunction &MF) { DT = &getAnalysis<MachineDominatorTreeWrapperPass>().getDomTree(); PDT = &getAnalysis<MachinePostDominatorTreeWrapperPass>().getPostDomTree(); CI = &getAnalysis<MachineCycleInfoWrapperPass>().getCycleInfo(); + PSI = &getAnalysis<ProfileSummaryInfoWrapperPass>().getPSI(); MBFI = UseBlockFreqInfo ? &getAnalysis<MachineBlockFrequencyInfoWrapperPass>().getMBFI() : nullptr; @@ -1217,12 +1223,12 @@ MachineSinking::GetAllSortedSuccessors(MachineInstr &MI, MachineBasicBlock *MBB, // Sort Successors according to their cycle depth or block frequency info. llvm::stable_sort( - AllSuccs, [this](const MachineBasicBlock *L, const MachineBasicBlock *R) { + AllSuccs, [&](const MachineBasicBlock *L, const MachineBasicBlock *R) { uint64_t LHSFreq = MBFI ? MBFI->getBlockFreq(L).getFrequency() : 0; uint64_t RHSFreq = MBFI ? MBFI->getBlockFreq(R).getFrequency() : 0; - bool HasBlockFreq = LHSFreq != 0 || RHSFreq != 0; - return HasBlockFreq ? LHSFreq < RHSFreq - : CI->getCycleDepth(L) < CI->getCycleDepth(R); + if (llvm::shouldOptimizeForSize(MBB, PSI, MBFI) || !LHSFreq || !RHSFreq) + return CI->getCycleDepth(L) < CI->getCycleDepth(R); + return LHSFreq < RHSFreq; }); auto it = AllSuccessors.insert(std::make_pair(MBB, AllSuccs)); |