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
, 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
, 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 int mod_ok
; /* true if modifications ok last insn. */
188 int must_be_last
= FALSE
;
191 for (insn
= start
; ; insn
= NEXT_INSN (insn
))
193 if (GET_CODE (insn
) == NOTE
)
196 if (GET_CODE (insn
) != INSN
&& GET_CODE (insn
) != CALL_INSN
)
199 /* Last insn wasn't last? */
203 if (modified_in_p (test
, insn
))
210 /* Now build the conditional form of the instruction. */
211 validate_change (insn
, &PATTERN (insn
),
212 gen_rtx_COND_EXEC (VOIDmode
, copy_rtx (test
),
223 /* Return the condition for a jump. Do not do any special processing. */
226 cond_exec_get_condition (jump
)
231 if (condjump_p (jump
))
232 test_if
= SET_SRC (PATTERN (jump
));
233 else if (condjump_in_parallel_p (jump
))
234 test_if
= SET_SRC (XVECEXP (PATTERN (jump
), 0, 0));
237 cond
= XEXP (test_if
, 0);
239 /* If this branches to JUMP_LABEL when the condition is false,
240 reverse the condition. */
241 if (GET_CODE (XEXP (test_if
, 2)) == LABEL_REF
242 && XEXP (XEXP (test_if
, 2), 0) == JUMP_LABEL (jump
))
243 cond
= gen_rtx_fmt_ee (reverse_condition (GET_CODE (cond
)),
244 GET_MODE (cond
), XEXP (cond
, 0),
250 /* Given a simple IF-THEN or IF-THEN-ELSE block, attempt to convert it
251 to conditional execution. Return TRUE if we were successful at
252 converting the the block. */
255 cond_exec_process_if_block (test_bb
, then_bb
, else_bb
, join_bb
)
256 basic_block test_bb
; /* Basic block test is in */
257 basic_block then_bb
; /* Basic block for THEN block */
258 basic_block else_bb
; /* Basic block for ELSE block */
259 basic_block join_bb
; /* Basic block the join label is in */
261 rtx test_expr
; /* expression in IF_THEN_ELSE that is tested */
262 rtx then_start
; /* first insn in THEN block */
263 rtx then_end
; /* last insn + 1 in THEN block */
264 rtx else_start
; /* first insn in ELSE block or NULL */
265 rtx else_end
; /* last insn + 1 in ELSE block */
266 int max
; /* max # of insns to convert. */
267 int then_mod_ok
; /* whether conditional mods are ok in THEN */
268 rtx true_expr
; /* test for else block insns */
269 rtx false_expr
; /* test for then block insns */
272 /* Find the conditional jump to the ELSE or JOIN part, and isolate
274 test_expr
= cond_exec_get_condition (test_bb
->end
);
278 /* Collect the bounds of where we're to search. */
280 then_start
= then_bb
->head
;
281 then_end
= then_bb
->end
;
283 /* Skip a (use (const_int 0)) or branch as the final insn. */
284 if (GET_CODE (then_end
) == INSN
285 && GET_CODE (PATTERN (then_end
)) == USE
286 && GET_CODE (XEXP (PATTERN (then_end
), 0)) == CONST_INT
)
287 then_end
= PREV_INSN (then_end
);
288 else if (GET_CODE (then_end
) == JUMP_INSN
)
289 then_end
= PREV_INSN (then_end
);
293 /* Skip the ELSE block's label. */
294 else_start
= NEXT_INSN (else_bb
->head
);
295 else_end
= else_bb
->end
;
297 /* Skip a (use (const_int 0)) or branch as the final insn. */
298 if (GET_CODE (else_end
) == INSN
299 && GET_CODE (PATTERN (else_end
)) == USE
300 && GET_CODE (XEXP (PATTERN (else_end
), 0)) == CONST_INT
)
301 else_end
= PREV_INSN (else_end
);
302 else if (GET_CODE (else_end
) == JUMP_INSN
)
303 else_end
= PREV_INSN (else_end
);
306 /* How many instructions should we convert in total? */
310 max
= 2 * MAX_CONDITIONAL_EXECUTE
;
311 n_insns
= count_bb_insns (else_bb
);
314 max
= MAX_CONDITIONAL_EXECUTE
;
315 n_insns
+= count_bb_insns (then_bb
);
319 /* Map test_expr/test_jump into the appropriate MD tests to use on
320 the conditionally executed code. */
322 true_expr
= test_expr
;
323 false_expr
= gen_rtx_fmt_ee (reverse_condition (GET_CODE (true_expr
)),
324 GET_MODE (true_expr
), XEXP (true_expr
, 0),
325 XEXP (true_expr
, 1));
327 /* For IF-THEN-ELSE blocks, we don't allow modifications of the test
328 on then THEN block. */
329 then_mod_ok
= (else_bb
== NULL_BLOCK
);
331 /* Go through the THEN and ELSE blocks converting the insns if possible
332 to conditional execution. */
335 && ! cond_exec_process_insns (then_start
, then_end
,
336 false_expr
, then_mod_ok
))
340 && ! cond_exec_process_insns (else_start
, else_end
,
344 if (! apply_change_group ())
347 /* Conversion succeeded. */
349 fprintf (rtl_dump_file
, "%d insn%s converted to conditional execution.\n",
350 n_insns
, (n_insns
== 1) ? " was" : "s were");
352 /* Merge the blocks! */
353 merge_if_block (test_bb
, then_bb
, else_bb
, join_bb
);
361 /* Used by noce_process_if_block to communicate with its subroutines.
363 The subroutines know that A and B may be evaluated freely. They
364 know that X is a register. They should insert new instructions
365 before cond_earliest. */
371 rtx jump
, cond
, cond_earliest
;
374 static rtx noce_emit_store_flag
PARAMS ((struct noce_if_info
*,
376 static int noce_try_store_flag
PARAMS ((struct noce_if_info
*));
377 static int noce_try_store_flag_inc
PARAMS ((struct noce_if_info
*));
378 static int noce_try_store_flag_constants
PARAMS ((struct noce_if_info
*));
379 static int noce_try_store_flag_mask
PARAMS ((struct noce_if_info
*));
380 static rtx noce_emit_cmove
PARAMS ((struct noce_if_info
*,
381 rtx
, enum rtx_code
, rtx
,
383 static int noce_try_cmove
PARAMS ((struct noce_if_info
*));
384 static int noce_try_cmove_arith
PARAMS ((struct noce_if_info
*));
386 /* Helper function for noce_try_store_flag*. */
389 noce_emit_store_flag (if_info
, x
, reversep
, normalize
)
390 struct noce_if_info
*if_info
;
392 int reversep
, normalize
;
394 rtx cond
= if_info
->cond
;
398 cond_complex
= (! general_operand (XEXP (cond
, 0), VOIDmode
)
399 || ! general_operand (XEXP (cond
, 1), VOIDmode
));
401 /* If earliest == jump, or when the condition is complex, try to
402 build the store_flag insn directly. */
405 cond
= XEXP (SET_SRC (PATTERN (if_info
->jump
)), 0);
407 if ((if_info
->cond_earliest
== if_info
->jump
|| cond_complex
)
408 && (normalize
== 0 || STORE_FLAG_VALUE
== normalize
))
412 code
= GET_CODE (cond
);
414 code
= reverse_condition (code
);
416 tmp
= gen_rtx_fmt_ee (code
, GET_MODE (x
), XEXP (cond
, 0),
418 tmp
= gen_rtx_SET (VOIDmode
, x
, tmp
);
421 tmp
= emit_insn (tmp
);
423 if (recog_memoized (tmp
) >= 0)
429 if_info
->cond_earliest
= if_info
->jump
;
437 /* Don't even try if the comparison operands are weird. */
441 code
= GET_CODE (cond
);
443 code
= reverse_condition (code
);
445 return emit_store_flag (x
, code
, XEXP (cond
, 0),
446 XEXP (cond
, 1), VOIDmode
,
447 (code
== LTU
|| code
== LEU
448 || code
== GEU
|| code
== GTU
), normalize
);
451 /* Convert "if (test) x = 1; else x = 0".
453 Only try 0 and STORE_FLAG_VALUE here. Other combinations will be
454 tried in noce_try_store_flag_constants after noce_try_cmove has had
455 a go at the conversion. */
458 noce_try_store_flag (if_info
)
459 struct noce_if_info
*if_info
;
464 if (GET_CODE (if_info
->b
) == CONST_INT
465 && INTVAL (if_info
->b
) == STORE_FLAG_VALUE
466 && if_info
->a
== const0_rtx
)
468 else if (if_info
->b
== const0_rtx
469 && GET_CODE (if_info
->a
) == CONST_INT
470 && INTVAL (if_info
->a
) == STORE_FLAG_VALUE
471 && can_reverse_comparison_p (if_info
->cond
, if_info
->jump
))
478 target
= noce_emit_store_flag (if_info
, if_info
->x
, reversep
, 0);
481 if (target
!= if_info
->x
)
482 emit_move_insn (if_info
->x
, target
);
486 emit_insns_before (seq
, if_info
->cond_earliest
);
497 /* Convert "if (test) x = a; else x = b", for A and B constant. */
500 noce_try_store_flag_constants (if_info
)
501 struct noce_if_info
*if_info
;
505 HOST_WIDE_INT itrue
, ifalse
, diff
, tmp
;
506 int normalize
, can_reverse
;
509 && GET_CODE (if_info
->a
) == CONST_INT
510 && GET_CODE (if_info
->b
) == CONST_INT
)
512 ifalse
= INTVAL (if_info
->a
);
513 itrue
= INTVAL (if_info
->b
);
514 diff
= itrue
- ifalse
;
516 can_reverse
= can_reverse_comparison_p (if_info
->cond
, if_info
->jump
);
519 if (diff
== STORE_FLAG_VALUE
|| diff
== -STORE_FLAG_VALUE
)
521 else if (ifalse
== 0 && exact_log2 (itrue
) >= 0
522 && (STORE_FLAG_VALUE
== 1
523 || BRANCH_COST
>= 2))
525 else if (itrue
== 0 && exact_log2 (ifalse
) >= 0 && can_reverse
526 && (STORE_FLAG_VALUE
== 1 || BRANCH_COST
>= 2))
527 normalize
= 1, reversep
= 1;
529 && (STORE_FLAG_VALUE
== -1
530 || BRANCH_COST
>= 2))
532 else if (ifalse
== -1 && can_reverse
533 && (STORE_FLAG_VALUE
== -1 || BRANCH_COST
>= 2))
534 normalize
= -1, reversep
= 1;
535 else if ((BRANCH_COST
>= 2 && STORE_FLAG_VALUE
== -1)
543 tmp
= itrue
; itrue
= ifalse
; ifalse
= tmp
;
548 target
= noce_emit_store_flag (if_info
, if_info
->x
, reversep
, normalize
);
555 /* if (test) x = 3; else x = 4;
556 => x = 3 + (test == 0); */
557 if (diff
== STORE_FLAG_VALUE
|| diff
== -STORE_FLAG_VALUE
)
559 target
= expand_binop (GET_MODE (if_info
->x
),
560 (diff
== STORE_FLAG_VALUE
561 ? add_optab
: sub_optab
),
562 GEN_INT (ifalse
), target
, if_info
->x
, 0,
566 /* if (test) x = 8; else x = 0;
567 => x = (test != 0) << 3; */
568 else if (ifalse
== 0 && (tmp
= exact_log2 (itrue
)) >= 0)
570 target
= expand_binop (GET_MODE (if_info
->x
), ashl_optab
,
571 target
, GEN_INT (tmp
), if_info
->x
, 0,
575 /* if (test) x = -1; else x = b;
576 => x = -(test != 0) | b; */
577 else if (itrue
== -1)
579 target
= expand_binop (GET_MODE (if_info
->x
), ior_optab
,
580 target
, GEN_INT (ifalse
), if_info
->x
, 0,
584 /* if (test) x = a; else x = b;
585 => x = (-(test != 0) & (b - a)) + a; */
588 target
= expand_binop (GET_MODE (if_info
->x
), and_optab
,
589 target
, GEN_INT (diff
), if_info
->x
, 0,
592 target
= expand_binop (GET_MODE (if_info
->x
), add_optab
,
593 target
, GEN_INT (ifalse
), if_info
->x
, 0,
603 if (target
!= if_info
->x
)
604 emit_move_insn (if_info
->x
, target
);
608 emit_insns_before (seq
, if_info
->cond_earliest
);
616 /* Convert "if (test) foo++" into "foo += (test != 0)", and
617 similarly for "foo--". */
620 noce_try_store_flag_inc (if_info
)
621 struct noce_if_info
*if_info
;
624 int subtract
, normalize
;
630 /* Should be no `else' case to worry about. */
631 && if_info
->b
== if_info
->x
632 && GET_CODE (if_info
->a
) == PLUS
633 && (XEXP (if_info
->a
, 1) == const1_rtx
634 || XEXP (if_info
->a
, 1) == constm1_rtx
)
635 && rtx_equal_p (XEXP (if_info
->a
, 0), if_info
->x
)
636 && can_reverse_comparison_p (if_info
->cond
, if_info
->jump
))
638 if (STORE_FLAG_VALUE
== INTVAL (XEXP (if_info
->a
, 1)))
639 subtract
= 0, normalize
= 0;
640 else if (-STORE_FLAG_VALUE
== INTVAL (XEXP (if_info
->a
, 1)))
641 subtract
= 1, normalize
= 0;
643 subtract
= 0, normalize
= INTVAL (XEXP (if_info
->a
, 1));
647 target
= noce_emit_store_flag (if_info
,
648 gen_reg_rtx (GET_MODE (if_info
->x
)),
652 target
= expand_binop (GET_MODE (if_info
->x
),
653 subtract
? sub_optab
: add_optab
,
654 if_info
->x
, target
, if_info
->x
, 0, OPTAB_WIDEN
);
657 if (target
!= if_info
->x
)
658 emit_move_insn (if_info
->x
, target
);
662 emit_insns_before (seq
, if_info
->cond_earliest
);
673 /* Convert "if (test) x = 0;" to "x &= -(test == 0);" */
676 noce_try_store_flag_mask (if_info
)
677 struct noce_if_info
*if_info
;
685 || STORE_FLAG_VALUE
== -1)
686 && ((if_info
->a
== const0_rtx
687 && rtx_equal_p (if_info
->b
, if_info
->x
))
688 || ((reversep
= can_reverse_comparison_p (if_info
->cond
,
690 && if_info
->b
== const0_rtx
691 && rtx_equal_p (if_info
->a
, if_info
->x
))))
694 target
= noce_emit_store_flag (if_info
,
695 gen_reg_rtx (GET_MODE (if_info
->x
)),
698 target
= expand_binop (GET_MODE (if_info
->x
), and_optab
,
699 if_info
->x
, target
, if_info
->x
, 0,
704 if (target
!= if_info
->x
)
705 emit_move_insn (if_info
->x
, target
);
709 emit_insns_before (seq
, if_info
->cond_earliest
);
720 /* Helper function for noce_try_cmove and noce_try_cmove_arith. */
723 noce_emit_cmove (if_info
, x
, code
, cmp_a
, cmp_b
, vfalse
, vtrue
)
724 struct noce_if_info
*if_info
;
725 rtx x
, cmp_a
, cmp_b
, vfalse
, vtrue
;
728 /* If earliest == jump, try to build the cmove insn directly.
729 This is helpful when combine has created some complex condition
730 (like for alpha's cmovlbs) that we can't hope to regenerate
731 through the normal interface. */
733 if (if_info
->cond_earliest
== if_info
->jump
)
737 tmp
= gen_rtx_fmt_ee (code
, GET_MODE (if_info
->cond
), cmp_a
, cmp_b
);
738 tmp
= gen_rtx_IF_THEN_ELSE (GET_MODE (x
), tmp
, vtrue
, vfalse
);
739 tmp
= gen_rtx_SET (VOIDmode
, x
, tmp
);
742 tmp
= emit_insn (tmp
);
744 if (recog_memoized (tmp
) >= 0)
756 /* Don't even try if the comparison operands are weird. */
757 if (! general_operand (cmp_a
, GET_MODE (cmp_a
))
758 || ! general_operand (cmp_b
, GET_MODE (cmp_b
)))
761 return emit_conditional_move (x
, code
, cmp_a
, cmp_b
, VOIDmode
,
762 vtrue
, vfalse
, GET_MODE (x
),
763 (code
== LTU
|| code
== GEU
764 || code
== LEU
|| code
== GTU
));
767 /* Try only simple constants and registers here. More complex cases
768 are handled in noce_try_cmove_arith after noce_try_store_flag_arith
769 has had a go at it. */
772 noce_try_cmove (if_info
)
773 struct noce_if_info
*if_info
;
778 if ((CONSTANT_P (if_info
->a
) || register_operand (if_info
->a
, VOIDmode
))
779 && (CONSTANT_P (if_info
->b
) || register_operand (if_info
->b
, VOIDmode
)))
783 code
= GET_CODE (if_info
->cond
);
784 target
= noce_emit_cmove (if_info
, if_info
->x
, code
,
785 XEXP (if_info
->cond
, 0),
786 XEXP (if_info
->cond
, 1),
787 if_info
->a
, if_info
->b
);
791 if (target
!= if_info
->x
)
792 emit_move_insn (if_info
->x
, target
);
796 emit_insns_before (seq
, if_info
->cond_earliest
);
809 /* Try more complex cases involving conditional_move. */
812 noce_try_cmove_arith (if_info
)
813 struct noce_if_info
*if_info
;
823 /* A conditional move from two memory sources is equivalent to a
824 conditional on their addresses followed by a load. Don't do this
825 early because it'll screw alias analysis. Note that we've
826 already checked for no side effects. */
827 if (! no_new_pseudos
&& cse_not_expected
828 && GET_CODE (a
) == MEM
&& GET_CODE (b
) == MEM
833 x
= gen_reg_rtx (Pmode
);
837 /* ??? We could handle this if we knew that a load from A or B could
838 not fault. This is true of stack memories or if we've already loaded
839 from the address along the path from ENTRY. */
840 else if (GET_CODE (a
) == MEM
|| GET_CODE (b
) == MEM
)
843 /* if (test) x = a + b; else x = c - d;
850 code
= GET_CODE (if_info
->cond
);
851 insn_a
= if_info
->insn_a
;
852 insn_b
= if_info
->insn_b
;
854 /* Possibly rearrange operands to make things come out more natural. */
855 if (can_reverse_comparison_p (if_info
->cond
, if_info
->jump
))
858 if (rtx_equal_p (b
, x
))
860 else if (general_operand (b
, GET_MODE (b
)))
865 code
= reverse_condition (code
);
866 tmp
= a
, a
= b
, b
= tmp
;
867 tmp
= insn_a
, insn_a
= insn_b
, insn_b
= tmp
;
873 /* If either operand is complex, load it into a register first.
874 The best way to do this is to copy the original insn. In this
875 way we preserve any clobbers etc that the insn may have had.
876 This is of course not possible in the IS_MEM case. */
877 if (! general_operand (a
, GET_MODE (a
)))
882 goto end_seq_and_fail
;
886 tmp
= gen_reg_rtx (GET_MODE (a
));
887 tmp
= emit_insn (gen_rtx_SET (VOIDmode
, tmp
, a
));
890 goto end_seq_and_fail
;
893 a
= gen_reg_rtx (GET_MODE (a
));
894 tmp
= copy_rtx (insn_a
);
895 set
= single_set (tmp
);
897 tmp
= emit_insn (PATTERN (tmp
));
899 if (recog_memoized (tmp
) < 0)
900 goto end_seq_and_fail
;
902 if (! general_operand (b
, GET_MODE (b
)))
907 goto end_seq_and_fail
;
911 tmp
= gen_reg_rtx (GET_MODE (b
));
912 tmp
= emit_insn (gen_rtx_SET (VOIDmode
, tmp
, b
));
915 goto end_seq_and_fail
;
918 b
= gen_reg_rtx (GET_MODE (b
));
919 tmp
= copy_rtx (insn_b
);
920 set
= single_set (tmp
);
922 tmp
= emit_insn (PATTERN (tmp
));
924 if (recog_memoized (tmp
) < 0)
925 goto end_seq_and_fail
;
928 target
= noce_emit_cmove (if_info
, x
, code
, XEXP (if_info
->cond
, 0),
929 XEXP (if_info
->cond
, 1), a
, b
);
932 goto end_seq_and_fail
;
934 /* If we're handling a memory for above, emit the load now. */
937 tmp
= gen_rtx_MEM (GET_MODE (if_info
->x
), target
);
939 /* Copy over flags as appropriate. */
940 if (MEM_VOLATILE_P (if_info
->a
) || MEM_VOLATILE_P (if_info
->b
))
941 MEM_VOLATILE_P (tmp
) = 1;
942 if (MEM_IN_STRUCT_P (if_info
->a
) && MEM_IN_STRUCT_P (if_info
->b
))
943 MEM_IN_STRUCT_P (tmp
) = 1;
944 if (MEM_SCALAR_P (if_info
->a
) && MEM_SCALAR_P (if_info
->b
))
945 MEM_SCALAR_P (tmp
) = 1;
946 if (MEM_ALIAS_SET (if_info
->a
) == MEM_ALIAS_SET (if_info
->b
))
947 MEM_ALIAS_SET (tmp
) = MEM_ALIAS_SET (if_info
->a
);
949 emit_move_insn (if_info
->x
, tmp
);
951 else if (target
!= x
)
952 emit_move_insn (x
, target
);
956 emit_insns_before (tmp
, if_info
->cond_earliest
);
964 /* Look for the condition for the jump first. We'd prefer to avoid
965 get_condition if we can -- it tries to look back for the contents
966 of an original compare. On targets that use normal integers for
967 comparisons, e.g. alpha, this is wasteful. */
970 noce_get_condition (jump
, earliest
)
976 /* If the condition variable is a register and is MODE_INT, accept it.
977 Otherwise, fall back on get_condition. */
979 if (! condjump_p (jump
))
982 cond
= XEXP (SET_SRC (PATTERN (jump
)), 0);
983 if (GET_CODE (XEXP (cond
, 0)) == REG
984 && GET_MODE_CLASS (GET_MODE (XEXP (cond
, 0))) == MODE_INT
)
988 /* If this branches to JUMP_LABEL when the condition is false,
989 reverse the condition. */
990 if (GET_CODE (XEXP (SET_SRC (PATTERN (jump
)), 2)) == LABEL_REF
991 && XEXP (XEXP (SET_SRC (PATTERN (jump
)), 2), 0) == JUMP_LABEL (jump
))
992 cond
= gen_rtx_fmt_ee (reverse_condition (GET_CODE (cond
)),
993 GET_MODE (cond
), XEXP (cond
, 0),
997 cond
= get_condition (jump
, earliest
);
1002 /* Given a simple IF-THEN or IF-THEN-ELSE block, attempt to convert it
1003 without using conditional execution. Return TRUE if we were
1004 successful at converting the the block. */
1007 noce_process_if_block (test_bb
, then_bb
, else_bb
, join_bb
)
1008 basic_block test_bb
; /* Basic block test is in */
1009 basic_block then_bb
; /* Basic block for THEN block */
1010 basic_block else_bb
; /* Basic block for ELSE block */
1011 basic_block join_bb
; /* Basic block the join label is in */
1013 /* We're looking for patterns of the form
1015 (1) if (...) x = a; else x = b;
1016 (2) x = b; if (...) x = a;
1017 (3) if (...) x = a; // as if with an initial x = x.
1019 The later patterns require jumps to be more expensive.
1021 ??? For future expansion, look for multiple X in such patterns. */
1023 struct noce_if_info if_info
;
1026 rtx orig_x
, x
, a
, b
;
1029 /* If this is not a standard conditional jump, we can't parse it. */
1030 jump
= test_bb
->end
;
1031 cond
= noce_get_condition (jump
, &if_info
.cond_earliest
);
1035 /* We must be comparing objects whose modes imply the size. */
1036 if (GET_MODE (XEXP (cond
, 0)) == BLKmode
)
1039 /* Look for one of the potential sets. */
1040 insn_a
= first_active_insn (then_bb
);
1042 || ! last_active_insn_p (then_bb
, insn_a
)
1043 || (set_a
= single_set (insn_a
)) == NULL_RTX
)
1046 x
= SET_DEST (set_a
);
1047 a
= SET_SRC (set_a
);
1049 /* Look for the other potential set. Make sure we've got equivalent
1051 /* ??? This is overconservative. Storing to two different mems is
1052 as easy as conditionally computing the address. Storing to a
1053 single mem merely requires a scratch memory to use as one of the
1054 destination addresses; often the memory immediately below the
1055 stack pointer is available for this. */
1059 insn_b
= first_active_insn (else_bb
);
1061 || ! last_active_insn_p (else_bb
, insn_b
)
1062 || (set_b
= single_set (insn_b
)) == NULL_RTX
1063 || ! rtx_equal_p (x
, SET_DEST (set_b
)))
1068 insn_b
= prev_nonnote_insn (if_info
.cond_earliest
);
1070 || GET_CODE (insn_b
) != INSN
1071 || (set_b
= single_set (insn_b
)) == NULL_RTX
1072 || ! rtx_equal_p (x
, SET_DEST (set_b
))
1073 || reg_mentioned_p (x
, cond
))
1074 insn_b
= set_b
= NULL_RTX
;
1076 b
= (set_b
? SET_SRC (set_b
) : x
);
1078 /* Only operate on register destinations, and even then avoid extending
1079 the lifetime of hard registers on small register class machines. */
1081 if (GET_CODE (x
) != REG
1082 || (SMALL_REGISTER_CLASSES
1083 && REGNO (x
) < FIRST_PSEUDO_REGISTER
))
1087 x
= gen_reg_rtx (GET_MODE (x
));
1090 /* Don't operate on sources that may trap or are volatile. */
1091 if (side_effects_p (a
) || side_effects_p (b
)
1092 || (GET_CODE (a
) != MEM
&& may_trap_p (a
))
1093 || (GET_CODE (b
) != MEM
&& may_trap_p (b
)))
1096 /* Set up the info block for our subroutines. */
1097 if_info
.cond
= cond
;
1098 if_info
.jump
= jump
;
1099 if_info
.insn_a
= insn_a
;
1100 if_info
.insn_b
= insn_b
;
1105 /* Try optimizations in some approximation of a useful order. */
1106 /* ??? Should first look to see if X is live incoming at all. If it
1107 isn't, we don't need anything but an unconditional set. */
1109 /* Look and see if A and B are really the same. Avoid creating silly
1110 cmove constructs that no one will fix up later. */
1111 if (rtx_equal_p (a
, b
))
1113 /* If we have an INSN_B, we don't have to create any new rtl. Just
1114 move the instruction that we already have. If we don't have an
1115 INSN_B, that means that A == X, and we've got a noop move. In
1116 that case don't do anything and let the code below delete INSN_A. */
1117 if (insn_b
&& else_bb
)
1119 if (else_bb
&& insn_b
== else_bb
->end
)
1120 else_bb
->end
= PREV_INSN (insn_b
);
1121 reorder_insns (insn_b
, insn_b
, PREV_INSN (if_info
.cond_earliest
));
1128 if (noce_try_store_flag (&if_info
))
1130 if (HAVE_conditional_move
1131 && noce_try_cmove (&if_info
))
1133 if (! HAVE_conditional_execution
)
1135 if (noce_try_store_flag_constants (&if_info
))
1137 if (noce_try_store_flag_inc (&if_info
))
1139 if (noce_try_store_flag_mask (&if_info
))
1141 if (HAVE_conditional_move
1142 && noce_try_cmove_arith (&if_info
))
1149 /* The original sets may now be killed. */
1150 if (insn_a
== then_bb
->end
)
1151 then_bb
->end
= PREV_INSN (insn_a
);
1152 flow_delete_insn (insn_a
);
1154 /* Several special cases here: First, we may have reused insn_b above,
1155 in which case insn_b is now NULL. Second, we want to delete insn_b
1156 if it came from the ELSE block, because follows the now correct
1157 write that appears in the TEST block. However, if we got insn_b from
1158 the TEST block, it may in fact be loading data needed for the comparison.
1159 We'll let life_analysis remove the insn if it's really dead. */
1160 if (insn_b
&& else_bb
)
1162 if (insn_b
== else_bb
->end
)
1163 else_bb
->end
= PREV_INSN (insn_b
);
1164 flow_delete_insn (insn_b
);
1167 /* The new insns will have been inserted before cond_earliest. We should
1168 be able to remove cond_earliest through the jump with impunity. */
1169 insn_a
= prev_nonnote_insn (if_info
.cond_earliest
);
1170 flow_delete_insn_chain (if_info
.cond_earliest
, test_bb
->end
);
1171 test_bb
->end
= insn_a
;
1173 /* If we used a temporary, fix it up now. */
1177 emit_move_insn (orig_x
, x
);
1178 insn_b
= gen_sequence ();
1181 test_bb
->end
= emit_insn_after (insn_b
, insn_a
);
1184 /* Merge the blocks! */
1185 merge_if_block (test_bb
, then_bb
, else_bb
, join_bb
);
1190 /* Attempt to convert an IF-THEN or IF-THEN-ELSE block into
1191 straight line code. Return true if successful. */
1194 process_if_block (test_bb
, then_bb
, else_bb
, join_bb
)
1195 basic_block test_bb
; /* Basic block test is in */
1196 basic_block then_bb
; /* Basic block for THEN block */
1197 basic_block else_bb
; /* Basic block for ELSE block */
1198 basic_block join_bb
; /* Basic block the join label is in */
1200 if (! reload_completed
1201 && noce_process_if_block (test_bb
, then_bb
, else_bb
, join_bb
))
1204 if (HAVE_conditional_execution
1206 && cond_exec_process_if_block (test_bb
, then_bb
, else_bb
, join_bb
))
1212 /* Merge the blocks and mark for local life update. */
1215 merge_if_block (test_bb
, then_bb
, else_bb
, join_bb
)
1216 basic_block test_bb
; /* Basic block test is in */
1217 basic_block then_bb
; /* Basic block for THEN block */
1218 basic_block else_bb
; /* Basic block for ELSE block */
1219 basic_block join_bb
; /* Basic block the join label is in */
1221 basic_block combo_bb
;
1223 /* All block merging is done into the lower block numbers. */
1227 /* First merge TEST block into THEN block. This is a no-brainer since
1228 the THEN block did not have a code label to begin with. */
1230 if (combo_bb
->global_live_at_end
)
1231 COPY_REG_SET (combo_bb
->global_live_at_end
, then_bb
->global_live_at_end
);
1232 merge_blocks_nomove (combo_bb
, then_bb
);
1233 num_removed_blocks
++;
1235 /* The ELSE block, if it existed, had a label. That label count
1236 will almost always be zero, but odd things can happen when labels
1237 get their addresses taken. */
1240 if (LABEL_NUSES (else_bb
->head
) == 0
1241 && ! LABEL_PRESERVE_P (else_bb
->head
)
1242 && ! LABEL_NAME (else_bb
->head
))
1244 /* We can merge the ELSE. */
1245 merge_blocks_nomove (combo_bb
, else_bb
);
1246 num_removed_blocks
++;
1250 /* We cannot merge the ELSE. */
1252 /* Properly rewire the edge out of the now combined
1253 TEST-THEN block to point here. */
1254 remove_edge (combo_bb
->succ
);
1255 if (combo_bb
->succ
|| else_bb
->pred
)
1257 make_edge (NULL
, combo_bb
, else_bb
, EDGE_FALLTHRU
);
1259 /* Remove the jump and cruft from the end of the TEST-THEN block. */
1260 tidy_fallthru_edge (combo_bb
->succ
, combo_bb
, else_bb
);
1262 /* Make sure we update life info properly. */
1263 SET_UPDATE_LIFE(combo_bb
);
1264 if (else_bb
->global_live_at_end
)
1265 COPY_REG_SET (else_bb
->global_live_at_start
,
1266 else_bb
->global_live_at_end
);
1268 /* The ELSE is the new combo block. */
1273 /* If there was no join block reported, that means it was not adjacent
1274 to the others, and so we cannot merge them. */
1278 /* The outgoing edge for the current COMBO block should already
1279 be correct. Verify this. */
1280 if (combo_bb
->succ
== NULL_EDGE
)
1283 /* There should sill be a branch at the end of the THEN or ELSE
1284 blocks taking us to our final destination. */
1285 if (! simplejump_p (combo_bb
->end
)
1286 && ! returnjump_p (combo_bb
->end
))
1290 /* The JOIN block had a label. It may have had quite a number
1291 of other predecessors too, but probably not. See if we can
1292 merge this with the others. */
1293 else if (LABEL_NUSES (join_bb
->head
) == 0
1294 && ! LABEL_PRESERVE_P (join_bb
->head
)
1295 && ! LABEL_NAME (join_bb
->head
))
1297 /* We can merge the JOIN. */
1298 if (combo_bb
->global_live_at_end
)
1299 COPY_REG_SET (combo_bb
->global_live_at_end
,
1300 join_bb
->global_live_at_end
);
1301 merge_blocks_nomove (combo_bb
, join_bb
);
1302 num_removed_blocks
++;
1306 /* We cannot merge the JOIN. */
1308 /* The outgoing edge for the current COMBO block should already
1309 be correct. Verify this. */
1310 if (combo_bb
->succ
->succ_next
!= NULL_EDGE
1311 || combo_bb
->succ
->dest
!= join_bb
)
1314 /* Remove the jump and cruft from the end of the COMBO block. */
1315 tidy_fallthru_edge (combo_bb
->succ
, combo_bb
, join_bb
);
1318 /* Make sure we update life info properly. */
1319 SET_UPDATE_LIFE (combo_bb
);
1321 num_updated_if_blocks
++;
1324 /* Find a block ending in a simple IF condition. Return TRUE if
1325 we were able to transform it in some way. */
1328 find_if_header (test_bb
)
1329 basic_block test_bb
;
1334 /* The kind of block we're looking for has exactly two successors. */
1335 if ((then_edge
= test_bb
->succ
) == NULL_EDGE
1336 || (else_edge
= then_edge
->succ_next
) == NULL_EDGE
1337 || else_edge
->succ_next
!= NULL_EDGE
)
1340 /* Neither edge should be abnormal. */
1341 if ((then_edge
->flags
& EDGE_COMPLEX
)
1342 || (else_edge
->flags
& EDGE_COMPLEX
))
1345 /* The THEN edge is canonically the one that falls through. */
1346 if (then_edge
->flags
& EDGE_FALLTHRU
)
1348 else if (else_edge
->flags
& EDGE_FALLTHRU
)
1351 else_edge
= then_edge
;
1355 /* Otherwise this must be a multiway branch of some sort. */
1358 if (find_if_block (test_bb
, then_edge
, else_edge
))
1361 && (! HAVE_conditional_execution
|| reload_completed
))
1363 if (find_if_case_1 (test_bb
, then_edge
, else_edge
))
1365 if (find_if_case_2 (test_bb
, then_edge
, else_edge
))
1373 fprintf (rtl_dump_file
, "Conversion succeeded.\n");
1377 /* Determine if a given basic block heads a simple IF-THEN or IF-THEN-ELSE
1378 block. If so, we'll try to convert the insns to not require the branch.
1379 Return TRUE if we were successful at converting the the block. */
1382 find_if_block (test_bb
, then_edge
, else_edge
)
1383 basic_block test_bb
;
1384 edge then_edge
, else_edge
;
1386 basic_block then_bb
= then_edge
->dest
;
1387 basic_block else_bb
= else_edge
->dest
;
1388 basic_block join_bb
= NULL_BLOCK
;
1389 edge then_succ
= then_bb
->succ
;
1390 edge else_succ
= else_bb
->succ
;
1393 /* The THEN block of an IF-THEN combo must have exactly one predecessor. */
1394 if (then_bb
->pred
->pred_next
!= NULL_EDGE
)
1397 /* The THEN block of an IF-THEN combo must have exactly one successor. */
1398 if (then_succ
== NULL_EDGE
1399 || then_succ
->succ_next
!= NULL_EDGE
1400 || (then_succ
->flags
& EDGE_COMPLEX
))
1403 /* The THEN block may not start with a label, as might happen with an
1404 unused user label that has had its address taken. */
1405 if (GET_CODE (then_bb
->head
) == CODE_LABEL
)
1408 /* If the THEN block's successor is the other edge out of the TEST block,
1409 then we have an IF-THEN combo without an ELSE. */
1410 if (then_succ
->dest
== else_bb
)
1413 else_bb
= NULL_BLOCK
;
1416 /* If the THEN and ELSE block meet in a subsequent block, and the ELSE
1417 has exactly one predecessor and one successor, and the outgoing edge
1418 is not complex, then we have an IF-THEN-ELSE combo. */
1419 else if (else_succ
!= NULL_EDGE
1420 && then_succ
->dest
== else_succ
->dest
1421 && else_bb
->pred
->pred_next
== NULL_EDGE
1422 && else_succ
->succ_next
== NULL_EDGE
1423 && ! (else_succ
->flags
& EDGE_COMPLEX
))
1424 join_bb
= else_succ
->dest
;
1426 /* Otherwise it is not an IF-THEN or IF-THEN-ELSE combination. */
1430 num_possible_if_blocks
++;
1435 fprintf (rtl_dump_file
,
1436 "\nIF-THEN-ELSE block found, start %d, then %d, else %d, join %d\n",
1437 test_bb
->index
, then_bb
->index
, else_bb
->index
,
1440 fprintf (rtl_dump_file
,
1441 "\nIF-THEN block found, start %d, then %d, join %d\n",
1442 test_bb
->index
, then_bb
->index
, join_bb
->index
);
1445 /* Make sure IF, THEN, and ELSE, blocks are adjacent. Actually, we
1446 get the first condition for free, since we've already asserted that
1447 there's a fallthru edge from IF to THEN. */
1448 /* ??? As an enhancement, move the ELSE block. Have to deal with EH and
1449 BLOCK notes, if by no other means than aborting the merge if they
1450 exist. Sticky enough I don't want to think about it now. */
1451 next_index
= then_bb
->index
;
1452 if (else_bb
&& ++next_index
!= else_bb
->index
)
1454 if (++next_index
!= join_bb
->index
)
1462 /* Do the real work. */
1463 return process_if_block (test_bb
, then_bb
, else_bb
, join_bb
);
1466 /* Look for IF-THEN-ELSE cases in which one of THEN or ELSE is
1467 transformable, but not necessarily the other. There need be no
1470 Return TRUE if we were successful at converting the the block.
1472 Cases we'd like to look at:
1475 if (test) goto over; // x not live
1483 if (! test) goto label;
1486 if (test) goto E; // x not live
1500 (3) // This one's really only interesting for targets that can do
1501 // multiway branching, e.g. IA-64 BBB bundles. For other targets
1502 // it results in multiple branches on a cache line, which often
1503 // does not sit well with predictors.
1505 if (test1) goto E; // predicted not taken
1521 (A) Don't do (2) if the branch is predicted against the block we're
1522 eliminating. Do it anyway if we can eliminate a branch; this requires
1523 that the sole successor of the eliminated block postdominate the other
1526 (B) With CE, on (3) we can steal from both sides of the if, creating
1535 Again, this is most useful if J postdominates.
1537 (C) CE substitutes for helpful life information.
1539 (D) These heuristics need a lot of work. */
1541 /* Tests for case 1 above. */
1544 find_if_case_1 (test_bb
, then_edge
, else_edge
)
1545 basic_block test_bb
;
1546 edge then_edge
, else_edge
;
1548 basic_block then_bb
= then_edge
->dest
;
1549 basic_block else_bb
= else_edge
->dest
;
1550 edge then_succ
= then_bb
->succ
;
1553 /* THEN has one successor. */
1554 if (!then_succ
|| then_succ
->succ_next
!= NULL
)
1557 /* THEN does not fall through, but is not strange either. */
1558 if (then_succ
->flags
& (EDGE_COMPLEX
| EDGE_FALLTHRU
))
1561 /* THEN has one predecessor. */
1562 if (then_bb
->pred
->pred_next
!= NULL
)
1565 /* THEN has no label. */
1566 if (GET_CODE (then_bb
->head
) == CODE_LABEL
)
1569 /* ELSE follows THEN. (??? could be moved) */
1570 if (else_bb
->index
!= then_bb
->index
+ 1)
1573 num_possible_if_blocks
++;
1575 fprintf (rtl_dump_file
,
1576 "\nIF-CASE-1 found, start %d, then %d\n",
1577 test_bb
->index
, then_bb
->index
);
1579 /* THEN is small. */
1580 if (count_bb_insns (then_bb
) > BRANCH_COST
)
1583 /* Find the label for THEN's destination. */
1584 if (then_succ
->dest
== EXIT_BLOCK_PTR
)
1588 new_lab
= JUMP_LABEL (then_bb
->end
);
1593 /* Registers set are dead, or are predicable. */
1594 if (! dead_or_predicable (test_bb
, then_bb
, else_bb
, new_lab
, 1))
1597 /* Conversion went ok, including moving the insns and fixing up the
1598 jump. Adjust the CFG to match. */
1600 SET_UPDATE_LIFE (test_bb
);
1601 bitmap_operation (test_bb
->global_live_at_end
,
1602 else_bb
->global_live_at_start
,
1603 then_bb
->global_live_at_end
, BITMAP_IOR
);
1605 make_edge (NULL
, test_bb
, then_succ
->dest
, 0);
1606 flow_delete_block (then_bb
);
1607 tidy_fallthru_edge (else_edge
, test_bb
, else_bb
);
1609 num_removed_blocks
++;
1610 num_updated_if_blocks
++;
1615 /* Test for case 2 above. */
1618 find_if_case_2 (test_bb
, then_edge
, else_edge
)
1619 basic_block test_bb
;
1620 edge then_edge
, else_edge
;
1622 basic_block then_bb
= then_edge
->dest
;
1623 basic_block else_bb
= else_edge
->dest
;
1624 edge else_succ
= else_bb
->succ
;
1627 /* ELSE has one successor. */
1628 if (!else_succ
|| else_succ
->succ_next
!= NULL
)
1631 /* ELSE outgoing edge is not complex. */
1632 if (else_succ
->flags
& EDGE_COMPLEX
)
1635 /* ELSE has one predecessor. */
1636 if (else_bb
->pred
->pred_next
!= NULL
)
1639 /* ELSE has a label we can delete. */
1640 if (LABEL_NUSES (else_bb
->head
) > 1
1641 || LABEL_PRESERVE_P (else_bb
->head
)
1642 || LABEL_NAME (else_bb
->head
))
1645 /* ELSE is predicted or SUCC(ELSE) postdominates THEN. */
1646 note
= find_reg_note (test_bb
->end
, REG_BR_PROB
, NULL_RTX
);
1647 if (note
&& INTVAL (XEXP (note
, 0)) >= REG_BR_PROB_BASE
/ 2)
1649 else if (else_succ
->dest
->index
< 0
1650 || (then_bb
->index
>= 0
1651 && TEST_BIT (post_dominators
[ORIG_INDEX (then_bb
)],
1652 ORIG_INDEX (else_succ
->dest
))))
1657 num_possible_if_blocks
++;
1659 fprintf (rtl_dump_file
,
1660 "\nIF-CASE-2 found, start %d, else %d\n",
1661 test_bb
->index
, else_bb
->index
);
1663 /* ELSE is small. */
1664 if (count_bb_insns (then_bb
) > BRANCH_COST
)
1667 /* Find the label for ELSE's destination. */
1668 if (else_succ
->dest
== EXIT_BLOCK_PTR
)
1672 if (else_succ
->flags
& EDGE_FALLTHRU
)
1674 new_lab
= else_succ
->dest
->head
;
1675 if (GET_CODE (new_lab
) != CODE_LABEL
)
1680 new_lab
= JUMP_LABEL (else_bb
->end
);
1686 /* Registers set are dead, or are predicable. */
1687 if (! dead_or_predicable (test_bb
, else_bb
, then_bb
, new_lab
, 0))
1690 /* Conversion went ok, including moving the insns and fixing up the
1691 jump. Adjust the CFG to match. */
1693 SET_UPDATE_LIFE (test_bb
);
1694 bitmap_operation (test_bb
->global_live_at_end
,
1695 then_bb
->global_live_at_start
,
1696 else_bb
->global_live_at_end
, BITMAP_IOR
);
1698 remove_edge (else_edge
);
1699 make_edge (NULL
, test_bb
, else_succ
->dest
, 0);
1700 flow_delete_block (else_bb
);
1702 num_removed_blocks
++;
1703 num_updated_if_blocks
++;
1705 /* ??? We may now fallthru from one of THEN's successors into a join
1706 block. Rerun cleanup_cfg? Examine things manually? Wait? */
1711 /* A subroutine of dead_or_predicable called through for_each_rtx.
1712 Return 1 if a memory is found. */
1715 find_memory (px
, data
)
1717 void *data ATTRIBUTE_UNUSED
;
1719 return GET_CODE (*px
) == MEM
;
1722 /* Used by the code above to perform the actual rtl transformations.
1723 Return TRUE if successful.
1725 TEST_BB is the block containing the conditional branch. MERGE_BB
1726 is the block containing the code to manipulate. NEW_DEST is the
1727 label TEST_BB should be branching to after the conversion.
1728 REVERSEP is true if the sense of the branch should be reversed. */
1731 dead_or_predicable (test_bb
, merge_bb
, other_bb
, new_dest
, reversep
)
1732 basic_block test_bb
, merge_bb
, other_bb
;
1736 rtx head
, end
, jump
, earliest
, old_dest
;
1738 jump
= test_bb
->end
;
1740 /* Find the extent of the real code in the merge block. */
1741 head
= merge_bb
->head
;
1742 end
= merge_bb
->end
;
1744 if (GET_CODE (head
) == CODE_LABEL
)
1745 head
= NEXT_INSN (head
);
1746 if (GET_CODE (head
) == NOTE
)
1750 head
= end
= NULL_RTX
;
1753 head
= NEXT_INSN (head
);
1756 if (GET_CODE (end
) == JUMP_INSN
)
1760 head
= end
= NULL_RTX
;
1763 end
= PREV_INSN (end
);
1766 if (HAVE_conditional_execution
)
1768 /* In the conditional execution case, we have things easy. We know
1769 the condition is reversable. We don't have to check life info,
1770 becase we're going to conditionally execute the code anyway.
1771 All that's left is making sure the insns involved can actually
1776 cond
= cond_exec_get_condition (jump
);
1778 cond
= gen_rtx_fmt_ee (reverse_condition (GET_CODE (cond
)),
1779 GET_MODE (cond
), XEXP (cond
, 0),
1782 if (! cond_exec_process_insns (head
, end
, cond
, 0))
1789 /* In the non-conditional execution case, we have to verify that there
1790 are no trapping operations, no calls, no references to memory, and
1791 that any registers modified are dead at the branch site. */
1793 rtx insn
, cond
, prev
;
1794 regset_head merge_set_head
, tmp_head
, test_live_head
, test_set_head
;
1795 regset merge_set
, tmp
, test_live
, test_set
;
1796 struct propagate_block_info
*pbi
;
1799 /* Check for no calls or trapping operations. */
1800 for (insn
= head
; ; insn
= NEXT_INSN (insn
))
1802 if (GET_CODE (insn
) == CALL_INSN
)
1806 if (may_trap_p (PATTERN (insn
)))
1809 /* ??? Even non-trapping memories such as stack frame
1810 references must be avoided. For stores, we collect
1811 no lifetime info; for reads, we'd have to assert
1812 true_dependance false against every store in the
1814 if (for_each_rtx (&PATTERN (insn
), find_memory
, NULL
))
1821 if (! condjump_p (jump
))
1824 /* Find the extent of the conditional. */
1825 cond
= noce_get_condition (jump
, &earliest
);
1830 MERGE_SET = set of registers set in MERGE_BB
1831 TEST_LIVE = set of registers live at EARLIEST
1832 TEST_SET = set of registers set between EARLIEST and the
1833 end of the block. */
1835 tmp
= INITIALIZE_REG_SET (tmp_head
);
1836 merge_set
= INITIALIZE_REG_SET (merge_set_head
);
1837 test_live
= INITIALIZE_REG_SET (test_live_head
);
1838 test_set
= INITIALIZE_REG_SET (test_set_head
);
1840 /* ??? bb->local_set is only valid during calculate_global_regs_live,
1841 so we must recompute usage for MERGE_BB. Not so bad, I suppose,
1842 since we've already asserted that MERGE_BB is small. */
1843 propagate_block (merge_bb
, tmp
, merge_set
, 0);
1845 /* For small register class machines, don't lengthen lifetimes of
1846 hard registers before reload. */
1847 if (SMALL_REGISTER_CLASSES
&& ! reload_completed
)
1849 EXECUTE_IF_SET_IN_BITMAP
1852 if (i
< FIRST_PSEUDO_REGISTER
1854 && ! global_regs
[i
])
1859 /* For TEST, we're interested in a range of insns, not a whole block.
1860 Moreover, we're interested in the insns live from OTHER_BB. */
1862 COPY_REG_SET (test_live
, other_bb
->global_live_at_start
);
1863 pbi
= init_propagate_block_info (test_bb
, test_live
, test_set
, 0);
1865 for (insn
= jump
; ; insn
= prev
)
1867 prev
= propagate_one_insn (pbi
, insn
);
1868 if (insn
== earliest
)
1872 free_propagate_block_info (pbi
);
1874 /* We can perform the transformation if
1875 MERGE_SET & (TEST_SET | TEST_LIVE)
1877 TEST_SET & merge_bb->global_live_at_start
1880 bitmap_operation (tmp
, test_set
, test_live
, BITMAP_IOR
);
1881 bitmap_operation (tmp
, tmp
, merge_set
, BITMAP_AND
);
1882 EXECUTE_IF_SET_IN_BITMAP(tmp
, 0, i
, fail
= 1);
1884 bitmap_operation (tmp
, test_set
, merge_bb
->global_live_at_start
,
1886 EXECUTE_IF_SET_IN_BITMAP(tmp
, 0, i
, fail
= 1);
1889 FREE_REG_SET (merge_set
);
1890 FREE_REG_SET (test_live
);
1891 FREE_REG_SET (test_set
);
1898 /* We don't want to use normal invert_jump or redirect_jump because
1899 we don't want to delete_insn called. Also, we want to do our own
1900 change group management. */
1902 old_dest
= JUMP_LABEL (jump
);
1904 ? ! invert_jump_1 (jump
, new_dest
)
1905 : ! redirect_jump_1 (jump
, new_dest
))
1908 if (! apply_change_group ())
1912 LABEL_NUSES (old_dest
) -= 1;
1914 LABEL_NUSES (new_dest
) += 1;
1915 JUMP_LABEL (jump
) = new_dest
;
1919 rtx note
= find_reg_note (jump
, REG_BR_PROB
, NULL_RTX
);
1921 XEXP (note
, 0) = GEN_INT (REG_BR_PROB_BASE
- INTVAL (XEXP (note
, 0)));
1924 /* Move the insns out of MERGE_BB to before the branch. */
1925 if (end
== merge_bb
->end
)
1926 merge_bb
->end
= merge_bb
->head
;
1929 head
= squeeze_notes (head
, end
);
1930 reorder_insns (head
, end
, PREV_INSN (earliest
));
1939 /* Main entry point for all if-conversion. */
1942 if_convert (life_data_ok
)
1947 num_possible_if_blocks
= 0;
1948 num_updated_if_blocks
= 0;
1949 num_removed_blocks
= 0;
1951 /* Free up basic_block_for_insn so that we don't have to keep it
1952 up to date, either here or in merge_blocks_nomove. */
1953 free_basic_block_vars (1);
1955 /* Compute postdominators if we think we'll use them. */
1956 post_dominators
= NULL
;
1957 if (HAVE_conditional_execution
|| life_data_ok
)
1959 post_dominators
= sbitmap_vector_alloc (n_basic_blocks
, n_basic_blocks
);
1960 compute_flow_dominators (NULL
, post_dominators
);
1963 /* Record initial block numbers. */
1964 for (block_num
= 0; block_num
< n_basic_blocks
; block_num
++)
1965 SET_ORIG_INDEX (BASIC_BLOCK (block_num
), block_num
);
1967 /* Go through each of the basic blocks looking for things to convert. */
1968 for (block_num
= 0; block_num
< n_basic_blocks
; )
1970 basic_block bb
= BASIC_BLOCK (block_num
);
1971 if (find_if_header (bb
))
1972 block_num
= bb
->index
;
1977 sbitmap_vector_free (post_dominators
);
1980 fflush (rtl_dump_file
);
1982 /* Rebuild basic_block_for_insn for update_life_info and for gcse. */
1983 compute_bb_for_insn (get_max_uid ());
1985 /* Rebuild life info for basic blocks that require it. */
1986 if (num_removed_blocks
&& life_data_ok
)
1988 sbitmap update_life_blocks
= sbitmap_alloc (n_basic_blocks
);
1989 sbitmap_zero (update_life_blocks
);
1991 /* If we allocated new pseudos, we must resize the array for sched1. */
1992 if (max_regno
< max_reg_num ())
1994 max_regno
= max_reg_num ();
1995 allocate_reg_info (max_regno
, FALSE
, FALSE
);
1998 for (block_num
= 0; block_num
< n_basic_blocks
; block_num
++)
1999 if (UPDATE_LIFE (BASIC_BLOCK (block_num
)))
2000 SET_BIT (update_life_blocks
, block_num
);
2002 count_or_remove_death_notes (update_life_blocks
, 1);
2003 update_life_info (update_life_blocks
, UPDATE_LIFE_LOCAL
,
2006 sbitmap_free (update_life_blocks
);
2009 /* Write the final stats. */
2010 if (rtl_dump_file
&& num_possible_if_blocks
> 0)
2012 fprintf (rtl_dump_file
,
2013 "\n%d possible IF blocks searched.\n",
2014 num_possible_if_blocks
);
2015 fprintf (rtl_dump_file
,
2016 "%d IF blocks converted.\n",
2017 num_updated_if_blocks
);
2018 fprintf (rtl_dump_file
,
2019 "%d basic blocks deleted.\n\n\n",
2020 num_removed_blocks
);
2023 verify_flow_info ();