https://gcc.gnu.org/bugzilla/show_bug.cgi?id=111560
Bug ID: 111560 Summary: Missed optimization of available expression 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 (regarding Available Expression) that GCC may have missed. We would greatly appreicate if you can take a look and let us know what you think. Here are three examples. Example 1: https://godbolt.org/z/eYcWocsPf Given the following code: ```c++ int test() { int a,b,c,d,e,f,g; cin>>a>>b>>c>>d>>g; e=a+b+c; //line 5 f=d+b+c; //"b+c" can be replaced with the value at line 5 cout<<e<<f; return 0; } ``` We note that `b+c` at line 6 in the test code can be replaced with the value at line 5, but gcc-trunk -O3 does not: ```asm test(): push rbx mov edi, OFFSET FLAT:_ZSt3cin sub rsp, 32 lea rsi, [rsp+12] call std::basic_istream<char, std::char_traits<char> >::operator>>(int&) lea rsi, [rsp+16] mov rdi, rax call std::basic_istream<char, std::char_traits<char> >::operator>>(int&) lea rsi, [rsp+20] mov rdi, rax call std::basic_istream<char, std::char_traits<char> >::operator>>(int&) lea rsi, [rsp+24] mov rdi, rax call std::basic_istream<char, std::char_traits<char> >::operator>>(int&) lea rsi, [rsp+28] mov rdi, rax call std::basic_istream<char, std::char_traits<char> >::operator>>(int&) mov esi, DWORD PTR [rsp+16] mov ebx, DWORD PTR [rsp+24] mov edi, OFFSET FLAT:_ZSt4cout mov eax, DWORD PTR [rsp+20] add ebx, esi add esi, DWORD PTR [rsp+12] add esi, eax add ebx, eax call std::basic_ostream<char, std::char_traits<char> >::operator<<(int) mov esi, ebx mov rdi, rax call std::basic_ostream<char, std::char_traits<char> >::operator<<(int) add rsp, 32 xor eax, eax pop rbx ret ``` Example 2: https://godbolt.org/z/o61sx6dGh Given the following code: ```c++ int var_13; int var_14; void test(int var_0, int var_4, int var_5) { var_13 = var_0 + var_4 + var_5; //line 3 var_14 = var_4 + var_5; //"var_4 + var_5" can be replaced with the value at line 3 } ``` We note that `var_4 + var_5` at line 4 in the test code can be replaced with the value at line 3, but gcc-trunk -O3 does not: ```asm test(int, int, int): add edi, esi add esi, edx add edi, edx mov DWORD PTR var_14[rip], esi mov DWORD PTR var_13[rip], edi ret var_14: .zero 4 var_13: .zero 4 ``` Example 3: https://godbolt.org/z/caYT9E6Mz Similar to example 2, but this is an example that might involve shifting operations: ```c++ int var_14,var_27; void test(int var_1, int var_3) { var_14 = var_1 + var_1; var_27 = var_3 + var_1 + var_1; } ``` ```asm test(int, int): lea eax, [rdi+rdi] mov DWORD PTR var_14[rip], eax lea eax, [rsi+rdi*2] mov DWORD PTR var_27[rip], eax ret var_27: .zero 4 var_14: .zero 4 ``` Thank you very much for your time and effort! We look forward to hearing from you.