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

    <tr>
        <th>Summary</th>
        <td>
            [InstCombine] `range` attribute should be dropped in `foldBitCeil`
        </td>
    </tr>

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

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

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

<pre>
    Reproducer: https://alive2.llvm.org/ce/z/NQTmkh (Please use latest alive2 with https://github.com/AliveToolkit/alive2/pull/1097)
```
define i32 @src(i32 %x) {
  %ctlz = call range(i32 1, 33) i32 @llvm.ctlz.i32(i32 %x, i1 false)
  %sub = sub i32 32, %ctlz
  %shl = shl i32 1, %sub
  %dec = add i32 %x, -1
  %ult = icmp ult i32 %dec, -2
  %sel = select i1 %ult, i32 %shl, i32 1
  ret i32 %sel
}

define i32 @tgt(i32 %x) {
  %ctlz = call range(i32 1, 33) i32 @llvm.ctlz.i32(i32 %x, i1 false)
 %1 = sub nsw i32 0, %ctlz
  %2 = and i32 %1, 31
  %sel = shl nuw i32 1, %2
  ret i32 %sel
}
```
```
----------------------------------------
define i32 @src(i32 %x) {
#0:
  %ctlz = ctlz i32 %x, 0
  %#range_0_%ctlz = !range i32 %ctlz, i32 1, i32 33
  %sub = sub i32 32, %#range_0_%ctlz
 %shl = shl i32 1, %sub
  %dec = add i32 %x, 4294967295
  %ult = icmp ult i32 %dec, 4294967294
  %sel = select i1 %ult, i32 %shl, i32 1
  ret i32 %sel
}
=>
define i32 @tgt(i32 %x) {
#0:
  %ctlz = ctlz i32 %x, 0
  %#range_0_%ctlz = !range i32 %ctlz, i32 1, i32 33
  %#1 = sub nsw i32 0, %#range_0_%ctlz
  %#2 = and i32 %#1, 31
  %sel = shl nuw i32 1, %#2
  ret i32 %sel
}
Transformation doesn't verify!

ERROR: Target is more poisonous than source

Example:
i32 %x = #xffffffff (4294967295, -1)

Source:
i32 %ctlz = #x00000000 (0)
i32 %#range_0_%ctlz = poison
i32 %sub = poison
i32 %shl = poison
i32 %dec = #xfffffffe (4294967294, -2)
i1 %ult = #x0 (0)
i32 %sel = #x00000001 (1)

Target:
i32 %ctlz = #x00000000 (0)
i32 %#range_0_%ctlz = poison
i32 %#1 = poison
i32 %#2 = poison
i32 %sel = poison
Source value: #x00000001 (1)
Target value: poison
```

</pre>
<img width="1px" height="1px" alt="" src="http://email.email.llvm.org/o/eJzEVlFvqzgT_TWTF6uRGQcIDzykyY30vXy72-37lQOT4L0GI2zStL9-ZSCUpOlVqtXuRVHA-MyM5xwPHmmtOlREKYSPgJi77PT2-gKIEG5msnWFadLh5Wxn8tf0ierG5G1GDYgVK5yrLYgV4BZwK7U6Es61PpZz0xwAtxkBbt8At___47n8UTDA5e-apCXWWmJaOrKO9WbsRbniyuFBuaLdzTNTAm5XHvZsjP6h3BgMcFu3WgNuA57EgAnwDfAVRHz4dcOc9qoipgQyWHDbZIDLboDhCTBhED_2QOZfZU6_MRAblkmtWSOrAw34AHDNhPAmg68uV28wVwIvvK6ZCtheakvjqjrvtt11zv3do73Z-hx2iit0jys0G2P39hNUTlmHknnOprEfggmo1a4DqaysmR8MyJyyDovTsDSEJU2Z8zn0DrqEejNb6PNojNLQ6NWSHkSIN8PDDRHcwf0iEQDDYFSgsi-dF35bBOzJrUZy--DBLcIKzar25UIrvI-ey816NXy48_riTgcU3NfZLcb9w5RDPkEBik6M7_z71AYw6F6f7Toex00yPAhxTx18jPAu3D8rigUmiySKMQnvL47RZvFvF4nYgPj2xVL55SoCis-L6VMlh8kP1eW9fanAvJO72H1uZGX3pimlU6ZiuSFbAcaOHalR-1fAYPqp-vb09NuTP9-eZXPwbi0rTUOsNsqayrSWuUJWzJq2yejC8CTLWtMoyZn_gV1x2g-XPwkn27H_Yo-nV_f_Z-_9ytVEK3Hiw-W98dH8nc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