https://gcc.gnu.org/bugzilla/show_bug.cgi?id=101979
Bug ID: 101979 Summary: aarch64: wrong code with -O3 Product: gcc Version: 12.0 Status: UNCONFIRMED Severity: normal Priority: P3 Component: tree-optimization Assignee: unassigned at gcc dot gnu.org Reporter: cooper.qu at linux dot alibaba.com Target Milestone: --- The C code: typedef int (*init_fn_t)(void); extern int rti_start(void); extern int rti_board_start(void); extern int rti_board_end(void); extern int rti_end(void); __attribute__((used)) const init_fn_t __rt_init_rti_start __attribute__((section(".rti_fn." "0"))) = rti_start; __attribute__((used)) const init_fn_t __rt_init_rti_board_start __attribute__((section(".rti_fn." "0.end"))) = rti_board_start; __attribute__((used)) const init_fn_t __rt_init_rti_board_end __attribute__((section(".rti_fn." "1.end"))) = rti_board_end; __attribute__((used)) const init_fn_t __rt_init_rti_end __attribute__((section(".rti_fn." "6.end"))) = rti_end; void rt_components_init(void) { const init_fn_t *fn_ptr; for (fn_ptr = &__rt_init_rti_board_end; fn_ptr < &__rt_init_rti_end; fn_ptr ++) { (*fn_ptr)(); } } The key loop assemble code generated by -O3 is : .L3: bl rti_board_end cmp x19, x20 add x19, x19, 1 bne .L3 It only calls function 'rti_board_end' instead of moving the pointer 'fn_ptr' forward. The (*fn_ptr)() optimized to MEM[(int (*init_fn_t) (void) *)&__rt_init_rti_board_end + ivtmp.5_11 * 8] in the ivopts pass, it then fold to const in the fre5 pass as following dump information: "RHS MEM[(int (*init_fn_t) (void) *)&__rt_init_rti_board_end + ivtmp.5_11 * 8] simplified to rti_board_end" And I found a similar issue mentioned in an email a long time ago (https://gcc.gnu.org/legacy-ml/gcc-patches/2012-06/msg01221.html), the conclusion in the email is that the code written incorrectly. Is there a problem with the code above, or it's a bug of tree optimization?