From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 48) id 5C14C3858284; Thu, 12 Oct 2023 08:34:46 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 5C14C3858284 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1697099686; bh=HEvi/ZT02KY2TYe/JczGL4SqAWbKvOoUeF37GFYKBps=; h=From:To:Subject:Date:In-Reply-To:References:From; b=CYSWn5uCbLcoPyJv5lblCkszg9TgbHLNze2V3q/4gc/DnSl3eBvIXw18rxpQ0FXdC x5XpwMGFQkAmS7uB+puczobFX4ONV3bfe75a5apFlUIacsezWU0g6xc2tfyH6u9ZzA DXFF/peN3v7EFOtW1jFX6aARa8kLHa5/6kdF3Ojo= From: "rguenth at gcc dot gnu.org" To: gcc-bugs@gcc.gnu.org Subject: [Bug tree-optimization/111780] Missed optimization of '(t*4)/(t*2) -> 2' Date: Thu, 12 Oct 2023 08:34:44 +0000 X-Bugzilla-Reason: CC X-Bugzilla-Type: changed X-Bugzilla-Watch-Reason: None X-Bugzilla-Product: gcc X-Bugzilla-Component: tree-optimization X-Bugzilla-Version: 14.0 X-Bugzilla-Keywords: missed-optimization X-Bugzilla-Severity: normal X-Bugzilla-Who: rguenth at gcc dot gnu.org X-Bugzilla-Status: NEW X-Bugzilla-Resolution: X-Bugzilla-Priority: P3 X-Bugzilla-Assigned-To: unassigned at gcc dot gnu.org X-Bugzilla-Target-Milestone: --- X-Bugzilla-Flags: X-Bugzilla-Changed-Fields: keywords bug_status cf_reconfirmed_on everconfirmed Message-ID: In-Reply-To: References: Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable X-Bugzilla-URL: http://gcc.gnu.org/bugzilla/ Auto-Submitted: auto-generated MIME-Version: 1.0 List-Id: https://gcc.gnu.org/bugzilla/show_bug.cgi?id=3D111780 Richard Biener changed: What |Removed |Added ---------------------------------------------------------------------------- Keywords| |missed-optimization Status|UNCONFIRMED |NEW Last reconfirmed| |2023-10-12 Ever confirmed|0 |1 --- Comment #1 from Richard Biener --- Confirmed. Same for int foo (int a, int b, int c) { return 2*c*(a*b) / (a*b); } note when we cannot remove the division like for c*a / d*a we have to watch out for -INT_MIN / -1, but I think the only way this would not invoke undefined behavior before the transform is when the factor is equal to -1 but then c*a cannot be -INT_MIN so it should be safe in general, not only when m and n are constants? Note we cannot re-associate for the transform. We only have /* Simplify (t * 2) / 2) -> t. */ (for div (trunc_div ceil_div floor_div round_div exact_div) (simplify (div (mult:c @0 @1) @1) (if (ANY_INTEGRAL_TYPE_P (type)) (if (TYPE_OVERFLOW_UNDEFINED (type)) @0 #if GIMPLE (with {value_range vr0, vr1;} (if (INTEGRAL_TYPE_P (type) && 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)) @0)) #endif )))) but not the case with two multiplies.=