[llvm] 523573e - [LoopDeletion] Revert 3af8a11 and add test coverage for breakage

Philip Reames via llvm-commits llvm-commits at lists.llvm.org
Mon Jan 17 11:44:27 PST 2022


Author: Philip Reames
Date: 2022-01-17T11:44:03-08:00
New Revision: 523573e90ddb6db3b58d7a04b7dbe90fa7366dc2

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

LOG: [LoopDeletion] Revert 3af8a11 and add test coverage for breakage

This reverts 3af8a11 because I'd used an upper bound where an lower bound was required.  The included reduced test case demonstrates the issue.

Added: 
    llvm/test/Transforms/LoopDeletion/D108848-regression.ll

Modified: 
    llvm/lib/Transforms/Scalar/LoopDeletion.cpp

Removed: 
    


################################################################################
diff  --git a/llvm/lib/Transforms/Scalar/LoopDeletion.cpp b/llvm/lib/Transforms/Scalar/LoopDeletion.cpp
index 5814e2f043d50..d70ff60a1aacb 100644
--- a/llvm/lib/Transforms/Scalar/LoopDeletion.cpp
+++ b/llvm/lib/Transforms/Scalar/LoopDeletion.cpp
@@ -407,25 +407,18 @@ breakBackedgeIfNotTaken(Loop *L, DominatorTree &DT, ScalarEvolution &SE,
   if (!L->getLoopLatch())
     return LoopDeletionResult::Unmodified;
 
-  auto *BTC = SE.getSymbolicMaxBackedgeTakenCount(L);
-  if (BTC->isZero()) {
-    // SCEV knows this backedge isn't taken!
-    breakLoopBackedge(L, DT, SE, LI, MSSA);
-    ++NumBackedgesBroken;
-    return LoopDeletionResult::Deleted;
-  }
-
-  // If SCEV leaves open the possibility of a zero trip count, see if
-  // symbolically evaluating the first iteration lets us prove the backedge
-  // unreachable.
-  if (isa<SCEVCouldNotCompute>(BTC) || !SE.isKnownNonZero(BTC))
-    if (canProveExitOnFirstIteration(L, DT, LI)) {
-      breakLoopBackedge(L, DT, SE, LI, MSSA);
-      ++NumBackedgesBroken;
-      return LoopDeletionResult::Deleted;
+  auto *BTCMax = SE.getConstantMaxBackedgeTakenCount(L);
+  if (!BTCMax->isZero()) {
+    auto *BTC = SE.getBackedgeTakenCount(L);
+    if (!BTC->isZero()) {
+      if (!isa<SCEVCouldNotCompute>(BTC) && SE.isKnownNonZero(BTC))
+        return LoopDeletionResult::Unmodified;
+      if (!canProveExitOnFirstIteration(L, DT, LI))
+        return LoopDeletionResult::Unmodified;
     }
-
-  return LoopDeletionResult::Unmodified;
+  }
+  breakLoopBackedge(L, DT, SE, LI, MSSA);
+  return LoopDeletionResult::Deleted;
 }
 
 /// Remove a loop if it is dead.

diff  --git a/llvm/test/Transforms/LoopDeletion/D108848-regression.ll b/llvm/test/Transforms/LoopDeletion/D108848-regression.ll
new file mode 100644
index 0000000000000..68cb32aaa2845
--- /dev/null
+++ b/llvm/test/Transforms/LoopDeletion/D108848-regression.ll
@@ -0,0 +1,47 @@
+; NOTE: Assertions have been autogenerated by utils/update_test_checks.py
+; RUN: opt -loop-deletion -S < %s | FileCheck %s
+
+ at a = global i1 false, align 4
+
+define i32 @main() {
+; CHECK-LABEL: @main(
+; CHECK-NEXT:  entry:
+; CHECK-NEXT:    br label [[FOR_BODY:%.*]]
+; CHECK:       for.body:
+; CHECK-NEXT:    [[D_015:%.*]] = phi i32 [ 0, [[ENTRY:%.*]] ]
+; CHECK-NEXT:    call void @foo()
+; CHECK-NEXT:    [[DOTB:%.*]] = load i1, i1* @a, align 4
+; CHECK-NEXT:    [[CONV1:%.*]] = select i1 [[DOTB]], i32 -128, i32 1
+; CHECK-NEXT:    [[CMP2_NOT:%.*]] = icmp ugt i32 [[CONV1]], [[D_015]]
+; CHECK-NEXT:    br i1 [[CMP2_NOT]], label [[CLEANUP:%.*]], label [[FOR_INC8:%.*]]
+; CHECK:       for.inc8:
+; CHECK-NEXT:    [[INC9:%.*]] = add nuw nsw i32 [[D_015]], 1
+; CHECK-NEXT:    [[EXITCOND_NOT:%.*]] = icmp eq i32 [[INC9]], 19
+; CHECK-NEXT:    br label [[CLEANUP]]
+; CHECK:       cleanup:
+; CHECK-NEXT:    store i1 true, i1* @a, align 4
+; CHECK-NEXT:    ret i32 0
+;
+entry:
+  br label %for.body
+
+for.body:
+  %d.015 = phi i32 [ 0, %entry ], [ %inc9, %for.inc8 ]
+  call void @foo()
+  %.b = load i1, i1* @a, align 4
+  %conv1 = select i1 %.b, i32 -128, i32 1
+  %cmp2.not = icmp ugt i32 %conv1, %d.015
+  br i1 %cmp2.not, label %cleanup, label %for.inc8
+
+for.inc8:
+  %inc9 = add nuw nsw i32 %d.015, 1
+  %exitcond.not = icmp eq i32 %inc9, 19
+  br i1 %exitcond.not, label %cleanup, label %for.body
+
+cleanup:
+  store i1 true, i1* @a, align 4
+  ret i32 0
+}
+
+declare void @foo()
+


        


More information about the llvm-commits mailing list