[PATCH] InstSimplify: Optimize signed icmp of -(zext V)

Nick Lewycky nlewycky at google.com
Wed May 14 00:00:32 PDT 2014


On 13 May 2014 23:32, David Majnemer <david.majnemer at gmail.com> wrote:

> Hi nicholas,
>
> We know that -(zext V) will always be <= zero, simplify signed icmps
> that have these.
>
> Uncovered using http://www.cs.utah.edu/~regehr/souper/
>
> http://reviews.llvm.org/D3754
>
> Files:
>   lib/Analysis/InstructionSimplify.cpp
>   test/Transforms/InstSimplify/compare.ll
>
> Index: lib/Analysis/InstructionSimplify.cpp
> ===================================================================
> --- lib/Analysis/InstructionSimplify.cpp
> +++ lib/Analysis/InstructionSimplify.cpp
> @@ -2280,6 +2280,24 @@
>      }
>    }
>
> +  // 0 - (zext X) pred C
> +  if (match(LHS, m_Neg(m_ZExt(m_Value())))) {
>

if (CmpInst::isSigned(Pred) && ICmpInst::isRelational(Pred) && match(...


> +    if (ConstantInt *RHSC = dyn_cast<ConstantInt>(RHS)) {
> +      if (RHSC->getValue().isStrictlyPositive()) {
> +        if (Pred == ICmpInst::ICMP_SLT)
> +          return ConstantInt::getTrue(RHSC->getContext());
> +        if (Pred == ICmpInst::ICMP_SGE)
> +          return ConstantInt::getFalse(RHSC->getContext());
> +      }
> +      if (RHSC->getValue().isNonNegative()) {
> +        if (Pred == ICmpInst::ICMP_SLE)
> +          return ConstantInt::getTrue(RHSC->getContext());
> +        if (Pred == ICmpInst::ICMP_SGT)
> +          return ConstantInt::getFalse(RHSC->getContext());
> +      }
> +    }
> +  }
> +
>    // icmp pred (urem X, Y), Y
>    if (LBO && match(LBO, m_URem(m_Value(), m_Specific(RHS)))) {
>      bool KnownNonNegative, KnownNegative;
> Index: test/Transforms/InstSimplify/compare.ll
> ===================================================================
> --- test/Transforms/InstSimplify/compare.ll
> +++ test/Transforms/InstSimplify/compare.ll
> @@ -757,3 +757,43 @@
>  ; CHECK-LABEL: @vectorselectfold
>  ; CHECK-NEXT: ret <4 x i8> %a
>  }
> +
> +define i1 @compare_always_true_slt(i16 %a) {
> +  %1 = zext i16 %a to i32
> +  %2 = sub nsw i32 0, %1
> +  %3 = icmp slt i32 %2, 1
> +  ret i1 %3
> +
> +; CHECK-LABEL: @compare_always_true_slt
> +; CHECK-NEXT: ret i1 true
> +}
> +
> +define i1 @compare_always_true_sle(i16 %a) {
> +  %1 = zext i16 %a to i32
> +  %2 = sub nsw i32 0, %1
> +  %3 = icmp sle i32 %2, 0
> +  ret i1 %3
> +
> +; CHECK-LABEL: @compare_always_true_sle
> +; CHECK-NEXT: ret i1 true
> +}
> +
> +define i1 @compare_always_false_sgt(i16 %a) {
> +  %1 = zext i16 %a to i32
> +  %2 = sub nsw i32 0, %1
> +  %3 = icmp sgt i32 %2, 0
> +  ret i1 %3
> +
> +; CHECK-LABEL: @compare_always_false_sgt
> +; CHECK-NEXT: ret i1 false
> +}
> +
> +define i1 @compare_always_false_sge(i16 %a) {
> +  %1 = zext i16 %a to i32
> +  %2 = sub nsw i32 0, %1
> +  %3 = icmp sge i32 %2, 1
> +  ret i1 %3
> +
> +; CHECK-LABEL: @compare_always_false
> +; CHECK-NEXT: ret i1 false
> +}
>

LGTM

Nick
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20140514/f73b3833/attachment.html>


More information about the llvm-commits mailing list