From: Matthew Fortune <[email protected]>

This should be solved using the various PIC related macros such as
PIC_OFFSET_TABLE_REGNUM and pic_offset_table_rtx but changing these is
too dangerous without investigation.  The lightweight fix for
shrink-wrapping being inhibited by -mgpopt only affects shrink wrapping.

gcc/
        * config/mips/mips.cc (mips_set_up_by_prologue): Clear
        the global pointer from being related to the prologue.
        (TARGET_SET_UP_BY_PROLOGUE): New macro.

gcc/testsuite/
        * gcc.target/mips/gpopt-shrink-wrap-1.c: New test.

Cherry-picked 4ea3a82b5e8e23591d79a9ca63018acceb53c2a5
from https://github.com/MIPS/gcc

Signed-off-by: Matthew Fortune <[email protected]>
Signed-off-by: Faraz Shahbazker <[email protected]>
Signed-off-by: Aleksandar Rakic <[email protected]>
Signed-off-by: Eldar Osmanovic <[email protected]>
---
 gcc/config/mips/mips.cc                       | 12 +++++++++++
 .../gcc.target/mips/gpopt-shrink-wrap-1.c     | 21 +++++++++++++++++++
 2 files changed, 33 insertions(+)
 create mode 100644 gcc/testsuite/gcc.target/mips/gpopt-shrink-wrap-1.c

diff --git a/gcc/config/mips/mips.cc b/gcc/config/mips/mips.cc
index fdbb0a764bd..a3a0dd15fe0 100644
--- a/gcc/config/mips/mips.cc
+++ b/gcc/config/mips/mips.cc
@@ -23302,6 +23302,15 @@ mips_c_mode_for_floating_type (enum tree_index ti)
   return default_mode_for_floating_type (ti);
 }
 
+/* Implement TARGET_SET_UP_BY_PROLOGUE.  */
+
+static void
+mips_set_up_by_prologue (hard_reg_set_container *regs)
+{
+  if (!TARGET_USE_GOT && TARGET_GPOPT)
+    CLEAR_HARD_REG_BIT (regs->set, GLOBAL_POINTER_REGNUM);
+}
+
 void
 mips_bit_clear_info (enum machine_mode mode, unsigned HOST_WIDE_INT m,
                      int *start_pos, int *size)
@@ -23455,6 +23464,9 @@ mips_print_patchable_function_entry (FILE *file 
ATTRIBUTE_UNUSED,
 #undef TARGET_IN_SMALL_DATA_P
 #define TARGET_IN_SMALL_DATA_P mips_in_small_data_p
 
+#undef TARGET_SET_UP_BY_PROLOGUE
+#define TARGET_SET_UP_BY_PROLOGUE mips_set_up_by_prologue
+
 #undef TARGET_MACHINE_DEPENDENT_REORG
 #define TARGET_MACHINE_DEPENDENT_REORG mips_reorg
 
diff --git a/gcc/testsuite/gcc.target/mips/gpopt-shrink-wrap-1.c 
b/gcc/testsuite/gcc.target/mips/gpopt-shrink-wrap-1.c
new file mode 100644
index 00000000000..01eb91544ce
--- /dev/null
+++ b/gcc/testsuite/gcc.target/mips/gpopt-shrink-wrap-1.c
@@ -0,0 +1,21 @@
+/* { dg-do compile } */
+/* { dg-skip-if "requires O2" { *-*-* } { "*" } { "-O2" } } */
+/* { dg-skip-if "requires non-LTO" { *-*-* } { "-flto" } { "" } } */
+/* { dg-options "-mabi=32 -march=mips32r2 -mno-abicalls -fno-pic -mgpopt -G8 
-fno-optimize-sibling-calls -fdump-rtl-pro_and_epilogue" } */
+
+extern int cold (int);
+int small;
+
+int
+f (int x)
+{
+  int y = small;
+  if (x == 0)
+    return y + 1;
+
+    int z = cold (x + y);
+    return z + y;
+}
+
+/* { dg-final { scan-assembler 
"\tlw\t\\\$\[0-9\]+,%gp_rel\\(small\\)\\(\\\$28\\)" } } */
+/* { dg-final { scan-rtl-dump "Performing shrink-wrapping" "pro_and_epilogue" 
} } */
-- 
2.43.0

Reply via email to