From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 48) id DFA34385841D; Mon, 14 Feb 2022 14:33:20 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org DFA34385841D From: "jakub at gcc dot gnu.org" To: gcc-bugs@gcc.gnu.org Subject: [Bug tree-optimization/104526] [12 Regression] Dead Code Elimination Regression at -O3 (trunk vs. 11.2.0) Date: Mon, 14 Feb 2022 14:33:20 +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: missed-optimization X-Bugzilla-Severity: normal X-Bugzilla-Who: jakub at gcc dot gnu.org X-Bugzilla-Status: NEW X-Bugzilla-Resolution: X-Bugzilla-Priority: P1 X-Bugzilla-Assigned-To: unassigned at gcc dot gnu.org X-Bugzilla-Target-Milestone: 12.0 X-Bugzilla-Flags: X-Bugzilla-Changed-Fields: cc priority cf_reconfirmed_on everconfirmed bug_status 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, 14 Feb 2022 14:33:21 -0000 https://gcc.gnu.org/bugzilla/show_bug.cgi?id=3D104526 Jakub Jelinek changed: What |Removed |Added ---------------------------------------------------------------------------- CC| |aldyh at gcc dot gnu.org, | |amacleod at redhat dot com Priority|P3 |P1 Last reconfirmed| |2022-02-14 Ever confirmed|0 |1 Status|UNCONFIRMED |NEW --- Comment #2 from Jakub Jelinek --- Seems like something EVRP should optimize. The pre- r12-6924 IL was: c.0_1 =3D c; _2 =3D *c.0_1; # RANGE [-1, 1] _3 =3D 1 / _2; # RANGE [1, 2] NONZERO 3 d_11 =3D 2 >> _3; and evrp properly figured out those ranges, that 1 / int is [-1, 1] and that 2 >> [-1, 1] is [1, 2]. But since r12-6924 the IL is: c.0_1 =3D c; _2 =3D *c.0_1; _11 =3D (unsigned int) _2; _12 =3D _11 + 1; _13 =3D _12 <=3D 2; _3 =3D _12 <=3D 2 ? _2 : 0; # RANGE [0, 2] NONZERO 3 d_14 =3D 2 >> _3; and the range for d_14 is too broad (includes 0) and no ranges are recorded= for the other SSA_NAMEs. Now, __1 and _12 are of course VARYING, and because _13 is _Bool, it is also VARYING. The important missing part is that we don't realize that _12 <=3D 2 ? _2 : 0 implies [-1, 1] range. The _2 + 1U <=3D 2U is a standard pattern how range= s are encoded. Now if I rewrite the testcase by hand to: void foo(void); static int a, b =3D 1, *c =3D &b; int main() { for (; a; a--) { int e; int ct =3D *c; if (ct + 1U <=3D 2U) e =3D ct; else e =3D 0; int d =3D 2 >> e; if (!d) foo(); } } which is equivalent to doing the 1 / int PR95424 optimization by hand, but instead of having it in a COND_EXPR do it in separate bbs, i.e.: c.0_1 =3D c; ct_12 =3D *c.0_1; ct.1_2 =3D (unsigned int) ct_12; _3 =3D ct.1_2 + 1; if (_3 <=3D 2) goto ; [INV] else goto ; [INV] : : # RANGE [-1, 1] # e_7 =3D PHI # RANGE [1, 2] NONZERO 3 d_15 =3D 2 >> e_7; then evrp handles it just fine. So, Andrew/Aldy, how hard would it be to improve ranger COND_EXPR handling,= so that it essentially does what we do for the PHI cases? I.e. from the COND_= EXPR condition, compute "assertion" if condition is true or if condition is fals= e, and use that on the COND_EXPR's second and third argument. So for the _3 =3D _12 <=3D 2 ? _2 : 0; comparison, for second argument the condition must be true which implies th= at _2 must be there [-1, 1], while for the third argument the condition must be false, but the argument is constant 0, so range is [0, 0], then just union those 2 ranges. As this is a P1 regression, if we can fix it, would be nice to get it into = GCC 12.=