i386: Insert ENDBR before the profiling counter call
authorH.J. Lu <hongjiu.lu@intel.com>
Mon, 24 Sep 2018 17:20:58 +0000 (17:20 +0000)
committerH.J. Lu <hjl@gcc.gnu.org>
Mon, 24 Sep 2018 17:20:58 +0000 (10:20 -0700)
ENDBR must be the first instruction of a function.  This patch queues
ENDBR if we need to put the profiling counter call before the prologue
and generate ENDBR before the profiling counter call.

gcc/

PR target/82699
* config/i386/i386.c (rest_of_insert_endbranch): Set
endbr_queued_at_entrance to true and don't insert ENDBR if
x86_function_profiler will be called.
(x86_function_profiler): Insert ENDBR if endbr_queued_at_entrance
is true.
* config/i386/i386.h (machine_function): Add
endbr_queued_at_entrance.

gcc/testsuite/

PR target/82699
* gcc.target/i386/pr82699-1.c: New file.
* gcc.target/i386/pr82699-2.c: Likewise.
* gcc.target/i386/pr82699-3.c: Likewise.
* gcc.target/i386/pr82699-4.c: Likewise.
* gcc.target/i386/pr82699-5.c: Likewise.
* gcc.target/i386/pr82699-6.c: Likewise.

From-SVN: r264540

gcc/ChangeLog
gcc/config/i386/i386.c
gcc/config/i386/i386.h
gcc/testsuite/ChangeLog
gcc/testsuite/gcc.target/i386/pr82699-1.c [new file with mode: 0644]
gcc/testsuite/gcc.target/i386/pr82699-2.c [new file with mode: 0644]
gcc/testsuite/gcc.target/i386/pr82699-3.c [new file with mode: 0644]
gcc/testsuite/gcc.target/i386/pr82699-4.c [new file with mode: 0644]
gcc/testsuite/gcc.target/i386/pr82699-5.c [new file with mode: 0644]
gcc/testsuite/gcc.target/i386/pr82699-6.c [new file with mode: 0644]

index 7a85aecbd9e16084679c6ac788f42189304d223d..b3d14b95530d82fae355d4b5ee508e2757bbd958 100644 (file)
@@ -1,3 +1,14 @@
+2018-09-24  H.J. Lu  <hongjiu.lu@intel.com>
+
+       PR target/82699
+       * config/i386/i386.c (rest_of_insert_endbranch): Set
+       endbr_queued_at_entrance to true and don't insert ENDBR if
+       x86_function_profiler will be called.
+       (x86_function_profiler): Insert ENDBR if endbr_queued_at_entrance
+       is true.
+       * config/i386/i386.h (machine_function): Add
+       endbr_queued_at_entrance.
+
 2018-09-24  Ilya Leoshkevich  <iii@linux.ibm.com>
 
        * genattrtab.c (mk_attr_alt): Use alternative_mask.
index 6dd31309495147a13b1beba5f83c0f5a1e4b9e1d..052ca63e4604f9fabe35a7386889e3267d81e24a 100644 (file)
@@ -2593,11 +2593,17 @@ rest_of_insert_endbranch (void)
                         TYPE_ATTRIBUTES (TREE_TYPE (cfun->decl)))
       && !cgraph_node::get (cfun->decl)->only_called_directly_p ())
     {
-      cet_eb = gen_nop_endbr ();
+      /* Queue ENDBR insertion to x86_function_profiler.  */
+      if (crtl->profile && flag_fentry)
+       cfun->machine->endbr_queued_at_entrance = true;
+      else
+       {
+         cet_eb = gen_nop_endbr ();
 
-      bb = ENTRY_BLOCK_PTR_FOR_FN (cfun)->next_bb;
-      insn = BB_HEAD (bb);
-      emit_insn_before (cet_eb, insn);
+         bb = ENTRY_BLOCK_PTR_FOR_FN (cfun)->next_bb;
+         insn = BB_HEAD (bb);
+         emit_insn_before (cet_eb, insn);
+       }
     }
 
   bb = 0;
