[PATCH] D46045: [LoopUnswitch] Fix SCEV invalidation in unswitching

Max Kazantsev via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Wed May 2 20:54:23 PDT 2018


mkazantsev updated this revision to Diff 144978.
mkazantsev retitled this revision from "[LoopUnswitch] Fix potentially incorrect SCEV invalidation in unswitching" to "[LoopUnswitch] Fix SCEV invalidation in unswitching".
mkazantsev edited the summary of this revision.
mkazantsev added a reviewer: uabelho.
mkazantsev added a comment.

This one appeared to be a real bug, at least we have a test that fails with assert for trivial unswitching. Thanks @uabelho for providing the test case.


https://reviews.llvm.org/D46045

Files:
  lib/Transforms/Scalar/LoopUnswitch.cpp
  test/Transforms/LoopUnswitch/invalidate-scev.ll


Index: test/Transforms/LoopUnswitch/invalidate-scev.ll
===================================================================
--- test/Transforms/LoopUnswitch/invalidate-scev.ll
+++ test/Transforms/LoopUnswitch/invalidate-scev.ll
@@ -0,0 +1,33 @@
+; RUN: opt -S -indvars -loop-unswitch < %s | FileCheck %s
+
+target triple = "x86_64-unknown-linux-gnu"
+
+define void @test_01() {
+
+; Make sure we don't fail by SCEV's assertion due to incorrect invalidation.
+; CHECK-LABEL: @test_01
+
+entry:
+  br label %loop
+
+loop:                           ; preds = %backedge, %entry
+  %p_50.addr.0 = phi i16 [ undef, %entry ], [ %add2699, %backedge ]
+  %idxprom2690 = sext i16 %p_50.addr.0 to i32
+  %arrayidx2691 = getelementptr inbounds [5 x i32], [5 x i32]* undef, i32 0, i32 %idxprom2690
+  %0 = load i32, i32* %arrayidx2691, align 1
+  %tobool2692 = icmp ne i32 %0, 0
+  br label %inner_loop
+
+inner_loop:                                     ; preds = %inner_backedge, %loop
+  br i1 %tobool2692, label %backedge, label %inner_backedge
+
+inner_backedge:                                       ; preds = %inner_loop
+  br label %inner_loop
+
+backedge:                                      ; preds = %inner_loop
+  %add2699 = add nsw i16 %p_50.addr.0, 1
+  br i1 false, label %loop, label %exit
+
+exit:               ; preds = %backedge
+  unreachable
+}
Index: lib/Transforms/Scalar/LoopUnswitch.cpp
===================================================================
--- lib/Transforms/Scalar/LoopUnswitch.cpp
+++ lib/Transforms/Scalar/LoopUnswitch.cpp
@@ -975,6 +975,10 @@
                << " blocks] in Function "
                << L->getHeader()->getParent()->getName() << " on cond: " << *Val
                << " == " << *Cond << "\n");
+  // We are going to make essential changes to CFG. This may invalidate cached
+  // information for L or one of its parent loops in SCEV.
+  if (auto *SEWP = getAnalysisIfAvailable<ScalarEvolutionWrapperPass>())
+    SEWP->getSE().forgetTopmostLoop(L);
 
   // First step, split the preheader, so that we know that there is a safe place
   // to insert the conditional branch.  We will change loopPreheader to have a
@@ -1201,8 +1205,10 @@
         << " blocks] in Function " << F->getName()
         << " when '" << *Val << "' == " << *LIC << "\n");
 
+  // We are going to make essential changes to CFG. This may invalidate cached
+  // information for L or one of its parent loops in SCEV.
   if (auto *SEWP = getAnalysisIfAvailable<ScalarEvolutionWrapperPass>())
-    SEWP->getSE().forgetLoop(L);
+    SEWP->getSE().forgetTopmostLoop(L);
 
   LoopBlocks.clear();
   NewBlocks.clear();


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D46045.144978.patch
Type: text/x-patch
Size: 2635 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20180503/6fd34bde/attachment.bin>


More information about the llvm-commits mailing list