+/* Determine how many alternatives there are in INSN, and how many
+ operands. */
+
+static void
+collect_insn_data (rtx pattern, int *palt, int *pmax)
+{
+ const char *fmt;
+ enum rtx_code code;
+ int i, j, len;
+
+ code = GET_CODE (pattern);
+ switch (code)
+ {
+ case MATCH_OPERAND:
+ i = n_alternatives (XSTR (pattern, 2));
+ *palt = (i > *palt ? i : *palt);
+ /* Fall through. */
+
+ case MATCH_OPERATOR:
+ case MATCH_SCRATCH:
+ case MATCH_PARALLEL:
+ i = XINT (pattern, 0);
+ if (i > *pmax)
+ *pmax = i;
+ break;
+
+ default:
+ break;
+ }
+
+ fmt = GET_RTX_FORMAT (code);
+ len = GET_RTX_LENGTH (code);
+ for (i = 0; i < len; i++)
+ {
+ switch (fmt[i])
+ {
+ case 'e': case 'u':
+ collect_insn_data (XEXP (pattern, i), palt, pmax);
+ break;
+
+ case 'V':
+ if (XVEC (pattern, i) == NULL)
+ break;
+ /* Fall through. */
+ case 'E':
+ for (j = XVECLEN (pattern, i) - 1; j >= 0; --j)
+ collect_insn_data (XVECEXP (pattern, i, j), palt, pmax);
+ break;
+
+ case 'i': case 'w': case '0': case 's': case 'S': case 'T':
+ break;
+
+ default:
+ gcc_unreachable ();
+ }
+ }
+}
+
+static rtx
+alter_predicate_for_insn (rtx pattern, int alt, int max_op, int lineno)
+{
+ const char *fmt;
+ enum rtx_code code;
+ int i, j, len;
+
+ code = GET_CODE (pattern);
+ switch (code)
+ {
+ case MATCH_OPERAND:
+ {
+ const char *c = XSTR (pattern, 2);
+
+ if (n_alternatives (c) != 1)
+ {
+ message_with_line (lineno,
+ "too many alternatives for operand %d",
+ XINT (pattern, 0));
+ errors = 1;
+ return NULL;
+ }
+
+ /* Replicate C as needed to fill out ALT alternatives. */
+ if (c && *c && alt > 1)
+ {
+ size_t c_len = strlen (c);
+ size_t len = alt * (c_len + 1);
+ char *new_c = XNEWVEC(char, len);
+
+ memcpy (new_c, c, c_len);
+ for (i = 1; i < alt; ++i)
+ {
+ new_c[i * (c_len + 1) - 1] = ',';
+ memcpy (&new_c[i * (c_len + 1)], c, c_len);
+ }
+ new_c[len - 1] = '\0';
+ XSTR (pattern, 2) = new_c;
+ }
+ }
+ /* Fall through. */
+
+ case MATCH_OPERATOR:
+ case MATCH_SCRATCH:
+ case MATCH_PARALLEL:
+ XINT (pattern, 0) += max_op;
+ break;
+
+ default:
+ break;
+ }
+
+ fmt = GET_RTX_FORMAT (code);
+ len = GET_RTX_LENGTH (code);
+ for (i = 0; i < len; i++)
+ {
+ rtx r;
+
+ switch (fmt[i])
+ {
+ case 'e': case 'u':
+ r = alter_predicate_for_insn (XEXP (pattern, i), alt,
+ max_op, lineno);
+ if (r == NULL)
+ return r;
+ break;
+
+ case 'E':
+ for (j = XVECLEN (pattern, i) - 1; j >= 0; --j)
+ {
+ r = alter_predicate_for_insn (XVECEXP (pattern, i, j),
+ alt, max_op, lineno);
+ if (r == NULL)
+ return r;
+ }
+ break;
+
+ case 'i': case 'w': case '0': case 's':
+ break;
+
+ default:
+ gcc_unreachable ();
+ }
+ }
+
+ return pattern;
+}
+
+static const char *
+alter_test_for_insn (struct queue_elem *ce_elem,
+ struct queue_elem *insn_elem)
+{
+ const char *ce_test, *insn_test;
+
+ ce_test = XSTR (ce_elem->data, 1);
+ insn_test = XSTR (insn_elem->data, 2);
+ if (!ce_test || *ce_test == '\0')
+ return insn_test;
+ if (!insn_test || *insn_test == '\0')
+ return ce_test;
+
+ return concat ("(", ce_test, ") && (", insn_test, ")", NULL);
+}
+
+/* Adjust all of the operand numbers in SRC to match the shift they'll
+ get from an operand displacement of DISP. Return a pointer after the
+ adjusted string. */
+
+static char *
+shift_output_template (char *dest, const char *src, int disp)
+{
+ while (*src)
+ {
+ char c = *src++;
+ *dest++ = c;
+ if (c == '%')
+ {
+ c = *src++;
+ if (ISDIGIT ((unsigned char) c))
+ c += disp;
+ else if (ISALPHA (c))
+ {
+ *dest++ = c;
+ c = *src++ + disp;
+ }
+ *dest++ = c;
+ }
+ }
+
+ return dest;
+}
+
+static const char *
+alter_output_for_insn (struct queue_elem *ce_elem,
+ struct queue_elem *insn_elem,
+ int alt, int max_op)
+{
+ const char *ce_out, *insn_out;
+ char *result, *p;
+ size_t len, ce_len, insn_len;
+
+ /* ??? Could coordinate with genoutput to not duplicate code here. */
+
+ ce_out = XSTR (ce_elem->data, 2);
+ insn_out = XTMPL (insn_elem->data, 3);
+ if (!ce_out || *ce_out == '\0')
+ return insn_out;
+
+ ce_len = strlen (ce_out);
+ insn_len = strlen (insn_out);
+
+ if (*insn_out == '*')
+ /* You must take care of the predicate yourself. */
+ return insn_out;
+
+ if (*insn_out == '@')
+ {
+ len = (ce_len + 1) * alt + insn_len + 1;
+ p = result = XNEWVEC(char, len);
+
+ do
+ {
+ do
+ *p++ = *insn_out++;
+ while (ISSPACE ((unsigned char) *insn_out));
+
+ if (*insn_out != '#')
+ {
+ p = shift_output_template (p, ce_out, max_op);
+ *p++ = ' ';
+ }
+
+ do
+ *p++ = *insn_out++;
+ while (*insn_out && *insn_out != '\n');
+ }
+ while (*insn_out);
+ *p = '\0';
+ }
+ else
+ {
+ len = ce_len + 1 + insn_len + 1;
+ result = XNEWVEC (char, len);
+
+ p = shift_output_template (result, ce_out, max_op);
+ *p++ = ' ';
+ memcpy (p, insn_out, insn_len + 1);
+ }
+
+ return result;
+}
+
+/* Replicate insns as appropriate for the given DEFINE_COND_EXEC. */
+
+static void
+process_one_cond_exec (struct queue_elem *ce_elem)
+{
+ struct queue_elem *insn_elem;
+ for (insn_elem = define_insn_queue; insn_elem ; insn_elem = insn_elem->next)
+ {
+ int alternatives, max_operand;
+ rtx pred, insn, pattern, split;
+ int i;
+
+ if (! is_predicable (insn_elem))
+ continue;
+
+ alternatives = 1;
+ max_operand = -1;
+ collect_insn_data (insn_elem->data, &alternatives, &max_operand);
+ max_operand += 1;
+
+ if (XVECLEN (ce_elem->data, 0) != 1)
+ {
+ message_with_line (ce_elem->lineno,
+ "too many patterns in predicate");
+ errors = 1;
+ return;
+ }
+
+ pred = copy_rtx (XVECEXP (ce_elem->data, 0, 0));
+ pred = alter_predicate_for_insn (pred, alternatives, max_operand,
+ ce_elem->lineno);
+ if (pred == NULL)
+ return;
+
+ /* Construct a new pattern for the new insn. */
+ insn = copy_rtx (insn_elem->data);
+ XSTR (insn, 0) = "";
+ pattern = rtx_alloc (COND_EXEC);
+ XEXP (pattern, 0) = pred;
+ if (XVECLEN (insn, 1) == 1)
+ {
+ XEXP (pattern, 1) = XVECEXP (insn, 1, 0);
+ XVECEXP (insn, 1, 0) = pattern;
+ PUT_NUM_ELEM (XVEC (insn, 1), 1);
+ }
+ else
+ {
+ XEXP (pattern, 1) = rtx_alloc (PARALLEL);
+ XVEC (XEXP (pattern, 1), 0) = XVEC (insn, 1);
+ XVEC (insn, 1) = rtvec_alloc (1);
+ XVECEXP (insn, 1, 0) = pattern;
+ }
+
+ XSTR (insn, 2) = alter_test_for_insn (ce_elem, insn_elem);
+ XTMPL (insn, 3) = alter_output_for_insn (ce_elem, insn_elem,
+ alternatives, max_operand);
+
+ /* ??? Set `predicable' to false. Not crucial since it's really
+ only used here, and we won't reprocess this new pattern. */
+
+ /* Put the new pattern on the `other' list so that it
+ (a) is not reprocessed by other define_cond_exec patterns
+ (b) appears after all normal define_insn patterns.
+
+ ??? B is debatable. If one has normal insns that match
+ cond_exec patterns, they will be preferred over these
+ generated patterns. Whether this matters in practice, or if
+ it's a good thing, or whether we should thread these new
+ patterns into the define_insn chain just after their generator
+ is something we'll have to experiment with. */
+
+ queue_pattern (insn, &other_tail, insn_elem->filename,
+ insn_elem->lineno);
+
+ if (!insn_elem->split)
+ continue;
+
+ /* If the original insn came from a define_insn_and_split,
+ generate a new split to handle the predicated insn. */
+ split = copy_rtx (insn_elem->split->data);
+ /* Predicate the pattern matched by the split. */
+ pattern = rtx_alloc (COND_EXEC);
+ XEXP (pattern, 0) = pred;
+ if (XVECLEN (split, 0) == 1)
+ {
+ XEXP (pattern, 1) = XVECEXP (split, 0, 0);
+ XVECEXP (split, 0, 0) = pattern;
+ PUT_NUM_ELEM (XVEC (split, 0), 1);
+ }
+ else
+ {
+ XEXP (pattern, 1) = rtx_alloc (PARALLEL);
+ XVEC (XEXP (pattern, 1), 0) = XVEC (split, 0);
+ XVEC (split, 0) = rtvec_alloc (1);
+ XVECEXP (split, 0, 0) = pattern;
+ }
+ /* Predicate all of the insns generated by the split. */
+ for (i = 0; i < XVECLEN (split, 2); i++)
+ {
+ pattern = rtx_alloc (COND_EXEC);
+ XEXP (pattern, 0) = pred;
+ XEXP (pattern, 1) = XVECEXP (split, 2, i);
+ XVECEXP (split, 2, i) = pattern;
+ }
+ /* Add the new split to the queue. */
+ queue_pattern (split, &other_tail, read_rtx_filename,
+ insn_elem->split->lineno);
+ }
+}
+
+/* If we have any DEFINE_COND_EXEC patterns, expand the DEFINE_INSN
+ patterns appropriately. */
+
+static void
+process_define_cond_exec (void)
+{
+ struct queue_elem *elem;
+
+ identify_predicable_attribute ();
+ if (errors)
+ return;
+
+ for (elem = define_cond_exec_queue; elem ; elem = elem->next)
+ process_one_cond_exec (elem);
+}
+
+static char *
+save_string (const char *s, int len)
+{
+ char *result = XNEWVEC (char, len + 1);
+
+ memcpy (result, s, len);
+ result[len] = 0;
+ return result;
+}
+
+\f