[PATCH 2/9] ipa: Better way of applying both IPA-CP and IPA-SRA (PR 103227)

2022-12-12 Thread Martin Jambor
Hi,

I'm re-posting patches which I have posted at the end of stage 1 but
which have not passed review yet.

I would really like to have at least the previous patch and this one
accepted for GCC 13 so that PR 103227 is fixed ina way which does not
sometimes leave unused parameters behind.

The rest of the patches make IPA-SRA more powerful but if they cannot
be reviewed in time, I guess they'll be fine in GCC 14 too.

8<

This is basically a better fix for PR 103227.  The one currently in
use, rushed in late at stage3, which means that IPA-CP transformation
simply does a replacement of default-definition of IPA-SRA-created
scalar parameters with a constant, meant that IPA-SRA actually often
led to creation of a bunch of unused parameters, which was rather
ironic and sub-optimal.

This patch rips that old way out and makes sure the clash is resolved
at clone-materialization time.  What happens is that:

1) IPA-SRA IPA analysis (decision) stage recognizes the clash and does
   not create a param adjustment entry for such a scalar component.

2) Clone materialization code checks the IPA-CP transformation
   summary and when it realizes that it is removing a parameter that
   is a base for a discovered IPA-CP aggregate constant, and:

   a) the value is passed by reference, it internally records that any
  load of the value is replaced directly with the known constant
  value.  IPA-SRA will not attempt to split values passed by
  reference when there is a write to it so we know such a load
  won't be on a a LHS.

   b) the value is passed by value, there can be stores to the
  corresponding bit of the aggregate and so all accesses are
  replaced with a new decl and an assignment of the constant to
  this decl is generated at the beginning of the function.

The new testcase contains an xfail as the patch does not fix PR 107640
but it is one that ICEs when one is not careful about remapping
indices of parameters, so I'd like to have it in testsuite/gcc.gd/ipa/
even now.

I don't think that PR 107640 should be attempted through
ipa-param-manipulation replacements because the information is not
really there any more and we'd either need to do the replacements
earlier or dig deep into the clone parent info.  Instead, we should
record somewhere that at the beginning of the function the bits of the
global decl have known values and use that in the value numbering.
That way we could one day encode also known constants in globals that
do not come through parameters.

Bootstrapped and LTO-bootstrapped on x86_64-linux.  OK for master?


gcc/ChangeLog:

2022-11-11  Martin Jambor  

PR ipa/103227
* ipa-param-manipulation.h (class ipa_param_adjustments): Removed
member function get_updated_index_or_split.
(class ipa_param_body_adjustments): New overload of
register_replacement, new member function append_init_stmts, new
member m_split_agg_csts_inits.
* ipa-param-manipulation.cc: Include ipa-prop.h.
(ipa_param_adjustments::get_updated_index_or_split): Removed.
(ipa_param_body_adjustments::register_replacement): New overload, use
it from the older one.
(ipa_param_body_adjustments::common_initialization): Added the
capability to create replacements for conflicting IPA-CP discovered
constants.
(ipa_param_body_adjustments::ipa_param_body_adjustments): Construct
the new member.
(ipa_param_body_adjustments::append_init_stmts): New function.
* ipa-sra.cc: Include ipa-prop.h.
(push_param_adjustments_for_index): Require IPA-CP transformation
summary as a parameter, do not create replacements which are known to
have constant values.
(process_isra_node_results): Find and pass to the above function the
IPA-CP transformation summary.
* ipa-prop.cc (adjust_agg_replacement_values): Remove the
functionality replacing IPA-SRA created scalar parameters with
constants.  Simplify, do not require parameter descriptors, do not
return anything.
(ipcp_transform_function): Simplify now that
adjust_agg_replacement_values does not change cfg.  Move definition
and initialization of descriptors lower.
* tree-inline.cc (tree_function_versioning): Call append_init_stmts of
param_body_adjs, if there are any.

gcc/testsuite/ChangeLog:

2022-11-11  Martin Jambor  

PR ipa/103227
PR ipa/107640
* gcc.dg/ipa/pr107640-2.c: New test.
---
 gcc/ipa-param-manipulation.cc | 155 --
 gcc/ipa-param-manipulation.h  |  19 ++--
 gcc/ipa-prop.cc   |  66 ---
 gcc/ipa-sra.cc|  30 -
 gcc/testsuite/gcc.dg/ipa/pr107640-2.c |  50 +
 gcc/tree-inline.cc|   2 +
 6 files changed, 208 insertions(+), 11

Re: [PATCH 2/9] ipa: Better way of applying both IPA-CP and IPA-SRA (PR 103227)

