From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [63.128.21.124]) by sourceware.org (Postfix) with ESMTP id BEC793857804 for ; Sat, 5 Dec 2020 09:10:33 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.3.2 sourceware.org BEC793857804 Received: from mimecast-mx01.redhat.com (mimecast-mx01.redhat.com [209.132.183.4]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-540-v1xHXOT8MjqV7R_xIc25bQ-1; Sat, 05 Dec 2020 04:10:31 -0500 X-MC-Unique: v1xHXOT8MjqV7R_xIc25bQ-1 Received: from smtp.corp.redhat.com (int-mx05.intmail.prod.int.phx2.redhat.com [10.5.11.15]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id E16FE180A08A; Sat, 5 Dec 2020 09:10:29 +0000 (UTC) Received: from tucnak.zalov.cz (ovpn-112-11.ams2.redhat.com [10.36.112.11]) by smtp.corp.redhat.com (Postfix) with ESMTPS id 7C5505D6D3; Sat, 5 Dec 2020 09:10:29 +0000 (UTC) Received: from tucnak.zalov.cz (localhost [127.0.0.1]) by tucnak.zalov.cz (8.16.1/8.16.1) with ESMTPS id 0B59AQ7c1214152 (version=TLSv1.3 cipher=TLS_AES_256_GCM_SHA384 bits=256 verify=NOT); Sat, 5 Dec 2020 10:10:26 +0100 Received: (from jakub@localhost) by tucnak.zalov.cz (8.16.1/8.16.1/Submit) id 0B59APuO1214151; Sat, 5 Dec 2020 10:10:25 +0100 Date: Sat, 5 Dec 2020 10:10:25 +0100 From: Jakub Jelinek To: Richard Biener Cc: gcc-patches@gcc.gnu.org Subject: [PATCH] phiopt: Improve conditional_replacement for x ? 0 : -1 [PR796232] Message-ID: <20201205091025.GX3788@tucnak> Reply-To: Jakub Jelinek MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.79 on 10.5.11.15 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Type: text/plain; charset=us-ascii Content-Disposition: inline X-Spam-Status: No, score=-5.8 required=5.0 tests=BAYES_00, DKIMWL_WL_HIGH, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, RCVD_IN_DNSWL_NONE, RCVD_IN_MSPIKE_H4, 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 09:10:34 -0000 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. Bootstrapped/regtested on x86_64-linux and i686-linux, ok for trunk? And/or, shall we have a match.pd optimization to turn that -(type)!x for BOOLEAN_TYPE (or other 1 bit unsigned precision values) into (type) - 1. 2020-12-05 Jakub Jelinek PR tree-optimization/96232 * tree-ssa-phiopt.c (conditional_replacement): Optimize x ? 0 : -1 as (int) x - 1 rather than -(int)!x. * gcc.dg/tree-ssa/pr96232-1.c: New test. --- gcc/tree-ssa-phiopt.c.jj 2020-11-04 11:58:58.670252748 +0100 +++ gcc/tree-ssa-phiopt.c 2020-12-04 17:27:53.472837921 +0100 @@ -827,10 +827,24 @@ conditional_replacement (basic_block con if (neg) { - cond = fold_convert_loc (gimple_location (stmt), - TREE_TYPE (result), cond); - cond = fold_build1_loc (gimple_location (stmt), - NEGATE_EXPR, TREE_TYPE (cond), cond); + if (TREE_CODE (cond) == TRUTH_NOT_EXPR + && INTEGRAL_TYPE_P (TREE_TYPE (nonzero_arg))) + { + /* x ? 0 : -1 is better optimized as (int) x - 1 than + -(int)!x. */ + cond = fold_convert_loc (gimple_location (stmt), + TREE_TYPE (result), + TREE_OPERAND (cond, 0)); + cond = fold_build2_loc (gimple_location (stmt), PLUS_EXPR, + TREE_TYPE (result), cond, nonzero_arg); + } + else + { + cond = fold_convert_loc (gimple_location (stmt), + TREE_TYPE (result), cond); + cond = fold_build1_loc (gimple_location (stmt), + NEGATE_EXPR, TREE_TYPE (cond), cond); + } } else if (shift) { --- gcc/testsuite/gcc.dg/tree-ssa/pr96232-1.c.jj 2020-12-04 17:32:40.607615276 +0100 +++ gcc/testsuite/gcc.dg/tree-ssa/pr96232-1.c 2020-12-04 17:33:09.914286354 +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