[RFC/RFA] Const variable initializer folding

H.J. Lu hjl.tools@gmail.com
Fri Sep 10 05:13:00 GMT 2010


On Tue, Aug 31, 2010 at 4:08 PM, Jan Hubicka <hubicka@ucw.cz> wrote:
> Hi.
> at various places we need to fold const static variable via knowledge of its
> initializer.  The precise conditions when we do so hoever vary. For example
>
> const int a=5;
> const int b[2]={0,1};
> main()
> {
>  return a+b[0];
> }
>
> compiled with -O2 -fpic gets a folded to 5, but b[0] is not folded to 0.
>
> In order to fix darwin WHOPR partitioning I need variables with extern linkage
> but known initializer.  Even this is supported for scalars, but not for arrays,
> see i.e. g++.dg/opt/static3.C but not for arrays or structures.
>
> I am quite convinced that with WHOPR partitioning, we can't derive the fact
> from current DECL flags.
>
> static const int a;
>
> Allows folding referenced of A to 0. However with partitioning it might become:
> __attribute__ ((visibility ("hidden"))) extern const int a;
> ... and we would lose knowledge that A is 0.
>
> For this reason patch adds const_value_known into varpool structure and makes
> existing folders to use it.  Note that there is other place folding initialiers
> in expand.c I would like to remove (tree optimizers can do the same) but that
> would need some extra work.
>
> There is one failure with the patch, gfortran.dg/cray_pointers_5.f90.  It is
> because fortran adds into varpool variable that is !TREE_STATIC &&
> !DECL_EXTERNAL that seems like a bug to me, but I am not familiar enough with
> this area of FE to know what it is supposed to do.
>
> Does this seem to make sense?  Patch was bootstrapped/regtested x86_64-linux and
> darwin.
>
> Honza
>
>        * cgraph.h (struct varpool_node): Add const_value_known.
>        (varpool_decide_const_value_known): Declare.
>        * tree-ssa-ccp.c (fold_const_aggregate_ref): Update initializer folding.
>        * lto-cgraph.c (lto_output_varpool_node): Store const_value_known.
>        (input_varpool_node): Restore const_value_known.
>        * tree-ssa-loop-ivcanon (constant_after_peeling): Check varpool for
>        initializer folding.
>        * ipa.c (ipa_discover_readonly_nonaddressable_var,
>        function_and_variable_visibility): Compute const_value_known.
>        * gimple-fold.c (get_symbol_constant_value): Use varpool for initializer
>        folding.
>        * varpool.c (varpool_decide_const_value_known): New function.

This caused:

http://gcc.gnu.org/bugzilla/show_bug.cgi?id=45626

-- 
H.J.



More information about the Gcc-patches mailing list