[LLVMbugs] [Bug 23821] New: Unnecessary instructions generated for logical vector shift by (__m128i)0

bugzilla-daemon at llvm.org bugzilla-daemon at llvm.org
Thu Jun 11 16:02:50 PDT 2015


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

            Bug ID: 23821
           Summary: Unnecessary instructions generated for logical vector
                    shift by (__m128i)0
           Product: libraries
           Version: trunk
          Hardware: PC
                OS: Windows NT
            Status: NEW
          Severity: normal
          Priority: P
         Component: Backend: X86
          Assignee: unassignedbugs at nondot.org
          Reporter: charles_li at playstation.sony.com
                CC: llvmbugs at cs.uiuc.edu
    Classification: Unclassified

On X86, logically shifting a vector by “(__m128i) 0” will cause unnecessary
instructions at -O2
( "(__m128i) 0" being a 128-bit vector constant of value 0 )

Here is an example of poor code gen assembling the following results in
a vector xor followed by vector shift logical left.
Since xor of a register with itself is 0 and shifting a value by 0 is itself,
the 2 instructions are redundant.

test.cpp:
  __m128i logic_left_shift_vec_0_of_64( __m128i a ) {
    return _mm_sll_epi64( a, (__m128i) {0, 0} ); 
  }

  clang test.cpp -S -O2

test.s:
  vpxor     %xmm1, %xmm1, %xmm1
  vpsllq    %xmm1, %xmm0, %xmm0
  retq


Here is an example of where Clang does good code gen, in this intrinsic, 0 is a
scalar integer
When assembling at -O2, only return instruction is generated.

test2.cpp:
  __m128i logic_left_shift_int_0_of_64( __m128i a ) {
    return _mm_slli_epi64( a, 0 ); 
  }

  clang test2.cpp -S -O2

test2.s:
  retq


Here is another example of good code gen, Arithmetic shift of a vector by
“(__m128i) 0”.
In this example, despite the 0 being a vector immediate, Clang is still able 
to deduce that shifting by 0 has no effect.

  __m128i arith_right_shift_int_0_of_32( __m128i a ) {
    return _mm_srai_epi32( a, 0 ); 
  }

  clang test3.cpp -S -O2

  retq


Here are all of the vector shift intrinsics that results in poor code gen.
/***************************************************/
#include <x86intrin.h>

// Logical shift by vec immediate zero
__m128i logic_left_shift_vec_0_of_64( __m128i a ) {
  return _mm_sll_epi64( a, (__m128i) {0, 0} ); 
}

__m128i logic_right_shift_vec_0_of_64( __m128i a ) {
  return _mm_srl_epi64( a, (__m128i) {0, 0} ); 
}

__m128i logic_left_shift_vec_0_of_32( __m128i a ) {
  return _mm_sll_epi32( a, (__m128i) {0, 0} ); 
}

__m128i logic_right_shift_vec_0_of_32( __m128i a ) {
  return _mm_srl_epi32( a, (__m128i) {0, 0} ); 
}

__m128i logic_left_shift_vec_0_of_16( __m128i a ) {
  return _mm_sll_epi16( a, (__m128i) {0, 0} ); 
}

__m128i logic_right_shift_vec_0_of_16( __m128i a ) {
  return _mm_srl_epi16( a, (__m128i) {0, 0} ); 
}
/***************************************************/


For comparison, here are the other vector shift intrinsics 
that result in good code gen.
/***************************************************/
// Logical shift by int immediate zero
__m128i logic_left_shift_int_0_of_64( __m128i a ) {
  return _mm_slli_epi64( a, 0 ); 
}

__m128i logic_right_shift_int_0_of_64( __m128i a ) {
  return _mm_srli_epi64( a, 0 ); 
}

__m128i logic_left_shift_int_0_of_32( __m128i a ) {
  return _mm_slli_epi32( a, 0 ); 
}

__m128i logic_right_shift_int_0_of_32( __m128i a ) {
  return _mm_srli_epi32( a, 0 ); 
}

__m128i logic_left_shift_int_0_of_16( __m128i a ) {
  return _mm_slli_epi16( a, 0 ); 
}

__m128i logic_right_shift_int_0_of_16( __m128i a ) {
  return _mm_srli_epi16( a, 0 ); 
}

// Arithmetic Shifts
__m128i arith_right_shift_vec_0_of_32( __m128i a ) {
  return _mm_sra_epi32( a, (__m128i) {0, 0} ); 
}

__m128i arith_right_shift_int_0_of_32( __m128i a ) {
  return _mm_srai_epi32( a, 0 ); 
}

__m128i arith_right_shift_vec_0_of_16( __m128i a ) {
  return _mm_sra_epi16( a, (__m128i) {0, 0} ); 
}

__m128i arith_right_shift_int_0_of_16( __m128i a ) {
  return _mm_srai_epi16( a, 0 ); 
}
/***************************************************/

-- 
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/20150611/ef83e190/attachment.html>


More information about the llvm-bugs mailing list