llvm-c166/llvm/lib/CodeGen/LazyMachineBlockFrequencyInfo.cpp
Alexis Engelke 206e4c0549
[Analysis] Use CycleInfo for BlockFrequencyInfo (#213488)
BranchProbabilityAnalysis uses CycleInfo, but BFI doesn't, causing the
somewhat redundant construction of an extra LoopInfo. Avoid this by
porting BFI to use CycleInfo.

This requires a minor change to the BFI implementation to avoid
incorrect results with irreducible loops that show up as nested but
don't show up as nested loops -- such cycle entries are skipped now.
(Such an entry heads a loop the cycle absorbed, and which entry keeps a
nested cycle of its own depends on the order the search found the entries
in.) @crossloops reaches c1 and c2 alike, yet only c1 heads a nested cycle,
so seeding it gave c1 a loop scale c2 never got and their frequencies came
out 0.68571 and 1.1429 where the test derives 1.0 for both. Represent none
of those entries and leave the region to computeIrreducibleMass, which
decomposes it from the reverse postorder as it does when LoopInfo finds
no natural loop there.

Passing the parent down also fixes the loop nest: &Loops.back() is whichever
loop was created last, not the enclosing one, and every crash on this branch
came from that.

Co-authored-by: Fangrui Song <i@maskray.me>
2026-08-05 10:21:50 +00:00

80 lines
2.9 KiB
C++

///===- LazyMachineBlockFrequencyInfo.cpp - Lazy Machine Block Frequency --===//
///
/// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
/// See https://llvm.org/LICENSE.txt for license information.
/// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
///
///===---------------------------------------------------------------------===//
/// \file
/// This is an alternative analysis pass to MachineBlockFrequencyInfo. The
/// difference is that with this pass the block frequencies are not computed
/// when the analysis pass is executed but rather when the BFI result is
/// explicitly requested by the analysis client.
///
///===---------------------------------------------------------------------===//
#include "llvm/CodeGen/LazyMachineBlockFrequencyInfo.h"
#include "llvm/CodeGen/MachineBranchProbabilityInfo.h"
#include "llvm/CodeGen/MachineCycleAnalysis.h"
#include "llvm/InitializePasses.h"
using namespace llvm;
#define DEBUG_TYPE "lazy-machine-block-freq"
INITIALIZE_PASS_BEGIN(LazyMachineBlockFrequencyInfoPass, DEBUG_TYPE,
"Lazy Machine Block Frequency Analysis", true, true)
INITIALIZE_PASS_DEPENDENCY(MachineBranchProbabilityInfoWrapperPass)
INITIALIZE_PASS_DEPENDENCY(MachineCycleInfoWrapperPass)
INITIALIZE_PASS_END(LazyMachineBlockFrequencyInfoPass, DEBUG_TYPE,
"Lazy Machine Block Frequency Analysis", true, true)
char LazyMachineBlockFrequencyInfoPass::ID = 0;
LazyMachineBlockFrequencyInfoPass::LazyMachineBlockFrequencyInfoPass()
: MachineFunctionPass(ID) {}
void LazyMachineBlockFrequencyInfoPass::getAnalysisUsage(
AnalysisUsage &AU) const {
AU.addRequired<MachineBranchProbabilityInfoWrapperPass>();
AU.setPreservesAll();
MachineFunctionPass::getAnalysisUsage(AU);
}
void LazyMachineBlockFrequencyInfoPass::releaseMemory() {
OwnedMBFI.reset();
OwnedMCI.reset();
}
MachineBlockFrequencyInfo &
LazyMachineBlockFrequencyInfoPass::calculateIfNotAvailable() const {
auto *MBFIWrapper =
getAnalysisIfAvailable<MachineBlockFrequencyInfoWrapperPass>();
if (MBFIWrapper) {
LLVM_DEBUG(dbgs() << "MachineBlockFrequencyInfo is available\n");
return MBFIWrapper->getMBFI();
}
auto &MBPI = getAnalysis<MachineBranchProbabilityInfoWrapperPass>().getMBPI();
auto *MCIWrapper = getAnalysisIfAvailable<MachineCycleInfoWrapperPass>();
auto *MCI = MCIWrapper ? &MCIWrapper->getCycleInfo() : nullptr;
LLVM_DEBUG(dbgs() << "Building MachineBlockFrequencyInfo on the fly\n");
LLVM_DEBUG(if (MCI) dbgs() << "CycleInfo is available\n");
if (!MCI) {
LLVM_DEBUG(dbgs() << "Building CycleInfo on the fly\n");
OwnedMCI = std::make_unique<MachineCycleInfo>();
OwnedMCI->compute(*MF);
MCI = OwnedMCI.get();
}
OwnedMBFI = std::make_unique<MachineBlockFrequencyInfo>();
OwnedMBFI->calculate(*MF, MBPI, *MCI);
return *OwnedMBFI;
}
bool LazyMachineBlockFrequencyInfoPass::runOnMachineFunction(
MachineFunction &F) {
MF = &F;
return false;
}