Skip to content

bcmp/memcmp removal optimization should remove unneeded allocas #52701

Open
@scottmcm

Description

@scottmcm

Example from rust-lang/rust#91838: https://godbolt.org/z/9h83ezxvj
Demonstration that more opt -O3 doesn't help: https://llvm.godbolt.org/z/qdanMeEar
Codegen repro via llc trunk: https://llvm.godbolt.org/z/oxMh6fEjq

It's excellent that short, known-length memcmp can just be mov+cmp in codegen.

But, unfortunately, if one of the sides of the comparison was passed directly (not via pointer), the alloca into which it was written to be able to call memcmp sticks around, resulting in generated assembly that writes the argument to stack then immediately reads it again:

demo_before:
        mov     dword ptr [rsp - 4], edx
        cmp     rsi, 4
        jne     .LBB0_1
        mov     eax, dword ptr [rdi]
        cmp     eax, dword ptr [rsp - 4]
        sete    al
        ret
.LBB0_1:
        xor     eax, eax
        ret

It would be nice if it could instead be

	cmp	rsi, 4
	jne	.LBB1_1
	cmp	dword ptr [rdi], edx
	sete	al
	ret

.LBB1_1:
	xor	eax, eax
	ret

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions