From 5a637cbdc7d8a59d5d56f329871cb3834c1f5095 Mon Sep 17 00:00:00 2001 From: Philip Reames Date: Tue, 9 Jul 2019 01:27:45 +0000 Subject: [PATCH] [LoopPred] Extend LFTR normalization to the inverse EQ case A while back, I added support for NE latches formed by LFTR. I didn't think that quite through, as LFTR will also produce the inverse EQ form for some loops and I hadn't handled that. This change just adds handling for that case as well. llvm-svn: 365419 --- .../lib/Transforms/Scalar/LoopPredication.cpp | 5 +++ llvm/test/Transforms/LoopPredication/basic.ll | 43 +++++++++++++++++++ 2 files changed, 48 insertions(+) diff --git a/llvm/lib/Transforms/Scalar/LoopPredication.cpp b/llvm/lib/Transforms/Scalar/LoopPredication.cpp index d3cec7568b6a..4ee3d0079c78 100644 --- a/llvm/lib/Transforms/Scalar/LoopPredication.cpp +++ b/llvm/lib/Transforms/Scalar/LoopPredication.cpp @@ -653,6 +653,11 @@ static void normalizePredicate(ScalarEvolution *SE, Loop *L, RC.IV->getStepRecurrence(*SE)->isOne() && SE->isKnownPredicate(ICmpInst::ICMP_ULE, RC.IV->getStart(), RC.Limit)) RC.Pred = ICmpInst::ICMP_ULT; + if (RC.Pred == ICmpInst::ICMP_EQ && + RC.IV->getStepRecurrence(*SE)->isOne() && + SE->isKnownPredicate(ICmpInst::ICMP_ULE, RC.IV->getStart(), RC.Limit)) + RC.Pred = ICmpInst::ICMP_UGE; + } diff --git a/llvm/test/Transforms/LoopPredication/basic.ll b/llvm/test/Transforms/LoopPredication/basic.ll index 88fa1bb95b2a..4a9cf6713193 100644 --- a/llvm/test/Transforms/LoopPredication/basic.ll +++ b/llvm/test/Transforms/LoopPredication/basic.ll @@ -1756,6 +1756,49 @@ exit: ret i32 0 } +; Same as previous, except swapped br/cmp +define i32 @eq_latch_dom_check_preinc(i32* %array, i32 %length, i32 %n) { +; CHECK-LABEL: @eq_latch_dom_check_preinc( +; CHECK-NEXT: entry: +; CHECK-NEXT: [[TMP5:%.*]] = icmp sle i32 [[N:%.*]], 0 +; CHECK-NEXT: br i1 [[TMP5]], label [[EXIT:%.*]], label [[LOOP_PREHEADER:%.*]] +; CHECK: loop.preheader: +; CHECK-NEXT: [[TMP0:%.*]] = add i32 [[LENGTH:%.*]], -1 +; CHECK-NEXT: [[TMP1:%.*]] = icmp ule i32 [[N]], [[TMP0]] +; CHECK-NEXT: [[TMP2:%.*]] = icmp ult i32 0, [[LENGTH]] +; CHECK-NEXT: [[TMP3:%.*]] = and i1 [[TMP2]], [[TMP1]] +; CHECK-NEXT: br label [[LOOP:%.*]] +; CHECK: loop: +; CHECK-NEXT: [[I:%.*]] = phi i32 [ [[I_NEXT:%.*]], [[LOOP]] ], [ 0, [[LOOP_PREHEADER]] ] +; CHECK-NEXT: call void (i1, ...) @llvm.experimental.guard(i1 [[TMP3]], i32 9) [ "deopt"() ] +; CHECK-NEXT: [[I_NEXT]] = add nuw i32 [[I]], 1 +; CHECK-NEXT: [[DONE:%.*]] = icmp eq i32 [[I]], [[N]] +; CHECK-NEXT: br i1 [[DONE]], label [[EXIT_LOOPEXIT:%.*]], label [[LOOP]] +; CHECK: exit.loopexit: +; CHECK-NEXT: br label [[EXIT]] +; CHECK: exit: +; CHECK-NEXT: ret i32 0 +; +entry: + %tmp5 = icmp sle i32 %n, 0 + br i1 %tmp5, label %exit, label %loop.preheader + +loop.preheader: + br label %loop + +loop: + %i = phi i32 [ %i.next, %loop ], [ 0, %loop.preheader ] + %within.bounds = icmp ult i32 %i, %length + call void (i1, ...) @llvm.experimental.guard(i1 %within.bounds, i32 9) [ "deopt"() ] + + %i.next = add nuw i32 %i, 1 + %done = icmp eq i32 %i, %n + br i1 %done, label %exit, label %loop + +exit: + ret i32 0 +} + ; NE latch - can't prove (end-start) mod step == 0 (i.e. might wrap ; around several times or even be infinite)