aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJason Merrill <jason@redhat.com>2009-04-13 16:53:34 -0400
committerJason Merrill <jason@gcc.gnu.org>2009-04-13 16:53:34 -0400
commit15237f9a89b2d626073e1261e19b6608088e1d70 (patch)
treedfce435461a53d08f59ec5e247223e0c5474d547
parent1244a8b735508cea75ab03c7a72f221d31b2bb4f (diff)
downloadgcc-15237f9a89b2d626073e1261e19b6608088e1d70.zip
gcc-15237f9a89b2d626073e1261e19b6608088e1d70.tar.gz
gcc-15237f9a89b2d626073e1261e19b6608088e1d70.tar.bz2
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: r146011
-rw-r--r--gcc/cp/ChangeLog6
-rw-r--r--gcc/cp/call.c14
-rw-r--r--gcc/testsuite/ChangeLog5
-rw-r--r--gcc/testsuite/g++.dg/init/copy7.C32
4 files changed, 57 insertions, 0 deletions
diff --git a/gcc/cp/ChangeLog b/gcc/cp/ChangeLog
index 905b6e4..d77634d 100644
--- a/gcc/cp/ChangeLog
+++ b/gcc/cp/ChangeLog
@@ -1,3 +1,9 @@
+2009-04-09 Jason Merrill <jason@redhat.com>
+
+ PR c++/39480
+ * call.c (build_over_call): Don't call memcpy if the target is
+ the same as the source.
+
2009-04-13 Jason Merrill <jason@redhat.com>
PR c++/39750
diff --git a/gcc/cp/call.c b/gcc/cp/call.c
index 869146e..ef7c045 100644
--- a/gcc/cp/call.c
+++ b/gcc/cp/call.c
@@ -5396,14 +5396,28 @@ build_over_call (struct z_candidate *cand, int flags, tsubst_flags_t complain)
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 = cp_build_unary_op (ADDR_EXPR, to, 0, complain);
+
+ 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 = cp_build_indirect_ref (t, 0, complain);
}
diff --git a/gcc/testsuite/ChangeLog b/gcc/testsuite/ChangeLog
index d29496f..457aee6 100644
--- a/gcc/testsuite/ChangeLog
+++ b/gcc/testsuite/ChangeLog
@@ -1,3 +1,8 @@
+2009-04-09 Jason Merrill <jason@redhat.com>
+
+ PR c++/39480
+ * g++.dg/init/copy7.C: New.
+
2009-04-13 H.J. Lu <hongjiu.lu@intel.com>
PR testsuite/39733
diff --git a/gcc/testsuite/g++.dg/init/copy7.C b/gcc/testsuite/g++.dg/init/copy7.C
new file mode 100644
index 0000000..f4364f3
--- /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);
+}