From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (qmail 22435 invoked by alias); 9 Mar 2004 17:17:04 -0000 Mailing-List: contact gcc-bugs-help@gcc.gnu.org; run by ezmlm Precedence: bulk List-Archive: List-Post: List-Help: Sender: gcc-bugs-owner@gcc.gnu.org Received: (qmail 22421 invoked by uid 48); 9 Mar 2004 17:17:03 -0000 Date: Tue, 09 Mar 2004 17:17:00 -0000 From: "stl at caltech dot edu" To: gcc-bugs@gcc.gnu.org Message-ID: <20040309171658.14504.stl@caltech.edu> Reply-To: gcc-bugzilla@gcc.gnu.org Subject: [Bug optimization/14504] New: Missed Bit Twiddling Optimization X-Bugzilla-Reason: CC X-SW-Source: 2004-03/txt/msg01129.txt.bz2 List-Id: 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 (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