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 1D12F3857037 for ; Fri, 15 Jan 2021 18:25:41 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.3.2 sourceware.org 1D12F3857037 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-236-t_MqJg_4OSSEjl1qFWGGKg-1; Fri, 15 Jan 2021 13:25:39 -0500 X-MC-Unique: t_MqJg_4OSSEjl1qFWGGKg-1 Received: from smtp.corp.redhat.com (int-mx01.intmail.prod.int.phx2.redhat.com [10.5.11.11]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id 37E0F9CDA0; Fri, 15 Jan 2021 18:25:38 +0000 (UTC) Received: from tucnak.zalov.cz (ovpn-112-64.ams2.redhat.com [10.36.112.64]) by smtp.corp.redhat.com (Postfix) with ESMTPS id C60D7620DE; Fri, 15 Jan 2021 18:25:37 +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 10FIPYvx3966840 (version=TLSv1.3 cipher=TLS_AES_256_GCM_SHA384 bits=256 verify=NOT); Fri, 15 Jan 2021 19:25:35 +0100 Received: (from jakub@localhost) by tucnak.zalov.cz (8.16.1/8.16.1/Submit) id 10FIPXdd3966839; Fri, 15 Jan 2021 19:25:33 +0100 Date: Fri, 15 Jan 2021 19:25:33 +0100 From: Jakub Jelinek To: Richard Biener Cc: gcc-patches@gcc.gnu.org Subject: [PATCH] match.pd: Optimize (x < 0) ^ (y < 0) to (x ^ y) < 0 etc. [PR96681] Message-ID: <20210115182533.GK1034503@tucnak> Reply-To: Jakub Jelinek MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.79 on 10.5.11.11 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=-6.4 required=5.0 tests=BAYES_00, DKIMWL_WL_HIGH, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, RCVD_IN_DNSWL_LOW, 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: Fri, 15 Jan 2021 18:25:42 -0000 Hi! This patch simplifies comparisons that test the sign bit xored together. If the comparisons are both < 0 or both >= 0, then we should xor the operands together and compare the result to < 0, if the comparisons are different, we should compare to >= 0. Bootstrapped/regtested on x86_64-linux and i686-linux, ok for trunk? 2021-01-15 Jakub Jelinek PR tree-optimization/96681 * match.pd ((x < 0) ^ (y < 0) to (x ^ y) < 0): New simplification. ((x >= 0) ^ (y >= 0) to (x ^ y) < 0): Likewise. ((x < 0) ^ (y >= 0) to (x ^ y) >= 0): Likewise. ((x >= 0) ^ (y < 0) to (x ^ y) >= 0): Likewise. * gcc.dg/tree-ssa/pr96681.c: New test. --- gcc/match.pd.jj 2021-01-15 13:12:11.232019067 +0100 +++ gcc/match.pd 2021-01-15 14:00:21.567135280 +0100 @@ -3993,6 +3993,24 @@ (define_operator_list COND_TERNARY (if (single_use (@2)) (cmp @0 @1))))) +/* Simplify (x < 0) ^ (y < 0) to (x ^ y) < 0 and + (x >= 0) ^ (y >= 0) to (x ^ y) < 0. */ +(for cmp (lt ge) + (simplify + (bit_xor (cmp:s @0 integer_zerop) (cmp:s @1 integer_zerop)) + (if (INTEGRAL_TYPE_P (TREE_TYPE (@0)) + && !TYPE_UNSIGNED (TREE_TYPE (@0)) + && types_match (TREE_TYPE (@0), TREE_TYPE (@1))) + (lt (bit_xor @0 @1) { build_zero_cst (TREE_TYPE (@0)); })))) +/* Simplify (x < 0) ^ (y >= 0) to (x ^ y) >= 0 and + (x >= 0) ^ (y < 0) to (x ^ y) >= 0. */ +(simplify + (bit_xor:c (lt:s @0 integer_zerop) (ge:s @1 integer_zerop)) + (if (INTEGRAL_TYPE_P (TREE_TYPE (@0)) + && !TYPE_UNSIGNED (TREE_TYPE (@0)) + && types_match (TREE_TYPE (@0), TREE_TYPE (@1))) + (ge (bit_xor @0 @1) { build_zero_cst (TREE_TYPE (@0)); }))) + /* Transform comparisons of the form X * C1 CMP 0 to X CMP 0 in the signed arithmetic case. That form is created by the compiler often enough for folding it to be of value. One example is in --- gcc/testsuite/gcc.dg/tree-ssa/pr96681.c.jj 2021-01-15 14:16:25.254366911 +0100 +++ gcc/testsuite/gcc.dg/tree-ssa/pr96681.c 2021-01-15 14:18:30.618941775 +0100 @@ -0,0 +1,35 @@ +/* PR tree-optimization/96681 */ +/* { dg-do compile } */ +/* { dg-options "-O2 -fdump-tree-optimized" } */ +/* { dg-final { scan-tree-dump-times " \\^ " 5 "optimized" } } */ +/* { dg-final { scan-tree-dump-times " (?:<|>=) 0" 5 "optimized" } } */ + +int +foo (int x, int y) +{ + return (x < 0) ^ (y < 0); +} + +int +bar (int x, int y) +{ + return (x > -1) ^ (y > -1); +} + +int +baz (int x, int y) +{ + return (x ^ y) < 0; +} + +int +qux (int x, int y) +{ + return (x ^ y) >= 0; +} + +int +corge (int x, int y) +{ + return (x >= 0) ^ (y < 0); +} Jakub