From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (qmail 8354 invoked by alias); 28 Oct 2003 14:45:41 -0000 Mailing-List: contact gcc-bugs-help@gcc.gnu.org; run by ezmlm Precedence: bulk List-Archive: List-Post: List-Help: Sender: gcc-bugs-owner@gcc.gnu.org Received: (qmail 8346 invoked by uid 48); 28 Oct 2003 14:45:40 -0000 Date: Tue, 28 Oct 2003 14:47:00 -0000 From: "bangerth at dealii dot org" To: gcc-bugs@gcc.gnu.org Message-ID: <20031028144536.12814.bangerth@dealii.org> Reply-To: gcc-bugzilla@gcc.gnu.org Subject: [Bug optimization/12814] New: Missed optimization with non-static but non-changing local arrays X-Bugzilla-Reason: CC X-SW-Source: 2003-10/txt/msg02505.txt.bz2 List-Id: PLEASE REPLY TO gcc-bugzilla@gcc.gnu.org ONLY, *NOT* gcc-bugs@gcc.gnu.org. http://gcc.gnu.org/bugzilla/show_bug.cgi?id=12814 Summary: Missed optimization with non-static but non-changing local arrays Product: gcc Version: 3.4 Status: UNCONFIRMED Severity: normal Priority: P2 Component: optimization AssignedTo: unassigned at gcc dot gnu dot org ReportedBy: bangerth at dealii dot org CC: gcc-bugs at gcc dot gnu dot org For this small code snippet: ------------------- int f1 (const int i) { const int x[3] = { 13, 26, 39 }; return x[i]; } int f2 (const int i) { static const int x[3] = { 13, 26, 39 }; return x[i]; } ------------------- here is essentially what we get on x86 with -O3 and -fomit-frame-pointer with all present branches: ------------------- _Z2f1i: subl $28, %esp movl $26, %ecx movl 32(%esp), %eax movl $13, (%esp) movl $39, %edx movl %ecx, 4(%esp) movl %edx, 8(%esp) movl (%esp,%eax,4), %eax addl $28, %esp ret _ZZ2f2iE1x: .long 13 .long 26 .long 39 _Z2f2i: movl 4(%esp), %eax movl _ZZ2f2iE1x(,%eax,4), %eax ret ------------------ Obviously, the code for f2 is more efficient than that for f1. Things get even worse if the array gets larger, since then its contents are spilled onto the stack in f1. The point is that this is really not necessary: gcc certainly has all the abilities to detect that the array in f1 is unchanging and that it could be treated as static even though it is not declared as that. Note also that the code generated for f2 _never_ uses more memory than that in f1, since the code for writing a constant into a register or a stack slot uses _more_ memory than having the constant in a table. W.