http://gcc.gnu.org/bugzilla/show_bug.cgi?id=46680
Summary: Suboptimal code generated for bool comparisons Product: gcc Version: 4.4.1 Status: UNCONFIRMED Severity: enhancement Priority: P3 Component: c++ AssignedTo: unassig...@gcc.gnu.org ReportedBy: adriano.reze...@openbossa.org Hi, Take as reference the code below (compiled using gcc-4.4.1 on linux. command line: g++ main.cpp): int main() { bool value = true; if (value) printf("true\n"); if (!value) printf("false\n"); return 0; } The first 'if' generates the following block: cmpb $0, 31(%esp) je .L2 movl $.LC0, (%esp) call puts While the second 'if' generates the following: .L2: movzbl 31(%esp), %eax xorl $1, %eax testb %al, %al je .L3 movl $.LC1, (%esp) call puts I think this: movzbl 31(%esp), %eax xorl $1, %eax testb %al, %al je .L3 Could be replaced by this: cmpb $0, 31(%esp) jne .L3 Is there a reason to be like this? Am I missing something? This change will remove two instructions per comparison and also solve a strange behavior, which was the original reason for me to investigate it; Take as an example a non-initialized bool variable, containing the value 0x8 (trash memory). The two lines below will be printed: if (value) printf("true"); if (!value) printf("also true"); since the second 'if' will compute (0x1 xor 0x8) resulting in 0x9 (also true). Maybe the spec doesn't care about this, but I think this is a strange behavior even for non-initialized variables. Br, Adriano