+/* For most cases, the simplified condition we found is the best
+ choice, but this is not the case for the min/max/abs transforms.
+ For these we wish to know that it is A or B in the condition. */
+
+static rtx
+noce_get_alt_condition (if_info, target, earliest)
+ struct noce_if_info *if_info;
+ rtx target;
+ rtx *earliest;
+{
+ rtx cond, set, insn;
+ int reverse;
+
+ /* If target is already mentioned in the known condition, return it. */
+ if (reg_mentioned_p (target, if_info->cond))
+ {
+ *earliest = if_info->cond_earliest;
+ return if_info->cond;
+ }
+
+ set = pc_set (if_info->jump);
+ cond = XEXP (SET_SRC (set), 0);
+ reverse
+ = GET_CODE (XEXP (SET_SRC (set), 2)) == LABEL_REF
+ && XEXP (XEXP (SET_SRC (set), 2), 0) == JUMP_LABEL (if_info->jump);
+
+ /* If we're looking for a constant, try to make the conditional
+ have that constant in it. There are two reasons why it may
+ not have the constant we want:
+
+ 1. GCC may have needed to put the constant in a register, because
+ the target can't compare directly against that constant. For
+ this case, we look for a SET immediately before the comparison
+ that puts a constant in that register.
+
+ 2. GCC may have canonicalized the conditional, for example
+ replacing "if x < 4" with "if x <= 3". We can undo that (or
+ make equivalent types of changes) to get the constants we need
+ if they're off by one in the right direction. */
+
+ if (GET_CODE (target) == CONST_INT)
+ {
+ enum rtx_code code = GET_CODE (if_info->cond);
+ rtx op_a = XEXP (if_info->cond, 0);
+ rtx op_b = XEXP (if_info->cond, 1);
+ rtx prev_insn;
+
+ /* First, look to see if we put a constant in a register. */
+ prev_insn = PREV_INSN (if_info->cond_earliest);
+ if (prev_insn
+ && INSN_P (prev_insn)
+ && GET_CODE (PATTERN (prev_insn)) == SET)
+ {
+ rtx src = find_reg_equal_equiv_note (prev_insn);
+ if (!src)
+ src = SET_SRC (PATTERN (prev_insn));
+ if (GET_CODE (src) == CONST_INT)
+ {
+ if (rtx_equal_p (op_a, SET_DEST (PATTERN (prev_insn))))
+ op_a = src;
+ else if (rtx_equal_p (op_b, SET_DEST (PATTERN (prev_insn))))
+ op_b = src;
+
+ if (GET_CODE (op_a) == CONST_INT)
+ {
+ rtx tmp = op_a;
+ op_a = op_b;
+ op_b = tmp;
+ code = swap_condition (code);
+ }
+ }
+ }
+
+ /* Now, look to see if we can get the right constant by
+ adjusting the conditional. */
+ if (GET_CODE (op_b) == CONST_INT)
+ {
+ HOST_WIDE_INT desired_val = INTVAL (target);
+ HOST_WIDE_INT actual_val = INTVAL (op_b);
+
+ switch (code)
+ {
+ case LT:
+ if (actual_val == desired_val + 1)
+ {
+ code = LE;
+ op_b = GEN_INT (desired_val);
+ }
+ break;
+ case LE:
+ if (actual_val == desired_val - 1)
+ {
+ code = LT;
+ op_b = GEN_INT (desired_val);
+ }
+ break;
+ case GT:
+ if (actual_val == desired_val - 1)
+ {
+ code = GE;
+ op_b = GEN_INT (desired_val);
+ }
+ break;
+ case GE:
+ if (actual_val == desired_val + 1)
+ {
+ code = GT;
+ op_b = GEN_INT (desired_val);
+ }
+ break;
+ default:
+ break;
+ }
+ }
+
+ /* If we made any changes, generate a new conditional that is
+ equivalent to what we started with, but has the right
+ constants in it. */
+ if (code != GET_CODE (if_info->cond)
+ || op_a != XEXP (if_info->cond, 0)
+ || op_b != XEXP (if_info->cond, 1))
+ {
+ cond = gen_rtx_fmt_ee (code, GET_MODE (cond), op_a, op_b);
+ *earliest = if_info->cond_earliest;
+ return cond;
+ }
+ }
+
+ cond = canonicalize_condition (if_info->jump, cond, reverse,
+ earliest, target);
+ if (! cond || ! reg_mentioned_p (target, cond))
+ return NULL;
+
+ /* We almost certainly searched back to a different place.
+ Need to re-verify correct lifetimes. */
+
+ /* X may not be mentioned in the range (cond_earliest, jump]. */
+ for (insn = if_info->jump; insn != *earliest; insn = PREV_INSN (insn))
+ if (INSN_P (insn) && reg_mentioned_p (if_info->x, insn))
+ return NULL;
+
+ /* A and B may not be modified in the range [cond_earliest, jump). */
+ for (insn = *earliest; insn != if_info->jump; insn = NEXT_INSN (insn))
+ if (INSN_P (insn)
+ && (modified_in_p (if_info->a, insn)
+ || modified_in_p (if_info->b, insn)))
+ return NULL;
+
+ return cond;
+}
+
+/* Convert "if (a < b) x = a; else x = b;" to "x = min(a, b);", etc. */
+
+static int
+noce_try_minmax (if_info)
+ struct noce_if_info *if_info;
+{
+ rtx cond, earliest, target, seq;
+ enum rtx_code code, op;
+ int unsignedp;
+
+ /* ??? Can't guarantee that expand_binop won't create pseudos. */
+ if (no_new_pseudos)
+ return FALSE;
+
+ /* ??? Reject modes with NaNs or signed zeros since we don't know how
+ they will be resolved with an SMIN/SMAX. It wouldn't be too hard
+ to get the target to tell us... */
+ if (HONOR_SIGNED_ZEROS (GET_MODE (if_info->x))
+ || HONOR_NANS (GET_MODE (if_info->x)))
+ return FALSE;
+
+ cond = noce_get_alt_condition (if_info, if_info->a, &earliest);
+ if (!cond)
+ return FALSE;
+
+ /* Verify the condition is of the form we expect, and canonicalize
+ the comparison code. */
+ code = GET_CODE (cond);
+ if (rtx_equal_p (XEXP (cond, 0), if_info->a))
+ {
+ if (! rtx_equal_p (XEXP (cond, 1), if_info->b))
+ return FALSE;
+ }
+ else if (rtx_equal_p (XEXP (cond, 1), if_info->a))
+ {
+ if (! rtx_equal_p (XEXP (cond, 0), if_info->b))
+ return FALSE;
+ code = swap_condition (code);
+ }
+ else
+ return FALSE;
+
+ /* Determine what sort of operation this is. Note that the code is for
+ a taken branch, so the code->operation mapping appears backwards. */
+ switch (code)
+ {
+ case LT:
+ case LE:
+ case UNLT:
+ case UNLE:
+ op = SMAX;
+ unsignedp = 0;
+ break;
+ case GT:
+ case GE:
+ case UNGT:
+ case UNGE:
+ op = SMIN;
+ unsignedp = 0;
+ break;
+ case LTU:
+ case LEU:
+ op = UMAX;
+ unsignedp = 1;
+ break;
+ case GTU:
+ case GEU:
+ op = UMIN;
+ unsignedp = 1;
+ break;
+ default:
+ return FALSE;
+ }
+
+ start_sequence ();
+
+ target = expand_simple_binop (GET_MODE (if_info->x), op,
+ if_info->a, if_info->b,
+ if_info->x, unsignedp, OPTAB_WIDEN);
+ if (! target)
+ {
+ end_sequence ();
+ return FALSE;
+ }
+ if (target != if_info->x)
+ noce_emit_move_insn (if_info->x, target);
+
+ seq = get_insns ();
+ end_sequence ();
+
+ if (seq_contains_jump (seq))
+ return FALSE;
+
+ emit_insns_before_scope (seq, if_info->jump, INSN_SCOPE (if_info->insn_a));
+ if_info->cond = cond;
+ if_info->cond_earliest = earliest;
+
+ return TRUE;
+}
+
+/* Convert "if (a < 0) x = -a; else x = a;" to "x = abs(a);", etc. */
+
+static int
+noce_try_abs (if_info)
+ struct noce_if_info *if_info;
+{
+ rtx cond, earliest, target, seq, a, b, c;
+ int negate;
+
+ /* ??? Can't guarantee that expand_binop won't create pseudos. */
+ if (no_new_pseudos)
+ return FALSE;
+
+ /* Recognize A and B as constituting an ABS or NABS. */
+ a = if_info->a;
+ b = if_info->b;
+ if (GET_CODE (a) == NEG && rtx_equal_p (XEXP (a, 0), b))
+ negate = 0;
+ else if (GET_CODE (b) == NEG && rtx_equal_p (XEXP (b, 0), a))
+ {
+ c = a; a = b; b = c;
+ negate = 1;
+ }
+ else
+ return FALSE;
+
+ cond = noce_get_alt_condition (if_info, b, &earliest);
+ if (!cond)
+ return FALSE;
+
+ /* Verify the condition is of the form we expect. */
+ if (rtx_equal_p (XEXP (cond, 0), b))
+ c = XEXP (cond, 1);
+ else if (rtx_equal_p (XEXP (cond, 1), b))
+ c = XEXP (cond, 0);
+ else
+ return FALSE;
+
+ /* Verify that C is zero. Search backward through the block for
+ a REG_EQUAL note if necessary. */
+ if (REG_P (c))
+ {
+ rtx insn, note = NULL;
+ for (insn = earliest;
+ insn != if_info->test_bb->head;
+ insn = PREV_INSN (insn))
+ if (INSN_P (insn)
+ && ((note = find_reg_note (insn, REG_EQUAL, c))
+ || (note = find_reg_note (insn, REG_EQUIV, c))))
+ break;
+ if (! note)
+ return FALSE;
+ c = XEXP (note, 0);
+ }
+ if (GET_CODE (c) == MEM
+ && GET_CODE (XEXP (c, 0)) == SYMBOL_REF
+ && CONSTANT_POOL_ADDRESS_P (XEXP (c, 0)))
+ c = get_pool_constant (XEXP (c, 0));
+
+ /* Work around funny ideas get_condition has wrt canonicalization.
+ Note that these rtx constants are known to be CONST_INT, and
+ therefore imply integer comparisons. */
+ if (c == constm1_rtx && GET_CODE (cond) == GT)
+ ;
+ else if (c == const1_rtx && GET_CODE (cond) == LT)
+ ;
+ else if (c != CONST0_RTX (GET_MODE (b)))
+ return FALSE;
+
+ /* Determine what sort of operation this is. */
+ switch (GET_CODE (cond))
+ {
+ case LT:
+ case LE:
+ case UNLT:
+ case UNLE:
+ negate = !negate;
+ break;
+ case GT:
+ case GE:
+ case UNGT:
+ case UNGE:
+ break;
+ default:
+ return FALSE;
+ }
+
+ start_sequence ();
+
+ target = expand_simple_unop (GET_MODE (if_info->x), ABS, b, if_info->x, 0);
+
+ /* ??? It's a quandry whether cmove would be better here, especially
+ for integers. Perhaps combine will clean things up. */
+ if (target && negate)
+ target = expand_simple_unop (GET_MODE (target), NEG, target, if_info->x, 0);
+
+ if (! target)
+ {
+ end_sequence ();
+ return FALSE;
+ }
+
+ if (target != if_info->x)
+ noce_emit_move_insn (if_info->x, target);
+
+ seq = get_insns ();
+ end_sequence ();
+
+ if (seq_contains_jump (seq))
+ return FALSE;
+
+ emit_insns_before_scope (seq, if_info->jump, INSN_SCOPE (if_info->insn_a));
+ if_info->cond = cond;
+ if_info->cond_earliest = earliest;
+
+ return TRUE;
+}
+