1 /* If-conversion support.
2 Copyright (C) 2000 Free Software Foundation, Inc.
4 This file is part of GNU CC.
6 GNU CC is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2, or (at your option)
11 GNU CC is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with GNU CC; see the file COPYING. If not, write to
18 the Free Software Foundation, 59 Temple Place - Suite 330,
19 Boston, MA 02111-1307, USA. */
28 #include "insn-config.h"
30 #include "hard-reg-set.h"
31 #include "basic-block.h"
37 #ifndef HAVE_conditional_execution
38 #define HAVE_conditional_execution 0
40 #ifndef HAVE_conditional_move
41 #define HAVE_conditional_move 0
50 #ifndef MAX_CONDITIONAL_EXECUTE
51 #define MAX_CONDITIONAL_EXECUTE (BRANCH_COST + 1)
54 #define EDGE_COMPLEX (EDGE_ABNORMAL | EDGE_ABNORMAL_CALL | EDGE_EH)
56 #define NULL_EDGE ((struct edge_def *)NULL)
57 #define NULL_BLOCK ((struct basic_block_def *)NULL)
59 /* # of IF-THEN or IF-THEN-ELSE blocks we looked at */
60 static int num_possible_if_blocks;
62 /* # of IF-THEN or IF-THEN-ELSE blocks were converted to conditional
64 static int num_updated_if_blocks;
66 /* # of basic blocks that were removed. */
67 static int num_removed_blocks;
69 /* The post-dominator relation on the original block numbers. */
70 static sbitmap *post_dominators;
72 /* Forward references. */
73 static int count_bb_insns PARAMS ((basic_block));
74 static rtx first_active_insn PARAMS ((basic_block));
75 static int last_active_insn_p PARAMS ((basic_block, rtx));
76 static int seq_contains_jump PARAMS ((rtx));
78 static int cond_exec_process_insns PARAMS ((rtx, rtx, rtx, rtx, int));
79 static rtx cond_exec_get_condition PARAMS ((rtx));
80 static int cond_exec_process_if_block PARAMS ((basic_block, basic_block,
81 basic_block, basic_block));
83 static rtx noce_get_condition PARAMS ((rtx, rtx *));
84 static int noce_process_if_block PARAMS ((basic_block, basic_block,
85 basic_block, basic_block));
87 static int process_if_block PARAMS ((basic_block, basic_block,
88 basic_block, basic_block));
89 static void merge_if_block PARAMS ((basic_block, basic_block,
90 basic_block, basic_block));
92 static int find_if_header PARAMS ((basic_block));
93 static int find_if_block PARAMS ((basic_block, edge, edge));
94 static int find_if_case_1 PARAMS ((basic_block, edge, edge));
95 static int find_if_case_2 PARAMS ((basic_block, edge, edge));
96 static int find_memory PARAMS ((rtx *, void *));
97 static int dead_or_predicable PARAMS ((basic_block, basic_block,
98 basic_block, rtx, int));
100 /* Abuse the basic_block AUX field to store the original block index,
101 as well as a flag indicating that the block should be rescaned for
104 #define SET_ORIG_INDEX(BB,I) ((BB)->aux = (void *)((size_t)(I) << 1))
105 #define ORIG_INDEX(BB) ((size_t)(BB)->aux >> 1)
106 #define SET_UPDATE_LIFE(BB) ((BB)->aux = (void *)((size_t)(BB)->aux | 1))
107 #define UPDATE_LIFE(BB) ((size_t)(BB)->aux & 1)
110 /* Count the number of non-jump active insns in BB. */
121 if (GET_CODE (insn) == CALL_INSN || GET_CODE (insn) == INSN)
126 insn = NEXT_INSN (insn);
132 /* Return the first non-jump active insn in the basic block. */
135 first_active_insn (bb)
140 if (GET_CODE (insn) == CODE_LABEL)
144 insn = NEXT_INSN (insn);
147 while (GET_CODE (insn) == NOTE)
151 insn = NEXT_INSN (insn);
154 if (GET_CODE (insn) == JUMP_INSN)
160 /* Return true if INSN is the last active non-jump insn in BB. */
163 last_active_insn_p (bb, insn)
171 insn = NEXT_INSN (insn);
173 while (GET_CODE (insn) == NOTE);
175 return GET_CODE (insn) == JUMP_INSN;
178 /* It is possible, especially when having dealt with multi-word
179 arithmetic, for the expanders to have emitted jumps. Search
180 through the sequence and return TRUE if a jump exists so that
181 we can abort the conversion. */
184 seq_contains_jump (insn)
189 if (GET_CODE (insn) == JUMP_INSN)
191 insn = NEXT_INSN (insn);
196 /* Go through a bunch of insns, converting them to conditional
197 execution format if possible. Return TRUE if all of the non-note
198 insns were processed. */
201 cond_exec_process_insns (start, end, test, prob_val, mod_ok)
202 rtx start; /* first insn to look at */
203 rtx end; /* last insn to look at */
204 rtx test; /* conditional execution test */
205 rtx prob_val; /* probability of branch taken. */
206 int mod_ok; /* true if modifications ok last insn. */
208 int must_be_last = FALSE;
211 for (insn = start; ; insn = NEXT_INSN (insn))
213 if (GET_CODE (insn) == NOTE)
216 if (GET_CODE (insn) != INSN && GET_CODE (insn) != CALL_INSN)
219 /* Remove USE and CLOBBER insns that get in the way. */
221 && (GET_CODE (PATTERN (insn)) == USE
222 || GET_CODE (PATTERN (insn)) == CLOBBER))
224 /* ??? Ug. Actually unlinking the thing is problematic,
225 given what we'd have to coordinate with our callers. */
226 PUT_CODE (insn, NOTE);
227 NOTE_LINE_NUMBER (insn) = NOTE_INSN_DELETED;
228 NOTE_SOURCE_FILE (insn) = 0;
232 /* Last insn wasn't last? */
236 if (modified_in_p (test, insn))
243 /* Now build the conditional form of the instruction. */
244 validate_change (insn, &PATTERN (insn),
245 gen_rtx_COND_EXEC (VOIDmode, copy_rtx (test),
248 if (GET_CODE (insn) == CALL_INSN && prob_val)
249 validate_change (insn, ®_NOTES (insn),
250 alloc_EXPR_LIST (REG_BR_PROB, prob_val,
251 REG_NOTES (insn)), 1);
261 /* Return the condition for a jump. Do not do any special processing. */
264 cond_exec_get_condition (jump)
269 if (any_condjump_p (jump))
270 test_if = SET_SRC (pc_set (jump));
273 cond = XEXP (test_if, 0);
275 /* If this branches to JUMP_LABEL when the condition is false,
276 reverse the condition. */
277 if (GET_CODE (XEXP (test_if, 2)) == LABEL_REF
278 && XEXP (XEXP (test_if, 2), 0) == JUMP_LABEL (jump))
279 cond = gen_rtx_fmt_ee (reverse_condition (GET_CODE (cond)),
280 GET_MODE (cond), XEXP (cond, 0),
286 /* Given a simple IF-THEN or IF-THEN-ELSE block, attempt to convert it
287 to conditional execution. Return TRUE if we were successful at
288 converting the the block. */
291 cond_exec_process_if_block (test_bb, then_bb, else_bb, join_bb)
292 basic_block test_bb; /* Basic block test is in */
293 basic_block then_bb; /* Basic block for THEN block */
294 basic_block else_bb; /* Basic block for ELSE block */
295 basic_block join_bb; /* Basic block the join label is in */
297 rtx test_expr; /* expression in IF_THEN_ELSE that is tested */
298 rtx then_start; /* first insn in THEN block */
299 rtx then_end; /* last insn + 1 in THEN block */
300 rtx else_start; /* first insn in ELSE block or NULL */
301 rtx else_end; /* last insn + 1 in ELSE block */
302 int max; /* max # of insns to convert. */
303 int then_mod_ok; /* whether conditional mods are ok in THEN */
304 rtx true_expr; /* test for else block insns */
305 rtx false_expr; /* test for then block insns */
306 rtx true_prob_val; /* probability of else block */
307 rtx false_prob_val; /* probability of then block */
310 /* Find the conditional jump to the ELSE or JOIN part, and isolate
312 test_expr = cond_exec_get_condition (test_bb->end);
316 /* If the conditional jump is more than just a conditional jump,
317 then we can not do conditional execution conversion on this block. */
318 if (!onlyjump_p (test_bb->end))
321 /* Collect the bounds of where we're to search. */
323 then_start = then_bb->head;
324 then_end = then_bb->end;
326 /* Skip a label heading THEN block. */
327 if (GET_CODE (then_start) == CODE_LABEL)
328 then_start = NEXT_INSN (then_start);
330 /* Skip a (use (const_int 0)) or branch as the final insn. */
331 if (GET_CODE (then_end) == INSN
332 && GET_CODE (PATTERN (then_end)) == USE
333 && GET_CODE (XEXP (PATTERN (then_end), 0)) == CONST_INT)
334 then_end = PREV_INSN (then_end);
335 else if (GET_CODE (then_end) == JUMP_INSN)
336 then_end = PREV_INSN (then_end);
340 /* Skip the ELSE block's label. */
341 else_start = NEXT_INSN (else_bb->head);
342 else_end = else_bb->end;
344 /* Skip a (use (const_int 0)) or branch as the final insn. */
345 if (GET_CODE (else_end) == INSN
346 && GET_CODE (PATTERN (else_end)) == USE
347 && GET_CODE (XEXP (PATTERN (else_end), 0)) == CONST_INT)
348 else_end = PREV_INSN (else_end);
349 else if (GET_CODE (else_end) == JUMP_INSN)
350 else_end = PREV_INSN (else_end);
353 /* How many instructions should we convert in total? */
357 max = 2 * MAX_CONDITIONAL_EXECUTE;
358 n_insns = count_bb_insns (else_bb);
361 max = MAX_CONDITIONAL_EXECUTE;
362 n_insns += count_bb_insns (then_bb);
366 /* Map test_expr/test_jump into the appropriate MD tests to use on
367 the conditionally executed code. */
369 true_expr = test_expr;
370 false_expr = gen_rtx_fmt_ee (reverse_condition (GET_CODE (true_expr)),
371 GET_MODE (true_expr), XEXP (true_expr, 0),
372 XEXP (true_expr, 1));
374 true_prob_val = find_reg_note (test_bb->end, REG_BR_PROB, NULL_RTX);
377 true_prob_val = XEXP (true_prob_val, 0);
378 false_prob_val = GEN_INT (REG_BR_PROB_BASE - INTVAL (true_prob_val));
381 false_prob_val = NULL_RTX;
383 /* For IF-THEN-ELSE blocks, we don't allow modifications of the test
384 on then THEN block. */
385 then_mod_ok = (else_bb == NULL_BLOCK);
387 /* Go through the THEN and ELSE blocks converting the insns if possible
388 to conditional execution. */
391 && ! cond_exec_process_insns (then_start, then_end,
392 false_expr, false_prob_val, then_mod_ok))
396 && ! cond_exec_process_insns (else_start, else_end,
397 true_expr, true_prob_val, TRUE))
400 if (! apply_change_group ())
403 /* Conversion succeeded. */
405 fprintf (rtl_dump_file, "%d insn%s converted to conditional execution.\n",
406 n_insns, (n_insns == 1) ? " was" : "s were");
408 /* Merge the blocks! */
409 merge_if_block (test_bb, then_bb, else_bb, join_bb);
417 /* Used by noce_process_if_block to communicate with its subroutines.
419 The subroutines know that A and B may be evaluated freely. They
420 know that X is a register. They should insert new instructions
421 before cond_earliest. */
427 rtx jump, cond, cond_earliest;
430 static rtx noce_emit_store_flag PARAMS ((struct noce_if_info *,
432 static int noce_try_store_flag PARAMS ((struct noce_if_info *));
433 static int noce_try_store_flag_inc PARAMS ((struct noce_if_info *));
434 static int noce_try_store_flag_constants PARAMS ((struct noce_if_info *));
435 static int noce_try_store_flag_mask PARAMS ((struct noce_if_info *));
436 static rtx noce_emit_cmove PARAMS ((struct noce_if_info *,
437 rtx, enum rtx_code, rtx,
439 static int noce_try_cmove PARAMS ((struct noce_if_info *));
440 static int noce_try_cmove_arith PARAMS ((struct noce_if_info *));
442 /* Helper function for noce_try_store_flag*. */
445 noce_emit_store_flag (if_info, x, reversep, normalize)
446 struct noce_if_info *if_info;
448 int reversep, normalize;
450 rtx cond = if_info->cond;
454 cond_complex = (! general_operand (XEXP (cond, 0), VOIDmode)
455 || ! general_operand (XEXP (cond, 1), VOIDmode));
457 /* If earliest == jump, or when the condition is complex, try to
458 build the store_flag insn directly. */
461 cond = XEXP (SET_SRC (PATTERN (if_info->jump)), 0);
463 if ((if_info->cond_earliest == if_info->jump || cond_complex)
464 && (normalize == 0 || STORE_FLAG_VALUE == normalize))
468 code = GET_CODE (cond);
470 code = reverse_condition (code);
472 tmp = gen_rtx_fmt_ee (code, GET_MODE (x), XEXP (cond, 0),
474 tmp = gen_rtx_SET (VOIDmode, x, tmp);
477 tmp = emit_insn (tmp);
479 if (recog_memoized (tmp) >= 0)
485 if_info->cond_earliest = if_info->jump;
493 /* Don't even try if the comparison operands are weird. */
497 code = GET_CODE (cond);
499 code = reverse_condition (code);
501 return emit_store_flag (x, code, XEXP (cond, 0),
502 XEXP (cond, 1), VOIDmode,
503 (code == LTU || code == LEU
504 || code == GEU || code == GTU), normalize);
507 /* Convert "if (test) x = 1; else x = 0".
509 Only try 0 and STORE_FLAG_VALUE here. Other combinations will be
510 tried in noce_try_store_flag_constants after noce_try_cmove has had
511 a go at the conversion. */
514 noce_try_store_flag (if_info)
515 struct noce_if_info *if_info;
520 if (GET_CODE (if_info->b) == CONST_INT
521 && INTVAL (if_info->b) == STORE_FLAG_VALUE
522 && if_info->a == const0_rtx)
524 else if (if_info->b == const0_rtx
525 && GET_CODE (if_info->a) == CONST_INT
526 && INTVAL (if_info->a) == STORE_FLAG_VALUE
527 && can_reverse_comparison_p (if_info->cond, if_info->jump))
534 target = noce_emit_store_flag (if_info, if_info->x, reversep, 0);
537 if (target != if_info->x)
538 emit_move_insn (if_info->x, target);
542 emit_insns_before (seq, if_info->cond_earliest);
553 /* Convert "if (test) x = a; else x = b", for A and B constant. */
556 noce_try_store_flag_constants (if_info)
557 struct noce_if_info *if_info;
561 HOST_WIDE_INT itrue, ifalse, diff, tmp;
562 int normalize, can_reverse;
565 && GET_CODE (if_info->a) == CONST_INT
566 && GET_CODE (if_info->b) == CONST_INT)
568 ifalse = INTVAL (if_info->a);
569 itrue = INTVAL (if_info->b);
570 diff = itrue - ifalse;
572 can_reverse = can_reverse_comparison_p (if_info->cond, if_info->jump);
575 if (diff == STORE_FLAG_VALUE || diff == -STORE_FLAG_VALUE)
577 else if (ifalse == 0 && exact_log2 (itrue) >= 0
578 && (STORE_FLAG_VALUE == 1
579 || BRANCH_COST >= 2))
581 else if (itrue == 0 && exact_log2 (ifalse) >= 0 && can_reverse
582 && (STORE_FLAG_VALUE == 1 || BRANCH_COST >= 2))
583 normalize = 1, reversep = 1;
585 && (STORE_FLAG_VALUE == -1
586 || BRANCH_COST >= 2))
588 else if (ifalse == -1 && can_reverse
589 && (STORE_FLAG_VALUE == -1 || BRANCH_COST >= 2))
590 normalize = -1, reversep = 1;
591 else if ((BRANCH_COST >= 2 && STORE_FLAG_VALUE == -1)
599 tmp = itrue; itrue = ifalse; ifalse = tmp;
604 target = noce_emit_store_flag (if_info, if_info->x, reversep, normalize);
611 /* if (test) x = 3; else x = 4;
612 => x = 3 + (test == 0); */
613 if (diff == STORE_FLAG_VALUE || diff == -STORE_FLAG_VALUE)
615 target = expand_binop (GET_MODE (if_info->x),
616 (diff == STORE_FLAG_VALUE
617 ? add_optab : sub_optab),
618 GEN_INT (ifalse), target, if_info->x, 0,
622 /* if (test) x = 8; else x = 0;
623 => x = (test != 0) << 3; */
624 else if (ifalse == 0 && (tmp = exact_log2 (itrue)) >= 0)
626 target = expand_binop (GET_MODE (if_info->x), ashl_optab,
627 target, GEN_INT (tmp), if_info->x, 0,
631 /* if (test) x = -1; else x = b;
632 => x = -(test != 0) | b; */
633 else if (itrue == -1)
635 target = expand_binop (GET_MODE (if_info->x), ior_optab,
636 target, GEN_INT (ifalse), if_info->x, 0,
640 /* if (test) x = a; else x = b;
641 => x = (-(test != 0) & (b - a)) + a; */
644 target = expand_binop (GET_MODE (if_info->x), and_optab,
645 target, GEN_INT (diff), if_info->x, 0,
648 target = expand_binop (GET_MODE (if_info->x), add_optab,
649 target, GEN_INT (ifalse), if_info->x, 0,
659 if (target != if_info->x)
660 emit_move_insn (if_info->x, target);
665 if (seq_contains_jump (seq))
668 emit_insns_before (seq, if_info->cond_earliest);
676 /* Convert "if (test) foo++" into "foo += (test != 0)", and
677 similarly for "foo--". */
680 noce_try_store_flag_inc (if_info)
681 struct noce_if_info *if_info;
684 int subtract, normalize;
690 /* Should be no `else' case to worry about. */
691 && if_info->b == if_info->x
692 && GET_CODE (if_info->a) == PLUS
693 && (XEXP (if_info->a, 1) == const1_rtx
694 || XEXP (if_info->a, 1) == constm1_rtx)
695 && rtx_equal_p (XEXP (if_info->a, 0), if_info->x)
696 && can_reverse_comparison_p (if_info->cond, if_info->jump))
698 if (STORE_FLAG_VALUE == INTVAL (XEXP (if_info->a, 1)))
699 subtract = 0, normalize = 0;
700 else if (-STORE_FLAG_VALUE == INTVAL (XEXP (if_info->a, 1)))
701 subtract = 1, normalize = 0;
703 subtract = 0, normalize = INTVAL (XEXP (if_info->a, 1));
707 target = noce_emit_store_flag (if_info,
708 gen_reg_rtx (GET_MODE (if_info->x)),
712 target = expand_binop (GET_MODE (if_info->x),
713 subtract ? sub_optab : add_optab,
714 if_info->x, target, if_info->x, 0, OPTAB_WIDEN);
717 if (target != if_info->x)
718 emit_move_insn (if_info->x, target);
723 if (seq_contains_jump (seq))
726 emit_insns_before (seq, if_info->cond_earliest);
737 /* Convert "if (test) x = 0;" to "x &= -(test == 0);" */
740 noce_try_store_flag_mask (if_info)
741 struct noce_if_info *if_info;
749 || STORE_FLAG_VALUE == -1)
750 && ((if_info->a == const0_rtx
751 && rtx_equal_p (if_info->b, if_info->x))
752 || ((reversep = can_reverse_comparison_p (if_info->cond,
754 && if_info->b == const0_rtx
755 && rtx_equal_p (if_info->a, if_info->x))))
758 target = noce_emit_store_flag (if_info,
759 gen_reg_rtx (GET_MODE (if_info->x)),
762 target = expand_binop (GET_MODE (if_info->x), and_optab,
763 if_info->x, target, if_info->x, 0,
768 if (target != if_info->x)
769 emit_move_insn (if_info->x, target);
774 if (seq_contains_jump (seq))
777 emit_insns_before (seq, if_info->cond_earliest);
788 /* Helper function for noce_try_cmove and noce_try_cmove_arith. */
791 noce_emit_cmove (if_info, x, code, cmp_a, cmp_b, vfalse, vtrue)
792 struct noce_if_info *if_info;
793 rtx x, cmp_a, cmp_b, vfalse, vtrue;
796 /* If earliest == jump, try to build the cmove insn directly.
797 This is helpful when combine has created some complex condition
798 (like for alpha's cmovlbs) that we can't hope to regenerate
799 through the normal interface. */
801 if (if_info->cond_earliest == if_info->jump)
805 tmp = gen_rtx_fmt_ee (code, GET_MODE (if_info->cond), cmp_a, cmp_b);
806 tmp = gen_rtx_IF_THEN_ELSE (GET_MODE (x), tmp, vtrue, vfalse);
807 tmp = gen_rtx_SET (VOIDmode, x, tmp);
810 tmp = emit_insn (tmp);
812 if (recog_memoized (tmp) >= 0)
824 /* Don't even try if the comparison operands are weird. */
825 if (! general_operand (cmp_a, GET_MODE (cmp_a))
826 || ! general_operand (cmp_b, GET_MODE (cmp_b)))
829 #if HAVE_conditional_move
830 return emit_conditional_move (x, code, cmp_a, cmp_b, VOIDmode,
831 vtrue, vfalse, GET_MODE (x),
832 (code == LTU || code == GEU
833 || code == LEU || code == GTU));
835 /* We'll never get here, as noce_process_if_block doesn't call the
836 functions involved. Ifdef code, however, should be discouraged
837 because it leads to typos in the code not selected. However,
838 emit_conditional_move won't exist either. */
843 /* Try only simple constants and registers here. More complex cases
844 are handled in noce_try_cmove_arith after noce_try_store_flag_arith
845 has had a go at it. */
848 noce_try_cmove (if_info)
849 struct noce_if_info *if_info;
854 if ((CONSTANT_P (if_info->a) || register_operand (if_info->a, VOIDmode))
855 && (CONSTANT_P (if_info->b) || register_operand (if_info->b, VOIDmode)))
859 code = GET_CODE (if_info->cond);
860 target = noce_emit_cmove (if_info, if_info->x, code,
861 XEXP (if_info->cond, 0),
862 XEXP (if_info->cond, 1),
863 if_info->a, if_info->b);
867 if (target != if_info->x)
868 emit_move_insn (if_info->x, target);
872 emit_insns_before (seq, if_info->cond_earliest);
885 /* Try more complex cases involving conditional_move. */
888 noce_try_cmove_arith (if_info)
889 struct noce_if_info *if_info;
899 /* A conditional move from two memory sources is equivalent to a
900 conditional on their addresses followed by a load. Don't do this
901 early because it'll screw alias analysis. Note that we've
902 already checked for no side effects. */
903 if (! no_new_pseudos && cse_not_expected
904 && GET_CODE (a) == MEM && GET_CODE (b) == MEM
909 x = gen_reg_rtx (Pmode);
913 /* ??? We could handle this if we knew that a load from A or B could
914 not fault. This is also true if we've already loaded
915 from the address along the path from ENTRY. */
916 else if (may_trap_p (a) || may_trap_p (b))
919 /* if (test) x = a + b; else x = c - d;
926 code = GET_CODE (if_info->cond);
927 insn_a = if_info->insn_a;
928 insn_b = if_info->insn_b;
930 /* Possibly rearrange operands to make things come out more natural. */
931 if (can_reverse_comparison_p (if_info->cond, if_info->jump))
934 if (rtx_equal_p (b, x))
936 else if (general_operand (b, GET_MODE (b)))
941 code = reverse_condition (code);
942 tmp = a, a = b, b = tmp;
943 tmp = insn_a, insn_a = insn_b, insn_b = tmp;
949 /* If either operand is complex, load it into a register first.
950 The best way to do this is to copy the original insn. In this
951 way we preserve any clobbers etc that the insn may have had.
952 This is of course not possible in the IS_MEM case. */
953 if (! general_operand (a, GET_MODE (a)))
958 goto end_seq_and_fail;
962 tmp = gen_reg_rtx (GET_MODE (a));
963 tmp = emit_insn (gen_rtx_SET (VOIDmode, tmp, a));
966 goto end_seq_and_fail;
969 a = gen_reg_rtx (GET_MODE (a));
970 tmp = copy_rtx (insn_a);
971 set = single_set (tmp);
973 tmp = emit_insn (PATTERN (tmp));
975 if (recog_memoized (tmp) < 0)
976 goto end_seq_and_fail;
978 if (! general_operand (b, GET_MODE (b)))
983 goto end_seq_and_fail;
987 tmp = gen_reg_rtx (GET_MODE (b));
988 tmp = emit_insn (gen_rtx_SET (VOIDmode, tmp, b));
991 goto end_seq_and_fail;
994 b = gen_reg_rtx (GET_MODE (b));
995 tmp = copy_rtx (insn_b);
996 set = single_set (tmp);
998 tmp = emit_insn (PATTERN (tmp));
1000 if (recog_memoized (tmp) < 0)
1001 goto end_seq_and_fail;
1004 target = noce_emit_cmove (if_info, x, code, XEXP (if_info->cond, 0),
1005 XEXP (if_info->cond, 1), a, b);
1008 goto end_seq_and_fail;
1010 /* If we're handling a memory for above, emit the load now. */
1013 tmp = gen_rtx_MEM (GET_MODE (if_info->x), target);
1015 /* Copy over flags as appropriate. */
1016 if (MEM_VOLATILE_P (if_info->a) || MEM_VOLATILE_P (if_info->b))
1017 MEM_VOLATILE_P (tmp) = 1;
1018 if (MEM_IN_STRUCT_P (if_info->a) && MEM_IN_STRUCT_P (if_info->b))
1019 MEM_IN_STRUCT_P (tmp) = 1;
1020 if (MEM_SCALAR_P (if_info->a) && MEM_SCALAR_P (if_info->b))
1021 MEM_SCALAR_P (tmp) = 1;
1022 if (MEM_ALIAS_SET (if_info->a) == MEM_ALIAS_SET (if_info->b))
1023 MEM_ALIAS_SET (tmp) = MEM_ALIAS_SET (if_info->a);
1025 emit_move_insn (if_info->x, tmp);
1027 else if (target != x)
1028 emit_move_insn (x, target);
1032 emit_insns_before (tmp, if_info->cond_earliest);
1040 /* Look for the condition for the jump first. We'd prefer to avoid
1041 get_condition if we can -- it tries to look back for the contents
1042 of an original compare. On targets that use normal integers for
1043 comparisons, e.g. alpha, this is wasteful. */
1046 noce_get_condition (jump, earliest)
1053 /* If the condition variable is a register and is MODE_INT, accept it.
1054 Otherwise, fall back on get_condition. */
1056 if (! any_condjump_p (jump))
1059 set = pc_set (jump);
1061 cond = XEXP (SET_SRC (set), 0);
1062 if (GET_CODE (XEXP (cond, 0)) == REG
1063 && GET_MODE_CLASS (GET_MODE (XEXP (cond, 0))) == MODE_INT)
1067 /* If this branches to JUMP_LABEL when the condition is false,
1068 reverse the condition. */
1069 if (GET_CODE (XEXP (SET_SRC (set), 2)) == LABEL_REF
1070 && XEXP (XEXP (SET_SRC (set), 2), 0) == JUMP_LABEL (jump))
1071 cond = gen_rtx_fmt_ee (reverse_condition (GET_CODE (cond)),
1072 GET_MODE (cond), XEXP (cond, 0),
1076 cond = get_condition (jump, earliest);
1081 /* Given a simple IF-THEN or IF-THEN-ELSE block, attempt to convert it
1082 without using conditional execution. Return TRUE if we were
1083 successful at converting the the block. */
1086 noce_process_if_block (test_bb, then_bb, else_bb, join_bb)
1087 basic_block test_bb; /* Basic block test is in */
1088 basic_block then_bb; /* Basic block for THEN block */
1089 basic_block else_bb; /* Basic block for ELSE block */
1090 basic_block join_bb; /* Basic block the join label is in */
1092 /* We're looking for patterns of the form
1094 (1) if (...) x = a; else x = b;
1095 (2) x = b; if (...) x = a;
1096 (3) if (...) x = a; // as if with an initial x = x.
1098 The later patterns require jumps to be more expensive.
1100 ??? For future expansion, look for multiple X in such patterns. */
1102 struct noce_if_info if_info;
1105 rtx orig_x, x, a, b;
1106 rtx jump, cond, insn;
1108 /* If this is not a standard conditional jump, we can't parse it. */
1109 jump = test_bb->end;
1110 cond = noce_get_condition (jump, &if_info.cond_earliest);
1114 /* If the conditional jump is more than just a conditional jump,
1115 then we can not do if-conversion on this block. */
1116 if (! onlyjump_p (jump))
1119 /* We must be comparing objects whose modes imply the size. */
1120 if (GET_MODE (XEXP (cond, 0)) == BLKmode)
1123 /* Look for one of the potential sets. */
1124 insn_a = first_active_insn (then_bb);
1126 || ! last_active_insn_p (then_bb, insn_a)
1127 || (set_a = single_set (insn_a)) == NULL_RTX)
1130 x = SET_DEST (set_a);
1131 a = SET_SRC (set_a);
1133 /* Look for the other potential set. Make sure we've got equivalent
1135 /* ??? This is overconservative. Storing to two different mems is
1136 as easy as conditionally computing the address. Storing to a
1137 single mem merely requires a scratch memory to use as one of the
1138 destination addresses; often the memory immediately below the
1139 stack pointer is available for this. */
1143 insn_b = first_active_insn (else_bb);
1145 || ! last_active_insn_p (else_bb, insn_b)
1146 || (set_b = single_set (insn_b)) == NULL_RTX
1147 || ! rtx_equal_p (x, SET_DEST (set_b)))
1152 insn_b = prev_nonnote_insn (if_info.cond_earliest);
1154 || GET_CODE (insn_b) != INSN
1155 || (set_b = single_set (insn_b)) == NULL_RTX
1156 || ! rtx_equal_p (x, SET_DEST (set_b))
1157 || reg_mentioned_p (x, cond)
1158 || reg_mentioned_p (x, a)
1159 || reg_mentioned_p (x, SET_SRC (set_b)))
1160 insn_b = set_b = NULL_RTX;
1162 b = (set_b ? SET_SRC (set_b) : x);
1164 /* X may not be mentioned in the range (cond_earliest, jump]. */
1165 for (insn = jump; insn != if_info.cond_earliest; insn = PREV_INSN (insn))
1166 if (INSN_P (insn) && reg_mentioned_p (x, insn))
1169 /* A and B may not be modified in the range [cond_earliest, jump). */
1170 for (insn = if_info.cond_earliest; insn != jump; insn = NEXT_INSN (insn))
1172 && (modified_in_p (a, insn) || modified_in_p (b, insn)))
1175 /* Only operate on register destinations, and even then avoid extending
1176 the lifetime of hard registers on small register class machines. */
1178 if (GET_CODE (x) != REG
1179 || (SMALL_REGISTER_CLASSES
1180 && REGNO (x) < FIRST_PSEUDO_REGISTER))
1184 x = gen_reg_rtx (GET_MODE (x));
1187 /* Don't operate on sources that may trap or are volatile. */
1188 if (side_effects_p (a) || side_effects_p (b)
1189 || (GET_CODE (a) != MEM && may_trap_p (a))
1190 || (GET_CODE (b) != MEM && may_trap_p (b)))
1193 /* Set up the info block for our subroutines. */
1194 if_info.cond = cond;
1195 if_info.jump = jump;
1196 if_info.insn_a = insn_a;
1197 if_info.insn_b = insn_b;
1202 /* Try optimizations in some approximation of a useful order. */
1203 /* ??? Should first look to see if X is live incoming at all. If it
1204 isn't, we don't need anything but an unconditional set. */
1206 /* Look and see if A and B are really the same. Avoid creating silly
1207 cmove constructs that no one will fix up later. */
1208 if (rtx_equal_p (a, b))
1210 /* If we have an INSN_B, we don't have to create any new rtl. Just
1211 move the instruction that we already have. If we don't have an
1212 INSN_B, that means that A == X, and we've got a noop move. In
1213 that case don't do anything and let the code below delete INSN_A. */
1214 if (insn_b && else_bb)
1216 if (else_bb && insn_b == else_bb->end)
1217 else_bb->end = PREV_INSN (insn_b);
1218 reorder_insns (insn_b, insn_b, PREV_INSN (if_info.cond_earliest));
1225 if (noce_try_store_flag (&if_info))
1227 if (HAVE_conditional_move
1228 && noce_try_cmove (&if_info))
1230 if (! HAVE_conditional_execution)
1232 if (noce_try_store_flag_constants (&if_info))
1234 if (noce_try_store_flag_inc (&if_info))
1236 if (noce_try_store_flag_mask (&if_info))
1238 if (HAVE_conditional_move
1239 && noce_try_cmove_arith (&if_info))
1246 /* The original sets may now be killed. */
1247 if (insn_a == then_bb->end)
1248 then_bb->end = PREV_INSN (insn_a);
1249 flow_delete_insn (insn_a);
1251 /* Several special cases here: First, we may have reused insn_b above,
1252 in which case insn_b is now NULL. Second, we want to delete insn_b
1253 if it came from the ELSE block, because follows the now correct
1254 write that appears in the TEST block. However, if we got insn_b from
1255 the TEST block, it may in fact be loading data needed for the comparison.
1256 We'll let life_analysis remove the insn if it's really dead. */
1257 if (insn_b && else_bb)
1259 if (insn_b == else_bb->end)
1260 else_bb->end = PREV_INSN (insn_b);
1261 flow_delete_insn (insn_b);
1264 /* The new insns will have been inserted before cond_earliest. We should
1265 be able to remove the jump with impunity, but the condition itself may
1266 have been modified by gcse to be shared across basic blocks. */
1267 test_bb->end = PREV_INSN (jump);
1268 flow_delete_insn (jump);
1270 /* If we used a temporary, fix it up now. */
1274 emit_move_insn (orig_x, x);
1275 insn_b = gen_sequence ();
1278 test_bb->end = emit_insn_after (insn_b, test_bb->end);
1281 /* Merge the blocks! */
1282 merge_if_block (test_bb, then_bb, else_bb, join_bb);
1287 /* Attempt to convert an IF-THEN or IF-THEN-ELSE block into
1288 straight line code. Return true if successful. */
1291 process_if_block (test_bb, then_bb, else_bb, join_bb)
1292 basic_block test_bb; /* Basic block test is in */
1293 basic_block then_bb; /* Basic block for THEN block */
1294 basic_block else_bb; /* Basic block for ELSE block */
1295 basic_block join_bb; /* Basic block the join label is in */
1297 if (! reload_completed
1298 && noce_process_if_block (test_bb, then_bb, else_bb, join_bb))
1301 if (HAVE_conditional_execution
1303 && cond_exec_process_if_block (test_bb, then_bb, else_bb, join_bb))
1309 /* Merge the blocks and mark for local life update. */
1312 merge_if_block (test_bb, then_bb, else_bb, join_bb)
1313 basic_block test_bb; /* Basic block test is in */
1314 basic_block then_bb; /* Basic block for THEN block */
1315 basic_block else_bb; /* Basic block for ELSE block */
1316 basic_block join_bb; /* Basic block the join label is in */
1318 basic_block combo_bb;
1320 /* All block merging is done into the lower block numbers. */
1324 /* First merge TEST block into THEN block. This is a no-brainer since
1325 the THEN block did not have a code label to begin with. */
1327 if (combo_bb->global_live_at_end)
1328 COPY_REG_SET (combo_bb->global_live_at_end, then_bb->global_live_at_end);
1329 merge_blocks_nomove (combo_bb, then_bb);
1330 num_removed_blocks++;
1332 /* The ELSE block, if it existed, had a label. That label count
1333 will almost always be zero, but odd things can happen when labels
1334 get their addresses taken. */
1337 merge_blocks_nomove (combo_bb, else_bb);
1338 num_removed_blocks++;
1341 /* If there was no join block reported, that means it was not adjacent
1342 to the others, and so we cannot merge them. */
1346 /* The outgoing edge for the current COMBO block should already
1347 be correct. Verify this. */
1348 if (combo_bb->succ == NULL_EDGE)
1351 /* There should sill be a branch at the end of the THEN or ELSE
1352 blocks taking us to our final destination. */
1353 if (! simplejump_p (combo_bb->end)
1354 && ! returnjump_p (combo_bb->end))
1358 /* The JOIN block may have had quite a number of other predecessors too.
1359 Since we've already merged the TEST, THEN and ELSE blocks, we should
1360 have only one remaining edge from our if-then-else diamond. If there
1361 is more than one remaining edge, it must come from elsewhere. */
1362 else if (join_bb->pred->pred_next == NULL)
1364 /* We can merge the JOIN. */
1365 if (combo_bb->global_live_at_end)
1366 COPY_REG_SET (combo_bb->global_live_at_end,
1367 join_bb->global_live_at_end);
1368 merge_blocks_nomove (combo_bb, join_bb);
1369 num_removed_blocks++;
1373 /* We cannot merge the JOIN. */
1375 /* The outgoing edge for the current COMBO block should already
1376 be correct. Verify this. */
1377 if (combo_bb->succ->succ_next != NULL_EDGE
1378 || combo_bb->succ->dest != join_bb)
1381 /* Remove the jump and cruft from the end of the COMBO block. */
1382 tidy_fallthru_edge (combo_bb->succ, combo_bb, join_bb);
1385 /* Make sure we update life info properly. */
1386 SET_UPDATE_LIFE (combo_bb);
1388 num_updated_if_blocks++;
1391 /* Find a block ending in a simple IF condition. Return TRUE if
1392 we were able to transform it in some way. */
1395 find_if_header (test_bb)
1396 basic_block test_bb;
1401 /* The kind of block we're looking for has exactly two successors. */
1402 if ((then_edge = test_bb->succ) == NULL_EDGE
1403 || (else_edge = then_edge->succ_next) == NULL_EDGE
1404 || else_edge->succ_next != NULL_EDGE)
1407 /* Neither edge should be abnormal. */
1408 if ((then_edge->flags & EDGE_COMPLEX)
1409 || (else_edge->flags & EDGE_COMPLEX))
1412 /* The THEN edge is canonically the one that falls through. */
1413 if (then_edge->flags & EDGE_FALLTHRU)
1415 else if (else_edge->flags & EDGE_FALLTHRU)
1418 else_edge = then_edge;
1422 /* Otherwise this must be a multiway branch of some sort. */
1425 if (find_if_block (test_bb, then_edge, else_edge))
1428 && (! HAVE_conditional_execution || reload_completed))
1430 if (find_if_case_1 (test_bb, then_edge, else_edge))
1432 if (find_if_case_2 (test_bb, then_edge, else_edge))
1440 fprintf (rtl_dump_file, "Conversion succeeded.\n");
1444 /* Determine if a given basic block heads a simple IF-THEN or IF-THEN-ELSE
1445 block. If so, we'll try to convert the insns to not require the branch.
1446 Return TRUE if we were successful at converting the the block. */
1449 find_if_block (test_bb, then_edge, else_edge)
1450 basic_block test_bb;
1451 edge then_edge, else_edge;
1453 basic_block then_bb = then_edge->dest;
1454 basic_block else_bb = else_edge->dest;
1455 basic_block join_bb = NULL_BLOCK;
1456 edge then_succ = then_bb->succ;
1457 edge else_succ = else_bb->succ;
1460 /* The THEN block of an IF-THEN combo must have exactly one predecessor. */
1461 if (then_bb->pred->pred_next != NULL_EDGE)
1464 /* The THEN block of an IF-THEN combo must have exactly one successor. */
1465 if (then_succ == NULL_EDGE
1466 || then_succ->succ_next != NULL_EDGE
1467 || (then_succ->flags & EDGE_COMPLEX))
1470 /* If the THEN block's successor is the other edge out of the TEST block,
1471 then we have an IF-THEN combo without an ELSE. */
1472 if (then_succ->dest == else_bb)
1475 else_bb = NULL_BLOCK;
1478 /* If the THEN and ELSE block meet in a subsequent block, and the ELSE
1479 has exactly one predecessor and one successor, and the outgoing edge
1480 is not complex, then we have an IF-THEN-ELSE combo. */
1481 else if (else_succ != NULL_EDGE
1482 && then_succ->dest == else_succ->dest
1483 && else_bb->pred->pred_next == NULL_EDGE
1484 && else_succ->succ_next == NULL_EDGE
1485 && ! (else_succ->flags & EDGE_COMPLEX))
1486 join_bb = else_succ->dest;
1488 /* Otherwise it is not an IF-THEN or IF-THEN-ELSE combination. */
1492 num_possible_if_blocks++;
1497 fprintf (rtl_dump_file,
1498 "\nIF-THEN-ELSE block found, start %d, then %d, else %d, join %d\n",
1499 test_bb->index, then_bb->index, else_bb->index,
1502 fprintf (rtl_dump_file,
1503 "\nIF-THEN block found, start %d, then %d, join %d\n",
1504 test_bb->index, then_bb->index, join_bb->index);
1507 /* Make sure IF, THEN, and ELSE, blocks are adjacent. Actually, we
1508 get the first condition for free, since we've already asserted that
1509 there's a fallthru edge from IF to THEN. */
1510 /* ??? As an enhancement, move the ELSE block. Have to deal with EH and
1511 BLOCK notes, if by no other means than aborting the merge if they
1512 exist. Sticky enough I don't want to think about it now. */
1513 next_index = then_bb->index;
1514 if (else_bb && ++next_index != else_bb->index)
1516 if (++next_index != join_bb->index)
1524 /* Do the real work. */
1525 return process_if_block (test_bb, then_bb, else_bb, join_bb);
1528 /* Look for IF-THEN-ELSE cases in which one of THEN or ELSE is
1529 transformable, but not necessarily the other. There need be no
1532 Return TRUE if we were successful at converting the the block.
1534 Cases we'd like to look at:
1537 if (test) goto over; // x not live
1545 if (! test) goto label;
1548 if (test) goto E; // x not live
1562 (3) // This one's really only interesting for targets that can do
1563 // multiway branching, e.g. IA-64 BBB bundles. For other targets
1564 // it results in multiple branches on a cache line, which often
1565 // does not sit well with predictors.
1567 if (test1) goto E; // predicted not taken
1583 (A) Don't do (2) if the branch is predicted against the block we're
1584 eliminating. Do it anyway if we can eliminate a branch; this requires
1585 that the sole successor of the eliminated block postdominate the other
1588 (B) With CE, on (3) we can steal from both sides of the if, creating
1597 Again, this is most useful if J postdominates.
1599 (C) CE substitutes for helpful life information.
1601 (D) These heuristics need a lot of work. */
1603 /* Tests for case 1 above. */
1606 find_if_case_1 (test_bb, then_edge, else_edge)
1607 basic_block test_bb;
1608 edge then_edge, else_edge;
1610 basic_block then_bb = then_edge->dest;
1611 basic_block else_bb = else_edge->dest;
1612 edge then_succ = then_bb->succ;
1615 /* THEN has one successor. */
1616 if (!then_succ || then_succ->succ_next != NULL)
1619 /* THEN does not fall through, but is not strange either. */
1620 if (then_succ->flags & (EDGE_COMPLEX | EDGE_FALLTHRU))
1623 /* THEN has one predecessor. */
1624 if (then_bb->pred->pred_next != NULL)
1627 /* ELSE follows THEN. (??? could be moved) */
1628 if (else_bb->index != then_bb->index + 1)
1631 num_possible_if_blocks++;
1633 fprintf (rtl_dump_file,
1634 "\nIF-CASE-1 found, start %d, then %d\n",
1635 test_bb->index, then_bb->index);
1637 /* THEN is small. */
1638 if (count_bb_insns (then_bb) > BRANCH_COST)
1641 /* Find the label for THEN's destination. */
1642 if (then_succ->dest == EXIT_BLOCK_PTR)
1646 new_lab = JUMP_LABEL (then_bb->end);
1651 /* Registers set are dead, or are predicable. */
1652 if (! dead_or_predicable (test_bb, then_bb, else_bb, new_lab, 1))
1655 /* Conversion went ok, including moving the insns and fixing up the
1656 jump. Adjust the CFG to match. */
1658 SET_UPDATE_LIFE (test_bb);
1659 bitmap_operation (test_bb->global_live_at_end,
1660 else_bb->global_live_at_start,
1661 then_bb->global_live_at_end, BITMAP_IOR);
1663 make_edge (NULL, test_bb, then_succ->dest, 0);
1664 flow_delete_block (then_bb);
1665 tidy_fallthru_edge (else_edge, test_bb, else_bb);
1667 num_removed_blocks++;
1668 num_updated_if_blocks++;
1673 /* Test for case 2 above. */
1676 find_if_case_2 (test_bb, then_edge, else_edge)
1677 basic_block test_bb;
1678 edge then_edge, else_edge;
1680 basic_block then_bb = then_edge->dest;
1681 basic_block else_bb = else_edge->dest;
1682 edge else_succ = else_bb->succ;
1685 /* ELSE has one successor. */
1686 if (!else_succ || else_succ->succ_next != NULL)
1689 /* ELSE outgoing edge is not complex. */
1690 if (else_succ->flags & EDGE_COMPLEX)
1693 /* ELSE has one predecessor. */
1694 if (else_bb->pred->pred_next != NULL)
1697 /* THEN is not EXIT. */
1698 if (then_bb->index < 0)
1701 /* ELSE is predicted or SUCC(ELSE) postdominates THEN. */
1702 note = find_reg_note (test_bb->end, REG_BR_PROB, NULL_RTX);
1703 if (note && INTVAL (XEXP (note, 0)) >= REG_BR_PROB_BASE / 2)
1705 else if (else_succ->dest->index < 0
1706 || TEST_BIT (post_dominators[ORIG_INDEX (then_bb)],
1707 ORIG_INDEX (else_succ->dest)))
1712 num_possible_if_blocks++;
1714 fprintf (rtl_dump_file,
1715 "\nIF-CASE-2 found, start %d, else %d\n",
1716 test_bb->index, else_bb->index);
1718 /* ELSE is small. */
1719 if (count_bb_insns (then_bb) > BRANCH_COST)
1722 /* Find the label for ELSE's destination. */
1723 if (else_succ->dest == EXIT_BLOCK_PTR)
1727 if (else_succ->flags & EDGE_FALLTHRU)
1729 new_lab = else_succ->dest->head;
1730 if (GET_CODE (new_lab) != CODE_LABEL)
1735 new_lab = JUMP_LABEL (else_bb->end);
1741 /* Registers set are dead, or are predicable. */
1742 if (! dead_or_predicable (test_bb, else_bb, then_bb, new_lab, 0))
1745 /* Conversion went ok, including moving the insns and fixing up the
1746 jump. Adjust the CFG to match. */
1748 SET_UPDATE_LIFE (test_bb);
1749 bitmap_operation (test_bb->global_live_at_end,
1750 then_bb->global_live_at_start,
1751 else_bb->global_live_at_end, BITMAP_IOR);
1753 remove_edge (else_edge);
1754 make_edge (NULL, test_bb, else_succ->dest, 0);
1755 flow_delete_block (else_bb);
1757 num_removed_blocks++;
1758 num_updated_if_blocks++;
1760 /* ??? We may now fallthru from one of THEN's successors into a join
1761 block. Rerun cleanup_cfg? Examine things manually? Wait? */
1766 /* A subroutine of dead_or_predicable called through for_each_rtx.
1767 Return 1 if a memory is found. */
1770 find_memory (px, data)
1772 void *data ATTRIBUTE_UNUSED;
1774 return GET_CODE (*px) == MEM;
1777 /* Used by the code above to perform the actual rtl transformations.
1778 Return TRUE if successful.
1780 TEST_BB is the block containing the conditional branch. MERGE_BB
1781 is the block containing the code to manipulate. NEW_DEST is the
1782 label TEST_BB should be branching to after the conversion.
1783 REVERSEP is true if the sense of the branch should be reversed. */
1786 dead_or_predicable (test_bb, merge_bb, other_bb, new_dest, reversep)
1787 basic_block test_bb, merge_bb, other_bb;
1791 rtx head, end, jump, earliest, old_dest;
1793 jump = test_bb->end;
1795 /* Find the extent of the real code in the merge block. */
1796 head = merge_bb->head;
1797 end = merge_bb->end;
1799 if (GET_CODE (head) == CODE_LABEL)
1800 head = NEXT_INSN (head);
1801 if (GET_CODE (head) == NOTE)
1805 head = end = NULL_RTX;
1808 head = NEXT_INSN (head);
1811 if (GET_CODE (end) == JUMP_INSN)
1815 head = end = NULL_RTX;
1818 end = PREV_INSN (end);
1821 if (HAVE_conditional_execution)
1823 /* In the conditional execution case, we have things easy. We know
1824 the condition is reversable. We don't have to check life info,
1825 becase we're going to conditionally execute the code anyway.
1826 All that's left is making sure the insns involved can actually
1831 cond = cond_exec_get_condition (jump);
1833 prob_val = find_reg_note (jump, REG_BR_PROB, NULL_RTX);
1835 prob_val = XEXP (prob_val, 0);
1839 cond = gen_rtx_fmt_ee (reverse_condition (GET_CODE (cond)),
1840 GET_MODE (cond), XEXP (cond, 0),
1843 prob_val = GEN_INT (REG_BR_PROB_BASE - INTVAL (prob_val));
1846 if (! cond_exec_process_insns (head, end, cond, prob_val, 0))
1853 /* In the non-conditional execution case, we have to verify that there
1854 are no trapping operations, no calls, no references to memory, and
1855 that any registers modified are dead at the branch site. */
1857 rtx insn, cond, prev;
1858 regset_head merge_set_head, tmp_head, test_live_head, test_set_head;
1859 regset merge_set, tmp, test_live, test_set;
1860 struct propagate_block_info *pbi;
1863 /* Check for no calls or trapping operations. */
1864 for (insn = head; ; insn = NEXT_INSN (insn))
1866 if (GET_CODE (insn) == CALL_INSN)
1870 if (may_trap_p (PATTERN (insn)))
1873 /* ??? Even non-trapping memories such as stack frame
1874 references must be avoided. For stores, we collect
1875 no lifetime info; for reads, we'd have to assert
1876 true_dependance false against every store in the
1878 if (for_each_rtx (&PATTERN (insn), find_memory, NULL))
1885 if (! any_condjump_p (jump))
1888 /* Find the extent of the conditional. */
1889 cond = noce_get_condition (jump, &earliest);
1894 MERGE_SET = set of registers set in MERGE_BB
1895 TEST_LIVE = set of registers live at EARLIEST
1896 TEST_SET = set of registers set between EARLIEST and the
1897 end of the block. */
1899 tmp = INITIALIZE_REG_SET (tmp_head);
1900 merge_set = INITIALIZE_REG_SET (merge_set_head);
1901 test_live = INITIALIZE_REG_SET (test_live_head);
1902 test_set = INITIALIZE_REG_SET (test_set_head);
1904 /* ??? bb->local_set is only valid during calculate_global_regs_live,
1905 so we must recompute usage for MERGE_BB. Not so bad, I suppose,
1906 since we've already asserted that MERGE_BB is small. */
1907 propagate_block (merge_bb, tmp, merge_set, 0);
1909 /* For small register class machines, don't lengthen lifetimes of
1910 hard registers before reload. */
1911 if (SMALL_REGISTER_CLASSES && ! reload_completed)
1913 EXECUTE_IF_SET_IN_BITMAP
1916 if (i < FIRST_PSEUDO_REGISTER
1918 && ! global_regs[i])
1923 /* For TEST, we're interested in a range of insns, not a whole block.
1924 Moreover, we're interested in the insns live from OTHER_BB. */
1926 COPY_REG_SET (test_live, other_bb->global_live_at_start);
1927 pbi = init_propagate_block_info (test_bb, test_live, test_set, 0);
1929 for (insn = jump; ; insn = prev)
1931 prev = propagate_one_insn (pbi, insn);
1932 if (insn == earliest)
1936 free_propagate_block_info (pbi);
1938 /* We can perform the transformation if
1939 MERGE_SET & (TEST_SET | TEST_LIVE)
1941 TEST_SET & merge_bb->global_live_at_start
1944 bitmap_operation (tmp, test_set, test_live, BITMAP_IOR);
1945 bitmap_operation (tmp, tmp, merge_set, BITMAP_AND);
1946 EXECUTE_IF_SET_IN_BITMAP(tmp, 0, i, fail = 1);
1948 bitmap_operation (tmp, test_set, merge_bb->global_live_at_start,
1950 EXECUTE_IF_SET_IN_BITMAP(tmp, 0, i, fail = 1);
1953 FREE_REG_SET (merge_set);
1954 FREE_REG_SET (test_live);
1955 FREE_REG_SET (test_set);
1962 /* We don't want to use normal invert_jump or redirect_jump because
1963 we don't want to delete_insn called. Also, we want to do our own
1964 change group management. */
1966 old_dest = JUMP_LABEL (jump);
1968 ? ! invert_jump_1 (jump, new_dest)
1969 : ! redirect_jump_1 (jump, new_dest))
1972 if (! apply_change_group ())
1976 LABEL_NUSES (old_dest) -= 1;
1978 LABEL_NUSES (new_dest) += 1;
1979 JUMP_LABEL (jump) = new_dest;
1983 rtx note = find_reg_note (jump, REG_BR_PROB, NULL_RTX);
1985 XEXP (note, 0) = GEN_INT (REG_BR_PROB_BASE - INTVAL (XEXP (note, 0)));
1988 /* Move the insns out of MERGE_BB to before the branch. */
1991 if (end == merge_bb->end)
1992 merge_bb->end = PREV_INSN (head);
1994 head = squeeze_notes (head, end);
1995 if (GET_CODE (end) == NOTE
1996 && (NOTE_LINE_NUMBER (end) == NOTE_INSN_BLOCK_END
1997 || NOTE_LINE_NUMBER (end) == NOTE_INSN_BLOCK_BEG
1998 || NOTE_LINE_NUMBER (end) == NOTE_INSN_LOOP_BEG
1999 || NOTE_LINE_NUMBER (end) == NOTE_INSN_LOOP_END
2000 || NOTE_LINE_NUMBER (end) == NOTE_INSN_LOOP_CONT
2001 || NOTE_LINE_NUMBER (end) == NOTE_INSN_LOOP_VTOP))
2005 end = PREV_INSN (end);
2008 reorder_insns (head, end, PREV_INSN (earliest));
2017 /* Main entry point for all if-conversion. */
2020 if_convert (life_data_ok)
2025 num_possible_if_blocks = 0;
2026 num_updated_if_blocks = 0;
2027 num_removed_blocks = 0;
2029 /* Free up basic_block_for_insn so that we don't have to keep it
2030 up to date, either here or in merge_blocks_nomove. */
2031 free_basic_block_vars (1);
2033 /* Compute postdominators if we think we'll use them. */
2034 post_dominators = NULL;
2035 if (HAVE_conditional_execution || life_data_ok)
2037 post_dominators = sbitmap_vector_alloc (n_basic_blocks, n_basic_blocks);
2038 compute_flow_dominators (NULL, post_dominators);
2041 /* Record initial block numbers. */
2042 for (block_num = 0; block_num < n_basic_blocks; block_num++)
2043 SET_ORIG_INDEX (BASIC_BLOCK (block_num), block_num);
2045 /* Go through each of the basic blocks looking for things to convert. */
2046 for (block_num = 0; block_num < n_basic_blocks; )
2048 basic_block bb = BASIC_BLOCK (block_num);
2049 if (find_if_header (bb))
2050 block_num = bb->index;
2055 if (post_dominators)
2056 sbitmap_vector_free (post_dominators);
2059 fflush (rtl_dump_file);
2061 /* Rebuild basic_block_for_insn for update_life_info and for gcse. */
2062 compute_bb_for_insn (get_max_uid ());
2064 /* Rebuild life info for basic blocks that require it. */
2065 if (num_removed_blocks && life_data_ok)
2067 sbitmap update_life_blocks = sbitmap_alloc (n_basic_blocks);
2068 sbitmap_zero (update_life_blocks);
2070 /* If we allocated new pseudos, we must resize the array for sched1. */
2071 if (max_regno < max_reg_num ())
2073 max_regno = max_reg_num ();
2074 allocate_reg_info (max_regno, FALSE, FALSE);
2077 for (block_num = 0; block_num < n_basic_blocks; block_num++)
2078 if (UPDATE_LIFE (BASIC_BLOCK (block_num)))
2079 SET_BIT (update_life_blocks, block_num);
2081 count_or_remove_death_notes (update_life_blocks, 1);
2082 /* ??? See about adding a mode that verifies that the initial
2083 set of blocks don't let registers come live. */
2084 update_life_info (update_life_blocks, UPDATE_LIFE_GLOBAL,
2085 PROP_DEATH_NOTES | PROP_SCAN_DEAD_CODE
2086 | PROP_KILL_DEAD_CODE);
2088 sbitmap_free (update_life_blocks);
2091 /* Write the final stats. */
2092 if (rtl_dump_file && num_possible_if_blocks > 0)
2094 fprintf (rtl_dump_file,
2095 "\n%d possible IF blocks searched.\n",
2096 num_possible_if_blocks);
2097 fprintf (rtl_dump_file,
2098 "%d IF blocks converted.\n",
2099 num_updated_if_blocks);
2100 fprintf (rtl_dump_file,
2101 "%d basic blocks deleted.\n\n\n",
2102 num_removed_blocks);
2105 #ifdef ENABLE_CHECKING
2106 verify_flow_info ();