http://gcc.gnu.org/bugzilla/show_bug.cgi?id=53419
--- Comment #8 from Richard Guenther <rguenth at gcc dot gnu.org> 2012-05-21 09:30:52 UTC --- (In reply to comment #6) > (In reply to comment #5) > > > extern func_ptr_t init_array_begin[1]; > > > extern func_ptr_t init_array_end[1]; > > > > The array sizes say they are size of one. If you want to be correct and not > > allow GCC to optimize away the check because array overflow, use [] instead > > of > > [1]. > > Good point. Do you happen to know if > extern func_ptr_t init_array_begin[]; > extern func_ptr_t init_array_end[]; > is say, C89 compatible? I ask, because I thought I ran into problems with > some > older compilers given the above syntax. Yes, it's C89 compatible. > 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) (); > } > } > > Questions regarding the optimization of the above. > > If the compiler concludes that n_init must be 1, then code that creates an > endless loop is not a valid optimization? > > Simplifying so that the loop executes only once might be, but I'm still having > a little trouble adjusting to that idea. size_t n_init = (init_array_end - init_array_begin); is by itself also undefined - you are taking the difference between two pointers to two distinct objects. I suppose you should instead declare extern func_ptr_t init_array_begin[]; extern func_ptr_t *init_array_end; > Is there an -f option that disables this sort of optimization? -fno-tree-vrp will, for your case. > > > 1