From 2a079134ab741a2b2a2f713432ff3b44ecc474a5 Mon Sep 17 00:00:00 2001 From: Artur Pilipenko Date: Thu, 12 Oct 2017 21:21:17 +0000 Subject: [PATCH] [LoopPredication] Check whether the loop is already guarded by the first iteration check condition git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@315623 91177308-0d34-0410-b5e6-96231b3b80d8 --- lib/Transforms/Scalar/LoopPredication.cpp | 8 +- test/Transforms/LoopPredication/basic.ll | 92 +++++++++++++++------- test/Transforms/LoopPredication/nested.ll | 10 +-- test/Transforms/LoopPredication/visited.ll | 4 +- 4 files changed, 77 insertions(+), 37 deletions(-) diff --git a/lib/Transforms/Scalar/LoopPredication.cpp b/lib/Transforms/Scalar/LoopPredication.cpp index bb22a50e604..393c6049af1 100644 --- a/lib/Transforms/Scalar/LoopPredication.cpp +++ b/lib/Transforms/Scalar/LoopPredication.cpp @@ -289,6 +289,10 @@ Value *LoopPredication::expandCheck(SCEVExpander &Expander, Type *Ty = LHS->getType(); assert(Ty == RHS->getType() && "expandCheck operands have different types?"); + + if (SE->isLoopEntryGuardedByCond(L, Pred, LHS, RHS)) + return Builder.getTrue(); + Value *LHSV = Expander.expandCodeFor(LHS, Ty, InsertAt); Value *RHSV = Expander.expandCodeFor(RHS, Ty, InsertAt); return Builder.CreateICmp(Pred, LHSV, RHSV); @@ -358,10 +362,10 @@ Optional LoopPredication::widenICmpRangeCheck(ICmpInst *ICI, return None; Instruction *InsertAt = Preheader->getTerminator(); - auto *FirstIterationCheck = expandCheck(Expander, Builder, RangeCheck->Pred, - Start, RangeCheck->Limit, InsertAt); auto *LimitCheck = expandCheck(Expander, Builder, LimitCheckPred, LatchCheck.Limit, RangeCheck->Limit, InsertAt); + auto *FirstIterationCheck = expandCheck(Expander, Builder, RangeCheck->Pred, + Start, RangeCheck->Limit, InsertAt); return Builder.CreateAnd(FirstIterationCheck, LimitCheck); } diff --git a/test/Transforms/LoopPredication/basic.ll b/test/Transforms/LoopPredication/basic.ll index dd0f4f94d42..e6c31d1ff52 100644 --- a/test/Transforms/LoopPredication/basic.ll +++ b/test/Transforms/LoopPredication/basic.ll @@ -11,8 +11,8 @@ entry: loop.preheader: ; CHECK: loop.preheader: -; CHECK: [[first_iteration_check:[^ ]+]] = icmp ult i32 0, %length -; CHECK-NEXT: [[limit_check:[^ ]+]] = icmp ule i32 %n, %length +; CHECK: [[limit_check:[^ ]+]] = icmp ule i32 %n, %length +; CHECK-NEXT: [[first_iteration_check:[^ ]+]] = icmp ult i32 0, %length ; CHECK-NEXT: [[wide_cond:[^ ]+]] = and i1 [[first_iteration_check]], [[limit_check]] ; CHECK-NEXT: br label %loop br label %loop @@ -47,8 +47,8 @@ entry: loop.preheader: ; CHECK: loop.preheader: -; CHECK: [[first_iteration_check:[^ ]+]] = icmp ult i32 0, %length -; CHECK-NEXT: [[limit_check:[^ ]+]] = icmp ult i32 %n, %length +; CHECK: [[limit_check:[^ ]+]] = icmp ult i32 %n, %length +; CHECK-NEXT: [[first_iteration_check:[^ ]+]] = icmp ult i32 0, %length ; CHECK-NEXT: [[wide_cond:[^ ]+]] = and i1 [[first_iteration_check]], [[limit_check]] ; CHECK-NEXT: br label %loop br label %loop @@ -83,8 +83,8 @@ entry: loop.preheader: ; CHECK: loop.preheader: -; CHECK: [[first_iteration_check:[^ ]+]] = icmp ult i32 0, %length -; CHECK-NEXT: [[limit_check:[^ ]+]] = icmp ule i32 %n, %length +; CHECK: [[limit_check:[^ ]+]] = icmp ule i32 %n, %length +; CHECK-NEXT: [[first_iteration_check:[^ ]+]] = icmp ult i32 0, %length ; CHECK-NEXT: [[wide_cond:[^ ]+]] = and i1 [[first_iteration_check]], [[limit_check]] ; CHECK-NEXT: br label %loop br label %loop @@ -119,8 +119,8 @@ entry: loop.preheader: ; CHECK: loop.preheader: -; CHECK: [[first_iteration_check:[^ ]+]] = icmp ult i32 0, %length -; CHECK-NEXT: [[limit_check:[^ ]+]] = icmp sle i32 %n, %length +; CHECK: [[limit_check:[^ ]+]] = icmp sle i32 %n, %length +; CHECK-NEXT: [[first_iteration_check:[^ ]+]] = icmp ult i32 0, %length ; CHECK-NEXT: [[wide_cond:[^ ]+]] = and i1 [[first_iteration_check]], [[limit_check]] ; CHECK-NEXT: br label %loop br label %loop @@ -147,6 +147,42 @@ exit: ret i32 %result } +define i32 @signed_loop_0_to_n_ult_check_length_range_known(i32* %array, i32* %length.ptr, i32 %n) { +; CHECK-LABEL: @signed_loop_0_to_n_ult_check_length_range_known +entry: + %tmp5 = icmp sle i32 %n, 0 + %length = load i32, i32* %length.ptr, !range !{i32 1, i32 2147483648} + br i1 %tmp5, label %exit, label %loop.preheader + +loop.preheader: +; CHECK: loop.preheader: +; CHECK: [[limit_check:[^ ]+]] = icmp sle i32 %n, %length +; CHECK-NEXT: [[wide_cond:[^ ]+]] = and i1 true, [[limit_check]] +; CHECK-NEXT: br label %loop + br label %loop + +loop: +; CHECK: loop: +; CHECK: call void (i1, ...) @llvm.experimental.guard(i1 [[wide_cond]], i32 9) [ "deopt"() ] + %loop.acc = phi i32 [ %loop.acc.next, %loop ], [ 0, %loop.preheader ] + %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.i64 = zext i32 %i to i64 + %array.i.ptr = getelementptr inbounds i32, i32* %array, i64 %i.i64 + %array.i = load i32, i32* %array.i.ptr, align 4 + %loop.acc.next = add i32 %loop.acc, %array.i + + %i.next = add nuw i32 %i, 1 + %continue = icmp slt i32 %i.next, %n + br i1 %continue, label %loop, label %exit + +exit: + %result = phi i32 [ 0, %entry ], [ %loop.acc.next, %loop ] + ret i32 %result +} + define i32 @signed_loop_0_to_n_inverse_latch_predicate(i32* %array, i32 %length, i32 %n) { ; CHECK-LABEL: @signed_loop_0_to_n_inverse_latch_predicate entry: @@ -155,8 +191,8 @@ entry: loop.preheader: ; CHECK: loop.preheader: -; CHECK: [[first_iteration_check:[^ ]+]] = icmp ult i32 0, %length -; CHECK-NEXT: [[limit_check:[^ ]+]] = icmp slt i32 %n, %length +; CHECK: [[limit_check:[^ ]+]] = icmp slt i32 %n, %length +; CHECK-NEXT: [[first_iteration_check:[^ ]+]] = icmp ult i32 0, %length ; CHECK-NEXT: [[wide_cond:[^ ]+]] = and i1 [[first_iteration_check]], [[limit_check]] ; CHECK-NEXT: br label %loop br label %loop @@ -191,8 +227,8 @@ entry: loop.preheader: ; CHECK: loop.preheader: -; CHECK: [[first_iteration_check:[^ ]+]] = icmp ult i32 0, %length -; CHECK-NEXT: [[limit_check:[^ ]+]] = icmp slt i32 %n, %length +; CHECK: [[limit_check:[^ ]+]] = icmp slt i32 %n, %length +; CHECK-NEXT: [[first_iteration_check:[^ ]+]] = icmp ult i32 0, %length ; CHECK-NEXT: [[wide_cond:[^ ]+]] = and i1 [[first_iteration_check]], [[limit_check]] ; CHECK-NEXT: br label %loop br label %loop @@ -295,8 +331,8 @@ entry: loop.preheader: ; CHECK: loop.preheader: -; CHECK: [[first_iteration_check:[^ ]+]] = icmp ult i32 0, %length -; CHECK-NEXT: [[limit_check:[^ ]+]] = icmp sle i32 %n, %length +; CHECK: [[limit_check:[^ ]+]] = icmp sle i32 %n, %length +; CHECK-NEXT: [[first_iteration_check:[^ ]+]] = icmp ult i32 0, %length ; CHECK-NEXT: [[wide_cond:[^ ]+]] = and i1 [[first_iteration_check]], [[limit_check]] ; CHECK-NEXT: br label %loop br label %loop @@ -372,11 +408,11 @@ entry: loop.preheader: ; CHECK: loop.preheader: -; CHECK: [[first_iteration_check_1:[^ ]+]] = icmp ult i32 0, %length.{{1|2}} -; CHECK-NEXT: [[limit_check_1:[^ ]+]] = icmp ule i32 %n, %length.{{1|2}} +; CHECK: [[limit_check_1:[^ ]+]] = icmp ule i32 %n, %length.{{1|2}} +; CHECK-NEXT: [[first_iteration_check_1:[^ ]+]] = icmp ult i32 0, %length.{{1|2}} ; CHECK-NEXT: [[wide_cond_1:[^ ]+]] = and i1 [[first_iteration_check_1]], [[limit_check_1]] -; CHECK-NEXT: [[first_iteration_check_2:[^ ]+]] = icmp ult i32 0, %length.{{1|2}} ; CHECK-NEXT: [[limit_check_2:[^ ]+]] = icmp ule i32 %n, %length.{{1|2}} +; CHECK-NEXT: [[first_iteration_check_2:[^ ]+]] = icmp ult i32 0, %length.{{1|2}} ; CHECK-NEXT: [[wide_cond_2:[^ ]+]] = and i1 [[first_iteration_check_2]], [[limit_check_2]] ; CHECK-NEXT: br label %loop br label %loop @@ -420,14 +456,14 @@ entry: loop.preheader: ; CHECK: loop.preheader: -; CHECK: [[first_iteration_check_1:[^ ]+]] = icmp ult i32 0, %length.{{1|2|3}} -; CHECK-NEXT: [[limit_check_1:[^ ]+]] = icmp ule i32 %n, %length.{{1|2|3}} +; CHECK: [[limit_check_1:[^ ]+]] = icmp ule i32 %n, %length.{{1|2|3}} +; CHECK-NEXT: [[first_iteration_check_1:[^ ]+]] = icmp ult i32 0, %length.{{1|2|3}} ; CHECK-NEXT: [[wide_cond_1:[^ ]+]] = and i1 [[first_iteration_check_1]], [[limit_check_1]] -; CHECK-NEXT: [[first_iteration_check_2:[^ ]+]] = icmp ult i32 0, %length.{{1|2|3}} ; CHECK-NEXT: [[limit_check_2:[^ ]+]] = icmp ule i32 %n, %length.{{1|2|3}} +; CHECK-NEXT: [[first_iteration_check_2:[^ ]+]] = icmp ult i32 0, %length.{{1|2|3}} ; CHECK-NEXT: [[wide_cond_2:[^ ]+]] = and i1 [[first_iteration_check_2]], [[limit_check_2]] -; CHECK-NEXT: [[first_iteration_check_3:[^ ]+]] = icmp ult i32 0, %length.{{1|2|3}} ; CHECK-NEXT: [[limit_check_3:[^ ]+]] = icmp ule i32 %n, %length.{{1|2|3}} +; CHECK-NEXT: [[first_iteration_check_3:[^ ]+]] = icmp ult i32 0, %length.{{1|2|3}} ; CHECK-NEXT: [[wide_cond_3:[^ ]+]] = and i1 [[first_iteration_check_3]], [[limit_check_3]] ; CHECK-NEXT: br label %loop br label %loop @@ -478,14 +514,14 @@ entry: loop.preheader: ; CHECK: loop.preheader: -; CHECK: [[first_iteration_check_1:[^ ]+]] = icmp ult i32 0, %length.{{1|2|3}} -; CHECK-NEXT: [[limit_check_1:[^ ]+]] = icmp ule i32 %n, %length.{{1|2|3}} +; CHECK: [[limit_check_1:[^ ]+]] = icmp ule i32 %n, %length.{{1|2|3}} +; CHECK-NEXT: [[first_iteration_check_1:[^ ]+]] = icmp ult i32 0, %length.{{1|2|3}} ; CHECK-NEXT: [[wide_cond_1:[^ ]+]] = and i1 [[first_iteration_check_1]], [[limit_check_1]] -; CHECK-NEXT: [[first_iteration_check_2:[^ ]+]] = icmp ult i32 0, %length.{{1|2|3}} ; CHECK-NEXT: [[limit_check_2:[^ ]+]] = icmp ule i32 %n, %length.{{1|2|3}} +; CHECK-NEXT: [[first_iteration_check_2:[^ ]+]] = icmp ult i32 0, %length.{{1|2|3}} ; CHECK-NEXT: [[wide_cond_2:[^ ]+]] = and i1 [[first_iteration_check_2]], [[limit_check_2]] -; CHECK-NEXT: [[first_iteration_check_3:[^ ]+]] = icmp ult i32 0, %length.{{1|2|3}} ; CHECK-NEXT: [[limit_check_3:[^ ]+]] = icmp ule i32 %n, %length.{{1|2|3}} +; CHECK-NEXT: [[first_iteration_check_3:[^ ]+]] = icmp ult i32 0, %length.{{1|2|3}} ; CHECK-NEXT: [[wide_cond_3:[^ ]+]] = and i1 [[first_iteration_check_3]], [[limit_check_3]] ; CHECK-NEXT: br label %loop br label %loop @@ -538,8 +574,8 @@ entry: loop.preheader: ; CHECK: loop.preheader: -; CHECK: [[first_iteration_check:[^ ]+]] = icmp ult i32 0, %length -; CHECK-NEXT: [[limit_check:[^ ]+]] = icmp ule i32 %n, %length +; CHECK: [[limit_check:[^ ]+]] = icmp ule i32 %n, %length +; CHECK-NEXT: [[first_iteration_check:[^ ]+]] = icmp ult i32 0, %length ; CHECK-NEXT: [[wide_cond:[^ ]+]] = and i1 [[first_iteration_check]], [[limit_check]] ; CHECK-NEXT: br label %loop br label %loop @@ -693,8 +729,8 @@ entry: loop.preheader: ; CHECK: loop.preheader: ; CHECK: [[length:[^ ]+]] = zext i16 %length.i16 to i32 -; CHECK-NEXT: [[first_iteration_check:[^ ]+]] = icmp ult i32 0, [[length]] ; CHECK-NEXT: [[limit_check:[^ ]+]] = icmp ule i32 %n, [[length]] +; CHECK-NEXT: [[first_iteration_check:[^ ]+]] = icmp ult i32 0, [[length]] ; CHECK-NEXT: [[wide_cond:[^ ]+]] = and i1 [[first_iteration_check]], [[limit_check]] ; CHECK-NEXT: br label %loop br label %loop diff --git a/test/Transforms/LoopPredication/nested.ll b/test/Transforms/LoopPredication/nested.ll index 796839feec8..8a43b058a87 100644 --- a/test/Transforms/LoopPredication/nested.ll +++ b/test/Transforms/LoopPredication/nested.ll @@ -20,8 +20,8 @@ outer.loop: inner.loop.preheader: ; CHECK: inner.loop.preheader: -; CHECK: [[first_iteration_check:[^ ]+]] = icmp ult i32 0, %length -; CHECK-NEXT: [[limit_check:[^ ]+]] = icmp sle i32 %l, %length +; CHECK: [[limit_check:[^ ]+]] = icmp sle i32 %l, %length +; CHECK-NEXT: [[first_iteration_check:[^ ]+]] = icmp ult i32 0, %length ; CHECK-NEXT: [[wide_cond:[^ ]+]] = and i1 [[first_iteration_check]], [[limit_check]] ; CHECK-NEXT: br label %inner.loop br label %inner.loop @@ -63,8 +63,8 @@ entry: outer.loop.preheader: ; CHECK: outer.loop.preheader: -; CHECK: [[first_iteration_check:[^ ]+]] = icmp ult i32 0, %length -; CHECK-NEXT: [[limit_check:[^ ]+]] = icmp sle i32 %n, %length +; CHECK: [[limit_check:[^ ]+]] = icmp sle i32 %n, %length +; CHECK-NEXT: [[first_iteration_check:[^ ]+]] = icmp ult i32 0, %length ; CHECK-NEXT: [[wide_cond:[^ ]+]] = and i1 [[first_iteration_check]], [[limit_check]] ; CHECK-NEXT: br label %outer.loop br label %outer.loop @@ -116,8 +116,8 @@ entry: outer.loop.preheader: ; CHECK: outer.loop.preheader: -; CHECK-NEXT: [[first_iteration_check_outer:[^ ]+]] = icmp ult i32 0, %length ; CHECK-NEXT: [[limit_check_outer:[^ ]+]] = icmp sle i32 %n, %length +; CHECK-NEXT: [[first_iteration_check_outer:[^ ]+]] = icmp ult i32 0, %length ; CHECK-NEXT: [[wide_cond_outer:[^ ]+]] = and i1 [[first_iteration_check_outer]], [[limit_check_outer]] ; CHECK-NEXT: br label %outer.loop br label %outer.loop diff --git a/test/Transforms/LoopPredication/visited.ll b/test/Transforms/LoopPredication/visited.ll index 01feaeabd16..8c3d54773ec 100644 --- a/test/Transforms/LoopPredication/visited.ll +++ b/test/Transforms/LoopPredication/visited.ll @@ -11,8 +11,8 @@ entry: loop.preheader: ; CHECK: loop.preheader: -; CHECK: [[first_iteration_check:[^ ]+]] = icmp ult i32 0, %length -; CHECK-NEXT: [[limit_check:[^ ]+]] = icmp ule i32 %n, %length +; CHECK: [[limit_check:[^ ]+]] = icmp ule i32 %n, %length +; CHECK-NEXT: [[first_iteration_check:[^ ]+]] = icmp ult i32 0, %length ; CHECK-NEXT: [[wide_cond:[^ ]+]] = and i1 [[first_iteration_check]], [[limit_check]] ; CHECK-NEXT: br label %loop br label %loop -- 2.40.0