2cca73
commit 6001db79c477b03eacc7e7049560921fb54b7845
2cca73
Author: Richard Sandiford <richard.sandiford@arm.com>
2cca73
Date:   Mon Sep 7 20:15:36 2020 +0100
2cca73
2cca73
    lra: Avoid cycling on certain subreg reloads [PR96796]
2cca73
    
2cca73
    This PR is about LRA cycling for a reload of the form:
2cca73
    
2cca73
    ----------------------------------------------------------------------------
2cca73
    Changing pseudo 196 in operand 1 of insn 103 on equiv [r105:DI*0x8+r140:DI]
2cca73
          Creating newreg=287, assigning class ALL_REGS to slow/invalid mem r287
2cca73
          Creating newreg=288, assigning class ALL_REGS to slow/invalid mem r288
2cca73
      103: r203:SI=r288:SI<<0x1+r196:DI#0
2cca73
          REG_DEAD r196:DI
2cca73
        Inserting slow/invalid mem reload before:
2cca73
      316: r287:DI=[r105:DI*0x8+r140:DI]
2cca73
      317: r288:SI=r287:DI#0
2cca73
    ----------------------------------------------------------------------------
2cca73
    
2cca73
    The problem is with r287.  We rightly give it a broad starting class of
2cca73
    POINTER_AND_FP_REGS (reduced from ALL_REGS by preferred_reload_class).
2cca73
    However, we never make forward progress towards narrowing it down to
2cca73
    a specific choice of class (POINTER_REGS or FP_REGS).
2cca73
    
2cca73
    I think in practice we rely on two things to narrow a reload pseudo's
2cca73
    class down to a specific choice:
2cca73
    
2cca73
    (1) a restricted class is specified when the pseudo is created
2cca73
    
2cca73
        This happens for input address reloads, where the class is taken
2cca73
        from the target's chosen base register class.  It also happens
2cca73
        for simple REG reloads, where the class is taken from the chosen
2cca73
        alternative's constraints.
2cca73
    
2cca73
    (2) uses of the reload pseudo as a direct input operand
2cca73
    
2cca73
        In this case get_reload_reg tries to reuse the existing register
2cca73
        and narrow its class, instead of creating a new reload pseudo.
2cca73
    
2cca73
    However, neither occurs here.  As described above, r287 rightly
2cca73
    starts out with a wide choice of class, ultimately derived from
2cca73
    ALL_REGS, so we don't get (1).  And as the comments in the PR
2cca73
    explain, r287 is never used as an input reload, only the subreg is,
2cca73
    so we don't get (2):
2cca73
    
2cca73
    ----------------------------------------------------------------------------
2cca73
             Choosing alt 13 in insn 317:  (0) r  (1) w {*movsi_aarch64}
2cca73
          Creating newreg=291, assigning class FP_REGS to r291
2cca73
      317: r288:SI=r291:SI
2cca73
        Inserting insn reload before:
2cca73
      320: r291:SI=r287:DI#0
2cca73
    ----------------------------------------------------------------------------
2cca73
    
2cca73
    IMO, in this case we should rely on the reload of r316 to narrow
2cca73
    down the class of r278.  Currently we do:
2cca73
    
2cca73
    ----------------------------------------------------------------------------
2cca73
             Choosing alt 7 in insn 316:  (0) r  (1) m {*movdi_aarch64}
2cca73
          Creating newreg=289 from oldreg=287, assigning class GENERAL_REGS to r289
2cca73
      316: r289:DI=[r105:DI*0x8+r140:DI]
2cca73
        Inserting insn reload after:
2cca73
      318: r287:DI=r289:DI
2cca73
    ---------------------------------------------------
2cca73
    
2cca73
    i.e. we create a new pseudo register r289 and give *that* pseudo
2cca73
    GENERAL_REGS instead.  This is because get_reload_reg only narrows
2cca73
    down the existing class for OP_IN and OP_INOUT, not OP_OUT.
2cca73
    
2cca73
    But if we have a reload pseudo in a reload instruction and have chosen
2cca73
    a specific class for the reload pseudo, I think we should simply install
2cca73
    it for OP_OUT reloads too, if the class is a subset of the existing class.
2cca73
    We will need to pick such a register whatever happens (for r289 in the
2cca73
    example above).  And as explained in the PR, doing this actually avoids
2cca73
    an unnecessary move via the FP registers too.
2cca73
    
2cca73
    The patch is quite aggressive in that it does this for all reload
2cca73
    pseudos in all reload instructions.  I wondered about reusing the
2cca73
    condition for a reload move in in_class_p:
2cca73
    
2cca73
              INSN_UID (curr_insn) >= new_insn_uid_start
2cca73
              && curr_insn_set != NULL
2cca73
              && ((OBJECT_P (SET_SRC (curr_insn_set))
2cca73
                   && ! CONSTANT_P (SET_SRC (curr_insn_set)))
2cca73
                  || (GET_CODE (SET_SRC (curr_insn_set)) == SUBREG
2cca73
                      && OBJECT_P (SUBREG_REG (SET_SRC (curr_insn_set)))
2cca73
                      && ! CONSTANT_P (SUBREG_REG (SET_SRC (curr_insn_set)))))))
