http://gcc.gnu.org/bugzilla/show_bug.cgi?id=53419
Bug #: 53419 Summary: loop incorrectly optimized to endless loop at -O2 for table delimited by extern addresses (x86-32) Classification: Unclassified Product: gcc Version: 4.8.0 Status: UNCONFIRMED Severity: normal Priority: P3 Component: tree-optimization AssignedTo: unassig...@gcc.gnu.org ReportedBy: g...@intrepid.com Host: x86-32 Target: x86-32 The attached test case, duplicated below yields an infinite loop when compiled by GCC 4.8 trunk version 187666. This appears to be a relatively recent regression (likely between 185454 and 186243). typedef unsigned int size_t; typedef void (*func_ptr_t) (void); extern func_ptr_t init_array_begin[1]; extern func_ptr_t init_array_end[1]; void per_thread_init (void) { size_t n_init = (init_array_end - init_array_begin); int i; for (i = 0; i < n_init; ++i) { func_ptr_t init_func = init_array_begin[i]; if (init_func) (*init_func) (); } } At -O1, the loop looks like this: .L4: movl init_array_begin(,%ebx,4), %eax testl %eax, %eax je .L3 call *%eax .L3: addl $1, %ebx cmpl %esi, %ebx jne .L4 At -O2 it looks like this. .L5: movl init_array_begin, %eax testl %eax, %eax je .L3 call *%eax .L3: cmpl $1, %ebx jne .L5 The comparison "cmpl $1, %ebx" is problematic, as well as the lack of indexing the array.