public inbox for gcc-patches@gcc.gnu.org
 help / color / mirror / Atom feed
* [RS6000] ABI_V4 init of toc section
@ 2016-01-29 16:38 Alan Modra
  2016-01-29 18:20 ` David Edelsohn
  2016-02-01 18:02 ` David Edelsohn
  0 siblings, 2 replies; 5+ messages in thread
From: Alan Modra @ 2016-01-29 16:38 UTC (permalink / raw)
  To: gcc-patches; +Cc: David Edelsohn

Since 4c4a180d, LTO has turned off flag_pic when linking a fixed
position executable.  This results in flag_pic being zero in
rs6000_file_start, and no definition of ".LCTOC1".

However, when we get to actually emitting code, flag_pic may be on
again, and references made to ".LCTOC1".  How flag_pic comes to be
enabled again is quite a story.  It goes like this..  If a function is
compiled with -fPIC then sysv4.h SUBTARGET_OVERRIDE_OPTIONS will set
TARGET_RELOCATABLE.  Conversely, if TARGET_RELOCATABLE is set and
flag_pic is zero, then SUBTARGET_OVERRIDE_OPTIONS will set flag_pic=2.
It also happens that TARGET_RELOCATABLE is a bit in rs6000_isa_flags,
which is handled by rs6000_function_specific_save and
rs6000_function_specific_restore.  That last fact means lto streaming
keeps track of the state of TARGET_RELOCATABLE for functions, and when
options are restored for a given function we'll set flag_pic=2 if the
function was originally compiled with -fPIC.  That's bad because it
defeats the purpose of the 4c4a180d lto change, resulting in worse
optimization of ppc32 executables.  What's more, we don't seem to turn
off flag_pic once it is on.

We should really untangle the flag_pic/TARGET_RELOCATABLE mess, but
that change is probably a little dangerous for stage4.  Instead, this
patch removes the toc symbol initialization from file_start and does
so when the first item is emitted to the toc, or after the function
epilogue in the cases where we emit code to initialize a toc pointer
but don't actually use it (-O0 mostly, I think).

Bootstrapped and regression tested powerpc64-linux biarch with all
languages enabled.  OK to apply?

	PR target/68662
	* config/rs6000/rs6000.c (need_toc_init): New var, set it
	whenever toc_label_name used.
	(rs6000_file_start): Don't set up toc section here,
	(rs6000_output_function_epilogue): do so here instead,
	(rs6000_xcoff_file_start): and here.
	* config/rs6000/rs6000.md (load_toc_aix_si): Set need_toc_init.
	(load_toc_aix_di): Likewise.

diff --git a/gcc/config/rs6000/rs6000.c b/gcc/config/rs6000/rs6000.c
index 4704d00..4ea4efb 100644
--- a/gcc/config/rs6000/rs6000.c
+++ b/gcc/config/rs6000/rs6000.c
@@ -209,7 +209,7 @@ tree rs6000_builtin_types[RS6000_BTI_MAX];
 tree rs6000_builtin_decls[RS6000_BUILTIN_COUNT];
 
 /* Flag to say the TOC is initialized */
-int toc_initialized;
+int toc_initialized, need_toc_init;
 char toc_label_name[10];
 
 /* Cached value of rs6000_variable_issue. This is cached in
@@ -5682,13 +5682,6 @@ rs6000_file_start (void)
 
   if (DEFAULT_ABI == ABI_ELFv2)
     fprintf (file, "\t.abiversion 2\n");
-
-  if (DEFAULT_ABI == ABI_AIX || DEFAULT_ABI == ABI_ELFv2
-      || (TARGET_ELF && flag_pic == 2))
-    {
-      switch_to_section (toc_section);
-      switch_to_section (text_section);
-    }
 }
 
 \f
@@ -20375,6 +20368,7 @@ rs6000_output_addr_const_extra (FILE *file, rtx x)
 	  {
 	    putc ('-', file);
 	    assemble_name (file, toc_label_name);
+	    need_toc_init = 1;
 	  }
 	else if (TARGET_ELF)
 	  fputs ("@toc", file);
@@ -24003,7 +23997,10 @@ rs6000_emit_load_toc_table (int fromprolog)
       ASM_GENERATE_INTERNAL_LABEL (buf, "L", CODE_LABEL_NUMBER (lab));
       lab = gen_rtx_SYMBOL_REF (Pmode, ggc_strdup (buf));
       if (flag_pic == 2)
-	got = gen_rtx_SYMBOL_REF (Pmode, ggc_strdup (toc_label_name));
+	{
+	  got = gen_rtx_SYMBOL_REF (Pmode, ggc_strdup (toc_label_name));
+	  need_toc_init = 1;
+	}
       else
 	got = rs6000_got_sym ();
       tmp1 = tmp2 = dest;
@@ -24048,6 +24045,7 @@ rs6000_emit_load_toc_table (int fromprolog)
 	  rtx tocsym, lab;
 
 	  tocsym = gen_rtx_SYMBOL_REF (Pmode, ggc_strdup (toc_label_name));
+	  need_toc_init = 1;
 	  lab = gen_label_rtx ();
 	  emit_insn (gen_load_toc_v4_PIC_1b (tocsym, lab));
 	  emit_move_insn (dest, gen_rtx_REG (Pmode, LR_REGNO));
@@ -24062,6 +24060,7 @@ rs6000_emit_load_toc_table (int fromprolog)
       /* This is for AIX code running in non-PIC ELF32.  */
       rtx realsym = gen_rtx_SYMBOL_REF (Pmode, ggc_strdup (toc_label_name));
 
