From mboxrd@z Thu Jan  1 00:00:00 1970
Return-Path: <gcc-patches-return-456761-listarch-gcc-patches=gcc.gnu.org@gcc.gnu.org>
Received: (qmail 36341 invoked by alias); 24 Jun 2017 18:51:27 -0000
Mailing-List: contact gcc-patches-help@gcc.gnu.org; run by ezmlm
Precedence: bulk
List-Id: <gcc-patches.gcc.gnu.org>
List-Archive: <http://gcc.gnu.org/ml/gcc-patches/>
List-Post: <mailto:gcc-patches@gcc.gnu.org>
List-Help: <mailto:gcc-patches-help@gcc.gnu.org>
Sender: gcc-patches-owner@gcc.gnu.org
Received: (qmail 36331 invoked by uid 89); 24 Jun 2017 18:51:26 -0000
Authentication-Results: sourceware.org; auth=none
X-Virus-Found: No
X-Spam-SWARE-Status: No, score=-1.7 required=5.0 tests=AWL,BAYES_00,FREEMAIL_FROM,RCVD_IN_DNSWL_NONE,RCVD_IN_SORBS_SPAM,SPF_PASS autolearn=no version=3.3.2 spammy=site
X-HELO: mail-yb0-f170.google.com
Received: from mail-yb0-f170.google.com (HELO mail-yb0-f170.google.com) (209.85.213.170) by sourceware.org (qpsmtpd/0.93/v0.84-503-g423c35a) with ESMTP; Sat, 24 Jun 2017 18:51:25 +0000
Received: by mail-yb0-f170.google.com with SMTP id b81so1677842yba.2        for <gcc-patches@gcc.gnu.org>; Sat, 24 Jun 2017 11:51:24 -0700 (PDT)
X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed;        d=1e100.net; s=20161025;        h=x-gm-message-state:mime-version:in-reply-to:references:from:date         :message-id:subject:to;        bh=9f3kOR3v5/bcD8Wzkmu0AWD/1jW6j4PP1NLKqneOIWU=;        b=i5nWl57gqIOXGFa7CIZzh0456jhBMQqQN60hoE9h/lG+bHRS8O2oKfkYdUiOedg6UQ         eWoiKncqUXDAJB8GekQUpEQ8IuDnMdrPz5fzUrVwfykRNF4S/5oQIGXpw5xTXT7kDdmR         8PUHcGMGbzs8BamUAgbBRPCM9GMWucr5QpNJTDFAqY0MIQERDTw0iVPj3GA9jfIhszBq         AgrCh0zKjdFc41MDs1rS2eBnsg7l1AUH3308xawfUetRT/8oGzIOqF0WLuIIhkoRLdSN         VCsId32SDnAqM0lsFXrnAGIAPXy7HhK0yzsyZ0FY1NUqntE1aEDX/+V+ablp4jLHM9CJ         cFJw==
X-Gm-Message-State: AKS2vOz6EXcFSA5hVtyK7BuvmKUpsSFP3iPjRPEyo2pDRG/Mr8jk9/uc	z88XOo4C6jcB/ScmrwjFbAmguQ+HJw==
X-Received: by 10.37.176.68 with SMTP id e4mr9889500ybj.26.1498330283021; Sat, 24 Jun 2017 11:51:23 -0700 (PDT)
MIME-Version: 1.0
Received: by 10.129.47.200 with HTTP; Sat, 24 Jun 2017 11:51:22 -0700 (PDT)
In-Reply-To: <alpine.DEB.2.20.1706240811460.2551@stedding.saclay.inria.fr>
References: <CA+=Sn1mWYuz8h5zBcNqwT2LG9N4B3DueNdoZHmyb4DRREFhwQQ@mail.gmail.com> <alpine.DEB.2.20.1706240811460.2551@stedding.saclay.inria.fr>
From: Andrew Pinski <pinskia@gmail.com>
Date: Sat, 24 Jun 2017 18:51:00 -0000
Message-ID: <CA+=Sn1=RRnWQuyxFh5_Vd2HzS9CCLuvYPkofLefuBUiTiH4wfA@mail.gmail.com>
Subject: Re: [PATCH] fold a * (a > 0 ? 1 : -1) to abs(a) and related optimizations
To: GCC Patches <gcc-patches@gcc.gnu.org>
Content-Type: text/plain; charset="UTF-8"
X-IsSubscribed: yes
X-SW-Source: 2017-06/txt/msg01856.txt.bz2

