diff mbox series

[avr,applied] Fix PR116407

Message ID fae6e70b-1b75-4596-88ed-84bc8149e099@gjlay.de
State New
Headers show
Series [avr,applied] Fix PR116407 | expand

Commit Message

Georg-Johann Lay Aug. 18, 2024, 1:28 p.m. UTC
This fixes "relocation truncated to fit" errors from
the linker due to bogus (too small) jump offsets.

Johann

--
diff mbox series

Patch

    AVR: target/116407 - Fix linker error "relocation truncated to fit".
    
    Some text peepholes output extra instructions prior to a branch
    instruction and that increase the jump offset of backward branches.
    
            PR target/116407
    gcc/
            * config/avr/avr-protos.h (avr_jump_mode): Add an int argument.
            * config/avr/avr.cc (avr_jump_mode): Add an int argument to increase
            the computed jump offset of backwards branches.
            * config/avr/avr.md (*dec-and-branchhi!=-1, *dec-and-branchsi!=-1):
            Increase the jump offset used by avr_jump_mode() as needed.
    gcc/testsuite/
            * gcc.target/avr/torture/pr116407-2.c: New test.
            * gcc.target/avr/torture/pr116407-4.c: New test.

diff --git a/gcc/config/avr/avr-protos.h b/gcc/config/avr/avr-protos.h
index 7b666f17718..34298b976a7 100644
--- a/gcc/config/avr/avr-protos.h
+++ b/gcc/config/avr/avr-protos.h
@@ -115,7 +115,7 @@  extern const char* avr_out_reload_inpsi (rtx*, rtx, int*);
 extern const char* avr_out_lpm (rtx_insn *, rtx*, int*);
 extern void avr_notice_update_cc (rtx body, rtx_insn *insn);
 extern int reg_unused_after (rtx_insn *insn, rtx reg);
-extern int avr_jump_mode (rtx x, rtx_insn *insn);
+extern int avr_jump_mode (rtx x, rtx_insn *insn, int = 0);
 extern int test_hard_reg_class (enum reg_class rclass, rtx x);
 extern int jump_over_one_insn_p (rtx_insn *insn, rtx dest);
 
diff --git a/gcc/config/avr/avr.cc b/gcc/config/avr/avr.cc
index 8c19bcb34a6..c520b98a178 100644
--- a/gcc/config/avr/avr.cc
+++ b/gcc/config/avr/avr.cc
@@ -4133,19 +4133,22 @@  avr_use_by_pieces_infrastructure_p (unsigned HOST_WIDE_INT size,
 /* Choose mode for jump insn:
    1 - relative jump in range -63 <= x <= 62 ;
    2 - relative jump in range -2046 <= x <= 2045 ;
-   3 - absolute jump (only for ATmega[16]03).  */
+   3 - absolute jump (only when we have JMP / CALL).
+
+   When jumping backwards, assume the jump offset is EXTRA words
+   bigger than inferred from insn addresses.  */
 
 int
-avr_jump_mode (rtx x, rtx_insn *insn)
+avr_jump_mode (rtx x, rtx_insn *insn, int extra)
 {
   int dest_addr = INSN_ADDRESSES (INSN_UID (GET_CODE (x) == LABEL_REF
 					    ? XEXP (x, 0) : x));
   int cur_addr = INSN_ADDRESSES (INSN_UID (insn));
   int jump_distance = cur_addr - dest_addr;
 
-  if (IN_RANGE (jump_distance, -63, 62))
+  if (IN_RANGE (jump_distance, -63, 62 - extra))
     return 1;
-  else if (IN_RANGE (jump_distance, -2046, 2045))
+  else if (IN_RANGE (jump_distance, -2046, 2045 - extra))
     return 2;
   else if (AVR_HAVE_JMP_CALL)
     return 3;
diff --git a/gcc/config/avr/avr.md b/gcc/config/avr/avr.md
index 28841e40db1..8c4819a901f 100644
--- a/gcc/config/avr/avr.md
+++ b/gcc/config/avr/avr.md
@@ -7605,7 +7605,7 @@  (define_peephole ; "*dec-and-branchsi!=-1.d.clobber"
                        "sbc %C0,__zero_reg__" CR_TAB
                        "sbc %D0,__zero_reg__", operands);
 
-    int jump_mode = avr_jump_mode (operands[2], insn);
+    int jump_mode = avr_jump_mode (operands[2], insn, 3 - avr_adiw_reg_p (operands[0]));
     const char *op = ((EQ == <CODE>) ^ (jump_mode == 1)) ? "brcc" : "brcs";
     operands[1] = gen_rtx_CONST_STRING (VOIDmode, op);
 
@@ -7642,7 +7642,7 @@  (define_peephole ; "*dec-and-branchhi!=-1"
       output_asm_insn ("subi %A0,1" CR_TAB
                        "sbc %B0,__zero_reg__", operands);
 
-    int jump_mode = avr_jump_mode (operands[2], insn);
+    int jump_mode = avr_jump_mode (operands[2], insn, 1 - avr_adiw_reg_p (operands[0]));
     const char *op = ((EQ == <CODE>) ^ (jump_mode == 1)) ? "brcc" : "brcs";
     operands[1] = gen_rtx_CONST_STRING (VOIDmode, op);
 
@@ -7681,7 +7681,7 @@  (define_peephole ; "*dec-and-branchhi!=-1.d.clobber"
       output_asm_insn ("subi %A0,1" CR_TAB
                        "sbc %B0,__zero_reg__", operands);
 
-    int jump_mode = avr_jump_mode (operands[2], insn);
+    int jump_mode = avr_jump_mode (operands[2], insn, 1 - avr_adiw_reg_p (operands[0]));
     const char *op = ((EQ == <CODE>) ^ (jump_mode == 1)) ? "brcc" : "brcs";
     operands[1] = gen_rtx_CONST_STRING (VOIDmode, op);
 
@@ -7718,7 +7718,7 @@  (define_peephole ; "*dec-and-branchhi!=-1.l.clobber"
                      "sub %A0,%3" CR_TAB
                      "sbc %B0,__zero_reg__", operands);
 
-    int jump_mode = avr_jump_mode (operands[2], insn);
+    int jump_mode = avr_jump_mode (operands[2], insn, 1 - avr_adiw_reg_p (operands[0]));
     const char *op = ((EQ == <CODE>) ^ (jump_mode == 1)) ? "brcc" : "brcs";
     operands[1] = gen_rtx_CONST_STRING (VOIDmode, op);
 
diff --git a/gcc/testsuite/gcc.target/avr/torture/pr116407-2.c b/gcc/testsuite/gcc.target/avr/torture/pr116407-2.c
new file mode 100644
index 00000000000..f580d129b5b
--- /dev/null
+++ b/gcc/testsuite/gcc.target/avr/torture/pr116407-2.c
@@ -0,0 +1,34 @@ 
+/* { dg-do link } */
+
+typedef __UINT16_TYPE__ u16;
+typedef __INT16_TYPE__  T;
+
+#ifdef __OPTIMIZE__
+
+static __inline__ __attribute__((always_inline))
+void delay (T x, u16 nops)
+{
+    do
+        __builtin_avr_nops (nops);
+    while (--x != -1);
+}
+
+#ifdef __AVR_HAVE_JMP_CALL__
+
+void delay_2043 (T x) { delay (x, 2043); }
+void delay_2044 (T x) { delay (x, 2044); }
+void delay_2045 (T x) { delay (x, 2045); }
+void delay_2046 (T x) { delay (x, 2046); }
+
+#endif /* have JUMP, CALL */
+
+void delay_61 (T x) { delay (x, 61); }
+void delay_62 (T x) { delay (x, 62); }
+void delay_63 (T x) { delay (x, 63); }
+
+#endif /* optimize */
+
+int main (void)
+{
+    return 0;
+}
diff --git a/gcc/testsuite/gcc.target/avr/torture/pr116407-4.c b/gcc/testsuite/gcc.target/avr/torture/pr116407-4.c
new file mode 100644
index 00000000000..4347d85e552
--- /dev/null
+++ b/gcc/testsuite/gcc.target/avr/torture/pr116407-4.c
@@ -0,0 +1,34 @@ 
+/* { dg-do link } */
+
+typedef __UINT16_TYPE__ u16;
+typedef __INT32_TYPE__  T;
+
+#ifdef __OPTIMIZE__
+
+static __inline__ __attribute__((always_inline))
+void delay (T x, u16 nops)
+{
+    do
+        __builtin_avr_nops (nops);
+    while (--x != -1);
+}
+
+#ifdef __AVR_HAVE_JMP_CALL__
+
+void delay_2043 (T x) { delay (x, 2043); }
+void delay_2044 (T x) { delay (x, 2044); }
+void delay_2045 (T x) { delay (x, 2045); }
+void delay_2046 (T x) { delay (x, 2046); }
+
+#endif /* have JUMP, CALL */
+
+void delay_61 (T x) { delay (x, 61); }
+void delay_62 (T x) { delay (x, 62); }
+void delay_63 (T x) { delay (x, 63); }
+
+#endif /* optimize */
+
+int main (void)
+{
+    return 0;
+}