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