[PATCH] D121925: [LoopSimplifyCFG] Check predecessors of exits before marking them dead.

Florian Hahn via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Fri Mar 18 01:55:21 PDT 2022


This revision was automatically updated to reflect the committed changes.
Closed by commit rG4a699ae9c6a6: [LoopSimplifyCFG] Check predecessors of exits before marking them dead. (authored by fhahn).

Changed prior to commit:
  https://reviews.llvm.org/D121925?vs=416223&id=416427#toc

Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D121925/new/

https://reviews.llvm.org/D121925

Files:
  llvm/lib/Transforms/Scalar/LoopSimplifyCFG.cpp
  llvm/test/Transforms/LoopSimplifyCFG/loop-not-in-simplify-form.ll


Index: llvm/test/Transforms/LoopSimplifyCFG/loop-not-in-simplify-form.ll
===================================================================
--- llvm/test/Transforms/LoopSimplifyCFG/loop-not-in-simplify-form.ll
+++ llvm/test/Transforms/LoopSimplifyCFG/loop-not-in-simplify-form.ll
@@ -4,14 +4,9 @@
 ; Test case from PR54023. After SimpleLoopUnswitch, one of the loops processed
 ; by LoopSimplifyCFG will have exit blocks with predecessors outside the loop
 ; (i.e. it is not in loop-simplify/canonical form).
-; FIXME: currently %res gets incorrectly replaced with undef.
 define i32 @test(i32 %v, i1 %c.1, i1 %c.2) {
 ; CHECK-LABEL: @test(
 ; CHECK-NEXT:  entry:
-; CHECK-NEXT:    switch i32 0, label [[ENTRY_SPLIT:%.*]] [
-; CHECK-NEXT:    i32 1, label [[EXIT_SPLIT:%.*]]
-; CHECK-NEXT:    ]
-; CHECK:       entry.split:
 ; CHECK-NEXT:    br label [[OUTER_HEADER:%.*]]
 ; CHECK:       outer.header:
 ; CHECK-NEXT:    br i1 [[C_1:%.*]], label [[OUTER_LATCH:%.*]], label [[INNER_HEADER_PREHEADER:%.*]]
@@ -28,9 +23,10 @@
 ; CHECK-NEXT:    br label [[OUTER_HEADER]]
 ; CHECK:       exit:
 ; CHECK-NEXT:    [[RES:%.*]] = phi i32 [ [[V]], [[INNER_HEADER]] ]
-; CHECK-NEXT:    br label [[EXIT_SPLIT]]
+; CHECK-NEXT:    br label [[EXIT_SPLIT:%.*]]
 ; CHECK:       exit.split:
-; CHECK-NEXT:    ret i32 undef
+; CHECK-NEXT:    [[RES_SPLIT:%.*]] = phi i32 [ [[RES]], [[EXIT]] ]
+; CHECK-NEXT:    ret i32 [[RES_SPLIT]]
 ;
 entry:
   br label %outer.header
Index: llvm/lib/Transforms/Scalar/LoopSimplifyCFG.cpp
===================================================================
--- llvm/lib/Transforms/Scalar/LoopSimplifyCFG.cpp
+++ llvm/lib/Transforms/Scalar/LoopSimplifyCFG.cpp
@@ -254,13 +254,17 @@
     assert(L.getNumBlocks() == LiveLoopBlocks.size() + DeadLoopBlocks.size() &&
            "Malformed block sets?");
 
-    // Now, all exit blocks that are not marked as live are dead.
+    // Now, all exit blocks that are not marked as live are dead, if all their
+    // predecessors are in the loop. This may not be the case, as the input loop
+    // may not by in loop-simplify/canonical form.
     SmallVector<BasicBlock *, 8> ExitBlocks;
     L.getExitBlocks(ExitBlocks);
     SmallPtrSet<BasicBlock *, 8> UniqueDeadExits;
     for (auto *ExitBlock : ExitBlocks)
       if (!LiveExitBlocks.count(ExitBlock) &&
-          UniqueDeadExits.insert(ExitBlock).second)
+          UniqueDeadExits.insert(ExitBlock).second &&
+          all_of(predecessors(ExitBlock),
+                 [this](BasicBlock *Pred) { return L.contains(Pred); }))
         DeadExitBlocks.push_back(ExitBlock);
 
     // Whether or not the edge From->To will still be present in graph after the


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D121925.416427.patch
Type: text/x-patch
Size: 2670 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20220318/88fa6126/attachment.bin>


More information about the llvm-commits mailing list