From 0acdce17938d1d2318f72af77000fa19d37f61ac Mon Sep 17 00:00:00 2001 From: Max Kazantsev Date: Sat, 8 Jul 2017 17:17:30 +0000 Subject: [PATCH] Re-enable "[IndVars] Canonicalize comparisons between non-negative values and indvars" The patch was reverted due to a bug. The bug was that if the IV is the 2nd operand of the icmp instruction, then the "Pred" variable gets swapped and differs from the instruction's predicate. In this patch we use the original predicate to do the transformation. Also added a test case that exercises this situation. Differentian Revision: https://reviews.llvm.org/D35107 git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@307477 91177308-0d34-0410-b5e6-96231b3b80d8 --- lib/Transforms/Utils/SimplifyIndVar.cpp | 11 +++ test/Analysis/ScalarEvolution/guards.ll | 6 +- .../IndVarSimplify/canonicalize-cmp.ll | 98 +++++++++++++++++++ .../IndVarSimplify/eliminate-comparison.ll | 4 +- .../IndVarSimplify/widen-loop-comp.ll | 2 +- 5 files changed, 115 insertions(+), 6 deletions(-) create mode 100644 test/Transforms/IndVarSimplify/canonicalize-cmp.ll diff --git a/lib/Transforms/Utils/SimplifyIndVar.cpp b/lib/Transforms/Utils/SimplifyIndVar.cpp index 4ebafd8ea78..6d90e6b4835 100644 --- a/lib/Transforms/Utils/SimplifyIndVar.cpp +++ b/lib/Transforms/Utils/SimplifyIndVar.cpp @@ -156,6 +156,7 @@ Value *SimplifyIndvar::foldIVUser(Instruction *UseInst, Instruction *IVOperand) void SimplifyIndvar::eliminateIVComparison(ICmpInst *ICmp, Value *IVOperand) { unsigned IVOperIdx = 0; ICmpInst::Predicate Pred = ICmp->getPredicate(); + ICmpInst::Predicate OriginalPred = Pred; if (IVOperand != ICmp->getOperand(0)) { // Swapped assert(IVOperand == ICmp->getOperand(1) && "Can't find IVOperand"); @@ -264,6 +265,16 @@ void SimplifyIndvar::eliminateIVComparison(ICmpInst *ICmp, Value *IVOperand) { ICmp->setPredicate(InvariantPredicate); ICmp->setOperand(0, NewLHS); ICmp->setOperand(1, NewRHS); + } else if (ICmpInst::isSigned(OriginalPred) && + SE->isKnownNonNegative(S) && SE->isKnownNonNegative(X)) { + // If we were unable to make anything above, all we can is to canonicalize + // the comparison hoping that it will open the doors for other + // optimizations. If we find out that we compare two non-negative values, + // we turn the instruction's predicate to its unsigned version. Note that + // we cannot rely on Pred here unless we check if we have swapped it. + assert(ICmp->getPredicate() == OriginalPred && "Predicate changed?"); + DEBUG(dbgs() << "INDVARS: Turn to unsigned comparison: " << *ICmp << '\n'); + ICmp->setPredicate(ICmpInst::getUnsignedPredicate(OriginalPred)); } else return; diff --git a/test/Analysis/ScalarEvolution/guards.ll b/test/Analysis/ScalarEvolution/guards.ll index 52ad4dc73d4..d4b1f431ffc 100644 --- a/test/Analysis/ScalarEvolution/guards.ll +++ b/test/Analysis/ScalarEvolution/guards.ll @@ -19,7 +19,7 @@ entry: loop: ; CHECK: loop: ; CHECK: call void (i1, ...) @llvm.experimental.guard(i1 true) [ "deopt"() ] -; CHECK: %iv.inc.cmp = icmp slt i32 %iv.inc, %len +; CHECK: %iv.inc.cmp = icmp ult i32 %iv.inc, %len ; CHECK: call void (i1, ...) @llvm.experimental.guard(i1 %iv.inc.cmp) [ "deopt"() ] ; CHECK: leave: @@ -41,7 +41,7 @@ leave: define void @test_2(i32 %n, i32* %len_buf) { ; CHECK-LABEL: @test_2( -; CHECK: [[LEN_SEXT:%[^ ]+]] = sext i32 %len to i64 +; CHECK: [[LEN_ZEXT:%[^ ]+]] = zext i32 %len to i64 ; CHECK: br label %loop entry: @@ -52,7 +52,7 @@ loop: ; CHECK: loop: ; CHECK: %indvars.iv = phi i64 [ %indvars.iv.next, %loop ], [ 0, %entry ] ; CHECK: %indvars.iv.next = add nuw nsw i64 %indvars.iv, 1 -; CHECK: %iv.inc.cmp = icmp slt i64 %indvars.iv.next, [[LEN_SEXT]] +; CHECK: %iv.inc.cmp = icmp ult i64 %indvars.iv.next, [[LEN_ZEXT]] ; CHECK: call void (i1, ...) @llvm.experimental.guard(i1 %iv.inc.cmp) [ "deopt"() ] ; CHECK: leave: diff --git a/test/Transforms/IndVarSimplify/canonicalize-cmp.ll b/test/Transforms/IndVarSimplify/canonicalize-cmp.ll new file mode 100644 index 00000000000..2b939767284 --- /dev/null +++ b/test/Transforms/IndVarSimplify/canonicalize-cmp.ll @@ -0,0 +1,98 @@ +; RUN: opt -S -indvars < %s | FileCheck %s + +; Check that we replace signed comparisons between non-negative values with +; unsigned comparisons if we can. + +target datalayout = "n8:16:32:64" + +define i32 @test_01(i32 %a, i32 %b, i32* %p) { + +; CHECK-LABEL: @test_01( +; CHECK-NOT: icmp slt +; CHECK: %cmp1 = icmp ult i32 %iv, 100 +; CHECK: %cmp2 = icmp ult i32 %iv, 100 +; CHECK-NOT: %cmp3 +; CHECK: %exitcond = icmp ne i32 %iv.next, 1000 + +entry: + br label %loop.entry + +loop.entry: + %iv = phi i32 [ 0, %entry ], [ %iv.next, %loop.be ] + %cmp1 = icmp slt i32 %iv, 100 + br i1 %cmp1, label %b1, label %b2 + +b1: + store i32 %iv, i32* %p + br label %merge + +b2: + store i32 %a, i32* %p + br label %merge + +merge: + %cmp2 = icmp ult i32 %iv, 100 + br i1 %cmp2, label %b3, label %b4 + +b3: + store i32 %iv, i32* %p + br label %loop.be + +b4: + store i32 %b, i32* %p + br label %loop.be + +loop.be: + %iv.next = add i32 %iv, 1 + %cmp3 = icmp slt i32 %iv.next, 1000 + br i1 %cmp3, label %loop.entry, label %exit + +exit: + ret i32 %iv +} + +define i32 @test_02(i32 %a, i32 %b, i32* %p) { + +; CHECK-LABEL: @test_02( +; CHECK-NOT: icmp sgt +; CHECK: %cmp1 = icmp ugt i32 100, %iv +; CHECK: %cmp2 = icmp ugt i32 100, %iv +; CHECK-NOT: %cmp3 +; CHECK: %exitcond = icmp ne i32 %iv.next, 1000 + +entry: + br label %loop.entry + +loop.entry: + %iv = phi i32 [ 0, %entry ], [ %iv.next, %loop.be ] + %cmp1 = icmp sgt i32 100, %iv + br i1 %cmp1, label %b1, label %b2 + +b1: + store i32 %iv, i32* %p + br label %merge + +b2: + store i32 %a, i32* %p + br label %merge + +merge: + %cmp2 = icmp ugt i32 100, %iv + br i1 %cmp2, label %b3, label %b4 + +b3: + store i32 %iv, i32* %p + br label %loop.be + +b4: + store i32 %b, i32* %p + br label %loop.be + +loop.be: + %iv.next = add i32 %iv, 1 + %cmp3 = icmp sgt i32 1000, %iv.next + br i1 %cmp3, label %loop.entry, label %exit + +exit: + ret i32 %iv +} diff --git a/test/Transforms/IndVarSimplify/eliminate-comparison.ll b/test/Transforms/IndVarSimplify/eliminate-comparison.ll index 612f01e3cad..a63617e62c0 100644 --- a/test/Transforms/IndVarSimplify/eliminate-comparison.ll +++ b/test/Transforms/IndVarSimplify/eliminate-comparison.ll @@ -111,7 +111,7 @@ return: ; Indvars should not turn the second loop into an infinite one. ; CHECK-LABEL: @func_11( -; CHECK: %tmp5 = icmp slt i32 %__key6.0, 10 +; CHECK: %tmp5 = icmp ult i32 %__key6.0, 10 ; CHECK-NOT: br i1 true, label %noassert68, label %unrolledend define i32 @func_11() nounwind uwtable { @@ -163,7 +163,7 @@ declare void @llvm.trap() noreturn nounwind ; In this case the second loop only has a single iteration, fold the header away ; CHECK-LABEL: @func_12( -; CHECK: %tmp5 = icmp slt i32 %__key6.0, 10 +; CHECK: %tmp5 = icmp ult i32 %__key6.0, 10 ; CHECK: br i1 true, label %noassert68, label %unrolledend define i32 @func_12() nounwind uwtable { entry: diff --git a/test/Transforms/IndVarSimplify/widen-loop-comp.ll b/test/Transforms/IndVarSimplify/widen-loop-comp.ll index b87cd055019..2d24cd732ce 100644 --- a/test/Transforms/IndVarSimplify/widen-loop-comp.ll +++ b/test/Transforms/IndVarSimplify/widen-loop-comp.ll @@ -64,7 +64,7 @@ for.end: ; CHECK-LABEL: @test2 ; CHECK: for.body4.us ; CHECK: %indvars.iv.next = add nuw nsw i64 %indvars.iv, 1 -; CHECK: %cmp2.us = icmp slt i64 +; CHECK: %cmp2.us = icmp ult i64 ; CHECK-NOT: %2 = trunc i64 %indvars.iv.next to i32 ; CHECK-NOT: %cmp2.us = icmp slt i32 -- 2.40.0