2022-12-12 Thread Jan Hubicka via Gcc-patches
> 2022-11-11  Martin Jambor  
> 
>   PR ipa/103227
>   * ipa-param-manipulation.h (class ipa_param_adjustments): Removed
>   member function get_updated_index_or_split.
>   (class ipa_param_body_adjustments): New overload of
>   register_replacement, new member function append_init_stmts, new
>   member m_split_agg_csts_inits.
>   * ipa-param-manipulation.cc: Include ipa-prop.h.
>   (ipa_param_adjustments::get_updated_index_or_split): Removed.
>   (ipa_param_body_adjustments::register_replacement): New overload, use
>   it from the older one.
>   (ipa_param_body_adjustments::common_initialization): Added the
>   capability to create replacements for conflicting IPA-CP discovered
>   constants.
>   (ipa_param_body_adjustments::ipa_param_body_adjustments): Construct
>   the new member.
>   (ipa_param_body_adjustments::append_init_stmts): New function.
>   * ipa-sra.cc: Include ipa-prop.h.
>   (push_param_adjustments_for_index): Require IPA-CP transformation
>   summary as a parameter, do not create replacements which are known to
>   have constant values.
>   (process_isra_node_results): Find and pass to the above function the
>   IPA-CP transformation summary.
>   * ipa-prop.cc (adjust_agg_replacement_values): Remove the
>   functionality replacing IPA-SRA created scalar parameters with
>   constants.  Simplify, do not require parameter descriptors, do not
>   return anything.
>   (ipcp_transform_function): Simplify now that
>   adjust_agg_replacement_values does not change cfg.  Move definition
>   and initialization of descriptors lower.
>   * tree-inline.cc (tree_function_versioning): Call append_init_stmts of
>   param_body_adjs, if there are any.
OK,
Honza
> 
> gcc/testsuite/ChangeLog:
> 
> 2022-11-11  Martin Jambor  
> 
>   PR ipa/103227
>   PR ipa/107640
>   * gcc.dg/ipa/pr107640-2.c: New test.
> ---
>  gcc/ipa-param-manipulation.cc | 155 --
>  gcc/ipa-param-manipulation.h  |  19 ++--
>  gcc/ipa-prop.cc   |  66 ---
>  gcc/ipa-sra.cc|  30 -
>  gcc/testsuite/gcc.dg/ipa/pr107640-2.c |  50 +
>  gcc/tree-inline.cc|   2 +
>  6 files changed, 208 insertions(+), 114 deletions(-)
>  create mode 100644 gcc/testsuite/gcc.dg/ipa/pr107640-2.c
> 
> diff --git a/gcc/ipa-param-manipulation.cc b/gcc/ipa-param-manipulation.cc
> index cee0e23f946..e92cfc0b6d5 100644
> --- a/gcc/ipa-param-manipulation.cc
> +++ b/gcc/ipa-param-manipulation.cc
> @@ -46,7 +46,7 @@ along with GCC; see the file COPYING3.  If not see
>  #include "tree-phinodes.h"
>  #include "cfgexpand.h"
>  #include "attribs.h"
> -
> +#include "ipa-prop.h"
>  
>  /* Actual prefixes of different newly synthetized parameters.  Keep in sync
> with IPA_PARAM_PREFIX_* defines.  */
> @@ -449,39 +449,6 @@ ipa_param_adjustments::get_updated_indices (vec 
> *new_indices)
>  }
>  }
>  
> -/* If a parameter with original INDEX has survived intact, return its new
> -   index.  Otherwise return -1.  In that case, if it has been split and there
> -   is a new parameter representing a portion at unit OFFSET for which a value
> -   of a TYPE can be substituted, store its new index into SPLIT_INDEX,
> -   otherwise store -1 there.  */
> -int
> -ipa_param_adjustments::get_updated_index_or_split (int index,
> -unsigned unit_offset,
> -tree type, int *split_index)
> -{
> -  unsigned adj_len = vec_safe_length (m_adj_params);
> -  for (unsigned i = 0; i < adj_len ; i++)
> -{
> -  ipa_adjusted_param *apm = &(*m_adj_params)[i];
> -  if (apm->base_index != index)
> - continue;
> -  if (apm->op == IPA_PARAM_OP_COPY)
> - return i;
> -  if (apm->op == IPA_PARAM_OP_SPLIT
> -   && apm->unit_offset == unit_offset)
> - {
> -   if (useless_type_conversion_p (apm->type, type))
> - *split_index = i;
> -   else
> - *split_index = -1;
> -   return -1;
> - }
> -}
> -
> -  *split_index = -1;
> -  return -1;
> -}
> -
>  /* Return the original index for the given new parameter index.  Return a
> negative number if not available.  */
>  
> @@ -1020,6 +987,21 @@ ipa_param_adjustments::debug ()
>dump (stderr);
>  }
>  
> +/* Register a REPLACEMENT for accesses to BASE at UNIT_OFFSET.  */
> +
> +void
> +ipa_param_body_adjustments::register_replacement (tree base,
> +   unsigned unit_offset,
> +   tree replacement)
> +{
> +  ipa_param_body_replacement psr;
> +  psr.base = base;
> +  psr.repl = replacement;
> +  psr.dummy = NULL_TREE;
> +  psr.unit_offset = unit_offset;
> +  m_replacements.safe_push (psr);
> +}
> +
>  /* Register that REPLACEMENT should replace parameter described i