[PATCH] avoid chainon when building record types in Fortran FE

Mikael Morin mikael.morin@sfr.fr
Fri Jul 2 15:22:00 GMT 2010


Le 02.07.2010 15:44, Nathan Froyd a écrit :
>
> The Fortran FE builds up the fields for RECORD_TYPEs with repeated use
> of chainon:
>
>    list = chainon (list, decl);
>    list = chainon (list, decl);
>    ...
>
> which does more work than it needs to.  This patch addresses that by
> adding a parameter to gfc_add_field_to_struct that points to the end
> (last TREE_CHAIN field, really) of the field list being built.  It also
> makes gfc_add_field_to_struct be used in a few more places, though with
> a bit of complexity, as we have to be careful to not add alignment
> information where it wasn't present before.
>
> Tested on x86_64-unknown-linux-gnu.  OK to commit?

[...]

> @@ -1853,26 +1841,44 @@ gfc_finish_type (tree type)
>   }
>   

>   /* Add a field of given NAME and TYPE to the context of a UNION_TYPE
> -   or RECORD_TYPE pointed to by STYPE.  The new field is chained
> -   to the fieldlist pointed to by FIELDLIST.
> +   or RECORD_TYPE pointed to by CONTEXT.  The new field is chained
> +   to the fieldlist pointed to by FIELDLIST through *CHAIN.
>
>      Returns a pointer to the new field.  */
>
> +static tree
> +gfc_add_field_to_struct_1 (tree *fieldlist, tree context,
> +				 tree name, tree type, tree **chain)
> +{
> +  tree decl = build_decl (input_location, FIELD_DECL, name, type);
> +
> +  DECL_CONTEXT (decl) = context;
> +  TREE_CHAIN (decl) = NULL_TREE;
> +  if (*fieldlist == NULL_TREE)
> +    *fieldlist = decl;
It seems you can remove fieldlist from the the parameters and use 
TYPE_FIELDS(context) instead here.
OK with that change.
Actually, OK even without it.

Mikael

> +  if (chain != NULL)
> +    {
> +      if (*chain != NULL)
> +	**chain = decl;
> +      *chain =&TREE_CHAIN (decl);
> +    }
> +
> +  return decl;
> +}
> +
> +/* Like `gfc_add_field_to_struct_1', but adds alignment
> +   information.  */
> +
>   tree
>   gfc_add_field_to_struct (tree *fieldlist, tree context,
> -			 tree name, tree type)
> +			 tree name, tree type, tree **chain)
>   {
> -  tree decl;
> +  tree decl = gfc_add_field_to_struct_1 (fieldlist, context,
> +					 name, type, chain);
>
> -  decl = build_decl (input_location,
> -		     FIELD_DECL, name, type);
> -
> -  DECL_CONTEXT (decl) = context;
>     DECL_INITIAL (decl) = 0;
>     DECL_ALIGN (decl) = 0;
>     DECL_USER_ALIGN (decl) = 0;
> -  TREE_CHAIN (decl) = NULL_TREE;
> -  *fieldlist = chainon (*fieldlist, decl);
>
>     return decl;
>   }
[...]



More information about the Gcc-patches mailing list