https://gcc.gnu.org/bugzilla/show_bug.cgi?id=112746
Bug ID: 112746 Summary: Missed optimization for redundancy computation elimination (fre1(tree) for global variable) 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 noticed that maybe there is a missed optimization for redundancy computation elimination. When b is a local variable, the computation of m optimizes to 3 as expected. When b appears as a global variable, it misses this optimization. https://godbolt.org/z/j3cKsrKb6 int n,m; int b; void test(int b){ b=n+n; m=(n+b)/(n); } void test2(){ b=n+n; m=(n+b)/(n); } But GCC -O3 -fwrapv or GCC -O3: test(int): mov DWORD PTR m[rip], 3 ret test2(): mov ecx, DWORD PTR n[rip] lea eax, [rcx+rcx] mov DWORD PTR b[rip], eax add eax, ecx cdq idiv ecx mov DWORD PTR m[rip], eax ret Expected code (Clang): test2(): # @test2() mov eax, dword ptr [rip + n] add eax, eax mov dword ptr [rip + b], eax mov dword ptr [rip + m], 3 ret We see a difference in fre1(tree) for these two functions, which might help with this issue: void test (int b) { int n.0_1; int _3; int _5; <bb 2> : # DEBUG BEGIN_STMT n.0_1 = n; b_7 = n.0_1 * 2; # DEBUG b => b_7 # DEBUG BEGIN_STMT _3 = n.0_1 * 3; _5 = 3; m = 3; return; } void test2 () { int n.3_1; int _2; int _5; int _7; <bb 2> : # DEBUG BEGIN_STMT n.3_1 = n; _2 = n.3_1 * 2; b = _2; # DEBUG BEGIN_STMT _5 = n.3_1 + _2; _7 = _5 / n.3_1; m = _7; return; } Thank you very much for your time and effort! We look forward to hearing from you.