[Bug tree-optimization/126626] New: `(x & -x) == 0` can be optimized to `x == 0`

"pinskia at gcc dot gnu.org via Gcc-bugs" <[email protected]> Tue, 04 Aug 2026 00:53:00 +0000
Newsgroups gmane.comp.gcc.bugs
Message-ID <[email protected]/bugzilla/>
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=3D126626

            Bug ID: 126626
           Summary: `(x & -x) =3D=3D 0` can be optimized to `x =3D=3D 0`
           Product: gcc
           Version: 17.0
            Status: UNCONFIRMED
          Keywords: easyhack, missed-optimization
          Severity: normal
          Priority: P3
         Component: tree-optimization
          Assignee: unassigned at gcc dot gnu.org
          Reporter: pinskia at gcc dot gnu.org
  Target Milestone: ---

Here is a full list:

CST =3D=3D 0
(x & -x) =3D=3D 0 -> x =3D=3D 0
(x & -x) !=3D 0 -> x !=3D 0

powerof 2 CST:
(x & -x) =3D=3D N -> (x&((N<<1)-1)) =3D=3D N
(x & -x) !=3D N -> (x&((N<<1)-1)) !=3D N

[ Note CST =3D=3D 1<<precision should basically produce:
(x & -x) =3D=3D N -> x =3D=3D N
(x & -x) !=3D N -> x !=3D N

And CST =3D=3D 1 should produce:
(x & -x) =3D=3D 1 -> (x&1) =3D=3D 1 or (bool)(x&1)
(x & -x) !=3D 1 -> (x&1) !=3D 1 or ((x&1) =3D=3D 0)
]

non powerof 2 CST
(x & -x) =3D=3D CST -> false
(x & -x) !=3D CST -> true


Note this is more than that is proposed for LLVM (which is just the 0/1 cas=
e).
But this has the full CST case.=