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

            Bug ID: 128756
           Summary: wrong code at -O1: A w = A{} = g; copies the {}
                    initializer of the temporary, not the assigned value,
                    since r15-8011
           Product: gcc
           Version: 17.0
            Status: UNCONFIRMED
          Keywords: wrong-code
          Severity: normal
          Priority: P3
         Component: c++
          Assignee: unassigned at gcc dot gnu.org
          Reporter: jeffchen006 at gmail dot com
  Target Milestone: ---
            Target: x86_64-pc-linux-gnu

#include <cstdio>
struct A {
  int i;
  char c = 0;
};
A g{5};
int main() {
  A w = A{} = g;
  printf("%d\n", w.i);
}

$ g++ -O0 t.cpp && ./a.out
5
$ g++ -O1 t.cpp && ./a.out
0

-O1, -O2, -O3 fail. Clang and GCC 14 give 5.

Expected 5: A{} is materialized, g is assigned to it (trivial copy assignment),
and w is initialized from the result.

A has tail padding and is not POD (default member initializer), so
build_over_call (cp/call.cc) does the trivial assignment as a 5-byte MEM copy.
-fdump-tree-original:

-O0:
  w = MEM <unsigned char[5]> [... SAVE_EXPR <TARGET_EXPR <D.3665, {.i=0,
.c=0}>, (struct A &) &D.3665> ...] = MEM <unsigned char[5]> [... &g],
      TARGET_EXPR <D.3696, *(const struct A &) (NON_LVALUE_EXPR <SAVE_EXPR
<TARGET_EXPR <D.3665, {.i=0, .c=0}>, (struct A &) &D.3665>>)>

-O1:
  w = MEM <unsigned char[5]> [... same ...] = MEM <unsigned char[5]> [... &g],
      TARGET_EXPR <D.3737, {.i=0, .c=0}>

So with optimization the front end folds the read *(SAVE_EXPR <TARGET_EXPR <D,
{.i=0, .c=0}>, &D>) to the initializer of D, and the 5-byte copy into D is
ignored. The gimple at -O1 is `MEM <unsigned char[5]> [D] = MEM <unsigned
char[5]> [&g]; w = {};`.

First bad commit: r15-8011-g3dd7b598065 (PR117512, bisected at -O1). It stopped
the *&e -> e folding in cp_build_indirect_ref_1 when the result is not an
lvalue, so the TARGET_EXPR read now reaches cp_fold (cp/cp-gimplify.cc), which
runs only when optimizing and folds it to the constant initializer.

The same happens with a derived class (B : A, A has tail padding): `B b = B{} =
B{{2,'x'}};` gives 2 120 at -O0 and 0 0 at -O1. g++.dg/cpp2a/class-copy1.C (the
PR127710 test) also aborts at -O1 and above.

-O0 -fsanitize=undefined,address is clean.

Cause: cp_fold (cp/cp-gimplify.cc), which runs only when optimizing, folds the
read `*(SAVE_EXPR <TARGET_EXPR <D, {.i=0, .c=0}>, &D>)` of the temporary to the
TARGET_EXPR initializer. The temporary comes from build_over_call (cp/call.cc),
which does the trivial operator= of a class with tail padding as a 5-byte MEM
copy into D; that copy is in the other operand of the COMPOUND_EXPR, so the
folded value ignores it.

gcc -Wall -Wextra gives no warnings. -fno-strict-aliasing -fwrapv does not
change the output.

Possible fix: One possible fix is to not fold, in cp_fold, an INDIRECT_REF of a
TARGET_EXPR (or of a SAVE_EXPR that contains one) to the TARGET_EXPR
initializer, and keep it as a read of the slot as at -O0; r15-8011 made the
same choice for cp_build_indirect_ref_1. We are not sure of the exact place in
cp_fold; a minimal guard would be to skip constant folding of a TARGET_EXPR
slot whose address is taken in the enclosing COMPOUND_EXPR.

gcc version 17.0.0 20261114 (experimental), r17-6275-gfceddda853f,
x86_64-pc-linux-gnu
Configured with: --enable-languages=c,c++ --disable-multilib
--disable-bootstrap --enable-checking=release

Reply via email to