diff --git a/llvm/lib/Analysis/ScalarEvolution.cpp b/llvm/lib/Analysis/ScalarEvolution.cpp --- a/llvm/lib/Analysis/ScalarEvolution.cpp +++ b/llvm/lib/Analysis/ScalarEvolution.cpp @@ -13410,6 +13410,25 @@ SCEVTraversal(F).visitAll(S); } +static void getReachableBlocks(SmallPtrSetImpl &Reachable, + Function &F) { + SmallVector Worklist; + Worklist.push_back(&F.getEntryBlock()); + while (!Worklist.empty()) { + BasicBlock *BB = Worklist.pop_back_val(); + if (!Reachable.insert(BB).second) + continue; + + const APInt *Cond; + BasicBlock *TrueBB, *FalseBB; + if (match(BB->getTerminator(), + m_Br(m_APInt(Cond), m_BasicBlock(TrueBB), m_BasicBlock(FalseBB)))) + Worklist.push_back(Cond->isOne() ? TrueBB : FalseBB); + else + append_range(Worklist, successors(BB)); + } +} + void ScalarEvolution::verify() const { ScalarEvolution &SE = *const_cast(this); ScalarEvolution SE2(F, TLI, AC, DT, LI); @@ -13434,11 +13453,18 @@ }; SCEVMapper SCM(SE2); + SmallPtrSet ReachableBlocks; + getReachableBlocks(ReachableBlocks, F); while (!LoopStack.empty()) { auto *L = LoopStack.pop_back_val(); llvm::append_range(LoopStack, *L); + // Only verify BECounts in reachable loops. For an unreachable loop, + // any BECount is legal. + if (!ReachableBlocks.contains(L->getHeader())) + continue; + auto *CurBECount = SCM.visit( const_cast(this)->getBackedgeTakenCount(L)); auto *NewBECount = SE2.getBackedgeTakenCount(L); diff --git a/llvm/test/Transforms/IndVarSimplify/X86/pr35406.ll b/llvm/test/Transforms/IndVarSimplify/X86/pr35406.ll --- a/llvm/test/Transforms/IndVarSimplify/X86/pr35406.ll +++ b/llvm/test/Transforms/IndVarSimplify/X86/pr35406.ll @@ -1,5 +1,5 @@ ; NOTE: Assertions have been autogenerated by utils/update_test_checks.py -; RUN: opt -S -indvars %s | FileCheck %s +; RUN: opt -S -passes='loop(indvars),verify' %s | FileCheck %s target datalayout = "e-m:e-i64:64-f80:128-n8:16:32:64-S128-ni:1" target triple = "x86_64-unknown-linux-gnu"