https://gcc.gnu.org/bugzilla/show_bug.cgi?id=87737

Richard Biener <rguenth at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|UNCONFIRMED                 |NEW
   Last reconfirmed|                            |2018-10-25
          Component|tree-optimization           |fortran
     Ever confirmed|0                           |1

--- Comment #2 from Richard Biener <rguenth at gcc dot gnu.org> ---
Confirmed.

923             SSA_NAME_DEF_STMT (*tp) = wi_p->stmt;
(gdb) l
918       if (TREE_CODE (*tp) == SSA_NAME)
919         {
920           *tp = remap_ssa_name (*tp, id);
921           *walk_subtrees = 0;
922           if (is_lhs)
923             SSA_NAME_DEF_STMT (*tp) = wi_p->stmt;
924           return NULL;
925         }
926       else if (auto_var_in_fn_p (*tp, fn))
927         {
(gdb) p *tp
$1 = <addr_expr 0x7ffff6a62ec0>
(gdb) p is_lhs
$2 = true

we do not expect an invariant for remapping a definition.

# .MEM_4 = VDEF <.MEM_3(D)>
&str.6 = __builtin_malloc (1);

that looks like the following before remapping:

# .MEM_4 = VDEF <.MEM_3(D)>
__result_5 = __builtin_malloc (1);

and we remap __result_5 to the bogus value in remap_ssa_name via

  /* Do not set DEF_STMT yet as statement is not copied yet. We do that
     in copy_bb.  */
  new_tree = remap_decl (var, id);

where we remapped the PARM_DECL __result accordingly.

It smells like registering this __result mapping is bogus.  Indeed we do this
here:

  /* If the parameter is never assigned to, has no SSA_NAMEs created,
     we would not need to create a new variable here at all, if it
     weren't for debug info.  Still, we can just use the argument
     value.  */
  if (TREE_READONLY (p)
      && !TREE_ADDRESSABLE (p)
      && value && !TREE_SIDE_EFFECTS (value)
      && !def)
    {
      /* We may produce non-gimple trees by adding NOPs or introduce
         invalid sharing when operand is not really constant.
         It is not big deal to prohibit constant propagation here as
         we will constant propagate in DOM1 pass anyway.  */
      if (is_gimple_min_invariant (value)
          && useless_type_conversion_p (TREE_TYPE (p),
                                                 TREE_TYPE (value))
          /* We have to be very careful about ADDR_EXPR.  Make sure
             the base variable isn't a local variable of the inlined
             function, e.g., when doing recursive inlining, direct or
             mutually-recursive or whatever, which is why we don't
             just test whether fn == current_function_decl.  */
          && ! self_inlining_addr_expr (value, fn))
        {
          insert_decl_map (id, p, value);
          insert_debug_decl_map (id, p, var);
          return insert_init_debug_bind (id, bb, var, value, NULL);
        }
    }

but has no SSA_NAMEs created is just checked via looking for a default-def.

So the above looks like premature optimization to me unless we really
want to walk _all_ SSA names to look for one that eventually uses p
as SSA_NAME_VAR ...

Replacing !def with !is_gimple_reg (p) might be the least invasive fix...

What is odd of course is that the parameter is appearantly TREE_READONLY
but it is assigned to...  That's probably because of the allocatable
flag?

Thus, the FE setting TREE_READONLY on __result is another place that
could get the fix - and that sounds better given TREE_READONLY might
have other unintended side-effects here.

Reply via email to