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

--- Comment #6 from Jakub Jelinek <jakub at gcc dot gnu.org> ---
cp_gimplify_expr uses for CALL_EXPR_REVERSE_ARGS and CALL_EXPR_ORDERED_ARGS
(and for !CALL_EXPR_OPERATOR_SYNTAX method calls) gimplify_arg, but that
clearly isn't enough once there are any TREE_SIDE_EFFECTS on any of the
arguments.
Because e.g. for the ++i, ++i arguments for scalar non-addressable VAR_DECL i,
we end up with:
      i = i + 1;
      i = i + 1;
      C::C (&p, i, i);
which is wrong, is_gimple_val is true on i, but we really need to gimplify the
argument using gimplify_to_rvalue with is_gimple_val test if is_gimple_reg_type
(TREE_TYPE (arg)) and any of the args that are supposed to be evaluated after
it has TREE_SIDE_EFFECTS set.
That seems an easy change and would be (except for -O0) even cheap.
This would fix foo in the #c5 testcase.
bar works because i is addressable and thus gimplification already forces it
into  SSA_NAME or temporary.
baz works because the (int &) casts also force
C::C (&p, *( ++i;, (int &) &i;), *( ++i;, (int &) &i;))
before gimplification, so it is again gimplified into SSA_NAMEs or temporaries.
But, qux needs some other fix.
B::B (&p, TARGET_EXPR <D.2274, *(const struct A &) A::operator++ (&i)>,
TARGET_EXPR <D.2275, *(const struct A &) A::operator++ (&i)>)
The problem is that gimplify_arg optimizes:
  /* In general, we allow lvalues for function arguments to avoid
     extra overhead of copying large aggregates out of even larger
     aggregates into temporaries only to copy the temporaries to
     the argument list.  Make optimizers happy by pulling out to
     temporaries those types that fit in registers.  */
  if (is_gimple_reg_type (TREE_TYPE (*arg_p)))
    test = is_gimple_val, fb = fb_rvalue;
  else
    {
      test = is_gimple_lvalue, fb = fb_either;
      /* Also strip a TARGET_EXPR that would force an extra copy.  */
      if (TREE_CODE (*arg_p) == TARGET_EXPR)
        {
          tree init = TARGET_EXPR_INITIAL (*arg_p);
          if (init
              && !VOID_TYPE_P (TREE_TYPE (init)))
            *arg_p = init;
        }
    }
where the stripping of the TARGET_EXPR avoids the copy that is needed.
So perhaps add some ordered argument to gimplify_arg, in calls from gimplify.c
pass false, and in calls from cp_gimplify_expr pass true if the current
argument in the desired evaluation order is followed by any TREE_SIDE_EFFECTS
arguments.

Reply via email to