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 "basic-block.h"
33 #include "hard-reg-set.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));
77 static int cond_exec_process_insns PARAMS ((rtx, rtx, rtx, rtx, int));
78 static rtx cond_exec_get_condition PARAMS ((rtx));
79 static int cond_exec_process_if_block PARAMS ((basic_block, basic_block,
80 basic_block, basic_block));
82 static rtx noce_get_condition PARAMS ((rtx, rtx *));
83 static int noce_process_if_block PARAMS ((basic_block, basic_block,
84 basic_block, basic_block));
86 static int process_if_block PARAMS ((basic_block, basic_block,
87 basic_block, basic_block));
88 static void merge_if_block PARAMS ((basic_block, basic_block,
89 basic_block, basic_block));
91 static int find_if_header PARAMS ((basic_block));
92 static int find_if_block PARAMS ((basic_block, edge, edge));
93 static int find_if_case_1 PARAMS ((basic_block, edge, edge));
94 static int find_if_case_2 PARAMS ((basic_block, edge, edge));
95 static int find_memory PARAMS ((rtx *, void *));
96 static int dead_or_predicable PARAMS ((basic_block, basic_block,
97 basic_block, rtx, int));
99 /* Abuse the basic_block AUX field to store the original block index,
100 as well as a flag indicating that the block should be rescaned for
103 #define SET_ORIG_INDEX(BB,I) ((BB)->aux = (void *)((size_t)(I) << 1))
104 #define ORIG_INDEX(BB) ((size_t)(BB)->aux >> 1)
105 #define SET_UPDATE_LIFE(BB) ((BB)->aux = (void *)((size_t)(BB)->aux | 1))
106 #define UPDATE_LIFE(BB) ((size_t)(BB)->aux & 1)
109 /* Count the number of non-jump active insns in BB. */
120 if (GET_CODE (insn) == CALL_INSN || GET_CODE (insn) == INSN)
125 insn = NEXT_INSN (insn);
131 /* Return the first non-jump active insn in the basic block. */
134 first_active_insn (bb)
139 if (GET_CODE (insn) == CODE_LABEL)
143 insn = NEXT_INSN (insn);
146 while (GET_CODE (insn) == NOTE)
150 insn = NEXT_INSN (insn);
153 if (GET_CODE (insn) == JUMP_INSN)
159 /* Return true if INSN is the last active non-jump insn in BB. */
162 last_active_insn_p (bb, insn)
170 insn = NEXT_INSN (insn);
172 while (GET_CODE (insn) == NOTE);
174 return GET_CODE (insn) == JUMP_INSN;
177 /* Go through a bunch of insns, converting them to conditional
178 execution format if possible. Return TRUE if all of the non-note
179 insns were processed. */
182 cond_exec_process_insns (start, end, test, prob_val, mod_ok)
183 rtx start; /* first insn to look at */
184 rtx end; /* last insn to look at */
185 rtx test; /* conditional execution test */
186 rtx prob_val; /* probability of branch taken. */
187 int mod_ok; /* true if modifications ok last insn. */
189 int must_be_last = FALSE;
192 for (insn = start; ; insn = NEXT_INSN (insn))
194 if (GET_CODE (insn) == NOTE)
197 if (GET_CODE (insn) != INSN && GET_CODE (insn) != CALL_INSN)
200 /* Last insn wasn't last? */
204 if (modified_in_p (test, insn))
211 /* Now build the conditional form of the instruction. */
212 validate_change (insn, &PATTERN (insn),
213 gen_rtx_COND_EXEC (VOIDmode, copy_rtx (test),
216 if (GET_CODE (insn) == CALL_INSN && prob_val)
217 validate_change (insn, ®_NOTES (insn),
218 alloc_EXPR_LIST (REG_BR_PROB, prob_val,
219 REG_NOTES (insn)), 1);
229 /* Return the condition for a jump. Do not do any special processing. */
232 cond_exec_get_condition (jump)
237 if (condjump_p (jump))
238 test_if = SET_SRC (PATTERN (jump));
239 else if (condjump_in_parallel_p (jump))
240 test_if = SET_SRC (XVECEXP (PATTERN (jump), 0, 0));
243 cond = XEXP (test_if, 0);
245 /* If this branches to JUMP_LABEL when the condition is false,
246 reverse the condition. */
247 if (GET_CODE (XEXP (test_if, 2)) == LABEL_REF
248 && XEXP (XEXP (test_if, 2), 0) == JUMP_LABEL (jump))
249 cond = gen_rtx_fmt_ee (reverse_condition (GET_CODE (cond)),
250 GET_MODE (cond), XEXP (cond, 0),
256 /* Given a simple IF-THEN or IF-THEN-ELSE block, attempt to convert it
257 to conditional execution. Return TRUE if we were successful at
258 converting the the block. */
261 cond_exec_process_if_block (test_bb, then_bb, else_bb, join_bb)
262 basic_block test_bb; /* Basic block test is in */
263 basic_block then_bb; /* Basic block for THEN block */
264 basic_block else_bb; /* Basic block for ELSE block */
265 basic_block join_bb; /* Basic block the join label is in */
267 rtx test_expr; /* expression in IF_THEN_ELSE that is tested */
268 rtx then_start; /* first insn in THEN block */
269 rtx then_end; /* last insn + 1 in THEN block */
270 rtx else_start; /* first insn in ELSE block or NULL */
271 rtx else_end; /* last insn + 1 in ELSE block */
272 int max; /* max # of insns to convert. */
273 int then_mod_ok; /* whether conditional mods are ok in THEN */
274 rtx true_expr; /* test for else block insns */
275 rtx false_expr; /* test for then block insns */
276 rtx true_prob_val; /* probability of else block */
277 rtx false_prob_val; /* probability of then block */
280 /* Find the conditional jump to the ELSE or JOIN part, and isolate
282 test_expr = cond_exec_get_condition (test_bb->end);
286 /* Collect the bounds of where we're to search. */
288 then_start = then_bb->head;
289 then_end = then_bb->end;
291 /* Skip a (use (const_int 0)) or branch as the final insn. */
292 if (GET_CODE (then_end) == INSN
293 && GET_CODE (PATTERN (then_end)) == USE
294 && GET_CODE (XEXP (PATTERN (then_end), 0)) == CONST_INT)
295 then_end = PREV_INSN (then_end);
296 else if (GET_CODE (then_end) == JUMP_INSN)
297 then_end = PREV_INSN (then_end);
301 /* Skip the ELSE block's label. */
302 else_start = NEXT_INSN (else_bb->head);
303 else_end = else_bb->end;
305 /* Skip a (use (const_int 0)) or branch as the final insn. */
306 if (GET_CODE (else_end) == INSN
307 && GET_CODE (PATTERN (else_end)) == USE
308 && GET_CODE (XEXP (PATTERN (else_end), 0)) == CONST_INT)
309 else_end = PREV_INSN (else_end);
310 else if (GET_CODE (else_end) == JUMP_INSN)
311 else_end = PREV_INSN (else_end);
314 /* How many instructions should we convert in total? */
318 max = 2 * MAX_CONDITIONAL_EXECUTE;
319 n_insns = count_bb_insns (else_bb);
322 max = MAX_CONDITIONAL_EXECUTE;
323 n_insns += count_bb_insns (then_bb);
327 /* Map test_expr/test_jump into the appropriate MD tests to use on
328 the conditionally executed code. */
330 true_expr = test_expr;
331 false_expr = gen_rtx_fmt_ee (reverse_condition (GET_CODE (true_expr)),
332 GET_MODE (true_expr), XEXP (true_expr, 0),
333 XEXP (true_expr, 1));
335 true_prob_val = find_reg_note (test_bb->end, REG_BR_PROB, NULL_RTX);
338 true_prob_val = XEXP (true_prob_val, 0);
339 false_prob_val = GEN_INT (REG_BR_PROB_BASE - INTVAL (true_prob_val));
342 false_prob_val = NULL_RTX;
344 /* For IF-THEN-ELSE blocks, we don't allow modifications of the test
345 on then THEN block. */
346 then_mod_ok = (else_bb == NULL_BLOCK);
348 /* Go through the THEN and ELSE blocks converting the insns if possible
349 to conditional execution. */
352 && ! cond_exec_process_insns (then_start, then_end,
353 false_expr, false_prob_val, then_mod_ok))
357 && ! cond_exec_process_insns (else_start, else_end,
358 true_expr, true_prob_val, TRUE))
361 if (! apply_change_group ())
364 /* Conversion succeeded. */
366 fprintf (rtl_dump_file, "%d insn%s converted to conditional execution.\n",
367 n_insns, (n_insns == 1) ? " was" : "s were");
369 /* Merge the blocks! */
370 merge_if_block (test_bb, then_bb, else_bb, join_bb);
378 /* Used by noce_process_if_block to communicate with its subroutines.
380 The subroutines know that A and B may be evaluated freely. They
381 know that X is a register. They should insert new instructions
382 before cond_earliest. */
388 rtx jump, cond, cond_earliest;
391 static rtx noce_emit_store_flag PARAMS ((struct noce_if_info *,
393 static int noce_try_store_flag PARAMS ((struct noce_if_info *));
394 static int noce_try_store_flag_inc PARAMS ((struct noce_if_info *));
395 static int noce_try_store_flag_constants PARAMS ((struct noce_if_info *));
396 static int noce_try_store_flag_mask PARAMS ((struct noce_if_info *));
397 static rtx noce_emit_cmove PARAMS ((struct noce_if_info *,
398 rtx, enum rtx_code, rtx,
400 static int noce_try_cmove PARAMS ((struct noce_if_info *));
401 static int noce_try_cmove_arith PARAMS ((struct noce_if_info *));
403 /* Helper function for noce_try_store_flag*. */
406 noce_emit_store_flag (if_info, x, reversep, normalize)
407 struct noce_if_info *if_info;
409 int reversep, normalize;
411 rtx cond = if_info->cond;
415 cond_complex = (! general_operand (XEXP (cond, 0), VOIDmode)
416 || ! general_operand (XEXP (cond, 1), VOIDmode));
418 /* If earliest == jump, or when the condition is complex, try to
419 build the store_flag insn directly. */
422 cond = XEXP (SET_SRC (PATTERN (if_info->jump)), 0);
424 if ((if_info->cond_earliest == if_info->jump || cond_complex)
425 && (normalize == 0 || STORE_FLAG_VALUE == normalize))
429 code = GET_CODE (cond);
431 code = reverse_condition (code);
433 tmp = gen_rtx_fmt_ee (code, GET_MODE (x), XEXP (cond, 0),
435 tmp = gen_rtx_SET (VOIDmode, x, tmp);
438 tmp = emit_insn (tmp);
440 if (recog_memoized (tmp) >= 0)
446 if_info->cond_earliest = if_info->jump;
454 /* Don't even try if the comparison operands are weird. */
458 code = GET_CODE (cond);
460 code = reverse_condition (code);
462 return emit_store_flag (x, code, XEXP (cond, 0),
463 XEXP (cond, 1), VOIDmode,
464 (code == LTU || code == LEU
465 || code == GEU || code == GTU), normalize);
468 /* Convert "if (test) x = 1; else x = 0".
470 Only try 0 and STORE_FLAG_VALUE here. Other combinations will be
471 tried in noce_try_store_flag_constants after noce_try_cmove has had
472 a go at the conversion. */
475 noce_try_store_flag (if_info)
476 struct noce_if_info *if_info;
481 if (GET_CODE (if_info->b) == CONST_INT
482 && INTVAL (if_info->b) == STORE_FLAG_VALUE
483 && if_info->a == const0_rtx)
485 else if (if_info->b == const0_rtx
486 && GET_CODE (if_info->a) == CONST_INT
487 && INTVAL (if_info->a) == STORE_FLAG_VALUE
488 && can_reverse_comparison_p (if_info->cond, if_info->jump))
495 target = noce_emit_store_flag (if_info, if_info->x, reversep, 0);
498 if (target != if_info->x)
499 emit_move_insn (if_info->x, target);
503 emit_insns_before (seq, if_info->cond_earliest);
514 /* Convert "if (test) x = a; else x = b", for A and B constant. */
517 noce_try_store_flag_constants (if_info)
518 struct noce_if_info *if_info;
522 HOST_WIDE_INT itrue, ifalse, diff, tmp;
523 int normalize, can_reverse;
526 && GET_CODE (if_info->a) == CONST_INT
527 && GET_CODE (if_info->b) == CONST_INT)
529 ifalse = INTVAL (if_info->a);
530 itrue = INTVAL (if_info->b);
531 diff = itrue - ifalse;
533 can_reverse = can_reverse_comparison_p (if_info->cond, if_info->jump);
536 if (diff == STORE_FLAG_VALUE || diff == -STORE_FLAG_VALUE)
538 else if (ifalse == 0 && exact_log2 (itrue) >= 0
539 && (STORE_FLAG_VALUE == 1
540 || BRANCH_COST >= 2))
542 else if (itrue == 0 && exact_log2 (ifalse) >= 0 && can_reverse
543 && (STORE_FLAG_VALUE == 1 || BRANCH_COST >= 2))
544 normalize = 1, reversep = 1;
546 && (STORE_FLAG_VALUE == -1
547 || BRANCH_COST >= 2))
549 else if (ifalse == -1 && can_reverse
550 && (STORE_FLAG_VALUE == -1 || BRANCH_COST >= 2))
551 normalize = -1, reversep = 1;
552 else if ((BRANCH_COST >= 2 && STORE_FLAG_VALUE == -1)
560 tmp = itrue; itrue = ifalse; ifalse = tmp;
565 target = noce_emit_store_flag (if_info, if_info->x, reversep, normalize);
572 /* if (test) x = 3; else x = 4;
573 => x = 3 + (test == 0); */
574 if (diff == STORE_FLAG_VALUE || diff == -STORE_FLAG_VALUE)
576 target = expand_binop (GET_MODE (if_info->x),
577 (diff == STORE_FLAG_VALUE
578 ? add_optab : sub_optab),
579 GEN_INT (ifalse), target, if_info->x, 0,
583 /* if (test) x = 8; else x = 0;
584 => x = (test != 0) << 3; */
585 else if (ifalse == 0 && (tmp = exact_log2 (itrue)) >= 0)
587 target = expand_binop (GET_MODE (if_info->x), ashl_optab,
588 target, GEN_INT (tmp), if_info->x, 0,
592 /* if (test) x = -1; else x = b;
593 => x = -(test != 0) | b; */
594 else if (itrue == -1)
596 target = expand_binop (GET_MODE (if_info->x), ior_optab,
597 target, GEN_INT (ifalse), if_info->x, 0,
601 /* if (test) x = a; else x = b;
602 => x = (-(test != 0) & (b - a)) + a; */
605 target = expand_binop (GET_MODE (if_info->x), and_optab,
606 target, GEN_INT (diff), if_info->x, 0,
609 target = expand_binop (GET_MODE (if_info->x), add_optab,
610 target, GEN_INT (ifalse), if_info->x, 0,
620 if (target != if_info->x)
621 emit_move_insn (if_info->x, target);
625 emit_insns_before (seq, if_info->cond_earliest);
633 /* Convert "if (test) foo++" into "foo += (test != 0)", and
634 similarly for "foo--". */
637 noce_try_store_flag_inc (if_info)
638 struct noce_if_info *if_info;
641 int subtract, normalize;
647 /* Should be no `else' case to worry about. */
648 && if_info->b == if_info->x
649 && GET_CODE (if_info->a) == PLUS
650 && (XEXP (if_info->a, 1) == const1_rtx
651 || XEXP (if_info->a, 1) == constm1_rtx)
652 && rtx_equal_p (XEXP (if_info->a, 0), if_info->x)
653 && can_reverse_comparison_p (if_info->cond, if_info->jump))
655 if (STORE_FLAG_VALUE == INTVAL (XEXP (if_info->a, 1)))
656 subtract = 0, normalize = 0;
657 else if (-STORE_FLAG_VALUE == INTVAL (XEXP (if_info->a, 1)))
658 subtract = 1, normalize = 0;
660 subtract = 0, normalize = INTVAL (XEXP (if_info->a, 1));
664 target = noce_emit_store_flag (if_info,
665 gen_reg_rtx (GET_MODE (if_info->x)),
669 target = expand_binop (GET_MODE (if_info->x),
670 subtract ? sub_optab : add_optab,
671 if_info->x, target, if_info->x, 0, OPTAB_WIDEN);
674 if (target != if_info->x)
675 emit_move_insn (if_info->x, target);
679 emit_insns_before (seq, if_info->cond_earliest);
690 /* Convert "if (test) x = 0;" to "x &= -(test == 0);" */
693 noce_try_store_flag_mask (if_info)
694 struct noce_if_info *if_info;
702 || STORE_FLAG_VALUE == -1)
703 && ((if_info->a == const0_rtx
704 && rtx_equal_p (if_info->b, if_info->x))
705 || ((reversep = can_reverse_comparison_p (if_info->cond,
707 && if_info->b == const0_rtx
708 && rtx_equal_p (if_info->a, if_info->x))))
711 target = noce_emit_store_flag (if_info,
712 gen_reg_rtx (GET_MODE (if_info->x)),
715 target = expand_binop (GET_MODE (if_info->x), and_optab,
716 if_info->x, target, if_info->x, 0,
721 if (target != if_info->x)
722 emit_move_insn (if_info->x, target);
726 emit_insns_before (seq, if_info->cond_earliest);
737 /* Helper function for noce_try_cmove and noce_try_cmove_arith. */
740 noce_emit_cmove (if_info, x, code, cmp_a, cmp_b, vfalse, vtrue)
741 struct noce_if_info *if_info;
742 rtx x, cmp_a, cmp_b, vfalse, vtrue;
745 /* If earliest == jump, try to build the cmove insn directly.
746 This is helpful when combine has created some complex condition
747 (like for alpha's cmovlbs) that we can't hope to regenerate
748 through the normal interface. */
750 if (if_info->cond_earliest == if_info->jump)
754 tmp = gen_rtx_fmt_ee (code, GET_MODE (if_info->cond), cmp_a, cmp_b);
755 tmp = gen_rtx_IF_THEN_ELSE (GET_MODE (x), tmp, vtrue, vfalse);
756 tmp = gen_rtx_SET (VOIDmode, x, tmp);
759 tmp = emit_insn (tmp);
761 if (recog_memoized (tmp) >= 0)
773 /* Don't even try if the comparison operands are weird. */
774 if (! general_operand (cmp_a, GET_MODE (cmp_a))
775 || ! general_operand (cmp_b, GET_MODE (cmp_b)))
778 #if HAVE_conditional_move
779 return emit_conditional_move (x, code, cmp_a, cmp_b, VOIDmode,
780 vtrue, vfalse, GET_MODE (x),
781 (code == LTU || code == GEU
782 || code == LEU || code == GTU));
784 /* We'll never get here, as noce_process_if_block doesn't call the
785 functions involved. Ifdef code, however, should be discouraged
786 because it leads to typos in the code not selected. However,
787 emit_conditional_move won't exist either. */
792 /* Try only simple constants and registers here. More complex cases
793 are handled in noce_try_cmove_arith after noce_try_store_flag_arith
794 has had a go at it. */
797 noce_try_cmove (if_info)
798 struct noce_if_info *if_info;
803 if ((CONSTANT_P (if_info->a) || register_operand (if_info->a, VOIDmode))
804 && (CONSTANT_P (if_info->b) || register_operand (if_info->b, VOIDmode)))
808 code = GET_CODE (if_info->cond);
809 target = noce_emit_cmove (if_info, if_info->x, code,
810 XEXP (if_info->cond, 0),
811 XEXP (if_info->cond, 1),
812 if_info->a, if_info->b);
816 if (target != if_info->x)
817 emit_move_insn (if_info->x, target);
821 emit_insns_before (seq, if_info->cond_earliest);
834 /* Try more complex cases involving conditional_move. */
837 noce_try_cmove_arith (if_info)
838 struct noce_if_info *if_info;
848 /* A conditional move from two memory sources is equivalent to a
849 conditional on their addresses followed by a load. Don't do this
850 early because it'll screw alias analysis. Note that we've
851 already checked for no side effects. */
852 if (! no_new_pseudos && cse_not_expected
853 && GET_CODE (a) == MEM && GET_CODE (b) == MEM
858 x = gen_reg_rtx (Pmode);
862 /* ??? We could handle this if we knew that a load from A or B could
863 not fault. This is also true if we've already loaded
864 from the address along the path from ENTRY. */
865 else if (may_trap_p (a) || may_trap_p (b))
868 /* if (test) x = a + b; else x = c - d;
875 code = GET_CODE (if_info->cond);
876 insn_a = if_info->insn_a;
877 insn_b = if_info->insn_b;
879 /* Possibly rearrange operands to make things come out more natural. */
880 if (can_reverse_comparison_p (if_info->cond, if_info->jump))
883 if (rtx_equal_p (b, x))
885 else if (general_operand (b, GET_MODE (b)))
890 code = reverse_condition (code);
891 tmp = a, a = b, b = tmp;
892 tmp = insn_a, insn_a = insn_b, insn_b = tmp;
898 /* If either operand is complex, load it into a register first.
899 The best way to do this is to copy the original insn. In this
900 way we preserve any clobbers etc that the insn may have had.
901 This is of course not possible in the IS_MEM case. */
902 if (! general_operand (a, GET_MODE (a)))
907 goto end_seq_and_fail;
911 tmp = gen_reg_rtx (GET_MODE (a));
912 tmp = emit_insn (gen_rtx_SET (VOIDmode, tmp, a));
915 goto end_seq_and_fail;
918 a = gen_reg_rtx (GET_MODE (a));
919 tmp = copy_rtx (insn_a);
920 set = single_set (tmp);
922 tmp = emit_insn (PATTERN (tmp));
924 if (recog_memoized (tmp) < 0)
925 goto end_seq_and_fail;
927 if (! general_operand (b, GET_MODE (b)))
932 goto end_seq_and_fail;
936 tmp = gen_reg_rtx (GET_MODE (b));
937 tmp = emit_insn (gen_rtx_SET (VOIDmode, tmp, b));
940 goto end_seq_and_fail;
943 b = gen_reg_rtx (GET_MODE (b));
944 tmp = copy_rtx (insn_b);
945 set = single_set (tmp);
947 tmp = emit_insn (PATTERN (tmp));
949 if (recog_memoized (tmp) < 0)
950 goto end_seq_and_fail;
953 target = noce_emit_cmove (if_info, x, code, XEXP (if_info->cond, 0),
954 XEXP (if_info->cond, 1), a, b);
957 goto end_seq_and_fail;
959 /* If we're handling a memory for above, emit the load now. */
962 tmp = gen_rtx_MEM (GET_MODE (if_info->x), target);
964 /* Copy over flags as appropriate. */
965 if (MEM_VOLATILE_P (if_info->a) || MEM_VOLATILE_P (if_info->b))
966 MEM_VOLATILE_P (tmp) = 1;
967 if (MEM_IN_STRUCT_P (if_info->a) && MEM_IN_STRUCT_P (if_info->b))
968 MEM_IN_STRUCT_P (tmp) = 1;
969 if (MEM_SCALAR_P (if_info->a) && MEM_SCALAR_P (if_info->b))
970 MEM_SCALAR_P (tmp) = 1;
971 if (MEM_ALIAS_SET (if_info->a) == MEM_ALIAS_SET (if_info->b))
972 MEM_ALIAS_SET (tmp) = MEM_ALIAS_SET (if_info->a);
974 emit_move_insn (if_info->x, tmp);
976 else if (target != x)
977 emit_move_insn (x, target);
981 emit_insns_before (tmp, if_info->cond_earliest);
989 /* Look for the condition for the jump first. We'd prefer to avoid
990 get_condition if we can -- it tries to look back for the contents
991 of an original compare. On targets that use normal integers for
992 comparisons, e.g. alpha, this is wasteful. */
995 noce_get_condition (jump, earliest)
1001 /* If the condition variable is a register and is MODE_INT, accept it.
1002 Otherwise, fall back on get_condition. */
1004 if (! condjump_p (jump))
1007 cond = XEXP (SET_SRC (PATTERN (jump)), 0);
1008 if (GET_CODE (XEXP (cond, 0)) == REG
1009 && GET_MODE_CLASS (GET_MODE (XEXP (cond, 0))) == MODE_INT)
1013 /* If this branches to JUMP_LABEL when the condition is false,
1014 reverse the condition. */
1015 if (GET_CODE (XEXP (SET_SRC (PATTERN (jump)), 2)) == LABEL_REF
1016 && XEXP (XEXP (SET_SRC (PATTERN (jump)), 2), 0) == JUMP_LABEL (jump))
1017 cond = gen_rtx_fmt_ee (reverse_condition (GET_CODE (cond)),
1018 GET_MODE (cond), XEXP (cond, 0),
1022 cond = get_condition (jump, earliest);
1027 /* Given a simple IF-THEN or IF-THEN-ELSE block, attempt to convert it
1028 without using conditional execution. Return TRUE if we were
1029 successful at converting the the block. */
1032 noce_process_if_block (test_bb, then_bb, else_bb, join_bb)
1033 basic_block test_bb; /* Basic block test is in */
1034 basic_block then_bb; /* Basic block for THEN block */
1035 basic_block else_bb; /* Basic block for ELSE block */
1036 basic_block join_bb; /* Basic block the join label is in */
1038 /* We're looking for patterns of the form
1040 (1) if (...) x = a; else x = b;
1041 (2) x = b; if (...) x = a;
1042 (3) if (...) x = a; // as if with an initial x = x.
1044 The later patterns require jumps to be more expensive.
1046 ??? For future expansion, look for multiple X in such patterns. */
1048 struct noce_if_info if_info;
1051 rtx orig_x, x, a, b;
1052 rtx jump, cond, insn;
1054 /* If this is not a standard conditional jump, we can't parse it. */
1055 jump = test_bb->end;
1056 cond = noce_get_condition (jump, &if_info.cond_earliest);
1060 /* We must be comparing objects whose modes imply the size. */
1061 if (GET_MODE (XEXP (cond, 0)) == BLKmode)
1064 /* Look for one of the potential sets. */
1065 insn_a = first_active_insn (then_bb);
1067 || ! last_active_insn_p (then_bb, insn_a)
1068 || (set_a = single_set (insn_a)) == NULL_RTX)
1071 x = SET_DEST (set_a);
1072 a = SET_SRC (set_a);
1074 /* Look for the other potential set. Make sure we've got equivalent
1076 /* ??? This is overconservative. Storing to two different mems is
1077 as easy as conditionally computing the address. Storing to a
1078 single mem merely requires a scratch memory to use as one of the
1079 destination addresses; often the memory immediately below the
1080 stack pointer is available for this. */
1084 insn_b = first_active_insn (else_bb);
1086 || ! last_active_insn_p (else_bb, insn_b)
1087 || (set_b = single_set (insn_b)) == NULL_RTX
1088 || ! rtx_equal_p (x, SET_DEST (set_b)))
1093 insn_b = prev_nonnote_insn (if_info.cond_earliest);
1095 || GET_CODE (insn_b) != INSN
1096 || (set_b = single_set (insn_b)) == NULL_RTX
1097 || ! rtx_equal_p (x, SET_DEST (set_b))
1098 || reg_mentioned_p (x, cond)
1099 || reg_mentioned_p (x, a)
1100 || reg_mentioned_p (x, SET_SRC (set_b)))
1101 insn_b = set_b = NULL_RTX;
1103 b = (set_b ? SET_SRC (set_b) : x);
1105 /* X may not be mentioned in the range (cond_earliest, jump]. */
1106 for (insn = jump; insn != if_info.cond_earliest; insn = PREV_INSN (insn))
1107 if (INSN_P (insn) && reg_mentioned_p (x, insn))
1110 /* A and B may not be modified in the range [cond_earliest, jump). */
1111 for (insn = if_info.cond_earliest; insn != jump; insn = NEXT_INSN (insn))
1113 && (modified_in_p (a, insn) || modified_in_p (b, insn)))
1116 /* Only operate on register destinations, and even then avoid extending
1117 the lifetime of hard registers on small register class machines. */
1119 if (GET_CODE (x) != REG
1120 || (SMALL_REGISTER_CLASSES
1121 && REGNO (x) < FIRST_PSEUDO_REGISTER))
1125 x = gen_reg_rtx (GET_MODE (x));
1128 /* Don't operate on sources that may trap or are volatile. */
1129 if (side_effects_p (a) || side_effects_p (b)
1130 || (GET_CODE (a) != MEM && may_trap_p (a))
1131 || (GET_CODE (b) != MEM && may_trap_p (b)))
1134 /* Set up the info block for our subroutines. */
1135 if_info.cond = cond;
1136 if_info.jump = jump;
1137 if_info.insn_a = insn_a;
1138 if_info.insn_b = insn_b;
1143 /* Try optimizations in some approximation of a useful order. */
1144 /* ??? Should first look to see if X is live incoming at all. If it
1145 isn't, we don't need anything but an unconditional set. */
1147 /* Look and see if A and B are really the same. Avoid creating silly
1148 cmove constructs that no one will fix up later. */
1149 if (rtx_equal_p (a, b))
1151 /* If we have an INSN_B, we don't have to create any new rtl. Just
1152 move the instruction that we already have. If we don't have an
1153 INSN_B, that means that A == X, and we've got a noop move. In
1154 that case don't do anything and let the code below delete INSN_A. */
1155 if (insn_b && else_bb)
1157 if (else_bb && insn_b == else_bb->end)
1158 else_bb->end = PREV_INSN (insn_b);
1159 reorder_insns (insn_b, insn_b, PREV_INSN (if_info.cond_earliest));
1166 if (noce_try_store_flag (&if_info))
1168 if (HAVE_conditional_move
1169 && noce_try_cmove (&if_info))
1171 if (! HAVE_conditional_execution)
1173 if (noce_try_store_flag_constants (&if_info))
1175 if (noce_try_store_flag_inc (&if_info))
1177 if (noce_try_store_flag_mask (&if_info))
1179 if (HAVE_conditional_move
1180 && noce_try_cmove_arith (&if_info))
1187 /* The original sets may now be killed. */
1188 if (insn_a == then_bb->end)
1189 then_bb->end = PREV_INSN (insn_a);
1190 flow_delete_insn (insn_a);
1192 /* Several special cases here: First, we may have reused insn_b above,
1193 in which case insn_b is now NULL. Second, we want to delete insn_b
1194 if it came from the ELSE block, because follows the now correct
1195 write that appears in the TEST block. However, if we got insn_b from
1196 the TEST block, it may in fact be loading data needed for the comparison.
1197 We'll let life_analysis remove the insn if it's really dead. */
1198 if (insn_b && else_bb)
1200 if (insn_b == else_bb->end)
1201 else_bb->end = PREV_INSN (insn_b);
1202 flow_delete_insn (insn_b);
1205 /* The new insns will have been inserted before cond_earliest. We should
1206 be able to remove the jump with impunity, but the condition itself may
1207 have been modified by gcse to be shared across basic blocks. */
1208 test_bb->end = PREV_INSN (jump);
1209 flow_delete_insn (jump);
1211 /* If we used a temporary, fix it up now. */
1215 emit_move_insn (orig_x, x);
1216 insn_b = gen_sequence ();
1219 test_bb->end = emit_insn_after (insn_b, test_bb->end);
1222 /* Merge the blocks! */
1223 merge_if_block (test_bb, then_bb, else_bb, join_bb);
1228 /* Attempt to convert an IF-THEN or IF-THEN-ELSE block into
1229 straight line code. Return true if successful. */
1232 process_if_block (test_bb, then_bb, else_bb, join_bb)
1233 basic_block test_bb; /* Basic block test is in */
1234 basic_block then_bb; /* Basic block for THEN block */
1235 basic_block else_bb; /* Basic block for ELSE block */
1236 basic_block join_bb; /* Basic block the join label is in */
1238 if (! reload_completed
1239 && noce_process_if_block (test_bb, then_bb, else_bb, join_bb))
1242 if (HAVE_conditional_execution
1244 && cond_exec_process_if_block (test_bb, then_bb, else_bb, join_bb))
1250 /* Merge the blocks and mark for local life update. */
1253 merge_if_block (test_bb, then_bb, else_bb, join_bb)
1254 basic_block test_bb; /* Basic block test is in */
1255 basic_block then_bb; /* Basic block for THEN block */
1256 basic_block else_bb; /* Basic block for ELSE block */
1257 basic_block join_bb; /* Basic block the join label is in */
1259 basic_block combo_bb;
1261 /* All block merging is done into the lower block numbers. */
1265 /* First merge TEST block into THEN block. This is a no-brainer since
1266 the THEN block did not have a code label to begin with. */
1268 if (combo_bb->global_live_at_end)
1269 COPY_REG_SET (combo_bb->global_live_at_end, then_bb->global_live_at_end);
1270 merge_blocks_nomove (combo_bb, then_bb);
1271 num_removed_blocks++;
1273 /* The ELSE block, if it existed, had a label. That label count
1274 will almost always be zero, but odd things can happen when labels
1275 get their addresses taken. */
1278 if (LABEL_NUSES (else_bb->head) == 0
1279 && ! LABEL_PRESERVE_P (else_bb->head)
1280 && ! LABEL_NAME (else_bb->head))
1282 /* We can merge the ELSE. */
1283 merge_blocks_nomove (combo_bb, else_bb);
1284 num_removed_blocks++;
1288 /* We cannot merge the ELSE. */
1290 /* Properly rewire the edge out of the now combined
1291 TEST-THEN block to point here. */
1292 remove_edge (combo_bb->succ);
1293 if (combo_bb->succ || else_bb->pred)
1295 make_edge (NULL, combo_bb, else_bb, EDGE_FALLTHRU);
1297 /* Remove the jump and cruft from the end of the TEST-THEN block. */
1298 tidy_fallthru_edge (combo_bb->succ, combo_bb, else_bb);
1300 /* Make sure we update life info properly. */
1301 SET_UPDATE_LIFE(combo_bb);
1302 if (else_bb->global_live_at_end)
1303 COPY_REG_SET (else_bb->global_live_at_start,
1304 else_bb->global_live_at_end);
1306 /* The ELSE is the new combo block. */
1311 /* If there was no join block reported, that means it was not adjacent
1312 to the others, and so we cannot merge them. */
1316 /* The outgoing edge for the current COMBO block should already
1317 be correct. Verify this. */
1318 if (combo_bb->succ == NULL_EDGE)
1321 /* There should sill be a branch at the end of the THEN or ELSE
1322 blocks taking us to our final destination. */
1323 if (! simplejump_p (combo_bb->end)
1324 && ! returnjump_p (combo_bb->end))
1328 /* The JOIN block had a label. It may have had quite a number
1329 of other predecessors too, but probably not. See if we can
1330 merge this with the others. */
1331 else if (LABEL_NUSES (join_bb->head) == 0
1332 && ! LABEL_PRESERVE_P (join_bb->head)
1333 && ! LABEL_NAME (join_bb->head))
1335 /* We can merge the JOIN. */
1336 if (combo_bb->global_live_at_end)
1337 COPY_REG_SET (combo_bb->global_live_at_end,
1338 join_bb->global_live_at_end);
1339 merge_blocks_nomove (combo_bb, join_bb);
1340 num_removed_blocks++;
1344 /* We cannot merge the JOIN. */
1346 /* The outgoing edge for the current COMBO block should already
1347 be correct. Verify this. */
1348 if (combo_bb->succ->succ_next != NULL_EDGE
1349 || combo_bb->succ->dest != join_bb)
1352 /* Remove the jump and cruft from the end of the COMBO block. */
1353 tidy_fallthru_edge (combo_bb->succ, combo_bb, join_bb);
1356 /* Make sure we update life info properly. */
1357 SET_UPDATE_LIFE (combo_bb);
1359 num_updated_if_blocks++;
1362 /* Find a block ending in a simple IF condition. Return TRUE if
1363 we were able to transform it in some way. */
1366 find_if_header (test_bb)
1367 basic_block test_bb;
1372 /* The kind of block we're looking for has exactly two successors. */
1373 if ((then_edge = test_bb->succ) == NULL_EDGE
1374 || (else_edge = then_edge->succ_next) == NULL_EDGE
1375 || else_edge->succ_next != NULL_EDGE)
1378 /* Neither edge should be abnormal. */
1379 if ((then_edge->flags & EDGE_COMPLEX)
1380 || (else_edge->flags & EDGE_COMPLEX))
1383 /* The THEN edge is canonically the one that falls through. */
1384 if (then_edge->flags & EDGE_FALLTHRU)
1386 else if (else_edge->flags & EDGE_FALLTHRU)
1389 else_edge = then_edge;
1393 /* Otherwise this must be a multiway branch of some sort. */
1396 if (find_if_block (test_bb, then_edge, else_edge))
1399 && (! HAVE_conditional_execution || reload_completed))
1401 if (find_if_case_1 (test_bb, then_edge, else_edge))
1403 if (find_if_case_2 (test_bb, then_edge, else_edge))
1411 fprintf (rtl_dump_file, "Conversion succeeded.\n");
1415 /* Determine if a given basic block heads a simple IF-THEN or IF-THEN-ELSE
1416 block. If so, we'll try to convert the insns to not require the branch.
1417 Return TRUE if we were successful at converting the the block. */
1420 find_if_block (test_bb, then_edge, else_edge)
1421 basic_block test_bb;
1422 edge then_edge, else_edge;
1424 basic_block then_bb = then_edge->dest;
1425 basic_block else_bb = else_edge->dest;
1426 basic_block join_bb = NULL_BLOCK;
1427 edge then_succ = then_bb->succ;
1428 edge else_succ = else_bb->succ;
1431 /* The THEN block of an IF-THEN combo must have exactly one predecessor. */
1432 if (then_bb->pred->pred_next != NULL_EDGE)
1435 /* The THEN block of an IF-THEN combo must have exactly one successor. */
1436 if (then_succ == NULL_EDGE
1437 || then_succ->succ_next != NULL_EDGE
1438 || (then_succ->flags & EDGE_COMPLEX))
1441 /* The THEN block may not start with a label, as might happen with an
1442 unused user label that has had its address taken. */
1443 if (GET_CODE (then_bb->head) == CODE_LABEL)
1446 /* If the THEN block's successor is the other edge out of the TEST block,
1447 then we have an IF-THEN combo without an ELSE. */
1448 if (then_succ->dest == else_bb)
1451 else_bb = NULL_BLOCK;
1454 /* If the THEN and ELSE block meet in a subsequent block, and the ELSE
1455 has exactly one predecessor and one successor, and the outgoing edge
1456 is not complex, then we have an IF-THEN-ELSE combo. */
1457 else if (else_succ != NULL_EDGE
1458 && then_succ->dest == else_succ->dest
1459 && else_bb->pred->pred_next == NULL_EDGE
1460 && else_succ->succ_next == NULL_EDGE
1461 && ! (else_succ->flags & EDGE_COMPLEX))
1462 join_bb = else_succ->dest;
1464 /* Otherwise it is not an IF-THEN or IF-THEN-ELSE combination. */
1468 num_possible_if_blocks++;
1473 fprintf (rtl_dump_file,
1474 "\nIF-THEN-ELSE block found, start %d, then %d, else %d, join %d\n",
1475 test_bb->index, then_bb->index, else_bb->index,
1478 fprintf (rtl_dump_file,
1479 "\nIF-THEN block found, start %d, then %d, join %d\n",
1480 test_bb->index, then_bb->index, join_bb->index);
1483 /* Make sure IF, THEN, and ELSE, blocks are adjacent. Actually, we
1484 get the first condition for free, since we've already asserted that
1485 there's a fallthru edge from IF to THEN. */
1486 /* ??? As an enhancement, move the ELSE block. Have to deal with EH and
1487 BLOCK notes, if by no other means than aborting the merge if they
1488 exist. Sticky enough I don't want to think about it now. */
1489 next_index = then_bb->index;
1490 if (else_bb && ++next_index != else_bb->index)
1492 if (++next_index != join_bb->index)
1500 /* Do the real work. */
1501 return process_if_block (test_bb, then_bb, else_bb, join_bb);
1504 /* Look for IF-THEN-ELSE cases in which one of THEN or ELSE is
1505 transformable, but not necessarily the other. There need be no
1508 Return TRUE if we were successful at converting the the block.
1510 Cases we'd like to look at:
1513 if (test) goto over; // x not live
1521 if (! test) goto label;
1524 if (test) goto E; // x not live
1538 (3) // This one's really only interesting for targets that can do
1539 // multiway branching, e.g. IA-64 BBB bundles. For other targets
1540 // it results in multiple branches on a cache line, which often
1541 // does not sit well with predictors.
1543 if (test1) goto E; // predicted not taken
1559 (A) Don't do (2) if the branch is predicted against the block we're
1560 eliminating. Do it anyway if we can eliminate a branch; this requires
1561 that the sole successor of the eliminated block postdominate the other
1564 (B) With CE, on (3) we can steal from both sides of the if, creating
1573 Again, this is most useful if J postdominates.
1575 (C) CE substitutes for helpful life information.
1577 (D) These heuristics need a lot of work. */
1579 /* Tests for case 1 above. */
1582 find_if_case_1 (test_bb, then_edge, else_edge)
1583 basic_block test_bb;
1584 edge then_edge, else_edge;
1586 basic_block then_bb = then_edge->dest;
1587 basic_block else_bb = else_edge->dest;
1588 edge then_succ = then_bb->succ;
1591 /* THEN has one successor. */
1592 if (!then_succ || then_succ->succ_next != NULL)
1595 /* THEN does not fall through, but is not strange either. */
1596 if (then_succ->flags & (EDGE_COMPLEX | EDGE_FALLTHRU))
1599 /* THEN has one predecessor. */
1600 if (then_bb->pred->pred_next != NULL)
1603 /* THEN has no label. */
1604 if (GET_CODE (then_bb->head) == CODE_LABEL)
1607 /* ELSE follows THEN. (??? could be moved) */
1608 if (else_bb->index != then_bb->index + 1)
1611 num_possible_if_blocks++;
1613 fprintf (rtl_dump_file,
1614 "\nIF-CASE-1 found, start %d, then %d\n",
1615 test_bb->index, then_bb->index);
1617 /* THEN is small. */
1618 if (count_bb_insns (then_bb) > BRANCH_COST)
1621 /* Find the label for THEN's destination. */
1622 if (then_succ->dest == EXIT_BLOCK_PTR)
1626 new_lab = JUMP_LABEL (then_bb->end);
1631 /* Registers set are dead, or are predicable. */
1632 if (! dead_or_predicable (test_bb, then_bb, else_bb, new_lab, 1))
1635 /* Conversion went ok, including moving the insns and fixing up the
1636 jump. Adjust the CFG to match. */
1638 SET_UPDATE_LIFE (test_bb);
1639 bitmap_operation (test_bb->global_live_at_end,
1640 else_bb->global_live_at_start,
1641 then_bb->global_live_at_end, BITMAP_IOR);
1643 make_edge (NULL, test_bb, then_succ->dest, 0);
1644 flow_delete_block (then_bb);
1645 tidy_fallthru_edge (else_edge, test_bb, else_bb);
1647 num_removed_blocks++;
1648 num_updated_if_blocks++;
1653 /* Test for case 2 above. */
1656 find_if_case_2 (test_bb, then_edge, else_edge)
1657 basic_block test_bb;
1658 edge then_edge, else_edge;
1660 basic_block then_bb = then_edge->dest;
1661 basic_block else_bb = else_edge->dest;
1662 edge else_succ = else_bb->succ;
1665 /* ELSE has one successor. */
1666 if (!else_succ || else_succ->succ_next != NULL)
1669 /* ELSE outgoing edge is not complex. */
1670 if (else_succ->flags & EDGE_COMPLEX)
1673 /* ELSE has one predecessor. */
1674 if (else_bb->pred->pred_next != NULL)
1677 /* ELSE has a label we can delete. */
1678 if (LABEL_NUSES (else_bb->head) > 1
1679 || LABEL_PRESERVE_P (else_bb->head)
1680 || LABEL_NAME (else_bb->head))
1683 /* ELSE is predicted or SUCC(ELSE) postdominates THEN. */
1684 note = find_reg_note (test_bb->end, REG_BR_PROB, NULL_RTX);
1685 if (note && INTVAL (XEXP (note, 0)) >= REG_BR_PROB_BASE / 2)
1687 else if (else_succ->dest->index < 0
1688 || (then_bb->index >= 0
1689 && TEST_BIT (post_dominators[ORIG_INDEX (then_bb)],
1690 ORIG_INDEX (else_succ->dest))))
1695 num_possible_if_blocks++;
1697 fprintf (rtl_dump_file,
1698 "\nIF-CASE-2 found, start %d, else %d\n",
1699 test_bb->index, else_bb->index);
1701 /* ELSE is small. */
1702 if (count_bb_insns (then_bb) > BRANCH_COST)
1705 /* Find the label for ELSE's destination. */
1706 if (else_succ->dest == EXIT_BLOCK_PTR)
1710 if (else_succ->flags & EDGE_FALLTHRU)
1712 new_lab = else_succ->dest->head;
1713 if (GET_CODE (new_lab) != CODE_LABEL)
1718 new_lab = JUMP_LABEL (else_bb->end);
1724 /* Registers set are dead, or are predicable. */
1725 if (! dead_or_predicable (test_bb, else_bb, then_bb, new_lab, 0))
1728 /* Conversion went ok, including moving the insns and fixing up the
1729 jump. Adjust the CFG to match. */
1731 SET_UPDATE_LIFE (test_bb);
1732 bitmap_operation (test_bb->global_live_at_end,
1733 then_bb->global_live_at_start,
1734 else_bb->global_live_at_end, BITMAP_IOR);
1736 remove_edge (else_edge);
1737 make_edge (NULL, test_bb, else_succ->dest, 0);
1738 flow_delete_block (else_bb);
1740 num_removed_blocks++;
1741 num_updated_if_blocks++;
1743 /* ??? We may now fallthru from one of THEN's successors into a join
1744 block. Rerun cleanup_cfg? Examine things manually? Wait? */
1749 /* A subroutine of dead_or_predicable called through for_each_rtx.
1750 Return 1 if a memory is found. */
1753 find_memory (px, data)
1755 void *data ATTRIBUTE_UNUSED;
1757 return GET_CODE (*px) == MEM;
1760 /* Used by the code above to perform the actual rtl transformations.
1761 Return TRUE if successful.
1763 TEST_BB is the block containing the conditional branch. MERGE_BB
1764 is the block containing the code to manipulate. NEW_DEST is the
1765 label TEST_BB should be branching to after the conversion.
1766 REVERSEP is true if the sense of the branch should be reversed. */
1769 dead_or_predicable (test_bb, merge_bb, other_bb, new_dest, reversep)
1770 basic_block test_bb, merge_bb, other_bb;
1774 rtx head, end, jump, earliest, old_dest;
1776 jump = test_bb->end;
1778 /* Find the extent of the real code in the merge block. */
1779 head = merge_bb->head;
1780 end = merge_bb->end;
1782 if (GET_CODE (head) == CODE_LABEL)
1783 head = NEXT_INSN (head);
1784 if (GET_CODE (head) == NOTE)
1788 head = end = NULL_RTX;
1791 head = NEXT_INSN (head);
1794 if (GET_CODE (end) == JUMP_INSN)
1798 head = end = NULL_RTX;
1801 end = PREV_INSN (end);
1804 if (HAVE_conditional_execution)
1806 /* In the conditional execution case, we have things easy. We know
1807 the condition is reversable. We don't have to check life info,
1808 becase we're going to conditionally execute the code anyway.
1809 All that's left is making sure the insns involved can actually
1814 cond = cond_exec_get_condition (jump);
1816 prob_val = find_reg_note (jump, REG_BR_PROB, NULL_RTX);
1818 prob_val = XEXP (prob_val, 0);
1822 cond = gen_rtx_fmt_ee (reverse_condition (GET_CODE (cond)),
1823 GET_MODE (cond), XEXP (cond, 0),
1826 prob_val = GEN_INT (REG_BR_PROB_BASE - INTVAL (prob_val));
1829 if (! cond_exec_process_insns (head, end, cond, prob_val, 0))
1836 /* In the non-conditional execution case, we have to verify that there
1837 are no trapping operations, no calls, no references to memory, and
1838 that any registers modified are dead at the branch site. */
1840 rtx insn, cond, prev;
1841 regset_head merge_set_head, tmp_head, test_live_head, test_set_head;
1842 regset merge_set, tmp, test_live, test_set;
1843 struct propagate_block_info *pbi;
1846 /* Check for no calls or trapping operations. */
1847 for (insn = head; ; insn = NEXT_INSN (insn))
1849 if (GET_CODE (insn) == CALL_INSN)
1853 if (may_trap_p (PATTERN (insn)))
1856 /* ??? Even non-trapping memories such as stack frame
1857 references must be avoided. For stores, we collect
1858 no lifetime info; for reads, we'd have to assert
1859 true_dependance false against every store in the
1861 if (for_each_rtx (&PATTERN (insn), find_memory, NULL))
1868 if (! condjump_p (jump))
1871 /* Find the extent of the conditional. */
1872 cond = noce_get_condition (jump, &earliest);
1877 MERGE_SET = set of registers set in MERGE_BB
1878 TEST_LIVE = set of registers live at EARLIEST
1879 TEST_SET = set of registers set between EARLIEST and the
1880 end of the block. */
1882 tmp = INITIALIZE_REG_SET (tmp_head);
1883 merge_set = INITIALIZE_REG_SET (merge_set_head);
1884 test_live = INITIALIZE_REG_SET (test_live_head);
1885 test_set = INITIALIZE_REG_SET (test_set_head);
1887 /* ??? bb->local_set is only valid during calculate_global_regs_live,
1888 so we must recompute usage for MERGE_BB. Not so bad, I suppose,
1889 since we've already asserted that MERGE_BB is small. */
1890 propagate_block (merge_bb, tmp, merge_set, 0);
1892 /* For small register class machines, don't lengthen lifetimes of
1893 hard registers before reload. */
1894 if (SMALL_REGISTER_CLASSES && ! reload_completed)
1896 EXECUTE_IF_SET_IN_BITMAP
1899 if (i < FIRST_PSEUDO_REGISTER
1901 && ! global_regs[i])
1906 /* For TEST, we're interested in a range of insns, not a whole block.
1907 Moreover, we're interested in the insns live from OTHER_BB. */
1909 COPY_REG_SET (test_live, other_bb->global_live_at_start);
1910 pbi = init_propagate_block_info (test_bb, test_live, test_set, 0);
1912 for (insn = jump; ; insn = prev)
1914 prev = propagate_one_insn (pbi, insn);
1915 if (insn == earliest)
1919 free_propagate_block_info (pbi);
1921 /* We can perform the transformation if
1922 MERGE_SET & (TEST_SET | TEST_LIVE)
1924 TEST_SET & merge_bb->global_live_at_start
1927 bitmap_operation (tmp, test_set, test_live, BITMAP_IOR);
1928 bitmap_operation (tmp, tmp, merge_set, BITMAP_AND);
1929 EXECUTE_IF_SET_IN_BITMAP(tmp, 0, i, fail = 1);
1931 bitmap_operation (tmp, test_set, merge_bb->global_live_at_start,
1933 EXECUTE_IF_SET_IN_BITMAP(tmp, 0, i, fail = 1);
1936 FREE_REG_SET (merge_set);
1937 FREE_REG_SET (test_live);
1938 FREE_REG_SET (test_set);
1945 /* We don't want to use normal invert_jump or redirect_jump because
1946 we don't want to delete_insn called. Also, we want to do our own
1947 change group management. */
1949 old_dest = JUMP_LABEL (jump);
1951 ? ! invert_jump_1 (jump, new_dest)
1952 : ! redirect_jump_1 (jump, new_dest))
1955 if (! apply_change_group ())
1959 LABEL_NUSES (old_dest) -= 1;
1961 LABEL_NUSES (new_dest) += 1;
1962 JUMP_LABEL (jump) = new_dest;
1966 rtx note = find_reg_note (jump, REG_BR_PROB, NULL_RTX);
1968 XEXP (note, 0) = GEN_INT (REG_BR_PROB_BASE - INTVAL (XEXP (note, 0)));
1971 /* Move the insns out of MERGE_BB to before the branch. */
1974 if (end == merge_bb->end)
1975 merge_bb->end = PREV_INSN (head);
1977 head = squeeze_notes (head, end);
1978 if (GET_CODE (end) == NOTE
1979 && (NOTE_LINE_NUMBER (end) == NOTE_INSN_BLOCK_END
1980 || NOTE_LINE_NUMBER (end) == NOTE_INSN_BLOCK_BEG
1981 || NOTE_LINE_NUMBER (end) == NOTE_INSN_LOOP_BEG
1982 || NOTE_LINE_NUMBER (end) == NOTE_INSN_LOOP_END
1983 || NOTE_LINE_NUMBER (end) == NOTE_INSN_LOOP_CONT
1984 || NOTE_LINE_NUMBER (end) == NOTE_INSN_LOOP_VTOP))
1988 end = PREV_INSN (end);
1991 reorder_insns (head, end, PREV_INSN (earliest));
2000 /* Main entry point for all if-conversion. */
2003 if_convert (life_data_ok)
2008 num_possible_if_blocks = 0;
2009 num_updated_if_blocks = 0;
2010 num_removed_blocks = 0;
2012 /* Free up basic_block_for_insn so that we don't have to keep it
2013 up to date, either here or in merge_blocks_nomove. */
2014 free_basic_block_vars (1);
2016 /* Compute postdominators if we think we'll use them. */
2017 post_dominators = NULL;
2018 if (HAVE_conditional_execution || life_data_ok)
2020 post_dominators = sbitmap_vector_alloc (n_basic_blocks, n_basic_blocks);
2021 compute_flow_dominators (NULL, post_dominators);
2024 /* Record initial block numbers. */
2025 for (block_num = 0; block_num < n_basic_blocks; block_num++)
2026 SET_ORIG_INDEX (BASIC_BLOCK (block_num), block_num);
2028 /* Go through each of the basic blocks looking for things to convert. */
2029 for (block_num = 0; block_num < n_basic_blocks; )
2031 basic_block bb = BASIC_BLOCK (block_num);
2032 if (find_if_header (bb))
2033 block_num = bb->index;
2038 sbitmap_vector_free (post_dominators);
2041 fflush (rtl_dump_file);
2043 /* Rebuild basic_block_for_insn for update_life_info and for gcse. */
2044 compute_bb_for_insn (get_max_uid ());
2046 /* Rebuild life info for basic blocks that require it. */
2047 if (num_removed_blocks && life_data_ok)
2049 sbitmap update_life_blocks = sbitmap_alloc (n_basic_blocks);
2050 sbitmap_zero (update_life_blocks);
2052 /* If we allocated new pseudos, we must resize the array for sched1. */
2053 if (max_regno < max_reg_num ())
2055 max_regno = max_reg_num ();
2056 allocate_reg_info (max_regno, FALSE, FALSE);
2059 for (block_num = 0; block_num < n_basic_blocks; block_num++)
2060 if (UPDATE_LIFE (BASIC_BLOCK (block_num)))
2061 SET_BIT (update_life_blocks, block_num);
2063 count_or_remove_death_notes (update_life_blocks, 1);
2064 update_life_info (update_life_blocks, UPDATE_LIFE_LOCAL,
2067 sbitmap_free (update_life_blocks);
2070 /* Write the final stats. */
2071 if (rtl_dump_file && num_possible_if_blocks > 0)
2073 fprintf (rtl_dump_file,
2074 "\n%d possible IF blocks searched.\n",
2075 num_possible_if_blocks);
2076 fprintf (rtl_dump_file,
2077 "%d IF blocks converted.\n",
2078 num_updated_if_blocks);
2079 fprintf (rtl_dump_file,
2080 "%d basic blocks deleted.\n\n\n",
2081 num_removed_blocks);
2084 #ifdef ENABLE_CHECKING
2085 verify_flow_info ();