From patchwork Wed Mar 6 08:28:12 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jakub Jelinek X-Patchwork-Id: 1908588 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@legolas.ozlabs.org Authentication-Results: legolas.ozlabs.org; dkim=pass (1024-bit key; unprotected) header.d=redhat.com header.i=@redhat.com header.a=rsa-sha256 header.s=mimecast20190719 header.b=hF+J5Wj1; dkim-atps=neutral Authentication-Results: legolas.ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=gcc.gnu.org (client-ip=2620:52:3:1:0:246e:9693:128c; helo=server2.sourceware.org; envelope-from=gcc-patches-bounces+incoming=patchwork.ozlabs.org@gcc.gnu.org; receiver=patchwork.ozlabs.org) Received: from server2.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 ECDSA (secp384r1) server-digest SHA384) (No client certificate requested) by legolas.ozlabs.org (Postfix) with ESMTPS id 4TqQbF1jPgz1yX4 for ; Wed, 6 Mar 2024 19:29:45 +1100 (AEDT) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id EAAD1385801C for ; Wed, 6 Mar 2024 08:29:42 +0000 (GMT) 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 [170.10.129.124]) by sourceware.org (Postfix) with ESMTPS id 94A1C38582B6 for ; Wed, 6 Mar 2024 08:28:18 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 94A1C38582B6 Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=redhat.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=redhat.com ARC-Filter: OpenARC Filter v1.0.0 sourceware.org 94A1C38582B6 Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=170.10.129.124 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1709713704; cv=none; b=NFp2yCpylZqFp1R3q1bSDVA6pmXcLQH0G2gQq9L5ZaZQDW31/eBakitzkBjc45s0/uRFe8+s+PjncfgbytWfffSMfgmolTV9+a978Y2Sg7at7/Y0sMMMsjRKnDnr/UZtLyeYLcHo1nphQJD/yHfFw0xX/odo39DXxo6mvO8lbqQ= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1709713704; c=relaxed/simple; bh=d4TuEavCJ2S0vEGGpEQgPXi3R79NIaDM1EAT9qQyY7Q=; h=DKIM-Signature:Date:From:To:Subject:Message-ID:MIME-Version; b=V/gYriY8jzkOp1aFGyn7GwXebRfjZaocOY4de9Elc859iqI+ob70Jx2oL5cYXurqt1reYV1zPgAAK3m0Hk32ar5FxTMtbDzt1q6HC5KWInTNwW+pd3PCIcou7mMyaQsXdAhP7pcyheDOtf0JqwjLnMZcFiy4qWI5B711VynIk5g= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1709713698; h=from:from:reply-to:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-type; bh=5eldcOYAttxpJzBxE2Yv46HpU4FuYnHrgCsF7lYrWP4=; b=hF+J5Wj1zP0UeZUp+J5i3wM05auc2hasDUyptUawWbPN5EYWa48TWSS1njkOdgnQt0zQNy +N4CMn2y9cSXnO2LyPZl5FD83Rcq+Rime87S1cq/mXKbn6Kk1ND42mC2u3OrG+nMzMhNi9 yH3PgviS9oAPyYKIso3sJhP3tRVjQhs= Received: from mimecast-mx02.redhat.com (mx-ext.redhat.com [66.187.233.73]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-47-nVvMCRDuNFmi30ma3B26HA-1; Wed, 06 Mar 2024 03:28:15 -0500 X-MC-Unique: nVvMCRDuNFmi30ma3B26HA-1 Received: from smtp.corp.redhat.com (int-mx10.intmail.prod.int.rdu2.redhat.com [10.11.54.10]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id B29E33C0C88E; Wed, 6 Mar 2024 08:28:14 +0000 (UTC) Received: from tucnak.zalov.cz (unknown [10.45.226.25]) by smtp.corp.redhat.com (Postfix) with ESMTPS id 768DD492BFA; Wed, 6 Mar 2024 08:28:14 +0000 (UTC) Received: from tucnak.zalov.cz (localhost [127.0.0.1]) by tucnak.zalov.cz (8.17.1/8.17.1) with ESMTPS id 4268SCqq3644342 (version=TLSv1.3 cipher=TLS_AES_256_GCM_SHA384 bits=256 verify=NOT); Wed, 6 Mar 2024 09:28:13 +0100 Received: (from jakub@localhost) by tucnak.zalov.cz (8.17.1/8.17.1/Submit) id 4268SCD83644341; Wed, 6 Mar 2024 09:28:12 +0100 Date: Wed, 6 Mar 2024 09:28:12 +0100 From: Jakub Jelinek To: Richard Biener Cc: gcc-patches@gcc.gnu.org Subject: [PATCH] match.pd: Optimize a * !a to 0 [PR114009] Message-ID: MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.4.1 on 10.11.54.10 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Disposition: inline X-Spam-Status: No, score=-3.8 required=5.0 tests=BAYES_00, DKIMWL_WL_HIGH, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, RCVD_IN_DNSWL_NONE, SPF_HELO_NONE, SPF_NONE, TXREP, T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org X-BeenThere: gcc-patches@gcc.gnu.org X-Mailman-Version: 2.1.30 Precedence: list List-Id: Gcc-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Reply-To: Jakub Jelinek Errors-To: gcc-patches-bounces+incoming=patchwork.ozlabs.org@gcc.gnu.org Hi! The following patch attempts to fix an optimization regression through adding a simple simplification. We already have the /* (m1 CMP m2) * d -> (m1 CMP m2) ? d : 0 */ (if (!canonicalize_math_p ()) (for cmp (tcc_comparison) (simplify (mult:c (convert (cmp@0 @1 @2)) @3) (if (INTEGRAL_TYPE_P (type) && INTEGRAL_TYPE_P (TREE_TYPE (@0))) (cond @0 @3 { build_zero_cst (type); }))) optimization which otherwise triggers during the a * !a multiplication, but that is done only late and we aren't able through range assumptions optimize it yet anyway. The patch adds a specific simplification for it. If a is zero, then a * !a will be 0 * 1 (or for signed 1-bit 0 * -1) and so 0. If a is non-zero, then a * !a will be a * 0 and so again 0. I had to use a hack, TREE_TYPE (@0) in the condition as opposed to using just type, because otherwise I get a false positive warning. The captures parameter is marked ARG_UNUSED in gimple, but not in generic and we end up with tree generic_simplify_168 (location_t ARG_UNUSED (loc), const tree ARG_UNUSED (type), tree ARG_UNUSED (_p0), tree ARG_UNUSED (_p1), tree *captures) { const bool debug_dump = dump_file && (dump_flags & TDF_FOLDING); if (INTEGRAL_TYPE_P (type) ) { if (TREE_SIDE_EFFECTS (_p1)) goto next_after_fail309; if (UNLIKELY (!dbg_cnt (match))) goto next_after_fail309; { tree _r; _r = build_zero_cst (type); if (UNLIKELY (debug_dump)) generic_dump_logs ("match.pd", 216, __FILE__, __LINE__, true); return _r; } next_after_fail309:; } return NULL_TREE; } there. I've looked at similar cases and in (simplify (mod @0 integer_onep) { build_zero_cst (type); }) case we don't get captures unused because it is used a single time and therefore evaluate it for side-effects instead of punting on them, and in (pointer_diff @@0 @0) { build_zero_cst (type); }) we do so as well. Though, using @@0 instead of @0 on either operand of this simplification doesn't help. Bootstrapped/regtested on x86_64-linux and i686-linux, ok for trunk? 2024-03-06 Jakub Jelinek PR tree-optimization/114009 * match.pd (a * !a -> 0): New simplification. * gcc.dg/tree-ssa/pr114009.c: New test. Jakub --- gcc/match.pd.jj 2024-03-01 14:56:42.442810053 +0100 +++ gcc/match.pd 2024-03-05 22:53:25.202579435 +0100 @@ -1219,6 +1219,12 @@ DEFINE_INT_AND_FLOAT_ROUND_FN (RINT) && tree_nop_conversion_p (type, TREE_TYPE (@1))) (lshift @0 @2))) +/* Fold a * !a into 0. */ +(simplify + (mult:c @0 (convert? (eq @0 integer_zerop))) + (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))) + { build_zero_cst (type); })) + /* Shifts by precision or greater result in zero. */ (for shift (lshift rshift) (simplify --- gcc/testsuite/gcc.dg/tree-ssa/pr114009.c.jj 2024-03-05 15:18:41.274541636 +0100 +++ gcc/testsuite/gcc.dg/tree-ssa/pr114009.c 2024-03-05 15:16:09.056589675 +0100 @@ -0,0 +1,24 @@ +/* PR tree-optimization/114009 */ +/* { dg-do compile } */ +/* { dg-options "-O2 -fdump-tree-optimized" } */ +/* { dg-final { scan-tree-dump-times " return 0;" 3 "optimized" } } */ + +int +foo (int x) +{ + x = (x / 2) * 2; + return (!x) * x; +} + +int +bar (int x, int y) +{ + (void) x; + return y * !y; +} + +unsigned long long +baz (unsigned long long x) +{ + return (!x) * x; +}