Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

a ne/eq (a ne/eq 0) pattern is not simplified #65073

Closed
k-arrows opened this issue Aug 29, 2023 · 1 comment · Fixed by #65852
Closed

a ne/eq (a ne/eq 0) pattern is not simplified #65073

k-arrows opened this issue Aug 29, 2023 · 1 comment · Fixed by #65852

Comments

@k-arrows
Copy link

Such a simple pattern may already be mentioned in some existing issues, but I couldn't find a duplicate. (If there is, please feel free to close this issue as duplicate.)

https://godbolt.org/z/rvEaKjE4E

bool f1(int a)
{
    return a != (a == 0);
}

bool f2(int a)
{
    return a != (a != 0);
}

bool f3(int a)
{
    return a == (a == 0);
}

bool f4(int a)
{
    return a == (a != 0);
}

GCC

f1(int):
        mov     eax, 1
        ret
f2(int):
        cmp     edi, 1
        seta    al
        ret
f3(int):
        xor     eax, eax
        ret
f4(int):
        cmp     edi, 1
        setbe   al
        ret

Clang

f1(int):                                 # @f1(int)
        xor     eax, eax
        test    edi, edi
        sete    al
        cmp     eax, edi
        setne   al
        ret
f2(int):                                 # @f2(int)
        xor     eax, eax
        test    edi, edi
        setne   al
        cmp     eax, edi
        setne   al
        ret
f3(int):                                 # @f3(int)
        xor     eax, eax
        test    edi, edi
        sete    al
        cmp     eax, edi
        sete    al
        ret
f4(int):                                 # @f4(int)
        xor     eax, eax
        test    edi, edi
        setne   al
        cmp     eax, edi
        sete    al
        ret

FYI
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=110293

@dtcxzyw
Copy link
Member

dtcxzyw commented Aug 29, 2023

https://alive2.llvm.org/ce/z/6bDMrD

@dtcxzyw dtcxzyw self-assigned this Aug 30, 2023
dtcxzyw added a commit that referenced this issue Oct 6, 2023
…65852)

This patch folds the pattern `a ne/eq (zext/sext (a ne/eq c))` into a boolean constant or a compare.
Clang vs GCC: https://godbolt.org/z/4ro817WE8
Proof for `zext`: https://alive2.llvm.org/ce/z/6z9NRF
Proof for `sext`: https://alive2.llvm.org/ce/z/tv5wuE
Fixes #65073.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants