The attached patch is to fix PR target/66563 which is a 4.9/5/6
regression.  These newer compilers can CSE some expressions on
the sequences for getting GOT.  The target should make sure it
won't happen.  See PR target/66563 for details.
Tested on sh4-unknown-linux-gnu and committed on trunk.
I'll backport it to 5 later and to 4.9 when it reopens.

Regards,
        kaz
--
2015-06-24  Kaz Kojima  <kkoj...@gcc.gnu.org>

        PR target/66563
        * config/sh/sh.md (GOTaddr2picreg): Add a new operand for
        an additional element of the unspec vector.  Modify indices
        of operands.
        (builtin_setjmp_receiver): Pass const0_rtx to gen_GOTaddr2picreg.
        * config/sh/sh.c (prepare_move_operands): Pass incremented
        const_int to gen_GOTaddr2picreg.
        (sh_expand_prologue): Pass const0_rtx to gen_GOTaddr2picreg.

diff --git a/config/sh/sh.c b/config/sh/sh.c
index 6f03206..2c247b1 100644
--- a/config/sh/sh.c
+++ b/config/sh/sh.c
@@ -1845,12 +1845,13 @@ prepare_move_operands (rtx operands[], machine_mode 
mode)
                  || tls_kind == TLS_MODEL_LOCAL_DYNAMIC
                  || tls_kind == TLS_MODEL_INITIAL_EXEC))
            {
+             static int got_labelno;
              /* Don't schedule insns for getting GOT address when
                 the first scheduling is enabled, to avoid spill
                 failures for R0.  */
              if (flag_schedule_insns)
                emit_insn (gen_blockage ());
-             emit_insn (gen_GOTaddr2picreg ());
+             emit_insn (gen_GOTaddr2picreg (GEN_INT (++got_labelno)));
              emit_use (gen_rtx_REG (SImode, PIC_REG));
              if (flag_schedule_insns)
                emit_insn (gen_blockage ());
@@ -7958,7 +7959,7 @@ sh_expand_prologue (void)
     }
 
   if (flag_pic && df_regs_ever_live_p (PIC_OFFSET_TABLE_REGNUM))
-    emit_insn (gen_GOTaddr2picreg ());
+    emit_insn (gen_GOTaddr2picreg (const0_rtx));
 
   if (SHMEDIA_REGS_STACK_ADJUST ())
     {
diff --git a/config/sh/sh.md b/config/sh/sh.md
index e88d249..43cd949 100644
--- a/config/sh/sh.md
+++ b/config/sh/sh.md
@@ -10592,12 +10592,18 @@ label:
   [(set_attr "in_delay_slot" "no")
    (set_attr "type" "arith")])
 
+;; Loads of the GOTPC relocation values must not be optimized away
+;; by e.g. any kind of CSE and must stay as they are.  Although there
+;; are other various ways to ensure this, we use an artificial counter
+;; operand to generate unique symbols.
 (define_expand "GOTaddr2picreg"
   [(set (reg:SI R0_REG)
-       (unspec:SI [(const:SI (unspec:SI [(match_dup 1)] UNSPEC_PIC))]
-                  UNSPEC_MOVA))
-   (set (match_dup 0) (const:SI (unspec:SI [(match_dup 1)] UNSPEC_PIC)))
-   (set (match_dup 0) (plus:SI (match_dup 0) (reg:SI R0_REG)))]
+       (unspec:SI [(const:SI (unspec:SI [(match_dup 2)
+                                         (match_operand:SI 0 "" "")]
+                                        UNSPEC_PIC))] UNSPEC_MOVA))
+   (set (match_dup 1)
+       (const:SI (unspec:SI [(match_dup 2) (match_dup 0)] UNSPEC_PIC)))
+   (set (match_dup 1) (plus:SI (match_dup 1) (reg:SI R0_REG)))]
   ""
 {
   if (TARGET_VXWORKS_RTP)
@@ -10608,8 +10614,8 @@ label:
       DONE;
     }
 
-  operands[0] = gen_rtx_REG (Pmode, PIC_REG);
-  operands[1] = gen_rtx_SYMBOL_REF (VOIDmode, GOT_SYMBOL_NAME);
+  operands[1] = gen_rtx_REG (Pmode, PIC_REG);
+  operands[2] = gen_rtx_SYMBOL_REF (VOIDmode, GOT_SYMBOL_NAME);
 
   if (TARGET_SHMEDIA)
     {
@@ -10618,23 +10624,23 @@ label:
       rtx lab = PATTERN (gen_call_site ());
       rtx insn, equiv;
 
-      equiv = operands[1];
-      operands[1] = gen_rtx_UNSPEC (Pmode, gen_rtvec (2, operands[1], lab),
+      equiv = operands[2];
+      operands[2] = gen_rtx_UNSPEC (Pmode, gen_rtvec (2, operands[2], lab),
                                    UNSPEC_PCREL_SYMOFF);
-      operands[1] = gen_rtx_CONST (Pmode, operands[1]);
+      operands[2] = gen_rtx_CONST (Pmode, operands[2]);
 
       if (Pmode == SImode)
        {
-         emit_insn (gen_movsi_const (pic, operands[1]));
+         emit_insn (gen_movsi_const (pic, operands[2]));
          emit_insn (gen_ptrel_si (tr, pic, copy_rtx (lab)));
        }
       else
        {
-         emit_insn (gen_movdi_const (pic, operands[1]));
+         emit_insn (gen_movdi_const (pic, operands[2]));
          emit_insn (gen_ptrel_di (tr, pic, copy_rtx (lab)));
        }
 
-      insn = emit_move_insn (operands[0], tr);
+      insn = emit_move_insn (operands[1], tr);
 
       set_unique_reg_note (insn, REG_EQUAL, equiv);
 
@@ -10688,7 +10694,7 @@ label:
   [(match_operand 0 "" "")]
   "flag_pic"
 {
-  emit_insn (gen_GOTaddr2picreg ());
+  emit_insn (gen_GOTaddr2picreg (const0_rtx));
   DONE;
 })
 

Reply via email to