From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (qmail 1481 invoked by alias); 19 May 2012 22:09:19 -0000 Received: (qmail 1469 invoked by uid 22791); 19 May 2012 22:09:18 -0000 X-SWARE-Spam-Status: No, hits=-4.3 required=5.0 tests=ALL_TRUSTED,AWL,BAYES_00,KHOP_THREADED X-Spam-Check-By: sourceware.org Received: from localhost (HELO gcc.gnu.org) (127.0.0.1) by sourceware.org (qpsmtpd/0.43rc1) with ESMTP; Sat, 19 May 2012 22:09:06 +0000 From: "gary at intrepid dot com" To: gcc-bugs@gcc.gnu.org Subject: [Bug tree-optimization/53419] loop incorrectly optimized to endless loop at -O2 for table delimited by extern addresses (x86-32) Date: Sat, 19 May 2012 22:12:00 -0000 X-Bugzilla-Reason: CC X-Bugzilla-Type: changed X-Bugzilla-Watch-Reason: None X-Bugzilla-Product: gcc X-Bugzilla-Component: tree-optimization X-Bugzilla-Keywords: X-Bugzilla-Severity: normal X-Bugzilla-Who: gary at intrepid dot com X-Bugzilla-Status: RESOLVED X-Bugzilla-Priority: P3 X-Bugzilla-Assigned-To: unassigned at gcc dot gnu.org X-Bugzilla-Target-Milestone: --- X-Bugzilla-Changed-Fields: Message-ID: In-Reply-To: References: X-Bugzilla-URL: http://gcc.gnu.org/bugzilla/ Auto-Submitted: auto-generated Content-Type: text/plain; charset="UTF-8" MIME-Version: 1.0 Mailing-List: contact gcc-bugs-help@gcc.gnu.org; run by ezmlm Precedence: bulk List-Id: List-Archive: List-Post: List-Help: Sender: gcc-bugs-owner@gcc.gnu.org X-SW-Source: 2012-05/txt/msg01904.txt.bz2 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=53419 --- Comment #6 from Gary Funck 2012-05-19 22:08:26 UTC --- (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. 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. Is there an -f option that disables this sort of optimization? 1