#include "config.h"
#include "system.h"
#include "rtl.h"
#include "regs.h"
#include "function.h"
#include "flags.h"
#include "insn-config.h"
#include "recog.h"
#include "except.h"
#include "hard-reg-set.h"
#include "basic-block.h"
#include "expr.h"
#include "real.h"
#include "output.h"
#include "toplev.h"
#include "tm_p.h"
#ifndef HAVE_conditional_execution
#define HAVE_conditional_execution 0
#endif
#ifndef HAVE_conditional_move
#define HAVE_conditional_move 0
#endif
#ifndef HAVE_incscc
#define HAVE_incscc 0
#endif
#ifndef HAVE_decscc
#define HAVE_decscc 0
#endif
#ifndef HAVE_trap
#define HAVE_trap 0
#endif
#ifndef HAVE_conditional_trap
#define HAVE_conditional_trap 0
#endif
#ifndef MAX_CONDITIONAL_EXECUTE
#define MAX_CONDITIONAL_EXECUTE (BRANCH_COST + 1)
#endif
#define NULL_EDGE ((struct edge_def *)NULL)
#define NULL_BLOCK ((struct basic_block_def *)NULL)
static int num_possible_if_blocks;
static int num_updated_if_blocks;
static int num_removed_blocks;
static int cond_exec_changed_p;
static bool life_data_ok;
static dominance_info post_dominators;
static int count_bb_insns PARAMS ((basic_block));
static rtx first_active_insn PARAMS ((basic_block));
static rtx last_active_insn PARAMS ((basic_block, int));
static int seq_contains_jump PARAMS ((rtx));
static basic_block block_fallthru PARAMS ((basic_block));
static int cond_exec_process_insns PARAMS ((ce_if_block_t *,
rtx, rtx, rtx, rtx, int));
static rtx cond_exec_get_condition PARAMS ((rtx));
static int cond_exec_process_if_block PARAMS ((ce_if_block_t *, int));
static rtx noce_get_condition PARAMS ((rtx, rtx *));
static int noce_operand_ok PARAMS ((rtx));
static int noce_process_if_block PARAMS ((ce_if_block_t *));
static int process_if_block PARAMS ((ce_if_block_t *));
static void merge_if_block PARAMS ((ce_if_block_t *));
static int find_cond_trap PARAMS ((basic_block, edge, edge));
static basic_block find_if_header PARAMS ((basic_block, int));
static int block_jumps_and_fallthru_p PARAMS ((basic_block, basic_block));
static int find_if_block PARAMS ((ce_if_block_t *));
static int find_if_case_1 PARAMS ((basic_block, edge, edge));
static int find_if_case_2 PARAMS ((basic_block, edge, edge));
static int find_memory PARAMS ((rtx *, void *));
static int dead_or_predicable PARAMS ((basic_block, basic_block,
basic_block, basic_block, int));
static void noce_emit_move_insn PARAMS ((rtx, rtx));
static rtx block_has_only_trap PARAMS ((basic_block));
static int
count_bb_insns (bb)
basic_block bb;
{
int count = 0;
rtx insn = bb->head;
while (1)
{
if (GET_CODE (insn) == CALL_INSN || GET_CODE (insn) == INSN)
count++;
if (insn == bb->end)
break;
insn = NEXT_INSN (insn);
}
return count;
}
static rtx
first_active_insn (bb)
basic_block bb;
{
rtx insn = bb->head;
if (GET_CODE (insn) == CODE_LABEL)
{
if (insn == bb->end)
return NULL_RTX;
insn = NEXT_INSN (insn);
}
while (GET_CODE (insn) == NOTE)
{
if (insn == bb->end)
return NULL_RTX;
insn = NEXT_INSN (insn);
}
if (GET_CODE (insn) == JUMP_INSN)
return NULL_RTX;
return insn;
}
static rtx
last_active_insn (bb, skip_use_p)
basic_block bb;
int skip_use_p;
{
rtx insn = bb->end;
rtx head = bb->head;
while (GET_CODE (insn) == NOTE
|| GET_CODE (insn) == JUMP_INSN
|| (skip_use_p
&& GET_CODE (insn) == INSN
&& GET_CODE (PATTERN (insn)) == USE))
{
if (insn == head)
return NULL_RTX;
insn = PREV_INSN (insn);
}
if (GET_CODE (insn) == CODE_LABEL)
return NULL_RTX;
return insn;
}
static int
seq_contains_jump (insn)
rtx insn;
{
while (insn)
{
if (GET_CODE (insn) == JUMP_INSN)
return 1;
insn = NEXT_INSN (insn);
}
return 0;
}
static basic_block
block_fallthru (bb)
basic_block bb;
{
edge e;
for (e = bb->succ;
e != NULL_EDGE && (e->flags & EDGE_FALLTHRU) == 0;
e = e->succ_next)
;
return (e) ? e->dest : NULL_BLOCK;
}
static int
cond_exec_process_insns (ce_info, start, end, test, prob_val, mod_ok)
ce_if_block_t *ce_info ATTRIBUTE_UNUSED;
rtx start;
rtx end;
rtx test;
rtx prob_val;
int mod_ok;
{
int must_be_last = FALSE;
rtx insn;
rtx xtest;
rtx pattern;
if (!start || !end)
return FALSE;
for (insn = start; ; insn = NEXT_INSN (insn))
{
if (GET_CODE (insn) == NOTE)
goto insn_done;
if (GET_CODE (insn) != INSN && GET_CODE (insn) != CALL_INSN)
abort ();
if (reload_completed && GET_CODE (PATTERN (insn)) == USE)
{
PUT_CODE (insn, NOTE);
NOTE_LINE_NUMBER (insn) = NOTE_INSN_DELETED;
NOTE_SOURCE_FILE (insn) = 0;
goto insn_done;
}
if (must_be_last)
return FALSE;
if (modified_in_p (test, insn))
{
if (!mod_ok)
return FALSE;
must_be_last = TRUE;
}
pattern = PATTERN (insn);
xtest = copy_rtx (test);
if (GET_CODE (pattern) == COND_EXEC)
{
if (GET_MODE (xtest) != GET_MODE (COND_EXEC_TEST (pattern)))
return FALSE;
xtest = gen_rtx_AND (GET_MODE (xtest), xtest,
COND_EXEC_TEST (pattern));
pattern = COND_EXEC_CODE (pattern);
}
pattern = gen_rtx_COND_EXEC (VOIDmode, xtest, pattern);
#ifdef IFCVT_MODIFY_INSN
IFCVT_MODIFY_INSN (ce_info, pattern, insn);
if (! pattern)
return FALSE;
#endif
validate_change (insn, &PATTERN (insn), pattern, 1);
if (GET_CODE (insn) == CALL_INSN && prob_val)
validate_change (insn, ®_NOTES (insn),
alloc_EXPR_LIST (REG_BR_PROB, prob_val,
REG_NOTES (insn)), 1);
insn_done:
if (insn == end)
break;
}
return TRUE;
}
static rtx
cond_exec_get_condition (jump)
rtx jump;
{
rtx test_if, cond;
if (any_condjump_p (jump))
test_if = SET_SRC (pc_set (jump));
else
return NULL_RTX;
cond = XEXP (test_if, 0);
if (GET_CODE (XEXP (test_if, 2)) == LABEL_REF
&& XEXP (XEXP (test_if, 2), 0) == JUMP_LABEL (jump))
{
enum rtx_code rev = reversed_comparison_code (cond, jump);
if (rev == UNKNOWN)
return NULL_RTX;
cond = gen_rtx_fmt_ee (rev, GET_MODE (cond), XEXP (cond, 0),
XEXP (cond, 1));
}
return cond;
}
static int
cond_exec_process_if_block (ce_info, do_multiple_p)
ce_if_block_t * ce_info;
int do_multiple_p;
{
basic_block test_bb = ce_info->test_bb;
basic_block then_bb = ce_info->then_bb;
basic_block else_bb = ce_info->else_bb;
rtx test_expr;
rtx then_start;
rtx then_end;
rtx else_start = NULL_RTX;
rtx else_end = NULL_RTX;
int max;
int then_mod_ok;
rtx true_expr;
rtx false_expr;
rtx true_prob_val;
rtx false_prob_val;
int n_insns;
enum rtx_code false_code;
if (!do_multiple_p && ce_info->num_multiple_test_blocks)
{
if (else_bb || ! ce_info->and_and_p)
return FALSE;
ce_info->test_bb = test_bb = ce_info->last_test_bb;
ce_info->num_multiple_test_blocks = 0;
ce_info->num_and_and_blocks = 0;
ce_info->num_or_or_blocks = 0;
}
test_expr = cond_exec_get_condition (test_bb->end);
if (! test_expr)
return FALSE;
if (! onlyjump_p (test_bb->end))
return FALSE;
then_start = first_active_insn (then_bb);
then_end = last_active_insn (then_bb, TRUE);
n_insns = ce_info->num_then_insns = count_bb_insns (then_bb);
max = MAX_CONDITIONAL_EXECUTE;
if (else_bb)
{
max *= 2;
else_start = first_active_insn (else_bb);
else_end = last_active_insn (else_bb, TRUE);
n_insns += ce_info->num_else_insns = count_bb_insns (else_bb);
}
if (n_insns > max)
return FALSE;
true_expr = test_expr;
false_code = reversed_comparison_code (true_expr, test_bb->end);
if (false_code != UNKNOWN)
false_expr = gen_rtx_fmt_ee (false_code, GET_MODE (true_expr),
XEXP (true_expr, 0), XEXP (true_expr, 1));
else
false_expr = NULL_RTX;
#ifdef IFCVT_MODIFY_TESTS
IFCVT_MODIFY_TESTS (ce_info, true_expr, false_expr);
if (!true_expr || !false_expr)
goto fail;
#endif
true_prob_val = find_reg_note (test_bb->end, REG_BR_PROB, NULL_RTX);
if (true_prob_val)
{
true_prob_val = XEXP (true_prob_val, 0);
false_prob_val = GEN_INT (REG_BR_PROB_BASE - INTVAL (true_prob_val));
}
else
false_prob_val = NULL_RTX;
if (ce_info->num_multiple_test_blocks > 0)
{
basic_block bb = test_bb;
basic_block last_test_bb = ce_info->last_test_bb;
if (! false_expr)
goto fail;
do
{
rtx start, end;
rtx t, f;
bb = block_fallthru (bb);
start = first_active_insn (bb);
end = last_active_insn (bb, TRUE);
if (start
&& ! cond_exec_process_insns (ce_info, start, end, false_expr,
false_prob_val, FALSE))
goto fail;
if (! onlyjump_p (bb->end))
goto fail;
t = cond_exec_get_condition (bb->end);
if (! t)
goto fail;
f = gen_rtx_fmt_ee (reverse_condition (GET_CODE (t)),
GET_MODE (t),
XEXP (t, 0),
XEXP (t, 1));
if (ce_info->and_and_p)
{
t = gen_rtx_AND (GET_MODE (t), true_expr, t);
f = gen_rtx_IOR (GET_MODE (t), false_expr, f);
}
else
{
t = gen_rtx_IOR (GET_MODE (t), true_expr, t);
f = gen_rtx_AND (GET_MODE (t), false_expr, f);
}
#ifdef IFCVT_MODIFY_MULTIPLE_TESTS
IFCVT_MODIFY_MULTIPLE_TESTS (ce_info, bb, t, f);
if (!t || !f)
goto fail;
#endif
true_expr = t;
false_expr = f;
}
while (bb != last_test_bb);
}
then_mod_ok = (else_bb == NULL_BLOCK);
if (then_end
&& (! false_expr
|| ! cond_exec_process_insns (ce_info, then_start, then_end,
false_expr, false_prob_val,
then_mod_ok)))
goto fail;
if (else_bb && else_end
&& ! cond_exec_process_insns (ce_info, else_start, else_end,
true_expr, true_prob_val, TRUE))
goto fail;
if (! apply_change_group ())
{
#ifdef IFCVT_MODIFY_CANCEL
IFCVT_MODIFY_CANCEL (ce_info);
#endif
return FALSE;
}
#ifdef IFCVT_MODIFY_FINAL
IFCVT_MODIFY_FINAL (ce_info);
#endif
if (rtl_dump_file)
fprintf (rtl_dump_file, "%d insn%s converted to conditional execution.\n",
n_insns, (n_insns == 1) ? " was" : "s were");
merge_if_block (ce_info);
cond_exec_changed_p = TRUE;
return TRUE;
fail:
#ifdef IFCVT_MODIFY_CANCEL
IFCVT_MODIFY_CANCEL (ce_info);
#endif
cancel_changes (0);
return FALSE;
}
struct noce_if_info
{
basic_block test_bb;
rtx insn_a, insn_b;
rtx x, a, b;
rtx jump, cond, cond_earliest;
};
static rtx noce_emit_store_flag PARAMS ((struct noce_if_info *,
rtx, int, int));
static int noce_try_store_flag PARAMS ((struct noce_if_info *));
static int noce_try_store_flag_inc PARAMS ((struct noce_if_info *));
static int noce_try_store_flag_constants PARAMS ((struct noce_if_info *));
static int noce_try_store_flag_mask PARAMS ((struct noce_if_info *));
static rtx noce_emit_cmove PARAMS ((struct noce_if_info *,
rtx, enum rtx_code, rtx,
rtx, rtx, rtx));
static int noce_try_cmove PARAMS ((struct noce_if_info *));
static int noce_try_cmove_arith PARAMS ((struct noce_if_info *));
static rtx noce_get_alt_condition PARAMS ((struct noce_if_info *,
rtx, rtx *));
static int noce_try_minmax PARAMS ((struct noce_if_info *));
static int noce_try_abs PARAMS ((struct noce_if_info *));
static rtx
noce_emit_store_flag (if_info, x, reversep, normalize)
struct noce_if_info *if_info;
rtx x;
int reversep, normalize;
{
rtx cond = if_info->cond;
int cond_complex;
enum rtx_code code;
cond_complex = (! general_operand (XEXP (cond, 0), VOIDmode)
|| ! general_operand (XEXP (cond, 1), VOIDmode));
if (cond_complex)
cond = XEXP (SET_SRC (pc_set (if_info->jump)), 0);
if (reversep)
code = reversed_comparison_code (cond, if_info->jump);
else
code = GET_CODE (cond);
if ((if_info->cond_earliest == if_info->jump || cond_complex)
&& (normalize == 0 || STORE_FLAG_VALUE == normalize))
{
rtx tmp;
tmp = gen_rtx_fmt_ee (code, GET_MODE (x), XEXP (cond, 0),
XEXP (cond, 1));
tmp = gen_rtx_SET (VOIDmode, x, tmp);
start_sequence ();
tmp = emit_insn (tmp);
if (recog_memoized (tmp) >= 0)
{
tmp = get_insns ();
end_sequence ();
emit_insn (tmp);
if_info->cond_earliest = if_info->jump;
return x;
}
end_sequence ();
}
if (cond_complex || !SCALAR_INT_MODE_P (GET_MODE (x)))
return NULL_RTX;
return emit_store_flag (x, code, XEXP (cond, 0),
XEXP (cond, 1), VOIDmode,
(code == LTU || code == LEU
|| code == GEU || code == GTU), normalize);
}
static void
noce_emit_move_insn (x, y)
rtx x, y;
{
enum machine_mode outmode, inmode;
rtx outer, inner;
int bitpos;
if (GET_CODE (x) != STRICT_LOW_PART)
{
emit_move_insn (x, y);
return;
}
outer = XEXP (x, 0);
inner = XEXP (outer, 0);
outmode = GET_MODE (outer);
inmode = GET_MODE (inner);
bitpos = SUBREG_BYTE (outer) * BITS_PER_UNIT;
store_bit_field (inner, GET_MODE_BITSIZE (outmode), bitpos, outmode, y,
GET_MODE_BITSIZE (inmode));
}
static int
noce_try_store_flag (if_info)
struct noce_if_info *if_info;
{
int reversep;
rtx target, seq;
if (GET_CODE (if_info->b) == CONST_INT
&& INTVAL (if_info->b) == STORE_FLAG_VALUE
&& if_info->a == const0_rtx)
reversep = 0;
else if (if_info->b == const0_rtx
&& GET_CODE (if_info->a) == CONST_INT
&& INTVAL (if_info->a) == STORE_FLAG_VALUE
&& (reversed_comparison_code (if_info->cond, if_info->jump)
!= UNKNOWN))
reversep = 1;
else
return FALSE;
start_sequence ();
target = noce_emit_store_flag (if_info, if_info->x, reversep, 0);
if (target)
{
if (target != if_info->x)
noce_emit_move_insn (if_info->x, target);
seq = get_insns ();
end_sequence ();
emit_insn_before_scope (seq, if_info->jump, INSN_SCOPE (if_info->insn_a));
return TRUE;
}
else
{
end_sequence ();
return FALSE;
}
}
static int
noce_try_store_flag_constants (if_info)
struct noce_if_info *if_info;
{
rtx target, seq;
int reversep;
HOST_WIDE_INT itrue, ifalse, diff, tmp;
int normalize, can_reverse;
enum machine_mode mode;
if (! no_new_pseudos
&& GET_CODE (if_info->a) == CONST_INT
&& GET_CODE (if_info->b) == CONST_INT)
{
mode = GET_MODE (if_info->x);
ifalse = INTVAL (if_info->a);
itrue = INTVAL (if_info->b);
if ((itrue - ifalse > 0)
!= ((ifalse < 0) != (itrue < 0) ? ifalse < 0 : ifalse < itrue))
return FALSE;
diff = trunc_int_for_mode (itrue - ifalse, mode);
can_reverse = (reversed_comparison_code (if_info->cond, if_info->jump)
!= UNKNOWN);
reversep = 0;
if (diff == STORE_FLAG_VALUE || diff == -STORE_FLAG_VALUE)
normalize = 0;
else if (ifalse == 0 && exact_log2 (itrue) >= 0
&& (STORE_FLAG_VALUE == 1
|| BRANCH_COST >= 2))
normalize = 1;
else if (itrue == 0 && exact_log2 (ifalse) >= 0 && can_reverse
&& (STORE_FLAG_VALUE == 1 || BRANCH_COST >= 2))
normalize = 1, reversep = 1;
else if (itrue == -1
&& (STORE_FLAG_VALUE == -1
|| BRANCH_COST >= 2))
normalize = -1;
else if (ifalse == -1 && can_reverse
&& (STORE_FLAG_VALUE == -1 || BRANCH_COST >= 2))
normalize = -1, reversep = 1;
else if ((BRANCH_COST >= 2 && STORE_FLAG_VALUE == -1)
|| BRANCH_COST >= 3)
normalize = -1;
else
return FALSE;
if (reversep)
{
tmp = itrue; itrue = ifalse; ifalse = tmp;
diff = trunc_int_for_mode (-diff, mode);
}
start_sequence ();
target = noce_emit_store_flag (if_info, if_info->x, reversep, normalize);
if (! target)
{
end_sequence ();
return FALSE;
}
if (diff == STORE_FLAG_VALUE || diff == -STORE_FLAG_VALUE)
{
target = expand_simple_binop (mode,
(diff == STORE_FLAG_VALUE
? PLUS : MINUS),
GEN_INT (ifalse), target, if_info->x, 0,
OPTAB_WIDEN);
}
else if (ifalse == 0 && (tmp = exact_log2 (itrue)) >= 0)
{
target = expand_simple_binop (mode, ASHIFT,
target, GEN_INT (tmp), if_info->x, 0,
OPTAB_WIDEN);
}
else if (itrue == -1)
{
target = expand_simple_binop (mode, IOR,
target, GEN_INT (ifalse), if_info->x, 0,
OPTAB_WIDEN);
}
else
{
target = expand_simple_binop (mode, AND,
target, GEN_INT (diff), if_info->x, 0,
OPTAB_WIDEN);
if (target)
target = expand_simple_binop (mode, PLUS,
target, GEN_INT (ifalse),
if_info->x, 0, 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_insn_before_scope (seq, if_info->jump, INSN_SCOPE (if_info->insn_a));
return TRUE;
}
return FALSE;
}
static int
noce_try_store_flag_inc (if_info)
struct noce_if_info *if_info;
{
rtx target, seq;
int subtract, normalize;
if (! no_new_pseudos
&& (BRANCH_COST >= 2
|| HAVE_incscc
|| HAVE_decscc)
&& if_info->b == if_info->x
&& GET_CODE (if_info->a) == PLUS
&& (XEXP (if_info->a, 1) == const1_rtx
|| XEXP (if_info->a, 1) == constm1_rtx)
&& rtx_equal_p (XEXP (if_info->a, 0), if_info->x)
&& (reversed_comparison_code (if_info->cond, if_info->jump)
!= UNKNOWN))
{
if (STORE_FLAG_VALUE == INTVAL (XEXP (if_info->a, 1)))
subtract = 0, normalize = 0;
else if (-STORE_FLAG_VALUE == INTVAL (XEXP (if_info->a, 1)))
subtract = 1, normalize = 0;
else
subtract = 0, normalize = INTVAL (XEXP (if_info->a, 1));
start_sequence ();
target = noce_emit_store_flag (if_info,
gen_reg_rtx (GET_MODE (if_info->x)),
1, normalize);
if (target)
target = expand_simple_binop (GET_MODE (if_info->x),
subtract ? MINUS : PLUS,
if_info->x, target, if_info->x,
0, OPTAB_WIDEN);
if (target)
{
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_insn_before_scope (seq, if_info->jump,
INSN_SCOPE (if_info->insn_a));
return TRUE;
}
end_sequence ();
}
return FALSE;
}
static int
noce_try_store_flag_mask (if_info)
struct noce_if_info *if_info;
{
rtx target, seq;
int reversep;
reversep = 0;
if (! no_new_pseudos
&& (BRANCH_COST >= 2
|| STORE_FLAG_VALUE == -1)
&& ((if_info->a == const0_rtx
&& rtx_equal_p (if_info->b, if_info->x))
|| ((reversep = (reversed_comparison_code (if_info->cond,
if_info->jump)
!= UNKNOWN))
&& if_info->b == const0_rtx
&& rtx_equal_p (if_info->a, if_info->x))))
{
start_sequence ();
target = noce_emit_store_flag (if_info,
gen_reg_rtx (GET_MODE (if_info->x)),
reversep, -1);
if (target)
target = expand_simple_binop (GET_MODE (if_info->x), AND,
if_info->x, target, if_info->x, 0,
OPTAB_WIDEN);
if (target)
{
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_insn_before_scope (seq, if_info->jump,
INSN_SCOPE (if_info->insn_a));
return TRUE;
}
end_sequence ();
}
return FALSE;
}
static rtx
noce_emit_cmove (if_info, x, code, cmp_a, cmp_b, vfalse, vtrue)
struct noce_if_info *if_info;
rtx x, cmp_a, cmp_b, vfalse, vtrue;
enum rtx_code code;
{
if (if_info->cond_earliest == if_info->jump)
{
rtx tmp;
tmp = gen_rtx_fmt_ee (code, GET_MODE (if_info->cond), cmp_a, cmp_b);
tmp = gen_rtx_IF_THEN_ELSE (GET_MODE (x), tmp, vtrue, vfalse);
tmp = gen_rtx_SET (VOIDmode, x, tmp);
start_sequence ();
tmp = emit_insn (tmp);
if (recog_memoized (tmp) >= 0)
{
tmp = get_insns ();
end_sequence ();
emit_insn (tmp);
return x;
}
end_sequence ();
}
if (! general_operand (cmp_a, GET_MODE (cmp_a))
|| ! general_operand (cmp_b, GET_MODE (cmp_b)))
return NULL_RTX;
#if HAVE_conditional_move
return emit_conditional_move (x, code, cmp_a, cmp_b, VOIDmode,
vtrue, vfalse, GET_MODE (x),
(code == LTU || code == GEU
|| code == LEU || code == GTU));
#else
return NULL_RTX;
#endif
}
static int
noce_try_cmove (if_info)
struct noce_if_info *if_info;
{
enum rtx_code code;
rtx target, seq;
if ((CONSTANT_P (if_info->a) || register_operand (if_info->a, VOIDmode))
&& (CONSTANT_P (if_info->b) || register_operand (if_info->b, VOIDmode)))
{
start_sequence ();
code = GET_CODE (if_info->cond);
target = noce_emit_cmove (if_info, if_info->x, code,
XEXP (if_info->cond, 0),
XEXP (if_info->cond, 1),
if_info->a, if_info->b);
if (target)
{
if (target != if_info->x)
noce_emit_move_insn (if_info->x, target);
seq = get_insns ();
end_sequence ();
emit_insn_before_scope (seq, if_info->jump,
INSN_SCOPE (if_info->insn_a));
return TRUE;
}
else
{
end_sequence ();
return FALSE;
}
}
return FALSE;
}
static int
noce_try_cmove_arith (if_info)
struct noce_if_info *if_info;
{
rtx a = if_info->a;
rtx b = if_info->b;
rtx x = if_info->x;
rtx insn_a, insn_b;
rtx tmp, target;
int is_mem = 0;
enum rtx_code code;
if (! no_new_pseudos && cse_not_expected
&& GET_CODE (a) == MEM && GET_CODE (b) == MEM
&& BRANCH_COST >= 5)
{
a = XEXP (a, 0);
b = XEXP (b, 0);
x = gen_reg_rtx (Pmode);
is_mem = 1;
}
else if (may_trap_p (a) || may_trap_p (b))
return FALSE;
code = GET_CODE (if_info->cond);
insn_a = if_info->insn_a;
insn_b = if_info->insn_b;
if (reversed_comparison_code (if_info->cond, if_info->jump) != UNKNOWN)
{
int reversep = 0;
if (rtx_equal_p (b, x))
reversep = 1;
else if (general_operand (b, GET_MODE (b)))
reversep = 1;
if (reversep)
{
code = reversed_comparison_code (if_info->cond, if_info->jump);
tmp = a, a = b, b = tmp;
tmp = insn_a, insn_a = insn_b, insn_b = tmp;
}
}
start_sequence ();
if (! general_operand (a, GET_MODE (a)))
{
rtx set;
if (no_new_pseudos)
goto end_seq_and_fail;
if (is_mem)
{
tmp = gen_reg_rtx (GET_MODE (a));
tmp = emit_insn (gen_rtx_SET (VOIDmode, tmp, a));
}
else if (! insn_a)
goto end_seq_and_fail;
else
{
a = gen_reg_rtx (GET_MODE (a));
tmp = copy_rtx (insn_a);
set = single_set (tmp);
SET_DEST (set) = a;
tmp = emit_insn (PATTERN (tmp));
}
if (recog_memoized (tmp) < 0)
goto end_seq_and_fail;
}
if (! general_operand (b, GET_MODE (b)))
{
rtx set;
if (no_new_pseudos)
goto end_seq_and_fail;
if (is_mem)
{
tmp = gen_reg_rtx (GET_MODE (b));
tmp = emit_insn (gen_rtx_SET (VOIDmode, tmp, b));
}
else if (! insn_b
#if 0
|| modified_between_p (SET_SRC (single_set (insn_b)),
if_info->cond_earliest,
NEXT_INSN (if_info->jump)))
#endif
)
goto end_seq_and_fail;
else
{
b = gen_reg_rtx (GET_MODE (b));
tmp = copy_rtx (insn_b);
set = single_set (tmp);
SET_DEST (set) = b;
tmp = emit_insn (PATTERN (tmp));
}
if (recog_memoized (tmp) < 0)
goto end_seq_and_fail;
}
target = noce_emit_cmove (if_info, x, code, XEXP (if_info->cond, 0),
XEXP (if_info->cond, 1), a, b);
if (! target)
goto end_seq_and_fail;
if (is_mem)
{
tmp = gen_rtx_MEM (GET_MODE (if_info->x), target);
if (MEM_VOLATILE_P (if_info->a) || MEM_VOLATILE_P (if_info->b))
MEM_VOLATILE_P (tmp) = 1;
if (MEM_IN_STRUCT_P (if_info->a) && MEM_IN_STRUCT_P (if_info->b))
MEM_IN_STRUCT_P (tmp) = 1;
if (MEM_SCALAR_P (if_info->a) && MEM_SCALAR_P (if_info->b))
MEM_SCALAR_P (tmp) = 1;
if (MEM_ALIAS_SET (if_info->a) == MEM_ALIAS_SET (if_info->b))
set_mem_alias_set (tmp, MEM_ALIAS_SET (if_info->a));
set_mem_align (tmp,
MIN (MEM_ALIGN (if_info->a), MEM_ALIGN (if_info->b)));
noce_emit_move_insn (if_info->x, tmp);
}
else if (target != x)
noce_emit_move_insn (x, target);
tmp = get_insns ();
end_sequence ();
emit_insn_before_scope (tmp, if_info->jump, INSN_SCOPE (if_info->insn_a));
return TRUE;
end_seq_and_fail:
end_sequence ();
return FALSE;
}
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 (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 (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;
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);
}
}
}
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 (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;
for (insn = if_info->jump; insn != *earliest; insn = PREV_INSN (insn))
if (INSN_P (insn) && reg_overlap_mentioned_p (if_info->x, PATTERN (insn)))
return NULL;
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;
}
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;
if (no_new_pseudos)
return FALSE;
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;
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;
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_insn_before_scope (seq, if_info->jump, INSN_SCOPE (if_info->insn_a));
if_info->cond = cond;
if_info->cond_earliest = earliest;
return TRUE;
}
static int
noce_try_abs (if_info)
struct noce_if_info *if_info;
{
rtx cond, earliest, target, seq, a, b, c;
int negate;
if (no_new_pseudos)
return FALSE;
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;
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;
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));
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;
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);
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_insn_before_scope (seq, if_info->jump, INSN_SCOPE (if_info->insn_a));
if_info->cond = cond;
if_info->cond_earliest = earliest;
return TRUE;
}
static rtx
noce_get_condition (jump, earliest)
rtx jump;
rtx *earliest;
{
rtx cond, set, tmp, insn;
bool reverse;
if (! any_condjump_p (jump))
return NULL_RTX;
set = pc_set (jump);
reverse = (GET_CODE (XEXP (SET_SRC (set), 2)) == LABEL_REF
&& XEXP (XEXP (SET_SRC (set), 2), 0) == JUMP_LABEL (jump));
cond = XEXP (SET_SRC (set), 0);
tmp = XEXP (cond, 0);
if (REG_P (tmp) && GET_MODE_CLASS (GET_MODE (tmp)) == MODE_INT)
{
*earliest = jump;
if (reverse)
cond = gen_rtx_fmt_ee (reverse_condition (GET_CODE (cond)),
GET_MODE (cond), tmp, XEXP (cond, 1));
return cond;
}
tmp = canonicalize_condition (jump, cond, reverse, earliest, NULL_RTX);
if (!tmp)
return NULL_RTX;
for (insn = *earliest; insn != jump; insn = NEXT_INSN (insn))
if (INSN_P (insn) && modified_in_p (tmp, insn))
break;
if (insn == jump)
return tmp;
tmp = XEXP (tmp, 0);
if (!REG_P (tmp) || GET_MODE_CLASS (GET_MODE (tmp)) != MODE_INT)
return NULL_RTX;
tmp = canonicalize_condition (jump, cond, reverse, earliest, tmp);
if (!tmp)
return NULL_RTX;
for (insn = *earliest; insn != jump; insn = NEXT_INSN (insn))
if (INSN_P (insn) && modified_in_p (tmp, insn))
return NULL_RTX;
return tmp;
}
static int
noce_operand_ok (op)
rtx op;
{
if (GET_CODE (op) == MEM)
return ! side_effects_p (XEXP (op, 0));
if (side_effects_p (op))
return FALSE;
return ! may_trap_p (op);
}
static int
noce_process_if_block (ce_info)
struct ce_if_block * ce_info;
{
basic_block test_bb = ce_info->test_bb;
basic_block then_bb = ce_info->then_bb;
basic_block else_bb = ce_info->else_bb;
struct noce_if_info if_info;
rtx insn_a, insn_b;
rtx set_a, set_b;
rtx orig_x, x, a, b;
rtx jump, cond;
if (ce_info->num_multiple_test_blocks)
{
if (else_bb || ! ce_info->and_and_p)
return FALSE;
ce_info->test_bb = test_bb = ce_info->last_test_bb;
ce_info->num_multiple_test_blocks = 0;
ce_info->num_and_and_blocks = 0;
ce_info->num_or_or_blocks = 0;
}
jump = test_bb->end;
cond = noce_get_condition (jump, &if_info.cond_earliest);
if (! cond)
return FALSE;
if (! onlyjump_p (jump))
return FALSE;
if (GET_MODE (XEXP (cond, 0)) == BLKmode)
return FALSE;
insn_a = first_active_insn (then_bb);
if (! insn_a
|| insn_a != last_active_insn (then_bb, FALSE)
|| (set_a = single_set (insn_a)) == NULL_RTX)
return FALSE;
x = SET_DEST (set_a);
a = SET_SRC (set_a);
set_b = NULL_RTX;
if (else_bb)
{
insn_b = first_active_insn (else_bb);
if (! insn_b
|| insn_b != last_active_insn (else_bb, FALSE)
|| (set_b = single_set (insn_b)) == NULL_RTX
|| ! rtx_equal_p (x, SET_DEST (set_b)))
return FALSE;
}
else
{
insn_b = prev_nonnote_insn (if_info.cond_earliest);
if (! insn_b
|| GET_CODE (insn_b) != INSN
|| (set_b = single_set (insn_b)) == NULL_RTX
|| ! rtx_equal_p (x, SET_DEST (set_b))
|| reg_overlap_mentioned_p (x, SET_SRC (set_b))
|| modified_between_p (SET_SRC (set_b),
PREV_INSN (if_info.cond_earliest), jump)
|| reg_overlap_mentioned_p (x, cond)
|| reg_overlap_mentioned_p (x, a)
|| modified_between_p (x, PREV_INSN (if_info.cond_earliest), jump))
insn_b = set_b = NULL_RTX;
}
b = (set_b ? SET_SRC (set_b) : x);
orig_x = x;
if (GET_CODE (x) != REG
|| (SMALL_REGISTER_CLASSES
&& REGNO (x) < FIRST_PSEUDO_REGISTER))
{
if (no_new_pseudos)
return FALSE;
x = gen_reg_rtx (GET_MODE (GET_CODE (x) == STRICT_LOW_PART
? XEXP (x, 0) : x));
}
if (! noce_operand_ok (a) || ! noce_operand_ok (b))
return FALSE;
if_info.test_bb = test_bb;
if_info.cond = cond;
if_info.jump = jump;
if_info.insn_a = insn_a;
if_info.insn_b = insn_b;
if_info.x = x;
if_info.a = a;
if_info.b = b;
if (rtx_equal_p (a, b))
{
if (insn_b && else_bb)
{
rtx note;
if (else_bb && insn_b == else_bb->end)
else_bb->end = PREV_INSN (insn_b);
reorder_insns (insn_b, insn_b, PREV_INSN (jump));
if ((note = find_reg_note (insn_b, REG_EQUAL, NULL_RTX)) != 0)
remove_note (insn_b, note);
insn_b = NULL_RTX;
}
else if (insn_b && side_effects_p (orig_x))
return FALSE;
x = orig_x;
goto success;
}
if (noce_try_store_flag (&if_info))
goto success;
if (noce_try_minmax (&if_info))
goto success;
if (noce_try_abs (&if_info))
goto success;
if (HAVE_conditional_move
&& noce_try_cmove (&if_info))
goto success;
if (! HAVE_conditional_execution)
{
if (noce_try_store_flag_constants (&if_info))
goto success;
if (noce_try_store_flag_inc (&if_info))
goto success;
if (noce_try_store_flag_mask (&if_info))
goto success;
if (HAVE_conditional_move
&& noce_try_cmove_arith (&if_info))
goto success;
}
return FALSE;
success:
delete_insn (insn_a);
if (insn_b && else_bb)
delete_insn (insn_b);
delete_insn (jump);
if (orig_x != x)
{
start_sequence ();
noce_emit_move_insn (copy_rtx (orig_x), x);
insn_b = get_insns ();
end_sequence ();
emit_insn_after_scope (insn_b, test_bb->end, INSN_SCOPE (insn_a));
}
merge_if_block (ce_info);
return TRUE;
}
static int
process_if_block (ce_info)
struct ce_if_block * ce_info;
{
if (! reload_completed
&& noce_process_if_block (ce_info))
return TRUE;
if (HAVE_conditional_execution && reload_completed)
{
if (cond_exec_process_if_block (ce_info, TRUE))
return TRUE;
if (ce_info->num_multiple_test_blocks)
{
cancel_changes (0);
if (cond_exec_process_if_block (ce_info, FALSE))
return TRUE;
}
}
return FALSE;
}
static void
merge_if_block (ce_info)
struct ce_if_block * ce_info;
{
basic_block test_bb = ce_info->test_bb;
basic_block then_bb = ce_info->then_bb;
basic_block else_bb = ce_info->else_bb;
basic_block join_bb = ce_info->join_bb;
basic_block combo_bb;
combo_bb = test_bb;
if (ce_info->num_multiple_test_blocks > 0)
{
basic_block bb = test_bb;
basic_block last_test_bb = ce_info->last_test_bb;
basic_block fallthru = block_fallthru (bb);
do
{
bb = fallthru;
fallthru = block_fallthru (bb);
if (post_dominators)
delete_from_dominance_info (post_dominators, bb);
merge_blocks_nomove (combo_bb, bb);
num_removed_blocks++;
}
while (bb != last_test_bb);
}
if (then_bb)
{
if (combo_bb->global_live_at_end)
COPY_REG_SET (combo_bb->global_live_at_end,
then_bb->global_live_at_end);
if (post_dominators)
delete_from_dominance_info (post_dominators, then_bb);
merge_blocks_nomove (combo_bb, then_bb);
num_removed_blocks++;
}
if (else_bb)
{
if (post_dominators)
delete_from_dominance_info (post_dominators, else_bb);
merge_blocks_nomove (combo_bb, else_bb);
num_removed_blocks++;
}
if (! join_bb)
{
rtx last = combo_bb->end;
if (combo_bb->succ == NULL_EDGE)
{
if (find_reg_note (last, REG_NORETURN, NULL))
;
else if (GET_CODE (last) == INSN
&& GET_CODE (PATTERN (last)) == TRAP_IF
&& TRAP_CONDITION (PATTERN (last)) == const_true_rtx)
;
else
abort ();
}
else if (GET_CODE (last) == JUMP_INSN)
;
else if (combo_bb->succ->dest == EXIT_BLOCK_PTR
&& GET_CODE (last) == CALL_INSN
&& SIBLING_CALL_P (last))
;
else if ((combo_bb->succ->flags & EDGE_EH)
&& can_throw_internal (last))
;
else
abort ();
}
else if ((join_bb->pred == NULL
|| join_bb->pred->pred_next == NULL)
&& join_bb != EXIT_BLOCK_PTR)
{
if (combo_bb->global_live_at_end)
COPY_REG_SET (combo_bb->global_live_at_end,
join_bb->global_live_at_end);
if (post_dominators)
delete_from_dominance_info (post_dominators, join_bb);
merge_blocks_nomove (combo_bb, join_bb);
num_removed_blocks++;
}
else
{
if (combo_bb->succ->succ_next != NULL_EDGE
|| combo_bb->succ->dest != join_bb)
abort ();
if (join_bb != EXIT_BLOCK_PTR)
tidy_fallthru_edge (combo_bb->succ, combo_bb, join_bb);
}
num_updated_if_blocks++;
}
static basic_block
find_if_header (test_bb, pass)
basic_block test_bb;
int pass;
{
ce_if_block_t ce_info;
edge then_edge;
edge else_edge;
if ((then_edge = test_bb->succ) == NULL_EDGE
|| (else_edge = then_edge->succ_next) == NULL_EDGE
|| else_edge->succ_next != NULL_EDGE)
return NULL;
if ((then_edge->flags & EDGE_COMPLEX)
|| (else_edge->flags & EDGE_COMPLEX))
return NULL;
if (then_edge->flags & EDGE_FALLTHRU)
;
else if (else_edge->flags & EDGE_FALLTHRU)
{
edge e = else_edge;
else_edge = then_edge;
then_edge = e;
}
else
return NULL;
memset ((PTR) &ce_info, '\0', sizeof (ce_info));
ce_info.test_bb = test_bb;
ce_info.then_bb = then_edge->dest;
ce_info.else_bb = else_edge->dest;
ce_info.pass = pass;
#ifdef IFCVT_INIT_EXTRA_FIELDS
IFCVT_INIT_EXTRA_FIELDS (&ce_info);
#endif
if (find_if_block (&ce_info))
goto success;
if (HAVE_trap && HAVE_conditional_trap
&& find_cond_trap (test_bb, then_edge, else_edge))
goto success;
if (post_dominators
&& (! HAVE_conditional_execution || reload_completed))
{
if (find_if_case_1 (test_bb, then_edge, else_edge))
goto success;
if (find_if_case_2 (test_bb, then_edge, else_edge))
goto success;
}
return NULL;
success:
if (rtl_dump_file)
fprintf (rtl_dump_file, "Conversion succeeded on pass %d.\n", pass);
return ce_info.test_bb;
}
static int
block_jumps_and_fallthru_p (cur_bb, target_bb)
basic_block cur_bb;
basic_block target_bb;
{
edge cur_edge;
int fallthru_p = FALSE;
int jump_p = FALSE;
rtx insn;
rtx end;
int n_insns = 0;
if (!cur_bb || !target_bb)
return -1;
if (cur_bb->succ == NULL_EDGE)
return FALSE;
for (cur_edge = cur_bb->succ;
cur_edge != NULL_EDGE;
cur_edge = cur_edge->succ_next)
{
if (cur_edge->flags & EDGE_COMPLEX)
return -1;
else if (cur_edge->flags & EDGE_FALLTHRU)
fallthru_p = TRUE;
else if (cur_edge->dest == target_bb)
jump_p = TRUE;
else
return -1;
}
if ((jump_p & fallthru_p) == 0)
return -1;
end = cur_bb->end;
insn = cur_bb->head;
while (insn != NULL_RTX)
{
if (GET_CODE (insn) == CALL_INSN)
return -1;
if (INSN_P (insn)
&& GET_CODE (insn) != JUMP_INSN
&& GET_CODE (PATTERN (insn)) != USE
&& GET_CODE (PATTERN (insn)) != CLOBBER)
n_insns++;
if (insn == end)
break;
insn = NEXT_INSN (insn);
}
return n_insns;
}
static int
find_if_block (ce_info)
struct ce_if_block * ce_info;
{
basic_block test_bb = ce_info->test_bb;
basic_block then_bb = ce_info->then_bb;
basic_block else_bb = ce_info->else_bb;
basic_block join_bb = NULL_BLOCK;
edge then_succ = then_bb->succ;
edge else_succ = else_bb->succ;
int then_predecessors;
int else_predecessors;
edge cur_edge;
basic_block next;
ce_info->last_test_bb = test_bb;
if (HAVE_conditional_execution && reload_completed
&& test_bb->pred != NULL_EDGE
&& test_bb->pred->pred_next == NULL_EDGE
&& test_bb->pred->flags == EDGE_FALLTHRU)
{
basic_block bb = test_bb->pred->src;
basic_block target_bb;
int max_insns = MAX_CONDITIONAL_EXECUTE;
int n_insns;
if ((n_insns = block_jumps_and_fallthru_p (bb, else_bb)) >= 0)
{
ce_info->and_and_p = TRUE;
target_bb = else_bb;
}
else if ((n_insns = block_jumps_and_fallthru_p (bb, then_bb)) >= 0)
{
ce_info->and_and_p = FALSE;
target_bb = then_bb;
}
else
target_bb = NULL_BLOCK;
if (target_bb && n_insns <= max_insns)
{
int total_insns = 0;
int blocks = 0;
ce_info->last_test_bb = test_bb;
do
{
ce_info->test_bb = test_bb = bb;
total_insns += n_insns;
blocks++;
if (bb->pred == NULL_EDGE || bb->pred->pred_next != NULL_EDGE)
break;
bb = bb->pred->src;
n_insns = block_jumps_and_fallthru_p (bb, target_bb);
}
while (n_insns >= 0 && (total_insns + n_insns) <= max_insns);
ce_info->num_multiple_test_blocks = blocks;
ce_info->num_multiple_test_insns = total_insns;
if (ce_info->and_and_p)
ce_info->num_and_and_blocks = blocks;
else
ce_info->num_or_or_blocks = blocks;
}
}
then_predecessors = 0;
for (cur_edge = then_bb->pred;
cur_edge != NULL_EDGE;
cur_edge = cur_edge->pred_next)
{
then_predecessors++;
if (cur_edge->flags & EDGE_COMPLEX)
return FALSE;
}
else_predecessors = 0;
for (cur_edge = else_bb->pred;
cur_edge != NULL_EDGE;
cur_edge = cur_edge->pred_next)
{
else_predecessors++;
if (cur_edge->flags & EDGE_COMPLEX)
return FALSE;
}
if ((then_predecessors - ce_info->num_or_or_blocks) != 1)
return FALSE;
if (then_succ != NULL_EDGE
&& (then_succ->succ_next != NULL_EDGE
|| (then_succ->flags & EDGE_COMPLEX)))
return FALSE;
if (then_succ == NULL)
{
if (else_bb->pred->pred_next == NULL_EDGE)
{
rtx last_insn = then_bb->end;
while (last_insn
&& GET_CODE (last_insn) == NOTE
&& last_insn != then_bb->head)
last_insn = PREV_INSN (last_insn);
if (last_insn
&& GET_CODE (last_insn) == JUMP_INSN
&& ! simplejump_p (last_insn))
return FALSE;
join_bb = else_bb;
else_bb = NULL_BLOCK;
}
else
return FALSE;
}
else if (then_succ->dest == else_bb)
{
join_bb = else_bb;
else_bb = NULL_BLOCK;
}
else if (else_succ != NULL_EDGE
&& then_succ->dest == else_succ->dest
&& else_bb->pred->pred_next == NULL_EDGE
&& else_succ->succ_next == NULL_EDGE
&& ! (else_succ->flags & EDGE_COMPLEX))
join_bb = else_succ->dest;
else
return FALSE;
num_possible_if_blocks++;
if (rtl_dump_file)
{
fprintf (rtl_dump_file, "\nIF-THEN%s block found, pass %d, start block %d [insn %d], then %d [%d]",
(else_bb) ? "-ELSE" : "",
ce_info->pass,
test_bb->index, (test_bb->head) ? (int)INSN_UID (test_bb->head) : -1,
then_bb->index, (then_bb->head) ? (int)INSN_UID (then_bb->head) : -1);
if (else_bb)
fprintf (rtl_dump_file, ", else %d [%d]",
else_bb->index, (else_bb->head) ? (int)INSN_UID (else_bb->head) : -1);
fprintf (rtl_dump_file, ", join %d [%d]",
join_bb->index, (join_bb->head) ? (int)INSN_UID (join_bb->head) : -1);
if (ce_info->num_multiple_test_blocks > 0)
fprintf (rtl_dump_file, ", %d %s block%s last test %d [%d]",
ce_info->num_multiple_test_blocks,
(ce_info->and_and_p) ? "&&" : "||",
(ce_info->num_multiple_test_blocks == 1) ? "" : "s",
ce_info->last_test_bb->index,
((ce_info->last_test_bb->head)
? (int)INSN_UID (ce_info->last_test_bb->head)
: -1));
fputc ('\n', rtl_dump_file);
}
next = then_bb;
if (else_bb && (next = next->next_bb) != else_bb)
return FALSE;
if ((next = next->next_bb) != join_bb && join_bb != EXIT_BLOCK_PTR)
{
if (else_bb)
join_bb = NULL;
else
return FALSE;
}
ce_info->else_bb = else_bb;
ce_info->join_bb = join_bb;
return process_if_block (ce_info);
}
static int
find_cond_trap (test_bb, then_edge, else_edge)
basic_block test_bb;
edge then_edge, else_edge;
{
basic_block then_bb = then_edge->dest;
basic_block else_bb = else_edge->dest;
basic_block other_bb, trap_bb;
rtx trap, jump, cond, cond_earliest, seq;
enum rtx_code code;
if ((trap = block_has_only_trap (then_bb)) != NULL)
trap_bb = then_bb, other_bb = else_bb;
else if ((trap = block_has_only_trap (else_bb)) != NULL)
trap_bb = else_bb, other_bb = then_bb;
else
return FALSE;
if (rtl_dump_file)
{
fprintf (rtl_dump_file, "\nTRAP-IF block found, start %d, trap %d\n",
test_bb->index, trap_bb->index);
}
jump = test_bb->end;
cond = noce_get_condition (jump, &cond_earliest);
if (! cond)
return FALSE;
if (! onlyjump_p (jump))
return FALSE;
if (GET_MODE (XEXP (cond, 0)) == BLKmode)
return FALSE;
code = GET_CODE (cond);
if (then_bb == trap_bb)
{
code = reversed_comparison_code (cond, jump);
if (code == UNKNOWN)
return FALSE;
}
seq = gen_cond_trap (code, XEXP (cond, 0), XEXP (cond, 1),
TRAP_CODE (PATTERN (trap)));
if (seq == NULL)
return FALSE;
emit_insn_before_scope (seq, cond_earliest, INSN_SCOPE (trap));
remove_edge (trap_bb == then_bb ? then_edge : else_edge);
if (trap_bb->pred == NULL)
{
if (post_dominators)
delete_from_dominance_info (post_dominators, trap_bb);
flow_delete_block (trap_bb);
num_removed_blocks++;
}
if (test_bb->next_bb == other_bb)
{
struct ce_if_block new_ce_info;
delete_insn (jump);
memset ((PTR) &new_ce_info, '\0', sizeof (new_ce_info));
new_ce_info.test_bb = test_bb;
new_ce_info.then_bb = NULL;
new_ce_info.else_bb = NULL;
new_ce_info.join_bb = other_bb;
merge_if_block (&new_ce_info);
}
else
{
rtx lab, newjump;
lab = JUMP_LABEL (jump);
newjump = emit_jump_insn_after (gen_jump (lab), jump);
LABEL_NUSES (lab) += 1;
JUMP_LABEL (newjump) = lab;
emit_barrier_after (newjump);
delete_insn (jump);
}
return TRUE;
}
static rtx
block_has_only_trap (bb)
basic_block bb;
{
rtx trap;
if (bb == EXIT_BLOCK_PTR)
return NULL_RTX;
if (bb->succ)
return NULL_RTX;
trap = first_active_insn (bb);
if (! (trap == bb->end
&& GET_CODE (PATTERN (trap)) == TRAP_IF
&& TRAP_CONDITION (PATTERN (trap)) == const_true_rtx))
return NULL_RTX;
return trap;
}
static int
find_if_case_1 (test_bb, then_edge, else_edge)
basic_block test_bb;
edge then_edge, else_edge;
{
basic_block then_bb = then_edge->dest;
basic_block else_bb = else_edge->dest, new_bb;
edge then_succ = then_bb->succ;
int then_bb_index;
if (!then_succ || then_succ->succ_next != NULL)
return FALSE;
if (then_succ->flags & (EDGE_COMPLEX | EDGE_FALLTHRU))
return FALSE;
if (then_bb->pred->pred_next != NULL)
return FALSE;
if (forwarder_block_p (then_bb))
return FALSE;
num_possible_if_blocks++;
if (rtl_dump_file)
fprintf (rtl_dump_file,
"\nIF-CASE-1 found, start %d, then %d\n",
test_bb->index, then_bb->index);
if (count_bb_insns (then_bb) > BRANCH_COST)
return FALSE;
if (! dead_or_predicable (test_bb, then_bb, else_bb,
then_bb->succ->dest, 1))
return FALSE;
bitmap_operation (test_bb->global_live_at_end,
else_bb->global_live_at_start,
then_bb->global_live_at_end, BITMAP_IOR);
new_bb = redirect_edge_and_branch_force (FALLTHRU_EDGE (test_bb), else_bb);
then_bb_index = then_bb->index;
if (post_dominators)
delete_from_dominance_info (post_dominators, then_bb);
flow_delete_block (then_bb);
if (new_bb)
{
new_bb->index = then_bb_index;
BASIC_BLOCK (then_bb_index) = new_bb;
}
num_removed_blocks++;
num_updated_if_blocks++;
return TRUE;
}
static int
find_if_case_2 (test_bb, then_edge, else_edge)
basic_block test_bb;
edge then_edge, else_edge;
{
basic_block then_bb = then_edge->dest;
basic_block else_bb = else_edge->dest;
edge else_succ = else_bb->succ;
rtx note;
if (!else_succ || else_succ->succ_next != NULL)
return FALSE;
if (else_succ->flags & EDGE_COMPLEX)
return FALSE;
if (else_bb->pred->pred_next != NULL)
return FALSE;
if (then_bb->index < 0)
return FALSE;
note = find_reg_note (test_bb->end, REG_BR_PROB, NULL_RTX);
if (note && INTVAL (XEXP (note, 0)) >= REG_BR_PROB_BASE / 2)
;
else if (else_succ->dest->index < 0
|| dominated_by_p (post_dominators, then_bb,
else_succ->dest))
;
else
return FALSE;
num_possible_if_blocks++;
if (rtl_dump_file)
fprintf (rtl_dump_file,
"\nIF-CASE-2 found, start %d, else %d\n",
test_bb->index, else_bb->index);
if (count_bb_insns (else_bb) > BRANCH_COST)
return FALSE;
if (! dead_or_predicable (test_bb, else_bb, then_bb, else_succ->dest, 0))
return FALSE;
bitmap_operation (test_bb->global_live_at_end,
then_bb->global_live_at_start,
else_bb->global_live_at_end, BITMAP_IOR);
if (post_dominators)
delete_from_dominance_info (post_dominators, else_bb);
flow_delete_block (else_bb);
num_removed_blocks++;
num_updated_if_blocks++;
return TRUE;
}
static int
find_memory (px, data)
rtx *px;
void *data ATTRIBUTE_UNUSED;
{
return GET_CODE (*px) == MEM;
}
static int
dead_or_predicable (test_bb, merge_bb, other_bb, new_dest, reversep)
basic_block test_bb, merge_bb, other_bb;
basic_block new_dest;
int reversep;
{
rtx head, end, jump, earliest, old_dest, new_label = NULL_RTX;
jump = test_bb->end;
head = merge_bb->head;
end = merge_bb->end;
if (GET_CODE (head) == CODE_LABEL)
head = NEXT_INSN (head);
if (GET_CODE (head) == NOTE)
{
if (head == end)
{
head = end = NULL_RTX;
goto no_body;
}
head = NEXT_INSN (head);
}
if (GET_CODE (end) == JUMP_INSN)
{
if (head == end)
{
head = end = NULL_RTX;
goto no_body;
}
end = PREV_INSN (end);
}
#ifndef IFCVT_MODIFY_TESTS
if (HAVE_conditional_execution)
{
rtx cond, prob_val;
cond = cond_exec_get_condition (jump);
if (! cond)
return FALSE;
prob_val = find_reg_note (jump, REG_BR_PROB, NULL_RTX);
if (prob_val)
prob_val = XEXP (prob_val, 0);
if (reversep)
{
enum rtx_code rev = reversed_comparison_code (cond, jump);
if (rev == UNKNOWN)
return FALSE;
cond = gen_rtx_fmt_ee (rev, GET_MODE (cond), XEXP (cond, 0),
XEXP (cond, 1));
if (prob_val)
prob_val = GEN_INT (REG_BR_PROB_BASE - INTVAL (prob_val));
}
if (! cond_exec_process_insns ((ce_if_block_t *)0, head, end, cond,
prob_val, 0))
goto cancel;
earliest = jump;
}
else
#endif
{
rtx insn, cond, prev;
regset_head merge_set_head, tmp_head, test_live_head, test_set_head;
regset merge_set, tmp, test_live, test_set;
struct propagate_block_info *pbi;
int i, fail = 0;
for (insn = head; ; insn = NEXT_INSN (insn))
{
if (GET_CODE (insn) == CALL_INSN)
return FALSE;
if (INSN_P (insn))
{
if (may_trap_p (PATTERN (insn)))
return FALSE;
if (for_each_rtx (&PATTERN (insn), find_memory, NULL))
return FALSE;
}
if (insn == end)
break;
}
if (! any_condjump_p (jump))
return FALSE;
cond = noce_get_condition (jump, &earliest);
if (! cond)
return FALSE;
tmp = INITIALIZE_REG_SET (tmp_head);
merge_set = INITIALIZE_REG_SET (merge_set_head);
test_live = INITIALIZE_REG_SET (test_live_head);
test_set = INITIALIZE_REG_SET (test_set_head);
propagate_block (merge_bb, tmp, merge_set, merge_set, 0);
if (SMALL_REGISTER_CLASSES && ! reload_completed)
{
EXECUTE_IF_SET_IN_BITMAP
(merge_set, 0, i,
{
if (i < FIRST_PSEUDO_REGISTER
&& ! fixed_regs[i]
&& ! global_regs[i])
fail = 1;
});
}
COPY_REG_SET (test_live, other_bb->global_live_at_start);
pbi = init_propagate_block_info (test_bb, test_live, test_set, test_set,
0);
for (insn = jump; ; insn = prev)
{
prev = propagate_one_insn (pbi, insn);
if (insn == earliest)
break;
}
free_propagate_block_info (pbi);
bitmap_operation (tmp, test_set, test_live, BITMAP_IOR);
bitmap_operation (tmp, tmp, merge_set, BITMAP_AND);
EXECUTE_IF_SET_IN_BITMAP(tmp, 0, i, fail = 1);
bitmap_operation (tmp, test_set, merge_bb->global_live_at_start,
BITMAP_AND);
EXECUTE_IF_SET_IN_BITMAP(tmp, 0, i, fail = 1);
FREE_REG_SET (tmp);
FREE_REG_SET (merge_set);
FREE_REG_SET (test_live);
FREE_REG_SET (test_set);
if (fail)
return FALSE;
}
no_body:
old_dest = JUMP_LABEL (jump);
if (other_bb != new_dest)
{
new_label = block_label (new_dest);
if (reversep
? ! invert_jump_1 (jump, new_label)
: ! redirect_jump_1 (jump, new_label))
goto cancel;
}
if (! apply_change_group ())
return FALSE;
if (other_bb != new_dest)
{
if (old_dest)
LABEL_NUSES (old_dest) -= 1;
if (new_label)
LABEL_NUSES (new_label) += 1;
JUMP_LABEL (jump) = new_label;
if (reversep)
invert_br_probabilities (jump);
redirect_edge_succ (BRANCH_EDGE (test_bb), new_dest);
if (reversep)
{
gcov_type count, probability;
count = BRANCH_EDGE (test_bb)->count;
BRANCH_EDGE (test_bb)->count = FALLTHRU_EDGE (test_bb)->count;
FALLTHRU_EDGE (test_bb)->count = count;
probability = BRANCH_EDGE (test_bb)->probability;
BRANCH_EDGE (test_bb)->probability
= FALLTHRU_EDGE (test_bb)->probability;
FALLTHRU_EDGE (test_bb)->probability = probability;
update_br_prob_note (test_bb);
}
}
if (head != NULL)
{
if (end == merge_bb->end)
merge_bb->end = PREV_INSN (head);
if (squeeze_notes (&head, &end))
return TRUE;
reorder_insns (head, end, PREV_INSN (earliest));
}
if (other_bb == new_dest)
{
delete_insn (jump);
remove_edge (BRANCH_EDGE (test_bb));
}
return TRUE;
cancel:
cancel_changes (0);
return FALSE;
}
void
if_convert (x_life_data_ok)
int x_life_data_ok;
{
basic_block bb;
int pass;
num_possible_if_blocks = 0;
num_updated_if_blocks = 0;
num_removed_blocks = 0;
life_data_ok = (x_life_data_ok != 0);
free_basic_block_vars (1);
post_dominators = NULL;
if (HAVE_conditional_execution || life_data_ok)
{
post_dominators = calculate_dominance_info (CDI_POST_DOMINATORS);
}
if (life_data_ok)
clear_bb_flags ();
pass = 0;
do
{
cond_exec_changed_p = FALSE;
pass++;
#ifdef IFCVT_MULTIPLE_DUMPS
if (rtl_dump_file && pass > 1)
fprintf (rtl_dump_file, "\n\n========== Pass %d ==========\n", pass);
#endif
FOR_EACH_BB (bb)
{
basic_block new_bb;
while ((new_bb = find_if_header (bb, pass)))
bb = new_bb;
}
#ifdef IFCVT_MULTIPLE_DUMPS
if (rtl_dump_file && cond_exec_changed_p)
print_rtl_with_bb (rtl_dump_file, get_insns ());
#endif
}
while (cond_exec_changed_p);
#ifdef IFCVT_MULTIPLE_DUMPS
if (rtl_dump_file)
fprintf (rtl_dump_file, "\n\n========== no more changes\n");
#endif
if (post_dominators)
free_dominance_info (post_dominators);
if (rtl_dump_file)
fflush (rtl_dump_file);
clear_aux_for_blocks ();
if (num_removed_blocks && life_data_ok)
{
if (max_regno < max_reg_num ())
{
max_regno = max_reg_num ();
allocate_reg_info (max_regno, FALSE, FALSE);
}
update_life_info_in_dirty_blocks (UPDATE_LIFE_GLOBAL_RM_NOTES,
PROP_DEATH_NOTES | PROP_SCAN_DEAD_CODE
| PROP_KILL_DEAD_CODE);
}
if (rtl_dump_file && num_possible_if_blocks > 0)
{
fprintf (rtl_dump_file,
"\n%d possible IF blocks searched.\n",
num_possible_if_blocks);
fprintf (rtl_dump_file,
"%d IF blocks converted.\n",
num_updated_if_blocks);
fprintf (rtl_dump_file,
"%d basic blocks deleted.\n\n\n",
num_removed_blocks);
}
#ifdef ENABLE_CHECKING
verify_flow_info ();
#endif
}