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

    <tr>
        <th>Summary</th>
        <td>
            Miscompilation of `x / umax(Pow2 << X, 1)` on LLVM15/LLVM16
        </td>
    </tr>

    <tr>
      <th>Labels</th>
      <td>
            miscompilation,
            llvm:instcombine
      </td>
    </tr>

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

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

<pre>
    This came up with investigating / patching PR62175

The following snippet:
```
declare i8 @llvm.umax.i8(i8, i8)
define i8 @udiv_fail_shl_overflow(i8 %x, i8 %y) {
  %shl = shl i8 2, %y
  %min = call i8 @llvm.umax.i8(i8 %shl, i8 1)
  %mul = udiv i8 %x, %min
  ret i8 %mul
}
```

Gets miscompiled on llvm15/llvm16: https://godbolt.org/z/ehen9hz6G
to
```
 %1 = add i8 %y, 1
  %mul1 = lshr i8 %x, %1
  ret i8 %mul1
```
which is not equivilent: https://alive2.llvm.org/ce/z/Yy7h57

because the `shl i8 2, %y` can overflow.

In the original IR, that would just mean dividing by `1` so returning `%x`.
In the generated IR that means `lshr` by `8` which returns `poison`.

This was fixed on trunc with: https://reviews.llvm.org/D148609

but the `AssumeNonZero` piece of the the patch needs to be backported
to llvm15/llvm16.
</pre>
<img width="1px" height="1px" alt="" src="http://email.email.llvm.org/o/eJx0VE2PozgQ_TXmUpoIDCFw4NDTUUYtzaxGrdFqdy8tg4tQs8ZmsZ10z69f2ZD-zEjdxsSvnus9qkpYS0eN2LDtZ7bdJ8K7wczN0ShpHZLWSWvkU_NjIAudGBH8BGdyA5A-oXV0FI70ERg_wCRcN4SX7_clz3Zblu5ZerOsPwaE3ihlzgFgNU0TOpavp6xM17_4KrFTYkagCliRKnUaN34UjxuqGK_CcgthrS_onvQF7CWdHnpB6sEO6sGccO6VOccwYHz7uMSG7RPjNbDd54UEwk92U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