<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 signed integer demotions"
   href="https://bugs.llvm.org/show_bug.cgi?id=44102">44102</a>
          </td>
        </tr>

        <tr>
          <th>Summary</th>
          <td>Missed canonicalizations for losslessness checks of signed 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>Much like <a class="bz_bug_link 
          bz_status_NEW "
   title="NEW - Missed canonicalizations for losslessness checks of unsigned integer demotions"
   href="show_bug.cgi?id=44100">https://bugs.llvm.org/show_bug.cgi?id=44100</a>
we have a missed canonicalization for signed pattern:

Given 

signed short t4(signed short x) {
  x+=1;
  return x;
}
signed short t5(signed short x) {
  x-=1;
  return x;
}

With -fsanitize=implicit-conversion we produce:

  %2 = sext i16 %0 to i32, !dbg !15
  %5 = add nsw i32 %2, 32769, !dbg !15
  %6 = icmp ult i32 %5, 65536, !dbg !15

and 

  %2 = sext i16 %0 to i32, !dbg !22
  %5 = add nsw i32 %2, 32767, !dbg !22
  %6 = icmp ult i32 %5, 65536, !dbg !22

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

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

Name: sext-signed-trunc-check - increment
Pre: C2 == 65536 && C1 == (C2/2)+C3 && C3 > 0 && C3 u<= C2
  %ZZ_IGNOREME = C3
  %conv = sext i16 %x to i32
  %zz = add i32 %conv, C1
  %cmp = icmp ult i32 %zz, C2
=>
  %ZZ_IGNOREME = C3
  %cmp = icmp slt i16 %x, (trunc (C2/2) - trunc (C3))

Name: sext-signed-trunc-check - decrement
Pre: C2 == 65536 && C1 == (C2/2)+C3 && C3 < 0 && C3 > -C2
  %ZZ_IGNOREME = C3
  %conv = sext i16 %x to i32
  %zz = add i32 %conv, C1
  %cmp = icmp ult i32 %zz, C2
=>
  %ZZ_IGNOREME = C3
  %cmp = icmp sge i16 %x, (-(trunc (C2/2)) - (trunc (C3)))


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