From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from smtp-out1.suse.de (smtp-out1.suse.de [195.135.223.130]) by sourceware.org (Postfix) with ESMTPS id 86068385C6D7 for ; Thu, 14 Dec 2023 10:41:23 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 86068385C6D7 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 86068385C6D7 Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=195.135.223.130 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1702550485; cv=none; b=GsZDIlLy2Jeto1AKR88Rz4M5LHZLB7fNCLiqsFRgpvB8ZCtnTwZC08A0nbdlt8t7kSQThmdg4qFzjMO+qpCBE7795l0agBqmQGRdjbK4u5+2sDDy6hNhy+4zVEi5nIDDZGLcT1DFgnYFXCbeGnDe6gFKErC1ie5YBnIvRFaanhE= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1702550485; c=relaxed/simple; bh=orB1oFZVNRcbyP0eaJuQ2UBnNu4u7RvG0EwRNlS4MZI=; h=DKIM-Signature:DKIM-Signature:DKIM-Signature:DKIM-Signature:From: Mime-Version:Subject:Date:Message-Id:To; b=a41MU8WI3BTnVLz9Y0NBDEkLtRnVobYLIdxedgR7GynjwMCuwl8RGPu6hl0Ka7Ya+yswFaON01fttHarlJYtZdaKu0QrskjFx4wKZ30TVYs+wLHIwmcv1GBg9uYqDrdcaUwjAvW+vLGVKBvZF4EpP0dwRO86MD9sVJ2v90Y99vg= ARC-Authentication-Results: i=1; server2.sourceware.org Received: from imap2.dmz-prg2.suse.org (imap2.dmz-prg2.suse.org [10.150.64.98]) (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-out1.suse.de (Postfix) with ESMTPS id 644AA2229A; Thu, 14 Dec 2023 10:41:22 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=suse.de; s=susede2_rsa; t=1702550482; h=from:from:reply-to:date:date:message-id:message-id:to:to:cc:cc: mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=BKI/+JeUY9t3lx6sjhYNKDGJc+mdwbiEiYckREwC2Dc=; b=avCHNBl4qo6iXAkM8HkLvLWWQhSfVUeDMjXgzThio0KAoEyoI7gVeDaCmwiHzaDp3zREUc 2d7LauO7n+00UfJXugAJEJ//WPzxsvXr0XYrjo7kDv20qWWxQyDOEyYzAV3GPPYPCpLadd WFT0CKyDONz66dRAcZx8Q2cZWZdlfco= DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=suse.de; s=susede2_ed25519; t=1702550482; h=from:from:reply-to:date:date:message-id:message-id:to:to:cc:cc: mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=BKI/+JeUY9t3lx6sjhYNKDGJc+mdwbiEiYckREwC2Dc=; b=0fCdKQer/Nf0cUJlYoZO6lyvOZAWSsnT3G7/TVoXc+/t6FlDtkW80iM7vMa4qyyXqOkJZp /nbqUGf25c5CTBAg== DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=suse.de; s=susede2_rsa; t=1702550482; h=from:from:reply-to:date:date:message-id:message-id:to:to:cc:cc: mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=BKI/+JeUY9t3lx6sjhYNKDGJc+mdwbiEiYckREwC2Dc=; b=avCHNBl4qo6iXAkM8HkLvLWWQhSfVUeDMjXgzThio0KAoEyoI7gVeDaCmwiHzaDp3zREUc 2d7LauO7n+00UfJXugAJEJ//WPzxsvXr0XYrjo7kDv20qWWxQyDOEyYzAV3GPPYPCpLadd WFT0CKyDONz66dRAcZx8Q2cZWZdlfco= DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=suse.de; s=susede2_ed25519; t=1702550482; h=from:from:reply-to:date:date:message-id:message-id:to:to:cc:cc: mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=BKI/+JeUY9t3lx6sjhYNKDGJc+mdwbiEiYckREwC2Dc=; b=0fCdKQer/Nf0cUJlYoZO6lyvOZAWSsnT3G7/TVoXc+/t6FlDtkW80iM7vMa4qyyXqOkJZp /nbqUGf25c5CTBAg== Received: from imap2.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 imap2.dmz-prg2.suse.org (Postfix) with ESMTPS id 49AEF134B0; Thu, 14 Dec 2023 10:41:22 +0000 (UTC) Received: from dovecot-director2.suse.de ([10.150.64.162]) by imap2.dmz-prg2.suse.org with ESMTPSA id 4+D3EdLbemW+UwAAn2gu4w (envelope-from ); Thu, 14 Dec 2023 10:41:22 +0000 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: quoted-printable From: Richard Biener Mime-Version: 1.0 (1.0) Subject: Re: [PATCH] match.pd: Simplify (t * u) / v -> t * (u / v) [PR112994] Date: Thu, 14 Dec 2023 11:41:11 +0100 Message-Id: <7F63D023-264D-48D3-9F2C-5A3F668F11C7@suse.de> References: Cc: gcc-patches@gcc.gnu.org In-Reply-To: To: Jakub Jelinek X-Mailer: iPhone Mail (21C62) X-Spam-Level: X-Spam-Score: -2.60 X-Spamd-Result: default: False [-2.60 / 50.00]; ARC_NA(0.00)[]; RCVD_VIA_SMTP_AUTH(0.00)[]; BAYES_HAM(-3.00)[100.00%]; FROM_HAS_DN(0.00)[]; TO_DN_SOME(0.00)[]; MV_CASE(0.50)[]; TO_MATCH_ENVRCPT_ALL(0.00)[]; MIME_GOOD(-0.10)[text/plain]; RCVD_COUNT_THREE(0.00)[3]; DKIM_SIGNED(0.00)[suse.de:s=susede2_rsa,suse.de:s=susede2_ed25519]; RCPT_COUNT_TWO(0.00)[2]; FUZZY_BLOCKED(0.00)[rspamd.com]; FROM_EQ_ENVFROM(0.00)[]; MIME_TRACE(0.00)[0:+]; RCVD_TLS_ALL(0.00)[]; MID_RHS_MATCH_FROM(0.00)[] X-Spam-Level: X-Spam-Score: -2.60 Authentication-Results: smtp-out1.suse.de; none X-Spam-Status: No, score=-5.4 required=5.0 tests=BAYES_00,DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,SPF_HELO_NONE,SPF_PASS,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 List-Id: > Am 14.12.2023 um 08:35 schrieb Jakub Jelinek : >=20 > =EF=BB=BFHi! >=20 > The following testcase is optimized just on GENERIC (using > strict_overflow_p =3D false; > if (TREE_CODE (arg1) =3D=3D INTEGER_CST > && (tem =3D extract_muldiv (op0, arg1, code, NULL_TREE, > &strict_overflow_p)) !=3D 0) > { > if (strict_overflow_p) > fold_overflow_warning (("assuming signed overflow does not occu= r " > "when simplifying division"), > WARN_STRICT_OVERFLOW_MISC); > return fold_convert_loc (loc, type, tem); > } > ) but not on GIMPLE. >=20 > The following included patch is what I've bootstrapped/regtested > for it on x86_64-linux and i686-linux, unfortunately it regressed > +FAIL: gcc.dg/Wstrict-overflow-3.c correct warning (test for warnings, lin= e 12) > test, we are indeed assuming that signed overflow does not occur > when simplifying division in there. >=20 > The attached version of the patch (which provides the simplification only > for GIMPLE) fixes that, but I haven't bootstrapped/regtested it yet. > And/or we could add the > fold_overflow_warning (("assuming signed overflow does not occu= r " > "when simplifying division"), > WARN_STRICT_OVERFLOW_MISC); > call into the simplification, but in that case IMHO it should go into > the (t * u) / u -> t simplification as well, there we assume the exact > same thing (of course, in both cases only in the spots where we don't > verify it through ranger that it never overflows). >=20 > Guarding the whole simplification to GIMPLE only IMHO makes sense because > the above mentioned folding does it for GENERIC (and extract_muldiv even > handles far more cases, dunno how many from that we should be doing on > GIMPLE in match.pd and what could be done elsewhere; e.g. extract_muldiv > can handle (x * 16 + y * 32) / 8 -> x * 2 + y * 4 etc.). >=20 > Dunno about the fold_overflow_warning, I always have doubts about why > such a warning is useful to users. >=20 > Ok for trunk (and which version)? I couldn=E2=80=99t spot the difference=E2=80=A6 OK for either Version (and no, calling fold_overflow_warning looks wrong) Richard=20 > 2023-12-14 Jakub Jelinek >=20 > PR tree-optimization/112994 > * match.pd ((t * 2) / 2 -> t): Adjust comment to use u instead of 2. > Punt without range checks if TYPE_OVERFLOW_SANITIZED. > ((t * u) / v -> t * (u / v)): New simplification. >=20 > * gcc.dg/tree-ssa/pr112994-1.c: New test. >=20 > --- gcc/match.pd.jj 2023-12-13 11:21:15.852158970 +0100 > +++ gcc/match.pd 2023-12-13 19:10:26.448927327 +0100 > @@ -930,12 +930,12 @@ DEFINE_INT_AND_FLOAT_ROUND_FN (RINT) > (if (INTEGRAL_TYPE_P (TREE_TYPE (@0)) && TYPE_UNSIGNED (TREE_TYPE (@0))) > (bit_and @0 (negate @1)))) >=20 > -/* Simplify (t * 2) / 2) -> t. */ > (for div (trunc_div ceil_div floor_div round_div exact_div) > + /* Simplify (t * u) / u -> t. */ > (simplify > (div (mult:c @0 @1) @1) > (if (ANY_INTEGRAL_TYPE_P (type)) > - (if (TYPE_OVERFLOW_UNDEFINED (type)) > + (if (TYPE_OVERFLOW_UNDEFINED (type) && !TYPE_OVERFLOW_SANITIZED (type)= ) > @0 > #if GIMPLE > (with {value_range vr0, vr1;} > @@ -945,6 +945,21 @@ DEFINE_INT_AND_FLOAT_ROUND_FN (RINT) > && range_op_handler (MULT_EXPR).overflow_free_p (vr0, vr1)) > @0)) > #endif > + ))) > + /* Simplify (t * u) / v -> t * (u / v) if u is multiple of v. */ > + (simplify > + (div (mult @0 INTEGER_CST@1) INTEGER_CST@2) > + (if (INTEGRAL_TYPE_P (type) > + && wi::multiple_of_p (wi::to_widest (@1), wi::to_widest (@2), SIGN= ED)) > + (if (TYPE_OVERFLOW_UNDEFINED (type) && !TYPE_OVERFLOW_SANITIZED (type)= ) > + (mult @0 (div! @1 @2)) > +#if GIMPLE > + (with {value_range vr0, vr1;} > + (if (get_range_query (cfun)->range_of_expr (vr0, @0) > + && get_range_query (cfun)->range_of_expr (vr1, @1) > + && range_op_handler (MULT_EXPR).overflow_free_p (vr0, vr1)) > + (mult @0 (div! @1 @2)))) > +#endif > )))) >=20 > #if GIMPLE > --- gcc/testsuite/gcc.dg/tree-ssa/pr112994-1.c.jj 2023-12-13 16:58:25.7= 57663610 +0100 > +++ gcc/testsuite/gcc.dg/tree-ssa/pr112994-1.c 2023-12-13 16:43:16.4131= 52969 +0100 > @@ -0,0 +1,13 @@ > +/* PR tree-optimization/112994 */ > +/* { dg-do compile } */ > +/* { dg-options "-O2 -fdump-tree-optimized" } */ > +/* { dg-final { scan-tree-dump-not " / \\\[2389-\\\]" "optimized" } } */ > + > +int f1 (int x) { return (x * 4) / 2; } > +int f2 (int x) { return (x * 56) / 8; } > +int f3 (int x) { return (x * 56) / -8; } > +int f4 (int x) { int y =3D x * 4; return y / 2; } > +int f5 (int x) { int y =3D x * 56; return y / 8; } > +int f6 (int x) { int y =3D x * 56; return y / -8; } > +unsigned f7 (unsigned x) { if (x > ~0U / 6) __builtin_unreachable (); uns= igned y =3D x * 6; return y / 3; } > +unsigned f8 (unsigned x) { if (x > ~0U / 63) __builtin_unreachable (); un= signed y =3D x * 63; return y / 9; } >=20 > Jakub >