llvm / llvm/llvm-project

[X86][SROA] memcpy of `i128` not optimized out

Open
#194,158 1 comment 0 reactions 0 assignees View on GitHub
llvm:optimizations missed-optimization
Dominant language
LLVM
Stars
40.5k
Forks
18.7k
PR merge metrics
PR metrics pending

Description

Given

https://godbolt.org/z/PGc51qhcT

```llvm
define i128 @typed(ptr %src, ptr %other, i1 %c) {
%tmp = alloca i128, align 16
br i1 %c, label %a, label %b

a:
call void @llvm.memcpy.p0.p0.i64(ptr %tmp, ptr %src, i64 16, i1 false)
br label %b

b:
%p = phi ptr [ %tmp, %a ], [ %other, %0 ]
%v = load i128, ptr %p, align 16
ret i128 %v
}

define i128 @bytes(ptr %src, ptr %other, i1 %c) {
%tmp = alloca [16 x i8], align 16
br i1 %c, label %a, label %b

a:
call void @llvm.memcpy.p0.p0.i64(ptr %tmp, ptr %src, i64 16, i1 false)
br label %b

b:
%p = phi ptr [ %tmp, %a ], [ %other, %0 ]
%v = load i128, ptr %p, align 16
ret i128 %v
}
```

The only difference here is the type of the `alloca`. In the latter case the memcpy is not optimized out

```assembly
typed:
test dl, 1
cmove rdi, rsi
mov rax, qword ptr [rdi]
mov rdx, qword ptr [rdi + 8]
ret

bytes:
test dl, 1
je .LBB1_2
movups xmm0, xmmword ptr [rdi]
movaps xmmword ptr [rsp - 24], xmm0
lea rsi, [rsp - 24]
.LBB1_2:
mov rax, qword ptr [rsi]
mov rdx, qword ptr [rsi + 8]
ret
```

The branch/phi appears to be important.

This might be related to https://github.com/llvm/llvm-project/issues/164308, but their example is quite messy, so hopefully a smaller reproducer helps in any case.

Contributor guide

Open the contributing guide

Research direction

Start with the Godbolt reproducer and compare the typed and bytes functions through the X86 SROA optimization path, focusing on the branch and phi. Check related issue 164308 for context; done means the i128 memcpy is optimized out in the bytes case without regressing the typed case.

Written by the indexing model from the issue text.

Assessment

Domain
compilers
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Quiet
Clarity
Mostly clear
Newbie friendliness
48/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.