public inbox for gcc-patches@gcc.gnu.org
 help / color / mirror / Atom feed
* [PATCH][PR 56727] Bypass PLT for recursive calls
@ 2017-07-01 20:43 Yuri Gribov
  2017-07-01 20:56 ` Rainer Orth
  0 siblings, 1 reply; 3+ messages in thread
From: Yuri Gribov @ 2017-07-01 20:43 UTC (permalink / raw)
  To: GCC Patches; +Cc: Jan Hubicka, Jakub Jelinek, Alexander Monakov

[-- Attachment #1: Type: text/plain, Size: 359 bytes --]

Hi all,

This is a fix for https://gcc.gnu.org/bugzilla/show_bug.cgi?id=56727
which replaces recursive PLT calls with direct calls to static
aliases.

In presense of function aliases this patch takes a conservative
approach and keeps the PLT call (Jan suggested to optimize more
aggressively even when aliases are present but I haven't implemented
this).

-Y

[-- Attachment #2: pr56727.patch --]
[-- Type: application/octet-stream, Size: 3066 bytes --]

2017-07-01  Yury Gribov  <tetra2005@gmail.com>

	PR middle-end/56727
	* ipa-visibility (function_and_variable_visibility): Convert
	recursive PLT call to direct call if appropriate.
	* testsuite/gcc.dg/pr56727-1.c: New test.
	* testsuite/gcc.dg/pr56727-2.c: New test.

diff -rupN gcc/gcc/ipa-visibility.c gcc-56727/gcc/ipa-visibility.c
--- gcc/gcc/ipa-visibility.c	2017-06-29 21:14:57.000000000 +0200
+++ gcc-56727/gcc/ipa-visibility.c	2017-07-01 22:03:34.000000000 +0200
@@ -617,6 +618,36 @@ function_and_variable_visibility (bool w
   /* All aliases should be procssed at this point.  */
   gcc_checking_assert (!alias_pairs || !alias_pairs->length ());
 
+  FOR_EACH_DEFINED_FUNCTION (node)
+    {
+      if (node->get_availability () != AVAIL_INTERPOSABLE
+          || node->can_be_discarded_p ()
+          || node->has_aliases_p ())
+        continue;
+
+      cgraph_node *alias = 0;
+      for (cgraph_edge *e = node->callees; e; e = e->next_callee)
+        {
+          /* Recursive function calls usually can't be interposed.  */
+
+          if (!e->recursive_p ())
+            continue;
+
+          if (!alias)
+            { 
+              alias = dyn_cast<cgraph_node *> (node->noninterposable_alias ());
+              gcc_assert (alias && alias != node);
+            }
+
+          e->redirect_callee (alias);
+          if (gimple_has_body_p (e->caller->decl))
+            { 
+              push_cfun (DECL_STRUCT_FUNCTION (e->caller->decl));
+              e->redirect_call_stmt_to_callee ();
+              pop_cfun (); 
+            }
+        }
+    }
   FOR_EACH_FUNCTION (node)
     {
       int flags = flags_from_decl_or_type (node->decl);
diff -rupN gcc/gcc/testsuite/gcc.dg/pr56727-1.c gcc-56727/gcc/testsuite/gcc.dg/pr56727-1.c
--- gcc/gcc/testsuite/gcc.dg/pr56727-1.c	1970-01-01 01:00:00.000000000 +0100
+++ gcc-56727/gcc/testsuite/gcc.dg/pr56727-1.c	2017-07-01 21:36:36.000000000 +0200
@@ -0,0 +1,23 @@
+/* { dg-do compile } */
+/* { dg-options "-O2 -fPIC" } */
+/* { dg-final { scan-assembler-not "@PLT" } } */
+
+#define define_func(type) \
+  void f_ ## type (type b) { f_ ## type (0); } \
+  void __attribute__((noinline, noclone)) f_noinline_ ## type (type b) \
+  { f_noinline_ ## type (0); }
+
+define_func(char)
+define_func(short)
+define_func(int)
+define_func(long)
+
+int foo(int n)
+{
+  return (n == 1 || n == 2) ? 1 : foo(n-1) * foo(n-2);
+}
+
+int __attribute__((noinline, noclone)) foo_noinline(int n)
+{
+  return (n == 1 || n == 2) ? 1 : foo_noinline(n-1) * foo_noinline(n-2);
+}
diff -rupN gcc/gcc/testsuite/gcc.dg/pr56727-2.c gcc-56727/gcc/testsuite/gcc.dg/pr56727-2.c
--- gcc/gcc/testsuite/gcc.dg/pr56727-2.c	1970-01-01 01:00:00.000000000 +0100
+++ gcc-56727/gcc/testsuite/gcc.dg/pr56727-2.c	2017-07-01 21:58:03.000000000 +0200
@@ -0,0 +1,16 @@
+/* { dg-do compile } */
+/* { dg-options "-O2 -fPIC" } */
+/* { dg-final { scan-assembler "@PLT" } } */
+
+__attribute__((noinline, noclone))
+void f (short b)
+{
+  f (0);
+}
+
+static void g (short) __attribute__ ((alias ("f")));
+
+void h ()
+{
+  g (0);
+}

^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: [PATCH][PR 56727] Bypass PLT for recursive calls
  2017-07-01 20:43 [PATCH][PR 56727] Bypass PLT for recursive calls Yuri Gribov
@ 2017-07-01 20:56 ` Rainer Orth
  2017-07-02  7:53   ` Yuri Gribov
  0 siblings, 1 reply; 3+ messages in thread
From: Rainer Orth @ 2017-07-01 20:56 UTC (permalink / raw)
  To: Yuri Gribov; +Cc: GCC Patches, Jan Hubicka, Jakub Jelinek, Alexander Monakov

Hi Yuri,

> diff -rupN gcc/gcc/testsuite/gcc.dg/pr56727-1.c gcc-56727/gcc/testsuite/gcc.dg/pr56727-1.c
> --- gcc/gcc/testsuite/gcc.dg/pr56727-1.c	1970-01-01 01:00:00.000000000 +0100
> +++ gcc-56727/gcc/testsuite/gcc.dg/pr56727-1.c	2017-07-01 21:36:36.000000000 +0200
> @@ -0,0 +1,23 @@
> +/* { dg-do compile } */
> +/* { dg-options "-O2 -fPIC" } */

both tests need to be restricted to target fpic...

> +/* { dg-final { scan-assembler-not "@PLT" } } */

... and @PLT won't work everywhere, either.  Judging from gcc/config, it
will be i386/x86_64 (except darwin), microblaze, mn10300, s390, sh, and
xtensa at most.

	Rainer

-- 
-----------------------------------------------------------------------------
Rainer Orth, Center for Biotechnology, Bielefeld University

^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: [PATCH][PR 56727] Bypass PLT for recursive calls
  2017-07-01 20:56 ` Rainer Orth
