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

    <tr>
        <th>Summary</th>
        <td>
            instcombine generating incorrect range metadata for a ctpop
        </td>
    </tr>

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

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

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

<pre>
    instcombine takes this code:

```llvm
define i32 @f(i32 %0) {
  %2 = lshr i32 2048, %0
  %3 = call i32 @llvm.ctpop.i32(i32 %2)
  %4 = freeze i32 %3
  ret i32 %4
}

; Function Attrs: nocallback nofree nosync nounwind speculatable willreturn memory(none)
declare i32 @llvm.ctpop.i32(i32) #0
```

and turns it into:

```llvm
define i32 @f(i32 %0) {
  %2 = lshr i32 2048, %0
  %.fr = freeze i32 %2
  %3 = call i32 @llvm.ctpop.i32(i32 %.fr), !range !0
  ret i32 %3
}

; Function Attrs: nocallback nofree nosync nounwind speculatable willreturn memory(none)
declare i32 @llvm.ctpop.i32(i32) #0

attributes #0 = { nocallback nofree nosync nounwind speculatable willreturn memory(none) }

!0 = !{i32 0, i32 13}
```

but the inferred 0..13 range is too tight. to see this, consider that %2 can easily be poison, this happens when %0 is uge 32. then, freeze(poison) can be any arbitrary value, and then the popcount can be in the range 0..32.

cc @nunoplopes 
</pre>
<img width="1px" height="1px" alt="" src="http://email.email.llvm.org/o/eJzUVduO4ygQ_Rr8UhoLF7nYD37oTJT_wLhis4PBgvK0Ml-_Ane2e1qtfVittFopShB1O-dUUdEp2ckT9eJ4EcdrpTeeQ-wjTTTHagjjo7c-sQnLYD0B6x-UgGebwISRhHoR8irk8_sk949zP5f9aqR7jrMKQRzkXWBbjniUAjsQ58vuBvkKQagruDTH4o_y0Ar8vju_e6niZbRzz6y5Wm14DWttFb6XQIHdh8BDCbxHol9vgPConvZI_Lw7vJE5X3_jpi5w27xhGzy8MMck1Av4kIEM2vwAH3Jq8CE9vAEfNv9q_QhpJbM5zXpwBK_WuUi8RQ8LLSE-BLY-ePoL6EjG6Uh_x6zohkp-kvwjVu1HyEUSWAbrOfwXfarv8QvB8R91sr7HLFEp0kTtJ8oH-UXz1P-qeXu3mKMdNqZUTEUUcb78i_DgsxzYvJXBRpwvGa_M4uZDo96dv5qtYWPgmcD6O8VII8i6bhTsTbEJOARgO81cAwdIRGVb5Owm-GRHisCz5n2OjPZAOln3gIFgDTYFn13Lgpn1upJP8DqTL8OV028TgcI6Qyie-3QJbJ_BXUk6EGj_AB0Hy1HHB_zUbqMcUN5GzphJrGE1YfP8jLH79U5G1rXC-iN3Y3Jf_ebD6sJKCaqxV2OnOl1R35zOqunOzamr5n6kUZNuzqMx5ihJHZQ8KYNdo8axObZU2R4lKtnItjkdO-xqMt3Q6uZ8MmdpTqeTOEhatHV1GaMQp8qmtFF_7Nq2rZweyKWytBEXm0xYVut0nm-BKPC7QCyPW72Ele1ifxVb-mz8sNuz6XitYp9N34ZtSnmGbeL0DoEtO_rtD2EiT1Gz9RNYb0KMZPhNv4VYj5o13EMEDeUlVFt0_cy85gco8CbwNlmet6E2YRF4K7D2n29rDH-QYYG3wjwJvBXyfwYAAP__zD3-fA">