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

    <tr>
        <th>Summary</th>
        <td>
            LLVM 14 Regression: Invalid ctlz result on riscv64
        </td>
    </tr>

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

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

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

<pre>
    Minimal reduction:
```llvm
; ModuleID = 'test'
source_filename = "test"
target datalayout = "e-m:e-p:64:64-i64:64-i128:128-n64-S128"
target triple = "riscv64-unknown-linux-unknown"

; Function Attrs: nofree nosync nounwind readnone speculatable willreturn
declare i15 @llvm.ctlz.i15(i15 %0, i1 immarg %1) #2

; Function Attrs: nobuiltin noredzone nounwind
define external fastcc void @"_start"() unnamed_addr #1 {
Entry:
  %value = alloca i15, align 2
  store i15 -16384, i15* %value, align 2
  call fastcc void @foo()
  %0 = load i15, i15* %value, align 2
  %1 = call i15 @llvm.ctlz.i15(i15 %0, i1 false)
  %2 = zext i15 %1 to i64
  call i64 asm sideeffect "ecall", "={x10},{x17},{x10},~{memory}"(i64 94, i64 %2)
  unreachable
}

; Function Attrs: nobuiltin noredzone nounwind
define internal fastcc void @foo() unnamed_addr #1 {
Entry:
  ret void
}

attributes #1 = { nobuiltin noredzone nounwind "target-features"="+m"}
attributes #2 = { nofree nosync nounwind readnone speculatable willreturn }
```

This program exits with 0 on LLVM 13.0.1, but with 7 on LLVM 14.0.5 and trunk
```
$ llc-13 test.ll -o test.o -filetype=obj -mtriple=riscv64-linux-none -O0
$ ld.lld test.o -o test && qemu-riscv64 ./test
$ echo $?
0
$ llc-14 test.ll -o test.o -filetype=obj -mtriple=riscv64-linux-none -O0
$ ld.lld test.o -o test && qemu-riscv64 ./test
$ echo $?
7
```

</pre>
<img width="1px" height="1px" alt="" src="http://email.email.llvm.org/o/eJzNV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