Hi,

On Tue, Jun 02 2020, Richard Biener wrote:
> On Thu, 28 May 2020, Martin Jambor wrote:
>
>> PR 93385 reveals that if the user explicitely disables DCE, IPA-SRA
>> can leave behind statements which are useless because their results
>> are eventually not used but can have problematic side effects,
>> especially since their inputs are now bogus that useless parameters
>> were removed.
>> 
>> This patch fixes the problem by doing a similar def-use walk when
>> materializing clones, marking which statements should not be copied
>> and which SSA_NAMEs will be removed by call redirections and now need
>> to be replaced with anything valid.  Default-definition SSA_NAMEs of
>> parameters which are removed and all SSA_NAMEs derived from them (in a
>> phi node or a simple assignment statement) are then remapped to
>> error_mark_node - a sure way to spot it if any is left in place.
>> 
>> There is one exception to the above rule, if such SSA_NAMEs appear as
>> an argument of a call, they need to be removed by call redirection and
>> not as part of clone materialization.  So to have something valid
>> there until that time, this patch pulls out dummy declarations out of
>> thin air.  If you do not like that, see patch number 4 in the series,
>> which changes this, but probably in a controversial way.
>> 
>> This patch only resets debug statements using the removed SSA_NAMEs.
>> The first follow-up patch adjusts debug statements in the current
>> function to still try to make the removed values available in debugger
>> in the current function and the subsequent one also in other functions
>> where they are passed.
>> 
>> gcc/ChangeLog:
>> 
>> 2020-05-14  Martin Jambor  <mjam...@suse.cz>
>> 
>>      PR ipa/93385
>>      * ipa-param-manipulation.h (class ipa_param_body_adjustments): New
>>      members m_dead_stmts, m_dead_ssas, mark_dead_statements and
>>      get_removed_call_arg_placeholder.
>>      * ipa-param-manipulation.c (phi_arg_will_live_p): New function.
>>      (ipa_param_body_adjustments::mark_dead_statements): New method.
>>      (ipa_param_body_adjustments::common_initialization): Call it.
>>      (ipa_param_body_adjustments::ipa_param_body_adjustments): Initialize
>>      new mwmbers.
>>      (ipa_param_body_adjustments::get_removed_call_arg_placeholder): New.
>>      (ipa_param_body_adjustments::modify_call_stmt): Replace dead SSAs
>>      with dummy decls.
>>      * tree-inline.c (remap_gimple_stmt): Do not copy dead statements,
>>      reset dead debug statements.
>>      (copy_phis_for_bb): Do not copy dead PHI nodes.
>> 
>> gcc/testsuite/ChangeLog:
>> 

[...]

