From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 48) id 787B13870C32; Fri, 30 Jun 2023 14:28:16 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 787B13870C32 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1688135296; bh=w70enyP1linPLOfbnAkx2Db5lw0O5BOVb0WaOtzUYJY=; h=From:To:Subject:Date:In-Reply-To:References:From; b=X4QGzs1CNX/lbTL6VRyC8NRWMjCPHCm4y592fDZK2h/FkeA1J1G5xJwS797N9pkv+ 2X5gqV3zBOTE/gko2l6p9dmpMmCSFc2Ik3vhSYjiqpsU7y5k+JGFP+dR4nTcj+WlXH 0fMue/+z93CgdZS8BRq5cSaenzrZmdLRK713KF1Y= From: "cvs-commit at gcc dot gnu.org" To: gcc-bugs@gcc.gnu.org Subject: [Bug middle-end/109849] suboptimal code for vector walking loop Date: Fri, 30 Jun 2023 14:28:14 +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: 13.0 X-Bugzilla-Keywords: missed-optimization X-Bugzilla-Severity: normal X-Bugzilla-Who: cvs-commit 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: 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=3D109849 --- Comment #20 from CVS Commits --- The master branch has been updated by Jan Hubicka : https://gcc.gnu.org/g:eab57b825bcc350e9ff44eb2fa739a80199d9bb1 commit r14-2219-geab57b825bcc350e9ff44eb2fa739a80199d9bb1 Author: Jan Hubicka Date: Fri Jun 30 16:27:27 2023 +0200 Fix handling of __builtin_expect_with_probability and improve first-mat= ch heuristics While looking into the std::vector _M_realloc_insert codegen I noticed = that call of __throw_bad_alloc is predicted with 10% probability. This is because the conditional guarding it has __builtin_expect (cond, 0) on it. This incorrectly takes precedence over more reliable heuristics predicting t= hat call to cold noreturn is likely not going to happen. So I reordered the predictors so __builtin_expect_with_probability comes first after predictors that never makes a mistake (so user can use it to alwa= ys specify the outcome by hand). I also downgraded malloc predictor since= I do think user-defined malloc functions & new operators may behave funny wa= ys and moved usual __builtin_expect after the noreturn cold predictor. This triggered latent bug in expr_expected_value_1 where if (*predictor < predictor2) *predictor =3D predictor2; should be: if (predictor2 < *predictor) *predictor =3D predictor2; which eventually triggered an ICE on combining heuristics. This made me notice that we can do slightly better while combining expected values in case = only one of the parameters (such as in a*b when we expect a=3D=3D0) can dete= rmine overall result. Note that the new code may pick weaker heuristics in case that both val= ues are predicted. Not sure if this scenario is worth the extra CPU time: ther= e is not correct way to combine the probabilities anyway since we do not kno= w if the predictions are independent, so I think users should not rely on it. Fixing this issue uncovered another problem. In 2018 Martin Liska added code predicting that MALLOC returns non-NULL but instead of that he predicts that it returns true (boolean 1). This sort of works for testcase test= ing malloc (10) !=3D NULL but, for example, we will predict malloc (10) =3D=3D malloc (10) as true, which is not right and such comparsion may happen in real code I think proper way is to update expr_expected_value_1 to work with value ranges, but that needs greater surgery so I decided to postpone this and only add FIXME and fill PR110499. gcc/ChangeLog: PR middle-end/109849 * predict.cc (estimate_bb_frequencies): Turn to static function. (expr_expected_value_1): Fix handling of binary expressions with predicted values. * predict.def (PRED_MALLOC_NONNULL): Move later in the priority queue. (PRED_BUILTIN_EXPECT_WITH_PROBABILITY): Move to almost top of t= he priority queue. * predict.h (estimate_bb_frequencies): No longer declare it. gcc/testsuite/ChangeLog: PR middle-end/109849 * gcc.dg/predict-18.c: Improve testcase.=