@ 2017-07-02  7:53   ` Yuri Gribov
  0 siblings, 0 replies; 3+ messages in thread
From: Yuri Gribov @ 2017-07-02  7:53 UTC (permalink / raw)
  To: Rainer Orth; +Cc: GCC Patches, Jan Hubicka, Jakub Jelinek, Alexander Monakov

On Sat, Jul 1, 2017 at 9:56 PM, Rainer Orth <ro@cebitec.uni-bielefeld.de> wrote:
> Hi Yuri,
>
>> diff -rupN gcc/gcc/testsuite/gcc.dg/pr56727-1.c gcc-56727/gcc/testsuite/gcc.dg/pr56727-1.c
>> --- gcc/gcc/testsuite/gcc.dg/pr56727-1.c      1970-01-01 01:00:00.000000000 +0100
>> +++ gcc-56727/gcc/testsuite/gcc.dg/pr56727-1.c        2017-07-01 21:36:36.000000000 +0200
>> @@ -0,0 +1,23 @@
>> +/* { dg-do compile } */
>> +/* { dg-options "-O2 -fPIC" } */
>
> both tests need to be restricted to target fpic...
>
>> +/* { dg-final { scan-assembler-not "@PLT" } } */
>
> ... and @PLT won't work everywhere, either.  Judging from gcc/config, it
> will be i386/x86_64 (except darwin), microblaze, mn10300, s390, sh, and
> xtensa at most.

Thanks Rainer, I'll update the patch. A pity that this can not be
tested in cross-platform way.

-Y

^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2017-07-02  7:53 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2017-07-01 20:43 [PATCH][PR 56727] Bypass PLT for recursive calls Yuri Gribov
2017-07-01 20:56 ` Rainer Orth
2017-07-02  7:53   ` Yuri Gribov

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).