+/* Synthetic attributes used by insn-automata.c and the scheduler.
+ These are primarily concerned with (define_insn_reservation)
+ patterns. */
+
+struct insn_reserv
+{
+ struct insn_reserv *next;
+
+ const char *name;
+ int default_latency;
+ rtx condexp;
+
+ /* Sequence number of this insn. */
+ int insn_num;
+
+ /* Whether a (define_bypass) construct names this insn in its
+ output list. */
+ bool bypassed;
+};
+
+static struct insn_reserv *all_insn_reservs = 0;
+static struct insn_reserv **last_insn_reserv_p = &all_insn_reservs;
+static size_t n_insn_reservs;
+
+/* Store information from a DEFINE_INSN_RESERVATION for future
+ attribute generation. */
+static void
+gen_insn_reserv (rtx def)
+{
+ struct insn_reserv *decl = oballoc (struct insn_reserv);
+
+ decl->name = DEF_ATTR_STRING (XSTR (def, 0));
+ decl->default_latency = XINT (def, 1);
+ decl->condexp = check_attr_test (XEXP (def, 2), 0, 0);
+ decl->insn_num = n_insn_reservs;
+ decl->bypassed = false;
+ decl->next = 0;
+
+ *last_insn_reserv_p = decl;
+ last_insn_reserv_p = &decl->next;
+ n_insn_reservs++;
+}
+
+/* Store information from a DEFINE_BYPASS for future attribute
+ generation. The only thing we care about is the list of output
+ insns, which will later be used to tag reservation structures with
+ a 'bypassed' bit. */
+
+struct bypass_list
+{
+ struct bypass_list *next;
+ const char *insn;
+};
+
+static struct bypass_list *all_bypasses;
+static size_t n_bypasses;
+
+static void
+gen_bypass_1 (const char *s, size_t len)
+{
+ struct bypass_list *b;
+
+ if (len == 0)
+ return;
+
+ s = attr_string (s, len);
+ for (b = all_bypasses; b; b = b->next)
+ if (s == b->insn)
+ return; /* already got that one */
+
+ b = oballoc (struct bypass_list);
+ b->insn = s;
+ b->next = all_bypasses;
+ all_bypasses = b;
+ n_bypasses++;
+}
+
+static void
+gen_bypass (rtx def)
+{
+ const char *p, *base;
+
+ for (p = base = XSTR (def, 1); *p; p++)
+ if (*p == ',')
+ {
+ gen_bypass_1 (base, p - base);
+ do
+ p++;
+ while (ISSPACE (*p));
+ base = p;
+ }
+ gen_bypass_1 (base, p - base);
+}
+
+/* Find and mark all of the bypassed insns. */
+static void
+process_bypasses (void)
+{
+ struct bypass_list *b;
+ struct insn_reserv *r;
+
+ /* The reservation list is likely to be much longer than the bypass
+ list. */
+ for (r = all_insn_reservs; r; r = r->next)
+ for (b = all_bypasses; b; b = b->next)
+ if (r->name == b->insn)
+ r->bypassed = true;
+}
+
+/* Check that attribute NAME is used in define_insn_reservation condition
+ EXP. Return true if it is. */
+static bool
+check_tune_attr (const char *name, rtx exp)
+{
+ switch (GET_CODE (exp))
+ {
+ case AND:
+ if (check_tune_attr (name, XEXP (exp, 0)))
+ return true;
+ return check_tune_attr (name, XEXP (exp, 1));
+
+ case IOR:
+ return (check_tune_attr (name, XEXP (exp, 0))
+ && check_tune_attr (name, XEXP (exp, 1)));
+
+ case EQ_ATTR:
+ return XSTR (exp, 0) == name;
+
+ default:
+ return false;
+ }
+}
+
+/* Try to find a const attribute (usually cpu or tune) that is used
+ in all define_insn_reservation conditions. */
+static struct attr_desc *
+find_tune_attr (rtx exp)
+{
+ struct attr_desc *attr;
+
+ switch (GET_CODE (exp))
+ {
+ case AND:
+ case IOR:
+ attr = find_tune_attr (XEXP (exp, 0));
+ if (attr)
+ return attr;
+ return find_tune_attr (XEXP (exp, 1));
+
+ case EQ_ATTR:
+ if (XSTR (exp, 0) == alternative_name)
+ return NULL;
+
+ attr = find_attr (&XSTR (exp, 0), 0);
+ gcc_assert (attr);
+
+ if (attr->is_const && !attr->is_special)
+ {
+ struct insn_reserv *decl;
+
+ for (decl = all_insn_reservs; decl; decl = decl->next)
+ if (! check_tune_attr (attr->name, decl->condexp))
+ return NULL;
+ return attr;
+ }
+ return NULL;
+
+ default:
+ return NULL;
+ }
+}
+
+/* Create all of the attributes that describe automaton properties. */
+static void
+make_automaton_attrs (void)
+{
+ int i;
+ struct insn_reserv *decl;
+ rtx code_exp, lats_exp, byps_exp;
+ struct attr_desc *tune_attr;
+
+ if (n_insn_reservs == 0)
+ return;
+
+ tune_attr = find_tune_attr (all_insn_reservs->condexp);
+ if (tune_attr != NULL)
+ {
+ rtx *condexps = XNEWVEC (rtx, n_insn_reservs * 3);
+ struct attr_value *val;
+ bool first = true;
+
+ gcc_assert (tune_attr->is_const
+ && !tune_attr->is_special
+ && !tune_attr->is_numeric);
+ for (val = tune_attr->first_value; val; val = val->next)
+ {
+ if (val == tune_attr->default_val)
+ continue;
+ gcc_assert (GET_CODE (val->value) == CONST_STRING);
+ printf ("static int internal_dfa_insn_code_%s (rtx);\n"
+ "static int insn_default_latency_%s (rtx);\n",
+ XSTR (val->value, 0), XSTR (val->value, 0));
+ }
+
+ printf ("\n");
+ printf ("int (*internal_dfa_insn_code) (rtx);\n");
+ printf ("int (*insn_default_latency) (rtx);\n");
+ printf ("\n");
+ printf ("void\n");
+ printf ("init_sched_attrs (void)\n");
+ printf ("{\n");
+
+ for (val = tune_attr->first_value; val; val = val->next)
+ {
+ int j;
+ char *name;
+ rtx test = attr_rtx (EQ_ATTR, tune_attr->name, XSTR (val->value, 0));
+
+ if (val == tune_attr->default_val)
+ continue;
+ for (decl = all_insn_reservs, i = 0;
+ decl;
+ decl = decl->next)
+ {
+ rtx ctest = test;
+ rtx condexp
+ = simplify_and_tree (decl->condexp, &ctest, -2, 0);
+ if (condexp == false_rtx)
+ continue;
+ if (condexp == true_rtx)
+ break;
+ condexps[i] = condexp;
+ condexps[i + 1] = make_numeric_value (decl->insn_num);
+ condexps[i + 2] = make_numeric_value (decl->default_latency);
+ i += 3;
+ }
+
+ code_exp = rtx_alloc (COND);
+ lats_exp = rtx_alloc (COND);
+
+ j = i / 3 * 2;
+ XVEC (code_exp, 0) = rtvec_alloc (j);
+ XVEC (lats_exp, 0) = rtvec_alloc (j);
+
+ if (decl)
+ {
+ XEXP (code_exp, 1) = make_numeric_value (decl->insn_num);
+ XEXP (lats_exp, 1) = make_numeric_value (decl->default_latency);
+ }
+ else
+ {
+ XEXP (code_exp, 1) = make_numeric_value (n_insn_reservs + 1);
+ XEXP (lats_exp, 1) = make_numeric_value (0);
+ }
+
+ while (i > 0)
+ {
+ i -= 3;
+ j -= 2;
+ XVECEXP (code_exp, 0, j) = condexps[i];
+ XVECEXP (lats_exp, 0, j) = condexps[i];
+
+ XVECEXP (code_exp, 0, j + 1) = condexps[i + 1];
+ XVECEXP (lats_exp, 0, j + 1) = condexps[i + 2];
+ }
+
+ name = XNEWVEC (char,
+ sizeof ("*internal_dfa_insn_code_")
+ + strlen (XSTR (val->value, 0)));
+ strcpy (name, "*internal_dfa_insn_code_");
+ strcat (name, XSTR (val->value, 0));
+ make_internal_attr (name, code_exp, ATTR_NONE);
+ strcpy (name, "*insn_default_latency_");
+ strcat (name, XSTR (val->value, 0));
+ make_internal_attr (name, lats_exp, ATTR_NONE);
+ XDELETEVEC (name);
+
+ if (first)
+ {
+ printf (" if (");
+ first = false;
+ }
+ else
+ printf (" else if (");
+ write_test_expr (test, 0, 0);
+ printf (")\n");
+ printf (" {\n");
+ printf (" internal_dfa_insn_code\n");
+ printf (" = internal_dfa_insn_code_%s;\n",
+ XSTR (val->value, 0));
+ printf (" insn_default_latency\n");
+ printf (" = insn_default_latency_%s;\n",
+ XSTR (val->value, 0));
+ printf (" }\n");
+ }
+
+ printf (" else\n");
+ printf (" gcc_unreachable ();\n");
+ printf ("}\n");
+ printf ("\n");
+
+ XDELETEVEC (condexps);
+ }
+ else
+ {
+ code_exp = rtx_alloc (COND);
+ lats_exp = rtx_alloc (COND);
+
+ XVEC (code_exp, 0) = rtvec_alloc (n_insn_reservs * 2);
+ XVEC (lats_exp, 0) = rtvec_alloc (n_insn_reservs * 2);
+
+ XEXP (code_exp, 1) = make_numeric_value (n_insn_reservs + 1);
+ XEXP (lats_exp, 1) = make_numeric_value (0);
+
+ for (decl = all_insn_reservs, i = 0;
+ decl;
+ decl = decl->next, i += 2)
+ {
+ XVECEXP (code_exp, 0, i) = decl->condexp;
+ XVECEXP (lats_exp, 0, i) = decl->condexp;
+
+ XVECEXP (code_exp, 0, i+1) = make_numeric_value (decl->insn_num);
+ XVECEXP (lats_exp, 0, i+1)
+ = make_numeric_value (decl->default_latency);
+ }
+ make_internal_attr ("*internal_dfa_insn_code", code_exp, ATTR_NONE);
+ make_internal_attr ("*insn_default_latency", lats_exp, ATTR_NONE);
+ }
+
+ if (n_bypasses == 0)
+ byps_exp = make_numeric_value (0);
+ else
+ {
+ process_bypasses ();
+
+ byps_exp = rtx_alloc (COND);
+ XVEC (byps_exp, 0) = rtvec_alloc (n_bypasses * 2);
+ XEXP (byps_exp, 1) = make_numeric_value (0);
+ for (decl = all_insn_reservs, i = 0;
+ decl;
+ decl = decl->next)
+ if (decl->bypassed)
+ {
+ XVECEXP (byps_exp, 0, i) = decl->condexp;
+ XVECEXP (byps_exp, 0, i+1) = make_numeric_value (1);
+ i += 2;
+ }
+ }
+
+ make_internal_attr ("*bypass_p", byps_exp, ATTR_NONE);
+}