[llvm-bugs] [Bug 40155] New: [X86] Mask from phi generates redundant pslld

via llvm-bugs llvm-bugs at lists.llvm.org
Tue Dec 25 13:15:07 PST 2018


            Bug ID: 40155
           Summary: [X86] Mask from phi generates redundant pslld
           Product: libraries
           Version: trunk
          Hardware: PC
                OS: All
            Status: NEW
          Severity: enhancement
          Priority: P
         Component: Backend: X86
          Assignee: unassignedbugs at nondot.org
          Reporter: nikita.ppv at gmail.com
                CC: craig.topper at gmail.com, llvm-bugs at lists.llvm.org,
                    llvm-dev at redking.me.uk, spatel+llvm at rotateright.com

Originally reported at https://github.com/rust-lang/rust/issues/57110.

The following Rust code:

pub unsafe fn foo(y: __m128i, mut x: __m128i) {
    let mut mask = _mm_cmplt_epi32(x, y);
    while _mm_extract_epi32(mask, 0) == 0 {
        x = _mm_blendv_epi8(y, x, mask);
        mask = _mm_cmplt_epi32(x, y);

With this slightly reduced IR:

define <4 x i32> @test(<4 x i32> %x, <4 x i32> %y) #0 {
  %mask = icmp slt <4 x i32> %x, %y
  %elem = extractelement <4 x i1> %mask, i32 0
  br i1 %elem, label %end, label %loop

  %mask2 = phi <4 x i1> [ %mask3, %loop ], [ %mask, %start ]
  %x2 = phi <4 x i32> [ %x3, %loop ], [ %x, %start ]
  %x3 = select <4 x i1> %mask2, <4 x i32> %x2, <4 x i32> %y
  %mask3 = icmp slt <4 x i32> %x3, %y
  %elem2 = extractelement <4 x i1> %mask3, i32 0
  br i1 %elem2, label %end, label %loop

  %x4 = phi <4 x i32> [ %x3, %loop ], [ %x, %start ]
  ret <4 x i32> %x4
attributes #0 = { "target-cpu"="x86-64" "target-features"="+sse4.1" }

Produces the following assembly for the loop BB:

.LBB0_2:                                # %loop
                                        # =>This Inner Loop Header: Depth=1
        pslld   $31, %xmm0
        movdqa  %xmm1, %xmm3
        blendvps        %xmm0, %xmm2, %xmm3
        movdqa  %xmm1, %xmm0
        pcmpgtd %xmm3, %xmm0
        pextrb  $0, %xmm0, %eax
        movaps  %xmm3, %xmm2
        testb   $1, %al
        je      .LBB0_2

The pslld 31, %xmm0 instruction here is unnecessary.

The issue is that the comparison mask comes in from a phi node. In the entry
block the v4i1 mask was type legalized to v4i32. In the loop block it's
truncated back down to v4i1 and used in a vselect. Type legalization then
converts the truncate into a sign_extend_inreg from v4i1 to v4i32. This is a
no-op for mask values and would usually get combined away. However, at this
point we don't know that this is a mask (coming from a pcmpgtd for both phi
operands) anymore.

This is somewhat related to https://bugs.llvm.org/show_bug.cgi?id=11730, though
there is no type legalization issue here (the chosen types are correct).

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/20181225/3d8b9ec4/attachment.html>

More information about the llvm-bugs mailing list