[llvm-bugs] [Bug 46590] New: [X86][SSE] Lower icmp_ugt(x, pow2 - 1) -> icmp_ne(and(x, pow2 - 1), 0)

via llvm-bugs llvm-bugs at lists.llvm.org
Sun Jul 5 05:35:30 PDT 2020


https://bugs.llvm.org/show_bug.cgi?id=46590

            Bug ID: 46590
           Summary: [X86][SSE] Lower icmp_ugt(x, pow2 - 1) ->
                    icmp_ne(and(x, pow2 - 1), 0)
           Product: libraries
           Version: trunk
          Hardware: PC
                OS: Windows NT
            Status: NEW
          Severity: enhancement
          Priority: P
         Component: Backend: X86
          Assignee: unassignedbugs at nondot.org
          Reporter: llvm-dev at redking.me.uk
                CC: craig.topper at gmail.com, llvm-bugs at lists.llvm.org,
                    llvm-dev at redking.me.uk, spatel+llvm at rotateright.com

Vector unsigned comparisons currently lower to pcmpeq(pmaxu(x,y),x) (or pminu
for ult).

typedef unsigned int __v4su __attribute__((__vector_size__(16)));
__v4su cmp_ugt(__v4su x) {
    return x > 0x7f;
}

define <4 x i32> @cmp_ugt(<4 x i32> %0) {
  %2 = icmp ugt <4 x i32> %0, <i32 127, i32 127, i32 127, i32 127>
  %3 = sext <4 x i1> %2 to <4 x i32>
  ret <4 x i32> %3
}

.LCPI0_0:
  .long 128 # 0x80
  .long 128 # 0x80
  .long 128 # 0x80
  .long 128 # 0x80
cmp_ugt:
  vpmaxud .LCPI0_0(%rip), %xmm0, %xmm1
  vpcmpeqd %xmm1, %xmm0, %xmm0
  retq

But for cases where we're comparing against a pow2-1 mask we can lower to:

define <4 x i32> @cmp_ugt(<4 x i32> %0) {
  %2 = and <4 x i32> %0, <i32 ~127, i32 ~127, i32 ~127, i32 ~127>
  %3 = icmp ne <4 x i32> %2, zeroinitializer
  %4 = sext <4 x i1> %3 to <4 x i32>
  ret <4 x i32> %4
}

.LCPI0_0:
  .long 128 # 0xFFFFFF80
  .long 128 # 0xFFFFFF80
  .long 128 # 0xFFFFFF80
  .long 128 # 0xFFFFFF80
cmp_ugt:
  vpand .LCPI0_0(%rip), %xmm0, %xmm0
  vpxor %xmm1, %xmm1, %xmm1
  vpcmpeqd %xmm1, %xmm0, %xmm0
  vpcmpeqd %xmm1, %xmm1, %xmm1
  vpxor %xmm1, %xmm0, %xmm0
  retq

(instcombine already performs the inverse of this fold).

This is definitely better in cases where we can pull out the NOT - reductions
(movmsk/ptest cases), selections etc. 

Noticed while looking at [Bug #46053]

-- 
You are receiving this mail because:
You are on the CC list for the bug.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/llvm-bugs/attachments/20200705/93966ce0/attachment.html>


More information about the llvm-bugs mailing list