15ffd83dbSDimitry Andric //===- MBFIWrapper.cpp - MachineBlockFrequencyInfo wrapper ----------------===//
25ffd83dbSDimitry Andric //
35ffd83dbSDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
45ffd83dbSDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
55ffd83dbSDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
65ffd83dbSDimitry Andric //
75ffd83dbSDimitry Andric //===----------------------------------------------------------------------===//
85ffd83dbSDimitry Andric //
95ffd83dbSDimitry Andric // This class keeps track of branch frequencies of newly created blocks and
105ffd83dbSDimitry Andric // tail-merged blocks. Used by the TailDuplication and MachineBlockPlacement.
115ffd83dbSDimitry Andric //
125ffd83dbSDimitry Andric //===----------------------------------------------------------------------===//
135ffd83dbSDimitry Andric
14*5f7ddb14SDimitry Andric #include "llvm/ADT/Optional.h"
155ffd83dbSDimitry Andric #include "llvm/CodeGen/MachineBlockFrequencyInfo.h"
16*5f7ddb14SDimitry Andric #include "llvm/CodeGen/MBFIWrapper.h"
175ffd83dbSDimitry Andric
185ffd83dbSDimitry Andric using namespace llvm;
195ffd83dbSDimitry Andric
getBlockFreq(const MachineBasicBlock * MBB) const205ffd83dbSDimitry Andric BlockFrequency MBFIWrapper::getBlockFreq(const MachineBasicBlock *MBB) const {
215ffd83dbSDimitry Andric auto I = MergedBBFreq.find(MBB);
225ffd83dbSDimitry Andric
235ffd83dbSDimitry Andric if (I != MergedBBFreq.end())
245ffd83dbSDimitry Andric return I->second;
255ffd83dbSDimitry Andric
265ffd83dbSDimitry Andric return MBFI.getBlockFreq(MBB);
275ffd83dbSDimitry Andric }
285ffd83dbSDimitry Andric
setBlockFreq(const MachineBasicBlock * MBB,BlockFrequency F)295ffd83dbSDimitry Andric void MBFIWrapper::setBlockFreq(const MachineBasicBlock *MBB,
305ffd83dbSDimitry Andric BlockFrequency F) {
315ffd83dbSDimitry Andric MergedBBFreq[MBB] = F;
325ffd83dbSDimitry Andric }
335ffd83dbSDimitry Andric
34af732203SDimitry Andric Optional<uint64_t>
getBlockProfileCount(const MachineBasicBlock * MBB) const35af732203SDimitry Andric MBFIWrapper::getBlockProfileCount(const MachineBasicBlock *MBB) const {
36af732203SDimitry Andric auto I = MergedBBFreq.find(MBB);
37af732203SDimitry Andric
38af732203SDimitry Andric // Modified block frequency also impacts profile count. So we should compute
39af732203SDimitry Andric // profile count from new block frequency if it has been changed.
40af732203SDimitry Andric if (I != MergedBBFreq.end())
41af732203SDimitry Andric return MBFI.getProfileCountFromFreq(I->second.getFrequency());
42af732203SDimitry Andric
43af732203SDimitry Andric return MBFI.getBlockProfileCount(MBB);
44af732203SDimitry Andric }
45af732203SDimitry Andric
printBlockFreq(raw_ostream & OS,const MachineBasicBlock * MBB) const465ffd83dbSDimitry Andric raw_ostream & MBFIWrapper::printBlockFreq(raw_ostream &OS,
475ffd83dbSDimitry Andric const MachineBasicBlock *MBB) const {
485ffd83dbSDimitry Andric return MBFI.printBlockFreq(OS, getBlockFreq(MBB));
495ffd83dbSDimitry Andric }
505ffd83dbSDimitry Andric
printBlockFreq(raw_ostream & OS,const BlockFrequency Freq) const515ffd83dbSDimitry Andric raw_ostream & MBFIWrapper::printBlockFreq(raw_ostream &OS,
525ffd83dbSDimitry Andric const BlockFrequency Freq) const {
535ffd83dbSDimitry Andric return MBFI.printBlockFreq(OS, Freq);
545ffd83dbSDimitry Andric }
555ffd83dbSDimitry Andric
view(const Twine & Name,bool isSimple)565ffd83dbSDimitry Andric void MBFIWrapper::view(const Twine &Name, bool isSimple) {
575ffd83dbSDimitry Andric MBFI.view(Name, isSimple);
585ffd83dbSDimitry Andric }
595ffd83dbSDimitry Andric
getEntryFreq() const605ffd83dbSDimitry Andric uint64_t MBFIWrapper::getEntryFreq() const {
615ffd83dbSDimitry Andric return MBFI.getEntryFreq();
625ffd83dbSDimitry Andric }
63