This is the mail archive of the gcc-patches@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Re: RFA: New optimization: Convert local statics into non-statics


Hi Nick,

What happens if local have their address taken and it escapes as in:

    int * foo (void) ( state int a = 1; return &a }

Cheers
Graham

--- Nick Clifton <nickc@redhat.com> wrote:

> Hi Guys,
> 
>   I would like to offer the attached patch consideration and I hope,
>   approval to check into gcc.  It implements a new optimization that
>   converts static variables local to a function into their non-static
>   equivalents.  So for example it would convert this:
> 
>     int foo (void) { static int a = 1; return a; }
> 
>   into this:
> 
>     int foo (void) { int a = 1; return a; }
> 
>   The conversion is only done when it can be shown that it is safe to
>   do so, which in practice means only when the variable is initialized
>   before it is used.  It is only performed for simple types, so arrays
>   and structures are not affected, and there is a new param provided
>   to stop the optimization from triggering on functions which have
>   lots of local variables already and adding some more would increase
>   the pressure on the stack too much.
> 
>   The benefit of the optimization is that it allows gcc the chance to
>   decide that the the converted variable can live entirely in a
>   register.  Plus even if the variable is pushed onto the stack, it is
>   often the case that accessing variables there is faster than
>   accessing variables in global memory.
> 
>   The optimization could be performed by hand by editing the source
>   code before compiling the program, but this option is not always
>   available to the user.  (For example this optimization was motivated
>   by the desire to improve the results for a well known benchmark.
>   Since the benchmark code could not be altered, the optimization was
>   added to the compiler).
> 
>   I have not added the optimization to those enabled by -O2 (or -O3)
>   because:
> 
>     a) I do not know the current policy for deciding when an
>     optimization is enabled by a given -Ox level.
> 
>     b) The optimization is new and unproven in the wide world of gcc
>     users.  (Although we have been using it internally at Red Hat for
>     a while and it has proved to be quite effective).
> 
>     c) There are some cases where the optimization results in worse
>     performance for the compiled program.  (Usually when too many
>     local variables end up on the stack).
> 
>   The patch includes a new gcc testcase, documentation and an update
>   to the changes.html file for gcc 4.4.  Tested without any
>   regressions with an x86 native toolchain.
> 
>   Please may I apply this patch ?
> 
> Cheers
>   Nick
> 
> gcc/ChangeLog
> 2008-07-14  Nick Clifton  <nickc@redhat.com>
> 
> 	* tree-optimize-local-statics.c: New file.  New optimization.
> 	Converts statics local to a function into non-statics.
> 	* tree-pass.h: Prototype pass_optimize_local_statics.
> 	* passes.c (init_optimization_passes): Add
> 	pass_optimize_local_statics.
> 	* common.opt (foptimize-local-statics): Add new command line
> 	option.
> 	* params.def (slo-local-variables-threshold): Define.
> 	* Makefile.in (OBJS-common): Add tree-optimize-local-statics.
> 	Add rule to compile tree-optimize-local-statics.c.
> 	* doc/invoke.texi: Document new command line option and param.
> 
> gcc/testsuite/ChangeLog
> 2008-07-14  Nick Clifton  <nickc@redhat.com>
> 
> 	* gcc.dg/tree-ssa/static-local.c: New file.  Test the local
> 	statics optimization.
> 
> 


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]