aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorXavier Leroy <xavier.leroy@inria.fr>2015-07-08 11:42:39 +0200
committerXavier Leroy <xavier.leroy@inria.fr>2015-07-08 11:42:39 +0200
commitf869da75c970aec78975f7154c806f29e3012b7a (patch)
tree32bd1fc3fc05e51d7d339956b04196f80e0f2116
parent3eb2b3b7bb464d9844f7c161fbcff53f597348b9 (diff)
downloadcompcert-f869da75c970aec78975f7154c806f29e3012b7a.tar.gz
compcert-f869da75c970aec78975f7154c806f29e3012b7a.zip
Turn off copy optimization when returning a composite by reference.
The copy optimization is not correct in case of overlap between destination and source. We would need to use an hypothetical __builtin_memmove_aligned that can cope with overlap to implement the copy at return of callee.
-rw-r--r--cparser/StructReturn.ml14
-rw-r--r--test/regression/Makefile2
-rw-r--r--test/regression/Results/struct120
-rw-r--r--test/regression/struct12.c39
4 files changed, 50 insertions, 5 deletions
diff --git a/cparser/StructReturn.ml b/cparser/StructReturn.ml
index 660f1d9b..5e5602f3 100644
--- a/cparser/StructReturn.ml
+++ b/cparser/StructReturn.ml
@@ -299,10 +299,15 @@ let rec transf_expr env ctx e =
transf_call env ctx None fn args e.etyp
(* Function calls returning a composite by reference: add first argument.
- ctx = Effects: lv = f(...) -> f(&lv, ...) [copy optimization]
+ ctx = Effects: lv = f(...) -> f(&newtemp, ...), lv = newtemp
f(...) -> f(&newtemp, ...)
ctx = Val: lv = f(...) -> f(&newtemp, ...), lv = newtemp
f(...) -> f(&newtemp, ...), newtemp
+
+ We used to do a copy optimization:
+ ctx = Effects: lv = f(...) -> f(&lv, ...)
+ but it is not correct in case of overlap (see test/regression/struct12.c)
+
Function calls returning a composite by value:
ctx = Effects: lv = f(...) -> newtemp = f(...), lv = newtemp
f(...) -> f(...)
@@ -332,13 +337,14 @@ and transf_call env ctx opt_lhs fn args ty =
| Effects, None ->
let tmp = new_temp ~name:"_res" ty in
{edesc = ECall(fn', eaddrof tmp :: args'); etyp = TVoid []}
- | Effects, Some lhs ->
- {edesc = ECall(fn', eaddrof lhs :: args'); etyp = TVoid []}
+ (* Copy optimization, turned off as explained above *)
+ (* | Effects, Some lhs ->
+ {edesc = ECall(fn', eaddrof lhs :: args'); etyp = TVoid []} *)
| Val, None ->
let tmp = new_temp ~name:"_res" ty in
ecomma {edesc = ECall(fn', eaddrof tmp :: args'); etyp = TVoid []}
tmp
- | Val, Some lhs ->
+ | _, Some lhs ->
let tmp = new_temp ~name:"_res" ty in
ecomma {edesc = ECall(fn', eaddrof tmp :: args'); etyp = TVoid []}
(eassign lhs tmp)
diff --git a/test/regression/Makefile b/test/regression/Makefile
index da7d5755..e2d94aa9 100644
--- a/test/regression/Makefile
+++ b/test/regression/Makefile
@@ -15,7 +15,7 @@ LIBS=$(LIBMATH)
TESTS=int32 int64 floats floats-basics \
expr1 expr6 funptr2 initializers initializers2 initializers3 \
volatile1 volatile2 volatile3 \
- funct3 expr5 struct7 struct8 struct11 casts1 casts2 char1 \
+ funct3 expr5 struct7 struct8 struct11 struct12 casts1 casts2 char1 \
sizeof1 sizeof2 binops bool for1 switch switch2 compound \
decl1 interop1 bitfields9 ptrs3
diff --git a/test/regression/Results/struct12 b/test/regression/Results/struct12
new file mode 100644
index 00000000..e69de29b
--- /dev/null
+++ b/test/regression/Results/struct12
diff --git a/test/regression/struct12.c b/test/regression/struct12.c
new file mode 100644
index 00000000..39e62b28
--- /dev/null
+++ b/test/regression/struct12.c
@@ -0,0 +1,39 @@
+/* This is was originally a regression test for bug 43784 of gcc.
+ See ISO/IEC 9899:TC3 ยง6.8.6.4p4 and footnote 139. */
+
+#include <stdio.h>
+
+struct s {
+ unsigned char a[256];
+};
+union u {
+ struct { struct s b; int c; } d;
+ struct { int c; struct s b; } e;
+};
+
+static union u v;
+static struct s *p = &v.d.b;
+static struct s *q = &v.e.b;
+
+static struct s __attribute__((noinline)) rp(void)
+{
+ return *p;
+}
+
+static void qp(void)
+{
+ *q = rp();
+}
+
+int main()
+{
+ int i;
+ for (i = 0; i < 256; i++)
+ p->a[i] = i;
+ qp();
+ for (i = 0; i < 256; i++)
+ if (q->a[i] != i)
+ printf("ERROR at %d: %d\n", i, q->a[i]);
+ return 0;
+}
+