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 merge_blocks_nomove (combo_bb, else_bb);
1279 num_removed_blocks++;
1282 /* If there was no join block reported, that means it was not adjacent
1283 to the others, and so we cannot merge them. */
1287 /* The outgoing edge for the current COMBO block should already
1288 be correct. Verify this. */
1289 if (combo_bb->succ == NULL_EDGE)
1292 /* There should sill be a branch at the end of the THEN or ELSE
1293 blocks taking us to our final destination. */
1294 if (! simplejump_p (combo_bb->end)
1295 && ! returnjump_p (combo_bb->end))
1299 /* The JOIN block may have had quite a number of other predecessors too.
1300 Since we've already merged the TEST, THEN and ELSE blocks, we should
1301 have only one remaining edge from our if-then-else diamond. If there
1302 is more than one remaining edge, it must come from elsewhere. */
1303 else if (join_bb->pred->pred_next == NULL)
1305 /* We can merge the JOIN. */
1306 if (combo_bb->global_live_at_end)
1307 COPY_REG_SET (combo_bb->global_live_at_end,
1308 join_bb->global_live_at_end);
1309 merge_blocks_nomove (combo_bb, join_bb);
1310 num_removed_blocks++;
1314 /* We cannot merge the JOIN. */
1316 /* The outgoing edge for the current COMBO block should already
1317 be correct. Verify this. */
1318 if (combo_bb->succ->succ_next != NULL_EDGE
1319 || combo_bb->succ->dest != join_bb)
1322 /* Remove the jump and cruft from the end of the COMBO block. */
1323 tidy_fallthru_edge (combo_bb->succ, combo_bb, join_bb);
1326 /* Make sure we update life info properly. */
1327 SET_UPDATE_LIFE (combo_bb);
1329 num_updated_if_blocks++;
1332 /* Find a block ending in a simple IF condition. Return TRUE if
1333 we were able to transform it in some way. */
1336 find_if_header (test_bb)
1337 basic_block test_bb;
1342 /* The kind of block we're looking for has exactly two successors. */
1343 if ((then_edge = test_bb->succ) == NULL_EDGE
1344 || (else_edge = then_edge->succ_next) == NULL_EDGE
1345 || else_edge->succ_next != NULL_EDGE)
1348 /* Neither edge should be abnormal. */
1349 if ((then_edge->flags & EDGE_COMPLEX)
1350 || (else_edge->flags & EDGE_COMPLEX))
1353 /* The THEN edge is canonically the one that falls through. */
1354 if (then_edge->flags & EDGE_FALLTHRU)
1356 else if (else_edge->flags & EDGE_FALLTHRU)
1359 else_edge = then_edge;
1363 /* Otherwise this must be a multiway branch of some sort. */
1366 if (find_if_block (test_bb, then_edge, else_edge))
1369 && (! HAVE_conditional_execution || reload_completed))
1371 if (find_if_case_1 (test_bb, then_edge, else_edge))
1373 if (find_if_case_2 (test_bb, then_edge, else_edge))
1381 fprintf (rtl_dump_file, "Conversion succeeded.\n");
1385 /* Determine if a given basic block heads a simple IF-THEN or IF-THEN-ELSE
1386 block. If so, we'll try to convert the insns to not require the branch.
1387 Return TRUE if we were successful at converting the the block. */
1390 find_if_block (test_bb, then_edge, else_edge)
1391 basic_block test_bb;
1392 edge then_edge, else_edge;
1394 basic_block then_bb = then_edge->dest;
1395 basic_block else_bb = else_edge->dest;
1396 basic_block join_bb = NULL_BLOCK;
1397 edge then_succ = then_bb->succ;
1398 edge else_succ = else_bb->succ;
1401 /* The THEN block of an IF-THEN combo must have exactly one predecessor. */
1402 if (then_bb->pred->pred_next != NULL_EDGE)
1405 /* The THEN block of an IF-THEN combo must have exactly one successor. */
1406 if (then_succ == NULL_EDGE
1407 || then_succ->succ_next != NULL_EDGE
1408 || (then_succ->flags & EDGE_COMPLEX))
1411 /* If the THEN block's successor is the other edge out of the TEST block,
1412 then we have an IF-THEN combo without an ELSE. */
1413 if (then_succ->dest == else_bb)
1416 else_bb = NULL_BLOCK;
1419 /* If the THEN and ELSE block meet in a subsequent block, and the ELSE
1420 has exactly one predecessor and one successor, and the outgoing edge
1421 is not complex, then we have an IF-THEN-ELSE combo. */
1422 else if (else_succ != NULL_EDGE
1423 && then_succ->dest == else_succ->dest
1424 && else_bb->pred->pred_next == NULL_EDGE
1425 && else_succ->succ_next == NULL_EDGE
1426 && ! (else_succ->flags & EDGE_COMPLEX))
1427 join_bb = else_succ->dest;
1429 /* Otherwise it is not an IF-THEN or IF-THEN-ELSE combination. */
1433 num_possible_if_blocks++;
1438 fprintf (rtl_dump_file,
1439 "\nIF-THEN-ELSE block found, start %d, then %d, else %d, join %d\n",
1440 test_bb->index, then_bb->index, else_bb->index,
1443 fprintf (rtl_dump_file,
1444 "\nIF-THEN block found, start %d, then %d, join %d\n",
1445 test_bb->index, then_bb->index, join_bb->index);
1448 /* Make sure IF, THEN, and ELSE, blocks are adjacent. Actually, we
1449 get the first condition for free, since we've already asserted that
1450 there's a fallthru edge from IF to THEN. */
1451 /* ??? As an enhancement, move the ELSE block. Have to deal with EH and
1452 BLOCK notes, if by no other means than aborting the merge if they
1453 exist. Sticky enough I don't want to think about it now. */
1454 next_index = then_bb->index;
1455 if (else_bb && ++next_index != else_bb->index)
1457 if (++next_index != join_bb->index)
1465 /* Do the real work. */
1466 return process_if_block (test_bb, then_bb, else_bb, join_bb);
1469 /* Look for IF-THEN-ELSE cases in which one of THEN or ELSE is
1470 transformable, but not necessarily the other. There need be no
1473 Return TRUE if we were successful at converting the the block.
1475 Cases we'd like to look at:
1478 if (test) goto over; // x not live
1486 if (! test) goto label;
1489 if (test) goto E; // x not live
1503 (3) // This one's really only interesting for targets that can do
1504 // multiway branching, e.g. IA-64 BBB bundles. For other targets
1505 // it results in multiple branches on a cache line, which often
1506 // does not sit well with predictors.
1508 if (test1) goto E; // predicted not taken
1524 (A) Don't do (2) if the branch is predicted against the block we're
1525 eliminating. Do it anyway if we can eliminate a branch; this requires
1526 that the sole successor of the eliminated block postdominate the other
1529 (B) With CE, on (3) we can steal from both sides of the if, creating
1538 Again, this is most useful if J postdominates.
1540 (C) CE substitutes for helpful life information.
1542 (D) These heuristics need a lot of work. */
1544 /* Tests for case 1 above. */
1547 find_if_case_1 (test_bb, then_edge, else_edge)
1548 basic_block test_bb;
1549 edge then_edge, else_edge;
1551 basic_block then_bb = then_edge->dest;
1552 basic_block else_bb = else_edge->dest;
1553 edge then_succ = then_bb->succ;
1556 /* THEN has one successor. */
1557 if (!then_succ || then_succ->succ_next != NULL)
1560 /* THEN does not fall through, but is not strange either. */
1561 if (then_succ->flags & (EDGE_COMPLEX | EDGE_FALLTHRU))
1564 /* THEN has one predecessor. */
1565 if (then_bb->pred->pred_next != NULL)
1568 /* ELSE follows THEN. (??? could be moved) */
1569 if (else_bb->index != then_bb->index + 1)
1572 num_possible_if_blocks++;
1574 fprintf (rtl_dump_file,
1575 "\nIF-CASE-1 found, start %d, then %d\n",
1576 test_bb->index, then_bb->index);
1578 /* THEN is small. */
1579 if (count_bb_insns (then_bb) > BRANCH_COST)
1582 /* Find the label for THEN's destination. */
1583 if (then_succ->dest == EXIT_BLOCK_PTR)
1587 new_lab = JUMP_LABEL (then_bb->end);
1592 /* Registers set are dead, or are predicable. */
1593 if (! dead_or_predicable (test_bb, then_bb, else_bb, new_lab, 1))
1596 /* Conversion went ok, including moving the insns and fixing up the
1597 jump. Adjust the CFG to match. */
1599 SET_UPDATE_LIFE (test_bb);
1600 bitmap_operation (test_bb->global_live_at_end,
1601 else_bb->global_live_at_start,
1602 then_bb->global_live_at_end, BITMAP_IOR);
1604 make_edge (NULL, test_bb, then_succ->dest, 0);
1605 flow_delete_block (then_bb);
1606 tidy_fallthru_edge (else_edge, test_bb, else_bb);
1608 num_removed_blocks++;
1609 num_updated_if_blocks++;
1614 /* Test for case 2 above. */
1617 find_if_case_2 (test_bb, then_edge, else_edge)
1618 basic_block test_bb;
1619 edge then_edge, else_edge;
1621 basic_block then_bb = then_edge->dest;
1622 basic_block else_bb = else_edge->dest;
1623 edge else_succ = else_bb->succ;
1626 /* ELSE has one successor. */
1627 if (!else_succ || else_succ->succ_next != NULL)
1630 /* ELSE outgoing edge is not complex. */
1631 if (else_succ->flags & EDGE_COMPLEX)
1634 /* ELSE has one predecessor. */
1635 if (else_bb->pred->pred_next != NULL)
1638 /* ELSE is predicted or SUCC(ELSE) postdominates THEN. */
1639 note = find_reg_note (test_bb->end, REG_BR_PROB, NULL_RTX);
1640 if (note && INTVAL (XEXP (note, 0)) >= REG_BR_PROB_BASE / 2)
1642 else if (else_succ->dest->index < 0
1643 || (then_bb->index >= 0
1644 && TEST_BIT (post_dominators[ORIG_INDEX (then_bb)],
1645 ORIG_INDEX (else_succ->dest))))
1650 num_possible_if_blocks++;
1652 fprintf (rtl_dump_file,
1653 "\nIF-CASE-2 found, start %d, else %d\n",
1654 test_bb->index, else_bb->index);
1656 /* ELSE is small. */
1657 if (count_bb_insns (then_bb) > BRANCH_COST)
1660 /* Find the label for ELSE's destination. */
1661 if (else_succ->dest == EXIT_BLOCK_PTR)
1665 if (else_succ->flags & EDGE_FALLTHRU)
1667 new_lab = else_succ->dest->head;
1668 if (GET_CODE (new_lab) != CODE_LABEL)
1673 new_lab = JUMP_LABEL (else_bb->end);
1679 /* Registers set are dead, or are predicable. */
1680 if (! dead_or_predicable (test_bb, else_bb, then_bb, new_lab, 0))
1683 /* Conversion went ok, including moving the insns and fixing up the
1684 jump. Adjust the CFG to match. */
1686 SET_UPDATE_LIFE (test_bb);
1687 bitmap_operation (test_bb->global_live_at_end,
1688 then_bb->global_live_at_start,
1689 else_bb->global_live_at_end, BITMAP_IOR);
1691 remove_edge (else_edge);
1692 make_edge (NULL, test_bb, else_succ->dest, 0);
1693 flow_delete_block (else_bb);
1695 num_removed_blocks++;
1696 num_updated_if_blocks++;
1698 /* ??? We may now fallthru from one of THEN's successors into a join
1699 block. Rerun cleanup_cfg? Examine things manually? Wait? */
1704 /* A subroutine of dead_or_predicable called through for_each_rtx.
1705 Return 1 if a memory is found. */
1708 find_memory (px, data)
1710 void *data ATTRIBUTE_UNUSED;
1712 return GET_CODE (*px) == MEM;
1715 /* Used by the code above to perform the actual rtl transformations.
1716 Return TRUE if successful.
1718 TEST_BB is the block containing the conditional branch. MERGE_BB
1719 is the block containing the code to manipulate. NEW_DEST is the
1720 label TEST_BB should be branching to after the conversion.
1721 REVERSEP is true if the sense of the branch should be reversed. */
1724 dead_or_predicable (test_bb, merge_bb, other_bb, new_dest, reversep)
1725 basic_block test_bb, merge_bb, other_bb;
1729 rtx head, end, jump, earliest, old_dest;
1731 jump = test_bb->end;
1733 /* Find the extent of the real code in the merge block. */
1734 head = merge_bb->head;
1735 end = merge_bb->end;
1737 if (GET_CODE (head) == CODE_LABEL)
1738 head = NEXT_INSN (head);
1739 if (GET_CODE (head) == NOTE)
1743 head = end = NULL_RTX;
1746 head = NEXT_INSN (head);
1749 if (GET_CODE (end) == JUMP_INSN)
1753 head = end = NULL_RTX;
1756 end = PREV_INSN (end);
1759 if (HAVE_conditional_execution)
1761 /* In the conditional execution case, we have things easy. We know
1762 the condition is reversable. We don't have to check life info,
1763 becase we're going to conditionally execute the code anyway.
1764 All that's left is making sure the insns involved can actually
1769 cond = cond_exec_get_condition (jump);
1771 prob_val = find_reg_note (jump, REG_BR_PROB, NULL_RTX);
1773 prob_val = XEXP (prob_val, 0);
1777 cond = gen_rtx_fmt_ee (reverse_condition (GET_CODE (cond)),
1778 GET_MODE (cond), XEXP (cond, 0),
1781 prob_val = GEN_INT (REG_BR_PROB_BASE - INTVAL (prob_val));
1784 if (! cond_exec_process_insns (head, end, cond, prob_val, 0))
1791 /* In the non-conditional execution case, we have to verify that there
1792 are no trapping operations, no calls, no references to memory, and
1793 that any registers modified are dead at the branch site. */
1795 rtx insn, cond, prev;
1796 regset_head merge_set_head, tmp_head, test_live_head, test_set_head;
1797 regset merge_set, tmp, test_live, test_set;
1798 struct propagate_block_info *pbi;
1801 /* Check for no calls or trapping operations. */
1802 for (insn = head; ; insn = NEXT_INSN (insn))
1804 if (GET_CODE (insn) == CALL_INSN)
1808 if (may_trap_p (PATTERN (insn)))
1811 /* ??? Even non-trapping memories such as stack frame
1812 references must be avoided. For stores, we collect
1813 no lifetime info; for reads, we'd have to assert
1814 true_dependance false against every store in the
1816 if (for_each_rtx (&PATTERN (insn), find_memory, NULL))
1823 if (! condjump_p (jump))
1826 /* Find the extent of the conditional. */
1827 cond = noce_get_condition (jump, &earliest);
1832 MERGE_SET = set of registers set in MERGE_BB
1833 TEST_LIVE = set of registers live at EARLIEST
1834 TEST_SET = set of registers set between EARLIEST and the
1835 end of the block. */
1837 tmp = INITIALIZE_REG_SET (tmp_head);
1838 merge_set = INITIALIZE_REG_SET (merge_set_head);
1839 test_live = INITIALIZE_REG_SET (test_live_head);
1840 test_set = INITIALIZE_REG_SET (test_set_head);
1842 /* ??? bb->local_set is only valid during calculate_global_regs_live,
1843 so we must recompute usage for MERGE_BB. Not so bad, I suppose,
1844 since we've already asserted that MERGE_BB is small. */
1845 propagate_block (merge_bb, tmp, merge_set, 0);
1847 /* For small register class machines, don't lengthen lifetimes of
1848 hard registers before reload. */
1849 if (SMALL_REGISTER_CLASSES && ! reload_completed)
1851 EXECUTE_IF_SET_IN_BITMAP
1854 if (i < FIRST_PSEUDO_REGISTER
1856 && ! global_regs[i])
1861 /* For TEST, we're interested in a range of insns, not a whole block.
1862 Moreover, we're interested in the insns live from OTHER_BB. */
1864 COPY_REG_SET (test_live, other_bb->global_live_at_start);
1865 pbi = init_propagate_block_info (test_bb, test_live, test_set, 0);
1867 for (insn = jump; ; insn = prev)
1869 prev = propagate_one_insn (pbi, insn);
1870 if (insn == earliest)
1874 free_propagate_block_info (pbi);
1876 /* We can perform the transformation if
1877 MERGE_SET & (TEST_SET | TEST_LIVE)
1879 TEST_SET & merge_bb->global_live_at_start
1882 bitmap_operation (tmp, test_set, test_live, BITMAP_IOR);
1883 bitmap_operation (tmp, tmp, merge_set, BITMAP_AND);
1884 EXECUTE_IF_SET_IN_BITMAP(tmp, 0, i, fail = 1);
1886 bitmap_operation (tmp, test_set, merge_bb->global_live_at_start,
1888 EXECUTE_IF_SET_IN_BITMAP(tmp, 0, i, fail = 1);
1891 FREE_REG_SET (merge_set);
1892 FREE_REG_SET (test_live);
1893 FREE_REG_SET (test_set);
1900 /* We don't want to use normal invert_jump or redirect_jump because
1901 we don't want to delete_insn called. Also, we want to do our own
1902 change group management. */
1904 old_dest = JUMP_LABEL (jump);
1906 ? ! invert_jump_1 (jump, new_dest)
1907 : ! redirect_jump_1 (jump, new_dest))
1910 if (! apply_change_group ())
1914 LABEL_NUSES (old_dest) -= 1;
1916 LABEL_NUSES (new_dest) += 1;
1917 JUMP_LABEL (jump) = new_dest;
1921 rtx note = find_reg_note (jump, REG_BR_PROB, NULL_RTX);
1923 XEXP (note, 0) = GEN_INT (REG_BR_PROB_BASE - INTVAL (XEXP (note, 0)));
1926 /* Move the insns out of MERGE_BB to before the branch. */
1929 if (end == merge_bb->end)
1930 merge_bb->end = PREV_INSN (head);
1932 head = squeeze_notes (head, end);
1933 if (GET_CODE (end) == NOTE
1934 && (NOTE_LINE_NUMBER (end) == NOTE_INSN_BLOCK_END
1935 || NOTE_LINE_NUMBER (end) == NOTE_INSN_BLOCK_BEG
1936 || NOTE_LINE_NUMBER (end) == NOTE_INSN_LOOP_BEG
1937 || NOTE_LINE_NUMBER (end) == NOTE_INSN_LOOP_END
1938 || NOTE_LINE_NUMBER (end) == NOTE_INSN_LOOP_CONT
1939 || NOTE_LINE_NUMBER (end) == NOTE_INSN_LOOP_VTOP))
1943 end = PREV_INSN (end);
1946 reorder_insns (head, end, PREV_INSN (earliest));
1955 /* Main entry point for all if-conversion. */
1958 if_convert (life_data_ok)
1963 num_possible_if_blocks = 0;
1964 num_updated_if_blocks = 0;
1965 num_removed_blocks = 0;
1967 /* Free up basic_block_for_insn so that we don't have to keep it
1968 up to date, either here or in merge_blocks_nomove. */
1969 free_basic_block_vars (1);
1971 /* Compute postdominators if we think we'll use them. */
1972 post_dominators = NULL;
1973 if (HAVE_conditional_execution || life_data_ok)
1975 post_dominators = sbitmap_vector_alloc (n_basic_blocks, n_basic_blocks);
1976 compute_flow_dominators (NULL, post_dominators);
1979 /* Record initial block numbers. */
1980 for (block_num = 0; block_num < n_basic_blocks; block_num++)
1981 SET_ORIG_INDEX (BASIC_BLOCK (block_num), block_num);
1983 /* Go through each of the basic blocks looking for things to convert. */
1984 for (block_num = 0; block_num < n_basic_blocks; )
1986 basic_block bb = BASIC_BLOCK (block_num);
1987 if (find_if_header (bb))
1988 block_num = bb->index;
1993 sbitmap_vector_free (post_dominators);
1996 fflush (rtl_dump_file);
1998 /* Rebuild basic_block_for_insn for update_life_info and for gcse. */
1999 compute_bb_for_insn (get_max_uid ());
2001 /* Rebuild life info for basic blocks that require it. */
2002 if (num_removed_blocks && life_data_ok)
2004 sbitmap update_life_blocks = sbitmap_alloc (n_basic_blocks);
2005 sbitmap_zero (update_life_blocks);
2007 /* If we allocated new pseudos, we must resize the array for sched1. */
2008 if (max_regno < max_reg_num ())
2010 max_regno = max_reg_num ();
2011 allocate_reg_info (max_regno, FALSE, FALSE);
2014 for (block_num = 0; block_num < n_basic_blocks; block_num++)
2015 if (UPDATE_LIFE (BASIC_BLOCK (block_num)))
2016 SET_BIT (update_life_blocks, block_num);
2018 count_or_remove_death_notes (update_life_blocks, 1);
2019 update_life_info (update_life_blocks, UPDATE_LIFE_LOCAL,
2022 sbitmap_free (update_life_blocks);
2025 /* Write the final stats. */
2026 if (rtl_dump_file && num_possible_if_blocks > 0)
2028 fprintf (rtl_dump_file,
2029 "\n%d possible IF blocks searched.\n",
2030 num_possible_if_blocks);
2031 fprintf (rtl_dump_file,
2032 "%d IF blocks converted.\n",
2033 num_updated_if_blocks);
2034 fprintf (rtl_dump_file,
2035 "%d basic blocks deleted.\n\n\n",
2036 num_removed_blocks);
2039 #ifdef ENABLE_CHECKING
2040 verify_flow_info ();