<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 - Missed canonicalizations for losslessness checks of unsigned integer demotions"
   href="https://bugs.llvm.org/show_bug.cgi?id=44100">44100</a>
          </td>
        </tr>

        <tr>
          <th>Summary</th>
          <td>Missed canonicalizations for losslessness checks of unsigned integer demotions
          </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>Linux
          </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>lebedev.ri@gmail.com
          </td>
        </tr>

        <tr>
          <th>CC</th>
          <td>llvm-bugs@lists.llvm.org
          </td>
        </tr></table>
      <p>
        <div>
        <pre>Given

unsigned short t0(unsigned short x) {
  x+=1;
  return x;
}
unsigned short t1(unsigned short x) {
  x-=1;
  return x;
}

With -fsanitize=implicit-conversion we produce:

  %3 = add nuw nsw i32 %2, 1, !dbg !15
  %4 = trunc i32 %3 to i16, !dbg !15
  %5 = icmp ult i32 %3, 65536, !dbg !15

and

  %2 = zext i16 %0 to i32, !dbg !22
  %3 = add nsw i32 %2, -1, !dbg !22
  %4 = trunc i32 %3 to i16, !dbg !22
  %5 = icmp ult i32 %3, 65536, !dbg !22

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

But in these cases we can simply check the original %2:

Name: unsigned truncation check - increment
Pre: C2 u>= C1
  %add = add nuw i32 %conv, C1 ; has extra uses
  %cmp = icmp ult i32 %add, C2 ; is truncation NUW?
=>
  %cmp = icmp ult i32 %conv, (C2-C1)

Name: unsigned truncation check - decrement
Pre: C1 <= 0 ; C1 is non-positive
  %conv = zext i16 %x to i32
  %add = add i32 %conv, C1 ; has extra uses
  %cmp = icmp ult i32 %add, 65536 ; is truncation NUW?
=>
  %cmp = icmp uge i32 %conv, -C1

<a href="https://rise4fun.com/Alive/x35G">https://rise4fun.com/Alive/x35G</a></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>