+
+static int
+find_matches (insn, matchp)
+ rtx insn;
+ struct match *matchp;
+{
+ int likely_spilled[MAX_RECOG_OPERANDS];
+ int operand_number;
+ int insn_code_number = recog_memoized (insn);
+ int any_matches = 0;
+
+ if (insn_code_number < 0)
+ return -1;
+
+ insn_extract (insn);
+ if (! constrain_operands (insn_code_number, 0))
+ return -1;
+
+ /* Must initialize this before main loop, because the code for
+ the commutative case may set matches for operands other than
+ the current one. */
+ for (operand_number = insn_n_operands[insn_code_number];
+ --operand_number >= 0; )
+ matchp->with[operand_number] = matchp->commutative[operand_number] = -1;
+
+ for (operand_number = 0; operand_number < insn_n_operands[insn_code_number];
+ operand_number++)
+ {
+ int output_operand = 0;
+ int matching_operand = operand_number;
+ char *p, c;
+ int i = 0;
+
+ p = insn_operand_constraint[insn_code_number][operand_number];
+
+ likely_spilled[operand_number] = 0;
+ matchp->use[operand_number] = READ;
+ if (*p == '=')
+ matchp->use[operand_number] = WRITE;
+ else if (*p == '+')
+ matchp->use[operand_number] = READWRITE;
+
+ for (;*p && i < which_alternative; p++)
+ if (*p == ',')
+ i++;
+
+ while ((c = *p++) != '\0' && c != ',')
+ switch (c)
+ {
+ case '=':
+ break;
+ case '+':
+ break;
+ case '&':
+ matchp->early_clobber[operand_number] = 1;
+ break;
+ case '%':
+ matchp->commutative[operand_number] = operand_number + 1;
+ matchp->commutative[operand_number + 1] = operand_number;
+ break;
+ case '0': case '1': case '2': case '3': case '4':
+ case '5': case '6': case '7': case '8': case '9':
+ c -= '0';
+ if (c < operand_number && likely_spilled[c])
+ break;
+ matchp->with[operand_number] = c;
+ any_matches = 1;
+ if (matchp->commutative[operand_number] >= 0)
+ matchp->with[matchp->commutative[operand_number]] = c;
+ break;
+ case 'a': case 'b': case 'c': case 'd': case 'e': case 'f': case 'h':
+ case 'j': case 'k': case 'l': case 'p': case 'q': case 't': case 'u':
+ case 'v': case 'w': case 'x': case 'y': case 'z': case 'A': case 'B':
+ case 'C': case 'D': case 'W': case 'Y': case 'Z':
+ if (CLASS_LIKELY_SPILLED_P (REG_CLASS_FROM_LETTER (c)))
+ likely_spilled[operand_number] = 1;
+ break;
+ }
+ }
+ return any_matches ? insn_code_number : -1;
+}
+
+/* Try to replace output operand DST in SET, with input operand SRC. SET is
+ the only set in INSN. INSN has just been recgnized and constrained.
+ SRC is operand number OPERAND_NUMBER in INSN.
+ DST is operand number MATCH_NUMBER in INSN.
+ If BACKWARD is nonzero, we have been called in a backward pass.
+ Return nonzero for success. */
+static int
+fixup_match_1 (insn, set, src, src_subreg, dst, backward, operand_number,
+ match_number, regmove_dump_file)
+ rtx insn, set, src, src_subreg, dst;
+ int backward, operand_number, match_number;
+ FILE *regmove_dump_file;
+{
+ rtx p;
+ rtx post_inc = 0, post_inc_set = 0, search_end = 0;
+ int success = 0;
+ int num_calls = 0, s_num_calls = 0;
+ enum rtx_code code = NOTE;
+ HOST_WIDE_INT insn_const, newconst;
+ rtx overlap = 0; /* need to move insn ? */
+ rtx src_note = find_reg_note (insn, REG_DEAD, src), dst_note;
+ int length, s_length, true_loop_depth;
+
+ if (! src_note)
+ {
+ /* Look for (set (regX) (op regA constX))
+ (set (regY) (op regA constY))
+ and change that to
+ (set (regA) (op regA constX)).
+ (set (regY) (op regA constY-constX)).
+ This works for add and shift operations, if
+ regA is dead after or set by the second insn. */
+
+ code = GET_CODE (SET_SRC (set));
+ if ((code == PLUS || code == LSHIFTRT
+ || code == ASHIFT || code == ASHIFTRT)
+ && XEXP (SET_SRC (set), 0) == src
+ && GET_CODE (XEXP (SET_SRC (set), 1)) == CONST_INT)
+ insn_const = INTVAL (XEXP (SET_SRC (set), 1));
+ else if (! stable_but_for_p (SET_SRC (set), src, dst))
+ return 0;
+ else
+ /* We might find a src_note while scanning. */
+ code = NOTE;
+ }
+
+ if (regmove_dump_file)
+ fprintf (regmove_dump_file,
+ "Could fix operand %d of insn %d matching operand %d.\n",
+ operand_number, INSN_UID (insn), match_number);
+
+ /* If SRC is equivalent to a constant set in a different basic block,
+ then do not use it for this optimization. We want the equivalence
+ so that if we have to reload this register, we can reload the
+ constant, rather than extending the lifespan of the register. */
+ if (reg_is_remote_constant_p (src, insn, get_insns ()))
+ return 0;
+
+ /* Scan forward to find the next instruction that
+ uses the output operand. If the operand dies here,
+ then replace it in both instructions with
+ operand_number. */
+
+ for (length = s_length = 0, p = NEXT_INSN (insn); p; p = NEXT_INSN (p))
+ {
+ if (GET_CODE (p) == CODE_LABEL || GET_CODE (p) == JUMP_INSN
+ || (GET_CODE (p) == NOTE
+ && (NOTE_LINE_NUMBER (p) == NOTE_INSN_LOOP_BEG
+ || NOTE_LINE_NUMBER (p) == NOTE_INSN_LOOP_END)))
+ break;
+
+ if (GET_RTX_CLASS (GET_CODE (p)) != 'i')
+ continue;
+
+ length++;
+ if (src_note)
+ s_length++;
+
+ if (reg_set_p (src, p) || reg_set_p (dst, p)
+ || (GET_CODE (PATTERN (p)) == USE
+ && reg_overlap_mentioned_p (src, XEXP (PATTERN (p), 0))))
+ break;
+
+ /* See if all of DST dies in P. This test is
+ slightly more conservative than it needs to be. */
+ if ((dst_note = find_regno_note (p, REG_DEAD, REGNO (dst)))
+ && (GET_MODE (XEXP (dst_note, 0)) == GET_MODE (dst)))
+ {
+ if (! src_note)
+ {
+ rtx q;
+ rtx set2;
+
+ /* If an optimization is done, the value of SRC while P
+ is executed will be changed. Check that this is OK. */
+ if (reg_overlap_mentioned_p (src, PATTERN (p)))
+ break;
+ for (q = p; q; q = NEXT_INSN (q))
+ {
+ if (GET_CODE (q) == CODE_LABEL || GET_CODE (q) == JUMP_INSN
+ || (GET_CODE (q) == NOTE
+ && (NOTE_LINE_NUMBER (q) == NOTE_INSN_LOOP_BEG
+ || NOTE_LINE_NUMBER (q) == NOTE_INSN_LOOP_END)))
+ {
+ q = 0;
+ break;
+ }
+ if (GET_RTX_CLASS (GET_CODE (q)) != 'i')
+ continue;
+ if (reg_overlap_mentioned_p (src, PATTERN (q))
+ || reg_set_p (src, q))
+ break;
+ }
+ if (q)
+ set2 = single_set (q);
+ if (! q || ! set2 || GET_CODE (SET_SRC (set2)) != code
+ || XEXP (SET_SRC (set2), 0) != src
+ || GET_CODE (XEXP (SET_SRC (set2), 1)) != CONST_INT
+ || (SET_DEST (set2) != src
+ && ! find_reg_note (q, REG_DEAD, src)))
+ {
+ /* If this is a PLUS, we can still save a register by doing
+ src += insn_const;
+ P;
+ src -= insn_const; .
+ This also gives opportunities for subsequent
+ optimizations in the backward pass, so do it there. */
+ if (code == PLUS && backward
+#ifdef HAVE_cc0
+ /* We may not emit an insn directly
+ after P if the latter sets CC0. */
+ && ! sets_cc0_p (PATTERN (p))
+#endif
+ )
+
+ {
+ search_end = q;
+ q = insn;
+ set2 = set;
+ newconst = -insn_const;
+ code = MINUS;
+ }
+ else
+ break;
+ }
+ else
+ {
+ newconst = INTVAL (XEXP (SET_SRC (set2), 1)) - insn_const;
+ /* Reject out of range shifts. */
+ if (code != PLUS
+ && (newconst < 0
+ || (newconst
+ >= GET_MODE_BITSIZE (GET_MODE (SET_SRC (set2))))))
+ break;
+ if (code == PLUS)
+ {
+ post_inc = q;
+ if (SET_DEST (set2) != src)
+ post_inc_set = set2;
+ }
+ }
+ /* We use 1 as last argument to validate_change so that all
+ changes are accepted or rejected together by apply_change_group
+ when it is called by validate_replace_rtx . */
+ validate_change (q, &XEXP (SET_SRC (set2), 1),
+ GEN_INT (newconst), 1);
+ }
+ validate_change (insn, recog_operand_loc[match_number], src, 1);
+ if (validate_replace_rtx (dst, src_subreg, p))
+ success = 1;
+ break;
+ }
+
+ if (reg_overlap_mentioned_p (dst, PATTERN (p)))
+ break;
+ if (! src_note && reg_overlap_mentioned_p (src, PATTERN (p)))
+ {
+ /* INSN was already checked to be movable when
+ we found no REG_DEAD note for src on it. */
+ overlap = p;
+ src_note = find_reg_note (p, REG_DEAD, src);
+ }
+
+ /* If we have passed a call instruction, and the pseudo-reg SRC is not
+ already live across a call, then don't perform the optimization. */
+ if (GET_CODE (p) == CALL_INSN)
+ {
+ if (REG_N_CALLS_CROSSED (REGNO (src)) == 0)
+ break;
+
+ num_calls++;
+
+ if (src_note)
+ s_num_calls++;
+
+ }
+ }
+
+ if (! success)
+ return 0;
+
+ true_loop_depth = backward ? 2 - loop_depth : loop_depth;
+
+ /* Remove the death note for DST from P. */
+ remove_note (p, dst_note);
+ if (code == MINUS)
+ {
+ post_inc = emit_insn_after (copy_rtx (PATTERN (insn)), p);
+#if defined (HAVE_PRE_INCREMENT) || defined (HAVE_PRE_DECREMENT)
+ if (search_end
+ && try_auto_increment (search_end, post_inc, 0, src, newconst, 1))
+ post_inc = 0;
+#endif
+ validate_change (insn, &XEXP (SET_SRC (set), 1), GEN_INT (insn_const), 0);
+ REG_N_SETS (REGNO (src))++;
+ REG_N_REFS (REGNO (src)) += true_loop_depth;
+ REG_LIVE_LENGTH (REGNO (src))++;
+ }
+ if (overlap)
+ {
+ /* The lifetime of src and dest overlap,
+ but we can change this by moving insn. */
+ rtx pat = PATTERN (insn);
+ if (src_note)
+ remove_note (overlap, src_note);
+#if defined (HAVE_POST_INCREMENT) || defined (HAVE_POST_DECREMENT)
+ if (code == PLUS
+ && try_auto_increment (overlap, insn, 0, src, insn_const, 0))
+ insn = overlap;
+ else
+#endif
+ {
+ rtx notes = REG_NOTES (insn);
+
+ emit_insn_after_with_line_notes (pat, PREV_INSN (p), insn);
+ PUT_CODE (insn, NOTE);
+ NOTE_LINE_NUMBER (insn) = NOTE_INSN_DELETED;
+ NOTE_SOURCE_FILE (insn) = 0;
+ /* emit_insn_after_with_line_notes has no
+ return value, so search for the new insn. */
+ for (insn = p; PATTERN (insn) != pat; )
+ insn = PREV_INSN (insn);
+
+ REG_NOTES (insn) = notes;
+ }
+ }
+ /* Sometimes we'd generate src = const; src += n;
+ if so, replace the instruction that set src
+ in the first place. */
+
+ if (! overlap && (code == PLUS || code == MINUS))
+ {
+ rtx note = find_reg_note (insn, REG_EQUAL, NULL_RTX);
+ rtx q, set2;
+ int num_calls2 = 0, s_length2 = 0;
+
+ if (note && CONSTANT_P (XEXP (note, 0)))
+ {
+ for (q = PREV_INSN (insn); q; q = PREV_INSN(q))
+ {
+ if (GET_CODE (q) == JUMP_INSN)
+ {
+ q = 0;
+ break;
+ }
+ if (GET_RTX_CLASS (GET_CODE (q)) != 'i')
+ continue;
+ s_length2++;
+ if (reg_set_p (src, q))
+ {
+ set2 = single_set (q);
+ break;
+ }
+ if (reg_overlap_mentioned_p (src, PATTERN (q)))
+ {
+ q = 0;
+ break;
+ }
+ if (GET_CODE (p) == CALL_INSN)
+ num_calls2++;
+ }
+ if (q && set2 && SET_DEST (set2) == src && CONSTANT_P (SET_SRC (set2))
+ && validate_change (insn, &SET_SRC (set), XEXP (note, 0), 0))
+ {
+ PUT_CODE (q, NOTE);
+ NOTE_LINE_NUMBER (q) = NOTE_INSN_DELETED;
+ NOTE_SOURCE_FILE (q) = 0;
+ REG_N_SETS (REGNO (src))--;
+ REG_N_CALLS_CROSSED (REGNO (src)) -= num_calls2;
+ REG_N_REFS (REGNO (src)) -= true_loop_depth;
+ REG_LIVE_LENGTH (REGNO (src)) -= s_length2;
+ insn_const = 0;
+ }
+ }
+ }
+ if (0) ;
+#if defined (HAVE_PRE_INCREMENT) || defined (HAVE_PRE_DECREMENT)
+ else if ((code == PLUS || code == MINUS) && insn_const
+ && try_auto_increment (p, insn, 0, src, insn_const, 1))
+ insn = p;
+#endif
+#if defined (HAVE_POST_INCREMENT) || defined (HAVE_POST_DECREMENT)
+ else if (post_inc
+ && try_auto_increment (p, post_inc, post_inc_set, src, newconst, 0))
+ post_inc = 0;
+#endif
+#if defined (HAVE_PRE_INCREMENT) || defined (HAVE_PRE_DECREMENT)
+ /* If post_inc still prevails, try to find an
+ insn where it can be used as a pre-in/decrement.
+ If code is MINUS, this was already tried. */
+ if (post_inc && code == PLUS
+ /* Check that newconst is likely to be usable
+ in a pre-in/decrement before starting the search. */
+ && (0
+#if defined (HAVE_PRE_INCREMENT)
+ || (newconst > 0 && newconst <= MOVE_MAX)
+#endif
+#if defined (HAVE_PRE_DECREMENT)
+ || (newconst < 0 && newconst >= -MOVE_MAX)
+#endif
+ ) && exact_log2 (newconst))
+ {
+ rtx q, inc_dest;
+
+ inc_dest = post_inc_set ? SET_DEST (post_inc_set) : src;
+ for (q = post_inc; q = NEXT_INSN (q); )
+ {
+ if (GET_CODE (q) == CODE_LABEL || GET_CODE (q) == JUMP_INSN
+ || (GET_CODE (q) == NOTE
+ && (NOTE_LINE_NUMBER (q) == NOTE_INSN_LOOP_BEG
+ || NOTE_LINE_NUMBER (q) == NOTE_INSN_LOOP_END)))
+ break;
+ if (GET_RTX_CLASS (GET_CODE (q)) != 'i')
+ continue;
+ if (src != inc_dest && (reg_overlap_mentioned_p (src, PATTERN (q))
+ || reg_set_p (src, q)))
+ break;
+ if (reg_set_p (inc_dest, q))
+ break;
+ if (reg_overlap_mentioned_p (inc_dest, PATTERN (q)))
+ {
+ try_auto_increment (q, post_inc,
+ post_inc_set, inc_dest, newconst, 1);
+ break;
+ }
+ }
+ }
+#endif /* defined (HAVE_PRE_INCREMENT) || defined (HAVE_PRE_DECREMENT) */
+ /* Move the death note for DST to INSN if it is used
+ there. */
+ if (reg_overlap_mentioned_p (dst, PATTERN (insn)))
+ {
+ XEXP (dst_note, 1) = REG_NOTES (insn);
+ REG_NOTES (insn) = dst_note;
+ }
+
+ if (src_note)
+ {
+ /* Move the death note for SRC from INSN to P. */
+ if (! overlap)
+ remove_note (insn, src_note);
+ XEXP (src_note, 1) = REG_NOTES (p);
+ REG_NOTES (p) = src_note;
+
+ REG_N_CALLS_CROSSED (REGNO (src)) += s_num_calls;
+ }
+
+ REG_N_SETS (REGNO (src))++;
+ REG_N_SETS (REGNO (dst))--;
+
+ REG_N_CALLS_CROSSED (REGNO (dst)) -= num_calls;
+
+ REG_LIVE_LENGTH (REGNO (src)) += s_length;
+ if (REG_LIVE_LENGTH (REGNO (dst)) >= 0)
+ {
+ REG_LIVE_LENGTH (REGNO (dst)) -= length;
+ /* REG_LIVE_LENGTH is only an approximation after
+ combine if sched is not run, so make sure that we
+ still have a reasonable value. */
+ if (REG_LIVE_LENGTH (REGNO (dst)) < 2)
+ REG_LIVE_LENGTH (REGNO (dst)) = 2;
+ }
+
+ /* We assume that a register is used exactly once per
+ insn in the updates above. If this is not correct,
+ no great harm is done. */
+
+ REG_N_REFS (REGNO (src)) += 2 * true_loop_depth;
+ REG_N_REFS (REGNO (dst)) -= 2 * true_loop_depth;
+
+ /* If that was the only time dst was set,
+ and dst was not live at the start of the
+ function, we know that we have no more
+ references to dst; clear REG_N_REFS so it
+ won't make reload do any work. */
+ if (REG_N_SETS (REGNO (dst)) == 0
+ && ! regno_uninitialized (REGNO (dst)))
+ REG_N_REFS (REGNO (dst)) = 0;
+
+ if (regmove_dump_file)
+ fprintf (regmove_dump_file,
+ "Fixed operand %d of insn %d matching operand %d.\n",
+ operand_number, INSN_UID (insn), match_number);
+ return 1;
+}
+
+