+      need_toc_init = 1;
       emit_insn (gen_elf_high (dest, realsym));
       emit_insn (gen_elf_low (dest, dest, realsym));
     }
@@ -27598,6 +27597,17 @@ rs6000_output_function_epilogue (FILE *file,
 
       fputs ("\t.align 2\n", file);
     }
+
+  /* Arrange to define .LCTOC1 label, if not already done.  */
+  if (need_toc_init)
+    {
+      need_toc_init = 0;
+      if (!toc_initialized)
+	{
+	  switch_to_section (toc_section);
+	  switch_to_section (current_function_section ());
+	}
+    }
 }
 
 /* -fsplit-stack support.  */
@@ -31745,6 +31755,7 @@ rs6000_elf_declare_function_name (FILE *file, const char *name, tree decl)
 
       fprintf (file, "\t.long ");
       assemble_name (file, toc_label_name);
+      need_toc_init = 1;
       putc ('-', file);
       ASM_GENERATE_INTERNAL_LABEL (buf, "LCF", rs6000_pic_labelno);
       assemble_name (file, buf);
@@ -32137,6 +32148,7 @@ rs6000_xcoff_file_start (void)
   fputc ('\n', asm_out_file);
   if (write_symbols != NO_DEBUG)
     switch_to_section (private_data_section);
+  switch_to_section (toc_section);
   switch_to_section (text_section);
   if (profile_flag)
     fprintf (asm_out_file, "\t.extern %s\n", RS6000_MCOUNT);
