From patchwork Thu Oct 24 15:12:49 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Richard Biener X-Patchwork-Id: 2001789 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=suse.de header.i=@suse.de header.a=rsa-sha256 header.s=susede2_rsa header.b=rEIZbh+v; dkim=pass header.d=suse.de header.i=@suse.de header.a=ed25519-sha256 header.s=susede2_ed25519 header.b=DJU30h4+; dkim=pass (1024-bit key) header.d=suse.de header.i=@suse.de header.a=rsa-sha256 header.s=susede2_rsa header.b=PEKUKpdJ; dkim=neutral header.d=suse.de header.i=@suse.de header.a=ed25519-sha256 header.s=susede2_ed25519 header.b=kVlJ8i5g; dkim-atps=neutral Authentication-Results: legolas.ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=gcc.gnu.org (client-ip=8.43.85.97; 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 [8.43.85.97]) (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 4XZ8Yl30g3z1xtp for ; Fri, 25 Oct 2024 02:13:15 +1100 (AEDT) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id A291E385840B for ; Thu, 24 Oct 2024 15:13:13 +0000 (GMT) X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from smtp-out2.suse.de (smtp-out2.suse.de [195.135.223.131]) by sourceware.org (Postfix) with ESMTPS id 019513858D28 for ; Thu, 24 Oct 2024 15:12:51 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 019513858D28 Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=suse.de Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=suse.de ARC-Filter: OpenARC Filter v1.0.0 sourceware.org 019513858D28 Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=195.135.223.131 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1729782774; cv=none; b=ICf//akVlNEFIV058Qez4d/jABypZIFnnifEIK6Julz3Aafg6HLE3zSmXv3me6ntUmC95svpTFTdhhZxK4tZvPCx19T1l2V5xFkYk+p+0ninxb2EZcH+ufGM6plnFGa3250JSVmgaBQTAlhamPfcdLoJj6rwJ2JRQ7BPUjgJnaw= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1729782774; c=relaxed/simple; bh=7W3I78jBF2HWwg+Tge+kRzBmyv7LmMJL0HTWTHI5PpU=; h=DKIM-Signature:DKIM-Signature:DKIM-Signature:DKIM-Signature:Date: From:To:Subject:MIME-Version:Message-Id; b=eIrk9Z15LrOIqUx4liX5VHFUY0cCrL1+6/h5fEHSOmfzrj71Szjo6Qt8zhwP/aw3CdLkrnYIsXDAic6GlHjHSGC+Yd3F2+421pXSAyykx06GIKdC4HaLZYTkTPTFzxIkGYVmXTPDuPdZ8ctE6NB5PTUZksxDs7ei/HtQkARuhso= ARC-Authentication-Results: i=1; server2.sourceware.org Received: from imap1.dmz-prg2.suse.org (imap1.dmz-prg2.suse.org [IPv6:2a07:de40:b281:104:10:150:64: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 smtp-out2.suse.de (Postfix) with ESMTPS id D01661FFE2 for ; Thu, 24 Oct 2024 15:12:49 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=suse.de; s=susede2_rsa; t=1729782770; h=from:from:reply-to:date:date:message-id:message-id:to:to:cc: mime-version:mime-version:content-type:content-type; bh=FSrhzXadCV95unp9wA6exGugqn26Y9Crtrq/HcVgyV0=; b=rEIZbh+vU4WdCdN+lt78+++17BRZB2WVnzkRGGxijwnUryggfnnD5XI9Hhl1N2sHd2hV8x x0lsH8Zn4UwKrv7Reiz6NSlU4x9V7BQTAnqv1wfvCsoT4Go8VGVy+allL2fC/X/A9EnRXI S4Gt7jedcp8IWcnYt/cDCJmIvcIJhOI= DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=suse.de; s=susede2_ed25519; t=1729782770; h=from:from:reply-to:date:date:message-id:message-id:to:to:cc: mime-version:mime-version:content-type:content-type; bh=FSrhzXadCV95unp9wA6exGugqn26Y9Crtrq/HcVgyV0=; b=DJU30h4+bu9SdLS3CUIXfzvwus5vuomB5gKzuQY1oOOgqlMYeQ8sCnY1Bxjl1NrbXueM0A GegfUsD5LTxIF5Aw== Authentication-Results: smtp-out2.suse.de; dkim=pass header.d=suse.de header.s=susede2_rsa header.b=PEKUKpdJ; dkim=pass header.d=suse.de header.s=susede2_ed25519 header.b=kVlJ8i5g DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=suse.de; s=susede2_rsa; t=1729782769; h=from:from:reply-to:date:date:message-id:message-id:to:to:cc: mime-version:mime-version:content-type:content-type; bh=FSrhzXadCV95unp9wA6exGugqn26Y9Crtrq/HcVgyV0=; b=PEKUKpdJkEZ7RlfE1dZd3O7aeieJHoVSAfUL5yq9qzctcNeLQygKmpwYbS3A3RXc6AWksV ynPawLvvO77IEBmGcUcuDiYmWYhTRC64N5yk9YcxvIH79e8kfv17WbbszE0/MLuCC4sKNH CkwhVGtH2R918tGrE51HalQCWy32RsY= DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=suse.de; s=susede2_ed25519; t=1729782769; h=from:from:reply-to:date:date:message-id:message-id:to:to:cc: mime-version:mime-version:content-type:content-type; bh=FSrhzXadCV95unp9wA6exGugqn26Y9Crtrq/HcVgyV0=; b=kVlJ8i5gqDrXxgJsStEwEPCwavLSVnV1Md2UWfszOnvba2o2OwCLV6VJydnvKTPtF9MqS2 4bVe3EKfEac3ucDg== Received: from imap1.dmz-prg2.suse.org (localhost [127.0.0.1]) (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 imap1.dmz-prg2.suse.org (Postfix) with ESMTPS id B67101368E for ; Thu, 24 Oct 2024 15:12:49 +0000 (UTC) Received: from dovecot-director2.suse.de ([2a07:de40:b281:106:10:150:64:167]) by imap1.dmz-prg2.suse.org with ESMTPSA id vbPxKvFjGmcLPwAAD6G6ig (envelope-from ) for ; Thu, 24 Oct 2024 15:12:49 +0000 Date: Thu, 24 Oct 2024 17:12:49 +0200 (CEST) From: Richard Biener To: gcc-patches@gcc.gnu.org Subject: [PATCH] Restrict :c to commutative ops as intended MIME-Version: 1.0 Message-Id: <20241024151249.B67101368E@imap1.dmz-prg2.suse.org> X-Rspamd-Queue-Id: D01661FFE2 X-Spam-Level: X-Spamd-Result: default: False [-4.51 / 50.00]; BAYES_HAM(-3.00)[100.00%]; NEURAL_HAM_LONG(-1.00)[-1.000]; NEURAL_HAM_SHORT(-0.20)[-1.000]; R_DKIM_ALLOW(-0.20)[suse.de:s=susede2_rsa,suse.de:s=susede2_ed25519]; MIME_GOOD(-0.10)[text/plain]; MX_GOOD(-0.01)[]; RBL_SPAMHAUS_BLOCKED_OPENRESOLVER(0.00)[2a07:de40:b281:104:10:150:64:97:from]; MIME_TRACE(0.00)[0:+]; ARC_NA(0.00)[]; RCPT_COUNT_ONE(0.00)[1]; FUZZY_BLOCKED(0.00)[rspamd.com]; DKIM_SIGNED(0.00)[suse.de:s=susede2_rsa,suse.de:s=susede2_ed25519]; TO_MATCH_ENVRCPT_ALL(0.00)[]; FROM_HAS_DN(0.00)[]; DKIM_TRACE(0.00)[suse.de:+]; SPAMHAUS_XBL(0.00)[2a07:de40:b281:104:10:150:64:97:from]; TO_DN_NONE(0.00)[]; RCVD_COUNT_TWO(0.00)[2]; FROM_EQ_ENVFROM(0.00)[]; RCVD_TLS_ALL(0.00)[]; PREVIOUSLY_DELIVERED(0.00)[gcc-patches@gcc.gnu.org]; RCVD_VIA_SMTP_AUTH(0.00)[]; RECEIVED_SPAMHAUS_BLOCKED_OPENRESOLVER(0.00)[2a07:de40:b281:106:10:150:64:167:received]; MISSING_XM_UA(0.00)[]; ASN(0.00)[asn:25478, ipnet:::/0, country:RU]; DBL_BLOCKED_OPENRESOLVER(0.00)[imap1.dmz-prg2.suse.org:mid, imap1.dmz-prg2.suse.org:helo, imap1.dmz-prg2.suse.org:rdns, suse.de:dkim] X-Rspamd-Server: rspamd2.dmz-prg2.suse.org X-Rspamd-Action: no action X-Spam-Score: -4.51 X-Spam-Status: No, score=-11.7 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, SPF_HELO_NONE, SPF_PASS, TXREP 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: , Errors-To: gcc-patches-bounces~incoming=patchwork.ozlabs.org@gcc.gnu.org genmatch was supposed to restrict :c to verifiable commutative operations while leaving :C to the "I know what I'm doing" case. The following enforces this, cleaning up parsing and amending the commutative_op helper. There's one pattern that needs adjustment, the pattern optimizing fmax (x, NaN) or fmax (NaN, x) to x since fmax isn't commutative. Bootstrap and regtest running on x86_64-unknown-linux-gnu. * genmatch.cc (commutative_op): Add paramter to indicate whether all compares should be considered commutative. Handle hypot, add_overflow and mul_overflow. (parser::parse_expr): Simplify 'c' handling by using commutative_op and error out when the operation is not. * match.pd ((minmax:c @0 NaN@1) -> @0): Use :C, we know what we are doing. --- gcc/genmatch.cc | 40 +++++++++++++++------------------------- gcc/match.pd | 2 +- 2 files changed, 16 insertions(+), 26 deletions(-) diff --git a/gcc/genmatch.cc b/gcc/genmatch.cc index 6287d6a7bc2..e2c02ee75ff 100644 --- a/gcc/genmatch.cc +++ b/gcc/genmatch.cc @@ -1106,12 +1106,13 @@ is_a_helper ::test (id_base *id) index of the first, otherwise return -1. */ static int -commutative_op (id_base *id) +commutative_op (id_base *id, bool compares_are_commutative = false) { if (operator_id *code = dyn_cast (id)) { if (commutative_tree_code (code->code) - || commutative_ternary_tree_code (code->code)) + || commutative_ternary_tree_code (code->code) + || (compares_are_commutative && comparison_code_p (code->code))) return 0; return -1; } @@ -1119,9 +1120,12 @@ commutative_op (id_base *id) switch (fn->fn) { CASE_CFN_FMA: + CASE_CFN_HYPOT: case CFN_FMS: case CFN_FNMA: case CFN_FNMS: + case CFN_ADD_OVERFLOW: + case CFN_MUL_OVERFLOW: return 0; case CFN_COND_ADD: @@ -1157,11 +1161,13 @@ commutative_op (id_base *id) } if (user_id *uid = dyn_cast (id)) { - int res = commutative_op (uid->substitutes[0]); + int res = commutative_op (uid->substitutes[0], + compares_are_commutative); if (res < 0) return -1; for (unsigned i = 1; i < uid->substitutes.length (); ++i) - if (res != commutative_op (uid->substitutes[i])) + if (res != commutative_op (uid->substitutes[i], + compares_are_commutative)) return -1; return res; } @@ -5213,27 +5219,11 @@ parser::parse_expr () { if (*sp == 'c') { - if (operator_id *o - = dyn_cast (e->operation)) - { - if (!commutative_tree_code (o->code) - && !comparison_code_p (o->code)) - fatal_at (token, "operation is not commutative"); - } - else if (user_id *p = dyn_cast (e->operation)) - for (unsigned i = 0; - i < p->substitutes.length (); ++i) - { - if (operator_id *q - = dyn_cast (p->substitutes[i])) - { - if (!commutative_tree_code (q->code) - && !comparison_code_p (q->code)) - fatal_at (token, "operation %s is not " - "commutative", q->id); - } - } - is_commutative = true; + if (commutative_op (e->operation, true) != -1) + is_commutative = true; + else + fatal_at (token, "operation %s is not known " + "commutative", e->operation->id); } else if (*sp == 'C') is_commutative = true; diff --git a/gcc/match.pd b/gcc/match.pd index 0455dfa6993..d78a19f450e 100644 --- a/gcc/match.pd +++ b/gcc/match.pd @@ -4667,7 +4667,7 @@ DEFINE_INT_AND_FLOAT_ROUND_FN (RINT) /* If either argument is NaN and other one is not sNaN, return the other one. Avoid the transformation if we get (and honor) a signalling NaN. */ (simplify - (minmax:c @0 REAL_CST@1) + (minmax:C @0 REAL_CST@1) (if (real_isnan (TREE_REAL_CST_PTR (@1)) && (!HONOR_SNANS (@1) || !TREE_REAL_CST (@1).signalling) && !tree_expr_maybe_signaling_nan_p (@0))