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

    <tr>
        <th>Summary</th>
        <td>
            [DAG] Unnecessary zero-extension of promoted lzcnt
        </td>
    </tr>

    <tr>
      <th>Labels</th>
      <td>
            backend:X86,
            missed-optimization,
            llvm:SelectionDAG
      </td>
    </tr>

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

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

<pre>
    https://alive2.llvm.org/ce/z/V_9Uoc

```ll
define i32 @src(i8  %value)  {
  %ext = zext i8 %value to i32
  %lz = tail call i32 @llvm.ctlz.i32(i32 %ext, i1 false)
  %sub = add nsw i32 %lz, -24
  ret i32 %sub
}

define  i32 @tgt(i8  %value)  {
  %lz = tail call i8 @llvm.ctlz.i8(i8 %value, i1 false)
  %ext = zext i8 %lz to i32
  ret i32 %ext
}
```
llc -mcpu=btver2
```asm
src: # @src
        movzbl  %dil, %eax
 lzcntl  %eax, %eax
        addl    $-24, %eax
        retq
tgt: # @tgt
        movzbl  %dil, %eax
 lzcntl  %eax, %eax
        addl    $-24, %eax
        movzbl  %al, %eax
        retq
```
In tgt, value tracking should understand that the `lzcnt(zext(x)) - 24` is guaranteed not to be negative, and is already zero-extended
</pre>
<img width="1px" height="1px" alt="" src="http://email.email.llvm.org/o/eJzEVEGPqzYQ_jXmMkpExiGBA4e8RVRVb316VW-VsWeJ-4yd2ibN49dXBrIbZbvqsQiFMPMx830z4xEh6N4S1az4woomE2M8O1__-stXPTibdU79qM8xXgLjJ4Ytw1YYfSXcGnMdts73DFtJDNuJYfvbH9U3J1nesPy0_h7y5TZmMSh61ZZAcwS2z4OXDEtdAjAsrsKMxLACYMcvC3q20y0C4w1M6Y8u36AQXYrzgDTTDIxCG5DCmHuamauMZtomPJazeQ7M8AX0Dl6FCSn1Q6wwdnMwoRTY8Des35gpfbLB_R3qKd59YexW1cfmsQir5jub2Mf_Fv1RSvmkpFyCvMf4TMm_1M9MT8V7UJGq8qTi3sTl1RgJm0FeRsabLl7J4xNMhGGxpPbyEzDk92av6ZZrcNepMzNHpU0SkNKL24oyk7RxcSfjs3u9hFImPRnuU1c-QXmKfy2WVP13Tunt_-L0kEp8zPSB-FMXfrYwD9ILrIfBC_ld2x7C2Y1GwWgV-RCFVRDPIkI8E6RzmAQwLKd5-MtbGhWsYAO4Z4ccdIB-FF7YSKTAupjmpCOw1Iuor_OUpZA6gDCehPoBE3m3oVskq0hlquaq4pXIqN4dscBjWVWH7FwfMOdSHWQlRC4o5-lWhB2q6lgdd12ma8xxn1c57jCvimJbdHyf73hR8YKQF69sn9MgtHnbPJkOYaR6lx-q4yEzoiMT5jWG2An5naxi_PR7eWCIDF8Y4qBDILVxl6gHPYmonX3zpaCMn76SIZkczemn5CuazNfJt-nGPqQDqEMM7xSijmbenQlfNPDNWpIUgvCPhQnaWXCvcPFucJHUMkbZ6M3Tbu11PI_dVrqBYTtTWh6bi3d_kowM21l0YNiuuq81_hMAAP__dtGiLw">