diff --git a/gcc/config/rs6000/rs6000.md b/gcc/config/rs6000/rs6000.md
index 6e22c52..fe19853 100644
--- a/gcc/config/rs6000/rs6000.md
+++ b/gcc/config/rs6000/rs6000.md
@@ -9475,6 +9475,8 @@
   "*
 {
   char buf[30];
+  extern int need_toc_init;
+  need_toc_init = 1;
   ASM_GENERATE_INTERNAL_LABEL (buf, \"LCTOC\", 1);
   operands[1] = gen_rtx_SYMBOL_REF (Pmode, ggc_strdup (buf));
   operands[2] = gen_rtx_REG (Pmode, 2);
@@ -9492,6 +9494,8 @@
   "*
 {
   char buf[30];
+  extern int need_toc_init;
+  need_toc_init = 1;
 #ifdef TARGET_RELOCATABLE
   ASM_GENERATE_INTERNAL_LABEL (buf, \"LCTOC\",
 			       !TARGET_MINIMAL_TOC || TARGET_RELOCATABLE);

-- 
Alan Modra
Australia Development Lab, IBM

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

* Re: [RS6000] ABI_V4 init of toc section
  2016-01-29 16:38 [RS6000] ABI_V4 init of toc section Alan Modra
@ 2016-01-29 18:20 ` David Edelsohn
  2016-01-30  9:44   ` Alan Modra
  2016-02-01 18:02 ` David Edelsohn
  1 sibling, 1 reply; 5+ messages in thread
From: David Edelsohn @ 2016-01-29 18:20 UTC (permalink / raw)
  To: Alan Modra; +Cc: GCC Patches

On Fri, Jan 29, 2016 at 11:38 AM, Alan Modra <amodra@gmail.com> wrote:
> Since 4c4a180d, LTO has turned off flag_pic when linking a fixed
> position executable.  This results in flag_pic being zero in
> rs6000_file_start, and no definition of ".LCTOC1".
>
> However, when we get to actually emitting code, flag_pic may be on
> again, and references made to ".LCTOC1".  How flag_pic comes to be
> enabled again is quite a story.  It goes like this..  If a function is
> compiled with -fPIC then sysv4.h SUBTARGET_OVERRIDE_OPTIONS will set
> TARGET_RELOCATABLE.  Conversely, if TARGET_RELOCATABLE is set and
> flag_pic is zero, then SUBTARGET_OVERRIDE_OPTIONS will set flag_pic=2.
> It also happens that TARGET_RELOCATABLE is a bit in rs6000_isa_flags,
> which is handled by rs6000_function_specific_save and
> rs6000_function_specific_restore.  That last fact means lto streaming
> keeps track of the state of TARGET_RELOCATABLE for functions, and when
> options are restored for a given function we'll set flag_pic=2 if the
> function was originally compiled with -fPIC.  That's bad because it
> defeats the purpose of the 4c4a180d lto change, resulting in worse
> optimization of ppc32 executables.  What's more, we don't seem to turn
> off flag_pic once it is on.
>
> We should really untangle the flag_pic/TARGET_RELOCATABLE mess, but
> that change is probably a little dangerous for stage4.  Instead, this
> patch removes the toc symbol initialization from file_start and does
> so when the first item is emitted to the toc, or after the function
> epilogue in the cases where we emit code to initialize a toc pointer
> but don't actually use it (-O0 mostly, I think).
>
> Bootstrapped and regression tested powerpc64-linux biarch with all
> languages enabled.  OK to apply?
>
>         PR target/68662
>         * config/rs6000/rs6000.c (need_toc_init): New var, set it
>         whenever toc_label_name used.
>         (rs6000_file_start): Don't set up toc section here,
>         (rs6000_output_function_epilogue): do so here instead,
>         (rs6000_xcoff_file_start): and here.
>         * config/rs6000/rs6000.md (load_toc_aix_si): Set need_toc_init.
>         (load_toc_aix_di): Likewise.

I'm worried about how this is going to interact with AIX.  AIX
assembler is single pass and this patch moves the initialization from
the beginning of the file to the end of the file, which means there
will be references to a label whose definition is delayed until the
end.

- David

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

* Re: [RS6000] ABI_V4 init of toc section
  2016-01-29 18:20 ` David Edelsohn
@ 2016-01-30  9:44   ` Alan Modra
  2016-02-01  0:38     ` Alan Modra
  0 siblings, 1 reply; 5+ messages in thread
From: Alan Modra @ 2016-01-30  9:44 UTC (permalink / raw)
  To: David Edelsohn; +Cc: GCC Patches

On Fri, Jan 29, 2016 at 01:20:08PM -0500, David Edelsohn wrote:
> On Fri, Jan 29, 2016 at 11:38 AM, Alan Modra <amodra@gmail.com> wrote:
> >         PR target/68662
> >         * config/rs6000/rs6000.c (need_toc_init): New var, set it
> >         whenever toc_label_name used.
> >         (rs6000_file_start): Don't set up toc section here,
> >         (rs6000_output_function_epilogue): do so here instead,
> >         (rs6000_xcoff_file_start): and here.
> >         * config/rs6000/rs6000.md (load_toc_aix_si): Set need_toc_init.
> >         (load_toc_aix_di): Likewise.
> 
> I'm worried about how this is going to interact with AIX.  AIX
> assembler is single pass and this patch moves the initialization from
> the beginning of the file to the end of the file, which means there
> will be references to a label whose definition is delayed until the
> end.

AIX toc init is still done at the start of the file.  The code to emit
.toc or set .LCTOC..1 has moved from rs6000_file_start to
rs6000_xcoff_file_start.

-- 
Alan Modra
Australia Development Lab, IBM

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

* Re: [RS6000] ABI_V4 init of toc section
  2016-01-30  9:44   ` Alan Modra
@ 2016-02-01  0:38     ` Alan Modra
  0 siblings, 0 replies; 5+ messages in thread
From: Alan Modra @ 2016-02-01  0:38 UTC (permalink / raw)
  To: gcc-patches; +Cc: David Edelsohn

This is the more comprehensive fix for PR68662, the one I'm a little
nervous about applying during stage4.  OK for when stage1 opens?
Applying this patch makes
https://gcc.gnu.org/ml/gcc-patches/2016-01/msg02325.html redundant,
but I'm inclined to leave it in:  Not initializing the toc symbol at
file start results in smaller assembly and object files when the TOC
is not used.
=================

For ABI_V4, -mrelocatable and -fPIC both generate position independent
code, with some extra "fixup" output for -mrelocatable.  The
similarity of these two options has led to the situation where the
sysv4.h SUBTARGET_OVERRIDE_OPTIONS sets flag_pic on seeing
-mrelocatable, and sets TARGET_RELOCATABLE on seeing -fPIC.  That
prevents LTO from properly optimizing position dependent executables,
because the mutual dependence of the flags and the fact that LTO
streaming records the state of rs6000_isa_flags, result in flag_pic
being set when it shouldn't be.

So, don't set TARGET_RELOCATABLE when -fPIC.  Places that currently
test TARGET_RELOCATABLE can instead test
TARGET_RELOCATABLE || (DEFAULT_ABI == ABI_V4 && flag_pic > 1)
or since TARGET_RELOCATABLE can only be enabled when ABI_V4,
DEFAULT_ABI == ABI_V4 && (TARGET_RELOCATABLE || flag_pic > 1).

Also, since flag_pic is set by -mrelocatable, a number of places that
currently test TARGET_RELOCATABLE can be simplified.  I also made
-mrelocatable set TARGET_NO_FP_IN_TOC, allowing TARGET_RELOCATABLE to
be removed from ASM_OUTPUT_SPECIAL_POOL_ENTRY_P.  Reducing occurrences
of TARGET_RELOCATABLE is a good thing.

	PR target/68662
	* config/rs6000/sysv4.h (SUBTARGET_OVERRIDE_OPTIONS): Don't
	set OPTION_MASK_RELOCATABLE when flag_pic == 2.  Set
	TARGET_NO_FP_IN_TOC for -mrelocatable.
	(MINIMAL_TOC_SECTION_ASM_OP): Remove redundant
	TARGET_RELOCATABLE test.
	(ASM_OUTPUT_SPECIAL_POOL_ENTRY_P): Likewise.
	(ASM_PREFERRED_EH_DATA_FORMAT): Likewise.
	* config/rs6000/linux64.h (MINIMAL_TOC_SECTION_ASM_OP): Likewise.
	(ASM_OUTPUT_SPECIAL_POOL_ENTRY_P): Likewise.
	(ASM_PREFERRED_EH_DATA_FORMAT): Likewise.
	* config/rs6000/freebsd64.h (MINIMAL_TOC_SECTION_ASM_OP): Likewise.
	(ASM_OUTPUT_SPECIAL_POOL_ENTRY_P): Likewise.
	(ASM_PREFERRED_EH_DATA_FORMAT): Likewise.
	* config/rs6000/predicates.md (easy_fp_constant): Likewise.
	* config/rs6000/rs6000.c (rs6000_elf_output_toc_section_asm_op):
	Likewise.
	(rs6000_assemble_integer): Update TARGET_RELOCATABLE test.
	(rs6000_stack_info): Likewise.
	(rs6000_elf_asm_out_constructor): Likewise.
	(rs6000_elf_asm_out_destructor): Likewise.
	(rs6000_elf_declare_function_name): Likewise.
	* config/rs6000/rs6000.md (load_toc_aix_di): Likewise.
	* config/rs6000/rs6000.h (MASK_RELOCATABLE, MASK_MINIMAL_TOC):
	Don't define.

diff --git a/gcc/config/rs6000/freebsd64.h b/gcc/config/rs6000/freebsd64.h
index 899b858..3038c43 100644
--- a/gcc/config/rs6000/freebsd64.h
+++ b/gcc/config/rs6000/freebsd64.h
@@ -349,7 +349,7 @@ extern int dot_symbols;
    true if the symbol may be affected by dynamic relocations.  */
 #undef	ASM_PREFERRED_EH_DATA_FORMAT
 #define	ASM_PREFERRED_EH_DATA_FORMAT(CODE, GLOBAL) \
-  ((TARGET_64BIT || flag_pic || TARGET_RELOCATABLE)			\
+  (TARGET_64BIT || flag_pic						\
    ? (((GLOBAL) ? DW_EH_PE_indirect : 0) | DW_EH_PE_pcrel		\
       | (TARGET_64BIT ? DW_EH_PE_udata8 : DW_EH_PE_sdata4))		\
    : DW_EH_PE_absptr)
@@ -384,7 +384,7 @@ extern int dot_symbols;
 #define MINIMAL_TOC_SECTION_ASM_OP \
   (TARGET_64BIT                                         \
    ? "\t.section\t\".toc1\",\"aw\""                     \
-   : ((TARGET_RELOCATABLE || flag_pic)                  \
+   : (flag_pic						\
       ? "\t.section\t\".got2\",\"aw\""                  \
       : "\t.section\t\".got1\",\"aw\""))
 
@@ -422,7 +422,6 @@ extern int dot_symbols;
                         && ! TARGET_NO_FP_IN_TOC)))                     \
                || (!TARGET_64BIT                                        \
                    && !TARGET_NO_FP_IN_TOC                              \
-                   && !TARGET_RELOCATABLE                               \
                    && SCALAR_FLOAT_MODE_P (GET_MODE (X))                \
                    && BITS_PER_WORD == HOST_BITS_PER_INT)))))
 
