1 /* If-conversion support.
2 Copyright (C) 2000-2013 Free Software Foundation, Inc.
4 This file is part of GCC.
6 GCC is free software; you can redistribute it and/or modify it
7 under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 3, or (at your option)
11 GCC is distributed in the hope that it will be useful, but WITHOUT
12 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
13 or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
14 License for more details.
16 You should have received a copy of the GNU General Public License
17 along with GCC; see the file COPYING3. If not see
18 <http://www.gnu.org/licenses/>. */
22 #include "coretypes.h"
29 #include "insn-config.h"
32 #include "hard-reg-set.h"
33 #include "basic-block.h"
37 #include "diagnostic-core.h"
41 #include "tree-pass.h"
44 #include "pointer-set.h"
47 #ifndef HAVE_conditional_move
48 #define HAVE_conditional_move 0
60 #ifndef MAX_CONDITIONAL_EXECUTE
61 #define MAX_CONDITIONAL_EXECUTE \
62 (BRANCH_COST (optimize_function_for_speed_p (cfun), false) \
66 #define IFCVT_MULTIPLE_DUMPS 1
68 #define NULL_BLOCK ((basic_block) NULL)
70 /* # of IF-THEN or IF-THEN-ELSE blocks we looked at */
71 static int num_possible_if_blocks
;
73 /* # of IF-THEN or IF-THEN-ELSE blocks were converted to conditional
75 static int num_updated_if_blocks
;
77 /* # of changes made. */
78 static int num_true_changes
;
80 /* Whether conditional execution changes were made. */
81 static int cond_exec_changed_p
;
83 /* Forward references. */
84 static int count_bb_insns (const_basic_block
);
85 static bool cheap_bb_rtx_cost_p (const_basic_block
, int, int);
86 static rtx
first_active_insn (basic_block
);
87 static rtx
last_active_insn (basic_block
, int);
88 static rtx
find_active_insn_before (basic_block
, rtx
);
89 static rtx
find_active_insn_after (basic_block
, rtx
);
90 static basic_block
block_fallthru (basic_block
);
91 static int cond_exec_process_insns (ce_if_block_t
*, rtx
, rtx
, rtx
, rtx
, int);
92 static rtx
cond_exec_get_condition (rtx
);
93 static rtx
noce_get_condition (rtx
, rtx
*, bool);
94 static int noce_operand_ok (const_rtx
);
95 static void merge_if_block (ce_if_block_t
*);
96 static int find_cond_trap (basic_block
, edge
, edge
);
97 static basic_block
find_if_header (basic_block
, int);
98 static int block_jumps_and_fallthru_p (basic_block
, basic_block
);
99 static int noce_find_if_block (basic_block
, edge
, edge
, int);
100 static int cond_exec_find_if_block (ce_if_block_t
*);
101 static int find_if_case_1 (basic_block
, edge
, edge
);
102 static int find_if_case_2 (basic_block
, edge
, edge
);
103 static int dead_or_predicable (basic_block
, basic_block
, basic_block
,
105 static void noce_emit_move_insn (rtx
, rtx
);
106 static rtx
block_has_only_trap (basic_block
);
108 /* Count the number of non-jump active insns in BB. */
111 count_bb_insns (const_basic_block bb
)
114 rtx insn
= BB_HEAD (bb
);
118 if ((CALL_P (insn
) || NONJUMP_INSN_P (insn
))
119 /* Don't count USE/CLOBBER insns, flow_find_cross_jump etc.
120 don't count them either and we need consistency. */
121 && GET_CODE (PATTERN (insn
)) != USE
122 && GET_CODE (PATTERN (insn
)) != CLOBBER
)
125 if (insn
== BB_END (bb
))
127 insn
= NEXT_INSN (insn
);
133 /* Determine whether the total insn_rtx_cost on non-jump insns in
134 basic block BB is less than MAX_COST. This function returns
135 false if the cost of any instruction could not be estimated.
137 The cost of the non-jump insns in BB is scaled by REG_BR_PROB_BASE
138 as those insns are being speculated. MAX_COST is scaled with SCALE
139 plus a small fudge factor. */
142 cheap_bb_rtx_cost_p (const_basic_block bb
, int scale
, int max_cost
)
145 rtx insn
= BB_HEAD (bb
);
146 bool speed
= optimize_bb_for_speed_p (bb
);
148 /* Our branch probability/scaling factors are just estimates and don't
149 account for cases where we can get speculation for free and other
150 secondary benefits. So we fudge the scale factor to make speculating
151 appear a little more profitable. */
152 scale
+= REG_BR_PROB_BASE
/ 8;
157 if (NONJUMP_INSN_P (insn
))
159 int cost
= insn_rtx_cost (PATTERN (insn
), speed
) * REG_BR_PROB_BASE
;
163 /* If this instruction is the load or set of a "stack" register,
164 such as a floating point register on x87, then the cost of
165 speculatively executing this insn may need to include
166 the additional cost of popping its result off of the
167 register stack. Unfortunately, correctly recognizing and
168 accounting for this additional overhead is tricky, so for
169 now we simply prohibit such speculative execution. */
172 rtx set
= single_set (insn
);
173 if (set
&& STACK_REG_P (SET_DEST (set
)))
179 if (count
>= max_cost
)
182 else if (CALL_P (insn
))
185 if (insn
== BB_END (bb
))
187 insn
= NEXT_INSN (insn
);
193 /* Return the first non-jump active insn in the basic block. */
196 first_active_insn (basic_block bb
)
198 rtx insn
= BB_HEAD (bb
);
202 if (insn
== BB_END (bb
))
204 insn
= NEXT_INSN (insn
);
207 while (NOTE_P (insn
) || DEBUG_INSN_P (insn
))
209 if (insn
== BB_END (bb
))
211 insn
= NEXT_INSN (insn
);
220 /* Return the last non-jump active (non-jump) insn in the basic block. */
223 last_active_insn (basic_block bb
, int skip_use_p
)
225 rtx insn
= BB_END (bb
);
226 rtx head
= BB_HEAD (bb
);
230 || DEBUG_INSN_P (insn
)
232 && NONJUMP_INSN_P (insn
)
233 && GET_CODE (PATTERN (insn
)) == USE
))
237 insn
= PREV_INSN (insn
);
246 /* Return the active insn before INSN inside basic block CURR_BB. */
249 find_active_insn_before (basic_block curr_bb
, rtx insn
)
251 if (!insn
|| insn
== BB_HEAD (curr_bb
))
254 while ((insn
= PREV_INSN (insn
)) != NULL_RTX
)
256 if (NONJUMP_INSN_P (insn
) || JUMP_P (insn
) || CALL_P (insn
))
259 /* No other active insn all the way to the start of the basic block. */
260 if (insn
== BB_HEAD (curr_bb
))
267 /* Return the active insn after INSN inside basic block CURR_BB. */
270 find_active_insn_after (basic_block curr_bb
, rtx insn
)
272 if (!insn
|| insn
== BB_END (curr_bb
))
275 while ((insn
= NEXT_INSN (insn
)) != NULL_RTX
)
277 if (NONJUMP_INSN_P (insn
) || JUMP_P (insn
) || CALL_P (insn
))
280 /* No other active insn all the way to the end of the basic block. */
281 if (insn
== BB_END (curr_bb
))
288 /* Return the basic block reached by falling though the basic block BB. */
291 block_fallthru (basic_block bb
)
293 edge e
= find_fallthru_edge (bb
->succs
);
295 return (e
) ? e
->dest
: NULL_BLOCK
;
298 /* Go through a bunch of insns, converting them to conditional
299 execution format if possible. Return TRUE if all of the non-note
300 insns were processed. */
303 cond_exec_process_insns (ce_if_block_t
*ce_info ATTRIBUTE_UNUSED
,
304 /* if block information */rtx start
,
305 /* first insn to look at */rtx end
,
306 /* last insn to look at */rtx test
,
307 /* conditional execution test */rtx prob_val
,
308 /* probability of branch taken. */int mod_ok
)
310 int must_be_last
= FALSE
;
318 for (insn
= start
; ; insn
= NEXT_INSN (insn
))
320 /* dwarf2out can't cope with conditional prologues. */
321 if (NOTE_P (insn
) && NOTE_KIND (insn
) == NOTE_INSN_PROLOGUE_END
)
324 if (NOTE_P (insn
) || DEBUG_INSN_P (insn
))
327 gcc_assert(NONJUMP_INSN_P (insn
) || CALL_P (insn
));
329 /* Remove USE insns that get in the way. */
330 if (reload_completed
&& GET_CODE (PATTERN (insn
)) == USE
)
332 /* ??? Ug. Actually unlinking the thing is problematic,
333 given what we'd have to coordinate with our callers. */
334 SET_INSN_DELETED (insn
);
338 /* Last insn wasn't last? */
342 if (modified_in_p (test
, insn
))
349 /* Now build the conditional form of the instruction. */
350 pattern
= PATTERN (insn
);
351 xtest
= copy_rtx (test
);
353 /* If this is already a COND_EXEC, rewrite the test to be an AND of the
355 if (GET_CODE (pattern
) == COND_EXEC
)
357 if (GET_MODE (xtest
) != GET_MODE (COND_EXEC_TEST (pattern
)))
360 xtest
= gen_rtx_AND (GET_MODE (xtest
), xtest
,
361 COND_EXEC_TEST (pattern
));
362 pattern
= COND_EXEC_CODE (pattern
);
365 pattern
= gen_rtx_COND_EXEC (VOIDmode
, xtest
, pattern
);
367 /* If the machine needs to modify the insn being conditionally executed,
368 say for example to force a constant integer operand into a temp
369 register, do so here. */
370 #ifdef IFCVT_MODIFY_INSN
371 IFCVT_MODIFY_INSN (ce_info
, pattern
, insn
);
376 validate_change (insn
, &PATTERN (insn
), pattern
, 1);
378 if (CALL_P (insn
) && prob_val
)
379 validate_change (insn
, ®_NOTES (insn
),
380 alloc_EXPR_LIST (REG_BR_PROB
, prob_val
,
381 REG_NOTES (insn
)), 1);
391 /* Return the condition for a jump. Do not do any special processing. */
394 cond_exec_get_condition (rtx jump
)
398 if (any_condjump_p (jump
))
399 test_if
= SET_SRC (pc_set (jump
));
402 cond
= XEXP (test_if
, 0);
404 /* If this branches to JUMP_LABEL when the condition is false,
405 reverse the condition. */
406 if (GET_CODE (XEXP (test_if
, 2)) == LABEL_REF
407 && XEXP (XEXP (test_if
, 2), 0) == JUMP_LABEL (jump
))
409 enum rtx_code rev
= reversed_comparison_code (cond
, jump
);
413 cond
= gen_rtx_fmt_ee (rev
, GET_MODE (cond
), XEXP (cond
, 0),
420 /* Given a simple IF-THEN or IF-THEN-ELSE block, attempt to convert it
421 to conditional execution. Return TRUE if we were successful at
422 converting the block. */
425 cond_exec_process_if_block (ce_if_block_t
* ce_info
,
426 /* if block information */int do_multiple_p
)
428 basic_block test_bb
= ce_info
->test_bb
; /* last test block */
429 basic_block then_bb
= ce_info
->then_bb
; /* THEN */
430 basic_block else_bb
= ce_info
->else_bb
; /* ELSE or NULL */
431 rtx test_expr
; /* expression in IF_THEN_ELSE that is tested */
432 rtx then_start
; /* first insn in THEN block */
433 rtx then_end
; /* last insn + 1 in THEN block */
434 rtx else_start
= NULL_RTX
; /* first insn in ELSE block or NULL */
435 rtx else_end
= NULL_RTX
; /* last insn + 1 in ELSE block */
436 int max
; /* max # of insns to convert. */
437 int then_mod_ok
; /* whether conditional mods are ok in THEN */
438 rtx true_expr
; /* test for else block insns */
439 rtx false_expr
; /* test for then block insns */
440 rtx true_prob_val
; /* probability of else block */
441 rtx false_prob_val
; /* probability of then block */
442 rtx then_last_head
= NULL_RTX
; /* Last match at the head of THEN */
443 rtx else_last_head
= NULL_RTX
; /* Last match at the head of ELSE */
444 rtx then_first_tail
= NULL_RTX
; /* First match at the tail of THEN */
445 rtx else_first_tail
= NULL_RTX
; /* First match at the tail of ELSE */
446 int then_n_insns
, else_n_insns
, n_insns
;
447 enum rtx_code false_code
;
449 /* If test is comprised of && or || elements, and we've failed at handling
450 all of them together, just use the last test if it is the special case of
451 && elements without an ELSE block. */
452 if (!do_multiple_p
&& ce_info
->num_multiple_test_blocks
)
454 if (else_bb
|| ! ce_info
->and_and_p
)
457 ce_info
->test_bb
= test_bb
= ce_info
->last_test_bb
;
458 ce_info
->num_multiple_test_blocks
= 0;
459 ce_info
->num_and_and_blocks
= 0;
460 ce_info
->num_or_or_blocks
= 0;
463 /* Find the conditional jump to the ELSE or JOIN part, and isolate
465 test_expr
= cond_exec_get_condition (BB_END (test_bb
));
469 /* If the conditional jump is more than just a conditional jump,
470 then we can not do conditional execution conversion on this block. */
471 if (! onlyjump_p (BB_END (test_bb
)))
474 /* Collect the bounds of where we're to search, skipping any labels, jumps
475 and notes at the beginning and end of the block. Then count the total
476 number of insns and see if it is small enough to convert. */
477 then_start
= first_active_insn (then_bb
);
478 then_end
= last_active_insn (then_bb
, TRUE
);
479 then_n_insns
= ce_info
->num_then_insns
= count_bb_insns (then_bb
);
480 n_insns
= then_n_insns
;
481 max
= MAX_CONDITIONAL_EXECUTE
;
488 else_start
= first_active_insn (else_bb
);
489 else_end
= last_active_insn (else_bb
, TRUE
);
490 else_n_insns
= ce_info
->num_else_insns
= count_bb_insns (else_bb
);
491 n_insns
+= else_n_insns
;
493 /* Look for matching sequences at the head and tail of the two blocks,
494 and limit the range of insns to be converted if possible. */
495 n_matching
= flow_find_cross_jump (then_bb
, else_bb
,
496 &then_first_tail
, &else_first_tail
,
498 if (then_first_tail
== BB_HEAD (then_bb
))
499 then_start
= then_end
= NULL_RTX
;
500 if (else_first_tail
== BB_HEAD (else_bb
))
501 else_start
= else_end
= NULL_RTX
;
506 then_end
= find_active_insn_before (then_bb
, then_first_tail
);
508 else_end
= find_active_insn_before (else_bb
, else_first_tail
);
509 n_insns
-= 2 * n_matching
;
514 && then_n_insns
> n_matching
515 && else_n_insns
> n_matching
)
517 int longest_match
= MIN (then_n_insns
- n_matching
,
518 else_n_insns
- n_matching
);
520 = flow_find_head_matching_sequence (then_bb
, else_bb
,
529 /* We won't pass the insns in the head sequence to
530 cond_exec_process_insns, so we need to test them here
531 to make sure that they don't clobber the condition. */
532 for (insn
= BB_HEAD (then_bb
);
533 insn
!= NEXT_INSN (then_last_head
);
534 insn
= NEXT_INSN (insn
))
535 if (!LABEL_P (insn
) && !NOTE_P (insn
)
536 && !DEBUG_INSN_P (insn
)
537 && modified_in_p (test_expr
, insn
))
541 if (then_last_head
== then_end
)
542 then_start
= then_end
= NULL_RTX
;
543 if (else_last_head
== else_end
)
544 else_start
= else_end
= NULL_RTX
;
549 then_start
= find_active_insn_after (then_bb
, then_last_head
);
551 else_start
= find_active_insn_after (else_bb
, else_last_head
);
552 n_insns
-= 2 * n_matching
;
560 /* Map test_expr/test_jump into the appropriate MD tests to use on
561 the conditionally executed code. */
563 true_expr
= test_expr
;
565 false_code
= reversed_comparison_code (true_expr
, BB_END (test_bb
));
566 if (false_code
!= UNKNOWN
)
567 false_expr
= gen_rtx_fmt_ee (false_code
, GET_MODE (true_expr
),
568 XEXP (true_expr
, 0), XEXP (true_expr
, 1));
570 false_expr
= NULL_RTX
;
572 #ifdef IFCVT_MODIFY_TESTS
573 /* If the machine description needs to modify the tests, such as setting a
574 conditional execution register from a comparison, it can do so here. */
575 IFCVT_MODIFY_TESTS (ce_info
, true_expr
, false_expr
);
577 /* See if the conversion failed. */
578 if (!true_expr
|| !false_expr
)
582 true_prob_val
= find_reg_note (BB_END (test_bb
), REG_BR_PROB
, NULL_RTX
);
585 true_prob_val
= XEXP (true_prob_val
, 0);
586 false_prob_val
= GEN_INT (REG_BR_PROB_BASE
- INTVAL (true_prob_val
));
589 false_prob_val
= NULL_RTX
;
591 /* If we have && or || tests, do them here. These tests are in the adjacent
592 blocks after the first block containing the test. */
593 if (ce_info
->num_multiple_test_blocks
> 0)
595 basic_block bb
= test_bb
;
596 basic_block last_test_bb
= ce_info
->last_test_bb
;
605 enum rtx_code f_code
;
607 bb
= block_fallthru (bb
);
608 start
= first_active_insn (bb
);
609 end
= last_active_insn (bb
, TRUE
);
611 && ! cond_exec_process_insns (ce_info
, start
, end
, false_expr
,
612 false_prob_val
, FALSE
))
615 /* If the conditional jump is more than just a conditional jump, then
616 we can not do conditional execution conversion on this block. */
617 if (! onlyjump_p (BB_END (bb
)))
620 /* Find the conditional jump and isolate the test. */
621 t
= cond_exec_get_condition (BB_END (bb
));
625 f_code
= reversed_comparison_code (t
, BB_END (bb
));
626 if (f_code
== UNKNOWN
)
629 f
= gen_rtx_fmt_ee (f_code
, GET_MODE (t
), XEXP (t
, 0), XEXP (t
, 1));
630 if (ce_info
->and_and_p
)
632 t
= gen_rtx_AND (GET_MODE (t
), true_expr
, t
);
633 f
= gen_rtx_IOR (GET_MODE (t
), false_expr
, f
);
637 t
= gen_rtx_IOR (GET_MODE (t
), true_expr
, t
);
638 f
= gen_rtx_AND (GET_MODE (t
), false_expr
, f
);
641 /* If the machine description needs to modify the tests, such as
642 setting a conditional execution register from a comparison, it can
644 #ifdef IFCVT_MODIFY_MULTIPLE_TESTS
645 IFCVT_MODIFY_MULTIPLE_TESTS (ce_info
, bb
, t
, f
);
647 /* See if the conversion failed. */
655 while (bb
!= last_test_bb
);
658 /* For IF-THEN-ELSE blocks, we don't allow modifications of the test
659 on then THEN block. */
660 then_mod_ok
= (else_bb
== NULL_BLOCK
);
662 /* Go through the THEN and ELSE blocks converting the insns if possible
663 to conditional execution. */
667 || ! cond_exec_process_insns (ce_info
, then_start
, then_end
,
668 false_expr
, false_prob_val
,
672 if (else_bb
&& else_end
673 && ! cond_exec_process_insns (ce_info
, else_start
, else_end
,
674 true_expr
, true_prob_val
, TRUE
))
677 /* If we cannot apply the changes, fail. Do not go through the normal fail
678 processing, since apply_change_group will call cancel_changes. */
679 if (! apply_change_group ())
681 #ifdef IFCVT_MODIFY_CANCEL
682 /* Cancel any machine dependent changes. */
683 IFCVT_MODIFY_CANCEL (ce_info
);
688 #ifdef IFCVT_MODIFY_FINAL
689 /* Do any machine dependent final modifications. */
690 IFCVT_MODIFY_FINAL (ce_info
);
693 /* Conversion succeeded. */
695 fprintf (dump_file
, "%d insn%s converted to conditional execution.\n",
696 n_insns
, (n_insns
== 1) ? " was" : "s were");
698 /* Merge the blocks! If we had matching sequences, make sure to delete one
699 copy at the appropriate location first: delete the copy in the THEN branch
700 for a tail sequence so that the remaining one is executed last for both
701 branches, and delete the copy in the ELSE branch for a head sequence so
702 that the remaining one is executed first for both branches. */
705 rtx from
= then_first_tail
;
707 from
= find_active_insn_after (then_bb
, from
);
708 delete_insn_chain (from
, BB_END (then_bb
), false);
711 delete_insn_chain (first_active_insn (else_bb
), else_last_head
, false);
713 merge_if_block (ce_info
);
714 cond_exec_changed_p
= TRUE
;
718 #ifdef IFCVT_MODIFY_CANCEL
719 /* Cancel any machine dependent changes. */
720 IFCVT_MODIFY_CANCEL (ce_info
);
727 /* Used by noce_process_if_block to communicate with its subroutines.
729 The subroutines know that A and B may be evaluated freely. They
730 know that X is a register. They should insert new instructions
731 before cond_earliest. */
735 /* The basic blocks that make up the IF-THEN-{ELSE-,}JOIN block. */
736 basic_block test_bb
, then_bb
, else_bb
, join_bb
;
738 /* The jump that ends TEST_BB. */
741 /* The jump condition. */
744 /* New insns should be inserted before this one. */
747 /* Insns in the THEN and ELSE block. There is always just this
748 one insns in those blocks. The insns are single_set insns.
749 If there was no ELSE block, INSN_B is the last insn before
750 COND_EARLIEST, or NULL_RTX. In the former case, the insn
751 operands are still valid, as if INSN_B was moved down below
755 /* The SET_SRC of INSN_A and INSN_B. */
758 /* The SET_DEST of INSN_A. */
761 /* True if this if block is not canonical. In the canonical form of
762 if blocks, the THEN_BB is the block reached via the fallthru edge
763 from TEST_BB. For the noce transformations, we allow the symmetric
765 bool then_else_reversed
;
767 /* Estimated cost of the particular branch instruction. */
771 static rtx
noce_emit_store_flag (struct noce_if_info
*, rtx
, int, int);
772 static int noce_try_move (struct noce_if_info
*);
773 static int noce_try_store_flag (struct noce_if_info
*);
774 static int noce_try_addcc (struct noce_if_info
*);
775 static int noce_try_store_flag_constants (struct noce_if_info
*);
776 static int noce_try_store_flag_mask (struct noce_if_info
*);
777 static rtx
noce_emit_cmove (struct noce_if_info
*, rtx
, enum rtx_code
, rtx
,
779 static int noce_try_cmove (struct noce_if_info
*);
780 static int noce_try_cmove_arith (struct noce_if_info
*);
781 static rtx
noce_get_alt_condition (struct noce_if_info
*, rtx
, rtx
*);
782 static int noce_try_minmax (struct noce_if_info
*);
783 static int noce_try_abs (struct noce_if_info
*);
784 static int noce_try_sign_mask (struct noce_if_info
*);
786 /* Helper function for noce_try_store_flag*. */
789 noce_emit_store_flag (struct noce_if_info
*if_info
, rtx x
, int reversep
,
792 rtx cond
= if_info
->cond
;
796 cond_complex
= (! general_operand (XEXP (cond
, 0), VOIDmode
)
797 || ! general_operand (XEXP (cond
, 1), VOIDmode
));
799 /* If earliest == jump, or when the condition is complex, try to
800 build the store_flag insn directly. */
804 rtx set
= pc_set (if_info
->jump
);
805 cond
= XEXP (SET_SRC (set
), 0);
806 if (GET_CODE (XEXP (SET_SRC (set
), 2)) == LABEL_REF
807 && XEXP (XEXP (SET_SRC (set
), 2), 0) == JUMP_LABEL (if_info
->jump
))
808 reversep
= !reversep
;
809 if (if_info
->then_else_reversed
)
810 reversep
= !reversep
;
814 code
= reversed_comparison_code (cond
, if_info
->jump
);
816 code
= GET_CODE (cond
);
818 if ((if_info
->cond_earliest
== if_info
->jump
|| cond_complex
)
819 && (normalize
== 0 || STORE_FLAG_VALUE
== normalize
))
823 tmp
= gen_rtx_fmt_ee (code
, GET_MODE (x
), XEXP (cond
, 0),
825 tmp
= gen_rtx_SET (VOIDmode
, x
, tmp
);
828 tmp
= emit_insn (tmp
);
830 if (recog_memoized (tmp
) >= 0)
836 if_info
->cond_earliest
= if_info
->jump
;
844 /* Don't even try if the comparison operands or the mode of X are weird. */
845 if (cond_complex
|| !SCALAR_INT_MODE_P (GET_MODE (x
)))
848 return emit_store_flag (x
, code
, XEXP (cond
, 0),
849 XEXP (cond
, 1), VOIDmode
,
850 (code
== LTU
|| code
== LEU
851 || code
== GEU
|| code
== GTU
), normalize
);
854 /* Emit instruction to move an rtx, possibly into STRICT_LOW_PART.
855 X is the destination/target and Y is the value to copy. */
858 noce_emit_move_insn (rtx x
, rtx y
)
860 enum machine_mode outmode
;
864 if (GET_CODE (x
) != STRICT_LOW_PART
)
866 rtx seq
, insn
, target
;
870 /* Check that the SET_SRC is reasonable before calling emit_move_insn,
871 otherwise construct a suitable SET pattern ourselves. */
872 insn
= (OBJECT_P (y
) || CONSTANT_P (y
) || GET_CODE (y
) == SUBREG
)
873 ? emit_move_insn (x
, y
)
874 : emit_insn (gen_rtx_SET (VOIDmode
, x
, y
));
878 if (recog_memoized (insn
) <= 0)
880 if (GET_CODE (x
) == ZERO_EXTRACT
)
882 rtx op
= XEXP (x
, 0);
883 unsigned HOST_WIDE_INT size
= INTVAL (XEXP (x
, 1));
884 unsigned HOST_WIDE_INT start
= INTVAL (XEXP (x
, 2));
886 /* store_bit_field expects START to be relative to
887 BYTES_BIG_ENDIAN and adjusts this value for machines with
888 BITS_BIG_ENDIAN != BYTES_BIG_ENDIAN. In order to be able to
889 invoke store_bit_field again it is necessary to have the START
890 value from the first call. */
891 if (BITS_BIG_ENDIAN
!= BYTES_BIG_ENDIAN
)
894 start
= BITS_PER_UNIT
- start
- size
;
897 gcc_assert (REG_P (op
));
898 start
= BITS_PER_WORD
- start
- size
;
902 gcc_assert (start
< (MEM_P (op
) ? BITS_PER_UNIT
: BITS_PER_WORD
));
903 store_bit_field (op
, size
, start
, 0, 0, GET_MODE (x
), y
);
907 switch (GET_RTX_CLASS (GET_CODE (y
)))
910 ot
= code_to_optab (GET_CODE (y
));
914 target
= expand_unop (GET_MODE (y
), ot
, XEXP (y
, 0), x
, 0);
915 if (target
!= NULL_RTX
)
918 emit_move_insn (x
, target
);
927 ot
= code_to_optab (GET_CODE (y
));
931 target
= expand_binop (GET_MODE (y
), ot
,
932 XEXP (y
, 0), XEXP (y
, 1),
934 if (target
!= NULL_RTX
)
937 emit_move_insn (x
, target
);
954 inner
= XEXP (outer
, 0);
955 outmode
= GET_MODE (outer
);
956 bitpos
= SUBREG_BYTE (outer
) * BITS_PER_UNIT
;
957 store_bit_field (inner
, GET_MODE_BITSIZE (outmode
), bitpos
,
961 /* Return sequence of instructions generated by if conversion. This
962 function calls end_sequence() to end the current stream, ensures
963 that are instructions are unshared, recognizable non-jump insns.
964 On failure, this function returns a NULL_RTX. */
967 end_ifcvt_sequence (struct noce_if_info
*if_info
)
970 rtx seq
= get_insns ();
972 set_used_flags (if_info
->x
);
973 set_used_flags (if_info
->cond
);
974 set_used_flags (if_info
->a
);
975 set_used_flags (if_info
->b
);
976 unshare_all_rtl_in_chain (seq
);
979 /* Make sure that all of the instructions emitted are recognizable,
980 and that we haven't introduced a new jump instruction.
981 As an exercise for the reader, build a general mechanism that
982 allows proper placement of required clobbers. */
983 for (insn
= seq
; insn
; insn
= NEXT_INSN (insn
))
985 || recog_memoized (insn
) == -1)
991 /* Convert "if (a != b) x = a; else x = b" into "x = a" and
992 "if (a == b) x = a; else x = b" into "x = b". */
995 noce_try_move (struct noce_if_info
*if_info
)
997 rtx cond
= if_info
->cond
;
998 enum rtx_code code
= GET_CODE (cond
);
1001 if (code
!= NE
&& code
!= EQ
)
1004 /* This optimization isn't valid if either A or B could be a NaN
1005 or a signed zero. */
1006 if (HONOR_NANS (GET_MODE (if_info
->x
))
1007 || HONOR_SIGNED_ZEROS (GET_MODE (if_info
->x
)))
1010 /* Check whether the operands of the comparison are A and in
1012 if ((rtx_equal_p (if_info
->a
, XEXP (cond
, 0))
1013 && rtx_equal_p (if_info
->b
, XEXP (cond
, 1)))
1014 || (rtx_equal_p (if_info
->a
, XEXP (cond
, 1))
1015 && rtx_equal_p (if_info
->b
, XEXP (cond
, 0))))
1017 y
= (code
== EQ
) ? if_info
->a
: if_info
->b
;
1019 /* Avoid generating the move if the source is the destination. */
1020 if (! rtx_equal_p (if_info
->x
, y
))
1023 noce_emit_move_insn (if_info
->x
, y
);
1024 seq
= end_ifcvt_sequence (if_info
);
1028 emit_insn_before_setloc (seq
, if_info
->jump
,
1029 INSN_LOCATION (if_info
->insn_a
));
1036 /* Convert "if (test) x = 1; else x = 0".
1038 Only try 0 and STORE_FLAG_VALUE here. Other combinations will be
1039 tried in noce_try_store_flag_constants after noce_try_cmove has had
1040 a go at the conversion. */
1043 noce_try_store_flag (struct noce_if_info
*if_info
)
1048 if (CONST_INT_P (if_info
->b
)
1049 && INTVAL (if_info
->b
) == STORE_FLAG_VALUE
1050 && if_info
->a
== const0_rtx
)
1052 else if (if_info
->b
== const0_rtx
1053 && CONST_INT_P (if_info
->a
)
1054 && INTVAL (if_info
->a
) == STORE_FLAG_VALUE
1055 && (reversed_comparison_code (if_info
->cond
, if_info
->jump
)
1063 target
= noce_emit_store_flag (if_info
, if_info
->x
, reversep
, 0);
1066 if (target
!= if_info
->x
)
1067 noce_emit_move_insn (if_info
->x
, target
);
1069 seq
= end_ifcvt_sequence (if_info
);
1073 emit_insn_before_setloc (seq
, if_info
->jump
,
1074 INSN_LOCATION (if_info
->insn_a
));
1084 /* Convert "if (test) x = a; else x = b", for A and B constant. */
1087 noce_try_store_flag_constants (struct noce_if_info
*if_info
)
1091 HOST_WIDE_INT itrue
, ifalse
, diff
, tmp
;
1092 int normalize
, can_reverse
;
1093 enum machine_mode mode
;
1095 if (CONST_INT_P (if_info
->a
)
1096 && CONST_INT_P (if_info
->b
))
1098 mode
= GET_MODE (if_info
->x
);
1099 ifalse
= INTVAL (if_info
->a
);
1100 itrue
= INTVAL (if_info
->b
);
1102 /* Make sure we can represent the difference between the two values. */
1103 if ((itrue
- ifalse
> 0)
1104 != ((ifalse
< 0) != (itrue
< 0) ? ifalse
< 0 : ifalse
< itrue
))
1107 diff
= trunc_int_for_mode (itrue
- ifalse
, mode
);
1109 can_reverse
= (reversed_comparison_code (if_info
->cond
, if_info
->jump
)
1113 if (diff
== STORE_FLAG_VALUE
|| diff
== -STORE_FLAG_VALUE
)
1115 else if (ifalse
== 0 && exact_log2 (itrue
) >= 0
1116 && (STORE_FLAG_VALUE
== 1
1117 || if_info
->branch_cost
>= 2))
1119 else if (itrue
== 0 && exact_log2 (ifalse
) >= 0 && can_reverse
1120 && (STORE_FLAG_VALUE
== 1 || if_info
->branch_cost
>= 2))
1121 normalize
= 1, reversep
= 1;
1122 else if (itrue
== -1
1123 && (STORE_FLAG_VALUE
== -1
1124 || if_info
->branch_cost
>= 2))
1126 else if (ifalse
== -1 && can_reverse
1127 && (STORE_FLAG_VALUE
== -1 || if_info
->branch_cost
>= 2))
1128 normalize
= -1, reversep
= 1;
1129 else if ((if_info
->branch_cost
>= 2 && STORE_FLAG_VALUE
== -1)
1130 || if_info
->branch_cost
>= 3)
1137 tmp
= itrue
; itrue
= ifalse
; ifalse
= tmp
;
1138 diff
= trunc_int_for_mode (-diff
, mode
);
1142 target
= noce_emit_store_flag (if_info
, if_info
->x
, reversep
, normalize
);
1149 /* if (test) x = 3; else x = 4;
1150 => x = 3 + (test == 0); */
1151 if (diff
== STORE_FLAG_VALUE
|| diff
== -STORE_FLAG_VALUE
)
1153 target
= expand_simple_binop (mode
,
1154 (diff
== STORE_FLAG_VALUE
1156 GEN_INT (ifalse
), target
, if_info
->x
, 0,
1160 /* if (test) x = 8; else x = 0;
1161 => x = (test != 0) << 3; */
1162 else if (ifalse
== 0 && (tmp
= exact_log2 (itrue
)) >= 0)
1164 target
= expand_simple_binop (mode
, ASHIFT
,
1165 target
, GEN_INT (tmp
), if_info
->x
, 0,
1169 /* if (test) x = -1; else x = b;
1170 => x = -(test != 0) | b; */
1171 else if (itrue
== -1)
1173 target
= expand_simple_binop (mode
, IOR
,
1174 target
, GEN_INT (ifalse
), if_info
->x
, 0,
1178 /* if (test) x = a; else x = b;
1179 => x = (-(test != 0) & (b - a)) + a; */
1182 target
= expand_simple_binop (mode
, AND
,
1183 target
, GEN_INT (diff
), if_info
->x
, 0,
1186 target
= expand_simple_binop (mode
, PLUS
,
1187 target
, GEN_INT (ifalse
),
1188 if_info
->x
, 0, OPTAB_WIDEN
);
1197 if (target
!= if_info
->x
)
1198 noce_emit_move_insn (if_info
->x
, target
);
1200 seq
= end_ifcvt_sequence (if_info
);
1204 emit_insn_before_setloc (seq
, if_info
->jump
,
1205 INSN_LOCATION (if_info
->insn_a
));
1212 /* Convert "if (test) foo++" into "foo += (test != 0)", and
1213 similarly for "foo--". */
1216 noce_try_addcc (struct noce_if_info
*if_info
)
1219 int subtract
, normalize
;
1221 if (GET_CODE (if_info
->a
) == PLUS
1222 && rtx_equal_p (XEXP (if_info
->a
, 0), if_info
->b
)
1223 && (reversed_comparison_code (if_info
->cond
, if_info
->jump
)
1226 rtx cond
= if_info
->cond
;
1227 enum rtx_code code
= reversed_comparison_code (cond
, if_info
->jump
);
1229 /* First try to use addcc pattern. */
1230 if (general_operand (XEXP (cond
, 0), VOIDmode
)
1231 && general_operand (XEXP (cond
, 1), VOIDmode
))
1234 target
= emit_conditional_add (if_info
->x
, code
,
1239 XEXP (if_info
->a
, 1),
1240 GET_MODE (if_info
->x
),
1241 (code
== LTU
|| code
== GEU
1242 || code
== LEU
|| code
== GTU
));
1245 if (target
!= if_info
->x
)
1246 noce_emit_move_insn (if_info
->x
, target
);
1248 seq
= end_ifcvt_sequence (if_info
);
1252 emit_insn_before_setloc (seq
, if_info
->jump
,
1253 INSN_LOCATION (if_info
->insn_a
));
1259 /* If that fails, construct conditional increment or decrement using
1261 if (if_info
->branch_cost
>= 2
1262 && (XEXP (if_info
->a
, 1) == const1_rtx
1263 || XEXP (if_info
->a
, 1) == constm1_rtx
))
1266 if (STORE_FLAG_VALUE
== INTVAL (XEXP (if_info
->a
, 1)))
1267 subtract
= 0, normalize
= 0;
1268 else if (-STORE_FLAG_VALUE
== INTVAL (XEXP (if_info
->a
, 1)))
1269 subtract
= 1, normalize
= 0;
1271 subtract
= 0, normalize
= INTVAL (XEXP (if_info
->a
, 1));
1274 target
= noce_emit_store_flag (if_info
,
1275 gen_reg_rtx (GET_MODE (if_info
->x
)),
1279 target
= expand_simple_binop (GET_MODE (if_info
->x
),
1280 subtract
? MINUS
: PLUS
,
1281 if_info
->b
, target
, if_info
->x
,
1285 if (target
!= if_info
->x
)
1286 noce_emit_move_insn (if_info
->x
, target
);
1288 seq
= end_ifcvt_sequence (if_info
);
1292 emit_insn_before_setloc (seq
, if_info
->jump
,
1293 INSN_LOCATION (if_info
->insn_a
));
1303 /* Convert "if (test) x = 0;" to "x &= -(test == 0);" */
1306 noce_try_store_flag_mask (struct noce_if_info
*if_info
)
1312 if ((if_info
->branch_cost
>= 2
1313 || STORE_FLAG_VALUE
== -1)
1314 && ((if_info
->a
== const0_rtx
1315 && rtx_equal_p (if_info
->b
, if_info
->x
))
1316 || ((reversep
= (reversed_comparison_code (if_info
->cond
,
1319 && if_info
->b
== const0_rtx
1320 && rtx_equal_p (if_info
->a
, if_info
->x
))))
1323 target
= noce_emit_store_flag (if_info
,
1324 gen_reg_rtx (GET_MODE (if_info
->x
)),
1327 target
= expand_simple_binop (GET_MODE (if_info
->x
), AND
,
1329 target
, if_info
->x
, 0,
1334 if (target
!= if_info
->x
)
1335 noce_emit_move_insn (if_info
->x
, target
);
1337 seq
= end_ifcvt_sequence (if_info
);
1341 emit_insn_before_setloc (seq
, if_info
->jump
,
1342 INSN_LOCATION (if_info
->insn_a
));
1352 /* Helper function for noce_try_cmove and noce_try_cmove_arith. */
1355 noce_emit_cmove (struct noce_if_info
*if_info
, rtx x
, enum rtx_code code
,
1356 rtx cmp_a
, rtx cmp_b
, rtx vfalse
, rtx vtrue
)
1358 rtx target ATTRIBUTE_UNUSED
;
1359 int unsignedp ATTRIBUTE_UNUSED
;
1361 /* If earliest == jump, try to build the cmove insn directly.
1362 This is helpful when combine has created some complex condition
1363 (like for alpha's cmovlbs) that we can't hope to regenerate
1364 through the normal interface. */
1366 if (if_info
->cond_earliest
== if_info
->jump
)
1370 tmp
= gen_rtx_fmt_ee (code
, GET_MODE (if_info
->cond
), cmp_a
, cmp_b
);
1371 tmp
= gen_rtx_IF_THEN_ELSE (GET_MODE (x
), tmp
, vtrue
, vfalse
);
1372 tmp
= gen_rtx_SET (VOIDmode
, x
, tmp
);
1375 tmp
= emit_insn (tmp
);
1377 if (recog_memoized (tmp
) >= 0)
1389 /* Don't even try if the comparison operands are weird. */
1390 if (! general_operand (cmp_a
, GET_MODE (cmp_a
))
1391 || ! general_operand (cmp_b
, GET_MODE (cmp_b
)))
1394 #if HAVE_conditional_move
1395 unsignedp
= (code
== LTU
|| code
== GEU
1396 || code
== LEU
|| code
== GTU
);
1398 target
= emit_conditional_move (x
, code
, cmp_a
, cmp_b
, VOIDmode
,
1399 vtrue
, vfalse
, GET_MODE (x
),
1404 /* We might be faced with a situation like:
1407 vtrue = (subreg:M (reg:N VTRUE) BYTE)
1408 vfalse = (subreg:M (reg:N VFALSE) BYTE)
1410 We can't do a conditional move in mode M, but it's possible that we
1411 could do a conditional move in mode N instead and take a subreg of
1414 If we can't create new pseudos, though, don't bother. */
1415 if (reload_completed
)
1418 if (GET_CODE (vtrue
) == SUBREG
&& GET_CODE (vfalse
) == SUBREG
)
1420 rtx reg_vtrue
= SUBREG_REG (vtrue
);
1421 rtx reg_vfalse
= SUBREG_REG (vfalse
);
1422 unsigned int byte_vtrue
= SUBREG_BYTE (vtrue
);
1423 unsigned int byte_vfalse
= SUBREG_BYTE (vfalse
);
1424 rtx promoted_target
;
1426 if (GET_MODE (reg_vtrue
) != GET_MODE (reg_vfalse
)
1427 || byte_vtrue
!= byte_vfalse
1428 || (SUBREG_PROMOTED_VAR_P (vtrue
)
1429 != SUBREG_PROMOTED_VAR_P (vfalse
))
1430 || (SUBREG_PROMOTED_UNSIGNED_P (vtrue
)
1431 != SUBREG_PROMOTED_UNSIGNED_P (vfalse
)))
1434 promoted_target
= gen_reg_rtx (GET_MODE (reg_vtrue
));
1436 target
= emit_conditional_move (promoted_target
, code
, cmp_a
, cmp_b
,
1437 VOIDmode
, reg_vtrue
, reg_vfalse
,
1438 GET_MODE (reg_vtrue
), unsignedp
);
1439 /* Nope, couldn't do it in that mode either. */
1443 target
= gen_rtx_SUBREG (GET_MODE (vtrue
), promoted_target
, byte_vtrue
);
1444 SUBREG_PROMOTED_VAR_P (target
) = SUBREG_PROMOTED_VAR_P (vtrue
);
1445 SUBREG_PROMOTED_UNSIGNED_SET (target
, SUBREG_PROMOTED_UNSIGNED_P (vtrue
));
1446 emit_move_insn (x
, target
);
1452 /* We'll never get here, as noce_process_if_block doesn't call the
1453 functions involved. Ifdef code, however, should be discouraged
1454 because it leads to typos in the code not selected. However,
1455 emit_conditional_move won't exist either. */
1460 /* Try only simple constants and registers here. More complex cases
1461 are handled in noce_try_cmove_arith after noce_try_store_flag_arith
1462 has had a go at it. */
1465 noce_try_cmove (struct noce_if_info
*if_info
)
1470 if ((CONSTANT_P (if_info
->a
) || register_operand (if_info
->a
, VOIDmode
))
1471 && (CONSTANT_P (if_info
->b
) || register_operand (if_info
->b
, VOIDmode
)))
1475 code
= GET_CODE (if_info
->cond
);
1476 target
= noce_emit_cmove (if_info
, if_info
->x
, code
,
1477 XEXP (if_info
->cond
, 0),
1478 XEXP (if_info
->cond
, 1),
1479 if_info
->a
, if_info
->b
);
1483 if (target
!= if_info
->x
)
1484 noce_emit_move_insn (if_info
->x
, target
);
1486 seq
= end_ifcvt_sequence (if_info
);
1490 emit_insn_before_setloc (seq
, if_info
->jump
,
1491 INSN_LOCATION (if_info
->insn_a
));
1504 /* Try more complex cases involving conditional_move. */
1507 noce_try_cmove_arith (struct noce_if_info
*if_info
)
1519 /* A conditional move from two memory sources is equivalent to a
1520 conditional on their addresses followed by a load. Don't do this
1521 early because it'll screw alias analysis. Note that we've
1522 already checked for no side effects. */
1523 /* ??? FIXME: Magic number 5. */
1524 if (cse_not_expected
1525 && MEM_P (a
) && MEM_P (b
)
1526 && MEM_ADDR_SPACE (a
) == MEM_ADDR_SPACE (b
)
1527 && if_info
->branch_cost
>= 5)
1529 enum machine_mode address_mode
= get_address_mode (a
);
1533 x
= gen_reg_rtx (address_mode
);
1537 /* ??? We could handle this if we knew that a load from A or B could
1538 not trap or fault. This is also true if we've already loaded
1539 from the address along the path from ENTRY. */
1540 else if (may_trap_or_fault_p (a
) || may_trap_or_fault_p (b
))
1543 /* if (test) x = a + b; else x = c - d;
1550 code
= GET_CODE (if_info
->cond
);
1551 insn_a
= if_info
->insn_a
;
1552 insn_b
= if_info
->insn_b
;
1554 /* Total insn_rtx_cost should be smaller than branch cost. Exit
1555 if insn_rtx_cost can't be estimated. */
1559 = insn_rtx_cost (PATTERN (insn_a
),
1560 optimize_bb_for_speed_p (BLOCK_FOR_INSN (insn_a
)));
1561 if (insn_cost
== 0 || insn_cost
> COSTS_N_INSNS (if_info
->branch_cost
))
1570 += insn_rtx_cost (PATTERN (insn_b
),
1571 optimize_bb_for_speed_p (BLOCK_FOR_INSN (insn_b
)));
1572 if (insn_cost
== 0 || insn_cost
> COSTS_N_INSNS (if_info
->branch_cost
))
1576 /* Possibly rearrange operands to make things come out more natural. */
1577 if (reversed_comparison_code (if_info
->cond
, if_info
->jump
) != UNKNOWN
)
1580 if (rtx_equal_p (b
, x
))
1582 else if (general_operand (b
, GET_MODE (b
)))
1587 code
= reversed_comparison_code (if_info
->cond
, if_info
->jump
);
1588 tmp
= a
, a
= b
, b
= tmp
;
1589 tmp
= insn_a
, insn_a
= insn_b
, insn_b
= tmp
;
1598 /* If either operand is complex, load it into a register first.
1599 The best way to do this is to copy the original insn. In this
1600 way we preserve any clobbers etc that the insn may have had.
1601 This is of course not possible in the IS_MEM case. */
1602 if (! general_operand (a
, GET_MODE (a
)))
1608 tmp
= gen_reg_rtx (GET_MODE (a
));
1609 tmp
= emit_insn (gen_rtx_SET (VOIDmode
, tmp
, a
));
1612 goto end_seq_and_fail
;
1615 a
= gen_reg_rtx (GET_MODE (a
));
1616 tmp
= copy_rtx (insn_a
);
1617 set
= single_set (tmp
);
1619 tmp
= emit_insn (PATTERN (tmp
));
1621 if (recog_memoized (tmp
) < 0)
1622 goto end_seq_and_fail
;
1624 if (! general_operand (b
, GET_MODE (b
)))
1630 tmp
= gen_reg_rtx (GET_MODE (b
));
1631 tmp
= gen_rtx_SET (VOIDmode
, tmp
, b
);
1634 goto end_seq_and_fail
;
1637 b
= gen_reg_rtx (GET_MODE (b
));
1638 tmp
= copy_rtx (insn_b
);
1639 set
= single_set (tmp
);
1641 tmp
= PATTERN (tmp
);
1644 /* If insn to set up A clobbers any registers B depends on, try to
1645 swap insn that sets up A with the one that sets up B. If even
1646 that doesn't help, punt. */
1647 last
= get_last_insn ();
1648 if (last
&& modified_in_p (orig_b
, last
))
1650 tmp
= emit_insn_before (tmp
, get_insns ());
1651 if (modified_in_p (orig_a
, tmp
))
1652 goto end_seq_and_fail
;
1655 tmp
= emit_insn (tmp
);
1657 if (recog_memoized (tmp
) < 0)
1658 goto end_seq_and_fail
;
1661 target
= noce_emit_cmove (if_info
, x
, code
, XEXP (if_info
->cond
, 0),
1662 XEXP (if_info
->cond
, 1), a
, b
);
1665 goto end_seq_and_fail
;
1667 /* If we're handling a memory for above, emit the load now. */
1670 tmp
= gen_rtx_MEM (GET_MODE (if_info
->x
), target
);
1672 /* Copy over flags as appropriate. */
1673 if (MEM_VOLATILE_P (if_info
->a
) || MEM_VOLATILE_P (if_info
->b
))
1674 MEM_VOLATILE_P (tmp
) = 1;
1675 if (MEM_ALIAS_SET (if_info
->a
) == MEM_ALIAS_SET (if_info
->b
))
1676 set_mem_alias_set (tmp
, MEM_ALIAS_SET (if_info
->a
));
1678 MIN (MEM_ALIGN (if_info
->a
), MEM_ALIGN (if_info
->b
)));
1680 gcc_assert (MEM_ADDR_SPACE (if_info
->a
) == MEM_ADDR_SPACE (if_info
->b
));
1681 set_mem_addr_space (tmp
, MEM_ADDR_SPACE (if_info
->a
));
1683 noce_emit_move_insn (if_info
->x
, tmp
);
1685 else if (target
!= x
)
1686 noce_emit_move_insn (x
, target
);
1688 tmp
= end_ifcvt_sequence (if_info
);
1692 emit_insn_before_setloc (tmp
, if_info
->jump
, INSN_LOCATION (if_info
->insn_a
));
1700 /* For most cases, the simplified condition we found is the best
1701 choice, but this is not the case for the min/max/abs transforms.
1702 For these we wish to know that it is A or B in the condition. */
1705 noce_get_alt_condition (struct noce_if_info
*if_info
, rtx target
,
1708 rtx cond
, set
, insn
;
1711 /* If target is already mentioned in the known condition, return it. */
1712 if (reg_mentioned_p (target
, if_info
->cond
))
1714 *earliest
= if_info
->cond_earliest
;
1715 return if_info
->cond
;
1718 set
= pc_set (if_info
->jump
);
1719 cond
= XEXP (SET_SRC (set
), 0);
1721 = GET_CODE (XEXP (SET_SRC (set
), 2)) == LABEL_REF
1722 && XEXP (XEXP (SET_SRC (set
), 2), 0) == JUMP_LABEL (if_info
->jump
);
1723 if (if_info
->then_else_reversed
)
1726 /* If we're looking for a constant, try to make the conditional
1727 have that constant in it. There are two reasons why it may
1728 not have the constant we want:
1730 1. GCC may have needed to put the constant in a register, because
1731 the target can't compare directly against that constant. For
1732 this case, we look for a SET immediately before the comparison
1733 that puts a constant in that register.
1735 2. GCC may have canonicalized the conditional, for example
1736 replacing "if x < 4" with "if x <= 3". We can undo that (or
1737 make equivalent types of changes) to get the constants we need
1738 if they're off by one in the right direction. */
1740 if (CONST_INT_P (target
))
1742 enum rtx_code code
= GET_CODE (if_info
->cond
);
1743 rtx op_a
= XEXP (if_info
->cond
, 0);
1744 rtx op_b
= XEXP (if_info
->cond
, 1);
1747 /* First, look to see if we put a constant in a register. */
1748 prev_insn
= prev_nonnote_insn (if_info
->cond_earliest
);
1750 && BLOCK_FOR_INSN (prev_insn
)
1751 == BLOCK_FOR_INSN (if_info
->cond_earliest
)
1752 && INSN_P (prev_insn
)
1753 && GET_CODE (PATTERN (prev_insn
)) == SET
)
1755 rtx src
= find_reg_equal_equiv_note (prev_insn
);
1757 src
= SET_SRC (PATTERN (prev_insn
));
1758 if (CONST_INT_P (src
))
1760 if (rtx_equal_p (op_a
, SET_DEST (PATTERN (prev_insn
))))
1762 else if (rtx_equal_p (op_b
, SET_DEST (PATTERN (prev_insn
))))
1765 if (CONST_INT_P (op_a
))
1770 code
= swap_condition (code
);
1775 /* Now, look to see if we can get the right constant by
1776 adjusting the conditional. */
1777 if (CONST_INT_P (op_b
))
1779 HOST_WIDE_INT desired_val
= INTVAL (target
);
1780 HOST_WIDE_INT actual_val
= INTVAL (op_b
);
1785 if (actual_val
== desired_val
+ 1)
1788 op_b
= GEN_INT (desired_val
);
1792 if (actual_val
== desired_val
- 1)
1795 op_b
= GEN_INT (desired_val
);
1799 if (actual_val
== desired_val
- 1)
1802 op_b
= GEN_INT (desired_val
);
1806 if (actual_val
== desired_val
+ 1)
1809 op_b
= GEN_INT (desired_val
);
1817 /* If we made any changes, generate a new conditional that is
1818 equivalent to what we started with, but has the right
1820 if (code
!= GET_CODE (if_info
->cond
)
1821 || op_a
!= XEXP (if_info
->cond
, 0)
1822 || op_b
!= XEXP (if_info
->cond
, 1))
1824 cond
= gen_rtx_fmt_ee (code
, GET_MODE (cond
), op_a
, op_b
);
1825 *earliest
= if_info
->cond_earliest
;
1830 cond
= canonicalize_condition (if_info
->jump
, cond
, reverse
,
1831 earliest
, target
, false, true);
1832 if (! cond
|| ! reg_mentioned_p (target
, cond
))
1835 /* We almost certainly searched back to a different place.
1836 Need to re-verify correct lifetimes. */
1838 /* X may not be mentioned in the range (cond_earliest, jump]. */
1839 for (insn
= if_info
->jump
; insn
!= *earliest
; insn
= PREV_INSN (insn
))
1840 if (INSN_P (insn
) && reg_overlap_mentioned_p (if_info
->x
, PATTERN (insn
)))
1843 /* A and B may not be modified in the range [cond_earliest, jump). */
1844 for (insn
= *earliest
; insn
!= if_info
->jump
; insn
= NEXT_INSN (insn
))
1846 && (modified_in_p (if_info
->a
, insn
)
1847 || modified_in_p (if_info
->b
, insn
)))
1853 /* Convert "if (a < b) x = a; else x = b;" to "x = min(a, b);", etc. */
1856 noce_try_minmax (struct noce_if_info
*if_info
)
1858 rtx cond
, earliest
, target
, seq
;
1859 enum rtx_code code
, op
;
1862 /* ??? Reject modes with NaNs or signed zeros since we don't know how
1863 they will be resolved with an SMIN/SMAX. It wouldn't be too hard
1864 to get the target to tell us... */
1865 if (HONOR_SIGNED_ZEROS (GET_MODE (if_info
->x
))
1866 || HONOR_NANS (GET_MODE (if_info
->x
)))
1869 cond
= noce_get_alt_condition (if_info
, if_info
->a
, &earliest
);
1873 /* Verify the condition is of the form we expect, and canonicalize
1874 the comparison code. */
1875 code
= GET_CODE (cond
);
1876 if (rtx_equal_p (XEXP (cond
, 0), if_info
->a
))
1878 if (! rtx_equal_p (XEXP (cond
, 1), if_info
->b
))
1881 else if (rtx_equal_p (XEXP (cond
, 1), if_info
->a
))
1883 if (! rtx_equal_p (XEXP (cond
, 0), if_info
->b
))
1885 code
= swap_condition (code
);
1890 /* Determine what sort of operation this is. Note that the code is for
1891 a taken branch, so the code->operation mapping appears backwards. */
1924 target
= expand_simple_binop (GET_MODE (if_info
->x
), op
,
1925 if_info
->a
, if_info
->b
,
1926 if_info
->x
, unsignedp
, OPTAB_WIDEN
);
1932 if (target
!= if_info
->x
)
1933 noce_emit_move_insn (if_info
->x
, target
);
1935 seq
= end_ifcvt_sequence (if_info
);
1939 emit_insn_before_setloc (seq
, if_info
->jump
, INSN_LOCATION (if_info
->insn_a
));
1940 if_info
->cond
= cond
;
1941 if_info
->cond_earliest
= earliest
;
1946 /* Convert "if (a < 0) x = -a; else x = a;" to "x = abs(a);",
1947 "if (a < 0) x = ~a; else x = a;" to "x = one_cmpl_abs(a);",
1951 noce_try_abs (struct noce_if_info
*if_info
)
1953 rtx cond
, earliest
, target
, seq
, a
, b
, c
;
1955 bool one_cmpl
= false;
1957 /* Reject modes with signed zeros. */
1958 if (HONOR_SIGNED_ZEROS (GET_MODE (if_info
->x
)))
1961 /* Recognize A and B as constituting an ABS or NABS. The canonical
1962 form is a branch around the negation, taken when the object is the
1963 first operand of a comparison against 0 that evaluates to true. */
1966 if (GET_CODE (a
) == NEG
&& rtx_equal_p (XEXP (a
, 0), b
))
1968 else if (GET_CODE (b
) == NEG
&& rtx_equal_p (XEXP (b
, 0), a
))
1970 c
= a
; a
= b
; b
= c
;
1973 else if (GET_CODE (a
) == NOT
&& rtx_equal_p (XEXP (a
, 0), b
))
1978 else if (GET_CODE (b
) == NOT
&& rtx_equal_p (XEXP (b
, 0), a
))
1980 c
= a
; a
= b
; b
= c
;
1987 cond
= noce_get_alt_condition (if_info
, b
, &earliest
);
1991 /* Verify the condition is of the form we expect. */
1992 if (rtx_equal_p (XEXP (cond
, 0), b
))
1994 else if (rtx_equal_p (XEXP (cond
, 1), b
))
2002 /* Verify that C is zero. Search one step backward for a
2003 REG_EQUAL note or a simple source if necessary. */
2006 rtx set
, insn
= prev_nonnote_insn (earliest
);
2008 && BLOCK_FOR_INSN (insn
) == BLOCK_FOR_INSN (earliest
)
2009 && (set
= single_set (insn
))
2010 && rtx_equal_p (SET_DEST (set
), c
))
2012 rtx note
= find_reg_equal_equiv_note (insn
);
2022 && GET_CODE (XEXP (c
, 0)) == SYMBOL_REF
2023 && CONSTANT_POOL_ADDRESS_P (XEXP (c
, 0)))
2024 c
= get_pool_constant (XEXP (c
, 0));
2026 /* Work around funny ideas get_condition has wrt canonicalization.
2027 Note that these rtx constants are known to be CONST_INT, and
2028 therefore imply integer comparisons. */
2029 if (c
== constm1_rtx
&& GET_CODE (cond
) == GT
)
2031 else if (c
== const1_rtx
&& GET_CODE (cond
) == LT
)
2033 else if (c
!= CONST0_RTX (GET_MODE (b
)))
2036 /* Determine what sort of operation this is. */
2037 switch (GET_CODE (cond
))
2056 target
= expand_one_cmpl_abs_nojump (GET_MODE (if_info
->x
), b
,
2059 target
= expand_abs_nojump (GET_MODE (if_info
->x
), b
, if_info
->x
, 1);
2061 /* ??? It's a quandary whether cmove would be better here, especially
2062 for integers. Perhaps combine will clean things up. */
2063 if (target
&& negate
)
2066 target
= expand_simple_unop (GET_MODE (target
), NOT
, target
,
2069 target
= expand_simple_unop (GET_MODE (target
), NEG
, target
,
2079 if (target
!= if_info
->x
)
2080 noce_emit_move_insn (if_info
->x
, target
);
2082 seq
= end_ifcvt_sequence (if_info
);
2086 emit_insn_before_setloc (seq
, if_info
->jump
, INSN_LOCATION (if_info
->insn_a
));
2087 if_info
->cond
= cond
;
2088 if_info
->cond_earliest
= earliest
;
2093 /* Convert "if (m < 0) x = b; else x = 0;" to "x = (m >> C) & b;". */
2096 noce_try_sign_mask (struct noce_if_info
*if_info
)
2098 rtx cond
, t
, m
, c
, seq
;
2099 enum machine_mode mode
;
2101 bool t_unconditional
;
2103 cond
= if_info
->cond
;
2104 code
= GET_CODE (cond
);
2109 if (if_info
->a
== const0_rtx
)
2111 if ((code
== LT
&& c
== const0_rtx
)
2112 || (code
== LE
&& c
== constm1_rtx
))
2115 else if (if_info
->b
== const0_rtx
)
2117 if ((code
== GE
&& c
== const0_rtx
)
2118 || (code
== GT
&& c
== constm1_rtx
))
2122 if (! t
|| side_effects_p (t
))
2125 /* We currently don't handle different modes. */
2126 mode
= GET_MODE (t
);
2127 if (GET_MODE (m
) != mode
)
2130 /* This is only profitable if T is unconditionally executed/evaluated in the
2131 original insn sequence or T is cheap. The former happens if B is the
2132 non-zero (T) value and if INSN_B was taken from TEST_BB, or there was no
2133 INSN_B which can happen for e.g. conditional stores to memory. For the
2134 cost computation use the block TEST_BB where the evaluation will end up
2135 after the transformation. */
2138 && (if_info
->insn_b
== NULL_RTX
2139 || BLOCK_FOR_INSN (if_info
->insn_b
) == if_info
->test_bb
));
2140 if (!(t_unconditional
2141 || (set_src_cost (t
, optimize_bb_for_speed_p (if_info
->test_bb
))
2142 < COSTS_N_INSNS (2))))
2146 /* Use emit_store_flag to generate "m < 0 ? -1 : 0" instead of expanding
2147 "(signed) m >> 31" directly. This benefits targets with specialized
2148 insns to obtain the signmask, but still uses ashr_optab otherwise. */
2149 m
= emit_store_flag (gen_reg_rtx (mode
), LT
, m
, const0_rtx
, mode
, 0, -1);
2150 t
= m
? expand_binop (mode
, and_optab
, m
, t
, NULL_RTX
, 0, OPTAB_DIRECT
)
2159 noce_emit_move_insn (if_info
->x
, t
);
2161 seq
= end_ifcvt_sequence (if_info
);
2165 emit_insn_before_setloc (seq
, if_info
->jump
, INSN_LOCATION (if_info
->insn_a
));
2170 /* Optimize away "if (x & C) x |= C" and similar bit manipulation
2174 noce_try_bitop (struct noce_if_info
*if_info
)
2176 rtx cond
, x
, a
, result
, seq
;
2177 enum machine_mode mode
;
2182 cond
= if_info
->cond
;
2183 code
= GET_CODE (cond
);
2185 /* Check for no else condition. */
2186 if (! rtx_equal_p (x
, if_info
->b
))
2189 /* Check for a suitable condition. */
2190 if (code
!= NE
&& code
!= EQ
)
2192 if (XEXP (cond
, 1) != const0_rtx
)
2194 cond
= XEXP (cond
, 0);
2196 /* ??? We could also handle AND here. */
2197 if (GET_CODE (cond
) == ZERO_EXTRACT
)
2199 if (XEXP (cond
, 1) != const1_rtx
2200 || !CONST_INT_P (XEXP (cond
, 2))
2201 || ! rtx_equal_p (x
, XEXP (cond
, 0)))
2203 bitnum
= INTVAL (XEXP (cond
, 2));
2204 mode
= GET_MODE (x
);
2205 if (BITS_BIG_ENDIAN
)
2206 bitnum
= GET_MODE_BITSIZE (mode
) - 1 - bitnum
;
2207 if (bitnum
< 0 || bitnum
>= HOST_BITS_PER_WIDE_INT
)
2214 if (GET_CODE (a
) == IOR
|| GET_CODE (a
) == XOR
)
2216 /* Check for "if (X & C) x = x op C". */
2217 if (! rtx_equal_p (x
, XEXP (a
, 0))
2218 || !CONST_INT_P (XEXP (a
, 1))
2219 || (INTVAL (XEXP (a
, 1)) & GET_MODE_MASK (mode
))
2220 != (unsigned HOST_WIDE_INT
) 1 << bitnum
)
2223 /* if ((x & C) == 0) x |= C; is transformed to x |= C. */
2224 /* if ((x & C) != 0) x |= C; is transformed to nothing. */
2225 if (GET_CODE (a
) == IOR
)
2226 result
= (code
== NE
) ? a
: NULL_RTX
;
2227 else if (code
== NE
)
2229 /* if ((x & C) == 0) x ^= C; is transformed to x |= C. */
2230 result
= gen_int_mode ((HOST_WIDE_INT
) 1 << bitnum
, mode
);
2231 result
= simplify_gen_binary (IOR
, mode
, x
, result
);
2235 /* if ((x & C) != 0) x ^= C; is transformed to x &= ~C. */
2236 result
= gen_int_mode (~((HOST_WIDE_INT
) 1 << bitnum
), mode
);
2237 result
= simplify_gen_binary (AND
, mode
, x
, result
);
2240 else if (GET_CODE (a
) == AND
)
2242 /* Check for "if (X & C) x &= ~C". */
2243 if (! rtx_equal_p (x
, XEXP (a
, 0))
2244 || !CONST_INT_P (XEXP (a
, 1))
2245 || (INTVAL (XEXP (a
, 1)) & GET_MODE_MASK (mode
))
2246 != (~((HOST_WIDE_INT
) 1 << bitnum
) & GET_MODE_MASK (mode
)))
2249 /* if ((x & C) == 0) x &= ~C; is transformed to nothing. */
2250 /* if ((x & C) != 0) x &= ~C; is transformed to x &= ~C. */
2251 result
= (code
== EQ
) ? a
: NULL_RTX
;
2259 noce_emit_move_insn (x
, result
);
2260 seq
= end_ifcvt_sequence (if_info
);
2264 emit_insn_before_setloc (seq
, if_info
->jump
,
2265 INSN_LOCATION (if_info
->insn_a
));
2271 /* Similar to get_condition, only the resulting condition must be
2272 valid at JUMP, instead of at EARLIEST.
2274 If THEN_ELSE_REVERSED is true, the fallthrough does not go to the
2275 THEN block of the caller, and we have to reverse the condition. */
2278 noce_get_condition (rtx jump
, rtx
*earliest
, bool then_else_reversed
)
2283 if (! any_condjump_p (jump
))
2286 set
= pc_set (jump
);
2288 /* If this branches to JUMP_LABEL when the condition is false,
2289 reverse the condition. */
2290 reverse
= (GET_CODE (XEXP (SET_SRC (set
), 2)) == LABEL_REF
2291 && XEXP (XEXP (SET_SRC (set
), 2), 0) == JUMP_LABEL (jump
));
2293 /* We may have to reverse because the caller's if block is not canonical,
2294 i.e. the THEN block isn't the fallthrough block for the TEST block
2295 (see find_if_header). */
2296 if (then_else_reversed
)
2299 /* If the condition variable is a register and is MODE_INT, accept it. */
2301 cond
= XEXP (SET_SRC (set
), 0);
2302 tmp
= XEXP (cond
, 0);
2303 if (REG_P (tmp
) && GET_MODE_CLASS (GET_MODE (tmp
)) == MODE_INT
2304 && (GET_MODE (tmp
) != BImode
2305 || !targetm
.small_register_classes_for_mode_p (BImode
)))
2310 cond
= gen_rtx_fmt_ee (reverse_condition (GET_CODE (cond
)),
2311 GET_MODE (cond
), tmp
, XEXP (cond
, 1));
2315 /* Otherwise, fall back on canonicalize_condition to do the dirty
2316 work of manipulating MODE_CC values and COMPARE rtx codes. */
2317 tmp
= canonicalize_condition (jump
, cond
, reverse
, earliest
,
2318 NULL_RTX
, false, true);
2320 /* We don't handle side-effects in the condition, like handling
2321 REG_INC notes and making sure no duplicate conditions are emitted. */
2322 if (tmp
!= NULL_RTX
&& side_effects_p (tmp
))
2328 /* Return true if OP is ok for if-then-else processing. */
2331 noce_operand_ok (const_rtx op
)
2333 if (side_effects_p (op
))
2336 /* We special-case memories, so handle any of them with
2337 no address side effects. */
2339 return ! side_effects_p (XEXP (op
, 0));
2341 return ! may_trap_p (op
);
2344 /* Return true if a write into MEM may trap or fault. */
2347 noce_mem_write_may_trap_or_fault_p (const_rtx mem
)
2351 if (MEM_READONLY_P (mem
))
2354 if (may_trap_or_fault_p (mem
))
2357 addr
= XEXP (mem
, 0);
2359 /* Call target hook to avoid the effects of -fpic etc.... */
2360 addr
= targetm
.delegitimize_address (addr
);
2363 switch (GET_CODE (addr
))
2371 addr
= XEXP (addr
, 0);
2375 addr
= XEXP (addr
, 1);
2378 if (CONST_INT_P (XEXP (addr
, 1)))
2379 addr
= XEXP (addr
, 0);
2386 if (SYMBOL_REF_DECL (addr
)
2387 && decl_readonly_section (SYMBOL_REF_DECL (addr
), 0))
2397 /* Return whether we can use store speculation for MEM. TOP_BB is the
2398 basic block above the conditional block where we are considering
2399 doing the speculative store. We look for whether MEM is set
2400 unconditionally later in the function. */
2403 noce_can_store_speculate_p (basic_block top_bb
, const_rtx mem
)
2405 basic_block dominator
;
2407 for (dominator
= get_immediate_dominator (CDI_POST_DOMINATORS
, top_bb
);
2409 dominator
= get_immediate_dominator (CDI_POST_DOMINATORS
, dominator
))
2413 FOR_BB_INSNS (dominator
, insn
)
2415 /* If we see something that might be a memory barrier, we
2416 have to stop looking. Even if the MEM is set later in
2417 the function, we still don't want to set it
2418 unconditionally before the barrier. */
2420 && (volatile_insn_p (PATTERN (insn
))
2421 || (CALL_P (insn
) && (!RTL_CONST_CALL_P (insn
)))))
2424 if (memory_must_be_modified_in_insn_p (mem
, insn
))
2426 if (modified_in_p (XEXP (mem
, 0), insn
))
2435 /* Given a simple IF-THEN-JOIN or IF-THEN-ELSE-JOIN block, attempt to convert
2436 it without using conditional execution. Return TRUE if we were successful
2437 at converting the block. */
2440 noce_process_if_block (struct noce_if_info
*if_info
)
2442 basic_block test_bb
= if_info
->test_bb
; /* test block */
2443 basic_block then_bb
= if_info
->then_bb
; /* THEN */
2444 basic_block else_bb
= if_info
->else_bb
; /* ELSE or NULL */
2445 basic_block join_bb
= if_info
->join_bb
; /* JOIN */
2446 rtx jump
= if_info
->jump
;
2447 rtx cond
= if_info
->cond
;
2450 rtx orig_x
, x
, a
, b
;
2452 /* We're looking for patterns of the form
2454 (1) if (...) x = a; else x = b;
2455 (2) x = b; if (...) x = a;
2456 (3) if (...) x = a; // as if with an initial x = x.
2458 The later patterns require jumps to be more expensive.
2460 ??? For future expansion, look for multiple X in such patterns. */
2462 /* Look for one of the potential sets. */
2463 insn_a
= first_active_insn (then_bb
);
2465 || insn_a
!= last_active_insn (then_bb
, FALSE
)
2466 || (set_a
= single_set (insn_a
)) == NULL_RTX
)
2469 x
= SET_DEST (set_a
);
2470 a
= SET_SRC (set_a
);
2472 /* Look for the other potential set. Make sure we've got equivalent
2474 /* ??? This is overconservative. Storing to two different mems is
2475 as easy as conditionally computing the address. Storing to a
2476 single mem merely requires a scratch memory to use as one of the
2477 destination addresses; often the memory immediately below the
2478 stack pointer is available for this. */
2482 insn_b
= first_active_insn (else_bb
);
2484 || insn_b
!= last_active_insn (else_bb
, FALSE
)
2485 || (set_b
= single_set (insn_b
)) == NULL_RTX
2486 || ! rtx_equal_p (x
, SET_DEST (set_b
)))
2491 insn_b
= prev_nonnote_nondebug_insn (if_info
->cond_earliest
);
2492 /* We're going to be moving the evaluation of B down from above
2493 COND_EARLIEST to JUMP. Make sure the relevant data is still
2496 || BLOCK_FOR_INSN (insn_b
) != BLOCK_FOR_INSN (if_info
->cond_earliest
)
2497 || !NONJUMP_INSN_P (insn_b
)
2498 || (set_b
= single_set (insn_b
)) == NULL_RTX
2499 || ! rtx_equal_p (x
, SET_DEST (set_b
))
2500 || ! noce_operand_ok (SET_SRC (set_b
))
2501 || reg_overlap_mentioned_p (x
, SET_SRC (set_b
))
2502 || modified_between_p (SET_SRC (set_b
), insn_b
, jump
)
2503 /* Avoid extending the lifetime of hard registers on small
2504 register class machines. */
2505 || (REG_P (SET_SRC (set_b
))
2506 && HARD_REGISTER_P (SET_SRC (set_b
))
2507 && targetm
.small_register_classes_for_mode_p
2508 (GET_MODE (SET_SRC (set_b
))))
2509 /* Likewise with X. In particular this can happen when
2510 noce_get_condition looks farther back in the instruction
2511 stream than one might expect. */
2512 || reg_overlap_mentioned_p (x
, cond
)
2513 || reg_overlap_mentioned_p (x
, a
)
2514 || modified_between_p (x
, insn_b
, jump
))
2515 insn_b
= set_b
= NULL_RTX
;
2518 /* If x has side effects then only the if-then-else form is safe to
2519 convert. But even in that case we would need to restore any notes
2520 (such as REG_INC) at then end. That can be tricky if
2521 noce_emit_move_insn expands to more than one insn, so disable the
2522 optimization entirely for now if there are side effects. */
2523 if (side_effects_p (x
))
2526 b
= (set_b
? SET_SRC (set_b
) : x
);
2528 /* Only operate on register destinations, and even then avoid extending
2529 the lifetime of hard registers on small register class machines. */
2532 || (HARD_REGISTER_P (x
)
2533 && targetm
.small_register_classes_for_mode_p (GET_MODE (x
))))
2535 if (GET_MODE (x
) == BLKmode
)
2538 if (GET_CODE (x
) == ZERO_EXTRACT
2539 && (!CONST_INT_P (XEXP (x
, 1))
2540 || !CONST_INT_P (XEXP (x
, 2))))
2543 x
= gen_reg_rtx (GET_MODE (GET_CODE (x
) == STRICT_LOW_PART
2544 ? XEXP (x
, 0) : x
));
2547 /* Don't operate on sources that may trap or are volatile. */
2548 if (! noce_operand_ok (a
) || ! noce_operand_ok (b
))
2552 /* Set up the info block for our subroutines. */
2553 if_info
->insn_a
= insn_a
;
2554 if_info
->insn_b
= insn_b
;
2559 /* Try optimizations in some approximation of a useful order. */
2560 /* ??? Should first look to see if X is live incoming at all. If it
2561 isn't, we don't need anything but an unconditional set. */
2563 /* Look and see if A and B are really the same. Avoid creating silly
2564 cmove constructs that no one will fix up later. */
2565 if (rtx_equal_p (a
, b
))
2567 /* If we have an INSN_B, we don't have to create any new rtl. Just
2568 move the instruction that we already have. If we don't have an
2569 INSN_B, that means that A == X, and we've got a noop move. In
2570 that case don't do anything and let the code below delete INSN_A. */
2571 if (insn_b
&& else_bb
)
2575 if (else_bb
&& insn_b
== BB_END (else_bb
))
2576 BB_END (else_bb
) = PREV_INSN (insn_b
);
2577 reorder_insns (insn_b
, insn_b
, PREV_INSN (jump
));
2579 /* If there was a REG_EQUAL note, delete it since it may have been
2580 true due to this insn being after a jump. */
2581 if ((note
= find_reg_note (insn_b
, REG_EQUAL
, NULL_RTX
)) != 0)
2582 remove_note (insn_b
, note
);
2586 /* If we have "x = b; if (...) x = a;", and x has side-effects, then
2587 x must be executed twice. */
2588 else if (insn_b
&& side_effects_p (orig_x
))
2595 if (!set_b
&& MEM_P (orig_x
))
2597 /* Disallow the "if (...) x = a;" form (implicit "else x = x;")
2598 for optimizations if writing to x may trap or fault,
2599 i.e. it's a memory other than a static var or a stack slot,
2600 is misaligned on strict aligned machines or is read-only. If
2601 x is a read-only memory, then the program is valid only if we
2602 avoid the store into it. If there are stores on both the
2603 THEN and ELSE arms, then we can go ahead with the conversion;
2604 either the program is broken, or the condition is always
2605 false such that the other memory is selected. */
2606 if (noce_mem_write_may_trap_or_fault_p (orig_x
))
2609 /* Avoid store speculation: given "if (...) x = a" where x is a
2610 MEM, we only want to do the store if x is always set
2611 somewhere in the function. This avoids cases like
2612 if (pthread_mutex_trylock(mutex))
2614 where we only want global_variable to be changed if the mutex
2615 is held. FIXME: This should ideally be expressed directly in
2617 if (!noce_can_store_speculate_p (test_bb
, orig_x
))
2621 if (noce_try_move (if_info
))
2623 if (noce_try_store_flag (if_info
))
2625 if (noce_try_bitop (if_info
))
2627 if (noce_try_minmax (if_info
))
2629 if (noce_try_abs (if_info
))
2631 if (HAVE_conditional_move
2632 && noce_try_cmove (if_info
))
2634 if (! targetm
.have_conditional_execution ())
2636 if (noce_try_store_flag_constants (if_info
))
2638 if (noce_try_addcc (if_info
))
2640 if (noce_try_store_flag_mask (if_info
))
2642 if (HAVE_conditional_move
2643 && noce_try_cmove_arith (if_info
))
2645 if (noce_try_sign_mask (if_info
))
2649 if (!else_bb
&& set_b
)
2651 insn_b
= set_b
= NULL_RTX
;
2660 /* If we used a temporary, fix it up now. */
2666 noce_emit_move_insn (orig_x
, x
);
2668 set_used_flags (orig_x
);
2669 unshare_all_rtl_in_chain (seq
);
2672 emit_insn_before_setloc (seq
, BB_END (test_bb
), INSN_LOCATION (insn_a
));
2675 /* The original THEN and ELSE blocks may now be removed. The test block
2676 must now jump to the join block. If the test block and the join block
2677 can be merged, do so. */
2680 delete_basic_block (else_bb
);
2684 remove_edge (find_edge (test_bb
, join_bb
));
2686 remove_edge (find_edge (then_bb
, join_bb
));
2687 redirect_edge_and_branch_force (single_succ_edge (test_bb
), join_bb
);
2688 delete_basic_block (then_bb
);
2691 if (can_merge_blocks_p (test_bb
, join_bb
))
2693 merge_blocks (test_bb
, join_bb
);
2697 num_updated_if_blocks
++;
2701 /* Check whether a block is suitable for conditional move conversion.
2702 Every insn must be a simple set of a register to a constant or a
2703 register. For each assignment, store the value in the pointer map
2704 VALS, keyed indexed by register pointer, then store the register
2705 pointer in REGS. COND is the condition we will test. */
2708 check_cond_move_block (basic_block bb
,
2709 struct pointer_map_t
*vals
,
2715 /* We can only handle simple jumps at the end of the basic block.
2716 It is almost impossible to update the CFG otherwise. */
2718 if (JUMP_P (insn
) && !onlyjump_p (insn
))
2721 FOR_BB_INSNS (bb
, insn
)
2726 if (!NONDEBUG_INSN_P (insn
) || JUMP_P (insn
))
2728 set
= single_set (insn
);
2732 dest
= SET_DEST (set
);
2733 src
= SET_SRC (set
);
2735 || (HARD_REGISTER_P (dest
)
2736 && targetm
.small_register_classes_for_mode_p (GET_MODE (dest
))))
2739 if (!CONSTANT_P (src
) && !register_operand (src
, VOIDmode
))
2742 if (side_effects_p (src
) || side_effects_p (dest
))
2745 if (may_trap_p (src
) || may_trap_p (dest
))
2748 /* Don't try to handle this if the source register was
2749 modified earlier in the block. */
2751 && pointer_map_contains (vals
, src
))
2752 || (GET_CODE (src
) == SUBREG
&& REG_P (SUBREG_REG (src
))
2753 && pointer_map_contains (vals
, SUBREG_REG (src
))))
2756 /* Don't try to handle this if the destination register was
2757 modified earlier in the block. */
2758 if (pointer_map_contains (vals
, dest
))
2761 /* Don't try to handle this if the condition uses the
2762 destination register. */
2763 if (reg_overlap_mentioned_p (dest
, cond
))
2766 /* Don't try to handle this if the source register is modified
2767 later in the block. */
2768 if (!CONSTANT_P (src
)
2769 && modified_between_p (src
, insn
, NEXT_INSN (BB_END (bb
))))
2772 slot
= pointer_map_insert (vals
, (void *) dest
);
2773 *slot
= (void *) src
;
2775 regs
->safe_push (dest
);
2781 /* Given a basic block BB suitable for conditional move conversion,
2782 a condition COND, and pointer maps THEN_VALS and ELSE_VALS containing
2783 the register values depending on COND, emit the insns in the block as
2784 conditional moves. If ELSE_BLOCK is true, THEN_BB was already
2785 processed. The caller has started a sequence for the conversion.
2786 Return true if successful, false if something goes wrong. */
2789 cond_move_convert_if_block (struct noce_if_info
*if_infop
,
2790 basic_block bb
, rtx cond
,
2791 struct pointer_map_t
*then_vals
,
2792 struct pointer_map_t
*else_vals
,
2796 rtx insn
, cond_arg0
, cond_arg1
;
2798 code
= GET_CODE (cond
);
2799 cond_arg0
= XEXP (cond
, 0);
2800 cond_arg1
= XEXP (cond
, 1);
2802 FOR_BB_INSNS (bb
, insn
)
2804 rtx set
, target
, dest
, t
, e
;
2805 void **then_slot
, **else_slot
;
2807 /* ??? Maybe emit conditional debug insn? */
2808 if (!NONDEBUG_INSN_P (insn
) || JUMP_P (insn
))
2810 set
= single_set (insn
);
2811 gcc_assert (set
&& REG_P (SET_DEST (set
)));
2813 dest
= SET_DEST (set
);
2815 then_slot
= pointer_map_contains (then_vals
, dest
);
2816 else_slot
= pointer_map_contains (else_vals
, dest
);
2817 t
= then_slot
? (rtx
) *then_slot
: NULL_RTX
;
2818 e
= else_slot
? (rtx
) *else_slot
: NULL_RTX
;
2822 /* If this register was set in the then block, we already
2823 handled this case there. */
2836 target
= noce_emit_cmove (if_infop
, dest
, code
, cond_arg0
, cond_arg1
,
2842 noce_emit_move_insn (dest
, target
);
2848 /* Given a simple IF-THEN-JOIN or IF-THEN-ELSE-JOIN block, attempt to convert
2849 it using only conditional moves. Return TRUE if we were successful at
2850 converting the block. */
2853 cond_move_process_if_block (struct noce_if_info
*if_info
)
2855 basic_block test_bb
= if_info
->test_bb
;
2856 basic_block then_bb
= if_info
->then_bb
;
2857 basic_block else_bb
= if_info
->else_bb
;
2858 basic_block join_bb
= if_info
->join_bb
;
2859 rtx jump
= if_info
->jump
;
2860 rtx cond
= if_info
->cond
;
2864 struct pointer_map_t
*then_vals
;
2865 struct pointer_map_t
*else_vals
;
2866 vec
<rtx
> then_regs
= vNULL
;
2867 vec
<rtx
> else_regs
= vNULL
;
2869 int success_p
= FALSE
;
2871 /* Build a mapping for each block to the value used for each
2873 then_vals
= pointer_map_create ();
2874 else_vals
= pointer_map_create ();
2876 /* Make sure the blocks are suitable. */
2877 if (!check_cond_move_block (then_bb
, then_vals
, &then_regs
, cond
)
2879 && !check_cond_move_block (else_bb
, else_vals
, &else_regs
, cond
)))
2882 /* Make sure the blocks can be used together. If the same register
2883 is set in both blocks, and is not set to a constant in both
2884 cases, then both blocks must set it to the same register. We
2885 have already verified that if it is set to a register, that the
2886 source register does not change after the assignment. Also count
2887 the number of registers set in only one of the blocks. */
2889 FOR_EACH_VEC_ELT (then_regs
, i
, reg
)
2891 void **then_slot
= pointer_map_contains (then_vals
, reg
);
2892 void **else_slot
= pointer_map_contains (else_vals
, reg
);
2894 gcc_checking_assert (then_slot
);
2899 rtx then_val
= (rtx
) *then_slot
;
2900 rtx else_val
= (rtx
) *else_slot
;
2901 if (!CONSTANT_P (then_val
) && !CONSTANT_P (else_val
)
2902 && !rtx_equal_p (then_val
, else_val
))
2907 /* Finish off c for MAX_CONDITIONAL_EXECUTE. */
2908 FOR_EACH_VEC_ELT (else_regs
, i
, reg
)
2910 gcc_checking_assert (pointer_map_contains (else_vals
, reg
));
2911 if (!pointer_map_contains (then_vals
, reg
))
2915 /* Make sure it is reasonable to convert this block. What matters
2916 is the number of assignments currently made in only one of the
2917 branches, since if we convert we are going to always execute
2919 if (c
> MAX_CONDITIONAL_EXECUTE
)
2922 /* Try to emit the conditional moves. First do the then block,
2923 then do anything left in the else blocks. */
2925 if (!cond_move_convert_if_block (if_info
, then_bb
, cond
,
2926 then_vals
, else_vals
, false)
2928 && !cond_move_convert_if_block (if_info
, else_bb
, cond
,
2929 then_vals
, else_vals
, true)))
2934 seq
= end_ifcvt_sequence (if_info
);
2938 loc_insn
= first_active_insn (then_bb
);
2941 loc_insn
= first_active_insn (else_bb
);
2942 gcc_assert (loc_insn
);
2944 emit_insn_before_setloc (seq
, jump
, INSN_LOCATION (loc_insn
));
2948 delete_basic_block (else_bb
);
2952 remove_edge (find_edge (test_bb
, join_bb
));
2954 remove_edge (find_edge (then_bb
, join_bb
));
2955 redirect_edge_and_branch_force (single_succ_edge (test_bb
), join_bb
);
2956 delete_basic_block (then_bb
);
2959 if (can_merge_blocks_p (test_bb
, join_bb
))
2961 merge_blocks (test_bb
, join_bb
);
2965 num_updated_if_blocks
++;
2970 pointer_map_destroy (then_vals
);
2971 pointer_map_destroy (else_vals
);
2972 then_regs
.release ();
2973 else_regs
.release ();
2978 /* Determine if a given basic block heads a simple IF-THEN-JOIN or an
2979 IF-THEN-ELSE-JOIN block.
2981 If so, we'll try to convert the insns to not require the branch,
2982 using only transformations that do not require conditional execution.
2984 Return TRUE if we were successful at converting the block. */
2987 noce_find_if_block (basic_block test_bb
, edge then_edge
, edge else_edge
,
2990 basic_block then_bb
, else_bb
, join_bb
;
2991 bool then_else_reversed
= false;
2994 struct noce_if_info if_info
;
2996 /* We only ever should get here before reload. */
2997 gcc_assert (!reload_completed
);
2999 /* Recognize an IF-THEN-ELSE-JOIN block. */
3000 if (single_pred_p (then_edge
->dest
)
3001 && single_succ_p (then_edge
->dest
)
3002 && single_pred_p (else_edge
->dest
)
3003 && single_succ_p (else_edge
->dest
)
3004 && single_succ (then_edge
->dest
) == single_succ (else_edge
->dest
))
3006 then_bb
= then_edge
->dest
;
3007 else_bb
= else_edge
->dest
;
3008 join_bb
= single_succ (then_bb
);
3010 /* Recognize an IF-THEN-JOIN block. */
3011 else if (single_pred_p (then_edge
->dest
)
3012 && single_succ_p (then_edge
->dest
)
3013 && single_succ (then_edge
->dest
) == else_edge
->dest
)
3015 then_bb
= then_edge
->dest
;
3016 else_bb
= NULL_BLOCK
;
3017 join_bb
= else_edge
->dest
;
3019 /* Recognize an IF-ELSE-JOIN block. We can have those because the order
3020 of basic blocks in cfglayout mode does not matter, so the fallthrough
3021 edge can go to any basic block (and not just to bb->next_bb, like in
3023 else if (single_pred_p (else_edge
->dest
)
3024 && single_succ_p (else_edge
->dest
)
3025 && single_succ (else_edge
->dest
) == then_edge
->dest
)
3027 /* The noce transformations do not apply to IF-ELSE-JOIN blocks.
3028 To make this work, we have to invert the THEN and ELSE blocks
3029 and reverse the jump condition. */
3030 then_bb
= else_edge
->dest
;
3031 else_bb
= NULL_BLOCK
;
3032 join_bb
= single_succ (then_bb
);
3033 then_else_reversed
= true;
3036 /* Not a form we can handle. */
3039 /* The edges of the THEN and ELSE blocks cannot have complex edges. */
3040 if (single_succ_edge (then_bb
)->flags
& EDGE_COMPLEX
)
3043 && single_succ_edge (else_bb
)->flags
& EDGE_COMPLEX
)
3046 num_possible_if_blocks
++;
3051 "\nIF-THEN%s-JOIN block found, pass %d, test %d, then %d",
3052 (else_bb
) ? "-ELSE" : "",
3053 pass
, test_bb
->index
, then_bb
->index
);
3056 fprintf (dump_file
, ", else %d", else_bb
->index
);
3058 fprintf (dump_file
, ", join %d\n", join_bb
->index
);
3061 /* If the conditional jump is more than just a conditional
3062 jump, then we can not do if-conversion on this block. */
3063 jump
= BB_END (test_bb
);
3064 if (! onlyjump_p (jump
))
3067 /* If this is not a standard conditional jump, we can't parse it. */
3068 cond
= noce_get_condition (jump
, &cond_earliest
, then_else_reversed
);
3072 /* We must be comparing objects whose modes imply the size. */
3073 if (GET_MODE (XEXP (cond
, 0)) == BLKmode
)
3076 /* Initialize an IF_INFO struct to pass around. */
3077 memset (&if_info
, 0, sizeof if_info
);
3078 if_info
.test_bb
= test_bb
;
3079 if_info
.then_bb
= then_bb
;
3080 if_info
.else_bb
= else_bb
;
3081 if_info
.join_bb
= join_bb
;
3082 if_info
.cond
= cond
;
3083 if_info
.cond_earliest
= cond_earliest
;
3084 if_info
.jump
= jump
;
3085 if_info
.then_else_reversed
= then_else_reversed
;
3086 if_info
.branch_cost
= BRANCH_COST (optimize_bb_for_speed_p (test_bb
),
3087 predictable_edge_p (then_edge
));
3089 /* Do the real work. */
3091 if (noce_process_if_block (&if_info
))
3094 if (HAVE_conditional_move
3095 && cond_move_process_if_block (&if_info
))
3102 /* Merge the blocks and mark for local life update. */
3105 merge_if_block (struct ce_if_block
* ce_info
)
3107 basic_block test_bb
= ce_info
->test_bb
; /* last test block */
3108 basic_block then_bb
= ce_info
->then_bb
; /* THEN */
3109 basic_block else_bb
= ce_info
->else_bb
; /* ELSE or NULL */
3110 basic_block join_bb
= ce_info
->join_bb
; /* join block */
3111 basic_block combo_bb
;
3113 /* All block merging is done into the lower block numbers. */
3116 df_set_bb_dirty (test_bb
);
3118 /* Merge any basic blocks to handle && and || subtests. Each of
3119 the blocks are on the fallthru path from the predecessor block. */
3120 if (ce_info
->num_multiple_test_blocks
> 0)
3122 basic_block bb
= test_bb
;
3123 basic_block last_test_bb
= ce_info
->last_test_bb
;
3124 basic_block fallthru
= block_fallthru (bb
);
3129 fallthru
= block_fallthru (bb
);
3130 merge_blocks (combo_bb
, bb
);
3133 while (bb
!= last_test_bb
);
3136 /* Merge TEST block into THEN block. Normally the THEN block won't have a
3137 label, but it might if there were || tests. That label's count should be
3138 zero, and it normally should be removed. */
3142 merge_blocks (combo_bb
, then_bb
);
3146 /* The ELSE block, if it existed, had a label. That label count
3147 will almost always be zero, but odd things can happen when labels
3148 get their addresses taken. */
3151 merge_blocks (combo_bb
, else_bb
);
3155 /* If there was no join block reported, that means it was not adjacent
3156 to the others, and so we cannot merge them. */
3160 rtx last
= BB_END (combo_bb
);
3162 /* The outgoing edge for the current COMBO block should already
3163 be correct. Verify this. */
3164 if (EDGE_COUNT (combo_bb
->succs
) == 0)
3165 gcc_assert (find_reg_note (last
, REG_NORETURN
, NULL
)
3166 || (NONJUMP_INSN_P (last
)
3167 && GET_CODE (PATTERN (last
)) == TRAP_IF
3168 && (TRAP_CONDITION (PATTERN (last
))
3169 == const_true_rtx
)));
3172 /* There should still be something at the end of the THEN or ELSE
3173 blocks taking us to our final destination. */
3174 gcc_assert (JUMP_P (last
)
3175 || (EDGE_SUCC (combo_bb
, 0)->dest
== EXIT_BLOCK_PTR
3177 && SIBLING_CALL_P (last
))
3178 || ((EDGE_SUCC (combo_bb
, 0)->flags
& EDGE_EH
)
3179 && can_throw_internal (last
)));
3182 /* The JOIN block may have had quite a number of other predecessors too.
3183 Since we've already merged the TEST, THEN and ELSE blocks, we should
3184 have only one remaining edge from our if-then-else diamond. If there
3185 is more than one remaining edge, it must come from elsewhere. There
3186 may be zero incoming edges if the THEN block didn't actually join
3187 back up (as with a call to a non-return function). */
3188 else if (EDGE_COUNT (join_bb
->preds
) < 2
3189 && join_bb
!= EXIT_BLOCK_PTR
)
3191 /* We can merge the JOIN cleanly and update the dataflow try
3192 again on this pass.*/
3193 merge_blocks (combo_bb
, join_bb
);
3198 /* We cannot merge the JOIN. */
3200 /* The outgoing edge for the current COMBO block should already
3201 be correct. Verify this. */
3202 gcc_assert (single_succ_p (combo_bb
)
3203 && single_succ (combo_bb
) == join_bb
);
3205 /* Remove the jump and cruft from the end of the COMBO block. */
3206 if (join_bb
!= EXIT_BLOCK_PTR
)
3207 tidy_fallthru_edge (single_succ_edge (combo_bb
));
3210 num_updated_if_blocks
++;
3213 /* Find a block ending in a simple IF condition and try to transform it
3214 in some way. When converting a multi-block condition, put the new code
3215 in the first such block and delete the rest. Return a pointer to this
3216 first block if some transformation was done. Return NULL otherwise. */
3219 find_if_header (basic_block test_bb
, int pass
)
3221 ce_if_block_t ce_info
;
3225 /* The kind of block we're looking for has exactly two successors. */
3226 if (EDGE_COUNT (test_bb
->succs
) != 2)
3229 then_edge
= EDGE_SUCC (test_bb
, 0);
3230 else_edge
= EDGE_SUCC (test_bb
, 1);
3232 if (df_get_bb_dirty (then_edge
->dest
))
3234 if (df_get_bb_dirty (else_edge
->dest
))
3237 /* Neither edge should be abnormal. */
3238 if ((then_edge
->flags
& EDGE_COMPLEX
)
3239 || (else_edge
->flags
& EDGE_COMPLEX
))
3242 /* Nor exit the loop. */
3243 if ((then_edge
->flags
& EDGE_LOOP_EXIT
)
3244 || (else_edge
->flags
& EDGE_LOOP_EXIT
))
3247 /* The THEN edge is canonically the one that falls through. */
3248 if (then_edge
->flags
& EDGE_FALLTHRU
)
3250 else if (else_edge
->flags
& EDGE_FALLTHRU
)
3253 else_edge
= then_edge
;
3257 /* Otherwise this must be a multiway branch of some sort. */
3260 memset (&ce_info
, 0, sizeof (ce_info
));
3261 ce_info
.test_bb
= test_bb
;
3262 ce_info
.then_bb
= then_edge
->dest
;
3263 ce_info
.else_bb
= else_edge
->dest
;
3264 ce_info
.pass
= pass
;
3266 #ifdef IFCVT_MACHDEP_INIT
3267 IFCVT_MACHDEP_INIT (&ce_info
);
3270 if (!reload_completed
3271 && noce_find_if_block (test_bb
, then_edge
, else_edge
, pass
))
3274 if (reload_completed
3275 && targetm
.have_conditional_execution ()
3276 && cond_exec_find_if_block (&ce_info
))
3280 && optab_handler (ctrap_optab
, word_mode
) != CODE_FOR_nothing
3281 && find_cond_trap (test_bb
, then_edge
, else_edge
))
3284 if (dom_info_state (CDI_POST_DOMINATORS
) >= DOM_NO_FAST_QUERY
3285 && (reload_completed
|| !targetm
.have_conditional_execution ()))
3287 if (find_if_case_1 (test_bb
, then_edge
, else_edge
))
3289 if (find_if_case_2 (test_bb
, then_edge
, else_edge
))
3297 fprintf (dump_file
, "Conversion succeeded on pass %d.\n", pass
);
3298 /* Set this so we continue looking. */
3299 cond_exec_changed_p
= TRUE
;
3300 return ce_info
.test_bb
;
3303 /* Return true if a block has two edges, one of which falls through to the next
3304 block, and the other jumps to a specific block, so that we can tell if the
3305 block is part of an && test or an || test. Returns either -1 or the number
3306 of non-note, non-jump, non-USE/CLOBBER insns in the block. */
3309 block_jumps_and_fallthru_p (basic_block cur_bb
, basic_block target_bb
)
3312 int fallthru_p
= FALSE
;
3319 if (!cur_bb
|| !target_bb
)
3322 /* If no edges, obviously it doesn't jump or fallthru. */
3323 if (EDGE_COUNT (cur_bb
->succs
) == 0)
3326 FOR_EACH_EDGE (cur_edge
, ei
, cur_bb
->succs
)
3328 if (cur_edge
->flags
& EDGE_COMPLEX
)
3329 /* Anything complex isn't what we want. */
3332 else if (cur_edge
->flags
& EDGE_FALLTHRU
)
3335 else if (cur_edge
->dest
== target_bb
)
3342 if ((jump_p
& fallthru_p
) == 0)
3345 /* Don't allow calls in the block, since this is used to group && and ||
3346 together for conditional execution support. ??? we should support
3347 conditional execution support across calls for IA-64 some day, but
3348 for now it makes the code simpler. */
3349 end
= BB_END (cur_bb
);
3350 insn
= BB_HEAD (cur_bb
);
3352 while (insn
!= NULL_RTX
)
3359 && !DEBUG_INSN_P (insn
)
3360 && GET_CODE (PATTERN (insn
)) != USE
3361 && GET_CODE (PATTERN (insn
)) != CLOBBER
)
3367 insn
= NEXT_INSN (insn
);
3373 /* Determine if a given basic block heads a simple IF-THEN or IF-THEN-ELSE
3374 block. If so, we'll try to convert the insns to not require the branch.
3375 Return TRUE if we were successful at converting the block. */
3378 cond_exec_find_if_block (struct ce_if_block
* ce_info
)
3380 basic_block test_bb
= ce_info
->test_bb
;
3381 basic_block then_bb
= ce_info
->then_bb
;
3382 basic_block else_bb
= ce_info
->else_bb
;
3383 basic_block join_bb
= NULL_BLOCK
;
3388 ce_info
->last_test_bb
= test_bb
;
3390 /* We only ever should get here after reload,
3391 and if we have conditional execution. */
3392 gcc_assert (reload_completed
&& targetm
.have_conditional_execution ());
3394 /* Discover if any fall through predecessors of the current test basic block
3395 were && tests (which jump to the else block) or || tests (which jump to
3397 if (single_pred_p (test_bb
)
3398 && single_pred_edge (test_bb
)->flags
== EDGE_FALLTHRU
)
3400 basic_block bb
= single_pred (test_bb
);
3401 basic_block target_bb
;
3402 int max_insns
= MAX_CONDITIONAL_EXECUTE
;
3405 /* Determine if the preceding block is an && or || block. */
3406 if ((n_insns
= block_jumps_and_fallthru_p (bb
, else_bb
)) >= 0)
3408 ce_info
->and_and_p
= TRUE
;
3409 target_bb
= else_bb
;
3411 else if ((n_insns
= block_jumps_and_fallthru_p (bb
, then_bb
)) >= 0)
3413 ce_info
->and_and_p
= FALSE
;
3414 target_bb
= then_bb
;
3417 target_bb
= NULL_BLOCK
;
3419 if (target_bb
&& n_insns
<= max_insns
)
3421 int total_insns
= 0;
3424 ce_info
->last_test_bb
= test_bb
;
3426 /* Found at least one && or || block, look for more. */
3429 ce_info
->test_bb
= test_bb
= bb
;
3430 total_insns
+= n_insns
;
3433 if (!single_pred_p (bb
))
3436 bb
= single_pred (bb
);
3437 n_insns
= block_jumps_and_fallthru_p (bb
, target_bb
);
3439 while (n_insns
>= 0 && (total_insns
+ n_insns
) <= max_insns
);
3441 ce_info
->num_multiple_test_blocks
= blocks
;
3442 ce_info
->num_multiple_test_insns
= total_insns
;
3444 if (ce_info
->and_and_p
)
3445 ce_info
->num_and_and_blocks
= blocks
;
3447 ce_info
->num_or_or_blocks
= blocks
;
3451 /* The THEN block of an IF-THEN combo must have exactly one predecessor,
3452 other than any || blocks which jump to the THEN block. */
3453 if ((EDGE_COUNT (then_bb
->preds
) - ce_info
->num_or_or_blocks
) != 1)
3456 /* The edges of the THEN and ELSE blocks cannot have complex edges. */
3457 FOR_EACH_EDGE (cur_edge
, ei
, then_bb
->preds
)
3459 if (cur_edge
->flags
& EDGE_COMPLEX
)
3463 FOR_EACH_EDGE (cur_edge
, ei
, else_bb
->preds
)
3465 if (cur_edge
->flags
& EDGE_COMPLEX
)
3469 /* The THEN block of an IF-THEN combo must have zero or one successors. */
3470 if (EDGE_COUNT (then_bb
->succs
) > 0
3471 && (!single_succ_p (then_bb
)
3472 || (single_succ_edge (then_bb
)->flags
& EDGE_COMPLEX
)
3473 || (epilogue_completed
3474 && tablejump_p (BB_END (then_bb
), NULL
, NULL
))))
3477 /* If the THEN block has no successors, conditional execution can still
3478 make a conditional call. Don't do this unless the ELSE block has
3479 only one incoming edge -- the CFG manipulation is too ugly otherwise.
3480 Check for the last insn of the THEN block being an indirect jump, which
3481 is listed as not having any successors, but confuses the rest of the CE
3482 code processing. ??? we should fix this in the future. */
3483 if (EDGE_COUNT (then_bb
->succs
) == 0)
3485 if (single_pred_p (else_bb
) && else_bb
!= EXIT_BLOCK_PTR
)
3487 rtx last_insn
= BB_END (then_bb
);
3490 && NOTE_P (last_insn
)
3491 && last_insn
!= BB_HEAD (then_bb
))
3492 last_insn
= PREV_INSN (last_insn
);
3495 && JUMP_P (last_insn
)
3496 && ! simplejump_p (last_insn
))
3500 else_bb
= NULL_BLOCK
;
3506 /* If the THEN block's successor is the other edge out of the TEST block,
3507 then we have an IF-THEN combo without an ELSE. */
3508 else if (single_succ (then_bb
) == else_bb
)
3511 else_bb
= NULL_BLOCK
;
3514 /* If the THEN and ELSE block meet in a subsequent block, and the ELSE
3515 has exactly one predecessor and one successor, and the outgoing edge
3516 is not complex, then we have an IF-THEN-ELSE combo. */
3517 else if (single_succ_p (else_bb
)
3518 && single_succ (then_bb
) == single_succ (else_bb
)
3519 && single_pred_p (else_bb
)
3520 && !(single_succ_edge (else_bb
)->flags
& EDGE_COMPLEX
)
3521 && !(epilogue_completed
3522 && tablejump_p (BB_END (else_bb
), NULL
, NULL
)))
3523 join_bb
= single_succ (else_bb
);
3525 /* Otherwise it is not an IF-THEN or IF-THEN-ELSE combination. */
3529 num_possible_if_blocks
++;
3534 "\nIF-THEN%s block found, pass %d, start block %d "
3535 "[insn %d], then %d [%d]",
3536 (else_bb
) ? "-ELSE" : "",
3539 BB_HEAD (test_bb
) ? (int)INSN_UID (BB_HEAD (test_bb
)) : -1,
3541 BB_HEAD (then_bb
) ? (int)INSN_UID (BB_HEAD (then_bb
)) : -1);
3544 fprintf (dump_file
, ", else %d [%d]",
3546 BB_HEAD (else_bb
) ? (int)INSN_UID (BB_HEAD (else_bb
)) : -1);
3548 fprintf (dump_file
, ", join %d [%d]",
3550 BB_HEAD (join_bb
) ? (int)INSN_UID (BB_HEAD (join_bb
)) : -1);
3552 if (ce_info
->num_multiple_test_blocks
> 0)
3553 fprintf (dump_file
, ", %d %s block%s last test %d [%d]",
3554 ce_info
->num_multiple_test_blocks
,
3555 (ce_info
->and_and_p
) ? "&&" : "||",
3556 (ce_info
->num_multiple_test_blocks
== 1) ? "" : "s",
3557 ce_info
->last_test_bb
->index
,
3558 ((BB_HEAD (ce_info
->last_test_bb
))
3559 ? (int)INSN_UID (BB_HEAD (ce_info
->last_test_bb
))
3562 fputc ('\n', dump_file
);
3565 /* Make sure IF, THEN, and ELSE, blocks are adjacent. Actually, we get the
3566 first condition for free, since we've already asserted that there's a
3567 fallthru edge from IF to THEN. Likewise for the && and || blocks, since
3568 we checked the FALLTHRU flag, those are already adjacent to the last IF
3570 /* ??? As an enhancement, move the ELSE block. Have to deal with
3571 BLOCK notes, if by no other means than backing out the merge if they
3572 exist. Sticky enough I don't want to think about it now. */
3574 if (else_bb
&& (next
= next
->next_bb
) != else_bb
)
3576 if ((next
= next
->next_bb
) != join_bb
&& join_bb
!= EXIT_BLOCK_PTR
)
3584 /* Do the real work. */
3586 ce_info
->else_bb
= else_bb
;
3587 ce_info
->join_bb
= join_bb
;
3589 /* If we have && and || tests, try to first handle combining the && and ||
3590 tests into the conditional code, and if that fails, go back and handle
3591 it without the && and ||, which at present handles the && case if there
3592 was no ELSE block. */
3593 if (cond_exec_process_if_block (ce_info
, TRUE
))
3596 if (ce_info
->num_multiple_test_blocks
)
3600 if (cond_exec_process_if_block (ce_info
, FALSE
))
3607 /* Convert a branch over a trap, or a branch
3608 to a trap, into a conditional trap. */
3611 find_cond_trap (basic_block test_bb
, edge then_edge
, edge else_edge
)
3613 basic_block then_bb
= then_edge
->dest
;
3614 basic_block else_bb
= else_edge
->dest
;
3615 basic_block other_bb
, trap_bb
;
3616 rtx trap
, jump
, cond
, cond_earliest
, seq
;
3619 /* Locate the block with the trap instruction. */
3620 /* ??? While we look for no successors, we really ought to allow
3621 EH successors. Need to fix merge_if_block for that to work. */
3622 if ((trap
= block_has_only_trap (then_bb
)) != NULL
)
3623 trap_bb
= then_bb
, other_bb
= else_bb
;
3624 else if ((trap
= block_has_only_trap (else_bb
)) != NULL
)
3625 trap_bb
= else_bb
, other_bb
= then_bb
;
3631 fprintf (dump_file
, "\nTRAP-IF block found, start %d, trap %d\n",
3632 test_bb
->index
, trap_bb
->index
);
3635 /* If this is not a standard conditional jump, we can't parse it. */
3636 jump
= BB_END (test_bb
);
3637 cond
= noce_get_condition (jump
, &cond_earliest
, false);
3641 /* If the conditional jump is more than just a conditional jump, then
3642 we can not do if-conversion on this block. */
3643 if (! onlyjump_p (jump
))
3646 /* We must be comparing objects whose modes imply the size. */
3647 if (GET_MODE (XEXP (cond
, 0)) == BLKmode
)
3650 /* Reverse the comparison code, if necessary. */
3651 code
= GET_CODE (cond
);
3652 if (then_bb
== trap_bb
)
3654 code
= reversed_comparison_code (cond
, jump
);
3655 if (code
== UNKNOWN
)
3659 /* Attempt to generate the conditional trap. */
3660 seq
= gen_cond_trap (code
, copy_rtx (XEXP (cond
, 0)),
3661 copy_rtx (XEXP (cond
, 1)),
3662 TRAP_CODE (PATTERN (trap
)));
3666 /* Emit the new insns before cond_earliest. */
3667 emit_insn_before_setloc (seq
, cond_earliest
, INSN_LOCATION (trap
));
3669 /* Delete the trap block if possible. */
3670 remove_edge (trap_bb
== then_bb
? then_edge
: else_edge
);
3671 df_set_bb_dirty (test_bb
);
3672 df_set_bb_dirty (then_bb
);
3673 df_set_bb_dirty (else_bb
);
3675 if (EDGE_COUNT (trap_bb
->preds
) == 0)
3677 delete_basic_block (trap_bb
);
3681 /* Wire together the blocks again. */
3682 if (current_ir_type () == IR_RTL_CFGLAYOUT
)
3683 single_succ_edge (test_bb
)->flags
|= EDGE_FALLTHRU
;
3688 lab
= JUMP_LABEL (jump
);
3689 newjump
= emit_jump_insn_after (gen_jump (lab
), jump
);
3690 LABEL_NUSES (lab
) += 1;
3691 JUMP_LABEL (newjump
) = lab
;
3692 emit_barrier_after (newjump
);
3696 if (can_merge_blocks_p (test_bb
, other_bb
))
3698 merge_blocks (test_bb
, other_bb
);
3702 num_updated_if_blocks
++;
3706 /* Subroutine of find_cond_trap: if BB contains only a trap insn,
3710 block_has_only_trap (basic_block bb
)
3714 /* We're not the exit block. */
3715 if (bb
== EXIT_BLOCK_PTR
)
3718 /* The block must have no successors. */
3719 if (EDGE_COUNT (bb
->succs
) > 0)
3722 /* The only instruction in the THEN block must be the trap. */
3723 trap
= first_active_insn (bb
);
3724 if (! (trap
== BB_END (bb
)
3725 && GET_CODE (PATTERN (trap
)) == TRAP_IF
3726 && TRAP_CONDITION (PATTERN (trap
)) == const_true_rtx
))
3732 /* Look for IF-THEN-ELSE cases in which one of THEN or ELSE is
3733 transformable, but not necessarily the other. There need be no
3736 Return TRUE if we were successful at converting the block.
3738 Cases we'd like to look at:
3741 if (test) goto over; // x not live
3749 if (! test) goto label;
3752 if (test) goto E; // x not live
3766 (3) // This one's really only interesting for targets that can do
3767 // multiway branching, e.g. IA-64 BBB bundles. For other targets
3768 // it results in multiple branches on a cache line, which often
3769 // does not sit well with predictors.
3771 if (test1) goto E; // predicted not taken
3787 (A) Don't do (2) if the branch is predicted against the block we're
3788 eliminating. Do it anyway if we can eliminate a branch; this requires
3789 that the sole successor of the eliminated block postdominate the other
3792 (B) With CE, on (3) we can steal from both sides of the if, creating
3801 Again, this is most useful if J postdominates.
3803 (C) CE substitutes for helpful life information.
3805 (D) These heuristics need a lot of work. */
3807 /* Tests for case 1 above. */
3810 find_if_case_1 (basic_block test_bb
, edge then_edge
, edge else_edge
)
3812 basic_block then_bb
= then_edge
->dest
;
3813 basic_block else_bb
= else_edge
->dest
;
3815 int then_bb_index
, then_prob
;
3816 rtx else_target
= NULL_RTX
;
3818 /* If we are partitioning hot/cold basic blocks, we don't want to
3819 mess up unconditional or indirect jumps that cross between hot
3822 Basic block partitioning may result in some jumps that appear to
3823 be optimizable (or blocks that appear to be mergeable), but which really
3824 must be left untouched (they are required to make it safely across
3825 partition boundaries). See the comments at the top of
3826 bb-reorder.c:partition_hot_cold_basic_blocks for complete details. */
3828 if ((BB_END (then_bb
)
3829 && find_reg_note (BB_END (then_bb
), REG_CROSSING_JUMP
, NULL_RTX
))
3830 || (BB_END (test_bb
)
3831 && find_reg_note (BB_END (test_bb
), REG_CROSSING_JUMP
, NULL_RTX
))
3832 || (BB_END (else_bb
)
3833 && find_reg_note (BB_END (else_bb
), REG_CROSSING_JUMP
,
3837 /* THEN has one successor. */
3838 if (!single_succ_p (then_bb
))
3841 /* THEN does not fall through, but is not strange either. */
3842 if (single_succ_edge (then_bb
)->flags
& (EDGE_COMPLEX
| EDGE_FALLTHRU
))
3845 /* THEN has one predecessor. */
3846 if (!single_pred_p (then_bb
))
3849 /* THEN must do something. */
3850 if (forwarder_block_p (then_bb
))
3853 num_possible_if_blocks
++;
3856 "\nIF-CASE-1 found, start %d, then %d\n",
3857 test_bb
->index
, then_bb
->index
);
3859 if (then_edge
->probability
)
3860 then_prob
= REG_BR_PROB_BASE
- then_edge
->probability
;
3862 then_prob
= REG_BR_PROB_BASE
/ 2;
3864 /* We're speculating from the THEN path, we want to make sure the cost
3865 of speculation is within reason. */
3866 if (! cheap_bb_rtx_cost_p (then_bb
, then_prob
,
3867 COSTS_N_INSNS (BRANCH_COST (optimize_bb_for_speed_p (then_edge
->src
),
3868 predictable_edge_p (then_edge
)))))
3871 if (else_bb
== EXIT_BLOCK_PTR
)
3873 rtx jump
= BB_END (else_edge
->src
);
3874 gcc_assert (JUMP_P (jump
));
3875 else_target
= JUMP_LABEL (jump
);
3878 /* Registers set are dead, or are predicable. */
3879 if (! dead_or_predicable (test_bb
, then_bb
, else_bb
,
3880 single_succ_edge (then_bb
), 1))
3883 /* Conversion went ok, including moving the insns and fixing up the
3884 jump. Adjust the CFG to match. */
3886 /* We can avoid creating a new basic block if then_bb is immediately
3887 followed by else_bb, i.e. deleting then_bb allows test_bb to fall
3888 through to else_bb. */
3890 if (then_bb
->next_bb
== else_bb
3891 && then_bb
->prev_bb
== test_bb
3892 && else_bb
!= EXIT_BLOCK_PTR
)
3894 redirect_edge_succ (FALLTHRU_EDGE (test_bb
), else_bb
);
3897 else if (else_bb
== EXIT_BLOCK_PTR
)
3898 new_bb
= force_nonfallthru_and_redirect (FALLTHRU_EDGE (test_bb
),
3899 else_bb
, else_target
);
3901 new_bb
= redirect_edge_and_branch_force (FALLTHRU_EDGE (test_bb
),
3904 df_set_bb_dirty (test_bb
);
3905 df_set_bb_dirty (else_bb
);
3907 then_bb_index
= then_bb
->index
;
3908 delete_basic_block (then_bb
);
3910 /* Make rest of code believe that the newly created block is the THEN_BB
3911 block we removed. */
3914 df_bb_replace (then_bb_index
, new_bb
);
3915 /* Since the fallthru edge was redirected from test_bb to new_bb,
3916 we need to ensure that new_bb is in the same partition as
3917 test bb (you can not fall through across section boundaries). */
3918 BB_COPY_PARTITION (new_bb
, test_bb
);
3922 num_updated_if_blocks
++;
3927 /* Test for case 2 above. */
3930 find_if_case_2 (basic_block test_bb
, edge then_edge
, edge else_edge
)
3932 basic_block then_bb
= then_edge
->dest
;
3933 basic_block else_bb
= else_edge
->dest
;
3935 int then_prob
, else_prob
;
3937 /* We do not want to speculate (empty) loop latches. */
3939 && else_bb
->loop_father
->latch
== else_bb
)
3942 /* If we are partitioning hot/cold basic blocks, we don't want to
3943 mess up unconditional or indirect jumps that cross between hot
3946 Basic block partitioning may result in some jumps that appear to
3947 be optimizable (or blocks that appear to be mergeable), but which really
3948 must be left untouched (they are required to make it safely across
3949 partition boundaries). See the comments at the top of
3950 bb-reorder.c:partition_hot_cold_basic_blocks for complete details. */
3952 if ((BB_END (then_bb
)
3953 && find_reg_note (BB_END (then_bb
), REG_CROSSING_JUMP
, NULL_RTX
))
3954 || (BB_END (test_bb
)
3955 && find_reg_note (BB_END (test_bb
), REG_CROSSING_JUMP
, NULL_RTX
))
3956 || (BB_END (else_bb
)
3957 && find_reg_note (BB_END (else_bb
), REG_CROSSING_JUMP
,
3961 /* ELSE has one successor. */
3962 if (!single_succ_p (else_bb
))
3965 else_succ
= single_succ_edge (else_bb
);
3967 /* ELSE outgoing edge is not complex. */
3968 if (else_succ
->flags
& EDGE_COMPLEX
)
3971 /* ELSE has one predecessor. */
3972 if (!single_pred_p (else_bb
))
3975 /* THEN is not EXIT. */
3976 if (then_bb
->index
< NUM_FIXED_BLOCKS
)
3979 if (else_edge
->probability
)
3981 else_prob
= else_edge
->probability
;
3982 then_prob
= REG_BR_PROB_BASE
- else_prob
;
3986 else_prob
= REG_BR_PROB_BASE
/ 2;
3987 then_prob
= REG_BR_PROB_BASE
/ 2;
3990 /* ELSE is predicted or SUCC(ELSE) postdominates THEN. */
3991 if (else_prob
> then_prob
)
3993 else if (else_succ
->dest
->index
< NUM_FIXED_BLOCKS
3994 || dominated_by_p (CDI_POST_DOMINATORS
, then_bb
,
4000 num_possible_if_blocks
++;
4003 "\nIF-CASE-2 found, start %d, else %d\n",
4004 test_bb
->index
, else_bb
->index
);
4006 /* We're speculating from the ELSE path, we want to make sure the cost
4007 of speculation is within reason. */
4008 if (! cheap_bb_rtx_cost_p (else_bb
, else_prob
,
4009 COSTS_N_INSNS (BRANCH_COST (optimize_bb_for_speed_p (else_edge
->src
),
4010 predictable_edge_p (else_edge
)))))
4013 /* Registers set are dead, or are predicable. */
4014 if (! dead_or_predicable (test_bb
, else_bb
, then_bb
, else_succ
, 0))
4017 /* Conversion went ok, including moving the insns and fixing up the
4018 jump. Adjust the CFG to match. */
4020 df_set_bb_dirty (test_bb
);
4021 df_set_bb_dirty (then_bb
);
4022 delete_basic_block (else_bb
);
4025 num_updated_if_blocks
++;
4027 /* ??? We may now fallthru from one of THEN's successors into a join
4028 block. Rerun cleanup_cfg? Examine things manually? Wait? */
4033 /* Used by the code above to perform the actual rtl transformations.
4034 Return TRUE if successful.
4036 TEST_BB is the block containing the conditional branch. MERGE_BB
4037 is the block containing the code to manipulate. DEST_EDGE is an
4038 edge representing a jump to the join block; after the conversion,
4039 TEST_BB should be branching to its destination.
4040 REVERSEP is true if the sense of the branch should be reversed. */
4043 dead_or_predicable (basic_block test_bb
, basic_block merge_bb
,
4044 basic_block other_bb
, edge dest_edge
, int reversep
)
4046 basic_block new_dest
= dest_edge
->dest
;
4047 rtx head
, end
, jump
, earliest
= NULL_RTX
, old_dest
;
4048 bitmap merge_set
= NULL
;
4049 /* Number of pending changes. */
4050 int n_validated_changes
= 0;
4051 rtx new_dest_label
= NULL_RTX
;
4053 jump
= BB_END (test_bb
);
4055 /* Find the extent of the real code in the merge block. */
4056 head
= BB_HEAD (merge_bb
);
4057 end
= BB_END (merge_bb
);
4059 while (DEBUG_INSN_P (end
) && end
!= head
)
4060 end
= PREV_INSN (end
);
4062 /* If merge_bb ends with a tablejump, predicating/moving insn's
4063 into test_bb and then deleting merge_bb will result in the jumptable
4064 that follows merge_bb being removed along with merge_bb and then we
4065 get an unresolved reference to the jumptable. */
4066 if (tablejump_p (end
, NULL
, NULL
))
4070 head
= NEXT_INSN (head
);
4071 while (DEBUG_INSN_P (head
) && head
!= end
)
4072 head
= NEXT_INSN (head
);
4077 head
= end
= NULL_RTX
;
4080 head
= NEXT_INSN (head
);
4081 while (DEBUG_INSN_P (head
) && head
!= end
)
4082 head
= NEXT_INSN (head
);
4089 head
= end
= NULL_RTX
;
4092 end
= PREV_INSN (end
);
4093 while (DEBUG_INSN_P (end
) && end
!= head
)
4094 end
= PREV_INSN (end
);
4097 /* Disable handling dead code by conditional execution if the machine needs
4098 to do anything funny with the tests, etc. */
4099 #ifndef IFCVT_MODIFY_TESTS
4100 if (targetm
.have_conditional_execution ())
4102 /* In the conditional execution case, we have things easy. We know
4103 the condition is reversible. We don't have to check life info
4104 because we're going to conditionally execute the code anyway.
4105 All that's left is making sure the insns involved can actually
4110 cond
= cond_exec_get_condition (jump
);
4114 prob_val
= find_reg_note (jump
, REG_BR_PROB
, NULL_RTX
);
4116 prob_val
= XEXP (prob_val
, 0);
4120 enum rtx_code rev
= reversed_comparison_code (cond
, jump
);
4123 cond
= gen_rtx_fmt_ee (rev
, GET_MODE (cond
), XEXP (cond
, 0),
4126 prob_val
= GEN_INT (REG_BR_PROB_BASE
- INTVAL (prob_val
));
4129 if (cond_exec_process_insns (NULL
, head
, end
, cond
, prob_val
, 0)
4130 && verify_changes (0))
4131 n_validated_changes
= num_validated_changes ();
4139 /* If we allocated new pseudos (e.g. in the conditional move
4140 expander called from noce_emit_cmove), we must resize the
4142 if (max_regno
< max_reg_num ())
4143 max_regno
= max_reg_num ();
4145 /* Try the NCE path if the CE path did not result in any changes. */
4146 if (n_validated_changes
== 0)
4152 /* In the non-conditional execution case, we have to verify that there
4153 are no trapping operations, no calls, no references to memory, and
4154 that any registers modified are dead at the branch site. */
4156 if (!any_condjump_p (jump
))
4159 /* Find the extent of the conditional. */
4160 cond
= noce_get_condition (jump
, &earliest
, false);
4164 live
= BITMAP_ALLOC (®_obstack
);
4165 simulate_backwards_to_point (merge_bb
, live
, end
);
4166 success
= can_move_insns_across (head
, end
, earliest
, jump
,
4168 df_get_live_in (other_bb
), NULL
);
4173 /* Collect the set of registers set in MERGE_BB. */
4174 merge_set
= BITMAP_ALLOC (®_obstack
);
4176 FOR_BB_INSNS (merge_bb
, insn
)
4177 if (NONDEBUG_INSN_P (insn
))
4178 df_simulate_find_defs (insn
, merge_set
);
4180 #ifdef HAVE_simple_return
4181 /* If shrink-wrapping, disable this optimization when test_bb is
4182 the first basic block and merge_bb exits. The idea is to not
4183 move code setting up a return register as that may clobber a
4184 register used to pass function parameters, which then must be
4185 saved in caller-saved regs. A caller-saved reg requires the
4186 prologue, killing a shrink-wrap opportunity. */
4187 if ((flag_shrink_wrap
&& HAVE_simple_return
&& !epilogue_completed
)
4188 && ENTRY_BLOCK_PTR
->next_bb
== test_bb
4189 && single_succ_p (new_dest
)
4190 && single_succ (new_dest
) == EXIT_BLOCK_PTR
4191 && bitmap_intersect_p (df_get_live_in (new_dest
), merge_set
))
4196 return_regs
= BITMAP_ALLOC (®_obstack
);
4198 /* Start off with the intersection of regs used to pass
4199 params and regs used to return values. */
4200 for (i
= 0; i
< FIRST_PSEUDO_REGISTER
; i
++)
4201 if (FUNCTION_ARG_REGNO_P (i
)
4202 && targetm
.calls
.function_value_regno_p (i
))
4203 bitmap_set_bit (return_regs
, INCOMING_REGNO (i
));
4205 bitmap_and_into (return_regs
, df_get_live_out (ENTRY_BLOCK_PTR
));
4206 bitmap_and_into (return_regs
, df_get_live_in (EXIT_BLOCK_PTR
));
4207 if (!bitmap_empty_p (return_regs
))
4209 FOR_BB_INSNS_REVERSE (new_dest
, insn
)
4210 if (NONDEBUG_INSN_P (insn
))
4213 unsigned int uid
= INSN_UID (insn
);
4215 /* If this insn sets any reg in return_regs.. */
4216 for (def_rec
= DF_INSN_UID_DEFS (uid
); *def_rec
; def_rec
++)
4218 df_ref def
= *def_rec
;
4219 unsigned r
= DF_REF_REGNO (def
);
4221 if (bitmap_bit_p (return_regs
, r
))
4224 /* ..then add all reg uses to the set of regs
4225 we're interested in. */
4227 df_simulate_uses (insn
, return_regs
);
4229 if (bitmap_intersect_p (merge_set
, return_regs
))
4231 BITMAP_FREE (return_regs
);
4232 BITMAP_FREE (merge_set
);
4236 BITMAP_FREE (return_regs
);
4242 /* We don't want to use normal invert_jump or redirect_jump because
4243 we don't want to delete_insn called. Also, we want to do our own
4244 change group management. */
4246 old_dest
= JUMP_LABEL (jump
);
4247 if (other_bb
!= new_dest
)
4249 if (JUMP_P (BB_END (dest_edge
->src
)))
4250 new_dest_label
= JUMP_LABEL (BB_END (dest_edge
->src
));
4251 else if (new_dest
== EXIT_BLOCK_PTR
)
4252 new_dest_label
= ret_rtx
;
4254 new_dest_label
= block_label (new_dest
);
4257 ? ! invert_jump_1 (jump
, new_dest_label
)
4258 : ! redirect_jump_1 (jump
, new_dest_label
))
4262 if (verify_changes (n_validated_changes
))
4263 confirm_change_group ();
4267 if (other_bb
!= new_dest
)
4269 redirect_jump_2 (jump
, old_dest
, new_dest_label
, 0, reversep
);
4271 redirect_edge_succ (BRANCH_EDGE (test_bb
), new_dest
);
4274 gcov_type count
, probability
;
4275 count
= BRANCH_EDGE (test_bb
)->count
;
4276 BRANCH_EDGE (test_bb
)->count
= FALLTHRU_EDGE (test_bb
)->count
;
4277 FALLTHRU_EDGE (test_bb
)->count
= count
;
4278 probability
= BRANCH_EDGE (test_bb
)->probability
;
4279 BRANCH_EDGE (test_bb
)->probability
4280 = FALLTHRU_EDGE (test_bb
)->probability
;
4281 FALLTHRU_EDGE (test_bb
)->probability
= probability
;
4282 update_br_prob_note (test_bb
);
4286 /* Move the insns out of MERGE_BB to before the branch. */
4291 if (end
== BB_END (merge_bb
))
4292 BB_END (merge_bb
) = PREV_INSN (head
);
4294 /* PR 21767: when moving insns above a conditional branch, the REG_EQUAL
4295 notes being moved might become invalid. */
4301 if (! INSN_P (insn
))
4303 note
= find_reg_note (insn
, REG_EQUAL
, NULL_RTX
);
4306 set
= single_set (insn
);
4307 if (!set
|| !function_invariant_p (SET_SRC (set
))
4308 || !function_invariant_p (XEXP (note
, 0)))
4309 remove_note (insn
, note
);
4310 } while (insn
!= end
&& (insn
= NEXT_INSN (insn
)));
4312 /* PR46315: when moving insns above a conditional branch, the REG_EQUAL
4313 notes referring to the registers being set might become invalid. */
4319 EXECUTE_IF_SET_IN_BITMAP (merge_set
, 0, i
, bi
)
4320 remove_reg_equal_equiv_notes_for_regno (i
);
4322 BITMAP_FREE (merge_set
);
4325 reorder_insns (head
, end
, PREV_INSN (earliest
));
4328 /* Remove the jump and edge if we can. */
4329 if (other_bb
== new_dest
)
4332 remove_edge (BRANCH_EDGE (test_bb
));
4333 /* ??? Can't merge blocks here, as then_bb is still in use.
4334 At minimum, the merge will get done just before bb-reorder. */
4343 BITMAP_FREE (merge_set
);
4348 /* Main entry point for all if-conversion. */
4358 df_live_add_problem ();
4359 df_live_set_all_dirty ();
4362 num_possible_if_blocks
= 0;
4363 num_updated_if_blocks
= 0;
4364 num_true_changes
= 0;
4366 loop_optimizer_init (AVOID_CFG_MODIFICATIONS
);
4367 mark_loop_exit_edges ();
4368 loop_optimizer_finalize ();
4369 free_dominance_info (CDI_DOMINATORS
);
4371 /* Compute postdominators. */
4372 calculate_dominance_info (CDI_POST_DOMINATORS
);
4374 df_set_flags (DF_LR_RUN_DCE
);
4376 /* Go through each of the basic blocks looking for things to convert. If we
4377 have conditional execution, we make multiple passes to allow us to handle
4378 IF-THEN{-ELSE} blocks within other IF-THEN{-ELSE} blocks. */
4383 /* Only need to do dce on the first pass. */
4384 df_clear_flags (DF_LR_RUN_DCE
);
4385 cond_exec_changed_p
= FALSE
;
4388 #ifdef IFCVT_MULTIPLE_DUMPS
4389 if (dump_file
&& pass
> 1)
4390 fprintf (dump_file
, "\n\n========== Pass %d ==========\n", pass
);
4396 while (!df_get_bb_dirty (bb
)
4397 && (new_bb
= find_if_header (bb
, pass
)) != NULL
)
4401 #ifdef IFCVT_MULTIPLE_DUMPS
4402 if (dump_file
&& cond_exec_changed_p
)
4403 print_rtl_with_bb (dump_file
, get_insns (), dump_flags
);
4406 while (cond_exec_changed_p
);
4408 #ifdef IFCVT_MULTIPLE_DUMPS
4410 fprintf (dump_file
, "\n\n========== no more changes\n");
4413 free_dominance_info (CDI_POST_DOMINATORS
);
4418 clear_aux_for_blocks ();
4420 /* If we allocated new pseudos, we must resize the array for sched1. */
4421 if (max_regno
< max_reg_num ())
4422 max_regno
= max_reg_num ();
4424 /* Write the final stats. */
4425 if (dump_file
&& num_possible_if_blocks
> 0)
4428 "\n%d possible IF blocks searched.\n",
4429 num_possible_if_blocks
);
4431 "%d IF blocks converted.\n",
4432 num_updated_if_blocks
);
4434 "%d true changes made.\n\n\n",
4439 df_remove_problem (df_live
);
4441 #ifdef ENABLE_CHECKING
4442 verify_flow_info ();
4447 gate_handle_if_conversion (void)
4449 return (optimize
> 0)
4450 && dbg_cnt (if_conversion
);
4453 /* If-conversion and CFG cleanup. */
4455 rest_of_handle_if_conversion (void)
4457 if (flag_if_conversion
)
4461 dump_reg_info (dump_file
);
4462 dump_flow_info (dump_file
, dump_flags
);
4464 cleanup_cfg (CLEANUP_EXPENSIVE
);
4472 struct rtl_opt_pass pass_rtl_ifcvt
=
4477 OPTGROUP_NONE
, /* optinfo_flags */
4478 gate_handle_if_conversion
, /* gate */
4479 rest_of_handle_if_conversion
, /* execute */
4482 0, /* static_pass_number */
4483 TV_IFCVT
, /* tv_id */
4484 0, /* properties_required */
4485 0, /* properties_provided */
4486 0, /* properties_destroyed */
4487 0, /* todo_flags_start */
4488 TODO_df_finish
| TODO_verify_rtl_sharing
|
4489 0 /* todo_flags_finish */
4494 gate_handle_if_after_combine (void)
4496 return optimize
> 0 && flag_if_conversion
4497 && dbg_cnt (if_after_combine
);
4501 /* Rerun if-conversion, as combine may have simplified things enough
4502 to now meet sequence length restrictions. */
4504 rest_of_handle_if_after_combine (void)
4510 struct rtl_opt_pass pass_if_after_combine
=
4515 OPTGROUP_NONE
, /* optinfo_flags */
4516 gate_handle_if_after_combine
, /* gate */
4517 rest_of_handle_if_after_combine
, /* execute */
4520 0, /* static_pass_number */
4521 TV_IFCVT
, /* tv_id */
4522 0, /* properties_required */
4523 0, /* properties_provided */
4524 0, /* properties_destroyed */
4525 0, /* todo_flags_start */
4526 TODO_df_finish
| TODO_verify_rtl_sharing
|
4527 TODO_ggc_collect
/* todo_flags_finish */
4533 gate_handle_if_after_reload (void)
4535 return optimize
> 0 && flag_if_conversion2
4536 && dbg_cnt (if_after_reload
);
4540 rest_of_handle_if_after_reload (void)
4547 struct rtl_opt_pass pass_if_after_reload
=
4552 OPTGROUP_NONE
, /* optinfo_flags */
4553 gate_handle_if_after_reload
, /* gate */
4554 rest_of_handle_if_after_reload
, /* execute */
4557 0, /* static_pass_number */
4558 TV_IFCVT2
, /* tv_id */
4559 0, /* properties_required */
4560 0, /* properties_provided */
4561 0, /* properties_destroyed */
4562 0, /* todo_flags_start */
4563 TODO_df_finish
| TODO_verify_rtl_sharing
|
4564 TODO_ggc_collect
/* todo_flags_finish */