From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mx2.suse.de (mx2.suse.de [195.135.220.15]) by sourceware.org (Postfix) with ESMTPS id 1D99A383E804 for ; Sat, 5 Dec 2020 10:20:14 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.3.2 sourceware.org 1D99A383E804 Authentication-Results: sourceware.org; dmarc=none (p=none dis=none) header.from=suse.de Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=rguenther@suse.de X-Virus-Scanned: by amavisd-new at test-mx.suse.de Received: from relay2.suse.de (unknown [195.135.221.27]) by mx2.suse.de (Postfix) with ESMTP id E5681AB63; Sat, 5 Dec 2020 10:20:12 +0000 (UTC) Date: Sat, 05 Dec 2020 11:20:11 +0100 User-Agent: K-9 Mail for Android In-Reply-To: <20201205091025.GX3788@tucnak> References: <20201205091025.GX3788@tucnak> MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: quoted-printable Subject: Re: [PATCH] phiopt: Improve conditional_replacement for x ? 0 : -1 [PR796232] To: Jakub Jelinek CC: gcc-patches@gcc.gnu.org From: Richard Biener Message-ID: X-Spam-Status: No, score=-5.2 required=5.0 tests=BAYES_00, KAM_DMARC_STATUS, RCVD_IN_MSPIKE_H3, RCVD_IN_MSPIKE_WL, SPF_HELO_NONE, SPF_PASS, TXREP autolearn=ham autolearn_force=no version=3.4.2 X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on server2.sourceware.org X-BeenThere: gcc-patches@gcc.gnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Gcc-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Sat, 05 Dec 2020 10:20:15 -0000 On December 5, 2020 10:10:25 AM GMT+01:00, Jakub Jelinek wrote: >Hi! > >As mentioned in the PR, for boolean x we currently optimize >in phiopt x ? 0 : -1 into -(int)!x but it can be optimized as >(int) x - 1 which is one less operation both in GIMPLE and in x86 >assembly=2E > >Bootstrapped/regtested on x86_64-linux and i686-linux, ok for trunk? > >And/or, shall we have a match=2Epd optimization to turn that -(type)!x >for BOOLEAN_TYPE (or other 1 bit unsigned precision values) into >(type) - 1=2E I think that would make sense=2E Does that then cover the phiopt case dire= ctly?=20 >2020-12-05 Jakub Jelinek > > PR tree-optimization/96232 > * tree-ssa-phiopt=2Ec (conditional_replacement): Optimize > x ? 0 : -1 as (int) x - 1 rather than -(int)!x=2E > > * gcc=2Edg/tree-ssa/pr96232-1=2Ec: New test=2E > >--- gcc/tree-ssa-phiopt=2Ec=2Ejj 2020-11-04 11:58:58=2E670252748 +0100 >+++ gcc/tree-ssa-phiopt=2Ec 2020-12-04 17:27:53=2E472837921 +0100 >@@ -827,10 +827,24 @@ conditional_replacement (basic_block con >=20 > if (neg) > { >- cond =3D fold_convert_loc (gimple_location (stmt), >- TREE_TYPE (result), cond); >- cond =3D fold_build1_loc (gimple_location (stmt), >- NEGATE_EXPR, TREE_TYPE (cond), cond); >+ if (TREE_CODE (cond) =3D=3D TRUTH_NOT_EXPR >+ && INTEGRAL_TYPE_P (TREE_TYPE (nonzero_arg))) >+ { >+ /* x ? 0 : -1 is better optimized as (int) x - 1 than >+ -(int)!x=2E */ >+ cond =3D fold_convert_loc (gimple_location (stmt), >+ TREE_TYPE (result), >+ TREE_OPERAND (cond, 0)); >+ cond =3D fold_build2_loc (gimple_location (stmt), PLUS_EXPR, >+ TREE_TYPE (result), cond, nonzero_arg); >+ } >+ else >+ { >+ cond =3D fold_convert_loc (gimple_location (stmt), >+ TREE_TYPE (result), cond); >+ cond =3D fold_build1_loc (gimple_location (stmt), >+ NEGATE_EXPR, TREE_TYPE (cond), cond); >+ } > } > else if (shift) > { >--- gcc/testsuite/gcc=2Edg/tree-ssa/pr96232-1=2Ec=2Ejj 2020-12-04 >17:32:40=2E607615276 +0100 >+++ gcc/testsuite/gcc=2Edg/tree-ssa/pr96232-1=2Ec 2020-12-04 >17:33:09=2E914286354 +0100 >@@ -0,0 +1,11 @@ >+/* PR tree-optimization/96232 */ >+/* { dg-do compile } */ >+/* { dg-options "-O2 -fdump-tree-optimized" } */ >+/* { dg-final { scan-tree-dump " \\+ -1;" "optimized" } } */ >+/* { dg-final { scan-tree-dump-not "~x_\[0-9]*\\\(D\\\)" "optimized" } >} */ >+ >+int >+foo (_Bool x) >+{ >+ return x ? 0 : -1; >+} > > Jakub