https://gcc.gnu.org/bugzilla/show_bug.cgi?id=113072
Bug ID: 113072 Summary: `(a ^ CST0) & (~a ^ CST0)` is not optimized to 0 at the gimple level Product: gcc Version: 14.0 Status: UNCONFIRMED Keywords: TREE Severity: enhancement Priority: P3 Component: tree-optimization Assignee: unassigned at gcc dot gnu.org Reporter: pinskia at gcc dot gnu.org Target Milestone: --- Take: ``` int foo(int a, int b) { return (a ^ 4) & (~a ^ 4); } ``` This should be optimized to 0 but is missed at the gimple level. RTL Combine catches it: ``` Trying 8, 7 -> 9: 8: {r105:SI=r101:SI^0xfffffffffffffffb;clobber flags:CC;} REG_DEAD r101:SI REG_UNUSED flags:CC 7: {r104:SI=r101:SI^0x4;clobber flags:CC;} REG_UNUSED flags:CC 9: {r103:SI=r104:SI&r105:SI;clobber flags:CC;} REG_DEAD r105:SI REG_DEAD r104:SI REG_UNUSED flags:CC Failed to match this instruction: (parallel [ (set (reg:SI 103) (const_int 0 [0])) (clobber (reg:CC 17 flags)) ]) Successfully matched this instruction: (set (reg:SI 103) (const_int 0 [0])) allowing combination of insns 7, 8 and 9 original costs 4 + 4 + 4 = 12 replacement cost 4 ``` via apply_distributive_law (I think) which does basically `(a ^ 4) & (a ^ (~4))` -> `(4 ^ (~4)) & (a ^ a)` which is 0 as `a ^ a` is 0.