https://gcc.gnu.org/bugzilla/show_bug.cgi?id=61169
Bug ID: 61169 Summary: [4.6,4.7,4.8,4.9] unnecessarily honors bracket in mathematical statements Product: gcc Version: 4.6.3 Status: UNCONFIRMED Severity: minor Priority: P3 Component: c++ Assignee: unassigned at gcc dot gnu.org Reporter: hannesroest at gmx dot ch I hope this is the right place to report this. During some code review of a C++ project, we have noticed that GCC seems to honor brackets added in mathematical statements in the code unnecessarily and produce slightly suboptimal code under some circumstances. I am not sure whether this is a feature or not but it can lead to suboptimal code as we discovered in our codebase and I hoped that I could get some answers here. Specifically, the issue is how GCC treats an expression like (x*x) with brackets vs an expression like x*x without the brackets and prevents optimization in the first case. Specifically, one can see this in the following example where gcc >= 4.6 uses 4 instructions for the second function "test4ops" but only 3 instructions for the first function "test3ops". Interestingly, gcc < 4.6 used 3 instructions for both functions: $ cat test.C int test3ops(int x, int c, int d) { return c * x + d * x * x; } int test4ops(int x, int c, int d) { return c * x + d * (x * x); } $ gcc -S -O3 test.C $ cat test.s .file "test.C" .text .p2align 4,,15 .globl _Z8test3opsiii .type _Z8test3opsiii, @function _Z8test3opsiii: .LFB0: .cfi_startproc imull %edi, %edx leal (%rdx,%rsi), %eax imull %edi, %eax ret .cfi_endproc .LFE0: .size _Z8test3opsiii, .-_Z8test3opsiii .p2align 4,,15 .globl _Z8test4opsiii .type _Z8test4opsiii, @function _Z8test4opsiii: .LFB1: .cfi_startproc imull %edi, %esi imull %edi, %edi imull %edx, %edi leal (%rsi,%rdi), %eax ret .cfi_endproc .LFE1: .size _Z8test4opsiii, .-_Z8test4opsiii .ident "GCC: (Ubuntu/Linaro 4.6.3-1ubuntu5) 4.6.3" .section .note.GNU-stack,"",@progbits $ gcc --version gcc (Ubuntu/Linaro 4.6.3-1ubuntu5) 4.6.3