]> granicus.if.org Git - llvm/commitdiff
[LoopPred] Fix a bug in unconditional latch bailout introduced in r362284
authorPhilip Reames <listmail@philipreames.com>
Thu, 6 Jun 2019 18:02:36 +0000 (18:02 +0000)
committerPhilip Reames <listmail@philipreames.com>
Thu, 6 Jun 2019 18:02:36 +0000 (18:02 +0000)
This is a really silly bug that even a simple test w/an unconditional latch would have caught.  I tried to guard against the case, but put it in the wrong if check.  Oops.

git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@362727 91177308-0d34-0410-b5e6-96231b3b80d8

lib/Transforms/Scalar/LoopPredication.cpp
test/Transforms/LoopPredication/basic.ll

index 017bf21d233c89f36f66339dc300fc398bc71951..ed715d36984321d712495240931a70a9b6e66855 100644 (file)
@@ -850,7 +850,7 @@ Optional<LoopICmp> LoopPredication::parseLoopLatchICmp() {
   }
 
   auto *BI = dyn_cast<BranchInst>(LoopLatch->getTerminator());
-  if (!BI) {
+  if (!BI || !BI->isConditional()) {
     LLVM_DEBUG(dbgs() << "Failed to match the latch terminator!\n");
     return None;
   }
@@ -860,7 +860,7 @@ Optional<LoopICmp> LoopPredication::parseLoopLatchICmp() {
       "One of the latch's destinations must be the header");
 
   auto *ICI = dyn_cast<ICmpInst>(BI->getCondition());
-  if (!ICI || !BI->isConditional()) {
+  if (!ICI) {
     LLVM_DEBUG(dbgs() << "Failed to match the latch condition!\n");
     return None;
   }
index 6f294b5b67ee54dba8e2598e8772307d5412575f..88fa1bb95b2aae85da276743b808393b8564a82f 100644 (file)
@@ -1853,3 +1853,29 @@ exit:
   ret i32 0
 }
 
+; Negative test, make sure we don't crash on unconditional latches
+; TODO: there's no reason we shouldn't be able to predicate the
+; condition for an statically infinite loop.
+define i32 @unconditional_latch(i32* %a, i32 %length) {
+; CHECK-LABEL: @unconditional_latch(
+; CHECK-NEXT:  loop.preheader:
+; CHECK-NEXT:    br label [[LOOP:%.*]]
+; CHECK:       loop:
+; CHECK-NEXT:    [[I:%.*]] = phi i32 [ [[I_NEXT:%.*]], [[LOOP]] ], [ 400, [[LOOP_PREHEADER:%.*]] ]
+; CHECK-NEXT:    [[WITHIN_BOUNDS:%.*]] = icmp ult i32 [[I]], [[LENGTH:%.*]]
+; CHECK-NEXT:    call void (i1, ...) @llvm.experimental.guard(i1 [[WITHIN_BOUNDS]], i32 9) [ "deopt"() ]
+; CHECK-NEXT:    store volatile i32 0, i32* [[A:%.*]]
+; CHECK-NEXT:    [[I_NEXT]] = add i32 [[I]], 1
+; CHECK-NEXT:    br label [[LOOP]]
+;
+loop.preheader:
+  br label %loop
+
+loop:
+  %i = phi i32 [ %i.next, %loop ], [ 400, %loop.preheader ]
+  %within.bounds = icmp ult i32 %i, %length
+  call void (i1, ...) @llvm.experimental.guard(i1 %within.bounds, i32 9) [ "deopt"() ]
+  store volatile i32 0, i32* %a
+  %i.next = add i32 %i, 1
+  br label %loop
+}