diff --git a/gcc/config/rs6000/linux64.h b/gcc/config/rs6000/linux64.h
index fefa0c4..e86b5d5 100644
--- a/gcc/config/rs6000/linux64.h
+++ b/gcc/config/rs6000/linux64.h
@@ -489,7 +489,7 @@ extern int dot_symbols;
 #define MINIMAL_TOC_SECTION_ASM_OP \
   (TARGET_64BIT						\
    ? "\t.section\t\".toc1\",\"aw\""			\
-   : ((TARGET_RELOCATABLE || flag_pic)			\
+   : (flag_pic						\
       ? "\t.section\t\".got2\",\"aw\""			\
       : "\t.section\t\".got1\",\"aw\""))
 
@@ -585,7 +585,6 @@ extern int dot_symbols;
 			&& ! TARGET_NO_FP_IN_TOC)))			\
 	       || (!TARGET_64BIT					\
 		   && !TARGET_NO_FP_IN_TOC				\
-		   && !TARGET_RELOCATABLE				\
 		   && SCALAR_FLOAT_MODE_P (GET_MODE (X))		\
 		   && BITS_PER_WORD == HOST_BITS_PER_INT)))))
 
@@ -594,7 +593,7 @@ extern int dot_symbols;
    true if the symbol may be affected by dynamic relocations.  */
 #undef	ASM_PREFERRED_EH_DATA_FORMAT
 #define	ASM_PREFERRED_EH_DATA_FORMAT(CODE, GLOBAL) \
