From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 48) id F2E9A3857C6F; Mon, 2 May 2022 12:44:06 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org F2E9A3857C6F From: "rguenth at gcc dot gnu.org" To: gcc-bugs@gcc.gnu.org Subject: [Bug tree-optimization/105329] [12/13 Regression] Bogus restrict warning when assigning 1-char string literal to std::string since r12-3347-g8af8abfbbace49e6 Date: Mon, 02 May 2022 12:44:06 +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: 12.0 X-Bugzilla-Keywords: diagnostic, 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: 12.0 X-Bugzilla-Flags: X-Bugzilla-Changed-Fields: cc target_milestone 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 X-BeenThere: gcc-bugs@gcc.gnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Gcc-bugs mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Mon, 02 May 2022 12:44:07 -0000 https://gcc.gnu.org/bugzilla/show_bug.cgi?id=3D105329 Richard Biener changed: What |Removed |Added ---------------------------------------------------------------------------- CC| |rguenth at gcc dot gnu.org Target Milestone|--- |12.0 --- Comment #8 from Richard Biener --- Btw, requires -std=3Dc++20 but -O2 is enough, -O3 not needed. To quote again: if (_22 >=3D &MEM [(void *)"5" + 1B]) goto ; [50.00%] else goto ; [50.00%] [local count: 44944954]: if (_22 <=3D "5") goto ; [50.00%] else goto ; [50.00%] [local count: 22472477]: _48 =3D _22 - "5"; if (_48 =3D=3D 1) goto ; [34.00%] else goto ; [66.00%] the "simple" thing we fail to thread is if (_22 >=3D _1 + 1) ; else { if (_22 <=3D _1) ; // this must be true, _22 < _1 is true even } in fact we fail to canonicalize _22 >=3D &MEM [(void *)"5" = + 1B] to _22 > "5". fold_comparison via maybe_canonicalize_comparison does such thing but not for pointers or &MEM. The following (too special) simplifies the above to [local count: 89889908]: if (_22 > &MEM [(void *)"5"]) goto ; [50.00%] else goto ; [50.00%] [local count: 44944954]: if (_22 <=3D "5") goto ; [50.00%] else goto ; [50.00%] but we still won't simplify the second compare. diff --git a/gcc/match.pd b/gcc/match.pd index 6d691d302b3..cb16694a150 100644 --- a/gcc/match.pd +++ b/gcc/match.pd @@ -5397,6 +5397,18 @@ DEFINE_INT_AND_FLOAT_ROUND_FN (RINT) (if (known_eq (off, 0)) { constant_boolean_node (cmp =3D=3D EQ_EXPR, type); })))))))) +(for cmp (ge le) + cmpp (gt lt) + (simplify + (cmp:c @0 addr@1) + (with + { tree m =3D TREE_OPERAND (@1, 0); } + (if (TREE_CODE (m) =3D=3D MEM_REF + && integer_onep (TREE_OPERAND (m, 1))) + (cmpp @0 { build1 (ADDR_EXPR, TREE_TYPE (@1), + build2 (MEM_REF, TREE_TYPE (m), TREE_OPERAND (m, 0), + build_zero_cst (TREE_TYPE (TREE_OPERAND (m, 1))))); }))))) + /* Equality compare simplifications from fold_binary */ (for cmp (eq ne) The ifcombine eventually arrives in Breakpoint 5, tree_ssa_ifcombine_bb_1 (inner_cond_bb=3D, outer_cond_bb=3D, then_bb=3D, else_bb=3D, phi_pred_bb=3D) at /home/rguenther/src/gcc-12-branch/gcc/tree-ssa-ifcombine.cc:646 646 if (phi_pred_bb !=3D else_bb [local count: 89889908]: if (_22 > &MEM [(void *)"5"]) goto ; [50.00%] else goto ; [50.00%] $7 =3D void [local count: 44944954]: if (_22 <=3D "5") goto ; [50.00%] else goto ; [50.00%] $8 =3D void but the CFG doesn't resemble any of the forms it handles and it does not try to catch the case where the inner condition would simplify (basically thread it without any code duplication). So it doesn't really fit ifcombine. The old VRP pass sees [local count: 44944954]: _112 =3D ASSERT_EXPR <_22, _22 <=3D &MEM [(void *)"5"]>; if (_112 <=3D "5") goto ; [50.00%] else goto ; [50.00%] but concludes xtract_range_from_stmt visiting: _112 =3D ASSERT_EXPR <_22, _22 <=3D &MEM [(void *)"5"]>; Found new range for _112: char * VARYING extract_range_from_stmt visiting: if (_112 <=3D "5") Visiting conditional with predicate: if (_112 <=3D "5") With known ranges _112: char * VARYING Predicate evaluates to: DON'T KNOW it might reason that _22 <=3D &MEM [(void *)"5"] means _22 =3D=3D &"5" and track the range of _22 as constant. That said, we relied on jump threading for these kind of simplifications but we are not good at this particular case.=