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

Florian Hahn via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Thu Mar 17 10:04:41 PDT 2022


fhahn created this revision.
fhahn added reviewers: nikic, reames, asbirlea, lebedev.ri.
Herald added a subscriber: hiraditya.
Herald added a project: All.
fhahn requested review of this revision.
Herald added a project: LLVM.

LoopSimplifyCFG may process loops that are not in
loop-simplify/canonical form. For loops not in canonical form, exit
blocks may be reachable from non-loop blocks and we cannot consider them
as dead if they only are not reachable from the loop itself.

Unfortunately the smallest test I could come up with requires running
multiple passes:

  -passes='loop-mssa(loop-instsimplify,loop-simplifycfg,simple-loop-unswitch)'

The reason is that loops are canonicalized at the beginning of loop
pipelines, so a later transform has to break canonical form in a way
that breaks LoopSimplifyCFG's dead-exit analysis.

Alternatively we could try to require all loop passes to maintain
canonical form. That in turn would also require additional verification.

Fixes #54023.


Repository:
  rG LLVM Github Monorepo

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
@@ -8,10 +8,6 @@
 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 +24,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.416223.patch
Type: text/x-patch
Size: 2395 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20220317/cc121dbd/attachment.bin>


More information about the llvm-commits mailing list