-  ((TARGET_64BIT || flag_pic || TARGET_RELOCATABLE)			\
+  (TARGET_64BIT || flag_pic						\
    ? (((GLOBAL) ? DW_EH_PE_indirect : 0) | DW_EH_PE_pcrel		\
       | (TARGET_64BIT ? DW_EH_PE_udata8 : DW_EH_PE_sdata4))		\
    : DW_EH_PE_absptr)
diff --git a/gcc/config/rs6000/predicates.md b/gcc/config/rs6000/predicates.md
index 302303c..ef104d5 100644
--- a/gcc/config/rs6000/predicates.md
+++ b/gcc/config/rs6000/predicates.md
@@ -538,13 +538,6 @@
   if (flag_pic && DEFAULT_ABI == ABI_V4)
     return 0;
 
-#ifdef TARGET_RELOCATABLE
-  /* Similarly if we are using -mrelocatable, consider all constants
-     to be hard.  */
-  if (TARGET_RELOCATABLE)
-    return 0;
-#endif
-
   /* If we have real FPRs, consider floating point constants hard (other than
      0.0 under VSX), so that the constant gets pushed to memory during the
      early RTL phases.  This has the advantage that double precision constants
diff --git a/gcc/config/rs6000/rs6000.c b/gcc/config/rs6000/rs6000.c
index 4ea4efb..1befd58 100644
--- a/gcc/config/rs6000/rs6000.c
+++ b/gcc/config/rs6000/rs6000.c
@@ -20404,7 +20404,8 @@ rs6000_assemble_integer (rtx x, unsigned int size, int aligned_p)
 	 don't need to mark it here.  We used to skip the text section, but it
 	 should never be valid for relocated addresses to be placed in the text
 	 section.  */
-      if (TARGET_RELOCATABLE
+      if (DEFAULT_ABI == ABI_V4
+	  && (TARGET_RELOCATABLE || flag_pic > 1)
 	  && in_section != toc_section
 	  && !recurse
 	  && !CONST_SCALAR_INT_P (x)
@@ -23599,7 +23600,9 @@ rs6000_stack_info (void)
 	  && !TARGET_PROFILE_KERNEL)
       || (DEFAULT_ABI == ABI_V4 && cfun->calls_alloca)
 #ifdef TARGET_RELOCATABLE
-      || (TARGET_RELOCATABLE && (get_pool_size () != 0))
+      || (DEFAULT_ABI == ABI_V4
+	  && (TARGET_RELOCATABLE || flag_pic > 1)
+	  && (get_pool_size () != 0))
 #endif
       || rs6000_ra_ever_killed ())
     info_ptr->lr_save_p = 1;
@@ -31067,30 +31070,30 @@ rs6000_ms_bitfield_layout_p (const_tree record_type)
 static void
 rs6000_elf_output_toc_section_asm_op (const void *data ATTRIBUTE_UNUSED)
 {
-  if ((DEFAULT_ABI == ABI_AIX || DEFAULT_ABI == ABI_ELFv2)
-      && TARGET_MINIMAL_TOC
-      && !TARGET_RELOCATABLE)
+  if (DEFAULT_ABI == ABI_AIX || DEFAULT_ABI == ABI_ELFv2)
     {
-      if (!toc_initialized)
+      if (TARGET_MINIMAL_TOC)
 	{
-	  toc_initialized = 1;
-	  fprintf (asm_out_file, "%s\n", TOC_SECTION_ASM_OP);
-	  (*targetm.asm_out.internal_label) (asm_out_file, "LCTOC", 0);
-	  fprintf (asm_out_file, "\t.tc ");
-	  ASM_OUTPUT_INTERNAL_LABEL_PREFIX (asm_out_file, "LCTOC1[TC],");
-	  ASM_OUTPUT_INTERNAL_LABEL_PREFIX (asm_out_file, "LCTOC1");
-	  fprintf (asm_out_file, "\n");
-
-	  fprintf (asm_out_file, "%s\n", MINIMAL_TOC_SECTION_ASM_OP);
-	  ASM_OUTPUT_INTERNAL_LABEL_PREFIX (asm_out_file, "LCTOC1");
-	  fprintf (asm_out_file, " = .+32768\n");
+	  if (!toc_initialized)
+	    {
+	      toc_initialized = 1;
+	      fprintf (asm_out_file, "%s\n", TOC_SECTION_ASM_OP);
+	      (*targetm.asm_out.internal_label) (asm_out_file, "LCTOC", 0);
+	      fprintf (asm_out_file, "\t.tc ");
+	      ASM_OUTPUT_INTERNAL_LABEL_PREFIX (asm_out_file, "LCTOC1[TC],");
+	      ASM_OUTPUT_INTERNAL_LABEL_PREFIX (asm_out_file, "LCTOC1");
+	      fprintf (asm_out_file, "\n");
+
+	      fprintf (asm_out_file, "%s\n", MINIMAL_TOC_SECTION_ASM_OP);
+	      ASM_OUTPUT_INTERNAL_LABEL_PREFIX (asm_out_file, "LCTOC1");
+	      fprintf (asm_out_file, " = .+32768\n");
+	    }
+	  else
+	    fprintf (asm_out_file, "%s\n", MINIMAL_TOC_SECTION_ASM_OP);
 	}
       else
-	fprintf (asm_out_file, "%s\n", MINIMAL_TOC_SECTION_ASM_OP);
+	fprintf (asm_out_file, "%s\n", TOC_SECTION_ASM_OP);
     }
-  else if ((DEFAULT_ABI == ABI_AIX || DEFAULT_ABI == ABI_ELFv2)
-	   && !TARGET_RELOCATABLE)
-    fprintf (asm_out_file, "%s\n", TOC_SECTION_ASM_OP);
   else
     {
       fprintf (asm_out_file, "%s\n", MINIMAL_TOC_SECTION_ASM_OP);
@@ -31672,7 +31675,8 @@ rs6000_elf_asm_out_constructor (rtx symbol, int priority)
   switch_to_section (get_section (section, SECTION_WRITE, NULL));
   assemble_align (POINTER_SIZE);
 
-  if (TARGET_RELOCATABLE)
+  if (DEFAULT_ABI == ABI_V4
+      && (TARGET_RELOCATABLE || flag_pic > 1))
     {
       fputs ("\t.long (", asm_out_file);
       output_addr_const (asm_out_file, symbol);
@@ -31702,7 +31706,8 @@ rs6000_elf_asm_out_destructor (rtx symbol, int priority)
   switch_to_section (get_section (section, SECTION_WRITE, NULL));
   assemble_align (POINTER_SIZE);
 
-  if (TARGET_RELOCATABLE)
+  if (DEFAULT_ABI == ABI_V4
+      && (TARGET_RELOCATABLE || flag_pic > 1))
     {
       fputs ("\t.long (", asm_out_file);
       output_addr_const (asm_out_file, symbol);
@@ -31744,7 +31749,8 @@ rs6000_elf_declare_function_name (FILE *file, const char *name, tree decl)
       return;
     }
 
-  if (TARGET_RELOCATABLE
+  if (DEFAULT_ABI == ABI_V4
+      && (TARGET_RELOCATABLE || flag_pic > 1)
       && !TARGET_SECURE_PLT
       && (get_pool_size () != 0 || crtl->profile)
       && uses_TOC ())
diff --git a/gcc/config/rs6000/rs6000.h b/gcc/config/rs6000/rs6000.h
index 8c6bd07..94606aa 100644
--- a/gcc/config/rs6000/rs6000.h
+++ b/gcc/config/rs6000/rs6000.h
@@ -635,18 +635,10 @@ extern int rs6000_vector_align[];
 #define MASK_64BIT			OPTION_MASK_64BIT
 #endif
 
-#ifdef TARGET_RELOCATABLE
-#define MASK_RELOCATABLE		OPTION_MASK_RELOCATABLE
-#endif
-
 #ifdef TARGET_LITTLE_ENDIAN
 #define MASK_LITTLE_ENDIAN		OPTION_MASK_LITTLE_ENDIAN
 #endif
 
-#ifdef TARGET_MINIMAL_TOC
-#define MASK_MINIMAL_TOC		OPTION_MASK_MINIMAL_TOC
-#endif
-
 #ifdef TARGET_REGNAMES
 #define MASK_REGNAMES			OPTION_MASK_REGNAMES
 #endif
diff --git a/gcc/config/rs6000/rs6000.md b/gcc/config/rs6000/rs6000.md
index fe19853..563d92c 100644
--- a/gcc/config/rs6000/rs6000.md
+++ b/gcc/config/rs6000/rs6000.md
@@ -9496,12 +9496,8 @@
   char buf[30];
   extern int need_toc_init;
   need_toc_init = 1;
-#ifdef TARGET_RELOCATABLE
   ASM_GENERATE_INTERNAL_LABEL (buf, \"LCTOC\",
-			       !TARGET_MINIMAL_TOC || TARGET_RELOCATABLE);
-#else
-  ASM_GENERATE_INTERNAL_LABEL (buf, \"LCTOC\", 1);
-#endif
+			       !TARGET_ELF || !TARGET_MINIMAL_TOC);
   if (TARGET_ELF)
     strcat (buf, \"@toc\");
   operands[1] = gen_rtx_SYMBOL_REF (Pmode, ggc_strdup (buf));
diff --git a/gcc/config/rs6000/sysv4.h b/gcc/config/rs6000/sysv4.h
index cbf9097..a4009c3 100644
--- a/gcc/config/rs6000/sysv4.h
+++ b/gcc/config/rs6000/sysv4.h
@@ -192,16 +192,25 @@ do {									\
       error ("-msecure-plt not supported by your assembler");		\
     }									\
 									\
-  /* Treat -fPIC the same as -mrelocatable.  */				\
   if (flag_pic > 1 && DEFAULT_ABI == ABI_V4)				\
     {									\
-      rs6000_isa_flags |= OPTION_MASK_RELOCATABLE | OPTION_MASK_MINIMAL_TOC; \
+      /* Note: flag_pic should not change any option flags that would	\
+	 be invalid with or pessimise -fno-PIC code.  LTO turns off	\
+	 flag_pic when linking/recompiling a fixed position executable. \
+	 However, if the objects were originally compiled with -fPIC,	\
+	 then other target options forced on here by -fPIC are restored \
+	 when recompiling those objects without -fPIC.  In particular	\
+	 TARGET_RELOCATABLE must not be enabled here by flag_pic.  */	\
+      rs6000_isa_flags |= OPTION_MASK_MINIMAL_TOC;			\
       TARGET_NO_FP_IN_TOC = 1;						\
     }									\
 									\
-  else if (TARGET_RELOCATABLE)						\
-    if (!flag_pic)							\
-      flag_pic = 2;							\
+  if (TARGET_RELOCATABLE)						\
+    {									\
+      if (!flag_pic)							\
+	flag_pic = 2;							\
+      TARGET_NO_FP_IN_TOC = 1;						\
+    }									\
 } while (0)
 
 #ifndef RS6000_BI_ARCH
@@ -317,8 +326,7 @@ do {									\
 
 /* Put PC relative got entries in .got2.  */
 #define	MINIMAL_TOC_SECTION_ASM_OP \
-  (TARGET_RELOCATABLE || (flag_pic && DEFAULT_ABI == ABI_V4)		\
-   ? "\t.section\t\".got2\",\"aw\"" : "\t.section\t\".got1\",\"aw\"")
+  (flag_pic ? "\t.section\t\".got2\",\"aw\"" : "\t.section\t\".got1\",\"aw\"")
 
 #define	SDATA_SECTION_ASM_OP "\t.section\t\".sdata\",\"aw\""
 #define	SDATA2_SECTION_ASM_OP "\t.section\t\".sdata2\",\"a\""
@@ -352,7 +360,6 @@ do {									\
        || (GET_CODE (X) == CONST_INT 					\
 	   && GET_MODE_BITSIZE (MODE) <= GET_MODE_BITSIZE (Pmode))	\
        || (!TARGET_NO_FP_IN_TOC						\
-	   && !TARGET_RELOCATABLE					\
 	   && GET_CODE (X) == CONST_DOUBLE				\
 	   && SCALAR_FLOAT_MODE_P (GET_MODE (X))			\
 	   && BITS_PER_WORD == HOST_BITS_PER_INT)))
@@ -941,9 +948,10 @@ ncrtn.o%s"
 /* Select a format to encode pointers in exception handling data.  CODE
    is 0 for data, 1 for code labels, 2 for function pointers.  GLOBAL is
    true if the symbol may be affected by dynamic relocations.  */
-#define ASM_PREFERRED_EH_DATA_FORMAT(CODE,GLOBAL)			     \
-  ((flag_pic || TARGET_RELOCATABLE)					     \
-   ? (((GLOBAL) ? DW_EH_PE_indirect : 0) | DW_EH_PE_pcrel | DW_EH_PE_sdata4) \
+#define ASM_PREFERRED_EH_DATA_FORMAT(CODE, GLOBAL)			\
+  (flag_pic								\
+   ? (((GLOBAL) ? DW_EH_PE_indirect : 0) | DW_EH_PE_pcrel		\
+      | DW_EH_PE_sdata4)						\
    : DW_EH_PE_absptr)
 
 #define DOUBLE_INT_ASM_OP "\t.quad\t"

-- 
Alan Modra
Australia Development Lab, IBM

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

* Re: [RS6000] ABI_V4 init of toc section
  2016-01-29 16:38 [RS6000] ABI_V4 init of toc section Alan Modra
  2016-01-29 18:20 ` David Edelsohn
@ 2016-02-01 18:02 ` David Edelsohn
  1 sibling, 0 replies; 5+ messages in thread
From: David Edelsohn @ 2016-02-01 18:02 UTC (permalink / raw)
  To: Alan Modra; +Cc: GCC Patches

On Fri, Jan 29, 2016 at 11:38 AM, Alan Modra <amodra@gmail.com> wrote:
> Since 4c4a180d, LTO has turned off flag_pic when linking a fixed
> position executable.  This results in flag_pic being zero in
> rs6000_file_start, and no definition of ".LCTOC1".
>
> However, when we get to actually emitting code, flag_pic may be on
> again, and references made to ".LCTOC1".  How flag_pic comes to be
> enabled again is quite a story.  It goes like this..  If a function is
> compiled with -fPIC then sysv4.h SUBTARGET_OVERRIDE_OPTIONS will set
> TARGET_RELOCATABLE.  Conversely, if TARGET_RELOCATABLE is set and
> flag_pic is zero, then SUBTARGET_OVERRIDE_OPTIONS will set flag_pic=2.
> It also happens that TARGET_RELOCATABLE is a bit in rs6000_isa_flags,
> which is handled by rs6000_function_specific_save and
> rs6000_function_specific_restore.  That last fact means lto streaming
> keeps track of the state of TARGET_RELOCATABLE for functions, and when
> options are restored for a given function we'll set flag_pic=2 if the
> function was originally compiled with -fPIC.  That's bad because it
> defeats the purpose of the 4c4a180d lto change, resulting in worse
> optimization of ppc32 executables.  What's more, we don't seem to turn
> off flag_pic once it is on.
>
> We should really untangle the flag_pic/TARGET_RELOCATABLE mess, but
> that change is probably a little dangerous for stage4.  Instead, this
> patch removes the toc symbol initialization from file_start and does
> so when the first item is emitted to the toc, or after the function
> epilogue in the cases where we emit code to initialize a toc pointer
> but don't actually use it (-O0 mostly, I think).
>
> Bootstrapped and regression tested powerpc64-linux biarch with all
> languages enabled.  OK to apply?
>
>         PR target/68662
>         * config/rs6000/rs6000.c (need_toc_init): New var, set it
>         whenever toc_label_name used.
>         (rs6000_file_start): Don't set up toc section here,
>         (rs6000_output_function_epilogue): do so here instead,
>         (rs6000_xcoff_file_start): and here.
>         * config/rs6000/rs6000.md (load_toc_aix_si): Set need_toc_init.
>         (load_toc_aix_di): Likewise.

This is okay as an interim fix for GCC 6.

Thanks, David

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

end of thread, other threads:[~2016-02-01 18:02 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2016-01-29 16:38 [RS6000] ABI_V4 init of toc section Alan Modra
2016-01-29 18:20 ` David Edelsohn
2016-01-30  9:44   ` Alan Modra
2016-02-01  0:38     ` Alan Modra
2016-02-01 18:02 ` David Edelsohn

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).