https://gcc.gnu.org/bugzilla/show_bug.cgi?id=111763
Bug ID: 111763
Summary: `(a & ~1) | 2` could be done as `(a & ~(1 | 2)) + 2`
which allows to use leal
Product: gcc
Version: 14.0
Status: UNCONFIRMED
Keywords: missed-optimization
Severity: enhancement
Priority: P3
Component: target
Assignee: unassigned at gcc dot gnu.org
Reporter: pinskia at gcc dot gnu.org
Target Milestone: ---
Target: x86_64-linux-gnu
Take:
```
int f1(int in) {
in = (in & ~(unsigned long)1);
in = in | 2;
return in;
}
int f2(int in) {
in = (in & ~(unsigned long)(1|2));
in = in + 2;
return in;
}
```
We currently get:
```
f1:
movl %edi, %eax
andl $-2, %eax
orl $2, %eax
ret
f2:
andl $-4, %edi
leal 2(%rdi), %eax
ret
```
The leal version is better because it saves more move due to leal not being a 2
operand but 3 operand instruction so it could improve register allocation ...
I noticed this whole looking into PR 111762 (and PR 111282) and looking at
clang/LLVM's code generation here .
Also I don't know how often this shows up though.