<table border="1" cellspacing="0" cellpadding="8">
    <tr>
        <th>Issue</th>
        <td>
            <a href=https://github.com/llvm/llvm-project/issues/58342>58342</a>
        </td>
    </tr>

    <tr>
        <th>Summary</th>
        <td>
            [InstCombine] Comparison of usub.sat does not fold
        </td>
    </tr>

    <tr>
      <th>Labels</th>
      <td>
            llvm:instcombine,
            missed-optimization
      </td>
    </tr>

    <tr>
      <th>Assignees</th>
      <td>
      </td>
    </tr>

    <tr>
      <th>Reporter</th>
      <td>
          nikic
      </td>
    </tr>
</table>

<pre>
    We fail to fold icmp of usub.sat in some cases, e.g. https://alive2.llvm.org/ce/z/oeb8f6.

The general rule here should go something like this:

    (arg < C ? 0 : arg - C) pred C2
    arg < C ? 0 pred C2 : (arg - C) pred C2

    # Assuming 0 pred C2 is false
    arg >= C && (arg - C) pred C2

    # Assuming 0 pred C2 is true
    arg < C || (arg - C) pred C2

Where the remaining and/or of icmps can fold into one comparison in many cases.
</pre>
<img width="1px" height="1px" alt="" src="http://email.email.llvm.org/o/eJylU02PmzAQ_TXmMgoCm4_kwCElG2nvlfZsYAB3jY1ss9Lur--YjZp0W6mHomGMP-a98cyjs8N784IwSqUhWBitHkD1ywp2hM1vXeplAGXA2wWhlx494y1gOqUwh7B6Js6MX8mkVm_IU63fltS6iVZ6JPdBr8XuOFYpyy4sO3_67zPChAad1OA2jTCjQ_Cz3Yh_sjtdmJWZQKtXBPrcmR4QgB7Gj9JNwEQLLfkrZOTPENcO0DJ-gtXhAC2_h3w9fzuwx93g_hb6yCng7P22xOTu8cpTDbXHr0xPTFwiGa_I_pchuO0Pgv0qdUv2b_SXvcqBau9wkcpEAmmG2CIXGx4b76nL5qYDQ4qwhvpul1U65a2JUlikef-UQppgk1dVWVVFkZfJ0IjhJE4yCSpobFj57dn40NqlUwZZSXW44zzKa7Dowdiwsyab083v0ppUmOkoZUGTKLDbcFid_YF9oKmiekVpXsujKHgyN0VdYn2s66qrjqMY8qqoi7JGLgte8E7wRMsOtY9JMs53UHFWlG5_S5fzKHQaFsLG4WDXoBb1IYOyJm6Wl0Q1POM8z3KRcyFEmfLTOJRV3p-qrM4yzFmRxTrrX39F4po98W6bPG1q5YO_b0rv1WRwr1zEl1uYrWuMelV9st-w2a_3E4rBHEU">