https://gcc.gnu.org/bugzilla/show_bug.cgi?id=111562
Bug ID: 111562 Summary: Missed optimization when the value of another variable can be used directly Product: gcc Version: 14.0 Status: UNCONFIRMED Severity: normal Priority: P3 Component: tree-optimization Assignee: unassigned at gcc dot gnu.org Reporter: 652023330028 at smail dot nju.edu.cn Target Milestone: --- Hello, we found some optimizations that GCC may have missed. We would greatly appreicate if you can take a look and let us know what you think. Example 1: https://godbolt.org/z/nPW7j1xvq Given the following code: ```c++ int var22=0; int var23=0; void test(int var7, int var9, int var11, int var12) { var22 = var12 + var9 + var11; var23 = var12 + (var9 + var7 + var11); } ``` In fact, `var23` can be directly optimized to `var22+var7`, but gcc-trunk -O3 does not: ```asm test(int, int, int, int): lea eax, [rcx+rsi] add esi, edi add esi, edx add eax, edx add esi, ecx mov DWORD PTR var22[rip], eax mov DWORD PTR var23[rip], esi ret var23: .zero 4 var22: .zero 4 ``` Example 2: https://godbolt.org/z/8vE69WPc7 Given the following code: ```c++ extern int var_15; extern int var_16; void test(int var_2, int var_3, int var_4, int var_6, int var_9) { var_15 = var_9 - (var_2 + var_6 + var_3 + var_4); var_16 = var_4 + var_6 + var_2; } ``` By adjusting the calculation order, the `var_16` calculation can actually use the intermediate result of the `var_15` calculation process directly, but gcc-trunk -O3 does not: ```asm test(int, int, int, int, int): lea eax, [rdi+rcx] add ecx, edx add eax, esi add ecx, edi add eax, edx mov DWORD PTR var_16[rip], ecx sub r8d, eax mov DWORD PTR var_15[rip], r8d ret ``` Thank you very much for your time and effort! We look forward to hearing from you.