diff --git a/llvm/include/llvm/Analysis/BlockFrequencyInfoImpl.h b/llvm/include/llvm/Analysis/BlockFrequencyInfoImpl.h --- a/llvm/include/llvm/Analysis/BlockFrequencyInfoImpl.h +++ b/llvm/include/llvm/Analysis/BlockFrequencyInfoImpl.h @@ -26,6 +26,7 @@ #include "llvm/IR/BasicBlock.h" #include "llvm/Support/BlockFrequency.h" #include "llvm/Support/BranchProbability.h" +#include "llvm/Support/CommandLine.h" #include "llvm/Support/DOTGraphTraits.h" #include "llvm/Support/Debug.h" #include "llvm/Support/ErrorHandling.h" @@ -46,6 +47,8 @@ #define DEBUG_TYPE "block-freq" +extern llvm::cl::opt CheckBFIUnknownBlockQueries; + namespace llvm { class BranchProbabilityInfo; @@ -1043,6 +1046,15 @@ computeMassInFunction(); unwrapLoops(); finalizeMetrics(); + + if (CheckBFIUnknownBlockQueries) { + // To detect BFI queries for unknown blocks, add entries for unreachable + // blocks, if any. This is to distinguish between known/existing unreachable + // blocks and unknown blocks. + for (const BlockT &BB : F) + if (!Nodes.count(&BB)) + setBlockFreq(&BB, 0); + } } template diff --git a/llvm/lib/Analysis/BlockFrequencyInfoImpl.cpp b/llvm/lib/Analysis/BlockFrequencyInfoImpl.cpp --- a/llvm/lib/Analysis/BlockFrequencyInfoImpl.cpp +++ b/llvm/lib/Analysis/BlockFrequencyInfoImpl.cpp @@ -40,6 +40,12 @@ #define DEBUG_TYPE "block-freq" +cl::opt CheckBFIUnknownBlockQueries( + "check-bfi-unknown-block-queries", + cl::init(false), cl::Hidden, + cl::desc("Check if block frequency is queried for an unknown block " + "for debugging missed BFI updates")); + ScaledNumber BlockMass::toScaled() const { if (isFull()) return ScaledNumber(1, 0); @@ -550,8 +556,17 @@ BlockFrequency BlockFrequencyInfoImplBase::getBlockFreq(const BlockNode &Node) const { - if (!Node.isValid()) + if (!Node.isValid()) { +#ifndef NDEBUG + if (CheckBFIUnknownBlockQueries) { + SmallString<256> Msg; + raw_svector_ostream OS(Msg); + OS << "*** Detected BFI query for unknown block " << getBlockName(Node); + report_fatal_error(OS.str()); + } +#endif return 0; + } return Freqs[Node.Index].Integer; }