diff --git a/llvm/include/llvm/CodeGen/MBFIWrapper.h b/llvm/include/llvm/CodeGen/MBFIWrapper.h --- a/llvm/include/llvm/CodeGen/MBFIWrapper.h +++ b/llvm/include/llvm/CodeGen/MBFIWrapper.h @@ -28,6 +28,8 @@ BlockFrequency getBlockFreq(const MachineBasicBlock *MBB) const; void setBlockFreq(const MachineBasicBlock *MBB, BlockFrequency F); + Optional getBlockProfileCount(const MachineBasicBlock *MBB) const; + raw_ostream &printBlockFreq(raw_ostream &OS, const MachineBasicBlock *MBB) const; raw_ostream &printBlockFreq(raw_ostream &OS, diff --git a/llvm/lib/CodeGen/MBFIWrapper.cpp b/llvm/lib/CodeGen/MBFIWrapper.cpp --- a/llvm/lib/CodeGen/MBFIWrapper.cpp +++ b/llvm/lib/CodeGen/MBFIWrapper.cpp @@ -30,6 +30,18 @@ MergedBBFreq[MBB] = F; } +Optional +MBFIWrapper::getBlockProfileCount(const MachineBasicBlock *MBB) const { + auto I = MergedBBFreq.find(MBB); + + // Modified block frequency also impacts profile count. So we should compute + // profile count from new block frequency if it has been changed. + if (I != MergedBBFreq.end()) + return MBFI.getProfileCountFromFreq(I->second.getFrequency()); + + return MBFI.getBlockProfileCount(MBB); +} + raw_ostream & MBFIWrapper::printBlockFreq(raw_ostream &OS, const MachineBasicBlock *MBB) const { return MBFI.printBlockFreq(OS, getBlockFreq(MBB)); diff --git a/llvm/lib/CodeGen/MachineBlockPlacement.cpp b/llvm/lib/CodeGen/MachineBlockPlacement.cpp --- a/llvm/lib/CodeGen/MachineBlockPlacement.cpp +++ b/llvm/lib/CodeGen/MachineBlockPlacement.cpp @@ -418,7 +418,7 @@ /// The return value is used to model tail duplication cost. BlockFrequency getBlockCountOrFrequency(const MachineBasicBlock *BB) { if (UseProfileCount) { - auto Count = MBFI->getMBFI().getBlockProfileCount(BB); + auto Count = MBFI->getBlockProfileCount(BB); if (Count) return *Count; else