@@ -41203,6 +41209,10 @@ x86_function_profiler (FILE *file, int labelno ATTRIBUTE_UNUSED)
 {
   const char *mcount_name = (flag_fentry ? MCOUNT_NAME_BEFORE_PROLOGUE
                                         : MCOUNT_NAME);
+
+  if (cfun->machine->endbr_queued_at_entrance)
+    fprintf (file, "\t%s\n", TARGET_64BIT ? "endbr64" : "endbr32");
+
   if (TARGET_64BIT)
     {
 #ifndef NO_PROFILE_COUNTERS
index 2fa9f2d53c4159a91999dde19fdfe33e1326b362..e77dac7ef3888db3d79decab0ec4d83e8fa1ed27 100644 (file)
@@ -2747,6 +2747,9 @@ struct GTY(()) machine_function {
   /* Nonzero if the function places outgoing arguments on stack.  */
   BOOL_BITFIELD outgoing_args_on_stack : 1;
 
+  /* If true, ENDBR is queued at function entrance.  */
+  BOOL_BITFIELD endbr_queued_at_entrance : 1;
+
   /* The largest alignment, in bytes, of stack slot actually used.  */
   unsigned int max_used_stack_alignment;
 
index 6e76e500c9d83d24f4a44c96aa65e63668f13090..b98b6ebf79b5e7a6044ac88efbafa9686508477c 100644 (file)
@@ -1,3 +1,13 @@
+2018-09-24  H.J. Lu  <hongjiu.lu@intel.com>
+
+       PR target/82699
+       * gcc.target/i386/pr82699-1.c: New file.
+       * gcc.target/i386/pr82699-2.c: Likewise.
+       * gcc.target/i386/pr82699-3.c: Likewise.
+       * gcc.target/i386/pr82699-4.c: Likewise.
+       * gcc.target/i386/pr82699-5.c: Likewise.
+       * gcc.target/i386/pr82699-6.c: Likewise.
+
 2018-09-24  Thomas Koenig  <tkoenig@gcc.gnu.org>
 
        PR fortran/87401
diff --git a/gcc/testsuite/gcc.target/i386/pr82699-1.c b/gcc/testsuite/gcc.target/i386/pr82699-1.c
new file mode 100644 (file)
index 0000000..272d079
--- /dev/null
@@ -0,0 +1,11 @@
+/* { dg-do compile { target *-*-linux* } } */
+/* { dg-options "-O2 -fno-pic -fcf-protection -pg -fasynchronous-unwind-tables" } */
+/* { dg-final { scan-assembler-times {\t\.cfi_startproc\n\tendbr} 1 } } */
+
+extern int bar (int);
+
+int
+foo (int i)
+{
+  return bar (i);
+}
diff --git a/gcc/testsuite/gcc.target/i386/pr82699-2.c b/gcc/testsuite/gcc.target/i386/pr82699-2.c
new file mode 100644 (file)
index 0000000..07a4ccb
--- /dev/null
@@ -0,0 +1,11 @@
+/* { dg-do compile { target *-*-linux* } } */
+/* { dg-options "-O2 -fno-pic -fcf-protection -pg -mfentry -fasynchronous-unwind-tables" } */
+/* { dg-final { scan-assembler-times {\t\.cfi_startproc\n\tendbr} 1 } } */
+
+extern int bar (int);
+
+int
+foo (int i)
+{
+  return bar (i);
+}
diff --git a/gcc/testsuite/gcc.target/i386/pr82699-3.c b/gcc/testsuite/gcc.target/i386/pr82699-3.c
new file mode 100644 (file)
index 0000000..08fa0e7
--- /dev/null
@@ -0,0 +1,11 @@
+/* { dg-do compile { target *-*-linux* } } */
+/* { dg-options "-O2 -fpic -fcf-protection -pg -fasynchronous-unwind-tables" } */
+/* { dg-final { scan-assembler-times {\t\.cfi_startproc\n\tendbr} 1 } } */
+
+extern int bar (int);
+
+int
+foo (int i)
+{
+  return bar (i);
+}
diff --git a/gcc/testsuite/gcc.target/i386/pr82699-4.c b/gcc/testsuite/gcc.target/i386/pr82699-4.c
new file mode 100644 (file)
index 0000000..3cc03db
--- /dev/null
@@ -0,0 +1,11 @@
+/* { dg-do compile { target { *-*-linux* && { ! ia32 } } } } */
+/* { dg-options "-O2 -fpic -fcf-protection -pg -mfentry -fasynchronous-unwind-tables" } */
+/* { dg-final { scan-assembler-times {\t\.cfi_startproc\n\tendbr} 1 } } */
+
+extern int bar (int);
+
+int
+foo (int i)
+{
+  return bar (i);
+}
diff --git a/gcc/testsuite/gcc.target/i386/pr82699-5.c b/gcc/testsuite/gcc.target/i386/pr82699-5.c
new file mode 100644 (file)
index 0000000..e0fe018
--- /dev/null
@@ -0,0 +1,11 @@
+/* { dg-do compile { target *-*-linux* } } */
+/* { dg-options "-O2 -fcf-protection -mfentry -fasynchronous-unwind-tables" } */
+/* { dg-final { scan-assembler-times {\t\.cfi_startproc\n\tendbr} 1 } } */
+
+extern int bar (int);
+
+int
+foo (int i)
+{
+  return bar (i);
+}
diff --git a/gcc/testsuite/gcc.target/i386/pr82699-6.c b/gcc/testsuite/gcc.target/i386/pr82699-6.c
new file mode 100644 (file)
index 0000000..cacf0ab
--- /dev/null
@@ -0,0 +1,11 @@
+/* { dg-do compile { target *-*-linux* } } */
+/* { dg-options "-O2 -fcf-protection -pg -mfentry -mrecord-mcount -mnop-mcount -fasynchronous-unwind-tables" } */
+/* { dg-final { scan-assembler-times {\t\.cfi_startproc\n\tendbr} 1 } } */
+
+extern int bar (int);
+
+int
+foo (int i)
+{
+  return bar (i);
+}