From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 48) id 5870A3858D20; Thu, 14 Dec 2023 16:36:28 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 5870A3858D20 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1702571788; bh=WZrMV3s94dGK5T3HUUhWHGf3lyQ0C+pC2sb+TFdsfnY=; h=From:To:Subject:Date:From; b=T5JS4o375nUr+zDhfPdwE9onWHg6rrrJ5WN9e+Q0YZHaW2yWJZlisHhpYg0+aM8dX qpl2wuGUyybbZBmQVsbCmmyBriF2XAfvnWQFv/BMaa6+EjBdyTm3s97dXk6aI7pwFq oTtFAu9K1A5tP4YaR+R3TB6GTdUgR2Lov/ZLvUpM= From: "jakub at gcc dot gnu.org" To: gcc-bugs@gcc.gnu.org Subject: [Bug tree-optimization/113024] New: Nested cast not optimized out in GIMPLE Date: Thu, 14 Dec 2023 16:36:26 +0000 X-Bugzilla-Reason: CC X-Bugzilla-Type: new X-Bugzilla-Watch-Reason: None X-Bugzilla-Product: gcc X-Bugzilla-Component: tree-optimization X-Bugzilla-Version: 14.0 X-Bugzilla-Keywords: ice-on-valid-code X-Bugzilla-Severity: normal X-Bugzilla-Who: jakub at gcc dot gnu.org X-Bugzilla-Status: UNCONFIRMED 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: bug_id short_desc product version bug_status keywords bug_severity priority component assigned_to reporter cc dependson target_milestone cf_gcchost cf_gcctarget Message-ID: 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=3D113024 Bug ID: 113024 Summary: Nested cast not optimized out in GIMPLE Product: gcc Version: 14.0 Status: UNCONFIRMED Keywords: ice-on-valid-code Severity: normal Priority: P3 Component: tree-optimization Assignee: unassigned at gcc dot gnu.org Reporter: jakub at gcc dot gnu.org CC: jakub at gcc dot gnu.org, unassigned at gcc dot gnu.org, zsojka at seznam dot cz Depends on: 112941 Target Milestone: --- Host: x86_64-pc-linux-gnu Target: x86_64-pc-linux-gnu +++ This bug was initially created as a clone of Bug #112941 +++ The above PR leads to a question, why don't we optimize: unsigned int foo (signed char x) { unsigned long long y =3D x; return y; } unsigned int bar (signed char x) { return (unsigned long long) x; } We do optimize the bar case in convert.cc's CASE_CONVERT: { tree argtype =3D TREE_TYPE (TREE_OPERAND (expr, 0)); /* Don't introduce a "can't convert between vector values of different size" error. */ if (TREE_CODE (argtype) =3D=3D VECTOR_TYPE && maybe_ne (GET_MODE_SIZE (TYPE_MODE (argtype)), GET_MODE_SIZE (TYPE_MODE (type)))) break; } /* If truncating after truncating, might as well do all at once. If truncating after extending, we may get rid of wasted work= .=20 */ return convert (type, get_unwidened (TREE_OPERAND (expr, 0), type)); This is the truncating after extending case. Now, for unsigned int baz (unsigned char x) { unsigned long long y =3D x; return y; } we optimize it in the match.pd /* Likewise, if the intermediate and initial types are either both float or both integer, we don't need the middle conversion if the former is wider than the latter and doesn't change the signedness (for integers). Avoid this if the final type is a pointer since then we sometimes need the middle conversion. */ (if (((inter_int && inside_int) || (inter_float && inside_float)) && (final_int || final_float) && inter_prec >=3D inside_prec && (inter_float || inter_unsignedp =3D=3D inside_unsignedp)) (ocvt @0)) case. In the foo case, we have inter_int && inside_int && final_int && inside_pre= c < final_prec && final_prec < inter_prec && (!inside_unsignedp) && inter_unsig= nedp && final_unsignedp, so don't trigger the above condition. Slightly later, there is a comment which describes the reason why 2 convers= ions would be needed: /* Two conversions in a row are not needed unless: - some conversion is floating-point (overstrict for now), or - some conversion is a vector (overstrict for now), or - the intermediate type is narrower than both initial and final, or - the intermediate type and innermost type differ in signedness, and the outermost type is wider than the intermediate, or - the initial type is a pointer type and the precisions of the intermediate and final types differ, or - the final type is a pointer type and the precisions of the initial and intermediate types differ. */ and I believe none of those bullets apply here, while intermediate type and innermost type differ in signedness, the outermost type is not wider than t= he intermediate. But the actually implemented rule below it has 7 cases rather than 6. Referenced Bugs: https://gcc.gnu.org/bugzilla/show_bug.cgi?id=3D112941 [Bug 112941] during GIMPLE pass: bitintlower ICE: in handle_operand_addr, at gimple-lower-bitint.cc:2126 (gimple-lower-bitint.cc:2134) at -O with _BitIn= t()=