I've checked in the attached patch to fix an oversight in the handling of GP-relative addressing modes in the nios2 back end. It allows GCC to generate addresses of the form "%gprel(sym+offset)(gp)" instead of just "%gprel(sym)(gp)". Formerly GCC required a scratch register and extra instruction to compute the address in this case, but in fact both the assembler and underlying relocation support a constant offset with %gprel.

-Sandra

2015-07-05  Chung-Lin Tang  <clt...@codesourcery.com>
	    Sandra Loosemore <san...@codesourcery.com>

	gcc/
	* config/nios2/nios2-protos.h (nios2_symbol_ref_in_small_data_p):
	Delete extern declaration.
	(gprel_constant_p): Add extern declaration.
	* config/nios2/constraints.md ("S"): Use gprel_constant_p
	instead of nios2_symbol_ref_in_small_data_p.
	* config/nios2/nios2.c (nios2_legitimate_address_p): Likewise.
	(nios2_symbol_ref_in_small_data_p): Make static.
	(gprel_constant_p): Make non-static.

	gcc/testsuite/
	* gcc.target/nios2/gprel-offset.c: New test.
Index: gcc/testsuite/gcc.target/nios2/gprel-offset.c
===================================================================
--- gcc/testsuite/gcc.target/nios2/gprel-offset.c	(revision 0)
+++ gcc/testsuite/gcc.target/nios2/gprel-offset.c	(revision 0)
@@ -0,0 +1,20 @@
+/* { dg-do compile } */
+/* { dg-options "-O2" } */
+
+static struct s {
+  int x;
+  char y;
+} s;
+
+void set (char c)
+{
+  s.y = c;
+}
+
+
+char get (void)
+{
+  return s.y;
+}
+
+/* { dg-final { scan-assembler-times "%gprel\\(s\\+4\\)\\(gp\\)" 2 } } */
Index: gcc/config/nios2/nios2-protos.h
===================================================================
--- gcc/config/nios2/nios2-protos.h	(revision 225420)
+++ gcc/config/nios2/nios2-protos.h	(working copy)
@@ -44,7 +44,7 @@ extern bool nios2_fpu_insn_enabled (enum
 extern const char * nios2_fpu_insn_asm (enum n2fpu_code);
 
 extern bool nios2_legitimate_pic_operand_p (rtx);
-extern bool nios2_symbol_ref_in_small_data_p (rtx);
+extern bool gprel_constant_p (rtx);
 extern bool nios2_regno_ok_for_base_p (int, bool);
 extern bool nios2_unspec_reloc_p (rtx);
 
Index: gcc/config/nios2/constraints.md
===================================================================
--- gcc/config/nios2/constraints.md	(revision 225419)
+++ gcc/config/nios2/constraints.md	(working copy)
@@ -84,8 +84,7 @@
 
 (define_constraint "S"
   "An immediate stored in small data, accessible by GP."
-  (and (match_code "symbol_ref")
-       (match_test "nios2_symbol_ref_in_small_data_p (op)")))
+  (match_test "gprel_constant_p (op)"))
 
 (define_constraint "T"
   "A constant unspec offset representing a relocation."
Index: gcc/config/nios2/nios2.c
===================================================================
--- gcc/config/nios2/nios2.c	(revision 225420)
+++ gcc/config/nios2/nios2.c	(working copy)
@@ -1614,14 +1614,15 @@ nios2_legitimate_address_p (machine_mode
     case SYMBOL_REF:
       if (SYMBOL_REF_TLS_MODEL (operand))
 	return false;
-      
-      if (nios2_symbol_ref_in_small_data_p (operand))
+
+      /* Else, fall through.  */
+    case CONST:
+      if (gprel_constant_p (operand))
 	return true;
 
       /* Else, fall through.  */
     case LABEL_REF:
     case CONST_INT:
-    case CONST:
     case CONST_DOUBLE:
       return false;
 
@@ -1688,7 +1689,7 @@ nios2_in_small_data_p (const_tree exp)
 
 /* Return true if symbol is in small data section.  */
 
-bool
+static bool
 nios2_symbol_ref_in_small_data_p (rtx sym)
 {
   tree decl;
@@ -2110,7 +2111,7 @@ nios2_print_operand (FILE *file, rtx op,
 }
 
 /* Return true if this is a GP-relative accessible reference.  */
-static bool
+bool
 gprel_constant_p (rtx op)
 {
   if (GET_CODE (op) == SYMBOL_REF

Reply via email to