[PATCH] D103340: [LoopUnroll] Use changeToUnreachable()

Nikita Popov via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Fri May 28 13:54:20 PDT 2021


nikic created this revision.
nikic added a reviewer: reames.
Herald added subscribers: zzheng, hiraditya.
nikic requested review of this revision.
Herald added a project: LLVM.
Herald added a subscriber: llvm-commits.

When fulling unrolling with a non-latch exit, the latch block is folded to unreachable. Replace this folding with the existing changeToUnreachable() helper, rather than performing it manually.

This is not NFC because we now also drop all the instructions from the unreachable latch block. Previously we would only convert the terminator, and (later) DCE anything that is trivially dead.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D103340

Files:
  llvm/lib/Transforms/Utils/LoopUnroll.cpp
  llvm/test/Transforms/LoopUnroll/unroll-unconditional-latch.ll


Index: llvm/test/Transforms/LoopUnroll/unroll-unconditional-latch.ll
===================================================================
--- llvm/test/Transforms/LoopUnroll/unroll-unconditional-latch.ll
+++ llvm/test/Transforms/LoopUnroll/unroll-unconditional-latch.ll
@@ -28,7 +28,6 @@
 ; CHECK-NEXT:    store i32 0, i32* [[PTR_3]], align 4
 ; CHECK-NEXT:    br i1 true, label [[IF_END_LOOPEXIT:%.*]], label [[FOR_LATCH_3:%.*]]
 ; CHECK:       for.latch.3:
-; CHECK-NEXT:    store volatile i64 3, i64* [[OUT]], align 4
 ; CHECK-NEXT:    unreachable
 ;
 
Index: llvm/lib/Transforms/Utils/LoopUnroll.cpp
===================================================================
--- llvm/lib/Transforms/Utils/LoopUnroll.cpp
+++ llvm/lib/Transforms/Utils/LoopUnroll.cpp
@@ -763,13 +763,6 @@
     }
   }
 
-  // When completely unrolling, the last latch becomes unreachable.
-  if (!LatchIsExiting && CompletelyUnroll) {
-    BranchInst *Term = cast<BranchInst>(Latches.back()->getTerminator());
-    new UnreachableInst(Term->getContext(), Term);
-    Term->eraseFromParent();
-  }
-
   // Update dominators of blocks we might reach through exits.
   // Immediate dominator of such block might change, because we add more
   // routes which can lead to the exit: we can now reach it from the copied
@@ -819,6 +812,12 @@
          DT->verify(DominatorTree::VerificationLevel::Fast));
 
   DomTreeUpdater DTU(DT, DomTreeUpdater::UpdateStrategy::Lazy);
+
+  // When completely unrolling, the last latch becomes unreachable.
+  if (!LatchIsExiting && CompletelyUnroll)
+    changeToUnreachable(&Latches.back()->front(), /* UseTrap */ false,
+                        PreserveLCSSA, &DTU);
+
   // Merge adjacent basic blocks, if possible.
   for (BasicBlock *Latch : Latches) {
     BranchInst *Term = dyn_cast<BranchInst>(Latch->getTerminator());


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D103340.348580.patch
Type: text/x-patch
Size: 1834 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20210528/677d6663/attachment.bin>


More information about the llvm-commits mailing list