[llvm] aeab616 - [SCEV] Only verify BECounts for reachable loops (PR50523)

Nikita Popov via llvm-commits llvm-commits at lists.llvm.org
Tue Mar 1 02:52:43 PST 2022


Author: Nikita Popov
Date: 2022-03-01T11:52:35+01:00
New Revision: aeab6167b0a13ab3a46798c0a9929458e0413f08

URL: https://github.com/llvm/llvm-project/commit/aeab6167b0a13ab3a46798c0a9929458e0413f08
DIFF: https://github.com/llvm/llvm-project/commit/aeab6167b0a13ab3a46798c0a9929458e0413f08.diff

LOG: [SCEV] Only verify BECounts for reachable loops (PR50523)

For unreachable loops, any BECount is legal, and since D98706 SCEV
can make use of this for loops that are unreachable due to constant
branches. To avoid false positives, adjust SCEV verification to only
check BECounts in reachable loops.

Fixes https://github.com/llvm/llvm-project/issues/50523.

Differential Revision: https://reviews.llvm.org/D120651

Added: 
    

Modified: 
    llvm/lib/Analysis/ScalarEvolution.cpp
    llvm/test/Transforms/IndVarSimplify/X86/pr35406.ll

Removed: 
    


################################################################################
diff  --git a/llvm/lib/Analysis/ScalarEvolution.cpp b/llvm/lib/Analysis/ScalarEvolution.cpp
index aa7f124ce4d30..f446f9101d6ee 100644
--- a/llvm/lib/Analysis/ScalarEvolution.cpp
+++ b/llvm/lib/Analysis/ScalarEvolution.cpp
@@ -13410,6 +13410,25 @@ ScalarEvolution::getUsedLoops(const SCEV *S,
   SCEVTraversal<FindUsedLoops>(F).visitAll(S);
 }
 
+static void getReachableBlocks(SmallPtrSetImpl<BasicBlock *> &Reachable,
+                               Function &F) {
+  SmallVector<BasicBlock *> 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<ScalarEvolution *>(this);
   ScalarEvolution SE2(F, TLI, AC, DT, LI);
@@ -13434,11 +13453,18 @@ void ScalarEvolution::verify() const {
   };
 
   SCEVMapper SCM(SE2);
+  SmallPtrSet<BasicBlock *, 16> 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<ScalarEvolution *>(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
index cd5615a1bc67f..45555323d7617 100644
--- 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<scalar-evolution>' %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"
 


        


More information about the llvm-commits mailing list