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