<html>
    <head>
      <base href="https://bugs.llvm.org/">
    </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 - Unnecessary conversion/simplification of vector constants can lead to duplication of constants"
   href="https://bugs.llvm.org/show_bug.cgi?id=43973">43973</a>
          </td>
        </tr>

        <tr>
          <th>Summary</th>
          <td>Unnecessary conversion/simplification of vector constants can lead to duplication of constants
          </td>
        </tr>

        <tr>
          <th>Product</th>
          <td>libraries
          </td>
        </tr>

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

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

        <tr>
          <th>OS</th>
          <td>Windows NT
          </td>
        </tr>

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

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

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

        <tr>
          <th>Component</th>
          <td>Register Allocator
          </td>
        </tr>

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

        <tr>
          <th>Reporter</th>
          <td>zingaburga+llvm@hotmail.com
          </td>
        </tr>

        <tr>
          <th>CC</th>
          <td>llvm-bugs@lists.llvm.org, quentin.colombet@gmail.com
          </td>
        </tr></table>
      <p>
        <div>
        <pre>Firstly, not sure if this is the right place to post this missed optimization -
sorry if I got it wrong.

With the optimizer enabled, it seems that a number of vector constants get
simplified where it results in worse codegen.  Have no clue where this happens,
so I'll use C -> x86 AVX as examples.

First example is an integer subtract with constant which always gets changed to
an add.  For a target like x86 SSE, I suppose this may make sense, as the
commutative property of addition gives more flexibility around register
placement, but it isn't always beneficial - for example, if the constant could
be re-used elsewhere.

Example:

        _mm_or_si128(
                _mm_sub_epi8(a, _mm_set1_epi8(99)),
                _mm_set1_epi8(99)
        );

In this case, the '99' constant can be used in both the subtract and or, but
Clang/LLVM will always convert the first use to a '-99' constant, meaning that
it now has to deal with two constants: <a href="https://godbolt.org/z/b_B8yC">https://godbolt.org/z/b_B8yC</a>

Perhaps a different issue, but reversing the arguments to the subtract,
Clang/LLVM performs an unnecessary reload of the constant:
<a href="https://godbolt.org/z/8kus5_">https://godbolt.org/z/8kus5_</a>



Another example would be truncated vector constants, e.g:

        const __m256i stuff = _mm256_set1_epi32(0x01020304);
        a = _mm256_permutexvar_epi16(a, stuff);
        return _mm_or_si128(
                _mm256_extracti128_si256(a, 1),
                _mm256_castsi256_si128(stuff)
        );

Vector for `stuff` can be re-used for both cases, but the second case doesn't
get recognised: <a href="https://godbolt.org/z/rZQqTf">https://godbolt.org/z/rZQqTf</a>

Perhaps a different issue, but interestingly in the previous link, even if the
vector isn't truncated, it still gets thought of as different somehow.



Another example, the constant for pshufb is reduced, resulting in duplication
of a constant:

        const __m128i shuf = _mm_set1_epi8(65);
        return _mm_or_si128(
                _mm_shuffle_epi8(a, shuf),
                shuf
        );

<a href="https://godbolt.org/z/vEs_S2">https://godbolt.org/z/vEs_S2</a>

If the `65` vector was kept instead of simplifying to a vector of `1`, only one
constant would be needed.</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>