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

    <tr>
        <th>Summary</th>
        <td>
            Miscompile of  1 << ZEXT(CTTZ(X))
        </td>
    </tr>

    <tr>
      <th>Labels</th>
      <td>
            new issue
      </td>
    </tr>

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

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

<pre>
    The IR below miscompiles on x86 (at  least).  I believe the root cause is https://github.com/llvm/llvm-project/pull/85066 which performs the following optimization:

```
1 << CTTZ(X)   => -X & X
```

I think the ZEXT confuses things such that the value X=0 is incorrectly computed.

IR:

```
; ModuleID = '__module'
source_filename = "__module"
target datalayout = "e-m:e-p270:32:32-p271:32:32-p272:64:64-i64:64-i128:128-f80:128-n8:16:32:64-S128"
target triple = "x86_64-grtev4-linux-gnu"

define private i16 @src(i8 %_x1) {
entry:
  %_0 = call i8 @llvm.cttz.i8(i8 %_x1, i1 false)
  %_3 = zext i8 %_0 to i16
  %_4 = shl i16 1, %_3
  ret i16 %_4
}
```

Good llc built at 765206e050453018e861637a08a4520f29238074

```
./llc.good min.ll -o -
        .text
        .file   "__module"
        .p2align        4, 0x90 # -- Begin function src
        .type   .Lsrc,@function
.Lsrc: # @src
        .cfi_startproc
# %bb.0: # %entry
        orl     $256, %edi                      # imm = 0x100
        rep             bsfl    %edi, %ecx
        movl    $1, %eax
 # kill: def $cl killed $cl killed $ecx
        shll    %cl, %eax
                                        # kill: def $ax killed $ax killed $eax
        retq
.Lfunc_end0:
        .size   .Lsrc, .Lfunc_end0-.Lsrc
        .cfi_endproc
 # -- End function
        .section        ".note.GNU-stack","",@progbits
```

Bad llc built at 6f2c61071c274a1b5e212e6ad4114641ec7c7fc3

This produces 0 for input 0, result should be 0x100.

```
./llc.bad min.ll -o -
        .text
        .file   "__module"
        .p2align        4, 0x90 # -- Begin function src
        .type   .Lsrc,@function
.Lsrc: # @src
        .cfi_startproc
# %bb.0: # %entry
        movl    %edi, %eax
        negb    %al
        andb    %dil, %al
        movzbl  %al, %eax
 # kill: def $ax killed $ax killed $eax
        retq
.Lfunc_end0:
        .size   .Lsrc, .Lfunc_end0-.Lsrc
        .cfi_endproc
 # -- End function
        .section        ".note.GNU-stack","",@progbits
```


</pre>
<img width="1px" height="1px" alt="" src="http://email.email.llvm.org/o/eJzkVk1v2zgQ_TX0ZSCBor4PPuSjKQJs99DNAkEvBkWNJG4pUStSjpNfvyAtKU4bBN1zjYARh2_eDDVv7OHGyHZA3JP0mqS3Oz7bTk_7Hjtsml2l6-f9Q4dw_xUqVPoJemmE7kep0IAe4FRkQFjBLYBCbixhZQhw78ASjwi2Q5i0tiD4bBCkgc7a0ZD4irA7wu5aabu5CoXuCbtT6rj-C8ZJ_4PCEnY3zkoRdlekNMvgqZOigxGnRk-98fSNVko_yaEFPVrZyxdupR5cBHpL6LpmdPnz2whIfEPiG7h5ePhGWPFIWAkAJL4l8ScIHoGwDB7fdT2v92A7OXz3CXz79PgAQg_NbNB4e2vAzKID23HrIUeuZoRHEt9S9w7kIPQ0obDqGdzbnC3W4Rv6rx_nT-Jr-KLrWeH9rcsaCMsPh95bCMvPIKPnSeChkQoH3uOCY684dsZZPrVooeaWK_6sZ7siMehJfIXByHJK4quY-cVto7db95Qlfgnk9hCxgsRXESuCpqDL0-BN2eqeJcFfDvZDKnaSo9oSPhXZIUuCdrJ4TAIlh_kUtMO8OZ3XGhs5IIyTPHKLIKMMSELNJAgrZAGEpYdT5MpM8uuzBw52et7eM3gI9UEFVwqcU0KdGENh7Usoi7dMNyAjaLgySFh5yRF7jhc8WVjgFKx2GV2iEo8ynfKpej7vvGImtOdLOOxy0fz2A0l-1roGpQRUs1QWuIU8SxnNkKY0SWMaFVhkURbnnBY8SRltWMnigubJB0ILfT-KsHXkvRxCpSDQEKwuZWjxZF93TmyElu_JzB2PjCvZDoSWibswPZUUCIshCOAaW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