From: Jason Merrill Date: Tue, 14 Apr 2009 03:30:12 +0000 (-0400) Subject: re PR c++/39480 (generated memcpy causes trouble in assignment) X-Git-Tag: releases/gcc-4.3.4~225 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=b75c92e2ab96a2d54fc9376f3da36cc5f281b9ed;p=thirdparty%2Fgcc.git re PR c++/39480 (generated memcpy causes trouble in assignment) PR c++/39480 * call.c (build_over_call): Don't call memcpy if the target is the same as the source. From-SVN: r146020 --- diff --git a/gcc/cp/ChangeLog b/gcc/cp/ChangeLog index 0d2dbc63c91f..d363604a6d21 100644 --- a/gcc/cp/ChangeLog +++ b/gcc/cp/ChangeLog @@ -1,3 +1,9 @@ +2009-04-13 Jason Merrill + + PR c++/39480 + * call.c (build_over_call): Don't call memcpy if the target is + the same as the source. + 2009-04-10 H.J. Lu Backport from mainline: diff --git a/gcc/cp/call.c b/gcc/cp/call.c index 13560e218177..13713131d0b1 100644 --- a/gcc/cp/call.c +++ b/gcc/cp/call.c @@ -5118,17 +5118,33 @@ build_over_call (struct z_candidate *cand, int flags) else { /* We must only copy the non-tail padding parts. - Use __builtin_memcpy for the bitwise copy. */ + Use __builtin_memcpy for the bitwise copy. + FIXME fix 22488 so we can go back to using MODIFY_EXPR + instead of an explicit call to memcpy. */ tree arg0, arg1, arg2, t; + tree test = NULL_TREE; arg2 = TYPE_SIZE_UNIT (as_base); arg1 = arg; arg0 = build_unary_op (ADDR_EXPR, to, 0); + + if (!(optimize && flag_tree_ter)) + { + /* When TER is off get_pointer_alignment returns 0, so a call + to __builtin_memcpy is expanded as a call to memcpy, which + is invalid with identical args. When TER is on it is + expanded as a block move, which should be safe. */ + arg0 = save_expr (arg0); + arg1 = save_expr (arg1); + test = build2 (EQ_EXPR, boolean_type_node, arg0, arg1); + } t = implicit_built_in_decls[BUILT_IN_MEMCPY]; t = build_call_n (t, 3, arg0, arg1, arg2); t = convert (TREE_TYPE (arg0), t); + if (test) + t = build3 (COND_EXPR, TREE_TYPE (t), test, arg0, t); val = build_indirect_ref (t, 0); } diff --git a/gcc/testsuite/ChangeLog b/gcc/testsuite/ChangeLog index 58fea4b5e179..f791425ec862 100644 --- a/gcc/testsuite/ChangeLog +++ b/gcc/testsuite/ChangeLog @@ -1,3 +1,8 @@ +2009-04-13 Jason Merrill + + PR c++/39480 + * g++.dg/init/copy7.C: New. + 2009-04-10 H.J. Lu Backport from mainline: diff --git a/gcc/testsuite/g++.dg/init/copy7.C b/gcc/testsuite/g++.dg/init/copy7.C new file mode 100644 index 000000000000..f4364f329e53 --- /dev/null +++ b/gcc/testsuite/g++.dg/init/copy7.C @@ -0,0 +1,32 @@ +// PR c++/39480 +// It isn't always safe to call memcpy with identical arguments. +// { dg-do run } + +extern "C" void abort(); +extern "C" void * +memcpy(void *dest, void *src, __SIZE_TYPE__ n) +{ + abort(); +} + +struct A +{ + double d[10]; +}; + +struct B: public A +{ + char bc; +}; + +B b; + +void f(B *a1, B* a2) +{ + *a1 = *a2; +} + +int main() +{ + f(&b,&b); +}