From patchwork Thu Dec 31 09:34:34 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jakub Jelinek X-Patchwork-Id: 1421513 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=2620:52:3:1:0:246e:9693:128c; helo=sourceware.org; envelope-from=gcc-patches-bounces@gcc.gnu.org; receiver=) Authentication-Results: ozlabs.org; dmarc=pass (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=VegDH0Jt; dkim-atps=neutral Received: from sourceware.org (server2.sourceware.org [IPv6:2620:52:3:1:0:246e:9693:128c]) (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 4D63066HKDz9sRf for ; Thu, 31 Dec 2020 20:34:46 +1100 (AEDT) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 1A9433857026; Thu, 31 Dec 2020 09:34:44 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 1A9433857026 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1609407284; bh=D3CXTCeDzOxE79fA7lmBeP5AcXchq4ncFS1XSnFfpXg=; h=Date:To:Subject:List-Id:List-Unsubscribe:List-Archive:List-Post: List-Help:List-Subscribe:From:Reply-To:Cc:From; b=VegDH0JtcUB5QE7TDrfqesIRBTKAzs3l9MYsfw2XFDNfLrGjbpb8As5l1/YQWF+Io OgA+Jw0ilyiRPxts/qAw7sp876ORkJAgvoNlfg1AWHf4rtNHww51olWQ0PSpVE+pYM TKkkSAgjipQCzjOaaCTI+co0fpGTtWg4RYrtpckc= X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [216.205.24.124]) by sourceware.org (Postfix) with ESMTP id 98237385800A for ; Thu, 31 Dec 2020 09:34:41 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.3.2 sourceware.org 98237385800A 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-237-xH8n3bYANISOST-1bQfKhQ-1; Thu, 31 Dec 2020 04:34:39 -0500 X-MC-Unique: xH8n3bYANISOST-1bQfKhQ-1 Received: from smtp.corp.redhat.com (int-mx03.intmail.prod.int.phx2.redhat.com [10.5.11.13]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id BF356107ACFB; Thu, 31 Dec 2020 09:34:37 +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 6ADDB190D0; Thu, 31 Dec 2020 09:34: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 0BV9YYXF3882097 (version=TLSv1.3 cipher=TLS_AES_256_GCM_SHA384 bits=256 verify=NOT); Thu, 31 Dec 2020 10:34:35 +0100 Received: (from jakub@localhost) by tucnak.zalov.cz (8.16.1/8.16.1/Submit) id 0BV9YYq73882096; Thu, 31 Dec 2020 10:34:34 +0100 Date: Thu, 31 Dec 2020 10:34:34 +0100 To: Richard Biener Subject: [PATCH] match.pd: Add (-(X < 0) | 1) * X -> abs (X) etc. simplifications [PR94785] Message-ID: <20201231093434.GD3788@tucnak> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.79 on 10.5.11.13 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Disposition: inline X-Spam-Status: No, score=-6.0 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_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-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 adds two simplifications to recognize idioms for ABS_EXPR resp. ABSU_EXPR. Bootstrapped/regtested on x86_64-linux and i686-linux, ok for trunk? 2020-12-31 Jakub Jelinek PR tree-optimization/94785 * match.pd ((-(X < 0) | 1) * X -> abs (X)): New simplification. ((-(X < 0) | 1U) * X -> absu (X)): Likewise. * gcc.dg/tree-ssa/pr94785.c: New test. Jakub --- gcc/match.pd.jj 2020-12-15 22:43:39.254276743 +0100 +++ gcc/match.pd 2020-12-30 12:00:00.924301784 +0100 @@ -1374,6 +1374,22 @@ (define_operator_list COND_TERNARY (absu tree_expr_nonnegative_p@0) (convert @0)) +/* Simplify (-(X < 0) | 1) * X into abs (X). */ +(simplify + (mult:c (bit_ior (negate (convert? (lt @0 integer_zerop))) integer_onep) @0) + (if (INTEGRAL_TYPE_P (type) && !TYPE_UNSIGNED (type)) + (abs @0))) + +/* Similarly (-(X < 0) | 1U) * X into absu (X). */ +(simplify + (mult:c (bit_ior (nop_convert (negate (convert? (lt @0 integer_zerop)))) + integer_onep) (nop_convert @0)) + (if (INTEGRAL_TYPE_P (type) + && TYPE_UNSIGNED (type) + && INTEGRAL_TYPE_P (TREE_TYPE (@0)) + && !TYPE_UNSIGNED (TREE_TYPE (@0))) + (absu @0))) + /* A few cases of fold-const.c negate_expr_p predicate. */ (match negate_expr_p INTEGER_CST --- gcc/testsuite/gcc.dg/tree-ssa/pr94785.c.jj 2020-12-30 12:03:45.693776141 +0100 +++ gcc/testsuite/gcc.dg/tree-ssa/pr94785.c 2020-12-30 12:04:26.895313170 +0100 @@ -0,0 +1,36 @@ +/* PR tree-optimization/94785 */ +/* { dg-do compile } */ +/* { dg-options "-O2 -fdump-tree-optimized" } */ +/* { dg-final { scan-tree-dump-times " = ABS_EXPR ;" 2 "optimized" } } */ +/* { dg-final { scan-tree-dump-times " = ABSU_EXPR ;" 2 "optimized" } } */ + +int +f1 (int v) +{ + return (1 | -(v < 0)) * v; +} + +unsigned +f2 (int v) +{ + return (1U | -(v < 0)) * v; +} + +int +f3 (int v) +{ + int a = v < 0; + int b = -a; + int c = 1 | b; + return c * v; +} + +unsigned +f4 (int v) +{ + int a = v < 0; + int b = -a; + unsigned c = b; + unsigned d = c | 1; + return d * v; +}