Bug 96928 - Failure to optimize one's complement abs pattern
Summary: Failure to optimize one's complement abs pattern
Status: RESOLVED FIXED
Alias: None
Product: gcc
Classification: Unclassified
Component: tree-optimization (show other bugs)
Version: 11.0
: P3 normal
Target Milestone: ---
Assignee: Jakub Jelinek
URL:
Keywords: missed-optimization
Depends on:
Blocks: 19987
  Show dependency treegraph
 
Reported: 2020-09-03 18:25 UTC by Gabriel Ravier
Modified: 2021-05-16 22:10 UTC (History)
0 users

See Also:
Host:
Target:
Build:
Known to work:
Known to fail:
Last reconfirmed: 2021-01-04 00:00:00


Attachments
gcc11-pr96928.patch (2.04 KB, patch)
2021-01-04 14:55 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-09-03 18:25:54 UTC
int f(int a)
{
    return (a < 0) ? ~a : a;
}

This can be optimized to `return (a >> 31) ^ a;`. This transformation is done by LLVM, but not by GCC.
Comment 1 Jakub Jelinek 2021-01-04 14:55:13 UTC
Created attachment 49874 [details]
gcc11-pr96928.patch

Untested fix.
Comment 2 GCC Commits 2021-01-05 15:36:32 UTC
The master branch has been updated by Jakub Jelinek <jakub@gcc.gnu.org>:

https://gcc.gnu.org/g:576714b309b330df0e80e34114bcdf0bba35e146

commit r11-6472-g576714b309b330df0e80e34114bcdf0bba35e146
Author: Jakub Jelinek <jakub@redhat.com>
Date:   Tue Jan 5 16:35:22 2021 +0100

    phiopt: Optimize x < 0 ? ~y : y to (x >> 31) ^ y [PR96928]
    
    As requested in the PR, the one's complement abs can be done more
    efficiently without cmov or branching.
    
    Had to change the ifcvt-onecmpl-abs-1.c testcase, we no longer optimize
    it in ifcvt, on x86_64 with -m32 we generate in the end the exact same
    code, but with -m64:
            movl    %edi, %eax
    -       notl    %eax
    -       cmpl    %edi, %eax
    -       cmovl   %edi, %eax
    +       sarl    $31, %eax
    +       xorl    %edi, %eax
            ret
    
    2021-01-05  Jakub Jelinek  <jakub@redhat.com>
    
            PR tree-optimization/96928
            * tree-ssa-phiopt.c (xor_replacement): New function.
            (tree_ssa_phiopt_worker): Call it.
    
            * gcc.dg/tree-ssa/pr96928.c: New test.
            * gcc.target/i386/ifcvt-onecmpl-abs-1.c: Remove -fdump-rtl-ce1,
            instead of scanning rtl dump for ifcvt message check assembly
            for xor instruction.
Comment 3 Jakub Jelinek 2021-01-05 15:37:07 UTC
Fixed.
Comment 4 Andrew Pinski 2021-05-16 22:10:17 UTC
Note while moving this optimization to match-and-simplify I noticed that the gimple produced is:
(~a) ^ b

But this get changed around to:
~(a ^ b)
By PRE latter on.

I only noticed this because the testcase is failing as it depends being that.