2cca73
    
2cca73
    but I can't really justify that on first principles.  I think we
2cca73
    should apply the rule consistently until we have a specific reason
2cca73
    for doing otherwise.
2cca73
    
2cca73
    gcc/
2cca73
            PR rtl-optimization/96796
2cca73
            * lra-constraints.c (in_class_p): Add a default-false
2cca73
            allow_all_reload_class_changes_p parameter.  Do not treat
2cca73
            reload moves specially when the parameter is true.
2cca73
            (get_reload_reg): Try to narrow the class of an existing OP_OUT
2cca73
            reload if we're reloading a reload pseudo in a reload instruction.
2cca73
    
2cca73
    gcc/testsuite/
2cca73
            PR rtl-optimization/96796
2cca73
            * gcc.c-torture/compile/pr96796.c: New test.
2cca73
2cca73
diff --git a/gcc/lra-constraints.c b/gcc/lra-constraints.c
2cca73
index 580da9c3ed6..161b721efb1 100644
2cca73
--- a/gcc/lra-constraints.c
2cca73
+++ b/gcc/lra-constraints.c
2cca73
@@ -236,12 +236,17 @@ get_reg_class (int regno)
2cca73
    CL.  Use elimination first if REG is a hard register.  If REG is a
2cca73
    reload pseudo created by this constraints pass, assume that it will
2cca73
    be allocated a hard register from its allocno class, but allow that
2cca73
-   class to be narrowed to CL if it is currently a superset of CL.
2cca73
+   class to be narrowed to CL if it is currently a superset of CL and
2cca73
+   if either:
2cca73
+
2cca73
+   - ALLOW_ALL_RELOAD_CLASS_CHANGES_P is true or
2cca73
+   - the instruction we're processing is not a reload move.
2cca73
 
2cca73
    If NEW_CLASS is nonnull, set *NEW_CLASS to the new allocno class of
2cca73
    REGNO (reg), or NO_REGS if no change in its class was needed.  */
2cca73
 static bool