>> 
>> diff --git a/gcc/ipa-param-manipulation.c b/gcc/ipa-param-manipulation.c
>> index 978916057f0..1f47f3a4268 100644
>> --- a/gcc/ipa-param-manipulation.c
>> +++ b/gcc/ipa-param-manipulation.c
>> @@ -953,6 +953,99 @@ ipa_param_body_adjustments::carry_over_param (tree t)
>>    return new_parm;
>>  }
>>  
>> +/* Return true if BLOCKS_TO_COPY is NULL or if PHI has an argument ARG in
>> +   position that corresponds to an edge that is coming from a block that has
>> +   the corresponding bit set in BLOCKS_TO_COPY.  */
>> +
>> +static bool
>> +phi_arg_will_live_p (gphi *phi, bitmap blocks_to_copy, tree arg)
>> +{
>> +  bool arg_will_survive = false;
>> +  if (!blocks_to_copy)
>> +    arg_will_survive = true;
>> +  else
>> +    for (unsigned i = 0; i < gimple_phi_num_args (phi); i++)
>> +      if (gimple_phi_arg_def (phi, i) == arg
>> +      && bitmap_bit_p (blocks_to_copy,
>> +                       gimple_phi_arg_edge (phi, i)->src->index))
>> +    {
>> +      arg_will_survive = true;
>> +      break;
>> +    }
>> +  return arg_will_survive;
>> +}
>> +
>> +/* Populate m_dead_stmts given that DEAD_PARAM is going to be removed 
>> without
>> +   any replacement or splitting.  */
>> +
>> +void
>> +ipa_param_body_adjustments::mark_dead_statements (tree dead_param)
>> +{
>> +  if (!is_gimple_reg (dead_param))
>
> Hmm, so non-registers are not a problem?  I guess IPA SRA simply
> ensures there are no actual uses (but call arguments) in that case?
> Please clearly document this function matches IPA SRA capabilities.

OK, added.

>
>> +    return;
>> +  tree parm_ddef = ssa_default_def (m_id->src_cfun, dead_param);
>> +  if (!parm_ddef || has_zero_uses (parm_ddef))
>> +    return;
>> +
>> +  auto_vec<tree, 4> stack;
>> +  m_dead_ssas.add (parm_ddef);
>> +  stack.safe_push (parm_ddef);
>> +  while (!stack.is_empty ())
>> +    {
>> +      tree t = stack.pop ();
>> +
>> +      imm_use_iterator imm_iter;
>> +      gimple *stmt;
>> +
>> +      insert_decl_map (m_id, t, error_mark_node);
>> +      FOR_EACH_IMM_USE_STMT (stmt, imm_iter, t)
>> +    {
>> +      if (is_gimple_call (stmt)
>> +          || (m_id->blocks_to_copy
>> +              && !bitmap_bit_p (m_id->blocks_to_copy,
>> +                                gimple_bb (stmt)->index)))
>> +        continue;
>> +
>> +      if (is_gimple_debug (stmt))
>> +        {
>> +          m_dead_stmts.add (stmt);
>> +          gcc_assert (gimple_debug_bind_p (stmt));
>> +        }
>> +      else if (gimple_code (stmt) == GIMPLE_PHI)
>> +        {
>> +          gphi *phi = as_a <gphi *> (stmt);
>> +          if (phi_arg_will_live_p (phi, m_id->blocks_to_copy, t))
>> +            {
>> +              m_dead_stmts.add (phi);
>> +              tree res = gimple_phi_result (phi);
>> +              if (!m_dead_ssas.contains (res))
>
> You can combine this with the add below which returns false if the
> item was not already present.

I see, done.

>
>> +                {
>> +                  stack.safe_push (res);
>> +                  m_dead_ssas.add (res);
>> +                }
>> +            }
>> +        }

[...]

>> @@ -1519,6 +1619,19 @@ remap_split_decl_to_dummy (tree *tp, int 
>> *walk_subtrees, void *data)
>>    return NULL_TREE;
>>  }
>>  
>> +/* Given ARG which is a SSA_NAME call argument which we are however removing
>> +   from the current function and which will be thus removed from the call
>> +   statement by ipa_param_adjustments::modify_call, return something that 
>> can
>> +   be used as a placeholder and which the operand scanner will accept until
>> +   then.  */
>> +
>> +tree
>> +ipa_param_body_adjustments::get_removed_call_arg_placeholder (tree arg)
>> +{
>> +  tree t = create_tmp_var (TREE_TYPE (arg));
>
> If it is temporarily then use _raw.  It looks like you can get called
> multiple times for the same arg and each time you get a new temporary
> decl?  Ugh.
>
>> +  insert_decl_map (m_id, t, t);
>
> I wonder why you can't use/keep the SSA default def of the removed
> parameter instead?  Or a literal zero?  That is, below you don't
> seem to be anything special during inlining itself so I presume
> those are all removed by call redirection?  Why are the actual
> parameters then still needed?  (and error_mark_node doesn't work?)

Please see the last patch in the series, which removes the ugly decl and
puts here - for a defined time period - either error_mark_node (with
-g0) or DEBUG_EXPR_DECL (when generating debug info).  But I expect that
patch to be at least slightly controversial because it basically extends
gimple during IPA passes.

I tried but cannot cannot use literal zero here because that then leaks
into debug information and debugger thinks the parameter really was
zero.

So if the last patch in the series is not acceptable, I can see two
options:

1) We can decide we will simply never attempt to generate debug info for
   such removed parameters and then we can either have one such dummy
   decl per function (type mismatch hopefully would not matter) or
   remove the parameters outright and mark them as already removed in
   some bitmap on associated cgraph_edges.

2) Or we decide we do want to attempt generating debug info about the
   value of the removed parameter but not extend IL during IPA passes
   and then my plan was to create a unique dummy decl here and create a
   mapping between this decl and the DEBUG_EXPR_DECL containing debug
   info - and keep it up to date for the rest of IPA machinery.  But the
   code keeping things up to date when the created clone is itself
   cloned or inlined quickly got so ugly I decided to try and propose
   the IL extension where all of this happens automatically (as the
   testcase in the patch shows).

>
>> +  return t;
>> +}
>>  
>>  /* If the call statement pointed at by STMT_P contains any expressions that
>>     need to replaced with a different one as noted by ADJUSTMENTS, do so.  f 
>> the
>> @@ -1658,7 +1771,10 @@ ipa_param_body_adjustments::modify_call_stmt (gcall 
>> **stmt_p)
>>        else
>>          {
>>            tree t = gimple_call_arg (stmt, i);
>> -          modify_expression (&t, true);
>> +          if (TREE_CODE (t) == SSA_NAME && m_dead_ssas.contains(t))
>
> space after function call (likewise elsewhere).

Oops, fixed.

Thanks,

Martin

Reply via email to