https://gcc.gnu.org/bugzilla/show_bug.cgi?id=97417
--- Comment #38 from Levy <admin at levyhsu dot com> --- Created attachment 49575 --> https://gcc.gnu.org/bugzilla/attachment.cgi?id=49575&action=edit riscv-shorten-memrefs_V1.patch Did little bit change in get_si_mem_base_reg() and transform() Now for the same c input array_test.c int load1r (int *array) { int a = 0; a += array[200]; a += array[201]; a += array[202]; a += array[203]; return a; } int main () { int arr[300]= {0}; arr[200] = 15; arr[201] = -33; arr[202] = 7; arr[203] = -999; int b = load1r(arr); printf("Result: %d\n",b); return 0; } in loadlr, when put a debug_rtx(pat) after: (unpatched)XEXP (pat, i) = replace_equiv_address (mem, addr); or (patched)XEXP (XEXP (pat, I), 0) = replace_equiv_address(XEXP (mem, 0), addr); unpatched gcc will produce follwing insns: --------------------------------------------------------- (set (reg:SI 81 [ MEM[(int *)array_5(D) + 800B] ]) (mem:SI (plus:DI (reg:DI 88) (const_int 32 [0x20])) [1 MEM[(int *)array_5(D) + 800B]+0 S4 A32])) (set (reg:SI 82 [ MEM[(int *)array_5(D) + 804B] ]) (mem:SI (plus:DI (reg:DI 89) (const_int 36 [0x24])) [1 MEM[(int *)array_5(D) + 804B]+0 S4 A32])) (set (reg:SI 84 [ MEM[(int *)array_5(D) + 808B] ]) (mem:SI (plus:DI (reg:DI 90) (const_int 40 [0x28])) [1 MEM[(int *)array_5(D) + 808B]+0 S4 A32])) (set (reg:SI 86 [ MEM[(int *)array_5(D) + 812B] ]) (mem:SI (plus:DI (reg:DI 91) (const_int 44 [0x2c])) [1 MEM[(int *)array_5(D) + 812B]+0 S4 A32])) --------------------------------------------------------- patched gcc will produce follwing insns: --------------------------------------------------------- (set (reg:DI 82 [ MEM[(int *)array_5(D) + 800B] ]) (sign_extend:DI (mem:SI (plus:DI (reg:DI 92) (const_int 32 [0x20])) [1 MEM[(int *)array_5(D) + 800B]+0 S4 A32]))) (set (reg:DI 84 [ MEM[(int *)array_5(D) + 804B] ]) (sign_extend:DI (mem:SI (plus:DI (reg:DI 93) (const_int 36 [0x24])) [1 MEM[(int *)array_5(D) + 804B]+0 S4 A32]))) (set (reg:DI 87 [ MEM[(int *)array_5(D) + 808B] ]) (sign_extend:DI (mem:SI (plus:DI (reg:DI 94) (const_int 40 [0x28])) [1 MEM[(int *)array_5(D) + 808B]+0 S4 A32]))) (set (reg:DI 90 [ MEM[(int *)array_5(D) + 812B] ]) (sign_extend:DI (mem:SI (plus:DI (reg:DI 95) (const_int 44 [0x2c])) [1 MEM[(int *)array_5(D) + 812B]+0 S4 A32]))) --------------------------------------------------------- which the patched one looks ok for me, but the final assembly code still shows no change (both under -Os) Not quite sure where the problem is, I'll have a look near array_test.c.250r.shorten_memrefs tomorrow. Please ignore the coding style as it's just a debug patch