From patchwork Thu Apr 30 13:05:38 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jakub Jelinek X-Patchwork-Id: 1280389 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=gcc.gnu.org (client-ip=8.43.85.97; helo=sourceware.org; envelope-from=gcc-patches-bounces@gcc.gnu.org; receiver=) Authentication-Results: ozlabs.org; dmarc=none (p=none dis=none) header.from=gcc.gnu.org Authentication-Results: ozlabs.org; dkim=pass (1024-bit key; unprotected) header.d=gcc.gnu.org header.i=@gcc.gnu.org header.a=rsa-sha256 header.s=default header.b=dR5qC2An; dkim-atps=neutral Received: from sourceware.org (server2.sourceware.org [8.43.85.97]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id 49CbHX3NxFz9sSd for ; Thu, 30 Apr 2020 23:06:30 +1000 (AEST) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 8C2AA389852C; Thu, 30 Apr 2020 13:06:27 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 8C2AA389852C DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1588251987; bh=mRyCTFr/kL4QeXkl42hzIKsMOGBH7PWK/Xe+uZnVY5k=; h=Date:To:Subject:List-Id:List-Unsubscribe:List-Archive:List-Post: List-Help:List-Subscribe:From:Reply-To:Cc:From; b=dR5qC2AnnZU019LVofQ40UvJ3DU9D6gs9vHNyMajOhwtaHENMjma6BE3curaON2/z 8YzBxyDhBOD0EZs3kMAny6k/DqPiz5VhdD/h1awQdovkBAS4aSTISwDKWKr3pV3X6f qM3C06pBAuEH4o5YQsaYw6c/NT1R97SrnZG62vXk= X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from us-smtp-delivery-1.mimecast.com (us-smtp-delivery-1.mimecast.com [205.139.110.120]) by sourceware.org (Postfix) with ESMTP id 0221E3892007 for ; Thu, 30 Apr 2020 13:06:25 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.3.2 sourceware.org 0221E3892007 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-72-dA67OF7SOzW5G1eVNQ5TCw-1; Thu, 30 Apr 2020 09:06:23 -0400 X-MC-Unique: dA67OF7SOzW5G1eVNQ5TCw-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 744D68463A3; Thu, 30 Apr 2020 13:05:42 +0000 (UTC) Received: from tucnak.zalov.cz (ovpn-112-104.ams2.redhat.com [10.36.112.104]) by smtp.corp.redhat.com (Postfix) with ESMTPS id 18E685EE06; Thu, 30 Apr 2020 13:05:41 +0000 (UTC) Received: from tucnak.zalov.cz (localhost [127.0.0.1]) by tucnak.zalov.cz (8.15.2/8.15.2) with ESMTP id 03UD5d4H030386; Thu, 30 Apr 2020 15:05:39 +0200 Received: (from jakub@localhost) by tucnak.zalov.cz (8.15.2/8.15.2/Submit) id 03UD5cc2030385; Thu, 30 Apr 2020 15:05:38 +0200 Date: Thu, 30 Apr 2020 15:05:38 +0200 To: Richard Biener Subject: [PATCH] match.pd: Optimize (x < 0) != (y < 0) into (x ^ y) < 0 [PR94718] Message-ID: <20200430130538.GW2424@tucnak> MIME-Version: 1.0 User-Agent: Mutt/1.11.3 (2019-02-01) X-Scanned-By: MIMEDefang 2.79 on 10.5.11.15 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Disposition: inline X-Spam-Status: No, score=-16.3 required=5.0 tests=BAYES_00, DKIMWL_WL_HIGH, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_2, GIT_PATCH_3, RCVD_IN_DNSWL_NONE, 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-Patchwork-Original-From: Jakub Jelinek via Gcc-patches From: Jakub Jelinek Reply-To: Jakub Jelinek Cc: gcc-patches@gcc.gnu.org Errors-To: gcc-patches-bounces@gcc.gnu.org Sender: "Gcc-patches" Hi! The following patch (on top of the two other PR94718 patches) performs the actual optimization requested in the PR. Bootstrapped/regtested on x86_64-linux and i686-linux, ok for stage1? 2020-04-30 Jakub Jelinek PR tree-optimization/94718 * match.pd ((X < 0) != (Y < 0) into (X ^ Y) < 0): New simplification. * gcc.dg/tree-ssa/pr94718-4.c: New test. * gcc.dg/tree-ssa/pr94718-5.c: New test. Jakub --- gcc/match.pd.jj 2020-04-30 12:43:10.473982154 +0200 +++ gcc/match.pd 2020-04-30 13:10:11.305134082 +0200 @@ -4358,6 +4358,28 @@ (define_operator_list COND_TERNARY (cmp (bit_and:cs @0 @2) (bit_and:cs @1 @2)) (cmp (bit_and (bit_xor @0 @1) @2) { build_zero_cst (TREE_TYPE (@2)); }))) +/* (X < 0) != (Y < 0) into (X ^ Y) < 0. + (X >= 0) != (Y >= 0) into (X ^ Y) < 0. + (X < 0) == (Y < 0) into (X ^ Y) >= 0. + (X >= 0) == (Y >= 0) into (X ^ Y) >= 0. */ +(for cmp (eq ne) + ncmp (ge lt) + (for sgncmp (ge lt) + (simplify + (cmp (sgncmp @0 integer_zerop@2) (sgncmp @1 integer_zerop)) + (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0)) + && !TYPE_UNSIGNED (TREE_TYPE (@0)) + && types_match (@0, @1)) + (ncmp (bit_xor @0 @1) @2))))) +(for cmp (eq ne) + ncmp (lt ge) + (simplify + (cmp:c (lt @0 integer_zerop@2) (ge @1 integer_zerop)) + (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0)) + && !TYPE_UNSIGNED (TREE_TYPE (@0)) + && types_match (@0, @1)) + (ncmp (bit_xor @0 @1) @2)))) + /* If we have (A & C) == C where C is a power of 2, convert this into (A & C) != 0. Similarly for NE_EXPR. */ (for cmp (eq ne) --- gcc/testsuite/gcc.dg/tree-ssa/pr94718-4.c.jj 2020-04-30 12:56:00.552606387 +0200 +++ gcc/testsuite/gcc.dg/tree-ssa/pr94718-4.c 2020-04-30 13:12:57.320699373 +0200 @@ -0,0 +1,61 @@ +/* PR tree-optimization/94718 */ +/* { dg-do compile } */ +/* { dg-options "-O2 -fno-ipa-icf -fdump-tree-optimized" } */ +/* { dg-final { scan-tree-dump-times "= \[xy]_\[0-9]+\\\(D\\\) \\^ \[xy]_\[0-9]+\\\(D\\\);" 8 "optimized" } } */ +/* { dg-final { scan-tree-dump-times "\[0-9]+ < 0;" 8 "optimized" } } */ + +int +f1 (int x, int y) +{ + return (x < 0) != (y < 0); +} + +int +f2 (int x, int y) +{ + return (x >= 0) != (y >= 0); +} + +int +f3 (int x, int y) +{ + return (x < 0) == (y >= 0); +} + +int +f4 (int x, int y) +{ + return (x >= 0) == (y < 0); +} + +int +f5 (int x, int y) +{ + int s = (x < 0); + int t = (y < 0); + return s != t; +} + +int +f6 (int x, int y) +{ + int s = (x >= 0); + int t = (y >= 0); + return s != t; +} + +int +f7 (int x, int y) +{ + int s = (x < 0); + int t = (y >= 0); + return s == t; +} + +int +f8 (int x, int y) +{ + int s = (x >= 0); + int t = (y < 0); + return s == t; +} --- gcc/testsuite/gcc.dg/tree-ssa/pr94718-5.c.jj 2020-04-30 13:01:44.893558240 +0200 +++ gcc/testsuite/gcc.dg/tree-ssa/pr94718-5.c 2020-04-30 13:14:00.434768470 +0200 @@ -0,0 +1,61 @@ +/* PR tree-optimization/94718 */ +/* { dg-do compile } */ +/* { dg-options "-O2 -fno-ipa-icf -fdump-tree-optimized" } */ +/* { dg-final { scan-tree-dump-times "= \[xy]_\[0-9]+\\\(D\\\) \\^ \[xy]_\[0-9]+\\\(D\\\);" 8 "optimized" } } */ +/* { dg-final { scan-tree-dump-times "\[0-9]+ >= 0;" 8 "optimized" } } */ + +int +f1 (int x, int y) +{ + return (x < 0) == (y < 0); +} + +int +f2 (int x, int y) +{ + return (x >= 0) == (y >= 0); +} + +int +f3 (int x, int y) +{ + return (x < 0) != (y >= 0); +} + +int +f4 (int x, int y) +{ + return (x >= 0) != (y < 0); +} + +int +f5 (int x, int y) +{ + int s = (x < 0); + int t = (y < 0); + return s == t; +} + +int +f6 (int x, int y) +{ + int s = (x >= 0); + int t = (y >= 0); + return s == t; +} + +int +f7 (int x, int y) +{ + int s = (x < 0); + int t = (y >= 0); + return s != t; +} + +int +f8 (int x, int y) +{ + int s = (x >= 0); + int t = (y < 0); + return s != t; +}