This is the mail archive of the
gcc-patches@gcc.gnu.org
mailing list for the GCC project.
Fix thunk generation for functions returning DECL_BY_REFERENCE
- From: Jan Hubicka <hubicka at ucw dot cz>
- To: gcc-patches at gcc dot gnu dot org
- Date: Sat, 28 Feb 2015 21:35:01 +0100
- Subject: Fix thunk generation for functions returning DECL_BY_REFERENCE
- Authentication-results: sourceware.org; auth=none
Hi,
the Chromium miscompilation is caused by introducing extra copy when returning
non-POD object (as tested in the testcase). This is bug in
cgraph_node::expand_thunk not setting gimple_call_set_return_slot_opt (I copied
same code from ipa-split).
This bug should reproduce on all release branches for target not defining
assembler thunks.
Bootstrapped/regtested x86_64-linux, comitted, earlier version was tested by
Martin to fix chromium.
Honza
PR ipa/65236
* g++.dg/ipa/ipa-icf-6.C: New testcase.
* cgraphunit.c (cgraph_node::expand_thunk): Enable return slot
opt.
Index: testsuite/g++.dg/ipa/ipa-icf-6.C
===================================================================
--- testsuite/g++.dg/ipa/ipa-icf-6.C (revision 0)
+++ testsuite/g++.dg/ipa/ipa-icf-6.C (revision 0)
@@ -0,0 +1,37 @@
+/* { dg-do run } */
+/* { dg-options "-O3 -fdump-ipa-icf" } */
+
+struct A {
+ A() {ptr=&b;}
+ A(const A &a) {ptr = &b;}
+ void test() { if (ptr != &b) __builtin_abort ();}
+ int b;
+ int *ptr;
+};
+
+A test1(A a)
+{
+ a.test();
+ return a;
+}
+A test2(A a)
+{
+ a.test();
+ return a;
+}
+__attribute__ ((noinline))
+static void
+test_me (A (*t)(A))
+{
+ struct A a, b=t(a);
+ b.test ();
+}
+int
+main()
+{
+ test_me (test1);
+ test_me (test2);
+ return 0;
+}
+/* { dg-final { scan-ipa-dump-times "Unified; Wrapper has been created" 1 "icf" } } */
+/* { dg-final { cleanup-ipa-dump "icf" } } */
Index: cgraphunit.c
===================================================================
--- cgraphunit.c (revision 221076)
+++ cgraphunit.c (working copy)
@@ -1680,6 +1680,14 @@ cgraph_node::expand_thunk (bool output_a
callees->call_stmt = call;
gimple_call_set_from_thunk (call, true);
gimple_call_set_with_bounds (call, instrumentation_clone);
+
+ /* Return slot optimization is always possible and in fact requred to
+ return values with DECL_BY_REFERENCE. */
+ if (aggregate_value_p (resdecl, TREE_TYPE (thunk_fndecl))
+ && (!is_gimple_reg_type (TREE_TYPE (resdecl))
+ || DECL_BY_REFERENCE (resdecl)))
+ gimple_call_set_return_slot_opt (call, true);
+
if (restmp && !alias_is_noreturn)
{
gimple_call_set_lhs (call, restmp);