<table border="1" cellspacing="0" cellpadding="8">
<tr>
<th>Issue</th>
<td>
<a href=https://github.com/llvm/llvm-project/issues/60351>60351</a>
</td>
</tr>
<tr>
<th>Summary</th>
<td>
clang generates incorrect code with bit shifts passed as function parameter
</td>
</tr>
<tr>
<th>Labels</th>
<td>
new issue
</td>
</tr>
<tr>
<th>Assignees</th>
<td>
</td>
</tr>
<tr>
<th>Reporter</th>
<td>
mtl1979
</td>
</tr>
</table>
<pre>
Source:
```
#include <stdlib.h>
#include <string.h>
int main()
{
int bits = 8;
char* a = (char*)malloc(1 << bits);
char* b = (char*)malloc(1 << bits);
memcpy(b, a, 1 << bits);
return 0;
}
```
Generated assembler code:
```
main: # @main
.cfi_startproc
# %bb.0:
pushq %rbp
.cfi_def_cfa_offset 16
.cfi_offset %rbp, -16
movq %rsp, %rbp
.cfi_def_cfa_register %rbp
subq $32, %rsp
movl $0, -4(%rbp)
movl $8, -8(%rbp)
movl -8(%rbp), %ecx
movl $1, %eax
# kill: def $cl killed $ecx
shll %cl, %eax
movslq %eax, %rdi
callq malloc@PLT
movq %rax, -16(%rbp)
movl -8(%rbp), %ecx
movl $1, %eax
# kill: def $cl killed $ecx
shll %cl, %eax
movslq %eax, %rdi
callq malloc@PLT
movq %rax, -24(%rbp)
movq -24(%rbp), %rdi
movq -16(%rbp), %rsi
movl -8(%rbp), %ecx
movl $1, %eax
# kill: def $cl killed $ecx
shll %cl, %eax
movslq %eax, %rdx
callq memcpy@PLT
xorl %eax, %eax
addq $32, %rsp
popq %rbp
.cfi_def_cfa %rsp, 8
retq
```
The relevant part is:
```
shll %cl, %eax
movslq %eax, %rdi
```
It should zero-extend, not sign-extend as bit shift is unsigned operation. It's also promoting after the shift which can possibly cause overflow, instead it should promote before the shift. In ideal world, one could use `UINTPTR_C()` to force promoting before the cast, but none of the common compilers understand it.
clang version: Ubuntu clang version 15.0.2-1
</pre>
<img width="1px" height="1px" alt="" src="http://email.email.llvm.org/o/eJysVk1v4zYQ_TX0ZRCBor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