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 /* True if after combine pass. */
71 static bool ifcvt_after_combine
;
73 /* # of IF-THEN or IF-THEN-ELSE blocks we looked at */
74 static int num_possible_if_blocks
;
76 /* # of IF-THEN or IF-THEN-ELSE blocks were converted to conditional
78 static int num_updated_if_blocks
;
80 /* # of changes made. */
81 static int num_true_changes
;
83 /* Whether conditional execution changes were made. */
84 static int cond_exec_changed_p
;
86 /* Forward references. */
87 static int count_bb_insns (const_basic_block
);
88 static bool cheap_bb_rtx_cost_p (const_basic_block
, int, int);
89 static rtx
first_active_insn (basic_block
);
90 static rtx
last_active_insn (basic_block
, int);
91 static rtx
find_active_insn_before (basic_block
, rtx
);
92 static rtx
find_active_insn_after (basic_block
, rtx
);
93 static basic_block
block_fallthru (basic_block
);
94 static int cond_exec_process_insns (ce_if_block_t
*, rtx
, rtx
, rtx
, rtx
, int);
95 static rtx
cond_exec_get_condition (rtx
);
96 static rtx
noce_get_condition (rtx
, rtx
*, bool);
97 static int noce_operand_ok (const_rtx
);
98 static void merge_if_block (ce_if_block_t
*);
99 static int find_cond_trap (basic_block
, edge
, edge
);
100 static basic_block
find_if_header (basic_block
, int);
101 static int block_jumps_and_fallthru_p (basic_block
, basic_block
);
102 static int noce_find_if_block (basic_block
, edge
, edge
, int);
103 static int cond_exec_find_if_block (ce_if_block_t
*);
104 static int find_if_case_1 (basic_block
, edge
, edge
);
105 static int find_if_case_2 (basic_block
, edge
, edge
);
106 static int dead_or_predicable (basic_block
, basic_block
, basic_block
,
108 static void noce_emit_move_insn (rtx
, rtx
);
109 static rtx
block_has_only_trap (basic_block
);
111 /* Count the number of non-jump active insns in BB. */
114 count_bb_insns (const_basic_block bb
)
117 rtx insn
= BB_HEAD (bb
);
121 if (CALL_P (insn
) || NONJUMP_INSN_P (insn
))
124 if (insn
== BB_END (bb
))
126 insn
= NEXT_INSN (insn
);
132 /* Determine whether the total insn_rtx_cost on non-jump insns in
133 basic block BB is less than MAX_COST. This function returns
134 false if the cost of any instruction could not be estimated.
136 The cost of the non-jump insns in BB is scaled by REG_BR_PROB_BASE
137 as those insns are being speculated. MAX_COST is scaled with SCALE
138 plus a small fudge factor. */
141 cheap_bb_rtx_cost_p (const_basic_block bb
, int scale
, int max_cost
)
144 rtx insn
= BB_HEAD (bb
);
145 bool speed
= optimize_bb_for_speed_p (bb
);
147 /* Set scale to REG_BR_PROB_BASE to void the identical scaling
148 applied to insn_rtx_cost when optimizing for size. Only do
149 this after combine because if-conversion might interfere with
150 passes before combine.
152 Use optimize_function_for_speed_p instead of the pre-defined
153 variable speed to make sure it is set to same value for all
154 basic blocks in one if-conversion transformation. */
155 if (!optimize_function_for_speed_p (cfun
) && ifcvt_after_combine
)
156 scale
= REG_BR_PROB_BASE
;
157 /* Our branch probability/scaling factors are just estimates and don't
158 account for cases where we can get speculation for free and other
159 secondary benefits. So we fudge the scale factor to make speculating
160 appear a little more profitable when optimizing for performance. */
162 scale
+= REG_BR_PROB_BASE
/ 8;
169 if (NONJUMP_INSN_P (insn
))
171 int cost
= insn_rtx_cost (PATTERN (insn
), speed
) * REG_BR_PROB_BASE
;
175 /* If this instruction is the load or set of a "stack" register,
176 such as a floating point register on x87, then the cost of
177 speculatively executing this insn may need to include
178 the additional cost of popping its result off of the
179 register stack. Unfortunately, correctly recognizing and
180 accounting for this additional overhead is tricky, so for
181 now we simply prohibit such speculative execution. */
184 rtx set
= single_set (insn
);
185 if (set
&& STACK_REG_P (SET_DEST (set
)))
191 if (count
>= max_cost
)
194 else if (CALL_P (insn
))
197 if (insn
== BB_END (bb
))
199 insn
= NEXT_INSN (insn
);
205 /* Return the first non-jump active insn in the basic block. */
208 first_active_insn (basic_block bb
)
210 rtx insn
= BB_HEAD (bb
);
214 if (insn
== BB_END (bb
))
216 insn
= NEXT_INSN (insn
);
219 while (NOTE_P (insn
) || DEBUG_INSN_P (insn
))
221 if (insn
== BB_END (bb
))
223 insn
= NEXT_INSN (insn
);
232 /* Return the last non-jump active (non-jump) insn in the basic block. */
235 last_active_insn (basic_block bb
, int skip_use_p
)
237 rtx insn
= BB_END (bb
);
238 rtx head
= BB_HEAD (bb
);
242 || DEBUG_INSN_P (insn
)
244 && NONJUMP_INSN_P (insn
)
245 && GET_CODE (PATTERN (insn
)) == USE
))
249 insn
= PREV_INSN (insn
);
258 /* Return the active insn before INSN inside basic block CURR_BB. */
261 find_active_insn_before (basic_block curr_bb
, rtx insn
)
263 if (!insn
|| insn
== BB_HEAD (curr_bb
))
266 while ((insn
= PREV_INSN (insn
)) != NULL_RTX
)
268 if (NONJUMP_INSN_P (insn
) || JUMP_P (insn
) || CALL_P (insn
))
271 /* No other active insn all the way to the start of the basic block. */
272 if (insn
== BB_HEAD (curr_bb
))
279 /* Return the active insn after INSN inside basic block CURR_BB. */
282 find_active_insn_after (basic_block curr_bb
, rtx insn
)
284 if (!insn
|| insn
== BB_END (curr_bb
))
287 while ((insn
= NEXT_INSN (insn
)) != NULL_RTX
)
289 if (NONJUMP_INSN_P (insn
) || JUMP_P (insn
) || CALL_P (insn
))
292 /* No other active insn all the way to the end of the basic block. */
293 if (insn
== BB_END (curr_bb
))
300 /* Return the basic block reached by falling though the basic block BB. */
303 block_fallthru (basic_block bb
)
305 edge e
= find_fallthru_edge (bb
->succs
);
307 return (e
) ? e
->dest
: NULL_BLOCK
;
310 /* Go through a bunch of insns, converting them to conditional
311 execution format if possible. Return TRUE if all of the non-note
312 insns were processed. */
315 cond_exec_process_insns (ce_if_block_t
*ce_info ATTRIBUTE_UNUSED
,
316 /* if block information */rtx start
,
317 /* first insn to look at */rtx end
,
318 /* last insn to look at */rtx test
,
319 /* conditional execution test */rtx prob_val
,
320 /* probability of branch taken. */int mod_ok
)
322 int must_be_last
= FALSE
;
330 for (insn
= start
; ; insn
= NEXT_INSN (insn
))
332 /* dwarf2out can't cope with conditional prologues. */
333 if (NOTE_P (insn
) && NOTE_KIND (insn
) == NOTE_INSN_PROLOGUE_END
)
336 if (NOTE_P (insn
) || DEBUG_INSN_P (insn
))
339 gcc_assert(NONJUMP_INSN_P (insn
) || CALL_P (insn
));
341 /* Remove USE insns that get in the way. */
342 if (reload_completed
&& GET_CODE (PATTERN (insn
)) == USE
)
344 /* ??? Ug. Actually unlinking the thing is problematic,
345 given what we'd have to coordinate with our callers. */
346 SET_INSN_DELETED (insn
);
350 /* Last insn wasn't last? */
354 if (modified_in_p (test
, insn
))
361 /* Now build the conditional form of the instruction. */
362 pattern
= PATTERN (insn
);
363 xtest
= copy_rtx (test
);
365 /* If this is already a COND_EXEC, rewrite the test to be an AND of the
367 if (GET_CODE (pattern
) == COND_EXEC
)
369 if (GET_MODE (xtest
) != GET_MODE (COND_EXEC_TEST (pattern
)))
372 xtest
= gen_rtx_AND (GET_MODE (xtest
), xtest
,
373 COND_EXEC_TEST (pattern
));
374 pattern
= COND_EXEC_CODE (pattern
);
377 pattern
= gen_rtx_COND_EXEC (VOIDmode
, xtest
, pattern
);
379 /* If the machine needs to modify the insn being conditionally executed,
380 say for example to force a constant integer operand into a temp
381 register, do so here. */
382 #ifdef IFCVT_MODIFY_INSN
383 IFCVT_MODIFY_INSN (ce_info
, pattern
, insn
);
388 validate_change (insn
, &PATTERN (insn
), pattern
, 1);
390 if (CALL_P (insn
) && prob_val
)
391 validate_change (insn
, ®_NOTES (insn
),
392 alloc_EXPR_LIST (REG_BR_PROB
, prob_val
,
393 REG_NOTES (insn
)), 1);
403 /* Return the condition for a jump. Do not do any special processing. */
406 cond_exec_get_condition (rtx jump
)
410 if (any_condjump_p (jump
))
411 test_if
= SET_SRC (pc_set (jump
));
414 cond
= XEXP (test_if
, 0);
416 /* If this branches to JUMP_LABEL when the condition is false,
417 reverse the condition. */
418 if (GET_CODE (XEXP (test_if
, 2)) == LABEL_REF
419 && XEXP (XEXP (test_if
, 2), 0) == JUMP_LABEL (jump
))
421 enum rtx_code rev
= reversed_comparison_code (cond
, jump
);
425 cond
= gen_rtx_fmt_ee (rev
, GET_MODE (cond
), XEXP (cond
, 0),
432 /* Given a simple IF-THEN or IF-THEN-ELSE block, attempt to convert it
433 to conditional execution. Return TRUE if we were successful at
434 converting the block. */
437 cond_exec_process_if_block (ce_if_block_t
* ce_info
,
438 /* if block information */int do_multiple_p
)
440 basic_block test_bb
= ce_info
->test_bb
; /* last test block */
441 basic_block then_bb
= ce_info
->then_bb
; /* THEN */
442 basic_block else_bb
= ce_info
->else_bb
; /* ELSE or NULL */
443 rtx test_expr
; /* expression in IF_THEN_ELSE that is tested */
444 rtx then_start
; /* first insn in THEN block */
445 rtx then_end
; /* last insn + 1 in THEN block */
446 rtx else_start
= NULL_RTX
; /* first insn in ELSE block or NULL */
447 rtx else_end
= NULL_RTX
; /* last insn + 1 in ELSE block */
448 int max
; /* max # of insns to convert. */
449 int then_mod_ok
; /* whether conditional mods are ok in THEN */
450 rtx true_expr
; /* test for else block insns */
451 rtx false_expr
; /* test for then block insns */
452 rtx true_prob_val
; /* probability of else block */
453 rtx false_prob_val
; /* probability of then block */
454 rtx then_last_head
= NULL_RTX
; /* Last match at the head of THEN */
455 rtx else_last_head
= NULL_RTX
; /* Last match at the head of ELSE */
456 rtx then_first_tail
= NULL_RTX
; /* First match at the tail of THEN */
457 rtx else_first_tail
= NULL_RTX
; /* First match at the tail of ELSE */
458 int then_n_insns
, else_n_insns
, n_insns
;
459 enum rtx_code false_code
;
461 /* If test is comprised of && or || elements, and we've failed at handling
462 all of them together, just use the last test if it is the special case of
463 && elements without an ELSE block. */
464 if (!do_multiple_p
&& ce_info
->num_multiple_test_blocks
)
466 if (else_bb
|| ! ce_info
->and_and_p
)
469 ce_info
->test_bb
= test_bb
= ce_info
->last_test_bb
;
470 ce_info
->num_multiple_test_blocks
= 0;
471 ce_info
->num_and_and_blocks
= 0;
472 ce_info
->num_or_or_blocks
= 0;
475 /* Find the conditional jump to the ELSE or JOIN part, and isolate
477 test_expr
= cond_exec_get_condition (BB_END (test_bb
));
481 /* If the conditional jump is more than just a conditional jump,
482 then we can not do conditional execution conversion on this block. */
483 if (! onlyjump_p (BB_END (test_bb
)))
486 /* Collect the bounds of where we're to search, skipping any labels, jumps
487 and notes at the beginning and end of the block. Then count the total
488 number of insns and see if it is small enough to convert. */
489 then_start
= first_active_insn (then_bb
);
490 then_end
= last_active_insn (then_bb
, TRUE
);
491 then_n_insns
= ce_info
->num_then_insns
= count_bb_insns (then_bb
);
492 n_insns
= then_n_insns
;
493 max
= MAX_CONDITIONAL_EXECUTE
;
500 else_start
= first_active_insn (else_bb
);
501 else_end
= last_active_insn (else_bb
, TRUE
);
502 else_n_insns
= ce_info
->num_else_insns
= count_bb_insns (else_bb
);
503 n_insns
+= else_n_insns
;
505 /* Look for matching sequences at the head and tail of the two blocks,
506 and limit the range of insns to be converted if possible. */
507 n_matching
= flow_find_cross_jump (then_bb
, else_bb
,
508 &then_first_tail
, &else_first_tail
,
510 if (then_first_tail
== BB_HEAD (then_bb
))
511 then_start
= then_end
= NULL_RTX
;
512 if (else_first_tail
== BB_HEAD (else_bb
))
513 else_start
= else_end
= NULL_RTX
;
518 then_end
= find_active_insn_before (then_bb
, then_first_tail
);
520 else_end
= find_active_insn_before (else_bb
, else_first_tail
);
521 n_insns
-= 2 * n_matching
;
524 if (then_start
&& else_start
)
526 int longest_match
= MIN (then_n_insns
- n_matching
,
527 else_n_insns
- n_matching
);
529 = flow_find_head_matching_sequence (then_bb
, else_bb
,
538 /* We won't pass the insns in the head sequence to
539 cond_exec_process_insns, so we need to test them here
540 to make sure that they don't clobber the condition. */
541 for (insn
= BB_HEAD (then_bb
);
542 insn
!= NEXT_INSN (then_last_head
);
543 insn
= NEXT_INSN (insn
))
544 if (!LABEL_P (insn
) && !NOTE_P (insn
)
545 && !DEBUG_INSN_P (insn
)
546 && modified_in_p (test_expr
, insn
))
550 if (then_last_head
== then_end
)
551 then_start
= then_end
= NULL_RTX
;
552 if (else_last_head
== else_end
)
553 else_start
= else_end
= NULL_RTX
;
558 then_start
= find_active_insn_after (then_bb
, then_last_head
);
560 else_start
= find_active_insn_after (else_bb
, else_last_head
);
561 n_insns
-= 2 * n_matching
;
569 /* Map test_expr/test_jump into the appropriate MD tests to use on
570 the conditionally executed code. */
572 true_expr
= test_expr
;
574 false_code
= reversed_comparison_code (true_expr
, BB_END (test_bb
));
575 if (false_code
!= UNKNOWN
)
576 false_expr
= gen_rtx_fmt_ee (false_code
, GET_MODE (true_expr
),
577 XEXP (true_expr
, 0), XEXP (true_expr
, 1));
579 false_expr
= NULL_RTX
;
581 #ifdef IFCVT_MODIFY_TESTS
582 /* If the machine description needs to modify the tests, such as setting a
583 conditional execution register from a comparison, it can do so here. */
584 IFCVT_MODIFY_TESTS (ce_info
, true_expr
, false_expr
);
586 /* See if the conversion failed. */
587 if (!true_expr
|| !false_expr
)
591 true_prob_val
= find_reg_note (BB_END (test_bb
), REG_BR_PROB
, NULL_RTX
);
594 true_prob_val
= XEXP (true_prob_val
, 0);
595 false_prob_val
= GEN_INT (REG_BR_PROB_BASE
- INTVAL (true_prob_val
));
598 false_prob_val
= NULL_RTX
;
600 /* If we have && or || tests, do them here. These tests are in the adjacent
601 blocks after the first block containing the test. */
602 if (ce_info
->num_multiple_test_blocks
> 0)
604 basic_block bb
= test_bb
;
605 basic_block last_test_bb
= ce_info
->last_test_bb
;
614 enum rtx_code f_code
;
616 bb
= block_fallthru (bb
);
617 start
= first_active_insn (bb
);
618 end
= last_active_insn (bb
, TRUE
);
620 && ! cond_exec_process_insns (ce_info
, start
, end
, false_expr
,
621 false_prob_val
, FALSE
))
624 /* If the conditional jump is more than just a conditional jump, then
625 we can not do conditional execution conversion on this block. */
626 if (! onlyjump_p (BB_END (bb
)))
629 /* Find the conditional jump and isolate the test. */
630 t
= cond_exec_get_condition (BB_END (bb
));
634 f_code
= reversed_comparison_code (t
, BB_END (bb
));
635 if (f_code
== UNKNOWN
)
638 f
= gen_rtx_fmt_ee (f_code
, GET_MODE (t
), XEXP (t
, 0), XEXP (t
, 1));
639 if (ce_info
->and_and_p
)
641 t
= gen_rtx_AND (GET_MODE (t
), true_expr
, t
);
642 f
= gen_rtx_IOR (GET_MODE (t
), false_expr
, f
);
646 t
= gen_rtx_IOR (GET_MODE (t
), true_expr
, t
);
647 f
= gen_rtx_AND (GET_MODE (t
), false_expr
, f
);
650 /* If the machine description needs to modify the tests, such as
651 setting a conditional execution register from a comparison, it can
653 #ifdef IFCVT_MODIFY_MULTIPLE_TESTS
654 IFCVT_MODIFY_MULTIPLE_TESTS (ce_info
, bb
, t
, f
);
656 /* See if the conversion failed. */
664 while (bb
!= last_test_bb
);
667 /* For IF-THEN-ELSE blocks, we don't allow modifications of the test
668 on then THEN block. */
669 then_mod_ok
= (else_bb
== NULL_BLOCK
);
671 /* Go through the THEN and ELSE blocks converting the insns if possible
672 to conditional execution. */
676 || ! cond_exec_process_insns (ce_info
, then_start
, then_end
,
677 false_expr
, false_prob_val
,
681 if (else_bb
&& else_end
682 && ! cond_exec_process_insns (ce_info
, else_start
, else_end
,
683 true_expr
, true_prob_val
, TRUE
))
686 /* If we cannot apply the changes, fail. Do not go through the normal fail
687 processing, since apply_change_group will call cancel_changes. */
688 if (! apply_change_group ())
690 #ifdef IFCVT_MODIFY_CANCEL
691 /* Cancel any machine dependent changes. */
692 IFCVT_MODIFY_CANCEL (ce_info
);
697 #ifdef IFCVT_MODIFY_FINAL
698 /* Do any machine dependent final modifications. */
699 IFCVT_MODIFY_FINAL (ce_info
);
702 /* Conversion succeeded. */
704 fprintf (dump_file
, "%d insn%s converted to conditional execution.\n",
705 n_insns
, (n_insns
== 1) ? " was" : "s were");
707 /* Merge the blocks! If we had matching sequences, make sure to delete one
708 copy at the appropriate location first: delete the copy in the THEN branch
709 for a tail sequence so that the remaining one is executed last for both
710 branches, and delete the copy in the ELSE branch for a head sequence so
711 that the remaining one is executed first for both branches. */
714 rtx from
= then_first_tail
;
716 from
= find_active_insn_after (then_bb
, from
);
717 delete_insn_chain (from
, BB_END (then_bb
), false);
720 delete_insn_chain (first_active_insn (else_bb
), else_last_head
, false);
722 merge_if_block (ce_info
);
723 cond_exec_changed_p
= TRUE
;
727 #ifdef IFCVT_MODIFY_CANCEL
728 /* Cancel any machine dependent changes. */
729 IFCVT_MODIFY_CANCEL (ce_info
);
736 /* Used by noce_process_if_block to communicate with its subroutines.
738 The subroutines know that A and B may be evaluated freely. They
739 know that X is a register. They should insert new instructions
740 before cond_earliest. */
744 /* The basic blocks that make up the IF-THEN-{ELSE-,}JOIN block. */
745 basic_block test_bb
, then_bb
, else_bb
, join_bb
;
747 /* The jump that ends TEST_BB. */
750 /* The jump condition. */
753 /* New insns should be inserted before this one. */
756 /* Insns in the THEN and ELSE block. There is always just this
757 one insns in those blocks. The insns are single_set insns.
758 If there was no ELSE block, INSN_B is the last insn before
759 COND_EARLIEST, or NULL_RTX. In the former case, the insn
760 operands are still valid, as if INSN_B was moved down below
764 /* The SET_SRC of INSN_A and INSN_B. */
767 /* The SET_DEST of INSN_A. */
770 /* True if this if block is not canonical. In the canonical form of
771 if blocks, the THEN_BB is the block reached via the fallthru edge
772 from TEST_BB. For the noce transformations, we allow the symmetric
774 bool then_else_reversed
;
776 /* Estimated cost of the particular branch instruction. */
780 static rtx
noce_emit_store_flag (struct noce_if_info
*, rtx
, int, int);
781 static int noce_try_move (struct noce_if_info
*);
782 static int noce_try_store_flag (struct noce_if_info
*);
783 static int noce_try_addcc (struct noce_if_info
*);
784 static int noce_try_store_flag_constants (struct noce_if_info
*);
785 static int noce_try_store_flag_mask (struct noce_if_info
*);
786 static rtx
noce_emit_cmove (struct noce_if_info
*, rtx
, enum rtx_code
, rtx
,
788 static int noce_try_cmove (struct noce_if_info
*);
789 static int noce_try_cmove_arith (struct noce_if_info
*);
790 static rtx
noce_get_alt_condition (struct noce_if_info
*, rtx
, rtx
*);
791 static int noce_try_minmax (struct noce_if_info
*);
792 static int noce_try_abs (struct noce_if_info
*);
793 static int noce_try_sign_mask (struct noce_if_info
*);
795 /* Helper function for noce_try_store_flag*. */
798 noce_emit_store_flag (struct noce_if_info
*if_info
, rtx x
, int reversep
,
801 rtx cond
= if_info
->cond
;
805 cond_complex
= (! general_operand (XEXP (cond
, 0), VOIDmode
)
806 || ! general_operand (XEXP (cond
, 1), VOIDmode
));
808 /* If earliest == jump, or when the condition is complex, try to
809 build the store_flag insn directly. */
813 rtx set
= pc_set (if_info
->jump
);
814 cond
= XEXP (SET_SRC (set
), 0);
815 if (GET_CODE (XEXP (SET_SRC (set
), 2)) == LABEL_REF
816 && XEXP (XEXP (SET_SRC (set
), 2), 0) == JUMP_LABEL (if_info
->jump
))
817 reversep
= !reversep
;
818 if (if_info
->then_else_reversed
)
819 reversep
= !reversep
;
823 code
= reversed_comparison_code (cond
, if_info
->jump
);
825 code
= GET_CODE (cond
);
827 if ((if_info
->cond_earliest
== if_info
->jump
|| cond_complex
)
828 && (normalize
== 0 || STORE_FLAG_VALUE
== normalize
))
832 tmp
= gen_rtx_fmt_ee (code
, GET_MODE (x
), XEXP (cond
, 0),
834 tmp
= gen_rtx_SET (VOIDmode
, x
, tmp
);
837 tmp
= emit_insn (tmp
);
839 if (recog_memoized (tmp
) >= 0)
845 if_info
->cond_earliest
= if_info
->jump
;
853 /* Don't even try if the comparison operands or the mode of X are weird. */
854 if (cond_complex
|| !SCALAR_INT_MODE_P (GET_MODE (x
)))
857 return emit_store_flag (x
, code
, XEXP (cond
, 0),
858 XEXP (cond
, 1), VOIDmode
,
859 (code
== LTU
|| code
== LEU
860 || code
== GEU
|| code
== GTU
), normalize
);
863 /* Emit instruction to move an rtx, possibly into STRICT_LOW_PART.
864 X is the destination/target and Y is the value to copy. */
867 noce_emit_move_insn (rtx x
, rtx y
)
869 enum machine_mode outmode
;
873 if (GET_CODE (x
) != STRICT_LOW_PART
)
875 rtx seq
, insn
, target
;
879 /* Check that the SET_SRC is reasonable before calling emit_move_insn,
880 otherwise construct a suitable SET pattern ourselves. */
881 insn
= (OBJECT_P (y
) || CONSTANT_P (y
) || GET_CODE (y
) == SUBREG
)
882 ? emit_move_insn (x
, y
)
883 : emit_insn (gen_rtx_SET (VOIDmode
, x
, y
));
887 if (recog_memoized (insn
) <= 0)
889 if (GET_CODE (x
) == ZERO_EXTRACT
)
891 rtx op
= XEXP (x
, 0);
892 unsigned HOST_WIDE_INT size
= INTVAL (XEXP (x
, 1));
893 unsigned HOST_WIDE_INT start
= INTVAL (XEXP (x
, 2));
895 /* store_bit_field expects START to be relative to
896 BYTES_BIG_ENDIAN and adjusts this value for machines with
897 BITS_BIG_ENDIAN != BYTES_BIG_ENDIAN. In order to be able to
898 invoke store_bit_field again it is necessary to have the START
899 value from the first call. */
900 if (BITS_BIG_ENDIAN
!= BYTES_BIG_ENDIAN
)
903 start
= BITS_PER_UNIT
- start
- size
;
906 gcc_assert (REG_P (op
));
907 start
= BITS_PER_WORD
- start
- size
;
911 gcc_assert (start
< (MEM_P (op
) ? BITS_PER_UNIT
: BITS_PER_WORD
));
912 store_bit_field (op
, size
, start
, 0, 0, GET_MODE (x
), y
);
916 switch (GET_RTX_CLASS (GET_CODE (y
)))
919 ot
= code_to_optab (GET_CODE (y
));
923 target
= expand_unop (GET_MODE (y
), ot
, XEXP (y
, 0), x
, 0);
924 if (target
!= NULL_RTX
)
927 emit_move_insn (x
, target
);
936 ot
= code_to_optab (GET_CODE (y
));
940 target
= expand_binop (GET_MODE (y
), ot
,
941 XEXP (y
, 0), XEXP (y
, 1),
943 if (target
!= NULL_RTX
)
946 emit_move_insn (x
, target
);
963 inner
= XEXP (outer
, 0);
964 outmode
= GET_MODE (outer
);
965 bitpos
= SUBREG_BYTE (outer
) * BITS_PER_UNIT
;
966 store_bit_field (inner
, GET_MODE_BITSIZE (outmode
), bitpos
,
970 /* Return sequence of instructions generated by if conversion. This
971 function calls end_sequence() to end the current stream, ensures
972 that are instructions are unshared, recognizable non-jump insns.
973 On failure, this function returns a NULL_RTX. */
976 end_ifcvt_sequence (struct noce_if_info
*if_info
)
979 rtx seq
= get_insns ();
981 set_used_flags (if_info
->x
);
982 set_used_flags (if_info
->cond
);
983 set_used_flags (if_info
->a
);
984 set_used_flags (if_info
->b
);
985 unshare_all_rtl_in_chain (seq
);
988 /* Make sure that all of the instructions emitted are recognizable,
989 and that we haven't introduced a new jump instruction.
990 As an exercise for the reader, build a general mechanism that
991 allows proper placement of required clobbers. */
992 for (insn
= seq
; insn
; insn
= NEXT_INSN (insn
))
994 || recog_memoized (insn
) == -1)
1000 /* Convert "if (a != b) x = a; else x = b" into "x = a" and
1001 "if (a == b) x = a; else x = b" into "x = b". */
1004 noce_try_move (struct noce_if_info
*if_info
)
1006 rtx cond
= if_info
->cond
;
1007 enum rtx_code code
= GET_CODE (cond
);
1010 if (code
!= NE
&& code
!= EQ
)
1013 /* This optimization isn't valid if either A or B could be a NaN
1014 or a signed zero. */
1015 if (HONOR_NANS (GET_MODE (if_info
->x
))
1016 || HONOR_SIGNED_ZEROS (GET_MODE (if_info
->x
)))
1019 /* Check whether the operands of the comparison are A and in
1021 if ((rtx_equal_p (if_info
->a
, XEXP (cond
, 0))
1022 && rtx_equal_p (if_info
->b
, XEXP (cond
, 1)))
1023 || (rtx_equal_p (if_info
->a
, XEXP (cond
, 1))
1024 && rtx_equal_p (if_info
->b
, XEXP (cond
, 0))))
1026 y
= (code
== EQ
) ? if_info
->a
: if_info
->b
;
1028 /* Avoid generating the move if the source is the destination. */
1029 if (! rtx_equal_p (if_info
->x
, y
))
1032 noce_emit_move_insn (if_info
->x
, y
);
1033 seq
= end_ifcvt_sequence (if_info
);
1037 emit_insn_before_setloc (seq
, if_info
->jump
,
1038 INSN_LOCATION (if_info
->insn_a
));
1045 /* Convert "if (test) x = 1; else x = 0".
1047 Only try 0 and STORE_FLAG_VALUE here. Other combinations will be
1048 tried in noce_try_store_flag_constants after noce_try_cmove has had
1049 a go at the conversion. */
1052 noce_try_store_flag (struct noce_if_info
*if_info
)
1057 if (CONST_INT_P (if_info
->b
)
1058 && INTVAL (if_info
->b
) == STORE_FLAG_VALUE
1059 && if_info
->a
== const0_rtx
)
1061 else if (if_info
->b
== const0_rtx
1062 && CONST_INT_P (if_info
->a
)
1063 && INTVAL (if_info
->a
) == STORE_FLAG_VALUE
1064 && (reversed_comparison_code (if_info
->cond
, if_info
->jump
)
1072 target
= noce_emit_store_flag (if_info
, if_info
->x
, reversep
, 0);
1075 if (target
!= if_info
->x
)
1076 noce_emit_move_insn (if_info
->x
, target
);
1078 seq
= end_ifcvt_sequence (if_info
);
1082 emit_insn_before_setloc (seq
, if_info
->jump
,
1083 INSN_LOCATION (if_info
->insn_a
));
1093 /* Convert "if (test) x = a; else x = b", for A and B constant. */
1096 noce_try_store_flag_constants (struct noce_if_info
*if_info
)
1100 HOST_WIDE_INT itrue
, ifalse
, diff
, tmp
;
1101 int normalize
, can_reverse
;
1102 enum machine_mode mode
;
1104 if (CONST_INT_P (if_info
->a
)
1105 && CONST_INT_P (if_info
->b
))
1107 mode
= GET_MODE (if_info
->x
);
1108 ifalse
= INTVAL (if_info
->a
);
1109 itrue
= INTVAL (if_info
->b
);
1111 /* Make sure we can represent the difference between the two values. */
1112 if ((itrue
- ifalse
> 0)
1113 != ((ifalse
< 0) != (itrue
< 0) ? ifalse
< 0 : ifalse
< itrue
))
1116 diff
= trunc_int_for_mode (itrue
- ifalse
, mode
);
1118 can_reverse
= (reversed_comparison_code (if_info
->cond
, if_info
->jump
)
1122 if (diff
== STORE_FLAG_VALUE
|| diff
== -STORE_FLAG_VALUE
)
1124 else if (ifalse
== 0 && exact_log2 (itrue
) >= 0
1125 && (STORE_FLAG_VALUE
== 1
1126 || if_info
->branch_cost
>= 2))
1128 else if (itrue
== 0 && exact_log2 (ifalse
) >= 0 && can_reverse
1129 && (STORE_FLAG_VALUE
== 1 || if_info
->branch_cost
>= 2))
1130 normalize
= 1, reversep
= 1;
1131 else if (itrue
== -1
1132 && (STORE_FLAG_VALUE
== -1
1133 || if_info
->branch_cost
>= 2))
1135 else if (ifalse
== -1 && can_reverse
1136 && (STORE_FLAG_VALUE
== -1 || if_info
->branch_cost
>= 2))
1137 normalize
= -1, reversep
= 1;
1138 else if ((if_info
->branch_cost
>= 2 && STORE_FLAG_VALUE
== -1)
1139 || if_info
->branch_cost
>= 3)
1146 tmp
= itrue
; itrue
= ifalse
; ifalse
= tmp
;
1147 diff
= trunc_int_for_mode (-diff
, mode
);
1151 target
= noce_emit_store_flag (if_info
, if_info
->x
, reversep
, normalize
);
1158 /* if (test) x = 3; else x = 4;
1159 => x = 3 + (test == 0); */
1160 if (diff
== STORE_FLAG_VALUE
|| diff
== -STORE_FLAG_VALUE
)
1162 target
= expand_simple_binop (mode
,
1163 (diff
== STORE_FLAG_VALUE
1165 gen_int_mode (ifalse
, mode
), target
,
1166 if_info
->x
, 0, OPTAB_WIDEN
);
1169 /* if (test) x = 8; else x = 0;
1170 => x = (test != 0) << 3; */
1171 else if (ifalse
== 0 && (tmp
= exact_log2 (itrue
)) >= 0)
1173 target
= expand_simple_binop (mode
, ASHIFT
,
1174 target
, GEN_INT (tmp
), if_info
->x
, 0,
1178 /* if (test) x = -1; else x = b;
1179 => x = -(test != 0) | b; */
1180 else if (itrue
== -1)
1182 target
= expand_simple_binop (mode
, IOR
,
1183 target
, gen_int_mode (ifalse
, mode
),
1184 if_info
->x
, 0, OPTAB_WIDEN
);
1187 /* if (test) x = a; else x = b;
1188 => x = (-(test != 0) & (b - a)) + a; */
1191 target
= expand_simple_binop (mode
, AND
,
1192 target
, gen_int_mode (diff
, mode
),
1193 if_info
->x
, 0, OPTAB_WIDEN
);
1195 target
= expand_simple_binop (mode
, PLUS
,
1196 target
, gen_int_mode (ifalse
, mode
),
1197 if_info
->x
, 0, OPTAB_WIDEN
);
1206 if (target
!= if_info
->x
)
1207 noce_emit_move_insn (if_info
->x
, target
);
1209 seq
= end_ifcvt_sequence (if_info
);
1213 emit_insn_before_setloc (seq
, if_info
->jump
,
1214 INSN_LOCATION (if_info
->insn_a
));
1221 /* Convert "if (test) foo++" into "foo += (test != 0)", and
1222 similarly for "foo--". */
1225 noce_try_addcc (struct noce_if_info
*if_info
)
1228 int subtract
, normalize
;
1230 if (GET_CODE (if_info
->a
) == PLUS
1231 && rtx_equal_p (XEXP (if_info
->a
, 0), if_info
->b
)
1232 && (reversed_comparison_code (if_info
->cond
, if_info
->jump
)
1235 rtx cond
= if_info
->cond
;
1236 enum rtx_code code
= reversed_comparison_code (cond
, if_info
->jump
);
1238 /* First try to use addcc pattern. */
1239 if (general_operand (XEXP (cond
, 0), VOIDmode
)
1240 && general_operand (XEXP (cond
, 1), VOIDmode
))
1243 target
= emit_conditional_add (if_info
->x
, code
,
1248 XEXP (if_info
->a
, 1),
1249 GET_MODE (if_info
->x
),
1250 (code
== LTU
|| code
== GEU
1251 || code
== LEU
|| code
== GTU
));
1254 if (target
!= if_info
->x
)
1255 noce_emit_move_insn (if_info
->x
, target
);
1257 seq
= end_ifcvt_sequence (if_info
);
1261 emit_insn_before_setloc (seq
, if_info
->jump
,
1262 INSN_LOCATION (if_info
->insn_a
));
1268 /* If that fails, construct conditional increment or decrement using
1270 if (if_info
->branch_cost
>= 2
1271 && (XEXP (if_info
->a
, 1) == const1_rtx
1272 || XEXP (if_info
->a
, 1) == constm1_rtx
))
1275 if (STORE_FLAG_VALUE
== INTVAL (XEXP (if_info
->a
, 1)))
1276 subtract
= 0, normalize
= 0;
1277 else if (-STORE_FLAG_VALUE
== INTVAL (XEXP (if_info
->a
, 1)))
1278 subtract
= 1, normalize
= 0;
1280 subtract
= 0, normalize
= INTVAL (XEXP (if_info
->a
, 1));
1283 target
= noce_emit_store_flag (if_info
,
1284 gen_reg_rtx (GET_MODE (if_info
->x
)),
1288 target
= expand_simple_binop (GET_MODE (if_info
->x
),
1289 subtract
? MINUS
: PLUS
,
1290 if_info
->b
, target
, if_info
->x
,
1294 if (target
!= if_info
->x
)
1295 noce_emit_move_insn (if_info
->x
, target
);
1297 seq
= end_ifcvt_sequence (if_info
);
1301 emit_insn_before_setloc (seq
, if_info
->jump
,
1302 INSN_LOCATION (if_info
->insn_a
));
1312 /* Convert "if (test) x = 0;" to "x &= -(test == 0);" */
1315 noce_try_store_flag_mask (struct noce_if_info
*if_info
)
1321 if ((if_info
->branch_cost
>= 2
1322 || STORE_FLAG_VALUE
== -1)
1323 && ((if_info
->a
== const0_rtx
1324 && rtx_equal_p (if_info
->b
, if_info
->x
))
1325 || ((reversep
= (reversed_comparison_code (if_info
->cond
,
1328 && if_info
->b
== const0_rtx
1329 && rtx_equal_p (if_info
->a
, if_info
->x
))))
1332 target
= noce_emit_store_flag (if_info
,
1333 gen_reg_rtx (GET_MODE (if_info
->x
)),
1336 target
= expand_simple_binop (GET_MODE (if_info
->x
), AND
,
1338 target
, if_info
->x
, 0,
1343 if (target
!= if_info
->x
)
1344 noce_emit_move_insn (if_info
->x
, target
);
1346 seq
= end_ifcvt_sequence (if_info
);
1350 emit_insn_before_setloc (seq
, if_info
->jump
,
1351 INSN_LOCATION (if_info
->insn_a
));
1361 /* Helper function for noce_try_cmove and noce_try_cmove_arith. */
1364 noce_emit_cmove (struct noce_if_info
*if_info
, rtx x
, enum rtx_code code
,
1365 rtx cmp_a
, rtx cmp_b
, rtx vfalse
, rtx vtrue
)
1367 rtx target ATTRIBUTE_UNUSED
;
1368 int unsignedp ATTRIBUTE_UNUSED
;
1370 /* If earliest == jump, try to build the cmove insn directly.
1371 This is helpful when combine has created some complex condition
1372 (like for alpha's cmovlbs) that we can't hope to regenerate
1373 through the normal interface. */
1375 if (if_info
->cond_earliest
== if_info
->jump
)
1379 tmp
= gen_rtx_fmt_ee (code
, GET_MODE (if_info
->cond
), cmp_a
, cmp_b
);
1380 tmp
= gen_rtx_IF_THEN_ELSE (GET_MODE (x
), tmp
, vtrue
, vfalse
);
1381 tmp
= gen_rtx_SET (VOIDmode
, x
, tmp
);
1384 tmp
= emit_insn (tmp
);
1386 if (recog_memoized (tmp
) >= 0)
1398 /* Don't even try if the comparison operands are weird. */
1399 if (! general_operand (cmp_a
, GET_MODE (cmp_a
))
1400 || ! general_operand (cmp_b
, GET_MODE (cmp_b
)))
1403 #if HAVE_conditional_move
1404 unsignedp
= (code
== LTU
|| code
== GEU
1405 || code
== LEU
|| code
== GTU
);
1407 target
= emit_conditional_move (x
, code
, cmp_a
, cmp_b
, VOIDmode
,
1408 vtrue
, vfalse
, GET_MODE (x
),
1413 /* We might be faced with a situation like:
1416 vtrue = (subreg:M (reg:N VTRUE) BYTE)
1417 vfalse = (subreg:M (reg:N VFALSE) BYTE)
1419 We can't do a conditional move in mode M, but it's possible that we
1420 could do a conditional move in mode N instead and take a subreg of
1423 If we can't create new pseudos, though, don't bother. */
1424 if (reload_completed
)
1427 if (GET_CODE (vtrue
) == SUBREG
&& GET_CODE (vfalse
) == SUBREG
)
1429 rtx reg_vtrue
= SUBREG_REG (vtrue
);
1430 rtx reg_vfalse
= SUBREG_REG (vfalse
);
1431 unsigned int byte_vtrue
= SUBREG_BYTE (vtrue
);
1432 unsigned int byte_vfalse
= SUBREG_BYTE (vfalse
);
1433 rtx promoted_target
;
1435 if (GET_MODE (reg_vtrue
) != GET_MODE (reg_vfalse
)
1436 || byte_vtrue
!= byte_vfalse
1437 || (SUBREG_PROMOTED_VAR_P (vtrue
)
1438 != SUBREG_PROMOTED_VAR_P (vfalse
))
1439 || (SUBREG_PROMOTED_UNSIGNED_P (vtrue
)
1440 != SUBREG_PROMOTED_UNSIGNED_P (vfalse
)))
1443 promoted_target
= gen_reg_rtx (GET_MODE (reg_vtrue
));
1445 target
= emit_conditional_move (promoted_target
, code
, cmp_a
, cmp_b
,
1446 VOIDmode
, reg_vtrue
, reg_vfalse
,
1447 GET_MODE (reg_vtrue
), unsignedp
);
1448 /* Nope, couldn't do it in that mode either. */
1452 target
= gen_rtx_SUBREG (GET_MODE (vtrue
), promoted_target
, byte_vtrue
);
1453 SUBREG_PROMOTED_VAR_P (target
) = SUBREG_PROMOTED_VAR_P (vtrue
);
1454 SUBREG_PROMOTED_UNSIGNED_SET (target
, SUBREG_PROMOTED_UNSIGNED_P (vtrue
));
1455 emit_move_insn (x
, target
);
1461 /* We'll never get here, as noce_process_if_block doesn't call the
1462 functions involved. Ifdef code, however, should be discouraged
1463 because it leads to typos in the code not selected. However,
1464 emit_conditional_move won't exist either. */
1469 /* Try only simple constants and registers here. More complex cases
1470 are handled in noce_try_cmove_arith after noce_try_store_flag_arith
1471 has had a go at it. */
1474 noce_try_cmove (struct noce_if_info
*if_info
)
1479 if ((CONSTANT_P (if_info
->a
) || register_operand (if_info
->a
, VOIDmode
))
1480 && (CONSTANT_P (if_info
->b
) || register_operand (if_info
->b
, VOIDmode
)))
1484 code
= GET_CODE (if_info
->cond
);
1485 target
= noce_emit_cmove (if_info
, if_info
->x
, code
,
1486 XEXP (if_info
->cond
, 0),
1487 XEXP (if_info
->cond
, 1),
1488 if_info
->a
, if_info
->b
);
1492 if (target
!= if_info
->x
)
1493 noce_emit_move_insn (if_info
->x
, target
);
1495 seq
= end_ifcvt_sequence (if_info
);
1499 emit_insn_before_setloc (seq
, if_info
->jump
,
1500 INSN_LOCATION (if_info
->insn_a
));
1513 /* Try more complex cases involving conditional_move. */
1516 noce_try_cmove_arith (struct noce_if_info
*if_info
)
1528 /* A conditional move from two memory sources is equivalent to a
1529 conditional on their addresses followed by a load. Don't do this
1530 early because it'll screw alias analysis. Note that we've
1531 already checked for no side effects. */
1532 /* ??? FIXME: Magic number 5. */
1533 if (cse_not_expected
1534 && MEM_P (a
) && MEM_P (b
)
1535 && MEM_ADDR_SPACE (a
) == MEM_ADDR_SPACE (b
)
1536 && if_info
->branch_cost
>= 5)
1538 enum machine_mode address_mode
= get_address_mode (a
);
1542 x
= gen_reg_rtx (address_mode
);
1546 /* ??? We could handle this if we knew that a load from A or B could
1547 not trap or fault. This is also true if we've already loaded
1548 from the address along the path from ENTRY. */
1549 else if (may_trap_or_fault_p (a
) || may_trap_or_fault_p (b
))
1552 /* if (test) x = a + b; else x = c - d;
1559 code
= GET_CODE (if_info
->cond
);
1560 insn_a
= if_info
->insn_a
;
1561 insn_b
= if_info
->insn_b
;
1563 /* Total insn_rtx_cost should be smaller than branch cost. Exit
1564 if insn_rtx_cost can't be estimated. */
1568 = insn_rtx_cost (PATTERN (insn_a
),
1569 optimize_bb_for_speed_p (BLOCK_FOR_INSN (insn_a
)));
1570 if (insn_cost
== 0 || insn_cost
> COSTS_N_INSNS (if_info
->branch_cost
))
1579 += insn_rtx_cost (PATTERN (insn_b
),
1580 optimize_bb_for_speed_p (BLOCK_FOR_INSN (insn_b
)));
1581 if (insn_cost
== 0 || insn_cost
> COSTS_N_INSNS (if_info
->branch_cost
))
1585 /* Possibly rearrange operands to make things come out more natural. */
1586 if (reversed_comparison_code (if_info
->cond
, if_info
->jump
) != UNKNOWN
)
1589 if (rtx_equal_p (b
, x
))
1591 else if (general_operand (b
, GET_MODE (b
)))
1596 code
= reversed_comparison_code (if_info
->cond
, if_info
->jump
);
1597 tmp
= a
, a
= b
, b
= tmp
;
1598 tmp
= insn_a
, insn_a
= insn_b
, insn_b
= tmp
;
1607 /* If either operand is complex, load it into a register first.
1608 The best way to do this is to copy the original insn. In this
1609 way we preserve any clobbers etc that the insn may have had.
1610 This is of course not possible in the IS_MEM case. */
1611 if (! general_operand (a
, GET_MODE (a
)))
1617 tmp
= gen_reg_rtx (GET_MODE (a
));
1618 tmp
= emit_insn (gen_rtx_SET (VOIDmode
, tmp
, a
));
1621 goto end_seq_and_fail
;
1624 a
= gen_reg_rtx (GET_MODE (a
));
1625 tmp
= copy_rtx (insn_a
);
1626 set
= single_set (tmp
);
1628 tmp
= emit_insn (PATTERN (tmp
));
1630 if (recog_memoized (tmp
) < 0)
1631 goto end_seq_and_fail
;
1633 if (! general_operand (b
, GET_MODE (b
)))
1639 tmp
= gen_reg_rtx (GET_MODE (b
));
1640 tmp
= gen_rtx_SET (VOIDmode
, tmp
, b
);
1643 goto end_seq_and_fail
;
1646 b
= gen_reg_rtx (GET_MODE (b
));
1647 tmp
= copy_rtx (insn_b
);
1648 set
= single_set (tmp
);
1650 tmp
= PATTERN (tmp
);
1653 /* If insn to set up A clobbers any registers B depends on, try to
1654 swap insn that sets up A with the one that sets up B. If even
1655 that doesn't help, punt. */
1656 last
= get_last_insn ();
1657 if (last
&& modified_in_p (orig_b
, last
))
1659 tmp
= emit_insn_before (tmp
, get_insns ());
1660 if (modified_in_p (orig_a
, tmp
))
1661 goto end_seq_and_fail
;
1664 tmp
= emit_insn (tmp
);
1666 if (recog_memoized (tmp
) < 0)
1667 goto end_seq_and_fail
;
1670 target
= noce_emit_cmove (if_info
, x
, code
, XEXP (if_info
->cond
, 0),
1671 XEXP (if_info
->cond
, 1), a
, b
);
1674 goto end_seq_and_fail
;
1676 /* If we're handling a memory for above, emit the load now. */
1679 tmp
= gen_rtx_MEM (GET_MODE (if_info
->x
), target
);
1681 /* Copy over flags as appropriate. */
1682 if (MEM_VOLATILE_P (if_info
->a
) || MEM_VOLATILE_P (if_info
->b
))
1683 MEM_VOLATILE_P (tmp
) = 1;
1684 if (MEM_ALIAS_SET (if_info
->a
) == MEM_ALIAS_SET (if_info
->b
))
1685 set_mem_alias_set (tmp
, MEM_ALIAS_SET (if_info
->a
));
1687 MIN (MEM_ALIGN (if_info
->a
), MEM_ALIGN (if_info
->b
)));
1689 gcc_assert (MEM_ADDR_SPACE (if_info
->a
) == MEM_ADDR_SPACE (if_info
->b
));
1690 set_mem_addr_space (tmp
, MEM_ADDR_SPACE (if_info
->a
));
1692 noce_emit_move_insn (if_info
->x
, tmp
);
1694 else if (target
!= x
)
1695 noce_emit_move_insn (x
, target
);
1697 tmp
= end_ifcvt_sequence (if_info
);
1701 emit_insn_before_setloc (tmp
, if_info
->jump
, INSN_LOCATION (if_info
->insn_a
));
1709 /* For most cases, the simplified condition we found is the best
1710 choice, but this is not the case for the min/max/abs transforms.
1711 For these we wish to know that it is A or B in the condition. */
1714 noce_get_alt_condition (struct noce_if_info
*if_info
, rtx target
,
1717 rtx cond
, set
, insn
;
1720 /* If target is already mentioned in the known condition, return it. */
1721 if (reg_mentioned_p (target
, if_info
->cond
))
1723 *earliest
= if_info
->cond_earliest
;
1724 return if_info
->cond
;
1727 set
= pc_set (if_info
->jump
);
1728 cond
= XEXP (SET_SRC (set
), 0);
1730 = GET_CODE (XEXP (SET_SRC (set
), 2)) == LABEL_REF
1731 && XEXP (XEXP (SET_SRC (set
), 2), 0) == JUMP_LABEL (if_info
->jump
);
1732 if (if_info
->then_else_reversed
)
1735 /* If we're looking for a constant, try to make the conditional
1736 have that constant in it. There are two reasons why it may
1737 not have the constant we want:
1739 1. GCC may have needed to put the constant in a register, because
1740 the target can't compare directly against that constant. For
1741 this case, we look for a SET immediately before the comparison
1742 that puts a constant in that register.
1744 2. GCC may have canonicalized the conditional, for example
1745 replacing "if x < 4" with "if x <= 3". We can undo that (or
1746 make equivalent types of changes) to get the constants we need
1747 if they're off by one in the right direction. */
1749 if (CONST_INT_P (target
))
1751 enum rtx_code code
= GET_CODE (if_info
->cond
);
1752 rtx op_a
= XEXP (if_info
->cond
, 0);
1753 rtx op_b
= XEXP (if_info
->cond
, 1);
1756 /* First, look to see if we put a constant in a register. */
1757 prev_insn
= prev_nonnote_insn (if_info
->cond_earliest
);
1759 && BLOCK_FOR_INSN (prev_insn
)
1760 == BLOCK_FOR_INSN (if_info
->cond_earliest
)
1761 && INSN_P (prev_insn
)
1762 && GET_CODE (PATTERN (prev_insn
)) == SET
)
1764 rtx src
= find_reg_equal_equiv_note (prev_insn
);
1766 src
= SET_SRC (PATTERN (prev_insn
));
1767 if (CONST_INT_P (src
))
1769 if (rtx_equal_p (op_a
, SET_DEST (PATTERN (prev_insn
))))
1771 else if (rtx_equal_p (op_b
, SET_DEST (PATTERN (prev_insn
))))
1774 if (CONST_INT_P (op_a
))
1779 code
= swap_condition (code
);
1784 /* Now, look to see if we can get the right constant by
1785 adjusting the conditional. */
1786 if (CONST_INT_P (op_b
))
1788 HOST_WIDE_INT desired_val
= INTVAL (target
);
1789 HOST_WIDE_INT actual_val
= INTVAL (op_b
);
1794 if (actual_val
== desired_val
+ 1)
1797 op_b
= GEN_INT (desired_val
);
1801 if (actual_val
== desired_val
- 1)
1804 op_b
= GEN_INT (desired_val
);
1808 if (actual_val
== desired_val
- 1)
1811 op_b
= GEN_INT (desired_val
);
1815 if (actual_val
== desired_val
+ 1)
1818 op_b
= GEN_INT (desired_val
);
1826 /* If we made any changes, generate a new conditional that is
1827 equivalent to what we started with, but has the right
1829 if (code
!= GET_CODE (if_info
->cond
)
1830 || op_a
!= XEXP (if_info
->cond
, 0)
1831 || op_b
!= XEXP (if_info
->cond
, 1))
1833 cond
= gen_rtx_fmt_ee (code
, GET_MODE (cond
), op_a
, op_b
);
1834 *earliest
= if_info
->cond_earliest
;
1839 cond
= canonicalize_condition (if_info
->jump
, cond
, reverse
,
1840 earliest
, target
, false, true);
1841 if (! cond
|| ! reg_mentioned_p (target
, cond
))
1844 /* We almost certainly searched back to a different place.
1845 Need to re-verify correct lifetimes. */
1847 /* X may not be mentioned in the range (cond_earliest, jump]. */
1848 for (insn
= if_info
->jump
; insn
!= *earliest
; insn
= PREV_INSN (insn
))
1849 if (INSN_P (insn
) && reg_overlap_mentioned_p (if_info
->x
, PATTERN (insn
)))
1852 /* A and B may not be modified in the range [cond_earliest, jump). */
1853 for (insn
= *earliest
; insn
!= if_info
->jump
; insn
= NEXT_INSN (insn
))
1855 && (modified_in_p (if_info
->a
, insn
)
1856 || modified_in_p (if_info
->b
, insn
)))
1862 /* Convert "if (a < b) x = a; else x = b;" to "x = min(a, b);", etc. */
1865 noce_try_minmax (struct noce_if_info
*if_info
)
1867 rtx cond
, earliest
, target
, seq
;
1868 enum rtx_code code
, op
;
1871 /* ??? Reject modes with NaNs or signed zeros since we don't know how
1872 they will be resolved with an SMIN/SMAX. It wouldn't be too hard
1873 to get the target to tell us... */
1874 if (HONOR_SIGNED_ZEROS (GET_MODE (if_info
->x
))
1875 || HONOR_NANS (GET_MODE (if_info
->x
)))
1878 cond
= noce_get_alt_condition (if_info
, if_info
->a
, &earliest
);
1882 /* Verify the condition is of the form we expect, and canonicalize
1883 the comparison code. */
1884 code
= GET_CODE (cond
);
1885 if (rtx_equal_p (XEXP (cond
, 0), if_info
->a
))
1887 if (! rtx_equal_p (XEXP (cond
, 1), if_info
->b
))
1890 else if (rtx_equal_p (XEXP (cond
, 1), if_info
->a
))
1892 if (! rtx_equal_p (XEXP (cond
, 0), if_info
->b
))
1894 code
= swap_condition (code
);
1899 /* Determine what sort of operation this is. Note that the code is for
1900 a taken branch, so the code->operation mapping appears backwards. */
1933 target
= expand_simple_binop (GET_MODE (if_info
->x
), op
,
1934 if_info
->a
, if_info
->b
,
1935 if_info
->x
, unsignedp
, OPTAB_WIDEN
);
1941 if (target
!= if_info
->x
)
1942 noce_emit_move_insn (if_info
->x
, target
);
1944 seq
= end_ifcvt_sequence (if_info
);
1948 emit_insn_before_setloc (seq
, if_info
->jump
, INSN_LOCATION (if_info
->insn_a
));
1949 if_info
->cond
= cond
;
1950 if_info
->cond_earliest
= earliest
;
1955 /* Convert "if (a < 0) x = -a; else x = a;" to "x = abs(a);",
1956 "if (a < 0) x = ~a; else x = a;" to "x = one_cmpl_abs(a);",
1960 noce_try_abs (struct noce_if_info
*if_info
)
1962 rtx cond
, earliest
, target
, seq
, a
, b
, c
;
1964 bool one_cmpl
= false;
1966 /* Reject modes with signed zeros. */
1967 if (HONOR_SIGNED_ZEROS (GET_MODE (if_info
->x
)))
1970 /* Recognize A and B as constituting an ABS or NABS. The canonical
1971 form is a branch around the negation, taken when the object is the
1972 first operand of a comparison against 0 that evaluates to true. */
1975 if (GET_CODE (a
) == NEG
&& rtx_equal_p (XEXP (a
, 0), b
))
1977 else if (GET_CODE (b
) == NEG
&& rtx_equal_p (XEXP (b
, 0), a
))
1979 c
= a
; a
= b
; b
= c
;
1982 else if (GET_CODE (a
) == NOT
&& rtx_equal_p (XEXP (a
, 0), b
))
1987 else if (GET_CODE (b
) == NOT
&& rtx_equal_p (XEXP (b
, 0), a
))
1989 c
= a
; a
= b
; b
= c
;
1996 cond
= noce_get_alt_condition (if_info
, b
, &earliest
);
2000 /* Verify the condition is of the form we expect. */
2001 if (rtx_equal_p (XEXP (cond
, 0), b
))
2003 else if (rtx_equal_p (XEXP (cond
, 1), b
))
2011 /* Verify that C is zero. Search one step backward for a
2012 REG_EQUAL note or a simple source if necessary. */
2015 rtx set
, insn
= prev_nonnote_insn (earliest
);
2017 && BLOCK_FOR_INSN (insn
) == BLOCK_FOR_INSN (earliest
)
2018 && (set
= single_set (insn
))
2019 && rtx_equal_p (SET_DEST (set
), c
))
2021 rtx note
= find_reg_equal_equiv_note (insn
);
2031 && GET_CODE (XEXP (c
, 0)) == SYMBOL_REF
2032 && CONSTANT_POOL_ADDRESS_P (XEXP (c
, 0)))
2033 c
= get_pool_constant (XEXP (c
, 0));
2035 /* Work around funny ideas get_condition has wrt canonicalization.
2036 Note that these rtx constants are known to be CONST_INT, and
2037 therefore imply integer comparisons. */
2038 if (c
== constm1_rtx
&& GET_CODE (cond
) == GT
)
2040 else if (c
== const1_rtx
&& GET_CODE (cond
) == LT
)
2042 else if (c
!= CONST0_RTX (GET_MODE (b
)))
2045 /* Determine what sort of operation this is. */
2046 switch (GET_CODE (cond
))
2065 target
= expand_one_cmpl_abs_nojump (GET_MODE (if_info
->x
), b
,
2068 target
= expand_abs_nojump (GET_MODE (if_info
->x
), b
, if_info
->x
, 1);
2070 /* ??? It's a quandary whether cmove would be better here, especially
2071 for integers. Perhaps combine will clean things up. */
2072 if (target
&& negate
)
2075 target
= expand_simple_unop (GET_MODE (target
), NOT
, target
,
2078 target
= expand_simple_unop (GET_MODE (target
), NEG
, target
,
2088 if (target
!= if_info
->x
)
2089 noce_emit_move_insn (if_info
->x
, target
);
2091 seq
= end_ifcvt_sequence (if_info
);
2095 emit_insn_before_setloc (seq
, if_info
->jump
, INSN_LOCATION (if_info
->insn_a
));
2096 if_info
->cond
= cond
;
2097 if_info
->cond_earliest
= earliest
;
2102 /* Convert "if (m < 0) x = b; else x = 0;" to "x = (m >> C) & b;". */
2105 noce_try_sign_mask (struct noce_if_info
*if_info
)
2107 rtx cond
, t
, m
, c
, seq
;
2108 enum machine_mode mode
;
2110 bool t_unconditional
;
2112 cond
= if_info
->cond
;
2113 code
= GET_CODE (cond
);
2118 if (if_info
->a
== const0_rtx
)
2120 if ((code
== LT
&& c
== const0_rtx
)
2121 || (code
== LE
&& c
== constm1_rtx
))
2124 else if (if_info
->b
== const0_rtx
)
2126 if ((code
== GE
&& c
== const0_rtx
)
2127 || (code
== GT
&& c
== constm1_rtx
))
2131 if (! t
|| side_effects_p (t
))
2134 /* We currently don't handle different modes. */
2135 mode
= GET_MODE (t
);
2136 if (GET_MODE (m
) != mode
)
2139 /* This is only profitable if T is unconditionally executed/evaluated in the
2140 original insn sequence or T is cheap. The former happens if B is the
2141 non-zero (T) value and if INSN_B was taken from TEST_BB, or there was no
2142 INSN_B which can happen for e.g. conditional stores to memory. For the
2143 cost computation use the block TEST_BB where the evaluation will end up
2144 after the transformation. */
2147 && (if_info
->insn_b
== NULL_RTX
2148 || BLOCK_FOR_INSN (if_info
->insn_b
) == if_info
->test_bb
));
2149 if (!(t_unconditional
2150 || (set_src_cost (t
, optimize_bb_for_speed_p (if_info
->test_bb
))
2151 < COSTS_N_INSNS (2))))
2155 /* Use emit_store_flag to generate "m < 0 ? -1 : 0" instead of expanding
2156 "(signed) m >> 31" directly. This benefits targets with specialized
2157 insns to obtain the signmask, but still uses ashr_optab otherwise. */
2158 m
= emit_store_flag (gen_reg_rtx (mode
), LT
, m
, const0_rtx
, mode
, 0, -1);
2159 t
= m
? expand_binop (mode
, and_optab
, m
, t
, NULL_RTX
, 0, OPTAB_DIRECT
)
2168 noce_emit_move_insn (if_info
->x
, t
);
2170 seq
= end_ifcvt_sequence (if_info
);
2174 emit_insn_before_setloc (seq
, if_info
->jump
, INSN_LOCATION (if_info
->insn_a
));
2179 /* Optimize away "if (x & C) x |= C" and similar bit manipulation
2183 noce_try_bitop (struct noce_if_info
*if_info
)
2185 rtx cond
, x
, a
, result
, seq
;
2186 enum machine_mode mode
;
2191 cond
= if_info
->cond
;
2192 code
= GET_CODE (cond
);
2194 /* Check for no else condition. */
2195 if (! rtx_equal_p (x
, if_info
->b
))
2198 /* Check for a suitable condition. */
2199 if (code
!= NE
&& code
!= EQ
)
2201 if (XEXP (cond
, 1) != const0_rtx
)
2203 cond
= XEXP (cond
, 0);
2205 /* ??? We could also handle AND here. */
2206 if (GET_CODE (cond
) == ZERO_EXTRACT
)
2208 if (XEXP (cond
, 1) != const1_rtx
2209 || !CONST_INT_P (XEXP (cond
, 2))
2210 || ! rtx_equal_p (x
, XEXP (cond
, 0)))
2212 bitnum
= INTVAL (XEXP (cond
, 2));
2213 mode
= GET_MODE (x
);
2214 if (BITS_BIG_ENDIAN
)
2215 bitnum
= GET_MODE_BITSIZE (mode
) - 1 - bitnum
;
2216 if (bitnum
< 0 || bitnum
>= HOST_BITS_PER_WIDE_INT
)
2223 if (GET_CODE (a
) == IOR
|| GET_CODE (a
) == XOR
)
2225 /* Check for "if (X & C) x = x op C". */
2226 if (! rtx_equal_p (x
, XEXP (a
, 0))
2227 || !CONST_INT_P (XEXP (a
, 1))
2228 || (INTVAL (XEXP (a
, 1)) & GET_MODE_MASK (mode
))
2229 != (unsigned HOST_WIDE_INT
) 1 << bitnum
)
2232 /* if ((x & C) == 0) x |= C; is transformed to x |= C. */
2233 /* if ((x & C) != 0) x |= C; is transformed to nothing. */
2234 if (GET_CODE (a
) == IOR
)
2235 result
= (code
== NE
) ? a
: NULL_RTX
;
2236 else if (code
== NE
)
2238 /* if ((x & C) == 0) x ^= C; is transformed to x |= C. */
2239 result
= gen_int_mode ((HOST_WIDE_INT
) 1 << bitnum
, mode
);
2240 result
= simplify_gen_binary (IOR
, mode
, x
, result
);
2244 /* if ((x & C) != 0) x ^= C; is transformed to x &= ~C. */
2245 result
= gen_int_mode (~((HOST_WIDE_INT
) 1 << bitnum
), mode
);
2246 result
= simplify_gen_binary (AND
, mode
, x
, result
);
2249 else if (GET_CODE (a
) == AND
)
2251 /* Check for "if (X & C) x &= ~C". */
2252 if (! rtx_equal_p (x
, XEXP (a
, 0))
2253 || !CONST_INT_P (XEXP (a
, 1))
2254 || (INTVAL (XEXP (a
, 1)) & GET_MODE_MASK (mode
))
2255 != (~((HOST_WIDE_INT
) 1 << bitnum
) & GET_MODE_MASK (mode
)))
2258 /* if ((x & C) == 0) x &= ~C; is transformed to nothing. */
2259 /* if ((x & C) != 0) x &= ~C; is transformed to x &= ~C. */
2260 result
= (code
== EQ
) ? a
: NULL_RTX
;
2268 noce_emit_move_insn (x
, result
);
2269 seq
= end_ifcvt_sequence (if_info
);
2273 emit_insn_before_setloc (seq
, if_info
->jump
,
2274 INSN_LOCATION (if_info
->insn_a
));
2280 /* Similar to get_condition, only the resulting condition must be
2281 valid at JUMP, instead of at EARLIEST.
2283 If THEN_ELSE_REVERSED is true, the fallthrough does not go to the
2284 THEN block of the caller, and we have to reverse the condition. */
2287 noce_get_condition (rtx jump
, rtx
*earliest
, bool then_else_reversed
)
2292 if (! any_condjump_p (jump
))
2295 set
= pc_set (jump
);
2297 /* If this branches to JUMP_LABEL when the condition is false,
2298 reverse the condition. */
2299 reverse
= (GET_CODE (XEXP (SET_SRC (set
), 2)) == LABEL_REF
2300 && XEXP (XEXP (SET_SRC (set
), 2), 0) == JUMP_LABEL (jump
));
2302 /* We may have to reverse because the caller's if block is not canonical,
2303 i.e. the THEN block isn't the fallthrough block for the TEST block
2304 (see find_if_header). */
2305 if (then_else_reversed
)
2308 /* If the condition variable is a register and is MODE_INT, accept it. */
2310 cond
= XEXP (SET_SRC (set
), 0);
2311 tmp
= XEXP (cond
, 0);
2312 if (REG_P (tmp
) && GET_MODE_CLASS (GET_MODE (tmp
)) == MODE_INT
2313 && (GET_MODE (tmp
) != BImode
2314 || !targetm
.small_register_classes_for_mode_p (BImode
)))
2319 cond
= gen_rtx_fmt_ee (reverse_condition (GET_CODE (cond
)),
2320 GET_MODE (cond
), tmp
, XEXP (cond
, 1));
2324 /* Otherwise, fall back on canonicalize_condition to do the dirty
2325 work of manipulating MODE_CC values and COMPARE rtx codes. */
2326 tmp
= canonicalize_condition (jump
, cond
, reverse
, earliest
,
2327 NULL_RTX
, false, true);
2329 /* We don't handle side-effects in the condition, like handling
2330 REG_INC notes and making sure no duplicate conditions are emitted. */
2331 if (tmp
!= NULL_RTX
&& side_effects_p (tmp
))
2337 /* Return true if OP is ok for if-then-else processing. */
2340 noce_operand_ok (const_rtx op
)
2342 if (side_effects_p (op
))
2345 /* We special-case memories, so handle any of them with
2346 no address side effects. */
2348 return ! side_effects_p (XEXP (op
, 0));
2350 return ! may_trap_p (op
);
2353 /* Return true if a write into MEM may trap or fault. */
2356 noce_mem_write_may_trap_or_fault_p (const_rtx mem
)
2360 if (MEM_READONLY_P (mem
))
2363 if (may_trap_or_fault_p (mem
))
2366 addr
= XEXP (mem
, 0);
2368 /* Call target hook to avoid the effects of -fpic etc.... */
2369 addr
= targetm
.delegitimize_address (addr
);
2372 switch (GET_CODE (addr
))
2380 addr
= XEXP (addr
, 0);
2384 addr
= XEXP (addr
, 1);
2387 if (CONST_INT_P (XEXP (addr
, 1)))
2388 addr
= XEXP (addr
, 0);
2395 if (SYMBOL_REF_DECL (addr
)
2396 && decl_readonly_section (SYMBOL_REF_DECL (addr
), 0))
2406 /* Return whether we can use store speculation for MEM. TOP_BB is the
2407 basic block above the conditional block where we are considering
2408 doing the speculative store. We look for whether MEM is set
2409 unconditionally later in the function. */
2412 noce_can_store_speculate_p (basic_block top_bb
, const_rtx mem
)
2414 basic_block dominator
;
2416 for (dominator
= get_immediate_dominator (CDI_POST_DOMINATORS
, top_bb
);
2418 dominator
= get_immediate_dominator (CDI_POST_DOMINATORS
, dominator
))
2422 FOR_BB_INSNS (dominator
, insn
)
2424 /* If we see something that might be a memory barrier, we
2425 have to stop looking. Even if the MEM is set later in
2426 the function, we still don't want to set it
2427 unconditionally before the barrier. */
2429 && (volatile_insn_p (PATTERN (insn
))
2430 || (CALL_P (insn
) && (!RTL_CONST_CALL_P (insn
)))))
2433 if (memory_must_be_modified_in_insn_p (mem
, insn
))
2435 if (modified_in_p (XEXP (mem
, 0), insn
))
2444 /* Given a simple IF-THEN-JOIN or IF-THEN-ELSE-JOIN block, attempt to convert
2445 it without using conditional execution. Return TRUE if we were successful
2446 at converting the block. */
2449 noce_process_if_block (struct noce_if_info
*if_info
)
2451 basic_block test_bb
= if_info
->test_bb
; /* test block */
2452 basic_block then_bb
= if_info
->then_bb
; /* THEN */
2453 basic_block else_bb
= if_info
->else_bb
; /* ELSE or NULL */
2454 basic_block join_bb
= if_info
->join_bb
; /* JOIN */
2455 rtx jump
= if_info
->jump
;
2456 rtx cond
= if_info
->cond
;
2459 rtx orig_x
, x
, a
, b
;
2461 /* We're looking for patterns of the form
2463 (1) if (...) x = a; else x = b;
2464 (2) x = b; if (...) x = a;
2465 (3) if (...) x = a; // as if with an initial x = x.
2467 The later patterns require jumps to be more expensive.
2469 ??? For future expansion, look for multiple X in such patterns. */
2471 /* Look for one of the potential sets. */
2472 insn_a
= first_active_insn (then_bb
);
2474 || insn_a
!= last_active_insn (then_bb
, FALSE
)
2475 || (set_a
= single_set (insn_a
)) == NULL_RTX
)
2478 x
= SET_DEST (set_a
);
2479 a
= SET_SRC (set_a
);
2481 /* Look for the other potential set. Make sure we've got equivalent
2483 /* ??? This is overconservative. Storing to two different mems is
2484 as easy as conditionally computing the address. Storing to a
2485 single mem merely requires a scratch memory to use as one of the
2486 destination addresses; often the memory immediately below the
2487 stack pointer is available for this. */
2491 insn_b
= first_active_insn (else_bb
);
2493 || insn_b
!= last_active_insn (else_bb
, FALSE
)
2494 || (set_b
= single_set (insn_b
)) == NULL_RTX
2495 || ! rtx_equal_p (x
, SET_DEST (set_b
)))
2500 insn_b
= prev_nonnote_nondebug_insn (if_info
->cond_earliest
);
2501 /* We're going to be moving the evaluation of B down from above
2502 COND_EARLIEST to JUMP. Make sure the relevant data is still
2505 || BLOCK_FOR_INSN (insn_b
) != BLOCK_FOR_INSN (if_info
->cond_earliest
)
2506 || !NONJUMP_INSN_P (insn_b
)
2507 || (set_b
= single_set (insn_b
)) == NULL_RTX
2508 || ! rtx_equal_p (x
, SET_DEST (set_b
))
2509 || ! noce_operand_ok (SET_SRC (set_b
))
2510 || reg_overlap_mentioned_p (x
, SET_SRC (set_b
))
2511 || modified_between_p (SET_SRC (set_b
), insn_b
, jump
)
2512 /* Avoid extending the lifetime of hard registers on small
2513 register class machines. */
2514 || (REG_P (SET_SRC (set_b
))
2515 && HARD_REGISTER_P (SET_SRC (set_b
))
2516 && targetm
.small_register_classes_for_mode_p
2517 (GET_MODE (SET_SRC (set_b
))))
2518 /* Likewise with X. In particular this can happen when
2519 noce_get_condition looks farther back in the instruction
2520 stream than one might expect. */
2521 || reg_overlap_mentioned_p (x
, cond
)
2522 || reg_overlap_mentioned_p (x
, a
)
2523 || modified_between_p (x
, insn_b
, jump
))
2524 insn_b
= set_b
= NULL_RTX
;
2527 /* If x has side effects then only the if-then-else form is safe to
2528 convert. But even in that case we would need to restore any notes
2529 (such as REG_INC) at then end. That can be tricky if
2530 noce_emit_move_insn expands to more than one insn, so disable the
2531 optimization entirely for now if there are side effects. */
2532 if (side_effects_p (x
))
2535 b
= (set_b
? SET_SRC (set_b
) : x
);
2537 /* Only operate on register destinations, and even then avoid extending
2538 the lifetime of hard registers on small register class machines. */
2541 || (HARD_REGISTER_P (x
)
2542 && targetm
.small_register_classes_for_mode_p (GET_MODE (x
))))
2544 if (GET_MODE (x
) == BLKmode
)
2547 if (GET_CODE (x
) == ZERO_EXTRACT
2548 && (!CONST_INT_P (XEXP (x
, 1))
2549 || !CONST_INT_P (XEXP (x
, 2))))
2552 x
= gen_reg_rtx (GET_MODE (GET_CODE (x
) == STRICT_LOW_PART
2553 ? XEXP (x
, 0) : x
));
2556 /* Don't operate on sources that may trap or are volatile. */
2557 if (! noce_operand_ok (a
) || ! noce_operand_ok (b
))
2561 /* Set up the info block for our subroutines. */
2562 if_info
->insn_a
= insn_a
;
2563 if_info
->insn_b
= insn_b
;
2568 /* Try optimizations in some approximation of a useful order. */
2569 /* ??? Should first look to see if X is live incoming at all. If it
2570 isn't, we don't need anything but an unconditional set. */
2572 /* Look and see if A and B are really the same. Avoid creating silly
2573 cmove constructs that no one will fix up later. */
2574 if (rtx_equal_p (a
, b
))
2576 /* If we have an INSN_B, we don't have to create any new rtl. Just
2577 move the instruction that we already have. If we don't have an
2578 INSN_B, that means that A == X, and we've got a noop move. In
2579 that case don't do anything and let the code below delete INSN_A. */
2580 if (insn_b
&& else_bb
)
2584 if (else_bb
&& insn_b
== BB_END (else_bb
))
2585 BB_END (else_bb
) = PREV_INSN (insn_b
);
2586 reorder_insns (insn_b
, insn_b
, PREV_INSN (jump
));
2588 /* If there was a REG_EQUAL note, delete it since it may have been
2589 true due to this insn being after a jump. */
2590 if ((note
= find_reg_note (insn_b
, REG_EQUAL
, NULL_RTX
)) != 0)
2591 remove_note (insn_b
, note
);
2595 /* If we have "x = b; if (...) x = a;", and x has side-effects, then
2596 x must be executed twice. */
2597 else if (insn_b
&& side_effects_p (orig_x
))
2604 if (!set_b
&& MEM_P (orig_x
))
2606 /* Disallow the "if (...) x = a;" form (implicit "else x = x;")
2607 for optimizations if writing to x may trap or fault,
2608 i.e. it's a memory other than a static var or a stack slot,
2609 is misaligned on strict aligned machines or is read-only. If
2610 x is a read-only memory, then the program is valid only if we
2611 avoid the store into it. If there are stores on both the
2612 THEN and ELSE arms, then we can go ahead with the conversion;
2613 either the program is broken, or the condition is always
2614 false such that the other memory is selected. */
2615 if (noce_mem_write_may_trap_or_fault_p (orig_x
))
2618 /* Avoid store speculation: given "if (...) x = a" where x is a
2619 MEM, we only want to do the store if x is always set
2620 somewhere in the function. This avoids cases like
2621 if (pthread_mutex_trylock(mutex))
2623 where we only want global_variable to be changed if the mutex
2624 is held. FIXME: This should ideally be expressed directly in
2626 if (!noce_can_store_speculate_p (test_bb
, orig_x
))
2630 if (noce_try_move (if_info
))
2632 if (noce_try_store_flag (if_info
))
2634 if (noce_try_bitop (if_info
))
2636 if (noce_try_minmax (if_info
))
2638 if (noce_try_abs (if_info
))
2640 if (HAVE_conditional_move
2641 && noce_try_cmove (if_info
))
2643 if (! targetm
.have_conditional_execution ())
2645 if (noce_try_store_flag_constants (if_info
))
2647 if (noce_try_addcc (if_info
))
2649 if (noce_try_store_flag_mask (if_info
))
2651 if (HAVE_conditional_move
2652 && noce_try_cmove_arith (if_info
))
2654 if (noce_try_sign_mask (if_info
))
2658 if (!else_bb
&& set_b
)
2660 insn_b
= set_b
= NULL_RTX
;
2669 /* If we used a temporary, fix it up now. */
2675 noce_emit_move_insn (orig_x
, x
);
2677 set_used_flags (orig_x
);
2678 unshare_all_rtl_in_chain (seq
);
2681 emit_insn_before_setloc (seq
, BB_END (test_bb
), INSN_LOCATION (insn_a
));
2684 /* The original THEN and ELSE blocks may now be removed. The test block
2685 must now jump to the join block. If the test block and the join block
2686 can be merged, do so. */
2689 delete_basic_block (else_bb
);
2693 remove_edge (find_edge (test_bb
, join_bb
));
2695 remove_edge (find_edge (then_bb
, join_bb
));
2696 redirect_edge_and_branch_force (single_succ_edge (test_bb
), join_bb
);
2697 delete_basic_block (then_bb
);
2700 if (can_merge_blocks_p (test_bb
, join_bb
))
2702 merge_blocks (test_bb
, join_bb
);
2706 num_updated_if_blocks
++;
2710 /* Check whether a block is suitable for conditional move conversion.
2711 Every insn must be a simple set of a register to a constant or a
2712 register. For each assignment, store the value in the pointer map
2713 VALS, keyed indexed by register pointer, then store the register
2714 pointer in REGS. COND is the condition we will test. */
2717 check_cond_move_block (basic_block bb
,
2718 struct pointer_map_t
*vals
,
2724 /* We can only handle simple jumps at the end of the basic block.
2725 It is almost impossible to update the CFG otherwise. */
2727 if (JUMP_P (insn
) && !onlyjump_p (insn
))
2730 FOR_BB_INSNS (bb
, insn
)
2735 if (!NONDEBUG_INSN_P (insn
) || JUMP_P (insn
))
2737 set
= single_set (insn
);
2741 dest
= SET_DEST (set
);
2742 src
= SET_SRC (set
);
2744 || (HARD_REGISTER_P (dest
)
2745 && targetm
.small_register_classes_for_mode_p (GET_MODE (dest
))))
2748 if (!CONSTANT_P (src
) && !register_operand (src
, VOIDmode
))
2751 if (side_effects_p (src
) || side_effects_p (dest
))
2754 if (may_trap_p (src
) || may_trap_p (dest
))
2757 /* Don't try to handle this if the source register was
2758 modified earlier in the block. */
2760 && pointer_map_contains (vals
, src
))
2761 || (GET_CODE (src
) == SUBREG
&& REG_P (SUBREG_REG (src
))
2762 && pointer_map_contains (vals
, SUBREG_REG (src
))))
2765 /* Don't try to handle this if the destination register was
2766 modified earlier in the block. */
2767 if (pointer_map_contains (vals
, dest
))
2770 /* Don't try to handle this if the condition uses the
2771 destination register. */
2772 if (reg_overlap_mentioned_p (dest
, cond
))
2775 /* Don't try to handle this if the source register is modified
2776 later in the block. */
2777 if (!CONSTANT_P (src
)
2778 && modified_between_p (src
, insn
, NEXT_INSN (BB_END (bb
))))
2781 slot
= pointer_map_insert (vals
, (void *) dest
);
2782 *slot
= (void *) src
;
2784 regs
->safe_push (dest
);
2790 /* Given a basic block BB suitable for conditional move conversion,
2791 a condition COND, and pointer maps THEN_VALS and ELSE_VALS containing
2792 the register values depending on COND, emit the insns in the block as
2793 conditional moves. If ELSE_BLOCK is true, THEN_BB was already
2794 processed. The caller has started a sequence for the conversion.
2795 Return true if successful, false if something goes wrong. */
2798 cond_move_convert_if_block (struct noce_if_info
*if_infop
,
2799 basic_block bb
, rtx cond
,
2800 struct pointer_map_t
*then_vals
,
2801 struct pointer_map_t
*else_vals
,
2805 rtx insn
, cond_arg0
, cond_arg1
;
2807 code
= GET_CODE (cond
);
2808 cond_arg0
= XEXP (cond
, 0);
2809 cond_arg1
= XEXP (cond
, 1);
2811 FOR_BB_INSNS (bb
, insn
)
2813 rtx set
, target
, dest
, t
, e
;
2814 void **then_slot
, **else_slot
;
2816 /* ??? Maybe emit conditional debug insn? */
2817 if (!NONDEBUG_INSN_P (insn
) || JUMP_P (insn
))
2819 set
= single_set (insn
);
2820 gcc_assert (set
&& REG_P (SET_DEST (set
)));
2822 dest
= SET_DEST (set
);
2824 then_slot
= pointer_map_contains (then_vals
, dest
);
2825 else_slot
= pointer_map_contains (else_vals
, dest
);
2826 t
= then_slot
? (rtx
) *then_slot
: NULL_RTX
;
2827 e
= else_slot
? (rtx
) *else_slot
: NULL_RTX
;
2831 /* If this register was set in the then block, we already
2832 handled this case there. */
2845 target
= noce_emit_cmove (if_infop
, dest
, code
, cond_arg0
, cond_arg1
,
2851 noce_emit_move_insn (dest
, target
);
2857 /* Given a simple IF-THEN-JOIN or IF-THEN-ELSE-JOIN block, attempt to convert
2858 it using only conditional moves. Return TRUE if we were successful at
2859 converting the block. */
2862 cond_move_process_if_block (struct noce_if_info
*if_info
)
2864 basic_block test_bb
= if_info
->test_bb
;
2865 basic_block then_bb
= if_info
->then_bb
;
2866 basic_block else_bb
= if_info
->else_bb
;
2867 basic_block join_bb
= if_info
->join_bb
;
2868 rtx jump
= if_info
->jump
;
2869 rtx cond
= if_info
->cond
;
2873 struct pointer_map_t
*then_vals
;
2874 struct pointer_map_t
*else_vals
;
2875 vec
<rtx
> then_regs
= vNULL
;
2876 vec
<rtx
> else_regs
= vNULL
;
2878 int success_p
= FALSE
;
2880 /* Build a mapping for each block to the value used for each
2882 then_vals
= pointer_map_create ();
2883 else_vals
= pointer_map_create ();
2885 /* Make sure the blocks are suitable. */
2886 if (!check_cond_move_block (then_bb
, then_vals
, &then_regs
, cond
)
2888 && !check_cond_move_block (else_bb
, else_vals
, &else_regs
, cond
)))
2891 /* Make sure the blocks can be used together. If the same register
2892 is set in both blocks, and is not set to a constant in both
2893 cases, then both blocks must set it to the same register. We
2894 have already verified that if it is set to a register, that the
2895 source register does not change after the assignment. Also count
2896 the number of registers set in only one of the blocks. */
2898 FOR_EACH_VEC_ELT (then_regs
, i
, reg
)
2900 void **then_slot
= pointer_map_contains (then_vals
, reg
);
2901 void **else_slot
= pointer_map_contains (else_vals
, reg
);
2903 gcc_checking_assert (then_slot
);
2908 rtx then_val
= (rtx
) *then_slot
;
2909 rtx else_val
= (rtx
) *else_slot
;
2910 if (!CONSTANT_P (then_val
) && !CONSTANT_P (else_val
)
2911 && !rtx_equal_p (then_val
, else_val
))
2916 /* Finish off c for MAX_CONDITIONAL_EXECUTE. */
2917 FOR_EACH_VEC_ELT (else_regs
, i
, reg
)
2919 gcc_checking_assert (pointer_map_contains (else_vals
, reg
));
2920 if (!pointer_map_contains (then_vals
, reg
))
2924 /* Make sure it is reasonable to convert this block. What matters
2925 is the number of assignments currently made in only one of the
2926 branches, since if we convert we are going to always execute
2928 if (c
> MAX_CONDITIONAL_EXECUTE
)
2931 /* Try to emit the conditional moves. First do the then block,
2932 then do anything left in the else blocks. */
2934 if (!cond_move_convert_if_block (if_info
, then_bb
, cond
,
2935 then_vals
, else_vals
, false)
2937 && !cond_move_convert_if_block (if_info
, else_bb
, cond
,
2938 then_vals
, else_vals
, true)))
2943 seq
= end_ifcvt_sequence (if_info
);
2947 loc_insn
= first_active_insn (then_bb
);
2950 loc_insn
= first_active_insn (else_bb
);
2951 gcc_assert (loc_insn
);
2953 emit_insn_before_setloc (seq
, jump
, INSN_LOCATION (loc_insn
));
2957 delete_basic_block (else_bb
);
2961 remove_edge (find_edge (test_bb
, join_bb
));
2963 remove_edge (find_edge (then_bb
, join_bb
));
2964 redirect_edge_and_branch_force (single_succ_edge (test_bb
), join_bb
);
2965 delete_basic_block (then_bb
);
2968 if (can_merge_blocks_p (test_bb
, join_bb
))
2970 merge_blocks (test_bb
, join_bb
);
2974 num_updated_if_blocks
++;
2979 pointer_map_destroy (then_vals
);
2980 pointer_map_destroy (else_vals
);
2981 then_regs
.release ();
2982 else_regs
.release ();
2987 /* Determine if a given basic block heads a simple IF-THEN-JOIN or an
2988 IF-THEN-ELSE-JOIN block.
2990 If so, we'll try to convert the insns to not require the branch,
2991 using only transformations that do not require conditional execution.
2993 Return TRUE if we were successful at converting the block. */
2996 noce_find_if_block (basic_block test_bb
, edge then_edge
, edge else_edge
,
2999 basic_block then_bb
, else_bb
, join_bb
;
3000 bool then_else_reversed
= false;
3003 struct noce_if_info if_info
;
3005 /* We only ever should get here before reload. */
3006 gcc_assert (!reload_completed
);
3008 /* Recognize an IF-THEN-ELSE-JOIN block. */
3009 if (single_pred_p (then_edge
->dest
)
3010 && single_succ_p (then_edge
->dest
)
3011 && single_pred_p (else_edge
->dest
)
3012 && single_succ_p (else_edge
->dest
)
3013 && single_succ (then_edge
->dest
) == single_succ (else_edge
->dest
))
3015 then_bb
= then_edge
->dest
;
3016 else_bb
= else_edge
->dest
;
3017 join_bb
= single_succ (then_bb
);
3019 /* Recognize an IF-THEN-JOIN block. */
3020 else if (single_pred_p (then_edge
->dest
)
3021 && single_succ_p (then_edge
->dest
)
3022 && single_succ (then_edge
->dest
) == else_edge
->dest
)
3024 then_bb
= then_edge
->dest
;
3025 else_bb
= NULL_BLOCK
;
3026 join_bb
= else_edge
->dest
;
3028 /* Recognize an IF-ELSE-JOIN block. We can have those because the order
3029 of basic blocks in cfglayout mode does not matter, so the fallthrough
3030 edge can go to any basic block (and not just to bb->next_bb, like in
3032 else if (single_pred_p (else_edge
->dest
)
3033 && single_succ_p (else_edge
->dest
)
3034 && single_succ (else_edge
->dest
) == then_edge
->dest
)
3036 /* The noce transformations do not apply to IF-ELSE-JOIN blocks.
3037 To make this work, we have to invert the THEN and ELSE blocks
3038 and reverse the jump condition. */
3039 then_bb
= else_edge
->dest
;
3040 else_bb
= NULL_BLOCK
;
3041 join_bb
= single_succ (then_bb
);
3042 then_else_reversed
= true;
3045 /* Not a form we can handle. */
3048 /* The edges of the THEN and ELSE blocks cannot have complex edges. */
3049 if (single_succ_edge (then_bb
)->flags
& EDGE_COMPLEX
)
3052 && single_succ_edge (else_bb
)->flags
& EDGE_COMPLEX
)
3055 num_possible_if_blocks
++;
3060 "\nIF-THEN%s-JOIN block found, pass %d, test %d, then %d",
3061 (else_bb
) ? "-ELSE" : "",
3062 pass
, test_bb
->index
, then_bb
->index
);
3065 fprintf (dump_file
, ", else %d", else_bb
->index
);
3067 fprintf (dump_file
, ", join %d\n", join_bb
->index
);
3070 /* If the conditional jump is more than just a conditional
3071 jump, then we can not do if-conversion on this block. */
3072 jump
= BB_END (test_bb
);
3073 if (! onlyjump_p (jump
))
3076 /* If this is not a standard conditional jump, we can't parse it. */
3077 cond
= noce_get_condition (jump
, &cond_earliest
, then_else_reversed
);
3081 /* We must be comparing objects whose modes imply the size. */
3082 if (GET_MODE (XEXP (cond
, 0)) == BLKmode
)
3085 /* Initialize an IF_INFO struct to pass around. */
3086 memset (&if_info
, 0, sizeof if_info
);
3087 if_info
.test_bb
= test_bb
;
3088 if_info
.then_bb
= then_bb
;
3089 if_info
.else_bb
= else_bb
;
3090 if_info
.join_bb
= join_bb
;
3091 if_info
.cond
= cond
;
3092 if_info
.cond_earliest
= cond_earliest
;
3093 if_info
.jump
= jump
;
3094 if_info
.then_else_reversed
= then_else_reversed
;
3095 if_info
.branch_cost
= BRANCH_COST (optimize_bb_for_speed_p (test_bb
),
3096 predictable_edge_p (then_edge
));
3098 /* Do the real work. */
3100 if (noce_process_if_block (&if_info
))
3103 if (HAVE_conditional_move
3104 && cond_move_process_if_block (&if_info
))
3111 /* Merge the blocks and mark for local life update. */
3114 merge_if_block (struct ce_if_block
* ce_info
)
3116 basic_block test_bb
= ce_info
->test_bb
; /* last test block */
3117 basic_block then_bb
= ce_info
->then_bb
; /* THEN */
3118 basic_block else_bb
= ce_info
->else_bb
; /* ELSE or NULL */
3119 basic_block join_bb
= ce_info
->join_bb
; /* join block */
3120 basic_block combo_bb
;
3122 /* All block merging is done into the lower block numbers. */
3125 df_set_bb_dirty (test_bb
);
3127 /* Merge any basic blocks to handle && and || subtests. Each of
3128 the blocks are on the fallthru path from the predecessor block. */
3129 if (ce_info
->num_multiple_test_blocks
> 0)
3131 basic_block bb
= test_bb
;
3132 basic_block last_test_bb
= ce_info
->last_test_bb
;
3133 basic_block fallthru
= block_fallthru (bb
);
3138 fallthru
= block_fallthru (bb
);
3139 merge_blocks (combo_bb
, bb
);
3142 while (bb
!= last_test_bb
);
3145 /* Merge TEST block into THEN block. Normally the THEN block won't have a
3146 label, but it might if there were || tests. That label's count should be
3147 zero, and it normally should be removed. */
3151 merge_blocks (combo_bb
, then_bb
);
3155 /* The ELSE block, if it existed, had a label. That label count
3156 will almost always be zero, but odd things can happen when labels
3157 get their addresses taken. */
3160 merge_blocks (combo_bb
, else_bb
);
3164 /* If there was no join block reported, that means it was not adjacent
3165 to the others, and so we cannot merge them. */
3169 rtx last
= BB_END (combo_bb
);
3171 /* The outgoing edge for the current COMBO block should already
3172 be correct. Verify this. */
3173 if (EDGE_COUNT (combo_bb
->succs
) == 0)
3174 gcc_assert (find_reg_note (last
, REG_NORETURN
, NULL
)
3175 || (NONJUMP_INSN_P (last
)
3176 && GET_CODE (PATTERN (last
)) == TRAP_IF
3177 && (TRAP_CONDITION (PATTERN (last
))
3178 == const_true_rtx
)));
3181 /* There should still be something at the end of the THEN or ELSE
3182 blocks taking us to our final destination. */
3183 gcc_assert (JUMP_P (last
)
3184 || (EDGE_SUCC (combo_bb
, 0)->dest
== EXIT_BLOCK_PTR
3186 && SIBLING_CALL_P (last
))
3187 || ((EDGE_SUCC (combo_bb
, 0)->flags
& EDGE_EH
)
3188 && can_throw_internal (last
)));
3191 /* The JOIN block may have had quite a number of other predecessors too.
3192 Since we've already merged the TEST, THEN and ELSE blocks, we should
3193 have only one remaining edge from our if-then-else diamond. If there
3194 is more than one remaining edge, it must come from elsewhere. There
3195 may be zero incoming edges if the THEN block didn't actually join
3196 back up (as with a call to a non-return function). */
3197 else if (EDGE_COUNT (join_bb
->preds
) < 2
3198 && join_bb
!= EXIT_BLOCK_PTR
)
3200 /* We can merge the JOIN cleanly and update the dataflow try
3201 again on this pass.*/
3202 merge_blocks (combo_bb
, join_bb
);
3207 /* We cannot merge the JOIN. */
3209 /* The outgoing edge for the current COMBO block should already
3210 be correct. Verify this. */
3211 gcc_assert (single_succ_p (combo_bb
)
3212 && single_succ (combo_bb
) == join_bb
);
3214 /* Remove the jump and cruft from the end of the COMBO block. */
3215 if (join_bb
!= EXIT_BLOCK_PTR
)
3216 tidy_fallthru_edge (single_succ_edge (combo_bb
));
3219 num_updated_if_blocks
++;
3222 /* Find a block ending in a simple IF condition and try to transform it
3223 in some way. When converting a multi-block condition, put the new code
3224 in the first such block and delete the rest. Return a pointer to this
3225 first block if some transformation was done. Return NULL otherwise. */
3228 find_if_header (basic_block test_bb
, int pass
)
3230 ce_if_block_t ce_info
;
3234 /* The kind of block we're looking for has exactly two successors. */
3235 if (EDGE_COUNT (test_bb
->succs
) != 2)
3238 then_edge
= EDGE_SUCC (test_bb
, 0);
3239 else_edge
= EDGE_SUCC (test_bb
, 1);
3241 if (df_get_bb_dirty (then_edge
->dest
))
3243 if (df_get_bb_dirty (else_edge
->dest
))
3246 /* Neither edge should be abnormal. */
3247 if ((then_edge
->flags
& EDGE_COMPLEX
)
3248 || (else_edge
->flags
& EDGE_COMPLEX
))
3251 /* Nor exit the loop. */
3252 if ((then_edge
->flags
& EDGE_LOOP_EXIT
)
3253 || (else_edge
->flags
& EDGE_LOOP_EXIT
))
3256 /* The THEN edge is canonically the one that falls through. */
3257 if (then_edge
->flags
& EDGE_FALLTHRU
)
3259 else if (else_edge
->flags
& EDGE_FALLTHRU
)
3262 else_edge
= then_edge
;
3266 /* Otherwise this must be a multiway branch of some sort. */
3269 memset (&ce_info
, 0, sizeof (ce_info
));
3270 ce_info
.test_bb
= test_bb
;
3271 ce_info
.then_bb
= then_edge
->dest
;
3272 ce_info
.else_bb
= else_edge
->dest
;
3273 ce_info
.pass
= pass
;
3275 #ifdef IFCVT_MACHDEP_INIT
3276 IFCVT_MACHDEP_INIT (&ce_info
);
3279 if (!reload_completed
3280 && noce_find_if_block (test_bb
, then_edge
, else_edge
, pass
))
3283 if (reload_completed
3284 && targetm
.have_conditional_execution ()
3285 && cond_exec_find_if_block (&ce_info
))
3289 && optab_handler (ctrap_optab
, word_mode
) != CODE_FOR_nothing
3290 && find_cond_trap (test_bb
, then_edge
, else_edge
))
3293 if (dom_info_state (CDI_POST_DOMINATORS
) >= DOM_NO_FAST_QUERY
3294 && (reload_completed
|| !targetm
.have_conditional_execution ()))
3296 if (find_if_case_1 (test_bb
, then_edge
, else_edge
))
3298 if (find_if_case_2 (test_bb
, then_edge
, else_edge
))
3306 fprintf (dump_file
, "Conversion succeeded on pass %d.\n", pass
);
3307 /* Set this so we continue looking. */
3308 cond_exec_changed_p
= TRUE
;
3309 return ce_info
.test_bb
;
3312 /* Return true if a block has two edges, one of which falls through to the next
3313 block, and the other jumps to a specific block, so that we can tell if the
3314 block is part of an && test or an || test. Returns either -1 or the number
3315 of non-note, non-jump, non-USE/CLOBBER insns in the block. */
3318 block_jumps_and_fallthru_p (basic_block cur_bb
, basic_block target_bb
)
3321 int fallthru_p
= FALSE
;
3328 if (!cur_bb
|| !target_bb
)
3331 /* If no edges, obviously it doesn't jump or fallthru. */
3332 if (EDGE_COUNT (cur_bb
->succs
) == 0)
3335 FOR_EACH_EDGE (cur_edge
, ei
, cur_bb
->succs
)
3337 if (cur_edge
->flags
& EDGE_COMPLEX
)
3338 /* Anything complex isn't what we want. */
3341 else if (cur_edge
->flags
& EDGE_FALLTHRU
)
3344 else if (cur_edge
->dest
== target_bb
)
3351 if ((jump_p
& fallthru_p
) == 0)
3354 /* Don't allow calls in the block, since this is used to group && and ||
3355 together for conditional execution support. ??? we should support
3356 conditional execution support across calls for IA-64 some day, but
3357 for now it makes the code simpler. */
3358 end
= BB_END (cur_bb
);
3359 insn
= BB_HEAD (cur_bb
);
3361 while (insn
!= NULL_RTX
)
3368 && !DEBUG_INSN_P (insn
)
3369 && GET_CODE (PATTERN (insn
)) != USE
3370 && GET_CODE (PATTERN (insn
)) != CLOBBER
)
3376 insn
= NEXT_INSN (insn
);
3382 /* Determine if a given basic block heads a simple IF-THEN or IF-THEN-ELSE
3383 block. If so, we'll try to convert the insns to not require the branch.
3384 Return TRUE if we were successful at converting the block. */
3387 cond_exec_find_if_block (struct ce_if_block
* ce_info
)
3389 basic_block test_bb
= ce_info
->test_bb
;
3390 basic_block then_bb
= ce_info
->then_bb
;
3391 basic_block else_bb
= ce_info
->else_bb
;
3392 basic_block join_bb
= NULL_BLOCK
;
3397 ce_info
->last_test_bb
= test_bb
;
3399 /* We only ever should get here after reload,
3400 and if we have conditional execution. */
3401 gcc_assert (reload_completed
&& targetm
.have_conditional_execution ());
3403 /* Discover if any fall through predecessors of the current test basic block
3404 were && tests (which jump to the else block) or || tests (which jump to
3406 if (single_pred_p (test_bb
)
3407 && single_pred_edge (test_bb
)->flags
== EDGE_FALLTHRU
)
3409 basic_block bb
= single_pred (test_bb
);
3410 basic_block target_bb
;
3411 int max_insns
= MAX_CONDITIONAL_EXECUTE
;
3414 /* Determine if the preceding block is an && or || block. */
3415 if ((n_insns
= block_jumps_and_fallthru_p (bb
, else_bb
)) >= 0)
3417 ce_info
->and_and_p
= TRUE
;
3418 target_bb
= else_bb
;
3420 else if ((n_insns
= block_jumps_and_fallthru_p (bb
, then_bb
)) >= 0)
3422 ce_info
->and_and_p
= FALSE
;
3423 target_bb
= then_bb
;
3426 target_bb
= NULL_BLOCK
;
3428 if (target_bb
&& n_insns
<= max_insns
)
3430 int total_insns
= 0;
3433 ce_info
->last_test_bb
= test_bb
;
3435 /* Found at least one && or || block, look for more. */
3438 ce_info
->test_bb
= test_bb
= bb
;
3439 total_insns
+= n_insns
;
3442 if (!single_pred_p (bb
))
3445 bb
= single_pred (bb
);
3446 n_insns
= block_jumps_and_fallthru_p (bb
, target_bb
);
3448 while (n_insns
>= 0 && (total_insns
+ n_insns
) <= max_insns
);
3450 ce_info
->num_multiple_test_blocks
= blocks
;
3451 ce_info
->num_multiple_test_insns
= total_insns
;
3453 if (ce_info
->and_and_p
)
3454 ce_info
->num_and_and_blocks
= blocks
;
3456 ce_info
->num_or_or_blocks
= blocks
;
3460 /* The THEN block of an IF-THEN combo must have exactly one predecessor,
3461 other than any || blocks which jump to the THEN block. */
3462 if ((EDGE_COUNT (then_bb
->preds
) - ce_info
->num_or_or_blocks
) != 1)
3465 /* The edges of the THEN and ELSE blocks cannot have complex edges. */
3466 FOR_EACH_EDGE (cur_edge
, ei
, then_bb
->preds
)
3468 if (cur_edge
->flags
& EDGE_COMPLEX
)
3472 FOR_EACH_EDGE (cur_edge
, ei
, else_bb
->preds
)
3474 if (cur_edge
->flags
& EDGE_COMPLEX
)
3478 /* The THEN block of an IF-THEN combo must have zero or one successors. */
3479 if (EDGE_COUNT (then_bb
->succs
) > 0
3480 && (!single_succ_p (then_bb
)
3481 || (single_succ_edge (then_bb
)->flags
& EDGE_COMPLEX
)
3482 || (epilogue_completed
3483 && tablejump_p (BB_END (then_bb
), NULL
, NULL
))))
3486 /* If the THEN block has no successors, conditional execution can still
3487 make a conditional call. Don't do this unless the ELSE block has
3488 only one incoming edge -- the CFG manipulation is too ugly otherwise.
3489 Check for the last insn of the THEN block being an indirect jump, which
3490 is listed as not having any successors, but confuses the rest of the CE
3491 code processing. ??? we should fix this in the future. */
3492 if (EDGE_COUNT (then_bb
->succs
) == 0)
3494 if (single_pred_p (else_bb
) && else_bb
!= EXIT_BLOCK_PTR
)
3496 rtx last_insn
= BB_END (then_bb
);
3499 && NOTE_P (last_insn
)
3500 && last_insn
!= BB_HEAD (then_bb
))
3501 last_insn
= PREV_INSN (last_insn
);
3504 && JUMP_P (last_insn
)
3505 && ! simplejump_p (last_insn
))
3509 else_bb
= NULL_BLOCK
;
3515 /* If the THEN block's successor is the other edge out of the TEST block,
3516 then we have an IF-THEN combo without an ELSE. */
3517 else if (single_succ (then_bb
) == else_bb
)
3520 else_bb
= NULL_BLOCK
;
3523 /* If the THEN and ELSE block meet in a subsequent block, and the ELSE
3524 has exactly one predecessor and one successor, and the outgoing edge
3525 is not complex, then we have an IF-THEN-ELSE combo. */
3526 else if (single_succ_p (else_bb
)
3527 && single_succ (then_bb
) == single_succ (else_bb
)
3528 && single_pred_p (else_bb
)
3529 && !(single_succ_edge (else_bb
)->flags
& EDGE_COMPLEX
)
3530 && !(epilogue_completed
3531 && tablejump_p (BB_END (else_bb
), NULL
, NULL
)))
3532 join_bb
= single_succ (else_bb
);
3534 /* Otherwise it is not an IF-THEN or IF-THEN-ELSE combination. */
3538 num_possible_if_blocks
++;
3543 "\nIF-THEN%s block found, pass %d, start block %d "
3544 "[insn %d], then %d [%d]",
3545 (else_bb
) ? "-ELSE" : "",
3548 BB_HEAD (test_bb
) ? (int)INSN_UID (BB_HEAD (test_bb
)) : -1,
3550 BB_HEAD (then_bb
) ? (int)INSN_UID (BB_HEAD (then_bb
)) : -1);
3553 fprintf (dump_file
, ", else %d [%d]",
3555 BB_HEAD (else_bb
) ? (int)INSN_UID (BB_HEAD (else_bb
)) : -1);
3557 fprintf (dump_file
, ", join %d [%d]",
3559 BB_HEAD (join_bb
) ? (int)INSN_UID (BB_HEAD (join_bb
)) : -1);
3561 if (ce_info
->num_multiple_test_blocks
> 0)
3562 fprintf (dump_file
, ", %d %s block%s last test %d [%d]",
3563 ce_info
->num_multiple_test_blocks
,
3564 (ce_info
->and_and_p
) ? "&&" : "||",
3565 (ce_info
->num_multiple_test_blocks
== 1) ? "" : "s",
3566 ce_info
->last_test_bb
->index
,
3567 ((BB_HEAD (ce_info
->last_test_bb
))
3568 ? (int)INSN_UID (BB_HEAD (ce_info
->last_test_bb
))
3571 fputc ('\n', dump_file
);
3574 /* Make sure IF, THEN, and ELSE, blocks are adjacent. Actually, we get the
3575 first condition for free, since we've already asserted that there's a
3576 fallthru edge from IF to THEN. Likewise for the && and || blocks, since
3577 we checked the FALLTHRU flag, those are already adjacent to the last IF
3579 /* ??? As an enhancement, move the ELSE block. Have to deal with
3580 BLOCK notes, if by no other means than backing out the merge if they
3581 exist. Sticky enough I don't want to think about it now. */
3583 if (else_bb
&& (next
= next
->next_bb
) != else_bb
)
3585 if ((next
= next
->next_bb
) != join_bb
&& join_bb
!= EXIT_BLOCK_PTR
)
3593 /* Do the real work. */
3595 ce_info
->else_bb
= else_bb
;
3596 ce_info
->join_bb
= join_bb
;
3598 /* If we have && and || tests, try to first handle combining the && and ||
3599 tests into the conditional code, and if that fails, go back and handle
3600 it without the && and ||, which at present handles the && case if there
3601 was no ELSE block. */
3602 if (cond_exec_process_if_block (ce_info
, TRUE
))
3605 if (ce_info
->num_multiple_test_blocks
)
3609 if (cond_exec_process_if_block (ce_info
, FALSE
))
3616 /* Convert a branch over a trap, or a branch
3617 to a trap, into a conditional trap. */
3620 find_cond_trap (basic_block test_bb
, edge then_edge
, edge else_edge
)
3622 basic_block then_bb
= then_edge
->dest
;
3623 basic_block else_bb
= else_edge
->dest
;
3624 basic_block other_bb
, trap_bb
;
3625 rtx trap
, jump
, cond
, cond_earliest
, seq
;
3628 /* Locate the block with the trap instruction. */
3629 /* ??? While we look for no successors, we really ought to allow
3630 EH successors. Need to fix merge_if_block for that to work. */
3631 if ((trap
= block_has_only_trap (then_bb
)) != NULL
)
3632 trap_bb
= then_bb
, other_bb
= else_bb
;
3633 else if ((trap
= block_has_only_trap (else_bb
)) != NULL
)
3634 trap_bb
= else_bb
, other_bb
= then_bb
;
3640 fprintf (dump_file
, "\nTRAP-IF block found, start %d, trap %d\n",
3641 test_bb
->index
, trap_bb
->index
);
3644 /* If this is not a standard conditional jump, we can't parse it. */
3645 jump
= BB_END (test_bb
);
3646 cond
= noce_get_condition (jump
, &cond_earliest
, false);
3650 /* If the conditional jump is more than just a conditional jump, then
3651 we can not do if-conversion on this block. */
3652 if (! onlyjump_p (jump
))
3655 /* We must be comparing objects whose modes imply the size. */
3656 if (GET_MODE (XEXP (cond
, 0)) == BLKmode
)
3659 /* Reverse the comparison code, if necessary. */
3660 code
= GET_CODE (cond
);
3661 if (then_bb
== trap_bb
)
3663 code
= reversed_comparison_code (cond
, jump
);
3664 if (code
== UNKNOWN
)
3668 /* Attempt to generate the conditional trap. */
3669 seq
= gen_cond_trap (code
, copy_rtx (XEXP (cond
, 0)),
3670 copy_rtx (XEXP (cond
, 1)),
3671 TRAP_CODE (PATTERN (trap
)));
3675 /* Emit the new insns before cond_earliest. */
3676 emit_insn_before_setloc (seq
, cond_earliest
, INSN_LOCATION (trap
));
3678 /* Delete the trap block if possible. */
3679 remove_edge (trap_bb
== then_bb
? then_edge
: else_edge
);
3680 df_set_bb_dirty (test_bb
);
3681 df_set_bb_dirty (then_bb
);
3682 df_set_bb_dirty (else_bb
);
3684 if (EDGE_COUNT (trap_bb
->preds
) == 0)
3686 delete_basic_block (trap_bb
);
3690 /* Wire together the blocks again. */
3691 if (current_ir_type () == IR_RTL_CFGLAYOUT
)
3692 single_succ_edge (test_bb
)->flags
|= EDGE_FALLTHRU
;
3697 lab
= JUMP_LABEL (jump
);
3698 newjump
= emit_jump_insn_after (gen_jump (lab
), jump
);
3699 LABEL_NUSES (lab
) += 1;
3700 JUMP_LABEL (newjump
) = lab
;
3701 emit_barrier_after (newjump
);
3705 if (can_merge_blocks_p (test_bb
, other_bb
))
3707 merge_blocks (test_bb
, other_bb
);
3711 num_updated_if_blocks
++;
3715 /* Subroutine of find_cond_trap: if BB contains only a trap insn,
3719 block_has_only_trap (basic_block bb
)
3723 /* We're not the exit block. */
3724 if (bb
== EXIT_BLOCK_PTR
)
3727 /* The block must have no successors. */
3728 if (EDGE_COUNT (bb
->succs
) > 0)
3731 /* The only instruction in the THEN block must be the trap. */
3732 trap
= first_active_insn (bb
);
3733 if (! (trap
== BB_END (bb
)
3734 && GET_CODE (PATTERN (trap
)) == TRAP_IF
3735 && TRAP_CONDITION (PATTERN (trap
)) == const_true_rtx
))
3741 /* Look for IF-THEN-ELSE cases in which one of THEN or ELSE is
3742 transformable, but not necessarily the other. There need be no
3745 Return TRUE if we were successful at converting the block.
3747 Cases we'd like to look at:
3750 if (test) goto over; // x not live
3758 if (! test) goto label;
3761 if (test) goto E; // x not live
3775 (3) // This one's really only interesting for targets that can do
3776 // multiway branching, e.g. IA-64 BBB bundles. For other targets
3777 // it results in multiple branches on a cache line, which often
3778 // does not sit well with predictors.
3780 if (test1) goto E; // predicted not taken
3796 (A) Don't do (2) if the branch is predicted against the block we're
3797 eliminating. Do it anyway if we can eliminate a branch; this requires
3798 that the sole successor of the eliminated block postdominate the other
3801 (B) With CE, on (3) we can steal from both sides of the if, creating
3810 Again, this is most useful if J postdominates.
3812 (C) CE substitutes for helpful life information.
3814 (D) These heuristics need a lot of work. */
3816 /* Tests for case 1 above. */
3819 find_if_case_1 (basic_block test_bb
, edge then_edge
, edge else_edge
)
3821 basic_block then_bb
= then_edge
->dest
;
3822 basic_block else_bb
= else_edge
->dest
;
3824 int then_bb_index
, then_prob
;
3825 rtx else_target
= NULL_RTX
;
3827 /* If we are partitioning hot/cold basic blocks, we don't want to
3828 mess up unconditional or indirect jumps that cross between hot
3831 Basic block partitioning may result in some jumps that appear to
3832 be optimizable (or blocks that appear to be mergeable), but which really
3833 must be left untouched (they are required to make it safely across
3834 partition boundaries). See the comments at the top of
3835 bb-reorder.c:partition_hot_cold_basic_blocks for complete details. */
3837 if ((BB_END (then_bb
)
3838 && find_reg_note (BB_END (then_bb
), REG_CROSSING_JUMP
, NULL_RTX
))
3839 || (BB_END (test_bb
)
3840 && find_reg_note (BB_END (test_bb
), REG_CROSSING_JUMP
, NULL_RTX
))
3841 || (BB_END (else_bb
)
3842 && find_reg_note (BB_END (else_bb
), REG_CROSSING_JUMP
,
3846 /* THEN has one successor. */
3847 if (!single_succ_p (then_bb
))
3850 /* THEN does not fall through, but is not strange either. */
3851 if (single_succ_edge (then_bb
)->flags
& (EDGE_COMPLEX
| EDGE_FALLTHRU
))
3854 /* THEN has one predecessor. */
3855 if (!single_pred_p (then_bb
))
3858 /* THEN must do something. */
3859 if (forwarder_block_p (then_bb
))
3862 num_possible_if_blocks
++;
3865 "\nIF-CASE-1 found, start %d, then %d\n",
3866 test_bb
->index
, then_bb
->index
);
3868 if (then_edge
->probability
)
3869 then_prob
= REG_BR_PROB_BASE
- then_edge
->probability
;
3871 then_prob
= REG_BR_PROB_BASE
/ 2;
3873 /* We're speculating from the THEN path, we want to make sure the cost
3874 of speculation is within reason. */
3875 if (! cheap_bb_rtx_cost_p (then_bb
, then_prob
,
3876 COSTS_N_INSNS (BRANCH_COST (optimize_bb_for_speed_p (then_edge
->src
),
3877 predictable_edge_p (then_edge
)))))
3880 if (else_bb
== EXIT_BLOCK_PTR
)
3882 rtx jump
= BB_END (else_edge
->src
);
3883 gcc_assert (JUMP_P (jump
));
3884 else_target
= JUMP_LABEL (jump
);
3887 /* Registers set are dead, or are predicable. */
3888 if (! dead_or_predicable (test_bb
, then_bb
, else_bb
,
3889 single_succ_edge (then_bb
), 1))
3892 /* Conversion went ok, including moving the insns and fixing up the
3893 jump. Adjust the CFG to match. */
3895 /* We can avoid creating a new basic block if then_bb is immediately
3896 followed by else_bb, i.e. deleting then_bb allows test_bb to fall
3897 through to else_bb. */
3899 if (then_bb
->next_bb
== else_bb
3900 && then_bb
->prev_bb
== test_bb
3901 && else_bb
!= EXIT_BLOCK_PTR
)
3903 redirect_edge_succ (FALLTHRU_EDGE (test_bb
), else_bb
);
3906 else if (else_bb
== EXIT_BLOCK_PTR
)
3907 new_bb
= force_nonfallthru_and_redirect (FALLTHRU_EDGE (test_bb
),
3908 else_bb
, else_target
);
3910 new_bb
= redirect_edge_and_branch_force (FALLTHRU_EDGE (test_bb
),
3913 df_set_bb_dirty (test_bb
);
3914 df_set_bb_dirty (else_bb
);
3916 then_bb_index
= then_bb
->index
;
3917 delete_basic_block (then_bb
);
3919 /* Make rest of code believe that the newly created block is the THEN_BB
3920 block we removed. */
3923 df_bb_replace (then_bb_index
, new_bb
);
3924 /* This should have been done above via force_nonfallthru_and_redirect
3925 (possibly called from redirect_edge_and_branch_force). */
3926 gcc_checking_assert (BB_PARTITION (new_bb
) == BB_PARTITION (test_bb
));
3930 num_updated_if_blocks
++;
3935 /* Test for case 2 above. */
3938 find_if_case_2 (basic_block test_bb
, edge then_edge
, edge else_edge
)
3940 basic_block then_bb
= then_edge
->dest
;
3941 basic_block else_bb
= else_edge
->dest
;
3943 int then_prob
, else_prob
;
3945 /* We do not want to speculate (empty) loop latches. */
3947 && else_bb
->loop_father
->latch
== else_bb
)
3950 /* If we are partitioning hot/cold basic blocks, we don't want to
3951 mess up unconditional or indirect jumps that cross between hot
3954 Basic block partitioning may result in some jumps that appear to
3955 be optimizable (or blocks that appear to be mergeable), but which really
3956 must be left untouched (they are required to make it safely across
3957 partition boundaries). See the comments at the top of
3958 bb-reorder.c:partition_hot_cold_basic_blocks for complete details. */
3960 if ((BB_END (then_bb
)
3961 && find_reg_note (BB_END (then_bb
), REG_CROSSING_JUMP
, NULL_RTX
))
3962 || (BB_END (test_bb
)
3963 && find_reg_note (BB_END (test_bb
), REG_CROSSING_JUMP
, NULL_RTX
))
3964 || (BB_END (else_bb
)
3965 && find_reg_note (BB_END (else_bb
), REG_CROSSING_JUMP
,
3969 /* ELSE has one successor. */
3970 if (!single_succ_p (else_bb
))
3973 else_succ
= single_succ_edge (else_bb
);
3975 /* ELSE outgoing edge is not complex. */
3976 if (else_succ
->flags
& EDGE_COMPLEX
)
3979 /* ELSE has one predecessor. */
3980 if (!single_pred_p (else_bb
))
3983 /* THEN is not EXIT. */
3984 if (then_bb
->index
< NUM_FIXED_BLOCKS
)
3987 if (else_edge
->probability
)
3989 else_prob
= else_edge
->probability
;
3990 then_prob
= REG_BR_PROB_BASE
- else_prob
;
3994 else_prob
= REG_BR_PROB_BASE
/ 2;
3995 then_prob
= REG_BR_PROB_BASE
/ 2;
3998 /* ELSE is predicted or SUCC(ELSE) postdominates THEN. */
3999 if (else_prob
> then_prob
)
4001 else if (else_succ
->dest
->index
< NUM_FIXED_BLOCKS
4002 || dominated_by_p (CDI_POST_DOMINATORS
, then_bb
,
4008 num_possible_if_blocks
++;
4011 "\nIF-CASE-2 found, start %d, else %d\n",
4012 test_bb
->index
, else_bb
->index
);
4014 /* We're speculating from the ELSE path, we want to make sure the cost
4015 of speculation is within reason. */
4016 if (! cheap_bb_rtx_cost_p (else_bb
, else_prob
,
4017 COSTS_N_INSNS (BRANCH_COST (optimize_bb_for_speed_p (else_edge
->src
),
4018 predictable_edge_p (else_edge
)))))
4021 /* Registers set are dead, or are predicable. */
4022 if (! dead_or_predicable (test_bb
, else_bb
, then_bb
, else_succ
, 0))
4025 /* Conversion went ok, including moving the insns and fixing up the
4026 jump. Adjust the CFG to match. */
4028 df_set_bb_dirty (test_bb
);
4029 df_set_bb_dirty (then_bb
);
4030 delete_basic_block (else_bb
);
4033 num_updated_if_blocks
++;
4035 /* ??? We may now fallthru from one of THEN's successors into a join
4036 block. Rerun cleanup_cfg? Examine things manually? Wait? */
4041 /* Used by the code above to perform the actual rtl transformations.
4042 Return TRUE if successful.
4044 TEST_BB is the block containing the conditional branch. MERGE_BB
4045 is the block containing the code to manipulate. DEST_EDGE is an
4046 edge representing a jump to the join block; after the conversion,
4047 TEST_BB should be branching to its destination.
4048 REVERSEP is true if the sense of the branch should be reversed. */
4051 dead_or_predicable (basic_block test_bb
, basic_block merge_bb
,
4052 basic_block other_bb
, edge dest_edge
, int reversep
)
4054 basic_block new_dest
= dest_edge
->dest
;
4055 rtx head
, end
, jump
, earliest
= NULL_RTX
, old_dest
;
4056 bitmap merge_set
= NULL
;
4057 /* Number of pending changes. */
4058 int n_validated_changes
= 0;
4059 rtx new_dest_label
= NULL_RTX
;
4061 jump
= BB_END (test_bb
);
4063 /* Find the extent of the real code in the merge block. */
4064 head
= BB_HEAD (merge_bb
);
4065 end
= BB_END (merge_bb
);
4067 while (DEBUG_INSN_P (end
) && end
!= head
)
4068 end
= PREV_INSN (end
);
4070 /* If merge_bb ends with a tablejump, predicating/moving insn's
4071 into test_bb and then deleting merge_bb will result in the jumptable
4072 that follows merge_bb being removed along with merge_bb and then we
4073 get an unresolved reference to the jumptable. */
4074 if (tablejump_p (end
, NULL
, NULL
))
4078 head
= NEXT_INSN (head
);
4079 while (DEBUG_INSN_P (head
) && head
!= end
)
4080 head
= NEXT_INSN (head
);
4085 head
= end
= NULL_RTX
;
4088 head
= NEXT_INSN (head
);
4089 while (DEBUG_INSN_P (head
) && head
!= end
)
4090 head
= NEXT_INSN (head
);
4097 head
= end
= NULL_RTX
;
4100 end
= PREV_INSN (end
);
4101 while (DEBUG_INSN_P (end
) && end
!= head
)
4102 end
= PREV_INSN (end
);
4105 /* Disable handling dead code by conditional execution if the machine needs
4106 to do anything funny with the tests, etc. */
4107 #ifndef IFCVT_MODIFY_TESTS
4108 if (targetm
.have_conditional_execution ())
4110 /* In the conditional execution case, we have things easy. We know
4111 the condition is reversible. We don't have to check life info
4112 because we're going to conditionally execute the code anyway.
4113 All that's left is making sure the insns involved can actually
4118 cond
= cond_exec_get_condition (jump
);
4122 prob_val
= find_reg_note (jump
, REG_BR_PROB
, NULL_RTX
);
4124 prob_val
= XEXP (prob_val
, 0);
4128 enum rtx_code rev
= reversed_comparison_code (cond
, jump
);
4131 cond
= gen_rtx_fmt_ee (rev
, GET_MODE (cond
), XEXP (cond
, 0),
4134 prob_val
= GEN_INT (REG_BR_PROB_BASE
- INTVAL (prob_val
));
4137 if (cond_exec_process_insns (NULL
, head
, end
, cond
, prob_val
, 0)
4138 && verify_changes (0))
4139 n_validated_changes
= num_validated_changes ();
4147 /* If we allocated new pseudos (e.g. in the conditional move
4148 expander called from noce_emit_cmove), we must resize the
4150 if (max_regno
< max_reg_num ())
4151 max_regno
= max_reg_num ();
4153 /* Try the NCE path if the CE path did not result in any changes. */
4154 if (n_validated_changes
== 0)
4160 /* In the non-conditional execution case, we have to verify that there
4161 are no trapping operations, no calls, no references to memory, and
4162 that any registers modified are dead at the branch site. */
4164 if (!any_condjump_p (jump
))
4167 /* Find the extent of the conditional. */
4168 cond
= noce_get_condition (jump
, &earliest
, false);
4172 live
= BITMAP_ALLOC (®_obstack
);
4173 simulate_backwards_to_point (merge_bb
, live
, end
);
4174 success
= can_move_insns_across (head
, end
, earliest
, jump
,
4176 df_get_live_in (other_bb
), NULL
);
4181 /* Collect the set of registers set in MERGE_BB. */
4182 merge_set
= BITMAP_ALLOC (®_obstack
);
4184 FOR_BB_INSNS (merge_bb
, insn
)
4185 if (NONDEBUG_INSN_P (insn
))
4186 df_simulate_find_defs (insn
, merge_set
);
4188 #ifdef HAVE_simple_return
4189 /* If shrink-wrapping, disable this optimization when test_bb is
4190 the first basic block and merge_bb exits. The idea is to not
4191 move code setting up a return register as that may clobber a
4192 register used to pass function parameters, which then must be
4193 saved in caller-saved regs. A caller-saved reg requires the
4194 prologue, killing a shrink-wrap opportunity. */
4195 if ((flag_shrink_wrap
&& HAVE_simple_return
&& !epilogue_completed
)
4196 && ENTRY_BLOCK_PTR
->next_bb
== test_bb
4197 && single_succ_p (new_dest
)
4198 && single_succ (new_dest
) == EXIT_BLOCK_PTR
4199 && bitmap_intersect_p (df_get_live_in (new_dest
), merge_set
))
4204 return_regs
= BITMAP_ALLOC (®_obstack
);
4206 /* Start off with the intersection of regs used to pass
4207 params and regs used to return values. */
4208 for (i
= 0; i
< FIRST_PSEUDO_REGISTER
; i
++)
4209 if (FUNCTION_ARG_REGNO_P (i
)
4210 && targetm
.calls
.function_value_regno_p (i
))
4211 bitmap_set_bit (return_regs
, INCOMING_REGNO (i
));
4213 bitmap_and_into (return_regs
, df_get_live_out (ENTRY_BLOCK_PTR
));
4214 bitmap_and_into (return_regs
, df_get_live_in (EXIT_BLOCK_PTR
));
4215 if (!bitmap_empty_p (return_regs
))
4217 FOR_BB_INSNS_REVERSE (new_dest
, insn
)
4218 if (NONDEBUG_INSN_P (insn
))
4221 unsigned int uid
= INSN_UID (insn
);
4223 /* If this insn sets any reg in return_regs.. */
4224 for (def_rec
= DF_INSN_UID_DEFS (uid
); *def_rec
; def_rec
++)
4226 df_ref def
= *def_rec
;
4227 unsigned r
= DF_REF_REGNO (def
);
4229 if (bitmap_bit_p (return_regs
, r
))
4232 /* ..then add all reg uses to the set of regs
4233 we're interested in. */
4235 df_simulate_uses (insn
, return_regs
);
4237 if (bitmap_intersect_p (merge_set
, return_regs
))
4239 BITMAP_FREE (return_regs
);
4240 BITMAP_FREE (merge_set
);
4244 BITMAP_FREE (return_regs
);
4250 /* We don't want to use normal invert_jump or redirect_jump because
4251 we don't want to delete_insn called. Also, we want to do our own
4252 change group management. */
4254 old_dest
= JUMP_LABEL (jump
);
4255 if (other_bb
!= new_dest
)
4257 if (JUMP_P (BB_END (dest_edge
->src
)))
4258 new_dest_label
= JUMP_LABEL (BB_END (dest_edge
->src
));
4259 else if (new_dest
== EXIT_BLOCK_PTR
)
4260 new_dest_label
= ret_rtx
;
4262 new_dest_label
= block_label (new_dest
);
4265 ? ! invert_jump_1 (jump
, new_dest_label
)
4266 : ! redirect_jump_1 (jump
, new_dest_label
))
4270 if (verify_changes (n_validated_changes
))
4271 confirm_change_group ();
4275 if (other_bb
!= new_dest
)
4277 redirect_jump_2 (jump
, old_dest
, new_dest_label
, 0, reversep
);
4279 redirect_edge_succ (BRANCH_EDGE (test_bb
), new_dest
);
4282 gcov_type count
, probability
;
4283 count
= BRANCH_EDGE (test_bb
)->count
;
4284 BRANCH_EDGE (test_bb
)->count
= FALLTHRU_EDGE (test_bb
)->count
;
4285 FALLTHRU_EDGE (test_bb
)->count
= count
;
4286 probability
= BRANCH_EDGE (test_bb
)->probability
;
4287 BRANCH_EDGE (test_bb
)->probability
4288 = FALLTHRU_EDGE (test_bb
)->probability
;
4289 FALLTHRU_EDGE (test_bb
)->probability
= probability
;
4290 update_br_prob_note (test_bb
);
4294 /* Move the insns out of MERGE_BB to before the branch. */
4299 if (end
== BB_END (merge_bb
))
4300 BB_END (merge_bb
) = PREV_INSN (head
);
4302 /* PR 21767: when moving insns above a conditional branch, the REG_EQUAL
4303 notes being moved might become invalid. */
4309 if (! INSN_P (insn
))
4311 note
= find_reg_note (insn
, REG_EQUAL
, NULL_RTX
);
4314 set
= single_set (insn
);
4315 if (!set
|| !function_invariant_p (SET_SRC (set
))
4316 || !function_invariant_p (XEXP (note
, 0)))
4317 remove_note (insn
, note
);
4318 } while (insn
!= end
&& (insn
= NEXT_INSN (insn
)));
4320 /* PR46315: when moving insns above a conditional branch, the REG_EQUAL
4321 notes referring to the registers being set might become invalid. */
4327 EXECUTE_IF_SET_IN_BITMAP (merge_set
, 0, i
, bi
)
4328 remove_reg_equal_equiv_notes_for_regno (i
);
4330 BITMAP_FREE (merge_set
);
4333 reorder_insns (head
, end
, PREV_INSN (earliest
));
4336 /* Remove the jump and edge if we can. */
4337 if (other_bb
== new_dest
)
4340 remove_edge (BRANCH_EDGE (test_bb
));
4341 /* ??? Can't merge blocks here, as then_bb is still in use.
4342 At minimum, the merge will get done just before bb-reorder. */
4351 BITMAP_FREE (merge_set
);
4356 /* Main entry point for all if-conversion. AFTER_COMBINE is true if
4357 we are after combine pass. */
4360 if_convert (bool after_combine
)
4367 df_live_add_problem ();
4368 df_live_set_all_dirty ();
4371 /* Record whether we are after combine pass. */
4372 ifcvt_after_combine
= after_combine
;
4373 num_possible_if_blocks
= 0;
4374 num_updated_if_blocks
= 0;
4375 num_true_changes
= 0;
4377 loop_optimizer_init (AVOID_CFG_MODIFICATIONS
);
4378 mark_loop_exit_edges ();
4379 loop_optimizer_finalize ();
4380 free_dominance_info (CDI_DOMINATORS
);
4382 /* Compute postdominators. */
4383 calculate_dominance_info (CDI_POST_DOMINATORS
);
4385 df_set_flags (DF_LR_RUN_DCE
);
4387 /* Go through each of the basic blocks looking for things to convert. If we
4388 have conditional execution, we make multiple passes to allow us to handle
4389 IF-THEN{-ELSE} blocks within other IF-THEN{-ELSE} blocks. */
4394 /* Only need to do dce on the first pass. */
4395 df_clear_flags (DF_LR_RUN_DCE
);
4396 cond_exec_changed_p
= FALSE
;
4399 #ifdef IFCVT_MULTIPLE_DUMPS
4400 if (dump_file
&& pass
> 1)
4401 fprintf (dump_file
, "\n\n========== Pass %d ==========\n", pass
);
4407 while (!df_get_bb_dirty (bb
)
4408 && (new_bb
= find_if_header (bb
, pass
)) != NULL
)
4412 #ifdef IFCVT_MULTIPLE_DUMPS
4413 if (dump_file
&& cond_exec_changed_p
)
4414 print_rtl_with_bb (dump_file
, get_insns (), dump_flags
);
4417 while (cond_exec_changed_p
);
4419 #ifdef IFCVT_MULTIPLE_DUMPS
4421 fprintf (dump_file
, "\n\n========== no more changes\n");
4424 free_dominance_info (CDI_POST_DOMINATORS
);
4429 clear_aux_for_blocks ();
4431 /* If we allocated new pseudos, we must resize the array for sched1. */
4432 if (max_regno
< max_reg_num ())
4433 max_regno
= max_reg_num ();
4435 /* Write the final stats. */
4436 if (dump_file
&& num_possible_if_blocks
> 0)
4439 "\n%d possible IF blocks searched.\n",
4440 num_possible_if_blocks
);
4442 "%d IF blocks converted.\n",
4443 num_updated_if_blocks
);
4445 "%d true changes made.\n\n\n",
4450 df_remove_problem (df_live
);
4452 #ifdef ENABLE_CHECKING
4453 verify_flow_info ();
4458 gate_handle_if_conversion (void)
4460 return (optimize
> 0)
4461 && dbg_cnt (if_conversion
);
4464 /* If-conversion and CFG cleanup. */
4466 rest_of_handle_if_conversion (void)
4468 if (flag_if_conversion
)
4472 dump_reg_info (dump_file
);
4473 dump_flow_info (dump_file
, dump_flags
);
4475 cleanup_cfg (CLEANUP_EXPENSIVE
);
4485 const pass_data pass_data_rtl_ifcvt
=
4487 RTL_PASS
, /* type */
4489 OPTGROUP_NONE
, /* optinfo_flags */
4490 true, /* has_gate */
4491 true, /* has_execute */
4492 TV_IFCVT
, /* tv_id */
4493 0, /* properties_required */
4494 0, /* properties_provided */
4495 0, /* properties_destroyed */
4496 0, /* todo_flags_start */
4497 ( TODO_df_finish
| TODO_verify_rtl_sharing
| 0 ), /* todo_flags_finish */
4500 class pass_rtl_ifcvt
: public rtl_opt_pass
4503 pass_rtl_ifcvt(gcc::context
*ctxt
)
4504 : rtl_opt_pass(pass_data_rtl_ifcvt
, ctxt
)
4507 /* opt_pass methods: */
4508 bool gate () { return gate_handle_if_conversion (); }
4509 unsigned int execute () { return rest_of_handle_if_conversion (); }
4511 }; // class pass_rtl_ifcvt
4516 make_pass_rtl_ifcvt (gcc::context
*ctxt
)
4518 return new pass_rtl_ifcvt (ctxt
);
4522 gate_handle_if_after_combine (void)
4524 return optimize
> 0 && flag_if_conversion
4525 && dbg_cnt (if_after_combine
);
4529 /* Rerun if-conversion, as combine may have simplified things enough
4530 to now meet sequence length restrictions. */
4532 rest_of_handle_if_after_combine (void)
4540 const pass_data pass_data_if_after_combine
=
4542 RTL_PASS
, /* type */
4544 OPTGROUP_NONE
, /* optinfo_flags */
4545 true, /* has_gate */
4546 true, /* has_execute */
4547 TV_IFCVT
, /* tv_id */
4548 0, /* properties_required */
4549 0, /* properties_provided */
4550 0, /* properties_destroyed */
4551 0, /* todo_flags_start */
4552 ( TODO_df_finish
| TODO_verify_rtl_sharing
), /* todo_flags_finish */
4555 class pass_if_after_combine
: public rtl_opt_pass
4558 pass_if_after_combine(gcc::context
*ctxt
)
4559 : rtl_opt_pass(pass_data_if_after_combine
, ctxt
)
4562 /* opt_pass methods: */
4563 bool gate () { return gate_handle_if_after_combine (); }
4564 unsigned int execute () { return rest_of_handle_if_after_combine (); }
4566 }; // class pass_if_after_combine
4571 make_pass_if_after_combine (gcc::context
*ctxt
)
4573 return new pass_if_after_combine (ctxt
);
4578 gate_handle_if_after_reload (void)
4580 return optimize
> 0 && flag_if_conversion2
4581 && dbg_cnt (if_after_reload
);
4585 rest_of_handle_if_after_reload (void)
4594 const pass_data pass_data_if_after_reload
=
4596 RTL_PASS
, /* type */
4598 OPTGROUP_NONE
, /* optinfo_flags */
4599 true, /* has_gate */
4600 true, /* has_execute */
4601 TV_IFCVT2
, /* tv_id */
4602 0, /* properties_required */
4603 0, /* properties_provided */
4604 0, /* properties_destroyed */
4605 0, /* todo_flags_start */
4606 ( TODO_df_finish
| TODO_verify_rtl_sharing
), /* todo_flags_finish */
4609 class pass_if_after_reload
: public rtl_opt_pass
4612 pass_if_after_reload(gcc::context
*ctxt
)
4613 : rtl_opt_pass(pass_data_if_after_reload
, ctxt
)
4616 /* opt_pass methods: */
4617 bool gate () { return gate_handle_if_after_reload (); }
4618 unsigned int execute () { return rest_of_handle_if_after_reload (); }
4620 }; // class pass_if_after_reload
4625 make_pass_if_after_reload (gcc::context
*ctxt
)
4627 return new pass_if_after_reload (ctxt
);