<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 - Are we missing nsw/nuw tags on vector operations?"
   href="https://bugs.llvm.org/show_bug.cgi?id=43399">43399</a>
          </td>
        </tr>

        <tr>
          <th>Summary</th>
          <td>Are we missing nsw/nuw tags on vector operations?
          </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>Scalar Optimizations
          </td>
        </tr>

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

        <tr>
          <th>Reporter</th>
          <td>llvm-dev@redking.me.uk
          </td>
        </tr>

        <tr>
          <th>CC</th>
          <td>llvm-bugs@lists.llvm.org, nunoplopes@sapo.pt, spatel+llvm@rotateright.com
          </td>
        </tr></table>
      <p>
        <div>
        <pre>Split off from <a class="bz_bug_link 
          bz_status_NEW "
   title="NEW - Failed to combine -smax(-x,-y) to smin(x,y)"
   href="show_bug.cgi?id=43310">https://bugs.llvm.org/show_bug.cgi?id=43310</a>

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

#include <stdint.h>
#include <x86intrin.h>

auto add_i32(int x, int y) {
    return x + y;
}
auto add_v4i32(__v4si x, __v4si y) {
    return x + y;
}

clang -g0 -O3 -march=btver2 -emit-llvm

define i32 @add_i32(i32 %0, i32 %1) {
  %3 = add nsw i32 %1, %0
  ret i32 %3
}
define <4 x i32> @add_v4i32(<4 x i32> %0, <4 x i32> %1) {
  %3 = add <4 x i32> %1, %0
  ret <4 x i32> %3
}

While we tag the scalar op with nsw we fail to do this for the vector version.

If we explicitly reduce the range of the vector values then nsw does reappear:

auto add_ashr_v4i32(__v4si x, __v4si y) {
    x >>= 15;
    y >>= 15;
    return x + y;
}

define <4 x i32> @add_ashr_v4i32(<4 x i32> %0, <4 x i32> %1) {
  %3 = ashr <4 x i32> %0, <i32 15, i32 15, i32 15, i32 15>
  %4 = ashr <4 x i32> %1, <i32 15, i32 15, i32 15, i32 15>
  %5 = add nsw <4 x i32> %4, %3
  ret <4 x i32> %5
}</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>