On Fri, Jun 23, 2017 at 11:50 PM, Marc Glisse <marc.glisse@inria.fr> wrote:
> On Fri, 23 Jun 2017, Andrew Pinski wrote:
>
>> Hi,
>>  I saw this on llvm's review site (https://reviews.llvm.org/D34579)
>> and I thought why not add it to GCC.  I expanded more than what was
>> done on the LLVM patch.
>>
>> I added the following optimizations:
>> Transform X * (X > 0 ? 1 : -1) into ABS(X).
>> Transform X * (X >= 0 ? 1 : -1) into ABS(X).
>> Transform X * (X > 0.0 ? 1.0 : -1.0) into ABS(X).
>> Transform X * (X >= 0.0 ? 1.0 : -1.0) into ABS(X).
>> Transform X * (X > 0 ? -1 : 1) into -ABS(X).
>> Transform X * (X >= 0 ? -1 : 1) into -ABS(X).
>> Transform X * (X > 0.0 ? -1.0 : 1.0) into -ABS(X).
>> Transform X * (X >= 0.0 ? -1.0 : 1.0) into -ABS(X).
>> Transform X * (X < 0 ? 1 : -1) into -ABS(X).
>> Transform X * (X <= 0 ? 1 : -1) into -ABS(X).
>> Transform X * (X < 0.0 ? 1.0 : -1.0) into -ABS(X).
>> Transform X * (X <= 0.0 ? 1.0 : -1.0) into -ABS(X).
>> Transform X * (X < 0 ? -1 : 1) into ABS(X).
>> Transform X * (X <= 0 ? -1 : 1) into ABS(X).
>> Transform X * (X < 0.0 ? -1.0 : 1.0) into ABS(X).
>> Transform X * (X <= 0.0 ? -1.0 : 1.0) into ABS(X).
>>
>> The floating points ones only happen when not honoring SNANS and not
>> honoring signed zeros.
>
>
> Some random comments (not a review):
>
> * if X is NaN, we may get a qNaN with the wrong sign bit. We probably don't
> care much though...

Ok, I changed it to when not honoring NANs.

>
> * I am surprised (X<0.?-1.:1.) and copysign(1., X) remain different for the
> whole optimization pipeline with -ffast-math. X*copysign(1., X) is another
> candidate to become fabs(X).

This might be a better idea because of ...

>
> * Whenever you get -ABS(X) for integers, what about the case where X is
> INT_MIN?

This.  Yes this is an issue; I guess I need to rethink the integer patterns.


>
> * I guess we can't get there with an unsigned type because X>0 would have
> become X!=0 .

No, unsigned is not an issue.

>
> * I wonder if we could use something like
>
> (for cmp (gt ge lt le)
>      outp (convert convert negate negate)
>      outn (negate negate convert convert)
> [...]
>   (outp (abs @0))
>
> to reduce duplication or if that would be less readable.


I did thought of that but I added the lt/le parts latter on.

>
> * Some of the cases are handled by PRE turning
>
>   # iftmp.0_1 = PHI <1.0e+0(5), -1.0e+0(3)>
>   _3 = iftmp.0_1 * a_2(D);
>
> into
>
>   _5 = -a_2(D);
> [...]
>   # iftmp.0_1 = PHI <1.0e+0(2), -1.0e+0(3)>
>   # prephitmp_6 = PHI <a_2(D)(2), _5(3)>
>
> which phiopt3 can handle (quite late).
>
> * With cond, this currently (?) only affects generic, so I am not sure it
> will hit very often... But it will be there if someone later writes a
> match.pd->phiopt generator ;-)

I have a start of this patch but I have not finished it yet.  Both
phiopt and ifcombine should be moved over to gimple match and
simplify.
I will submit a new patch which implements some of the above by the
end of the day; I might split up the patch into two (one for the
integer case and one for the floating point case).

Thanks,
Andrew

>
> --
> Marc Glisse