Skip to content

Optimisation difference between !x and x == false #70655

Open
@karwa

Description

@karwa

Description

No response

Reproduction

for a in 0...Int8.max {
    for b in 0...Int8.max {
        let x = a.subtractingReportingOverflow(b).overflow
        precondition(!x)
    }
}

When written this way, the compiler knows that x is always false, and optimises away the entire loop. However, if I change the precondition to:

precondition(x == false)

Suddenly it seems unable to optimise this code. Both are compiled with -O.

Check it out on this Godbolt

Expected behavior

When x is a Bool, !x and x == false are equivalent, so I expect the optimiser to handle them equally well.

Environment

Swift version 5.9 (swift-5.9-RELEASE)
Target: x86_64-unknown-linux-gnu

Godbolt link provided above for trying this version of the compiler on this target.

Additional information

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    SILOptimizerArea → compiler: SIL optimization passesbugA deviation from expected or documented behavior. Also: expected but undesirable behavior.compilerThe Swift compiler itselfrun-time performanceswift 5.9unexpected behaviorBug: Unexpected behavior or incorrect output

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions