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

--- Comment #18 from CVS Commits <cvs-commit at gcc dot gnu.org> ---
The master branch has been updated by Jakub Jelinek <ja...@gcc.gnu.org>:

https://gcc.gnu.org/g:7276a18aba41eed65c0cf535ae029e0ceeca6c77

commit r12-7686-g7276a18aba41eed65c0cf535ae029e0ceeca6c77
Author: Jakub Jelinek <ja...@redhat.com>
Date:   Thu Mar 17 09:23:45 2022 +0100

    gimplify: Emit clobbers for TARGET_EXPR_SLOT vars later [PR103984]

    As mentioned in the PR, we emit a bogus uninitialized warning but
    easily could emit wrong-code for it or similar testcases too.
    The bug is that we emit clobber for a TARGET_EXPR_SLOT too early:
              D.2499.e = B::qux (&h); [return slot optimization]
              D.2516 = 1;
              try
                {
                  B::B (&D.2498, &h);
                  try
                    {
                      _2 = baz (&D.2498);
                      D.2499.f = _2;
                      D.2516 = 0;
                      try
                        {
                          try
                            {
                              bar (&D.2499);
                            }
                          finally
                            {
                              C::~C (&D.2499);
                            }
                        }
                      finally
                        {
                          D.2499 = {CLOBBER(eol)};
                        }
                    }
                  finally
                    {
                      D.2498 = {CLOBBER(eol)};
                    }
                }
              catch
                {
                  if (D.2516 != 0) goto <D.2517>; else goto <D.2518>;
                  <D.2517>:
                  A::~A (&D.2499.e);
                  goto <D.2519>;
                  <D.2518>:
                  <D.2519>:
                }
    The CLOBBER for D.2499 is essentially only emitted on the non-exceptional
    path, if B::B or baz throws, then there is no CLOBBER for it but there
    is a conditional destructor A::~A (&D.2499.e).  Now, ehcleanup1
    sink_clobbers optimization assumes that clobbers in the EH cases are
    emitted after last use and so sinks the D.2499 = {CLOBBER(eol)}; later,
    so we then have
      # _3 = PHI <1(3), 0(9)>
    <L2>:
      D.2499 ={v} {CLOBBER(eol)};
      D.2498 ={v} {CLOBBER(eol)};
      if (_3 != 0)
        goto <bb 11>; [INV]
      else
        goto <bb 15>; [INV]

      <bb 11> :
      _35 = D.2499.a;
      if (&D.2499.b != _35)
    where that _35 = D.2499.a comes from inline expansion of the A::~A dtor,
    and that is a load from a clobbered memory.

    Now, what the gimplifier sees in this case is a CLEANUP_POINT_EXPR with
    somewhere inside of it a TARGET_EXPR for D.2499 (with the C::~C (&D.2499)
    cleanup) which in its TARGET_EXPR_INITIAL has another TARGET_EXPR for
    D.2516 bool flag which has CLEANUP_EH_ONLY which performs that conditional
    A::~A (&D.2499.e) call.
    The following patch ensures that CLOBBERs (and asan poisoning) are emitted
    after even those gimple_push_cleanup pushed cleanups from within the
    TARGET_EXPR_INITIAL gimplification (i.e. the last point where the slot
could
    be in theory used).  In my first version of the patch I've done it by just
    moving the
          /* Add a clobber for the temporary going out of scope, like
             gimplify_bind_expr.  */
          if (gimplify_ctxp->in_cleanup_point_expr
              && needs_to_live_in_memory (temp))
            {
    ...
            }
    block earlier in gimplify_target_expr, but that regressed a couple of tests
    where temp is marked TREE_ADDRESSABLE only during (well, very early during
    that) the gimplification of TARGET_EXPR_INITIAL, so we didn't emit e.g. on
    pr80032.C or stack2.C tests any clobbers for the slots and thus stack slot
    reuse wasn't performed.
    So that we don't regress those tests, this patch gimplifies
    TARGET_EXPR_INITIAL as before, but doesn't emit it directly into pre_p,
    emits it into a temporary sequence.  Then emits the CLOBBER cleanup
    into pre_p, then asan poisoning if needed, then appends the
    TARGET_EXPR_INITIAL temporary sequence and finally adds TARGET_EXPR_CLEANUP
    gimple_push_cleanup.  The earlier a GIMPLE_WCE appears in the sequence, the
    outer try/finally or try/catch it is.
    So, with this patch the part of the testcase in gimple dump cited above
    looks instead like:
              try
                {
                  D.2499.e = B::qux (&h); [return slot optimization]
                  D.2516 = 1;
                  try
                    {
                      try
                        {
                          B::B (&D.2498, &h);
                          _2 = baz (&D.2498);
                          D.2499.f = _2;
                          D.2516 = 0;
                          try
                            {
                              bar (&D.2499);
                            }
                          finally
                            {
                              C::~C (&D.2499);
                            }
                        }
                      finally
                        {
                          D.2498 = {CLOBBER(eol)};
                        }
                    }
                  catch
                    {
                      if (D.2516 != 0) goto <D.2517>; else goto <D.2518>;
                      <D.2517>:
                      A::~A (&D.2499.e);
                      goto <D.2519>;
                      <D.2518>:
                      <D.2519>:
                    }
                }
              finally
                {
                  D.2499 = {CLOBBER(eol)};
                }

    2022-03-17  Jakub Jelinek  <ja...@redhat.com>

            PR middle-end/103984
            * gimplify.cc (gimplify_target_expr): Gimplify type sizes and
            TARGET_EXPR_INITIAL into a temporary sequence, then push clobbers
            and asan unpoisioning, then append the temporary sequence and
            finally the TARGET_EXPR_CLEANUP clobbers.

            * g++.dg/opt/pr103984.C: New test.

Reply via email to