diff --git a/llvm/include/llvm/Analysis/LoopInfoImpl.h b/llvm/include/llvm/Analysis/LoopInfoImpl.h --- a/llvm/include/llvm/Analysis/LoopInfoImpl.h +++ b/llvm/include/llvm/Analysis/LoopInfoImpl.h @@ -537,6 +537,9 @@ void LoopInfoBase::analyze(const DomTreeBase &DomTree) { // Postorder traversal of the dominator tree. const DomTreeNodeBase *DomRoot = DomTree.getRootNode(); + if (!DomRoot) + return; + for (auto DomNode : post_order(DomRoot)) { BlockT *Header = DomNode->getBlock(); diff --git a/llvm/include/llvm/Support/GenericDomTreeConstruction.h b/llvm/include/llvm/Support/GenericDomTreeConstruction.h --- a/llvm/include/llvm/Support/GenericDomTreeConstruction.h +++ b/llvm/include/llvm/Support/GenericDomTreeConstruction.h @@ -42,6 +42,8 @@ #include "llvm/ADT/DepthFirstIterator.h" #include "llvm/ADT/PointerIntPair.h" #include "llvm/ADT/SmallPtrSet.h" +#include "llvm/IR/Function.h" +#include "llvm/Support/Casting.h" #include "llvm/Support/Debug.h" #include "llvm/Support/GenericDomTree.h" #include @@ -528,6 +530,16 @@ auto *Parent = DT.Parent; DT.reset(); DT.Parent = Parent; + + if constexpr (std::is_same< + Function *, + typename std::remove_const< + typename DomTreeT::ParentPtr>::type>::value) { + if (auto *F = dyn_cast(DT.Parent)) + if (F->empty()) + return; + } + SemiNCAInfo SNCA(nullptr); // Since we are rebuilding the whole tree, // there's no point doing it incrementally. diff --git a/llvm/lib/Analysis/BlockFrequencyInfo.cpp b/llvm/lib/Analysis/BlockFrequencyInfo.cpp --- a/llvm/lib/Analysis/BlockFrequencyInfo.cpp +++ b/llvm/lib/Analysis/BlockFrequencyInfo.cpp @@ -184,6 +184,9 @@ void BlockFrequencyInfo::calculate(const Function &F, const BranchProbabilityInfo &BPI, const LoopInfo &LI) { + if (F.empty()) + return; + if (!BFI) BFI.reset(new ImplType); BFI->calculate(F, BPI, LI); diff --git a/llvm/lib/Analysis/BranchProbabilityInfo.cpp b/llvm/lib/Analysis/BranchProbabilityInfo.cpp --- a/llvm/lib/Analysis/BranchProbabilityInfo.cpp +++ b/llvm/lib/Analysis/BranchProbabilityInfo.cpp @@ -1068,6 +1068,9 @@ PostDominatorTree *PDT) { LLVM_DEBUG(dbgs() << "---- Branch Probability Info : " << F.getName() << " ----\n\n"); + if (F.empty()) + return; + LastF = &F; // Store the last function we ran on for printing. assert(PostDominatedByUnreachable.empty()); assert(PostDominatedByColdCall.empty());