<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 - Example of missing overflow check folds"
   href="https://bugs.llvm.org/show_bug.cgi?id=43172">43172</a>
          </td>
        </tr>

        <tr>
          <th>Summary</th>
          <td>Example of missing overflow check folds
          </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>listmail@philipreames.com
          </td>
        </tr>

        <tr>
          <th>CC</th>
          <td>llvm-bugs@lists.llvm.org
          </td>
        </tr></table>
      <p>
        <div>
        <pre>Here is a collection of overflow check idioms which we don't currently turn
into the op.with.overflow equivalents (or more importantly, generate op
w/following OF flag check on x86).  

add i32, i32
  %8 = add i32 %1, %0                                                           
  %9 = xor i32 %8, %0                                                           
  %10 = xor i32 %8, %1                                                          
  %11 = and i32 %9, %10                                                         
  %12 = icmp sgt i32 %11, -1                                                    

sub i32 i32
  %8 = sub i32 %0, %1                                                           
  %9 = xor i32 %1, %0                                                           
  %10 = xor i32 %8, %0                                                          
  %11 = and i32 %10, %9                                                         
  %12 = icmp sgt i32 %11, -1

mul i32 i32
  %8 = sext i32 %0 to i64                                                       
  %9 = sext i32 %1 to i64                                                       
  %10 = mul nsw i64 %9, %8                                                      
  %11 = add nsw i64 %10, 2147483648                                             
  %12 = icmp ult i64 %11, 4294967296

increment i32 (i.e. add i32 1)
  %7 = icmp eq i32 %0, 2147483647 
  ...                                                                           
  %9 = add nsw i32 %0, 1                                                        
  ret i32 %9 

Note that in all cases, there's a branch on the overflow check leading to an
exception block.  I left that out for simplicity. 

All of these are derived from
<a href="https://github.com/AdoptOpenJDK/openjdk-jdk11/blob/master/src/java.base/share/classes/java/lang/Math.java">https://github.com/AdoptOpenJDK/openjdk-jdk11/blob/master/src/java.base/share/classes/java/lang/Math.java</a>
opExact family of methods.</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>