Bug 94786 - Missed min/max pattern using xor+and+less
Summary: Missed min/max pattern using xor+and+less
Status: RESOLVED FIXED
Alias: None
Product: gcc
Classification: Unclassified
Component: tree-optimization (show other bugs)
Version: 10.0
: P3 normal
Target Milestone: ---
Assignee: Jakub Jelinek
URL:
Keywords: easyhack, missed-optimization
Depends on:
Blocks:
 
Reported: 2020-04-27 08:11 UTC by Gabriel Ravier
Modified: 2020-05-08 08:56 UTC (History)
1 user (show)

See Also:
Host:
Target:
Build:
Known to work:
Known to fail:
Last reconfirmed: 2020-04-27 00:00:00


Attachments
gcc11-pr94786.patch (964 bytes, patch)
2020-05-07 16:44 UTC, Jakub Jelinek
Details | Diff

Note You need to log in before you can comment on or make changes to this bug.
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 CVS 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.