Skip to content

[llvm] Miscompilation of freeze instruction with llc -O1 #144780

Open
@8051Enthusiast

Description

@8051Enthusiast

If my understanding of the freeze instruction is correct, given n, the following code should always return a number that is of the form n + 2 * k where k can be anything.

define i32 @preserve_parity(i32 %n) {
    %2 = freeze i32 poison
    %3 = add i32 %n, %2
    %4 = add i32 %3, %2
    ret i32 %4
}

However, for example on x86_64 llc (with optimization level at least 1) outputs the following code:

preserve_parity:                        # @preserve_parity
        mov     eax, edi
        add     eax, eax
        add     eax, eax
        ret

Given that this always outputs an even number, but n + 2 * k is always odd for odd n, this function seems to behave incorrectly for odd n.
This does not only happen on x86_64, but a bunch of other backends too: https://godbolt.org/z/nzM4Mo98v.

Metadata

Metadata

Assignees

No one assigned

    Type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions