| Issue |
243052
|
| Summary |
[SROA] `a = c ? b : a` on a small struct is copied through the stack
|
| Labels |
missed-optimization,
llvm:transforms
|
| Assignees |
|
| Reporter |
chfast
|
```cpp
struct U { unsigned long w[2]; };
U assign(U a, U b, bool c)
{
a = c ? b : a;
return a;
}
U init(U a, U b, bool c)
{
U r = c ? b : a;
return r;
}
```
`clang++ -O2` (x86-64), https://godbolt.org/z/1G3b31c9r:
```asm
assign:
movq %rdi, -40(%rsp)
movq %rsi, -32(%rsp)
movq %rdx, -16(%rsp)
movq %rcx, -8(%rsp)
testl %r8d, %r8d
leaq -16(%rsp), %rax
leaq -40(%rsp), %rcx
cmovneq %rax, %rcx
movups (%rcx), %xmm0
movaps %xmm0, -40(%rsp)
movq -40(%rsp), %rax
movq -32(%rsp), %rdx
retq
init:
movq %rdi, %rax
testl %r8d, %r8d
cmovneq %rdx, %rax
cmovneq %rcx, %rsi
movq %rsi, %rdx
retq
```
`assign` should be the same as `init`. Instead both values go to the stack, a `cmov` picks an address, and a 16-byte load copies the words back. That load also fails store-to-load forwarding from the 8-byte stores.
Reduced IR (https://godbolt.org/z/3dGvP4Wsz):
```llvm
define { i64, i64 } @self(i64 %a0, i64 %a1, i64 %b0, i64 %b1, i1 %c) {
%a = alloca [2 x i64], align 8
%b = alloca [2 x i64], align 8
store i64 %a0, ptr %a, align 8
%a.hi = getelementptr inbounds i8, ptr %a, i64 8
store i64 %a1, ptr %a.hi, align 8
store i64 %b0, ptr %b, align 8
%b.hi = getelementptr inbounds i8, ptr %b, i64 8
store i64 %b1, ptr %b.hi, align 8
%p = select i1 %c, ptr %b, ptr %a
call void @llvm.memcpy.p0.p0.i64(ptr align 8 %a, ptr align 8 %p, i64 16, i1 false)
%r0 = load i64, ptr %a, align 8
%r1 = load i64, ptr %a.hi, align 8
%v = insertvalue { i64, i64 } poison, i64 %r0, 0
%r = insertvalue { i64, i64 } %v, i64 %r1, 1
ret { i64, i64 } %r
}
define { i64, i64 } @other(i64 %a0, i64 %a1, i64 %b0, i64 %b1, i1 %c) {
%a = alloca [2 x i64], align 8
%b = alloca [2 x i64], align 8
%d = alloca [2 x i64], align 8
store i64 %a0, ptr %a, align 8
%a.hi = getelementptr inbounds i8, ptr %a, i64 8
store i64 %a1, ptr %a.hi, align 8
store i64 %b0, ptr %b, align 8
%b.hi = getelementptr inbounds i8, ptr %b, i64 8
store i64 %b1, ptr %b.hi, align 8
%p = select i1 %c, ptr %b, ptr %a
call void @llvm.memcpy.p0.p0.i64(ptr align 8 %d, ptr align 8 %p, i64 16, i1 false)
%r0 = load i64, ptr %d, align 8
%d.hi = getelementptr inbounds i8, ptr %d, i64 8
%r1 = load i64, ptr %d.hi, align 8
%v = insertvalue { i64, i64 } poison, i64 %r0, 0
%r = insertvalue { i64, i64 } %v, i64 %r1, 1
ret { i64, i64 } %r
}
```
`opt -passes=sroa` turns `@other` into two selects and leaves `@self` unchanged. The only difference is the memcpy destination: in `@self` it is `%a`, which is also an operand of the select.
The select's user is the memcpy, not a load or store, so SROA gives up on `%a` and `%b` (`hasUnsafePHIOrSelectUse()`). In `@other`, SROA first rewrites `%d`, which splits the memcpy into loads from `%p` and stores to `%d`. After that the select has only load users and is speculated, and `%a` and `%b` are promoted. In `@self` the destination is `%a` itself, so nothing ever splits the memcpy.
Found in intx (multi-precision integers): a 3/2 division step conditionally adds the divisor to a 2-word remainder.
_______________________________________________
llvm-bugs mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-bugs