xref: /src/contrib/llvm-project/llvm/lib/CodeGen/MBFIWrapper.cpp (revision 5f757f3ff9144b609b3c433dfd370cc6bdc191ad)
1cfca06d7SDimitry Andric //===- MBFIWrapper.cpp - MachineBlockFrequencyInfo wrapper ----------------===//
2cfca06d7SDimitry Andric //
3cfca06d7SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4cfca06d7SDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
5cfca06d7SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6cfca06d7SDimitry Andric //
7cfca06d7SDimitry Andric //===----------------------------------------------------------------------===//
8cfca06d7SDimitry Andric //
9cfca06d7SDimitry Andric // This class keeps track of branch frequencies of newly created blocks and
10cfca06d7SDimitry Andric // tail-merged blocks. Used by the TailDuplication and MachineBlockPlacement.
11cfca06d7SDimitry Andric //
12cfca06d7SDimitry Andric //===----------------------------------------------------------------------===//
13cfca06d7SDimitry Andric 
14cfca06d7SDimitry Andric #include "llvm/CodeGen/MachineBlockFrequencyInfo.h"
15344a3780SDimitry Andric #include "llvm/CodeGen/MBFIWrapper.h"
16e3b55780SDimitry Andric #include <optional>
17cfca06d7SDimitry Andric 
18cfca06d7SDimitry Andric using namespace llvm;
19cfca06d7SDimitry Andric 
getBlockFreq(const MachineBasicBlock * MBB) const20cfca06d7SDimitry Andric BlockFrequency MBFIWrapper::getBlockFreq(const MachineBasicBlock *MBB) const {
21cfca06d7SDimitry Andric   auto I = MergedBBFreq.find(MBB);
22cfca06d7SDimitry Andric 
23cfca06d7SDimitry Andric   if (I != MergedBBFreq.end())
24cfca06d7SDimitry Andric     return I->second;
25cfca06d7SDimitry Andric 
26cfca06d7SDimitry Andric   return MBFI.getBlockFreq(MBB);
27cfca06d7SDimitry Andric }
28cfca06d7SDimitry Andric 
setBlockFreq(const MachineBasicBlock * MBB,BlockFrequency F)29cfca06d7SDimitry Andric void MBFIWrapper::setBlockFreq(const MachineBasicBlock *MBB,
30cfca06d7SDimitry Andric                                BlockFrequency F) {
31cfca06d7SDimitry Andric   MergedBBFreq[MBB] = F;
32cfca06d7SDimitry Andric }
33cfca06d7SDimitry Andric 
34e3b55780SDimitry Andric std::optional<uint64_t>
getBlockProfileCount(const MachineBasicBlock * MBB) const35b60736ecSDimitry Andric MBFIWrapper::getBlockProfileCount(const MachineBasicBlock *MBB) const {
36b60736ecSDimitry Andric   auto I = MergedBBFreq.find(MBB);
37b60736ecSDimitry Andric 
38b60736ecSDimitry Andric   // Modified block frequency also impacts profile count. So we should compute
39b60736ecSDimitry Andric   // profile count from new block frequency if it has been changed.
40b60736ecSDimitry Andric   if (I != MergedBBFreq.end())
41b1c73532SDimitry Andric     return MBFI.getProfileCountFromFreq(I->second);
42b60736ecSDimitry Andric 
43b60736ecSDimitry Andric   return MBFI.getBlockProfileCount(MBB);
44b60736ecSDimitry Andric }
45b60736ecSDimitry Andric 
view(const Twine & Name,bool isSimple)46cfca06d7SDimitry Andric void MBFIWrapper::view(const Twine &Name, bool isSimple) {
47cfca06d7SDimitry Andric   MBFI.view(Name, isSimple);
48cfca06d7SDimitry Andric }
49cfca06d7SDimitry Andric 
getEntryFreq() const50b1c73532SDimitry Andric BlockFrequency MBFIWrapper::getEntryFreq() const { return MBFI.getEntryFreq(); }
51