From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 48) id 99C0A3858436; Wed, 27 Sep 2023 07:15:42 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 99C0A3858436 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1695798942; bh=spo/COg3Gh9deWUlfylcoa5hh4BqW4fRxfOzcWePsTg=; h=From:To:Subject:Date:In-Reply-To:References:From; b=guoCze8T4UlTD7IbSPyct8NH/QRyj1pAFQHiqVmZqpSSwvUN3JX9PA1rSkTOUEoUn TKN4TEO63zugXiCR8oN9Mpupg9I5rFPOEizxM2jTJ6x+IINX8RkqoLOTFfBsyjIjwk L66OfXJcnbBOG29SH99vKSGMzeSP7Xuor/j36dKI= From: "rguenth at gcc dot gnu.org" To: gcc-bugs@gcc.gnu.org Subject: [Bug tree-optimization/109088] GCC does not always vectorize conditional reduction Date: Wed, 27 Sep 2023 07:15:42 +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: 13.0 X-Bugzilla-Keywords: missed-optimization X-Bugzilla-Severity: enhancement 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: --- X-Bugzilla-Flags: X-Bugzilla-Changed-Fields: cc 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=3D109088 Richard Biener changed: What |Removed |Added ---------------------------------------------------------------------------- CC| |rdapp at gcc dot gnu.org --- Comment #9 from Richard Biener --- (In reply to JuzheZhong from comment #8) > It's because the order of the operations we are doing: >=20 > For code as follows: >=20 > result +=3D mask ? a[i] + x : 0; >=20 > GCC: > result_ssa_1 =3D PHI > ... > STMT 1. tmp =3D a[i] + x; > STMT 2. tmp2 =3D tmp + result_ssa_1; > STMT 3. result_ssa_2 =3D mask ? tmp2 : result_ssa_1; >=20 > Here we can see both STMT 2 and STMT 3 are using 'result_ssa_1', > we end up with 2 uses of the PHI result. Then, we failed to vectorize. >=20 > Wheras LLVM: >=20 > result_ssa_1 =3D PHI > ... > IR 1. tmp =3D a[i] + x; > IR 2. tmp2 =3D mask ? tmp : 0; > IR 3. result_ssa_2 =3D tmp2 + result_ssa_1. For floating point these are not equivalent (adding zero isn't a no-op). > LLVM only has 1 use. >=20 > Is it reasonable to swap the order in match.pd ? if-conversion could be teached to swap this (it's if-conversion creating the IL for conditional reductions) when valid. IIRC Robin Dapp also has a patch to make if-conversion emit .COND_ADD instead which should make it even better to vectorize.=