From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 48) id 3F21C3858C62; Fri, 25 Aug 2023 12:27:25 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 3F21C3858C62 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1692966445; bh=fb81MWwmtkWJnhppp9x3rIHXMWKJ3H3EXCA0icpcepc=; h=From:To:Subject:Date:In-Reply-To:References:From; b=WI/JJfjAp0Hx3quJukJ5wJp9OGpoPmAPWzhaUL42ACsyL1z3qE3T9Hkio1CQADQVP 4tob0A1Mm4cQfG4YEQ0xoa7D9WykYtDkYxlQt77K0k4NypmU1NsDZVORnrUXDKGe07 Ox4aynmEsibE2xrk3YaIc1cjwlV4ijvZXyeWB4sc= From: "rguenth at gcc dot gnu.org" To: gcc-bugs@gcc.gnu.org Subject: [Bug middle-end/111151] [12/13/14 Regression] Wrong code at -O0 on x86_64-pc-linux-gnu Date: Fri, 25 Aug 2023 12:27:24 +0000 X-Bugzilla-Reason: CC X-Bugzilla-Type: changed X-Bugzilla-Watch-Reason: None X-Bugzilla-Product: gcc X-Bugzilla-Component: middle-end X-Bugzilla-Version: 14.0 X-Bugzilla-Keywords: needs-bisection, wrong-code 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: 12.4 X-Bugzilla-Flags: X-Bugzilla-Changed-Fields: cc 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=3D111151 Richard Biener changed: What |Removed |Added ---------------------------------------------------------------------------- CC| |pinskia at gcc dot gnu.org, | |rguenth at gcc dot gnu.org --- Comment #3 from Richard Biener --- instrumenting extract_muldiv shows ... Applying pattern match.pd:5113, generic-match-4.cc:2339 Applying fold-const.c:6892 Applying fold-const.c:7101 Applying fold-const.c:6892 Applying fold-const.c:6985 Applying pattern match.pd:4392, generic-match-8.cc:3091 and commenting case MIN_EXPR: case MAX_EXPR: /* If widening the type changes the signedness, then we can't perform this optimization as that changes the result. */ if (TYPE_UNSIGNED (ctype) !=3D TYPE_UNSIGNED (type)) break; /* MIN (a, b) / 5 -> MIN (a / 5, b / 5) */ sub_strict_overflow_p =3D false; if ((t1 =3D extract_muldiv (op0, c, code, wide_type, &sub_strict_overflow_p)) !=3D 0 && (t2 =3D extract_muldiv (op1, c, code, wide_type, &sub_strict_overflow_p)) !=3D 0) { if (tree_int_cst_sgn (c) < 0) tcode =3D (tcode =3D=3D MIN_EXPR ? MAX_EXPR : MIN_EXPR); if (sub_strict_overflow_p) *strict_overflow_p =3D true;=20 return DUMP_FOLD (fold_build2 (tcode, ctype, fold_convert (ctype, t1), fold_convert (ctype, t2))); }=20 break; fixes the testcase. We turn MAX ((long long unsigned int) t + 4503599, 32739) * 18446744073709551606 to MIN ((long long unsigned int) t * 18446744073709551606 + 18446744073664515626, 18446744073709224226) I think when overflow wraps we cannot do this transform at all, independent on the "sign" of 'c'. Maybe @@ -6970,8 +6972,11 @@ extract_muldiv_1 (tree t, tree c, enum tree_code cod= e, tree wide_type, /* MIN (a, b) / 5 -> MIN (a / 5, b / 5) */ sub_strict_overflow_p =3D false; - if ((t1 =3D extract_muldiv (op0, c, code, wide_type, - &sub_strict_overflow_p)) !=3D 0 + if ((wide_type + ? TYPE_OVERFLOW_UNDEFINED (wide_type) + : TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (op0)))=20 + && (t1 =3D extract_muldiv (op0, c, code, wide_type, + &sub_strict_overflow_p)) !=3D 0 && (t2 =3D extract_muldiv (op1, c, code, wide_type, &sub_strict_overflow_p)) !=3D 0) {=