2cca73
-in_class_p (rtx reg, enum reg_class cl, enum reg_class *new_class)
2cca73
+in_class_p (rtx reg, enum reg_class cl, enum reg_class *new_class,
2cca73
+	    bool allow_all_reload_class_changes_p = false)
2cca73
 {
2cca73
   enum reg_class rclass, common_class;
2cca73
   machine_mode reg_mode;
2cca73
@@ -266,7 +271,8 @@ in_class_p (rtx reg, enum reg_class cl, enum reg_class *new_class)
2cca73
 	 typically moves that have many alternatives, and restricting
2cca73
 	 reload pseudos for one alternative may lead to situations
2cca73
 	 where other reload pseudos are no longer allocatable.  */
2cca73
-      || (INSN_UID (curr_insn) >= new_insn_uid_start
2cca73
+      || (!allow_all_reload_class_changes_p
2cca73
+	  && INSN_UID (curr_insn) >= new_insn_uid_start
2cca73
 	  && curr_insn_set != NULL
2cca73
 	  && ((OBJECT_P (SET_SRC (curr_insn_set))
2cca73
 	       && ! CONSTANT_P (SET_SRC (curr_insn_set)))
2cca73
@@ -551,13 +557,12 @@ init_curr_insn_input_reloads (void)
2cca73
   curr_insn_input_reloads_num = 0;
2cca73
 }
2cca73
 
2cca73
-/* Create a new pseudo using MODE, RCLASS, ORIGINAL or reuse already
2cca73
-   created input reload pseudo (only if TYPE is not OP_OUT).  Don't
2cca73
-   reuse pseudo if IN_SUBREG_P is true and the reused pseudo should be
2cca73
-   wrapped up in SUBREG.  The result pseudo is returned through
2cca73
-   RESULT_REG.  Return TRUE if we created a new pseudo, FALSE if we
2cca73
-   reused the already created input reload pseudo.  Use TITLE to
2cca73
-   describe new registers for debug purposes.  */
2cca73
+/* Create a new pseudo using MODE, RCLASS, ORIGINAL or reuse an existing
2cca73
+   reload pseudo.  Don't reuse an existing reload pseudo if IN_SUBREG_P
2cca73
+   is true and the reused pseudo should be wrapped up in a SUBREG.
2cca73
+   The result pseudo is returned through RESULT_REG.  Return TRUE if we
2cca73
+   created a new pseudo, FALSE if we reused an existing reload pseudo.
2cca73
+   Use TITLE to describe new registers for debug purposes.  */
2cca73
 static bool
2cca73
 get_reload_reg (enum op_type type, machine_mode mode, rtx original,
2cca73
 		enum reg_class rclass, bool in_subreg_p,
2cca73
@@ -616,6 +621,35 @@ get_reload_reg (enum op_type type, machine_mode mode, rtx original,
2cca73
 
2cca73
   if (type == OP_OUT)
2cca73
     {
2cca73
+      /* Output reload registers tend to start out with a conservative
2cca73
+	 choice of register class.  Usually this is ALL_REGS, although
2cca73
+	 a target might narrow it (for performance reasons) through
2cca73
+	 targetm.preferred_reload_class.  It's therefore quite common
2cca73
+	 for a reload instruction to require a more restrictive class
2cca73
+	 than the class that was originally assigned to the reload register.
2cca73
+
2cca73
+	 In these situations, it's more efficient to refine the choice
2cca73
+	 of register class rather than create a second reload register.
2cca73
+	 This also helps to avoid cycling for registers that are only
2cca73
+	 used by reload instructions.  */
2cca73
+      if (REG_P (original)
2cca73
+	  && (int) REGNO (original) >= new_regno_start
2cca73
+	  && INSN_UID (curr_insn) >= new_insn_uid_start
2cca73
+	  && in_class_p (original, rclass, &new_class, true))
2cca73
+	{
2cca73
+	  unsigned int regno = REGNO (original);
2cca73
+	  if (lra_dump_file != NULL)
2cca73
+	    {
2cca73
+	      fprintf (lra_dump_file, "	 Reuse r%d for output ", regno);
2cca73
+	      dump_value_slim (lra_dump_file, original, 1);
2cca73
+	    }
2cca73
+	  if (new_class != lra_get_allocno_class (regno))
2cca73
+	    lra_change_class (regno, new_class, ", change to", false);
2cca73
+	  if (lra_dump_file != NULL)
2cca73
+	    fprintf (lra_dump_file, "\n");
2cca73
+	  *result_reg = original;
2cca73
+	  return false;
2cca73
+	}
2cca73
       *result_reg
2cca73
 	= lra_create_new_reg_with_unique_value (mode, original, rclass, title);
2cca73
       return true;
2cca73
diff --git a/gcc/testsuite/gcc.c-torture/compile/pr96796.c b/gcc/testsuite/gcc.c-torture/compile/pr96796.c
2cca73
new file mode 100644
2cca73
index 00000000000..8808e62fe77
2cca73
--- /dev/null
2cca73
+++ b/gcc/testsuite/gcc.c-torture/compile/pr96796.c
2cca73
@@ -0,0 +1,55 @@
2cca73
+/* { dg-additional-options "-fcommon" } */
2cca73
+
2cca73
+struct S0 {
2cca73
+  signed f0 : 8;
2cca73
+  unsigned f1;
2cca73
+  unsigned f4;
2cca73
+};
2cca73
+struct S1 {
2cca73
+  long f3;
2cca73
+  char f4;
2cca73
+} g_3_4;
2cca73
+
2cca73
+int g_5, func_1_l_32, func_50___trans_tmp_31;
2cca73
+static struct S0 g_144, g_834, g_1255, g_1261;
2cca73
+
2cca73
+int g_273[120] = {};
2cca73
+int *g_555;
2cca73
+char **g_979;
2cca73
+static int g_1092_0;
2cca73
+static int g_1193;
2cca73
+int safe_mul_func_int16_t_s_s(int si1, int si2) { return si1 * si2; }
2cca73
+static struct S0 *func_50();
2cca73
+int func_1() { func_50(g_3_4, g_5, func_1_l_32, 8, 3); }
2cca73
+void safe_div_func_int64_t_s_s(int *);
2cca73
+void safe_mod_func_uint32_t_u_u(struct S0);
2cca73
+struct S0 *func_50(int p_51, struct S0 p_52, struct S1 p_53, int p_54,
2cca73
+                   int p_55) {
2cca73
+  int __trans_tmp_30;
2cca73
+  char __trans_tmp_22;
2cca73
+  short __trans_tmp_19;
2cca73
+  long l_985_1;
2cca73
+  long l_1191[8];
2cca73
+  safe_div_func_int64_t_s_s(g_273);
2cca73
+  __builtin_printf((char*)g_1261.f4);
2cca73
+  safe_mod_func_uint32_t_u_u(g_834);
2cca73
+  g_144.f0 += 1;
2cca73
+  for (;;) {
2cca73
+    struct S1 l_1350 = {&l_1350};
2cca73
+    for (; p_53.f3; p_53.f3 -= 1)
2cca73
+      for (; g_1193 <= 2; g_1193 += 1) {
2cca73
+        __trans_tmp_19 = safe_mul_func_int16_t_s_s(l_1191[l_985_1 + p_53.f3],
2cca73
+                                                   p_55 % (**g_979 = 10));
2cca73
+        __trans_tmp_22 = g_1255.f1 * p_53.f4;
2cca73
+        __trans_tmp_30 = __trans_tmp_19 + __trans_tmp_22;
2cca73
+        if (__trans_tmp_30)
2cca73
+          g_1261.f0 = p_51;
2cca73
+        else {
2cca73
+          g_1255.f0 = p_53.f3;
2cca73
+          int *l_1422 = g_834.f0 = g_144.f4 != (*l_1422)++ > 0 < 0 ^ 51;
2cca73
+          g_555 = ~0;
2cca73
+          g_1092_0 |= func_50___trans_tmp_31;
2cca73
+        }
2cca73
+      }
2cca73
+  }
2cca73
+}