https://gcc.gnu.org/bugzilla/show_bug.cgi?id=114236
Bug ID: 114236 Summary: introduce unnecessary store operation when unrolling a loop Product: gcc Version: 13.2.0 Status: UNCONFIRMED Severity: normal Priority: P3 Component: tree-optimization Assignee: unassigned at gcc dot gnu.org Reporter: absoler at smail dot nju.edu.cn Target Milestone: --- here's the code, compiled with gcc-13.2.0 -O2 ``` // https://godbolt.org/z/5xWrr44hn short g_16[2][1] = {{(-8L)},{(-8L)}}; int g_22 = (-1L); int g_71 = 0xA62A9801L; int *g_70 = &g_71; int c; void func_1() { int d; for (; c;) ; for (d = -1; d < 17; d++) { short *e = g_16[0]; *g_70 = *e = g_22; } } ``` ``` func_1(): 401440: mov 0x10762(%rip),%eax # 411ba8 <c> 401446: test %eax,%eax 401448: je 401450 <func_1+0x10> 40144a: jmp 40144a <func_1+0xa> 40144c: nopl 0x0(%rax) 401450: mov 0x2c09(%rip),%rsi # 404060 <g_70> 401457: mov $0x12,%eax 40145c: nopl 0x0(%rax) 401460: mov 0x2c06(%rip),%ecx # 40406c <g_22> 401466: movswl %cx,%edx 401469: mov %edx,(%rsi) 40146b: sub $0x1,%eax 40146e: jne 401460 <func_1+0x20> 401470: mov %cx,0x2bf9(%rip) # 404070 <g_16> 401477: mov %edx,(%rsi) ``` we can see that the store operation at 0x401477 is unnecessary and lead to 19 times memory write, more one time than the loop. And I found this behavior showing up from the `141t.lim2` file when I check the output files of `-fdump-tree-all`.