Skip to content

[InstCombine] !(A & INT_MIN) && A & INT_MAX is not folded to signed comparison with zero #69123

Closed
@artem

Description

@artem

https://godbolt.org/z/rxPnbEnoT

void fn();

void test(unsigned *d) {
    int is_negative = *d & 0x80000000;
    
    if (!is_negative && *d & 0x7fffffff) {
        fn();
    }
}

GCC recognizes this pattern and folds it to comparison

test:
        mov     eax, DWORD PTR [rdi]
        test    eax, eax
        jg      .L4
        ret
.L4:
        xor     eax, eax
        jmp     fn

However, LLVM 18 main fails to optimize it:

test:
        mov     eax, dword ptr [rdi]
        test    eax, eax
        js      .LBB0_2
        and     eax, 2147483647
        je      .LBB0_2
        xor     eax, eax
        jmp     fn@PLT
.LBB0_2:
        ret

Metadata

Metadata

Assignees

Labels

llvm:analysisIncludes value tracking, cost tables and constant foldingllvm:instcombineCovers the InstCombine, InstSimplify and AggressiveInstCombine passesmissed-optimization

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions