Index: llvm/include/llvm/Analysis/ScalarEvolution.h =================================================================== --- llvm/include/llvm/Analysis/ScalarEvolution.h +++ llvm/include/llvm/Analysis/ScalarEvolution.h @@ -68,6 +68,8 @@ class Value; enum SCEVTypes : unsigned short; +extern bool VerifySCEV; + /// This class represents an analyzed expression in the program. These are /// opaque objects that the client is not allowed to do much with directly. /// Index: llvm/lib/Analysis/ScalarEvolution.cpp =================================================================== --- llvm/lib/Analysis/ScalarEvolution.cpp +++ llvm/lib/Analysis/ScalarEvolution.cpp @@ -148,6 +148,9 @@ STATISTIC(NumFoundPhiSCCs, "Number of found Phi-composed strongly connected components"); +// FIXME: Enable this with EXPENSIVE_CHECKS when the test suite is clean. +bool llvm::VerifySCEV = false; + static cl::opt MaxBruteForceIterations("scalar-evolution-max-iterations", cl::ReallyHidden, cl::ZeroOrMore, @@ -156,9 +159,8 @@ "derived loop"), cl::init(100)); -// FIXME: Enable this with EXPENSIVE_CHECKS when the test suite is clean. -static cl::opt VerifySCEV( - "verify-scev", cl::Hidden, +static cl::opt VerifySCEVOpt( + "verify-scev", cl::Hidden, cl::location(VerifySCEV), cl::desc("Verify ScalarEvolution's backedge taken counts (slow)")); static cl::opt VerifySCEVStrict( "verify-scev-strict", cl::Hidden, @@ -13439,8 +13441,14 @@ auto *L = LoopStack.pop_back_val(); llvm::append_range(LoopStack, *L); - auto *CurBECount = SCM.visit( - const_cast(this)->getBackedgeTakenCount(L)); + // Only verify cached BECounts. Computing new BECounts may change the + // results of subsequent SCEV uses. + auto It = BackedgeTakenCounts.find(L); + if (It == BackedgeTakenCounts.end()) + continue; + + auto *CurBECount = + SCM.visit(It->second.getExact(L, const_cast(this))); auto *NewBECount = SE2.getBackedgeTakenCount(L); if (CurBECount == SE2.getCouldNotCompute() || Index: llvm/lib/Transforms/Scalar/LoopPassManager.cpp =================================================================== --- llvm/lib/Transforms/Scalar/LoopPassManager.cpp +++ llvm/lib/Transforms/Scalar/LoopPassManager.cpp @@ -311,12 +311,12 @@ #ifndef NDEBUG // LoopAnalysisResults should always be valid. - // Note that we don't LAR.SE.verify() because that can change observed SE - // queries. See PR44815. if (VerifyDomInfo) LAR.DT.verify(); if (VerifyLoopInfo) LAR.LI.verify(LAR.DT); + if (VerifySCEV) + LAR.SE.verify(); if (LAR.MSSA && VerifyMemorySSA) LAR.MSSA->verifyMemorySSA(); #endif Index: llvm/test/Transforms/IndVarSimplify/X86/deterministic-scev-verify.ll =================================================================== --- llvm/test/Transforms/IndVarSimplify/X86/deterministic-scev-verify.ll +++ llvm/test/Transforms/IndVarSimplify/X86/deterministic-scev-verify.ll @@ -1,18 +1,13 @@ -; RUN: opt -indvars -stats -disable-output < %s 2>&1 | FileCheck %s --check-prefix=STATS -; RUN: opt -indvars -S < %s | FileCheck %s --check-prefix=IR -; REQUIRES: asserts +; RUN: opt -indvars -S < %s | FileCheck %s ; Check that IndVarSimplify's result is not influenced by stray calls to ; ScalarEvolution in debug builds. However, -verify-indvars may still do ; such calls. ; llvm.org/PR44815 -; STATS: 1 scalar-evolution - Number of loops with trip counts computed by force -; STATS: 2 scalar-evolution - Number of loops with predictable loop counts - ; In this test, adding -verify-indvars causes %tmp13 to not be optimized away. -; IR-LABEL: @foo -; IR-NOT: phi i32 +; CHECK-LABEL: @foo +; CHECK-NOT: phi i32 target triple = "x86_64-unknown-linux-gnu"