https://gcc.gnu.org/bugzilla/show_bug.cgi?id=95078
Bug ID: 95078 Summary: Missing fwprop for SIB address Product: gcc Version: 11.0 Status: UNCONFIRMED Severity: normal Priority: P3 Component: target Assignee: unassigned at gcc dot gnu.org Reporter: crazylht at gmail dot com CC: hjl.tools at gmail dot com Target Milestone: --- Target: i386, x86-64 cat test.c int foo (int* p1, int* p2, int scale) { int ret = *(p1 + scale * 4 + 11); *p2 = 3; int ret2 = *(p1 + scale * 4 + 11); return ret + ret2; } gcc11 -O2 test.c -S foo(int*, int*, int): sall $2, %edx movslq %edx, %rdx leaq 44(%rdi,%rdx,4), %rdx --- redundant could be fwprop movl (%rdx), %eax movl $3, (%rsi) addl (%rdx), %eax ret fwprop failed to propagate this because it think cost of address 44(%rdi,%rdx,4) is more expensive than (%rdx), that's correct locally, but under global view, if it could be propagated into both movl, leaq would be eliminated, which benifits performance. The ideal place to handle this issue is TER opt in pass_expand, but currently TER only handle simple situation ---- single use and block level 48 A pass is made through the function, one block at a time. No cross block 49 information is tracked. 50 51 Variables which only have one use, and whose defining stmt is considered 52 a replaceable expression (see ssa_is_replaceable_p) are tracked to see whether 53 they can be replaced at their use location. Should TER be extended? Another testcase has this issue in more complex cfg Refer to https://godbolt.org/z/ofjH9R