From patchwork Wed Mar 6 13:13:42 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jakub Jelinek X-Patchwork-Id: 1908810 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=VRdMpmEr; 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 4TqXvR4V20z1yX0 for ; Thu, 7 Mar 2024 00:14:10 +1100 (AEDT) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 63656385801C for ; Wed, 6 Mar 2024 13:14:08 +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 BDF623858C3A for ; Wed, 6 Mar 2024 13:13:48 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org BDF623858C3A 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 BDF623858C3A 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=1709730830; cv=none; b=ptKOc27QBSqWnWqeUcspbWcI9eBdV/whVHdUoeuKEEA4LQpPhRwioBZ7a4xKmWH5O2vgXPFvPN5A/zsa/08xnYVttAEjMnLFPGjEwKN55j89eryILsdoP1BKqR9VkXlZFHQ8Feu1CggX4k+TTfR+I7mpLFBrIhYcLSyxxXOs0o4= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1709730830; c=relaxed/simple; bh=8di0NFy4OHZydoREMaCBRyU5PFsz2WO4d5H30mIWXpE=; h=DKIM-Signature:Date:From:To:Subject:Message-ID:MIME-Version; b=q1GfgIRy4zhTH2o9XM0vEI3yzEKacH55lKtMUo5O4j1KPj5+U3/EwFp16dO2GkttNGS/cJ+Vd3qXYIkiXOspujlx7BkIGdEchjA5pNxKAAS9VIpM46vfVX3yklyr3GVwwSf0msTdgaXyYLKYV6Ha3loNTpGxwv4PfgLTPGStTjs= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1709730828; 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:in-reply-to:in-reply-to: references:references; bh=wX380PPEHqHJJp7njJJuvegjw62cU4vg2aw5dhFaNfs=; b=VRdMpmErmvf4ii+rwYbNjZ8xjqilcNL6z0MQlgg+1FJf2OoLXYfZ2I1ZZkxUviDLO20sAT OqI/bbUTyF/QBzxYKELPF75nCNeV8iNp4Aa5C1mx3XfCnHuBBOw+dG7A4AiJMjWDIW5jwC 8gUHg7asW1TA7+5GuqWzloNdpZDl07I= Received: from mimecast-mx02.redhat.com (mimecast-mx02.redhat.com [66.187.233.88]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-345-l3_r_IrnPACeHPwKgTB-5w-1; Wed, 06 Mar 2024 08:13:45 -0500 X-MC-Unique: l3_r_IrnPACeHPwKgTB-5w-1 Received: from smtp.corp.redhat.com (int-mx03.intmail.prod.int.rdu2.redhat.com [10.11.54.3]) (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 AC912185A784; Wed, 6 Mar 2024 13:13:44 +0000 (UTC) Received: from tucnak.zalov.cz (unknown [10.45.226.25]) by smtp.corp.redhat.com (Postfix) with ESMTPS id 723C2111DCFF; Wed, 6 Mar 2024 13:13:44 +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 426DDhZQ3777080 (version=TLSv1.3 cipher=TLS_AES_256_GCM_SHA384 bits=256 verify=NOT); Wed, 6 Mar 2024 14:13:43 +0100 Received: (from jakub@localhost) by tucnak.zalov.cz (8.17.1/8.17.1/Submit) id 426DDgM63777079; Wed, 6 Mar 2024 14:13:42 +0100 Date: Wed, 6 Mar 2024 14:13:42 +0100 From: Jakub Jelinek To: Richard Biener Cc: gcc-patches@gcc.gnu.org Subject: [PATCH] match.pd, v2: Optimize a * !a to 0 [PR114009] Message-ID: References: MIME-Version: 1.0 In-Reply-To: X-Scanned-By: MIMEDefang 3.4.1 on 10.11.54.3 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Disposition: inline X-Spam-Status: No, score=-3.6 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 On Wed, Mar 06, 2024 at 11:45:42AM +0100, Richard Biener wrote: > OK, though feel free to add ARG_UNUSED to 'captures' as well. Ok, done below. > I think the INTEGRAL_TYPE_P should be redundant - the pattern > should work for vectors and complex as well (with integer components > which integer_zerop constrains). Guess you're right. I think it is correct as is for both complex and int vector, while float etc. shouldn't appear there because of the integer_zerop and pointer because we don't do multiplication on pointer types. That said, it won't really actually match the vector int case actually emitted, where we have a VEC_COND_EXPR, added those as new patterns. And it doesn't match what is emitted for _Complex, though I think it isn't that important to simplify that. _Complex int freddy (_Complex int x) { return x * !x; } _Complex int garply (_Complex int x) { return x * (x == 0); } results in original in: return COMPLEX_EXPR > == 0 && IMAGPART_EXPR > == 0, 0> * x; and return (x == __complex__ (0, 0) ? __complex__ (1, 0) : __complex__ (0, 0)) * x; but after gimplification it is already something that is really too hard to optimize. So, is the following still ok if it passes bootstrap/regtest? 2024-03-06 Jakub Jelinek PR tree-optimization/114009 * genmatch.cc (decision_tree::gen): Emit ARG_UNUSED for captures argument even for GENERIC, not just for GIMPLE. * match.pd (a * !a -> 0): New simplifications. * gcc.dg/tree-ssa/pr114009.c: New test. Jakub --- gcc/genmatch.cc.jj 2024-02-26 10:09:16.594613314 +0100 +++ gcc/genmatch.cc 2024-03-06 13:47:35.001458638 +0100 @@ -4071,7 +4071,7 @@ decision_tree::gen (vec &files, for (unsigned i = 0; i < as_a (s->s->s->match)->ops.length (); ++i) fp_decl (f, " tree ARG_UNUSED (_p%d),", i); - fp_decl (f, " tree *captures"); + fp_decl (f, " tree *ARG_UNUSED (captures)"); } for (unsigned i = 0; i < s->s->s->for_subst_vec.length (); ++i) { --- gcc/match.pd.jj 2024-03-06 09:34:36.608271525 +0100 +++ gcc/match.pd 2024-03-06 13:58:27.400588289 +0100 @@ -1219,6 +1219,17 @@ (define_operator_list SYNC_FETCH_AND_AND && 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))) + { build_zero_cst (type); }) +(simplify + (mult:c @0 (vec_cond (eq @0 integer_zerop) @1 integer_zerop)) + { build_zero_cst (type); }) +(simplify + (mult:c @0 (vec_cond (ne @0 integer_zerop) integer_zerop @1)) + { 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-06 13:44:33.481924326 +0100 +++ gcc/testsuite/gcc.dg/tree-ssa/pr114009.c 2024-03-06 14:04:09.724918390 +0100 @@ -0,0 +1,33 @@ +/* PR tree-optimization/114009 */ +/* { dg-do compile } */ +/* { dg-options "-O2 -Wno-psabi -fdump-tree-forwprop1" } */ +/* { dg-final { scan-tree-dump-times " return 0;" 3 "forwprop1" } } */ +/* { dg-final { scan-tree-dump-times " (?:return| =) { 0, 0, 0, 0 };" 1 "forwprop1" } } */ + +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; +} + +typedef int V __attribute__((vector_size (4 * sizeof (int)))); + +V +qux (V x) +{ + return x * (x == 0); +}