From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 48) id ED5FB38515F7; Fri, 8 Apr 2022 10:55:26 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org ED5FB38515F7 From: "rguenth at gcc dot gnu.org" To: gcc-bugs@gcc.gnu.org Subject: [Bug tree-optimization/105198] [11/12 Regression] Wrong code for C loop (GCC 12 -O2, GCC 11 -O3) Date: Fri, 08 Apr 2022 10:55:26 +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: wrong-code X-Bugzilla-Severity: normal X-Bugzilla-Who: rguenth at gcc dot gnu.org X-Bugzilla-Status: ASSIGNED X-Bugzilla-Resolution: X-Bugzilla-Priority: P3 X-Bugzilla-Assigned-To: rguenth at gcc dot gnu.org X-Bugzilla-Target-Milestone: 11.3 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 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: Fri, 08 Apr 2022 10:55:27 -0000 https://gcc.gnu.org/bugzilla/show_bug.cgi?id=3D105198 --- Comment #5 from Richard Biener --- So before pcom we have [local count: 114863530]: j_29 =3D k_28(D) + -1; _1 =3D (long unsigned int) j_29; _2 =3D _1 * 4; _3 =3D x_30(D) + _2; _4 =3D *_3; _5 =3D _4 + 1; *_3 =3D _5; if (j_29 > 0) goto ; [94.50%] [local count: 108546036]: [local count: 1014686026]: # j_40 =3D PHI # tmp_39 =3D PHI <_17(12), _4(11)> _6 =3D (long unsigned int) j_40; _7 =3D _6 * 4; _8 =3D x_30(D) + _7; _9 =3D *_8; _11 =3D j_29 - j_40; _12 =3D n_33(D) - _11; if (_9 < _12) goto ; [5.50%] else goto ; [94.50%] and pcom replaces the _9 load like the following: [local count: 1014686026]: # j_40 =3D PHI # D__lsm0.5_25 =3D PHI tmp_39 =3D D__lsm0.5_25; _6 =3D (long unsigned int) j_40; _7 =3D _6 * 4; _8 =3D x_30(D) + _7; _9 =3D D__lsm0.5_25; _11 =3D j_29 - j_40; _12 =3D n_33(D) - _11; if (_9 < _12) goto ; [5.50%] else goto ; [94.50%] but that fails to realize that while _4 loads from the same address, the *_3 =3D _5 store clobbers the value. So with pcom applied we exit the loop immediately at the x[j] < n - (k - 1 -j)) test because we use the wrong loop entry value for x[j]. It somehow gets derailed to use 'tmp' here (it doesn't actually even analyze the load/store in BB2). So it's probably wrong in determining Store-loads chain 0x3410a90 max distance 1, may reuse first inits _4 references: *_15 (id 2, write) offset -1 distance 0 looparound ref in statement tmp_39 =3D PHI <_17(12), _4(11)> distance 1 *_8 (id 0) offset 0 distance 1 in particular identifying the looparound ref which is only partly a looparund ref (the initial value isn't). Indeed, prepare_initializers_ch= ain simply does /* If we have replaced some looparound phi nodes, use their initializers instead of creating our own. */ FOR_EACH_VEC_ELT (chain->refs, i, laref) { if (gimple_code (laref->stmt) !=3D GIMPLE_PHI) continue; gcc_assert (laref->distance > 0); chain->inits[n - laref->distance] =3D PHI_ARG_DEF_FROM_EDGE (laref->stmt, entry); } which would be OK, but find_looparound_phi simply looks at the def of the loop entry value and matches it up with the chain, not considering intermediate clobbering stmts.=