Skip to content

Missing optimization: fold max(b, a + 1) to b when a < b #75155

Closed
@XChy

Description

@XChy

Alive2 proof: https://alive2.llvm.org/ce/z/V82mt-

Description:

long src(long a, long b) {
    if(a < b){
        return max(b, a + 1);
    }else{
        return 0;
    }
}

can be folded to:

long tgt(long a, long b) {
    if(a < b){
        return b;
    }else{
        return 0;
    }
}

Similarly, it also applies to other cases like a > b, min(b, a + 1) and so on.

Real-world motivation

This snippet of IR is derived from redis/src/db.c@readGetKeys (after O3 pipeline).
The example above is a reduced version. If you're interested in the original suboptimal IR and optimal IR, see also:https://godbolt.org/z/YaEYnedYW

Let me know if you can confirm that it's an optimization opportunity, thanks.

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions