This is the mail archive of the
gcc-bugs@gcc.gnu.org
mailing list for the GCC project.
[Bug optimization/14504] New: Missed Bit Twiddling Optimization
- From: "stl at caltech dot edu" <gcc-bugzilla at gcc dot gnu dot org>
- To: gcc-bugs at gcc dot gnu dot org
- Date: 9 Mar 2004 17:17:03 -0000
- Subject: [Bug optimization/14504] New: Missed Bit Twiddling Optimization
- Reply-to: gcc-bugzilla at gcc dot gnu dot org
g++ 3.3.2 on i686-pc-linux-gnu with -s -O3 -fomit-frame-pointer compiles:
unsigned long cond_mask(bool flag, unsigned long mask, unsigned long target) {
return flag ? target | mask : target & ~mask;
}
into:
cmpb $0, 4(%esp)
movl 8(%esp), %eax
movl 12(%esp), %edx
je .L2
orl %edx, %eax
ret
.L2:
notl %eax
andl %edx, %eax
ret
This appears to be a straightforward translation of the code into assembly.
With the same options, this:
unsigned long cond_mask(bool flag, unsigned long mask, unsigned long target) {
return (mask | target ^ 0xFFFFFFFFUL + flag) ^ 0xFFFFFFFFUL + flag;
}
is compiled into:
movzbl 4(%esp), %edx
movl 12(%esp), %eax
movl 8(%esp), %ecx
decl %edx
xorl %edx, %eax
orl %ecx, %eax
xorl %edx, %eax
ret
Again this appears to be a straightforward translation of the code into
assembly (instead of flag + 0xFFFFFFFFUL, it uses static_cast<unsigned long>
(flag) - 1, which is the same thing).
However, the rewritten code lacks a branch yet does the exact same thing.
g++ ought to be able to perform this reasonably simple transformation on its
own - if, indeed, the transformation is desirable (which I think it is).
NB:
I do not know assembly, so I may have deleted important information from g++'s
output. I don't think I have, however.
This case was suggested by ryanm@microsoft.com, who said "this is something I
will never expect a compiler to be able to optimize for me. ^_^". I wrote the
transformed code; perhaps there is a cleverer way to transform it which I am
not aware of.
--
Summary: Missed Bit Twiddling Optimization
Product: gcc
Version: 3.3.2
Status: UNCONFIRMED
Severity: enhancement
Priority: P2
Component: optimization
AssignedTo: unassigned at gcc dot gnu dot org
ReportedBy: stl at caltech dot edu
CC: gcc-bugs at gcc dot gnu dot org,ryanm at microsoft dot
com
GCC build triplet: i686-pc-linux-gnu
GCC host triplet: i686-pc-linux-gnu
GCC target triplet: i686-pc-linux-gnu
http://gcc.gnu.org/bugzilla/show_bug.cgi?id=14504