Bug 94786

Summary: Missed min/max pattern using xor+and+less
Product: gcc Reporter: Gabriel Ravier <gabravier>
Component: tree-optimizationAssignee: Jakub Jelinek <jakub>
Status: RESOLVED FIXED    
Severity: normal CC: gabravier, jakub
Priority: P3 Keywords: easyhack, missed-optimization
Version: 10.0   
Target Milestone: ---   
Host: Target:
Build: Known to work:
Known to fail: Last reconfirmed: 2020-04-27 00:00:00
Attachments: gcc11-pr94786.patch

Description Gabriel Ravier 2020-04-27 08:11:06 UTC
int r1(int x, int y)
{
    return y ^ ((x ^ y) & -(x < y));
}

int r2(int x, int y)
{
    return x ^ ((x ^ y) & -(x < y));
}

`r1` can be optimized to `min` and `r2` to `max`. This transformation is done by LLVM, but not by GCC.

Comparison here: https://godbolt.org/z/hNhkqM
Comment 1 Richard Biener 2020-04-27 10:29:57 UTC
Confirmed.
Comment 2 Jakub Jelinek 2020-05-07 16:31:15 UTC
Seems this is PR92834 just using ^ instead of -.
Comment 3 Jakub Jelinek 2020-05-07 16:44:40 UTC
Created attachment 48475 [details]
gcc11-pr94786.patch

Untested fix.
Comment 4 GCC Commits 2020-05-08 08:55:29 UTC
The master branch has been updated by Jakub Jelinek <jakub@gcc.gnu.org>:

https://gcc.gnu.org/g:1595a1cb7bfac8d5a6026d5d6f3a495be0391506

commit r11-198-g1595a1cb7bfac8d5a6026d5d6f3a495be0391506
Author: Jakub Jelinek <jakub@redhat.com>
Date:   Fri May 8 10:52:47 2020 +0200

    match.pd: A ^ ((A ^ B) & -(C cmp D)) -> (C cmp D) ? B : A simplification [PR94786]
    
    We already have x - ((x - y) & -(z < w)) and
    x + ((y - x) & -(z < w)) simplifications, this one adds
    x ^ ((x ^ y) & -(z < w)) (not merged using for because of the
    :c that can be present on bit_xor and can't on minus).
    
    2020-05-08  Jakub Jelinek  <jakub@redhat.com>
    
            PR tree-optimization/94786
            * match.pd (A ^ ((A ^ B) & -(C cmp D)) -> (C cmp D) ? B : A): New
            simplification.
    
            * gcc.dg/tree-ssa/pr94786.c: New test.
Comment 5 Jakub Jelinek 2020-05-08 08:56:30 UTC
Fixed.