diff --git a/llvm/include/llvm/Analysis/ScalarEvolution.h b/llvm/include/llvm/Analysis/ScalarEvolution.h --- a/llvm/include/llvm/Analysis/ScalarEvolution.h +++ b/llvm/include/llvm/Analysis/ScalarEvolution.h @@ -67,6 +67,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. /// 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 @@ -142,6 +142,9 @@ STATISTIC(NumBruteForceTripCountsComputed, "Number of loops with trip counts computed by force"); +// 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, @@ -150,9 +153,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, @@ -13359,8 +13361,14 @@ if (!ReachableBlocks.contains(L->getHeader())) continue; - 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() || diff --git a/llvm/lib/Transforms/Scalar/LoopPassManager.cpp b/llvm/lib/Transforms/Scalar/LoopPassManager.cpp --- a/llvm/lib/Transforms/Scalar/LoopPassManager.cpp +++ b/llvm/lib/Transforms/Scalar/LoopPassManager.cpp @@ -309,12 +309,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 diff --git a/llvm/test/Transforms/IndVarSimplify/X86/deterministic-scev-verify.ll b/llvm/test/Transforms/IndVarSimplify/X86/deterministic-scev-verify.ll --- a/llvm/test/Transforms/IndVarSimplify/X86/deterministic-scev-verify.ll +++ b/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" 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 -passes='loop(indvars),verify' %s | FileCheck %s +; RUN: opt -S -indvars -verify-scev %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"