<html>
    <head>
      <base href="http://llvm.org/bugs/" />
    </head>
    <body><table border="1" cellspacing="0" cellpadding="8">
        <tr>
          <th>Bug ID</th>
          <td><a class="bz_bug_link 
          bz_status_NEW "
   title="NEW --- - clang emits unnecessary vinsertf128 and vextractf128"
   href="http://llvm.org/bugs/show_bug.cgi?id=15487">15487</a>
          </td>
        </tr>

        <tr>
          <th>Summary</th>
          <td>clang emits unnecessary vinsertf128 and vextractf128
          </td>
        </tr>

        <tr>
          <th>Product</th>
          <td>clang
          </td>
        </tr>

        <tr>
          <th>Version</th>
          <td>trunk
          </td>
        </tr>

        <tr>
          <th>Hardware</th>
          <td>PC
          </td>
        </tr>

        <tr>
          <th>OS</th>
          <td>Linux
          </td>
        </tr>

        <tr>
          <th>Status</th>
          <td>NEW
          </td>
        </tr>

        <tr>
          <th>Keywords</th>
          <td>quality-of-implementation
          </td>
        </tr>

        <tr>
          <th>Severity</th>
          <td>enhancement
          </td>
        </tr>

        <tr>
          <th>Priority</th>
          <td>P
          </td>
        </tr>

        <tr>
          <th>Component</th>
          <td>-New Bugs
          </td>
        </tr>

        <tr>
          <th>Assignee</th>
          <td>unassignedclangbugs@nondot.org
          </td>
        </tr>

        <tr>
          <th>Reporter</th>
          <td>kretz@kde.org
          </td>
        </tr>

        <tr>
          <th>CC</th>
          <td>llvmbugs@cs.uiuc.edu
          </td>
        </tr>

        <tr>
          <th>Classification</th>
          <td>Unclassified
          </td>
        </tr></table>
      <p>
        <div>
        <pre>Compile the following testcase with "clang++ -O3 -mavx -c":
#include <immintrin.h>

static inline __m256i __attribute__((always_inline)) my_add(__m256i a0, __m256i
b0)
{
    __m128i a1 = _mm256_extractf128_si256(a0, 1);
    __m128i b1 = _mm256_extractf128_si256(b0, 1);
    __m256i r  =
_mm256_castsi128_si256(_mm_add_epi32(_mm256_castsi256_si128(a0),
_mm256_castsi256_si128(b0)));
    r = _mm256_insertf128_si256(r, _mm_add_epi32(a1, b1), 1);
    return r;
}

extern int DATA[];

void use_insert_extract()
{
    __m256i x = _mm256_loadu_si256((__m256i*)&DATA[0]);
    __m256i y = _mm256_loadu_si256((__m256i*)&DATA[1]);
    x = my_add(x, y);
    x = my_add(x, y);
    _mm256_storeu_si256((__m256i*)&DATA[0], x);
}

Notice the following missing optimizations:
1. clang issues a vmovups + vinsertf128 for the two unaligned-load intrinsics.
Seeing the following vextractf128 it could optimze it though. I.e. this is what
I currently get:
vmovups 0x0(%rip),%xmm0                 # DATA
vinsertf128 $0x1,0x0(%rip),%ymm0,%ymm1  # DATA+0xf
vextractf128 $0x1,%ymm1,%xmm1

instead clang could optimize to:
vmovups 0x0(%rip),%xmm0        # DATA
vmovups 0x0(%rip),%ymm1        # DATA+0xf

2. clang issues two unnecessary vinsertf128 which result from the construction
of the r variable in my_add. The compiler already recognizes that the
vextractf128 in this case can be skipped, but the vinsertf128 is still kept.
I.e. this is what I currently get:
vpaddd %xmm1,%xmm3,%xmm3
vpaddd %xmm1,%xmm3,%xmm1
vpaddd %xmm0,%xmm2,%xmm4
vxorps %xmm2,%xmm2,%xmm2          
vinsertf128 $0x0,%xmm4,%ymm2,%ymm4
vinsertf128 $0x1,%xmm3,%ymm4,%ymm3
vpaddd %xmm0,%xmm3,%xmm0          
vinsertf128 $0x0,%xmm0,%ymm2,%ymm0
vinsertf128 $0x1,%xmm1,%ymm0,%ymm0

instead clang could optimize this to:
vpaddd %xmm1,%xmm3,%xmm3
vpaddd %xmm1,%xmm3,%xmm1
vpaddd %xmm0,%xmm2,%xmm2
vpaddd %xmm0,%xmm2,%xmm0          
vinsertf128 $0x1,%xmm1,%ymm0,%ymm0</pre>
        </div>
      </p>
      <hr>
      <span>You are receiving this mail because:</span>
      
      <ul>
          <li>You are on the CC list for the bug.</li>
      </ul>
    </body>
</html>