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/MBFIWrapper.h"
15 #include "llvm/CodeGen/MachineBlockFrequencyInfo.h"
16 
17 using namespace llvm;
18 
19 BlockFrequency MBFIWrapper::getBlockFreq(const MachineBasicBlock *MBB) const {
20   auto I = MergedBBFreq.find(MBB);
21 
22   if (I != MergedBBFreq.end())
23     return I->second;
24 
25   return MBFI.getBlockFreq(MBB);
26 }
27 
28 void MBFIWrapper::setBlockFreq(const MachineBasicBlock *MBB,
29                                BlockFrequency F) {
30   MergedBBFreq[MBB] = F;
31 }
32 
33 Optional<uint64_t>
34 MBFIWrapper::getBlockProfileCount(const MachineBasicBlock *MBB) const {
35   auto I = MergedBBFreq.find(MBB);
36 
37   // Modified block frequency also impacts profile count. So we should compute
38   // profile count from new block frequency if it has been changed.
39   if (I != MergedBBFreq.end())
40     return MBFI.getProfileCountFromFreq(I->second.getFrequency());
41 
42   return MBFI.getBlockProfileCount(MBB);
43 }
44 
45 raw_ostream & MBFIWrapper::printBlockFreq(raw_ostream &OS,
46                                           const MachineBasicBlock *MBB) const {
47   return MBFI.printBlockFreq(OS, getBlockFreq(MBB));
48 }
49 
50 raw_ostream & MBFIWrapper::printBlockFreq(raw_ostream &OS,
51                                           const BlockFrequency Freq) const {
52   return MBFI.printBlockFreq(OS, Freq);
53 }
54 
55 void MBFIWrapper::view(const Twine &Name, bool isSimple) {
56   MBFI.view(Name, isSimple);
57 }
58 
59 uint64_t MBFIWrapper::getEntryFreq() const {
60   return MBFI.getEntryFreq();
61 }
62