1 /* If-conversion support.
2 Copyright (C) 2000-2014 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"
46 #ifndef HAVE_conditional_move
47 #define HAVE_conditional_move 0
59 #ifndef MAX_CONDITIONAL_EXECUTE
60 #define MAX_CONDITIONAL_EXECUTE \
61 (BRANCH_COST (optimize_function_for_speed_p (cfun), false) \
65 #define IFCVT_MULTIPLE_DUMPS 1
67 #define NULL_BLOCK ((basic_block) NULL)
69 /* True if after combine pass. */
70 static bool ifcvt_after_combine
;
72 /* # of IF-THEN or IF-THEN-ELSE blocks we looked at */
73 static int num_possible_if_blocks
;
75 /* # of IF-THEN or IF-THEN-ELSE blocks were converted to conditional
77 static int num_updated_if_blocks
;
79 /* # of changes made. */
80 static int num_true_changes
;
82 /* Whether conditional execution changes were made. */
83 static int cond_exec_changed_p
;
85 /* Forward references. */
86 static int count_bb_insns (const_basic_block
);
87 static bool cheap_bb_rtx_cost_p (const_basic_block
, int, int);
88 static rtx
first_active_insn (basic_block
);
89 static rtx
last_active_insn (basic_block
, int);
90 static rtx
find_active_insn_before (basic_block
, rtx
);
91 static rtx
find_active_insn_after (basic_block
, rtx
);
92 static basic_block
block_fallthru (basic_block
);
93 static int cond_exec_process_insns (ce_if_block
*, rtx
, rtx
, rtx
, int, int);
94 static rtx
cond_exec_get_condition (rtx
);
95 static rtx
noce_get_condition (rtx
, rtx
*, bool);
96 static int noce_operand_ok (const_rtx
);
97 static void merge_if_block (ce_if_block
*);
98 static int find_cond_trap (basic_block
, edge
, edge
);
99 static basic_block
find_if_header (basic_block
, int);
100 static int block_jumps_and_fallthru_p (basic_block
, basic_block
);
101 static int noce_find_if_block (basic_block
, edge
, edge
, int);
102 static int cond_exec_find_if_block (ce_if_block
*);
103 static int find_if_case_1 (basic_block
, edge
, edge
);
104 static int find_if_case_2 (basic_block
, edge
, edge
);
105 static int dead_or_predicable (basic_block
, basic_block
, basic_block
,
107 static void noce_emit_move_insn (rtx
, rtx
);
108 static rtx
block_has_only_trap (basic_block
);
110 /* Count the number of non-jump active insns in BB. */
113 count_bb_insns (const_basic_block bb
)
116 rtx insn
= BB_HEAD (bb
);
120 if (active_insn_p (insn
) && !JUMP_P (insn
))
123 if (insn
== BB_END (bb
))
125 insn
= NEXT_INSN (insn
);
131 /* Determine whether the total insn_rtx_cost on non-jump insns in
132 basic block BB is less than MAX_COST. This function returns
133 false if the cost of any instruction could not be estimated.
135 The cost of the non-jump insns in BB is scaled by REG_BR_PROB_BASE
136 as those insns are being speculated. MAX_COST is scaled with SCALE
137 plus a small fudge factor. */
140 cheap_bb_rtx_cost_p (const_basic_block bb
, int scale
, int max_cost
)
143 rtx insn
= BB_HEAD (bb
);
144 bool speed
= optimize_bb_for_speed_p (bb
);
146 /* Set scale to REG_BR_PROB_BASE to void the identical scaling
147 applied to insn_rtx_cost when optimizing for size. Only do
148 this after combine because if-conversion might interfere with
149 passes before combine.
151 Use optimize_function_for_speed_p instead of the pre-defined
152 variable speed to make sure it is set to same value for all
153 basic blocks in one if-conversion transformation. */
154 if (!optimize_function_for_speed_p (cfun
) && ifcvt_after_combine
)
155 scale
= REG_BR_PROB_BASE
;
156 /* Our branch probability/scaling factors are just estimates and don't
157 account for cases where we can get speculation for free and other
158 secondary benefits. So we fudge the scale factor to make speculating
159 appear a little more profitable when optimizing for performance. */
161 scale
+= REG_BR_PROB_BASE
/ 8;
168 if (NONJUMP_INSN_P (insn
))
170 int cost
= insn_rtx_cost (PATTERN (insn
), speed
) * REG_BR_PROB_BASE
;
174 /* If this instruction is the load or set of a "stack" register,
175 such as a floating point register on x87, then the cost of
176 speculatively executing this insn may need to include
177 the additional cost of popping its result off of the
178 register stack. Unfortunately, correctly recognizing and
179 accounting for this additional overhead is tricky, so for
180 now we simply prohibit such speculative execution. */
183 rtx set
= single_set (insn
);
184 if (set
&& STACK_REG_P (SET_DEST (set
)))
190 if (count
>= max_cost
)
193 else if (CALL_P (insn
))
196 if (insn
== BB_END (bb
))
198 insn
= NEXT_INSN (insn
);
204 /* Return the first non-jump active insn in the basic block. */
207 first_active_insn (basic_block bb
)
209 rtx insn
= BB_HEAD (bb
);
213 if (insn
== BB_END (bb
))
215 insn
= NEXT_INSN (insn
);
218 while (NOTE_P (insn
) || DEBUG_INSN_P (insn
))
220 if (insn
== BB_END (bb
))
222 insn
= NEXT_INSN (insn
);
231 /* Return the last non-jump active (non-jump) insn in the basic block. */
234 last_active_insn (basic_block bb
, int skip_use_p
)
236 rtx insn
= BB_END (bb
);
237 rtx head
= BB_HEAD (bb
);
241 || DEBUG_INSN_P (insn
)
243 && NONJUMP_INSN_P (insn
)
244 && GET_CODE (PATTERN (insn
)) == USE
))
248 insn
= PREV_INSN (insn
);
257 /* Return the active insn before INSN inside basic block CURR_BB. */
260 find_active_insn_before (basic_block curr_bb
, rtx insn
)
262 if (!insn
|| insn
== BB_HEAD (curr_bb
))
265 while ((insn
= PREV_INSN (insn
)) != NULL_RTX
)
267 if (NONJUMP_INSN_P (insn
) || JUMP_P (insn
) || CALL_P (insn
))
270 /* No other active insn all the way to the start of the basic block. */
271 if (insn
== BB_HEAD (curr_bb
))
278 /* Return the active insn after INSN inside basic block CURR_BB. */
281 find_active_insn_after (basic_block curr_bb
, rtx insn
)
283 if (!insn
|| insn
== BB_END (curr_bb
))
286 while ((insn
= NEXT_INSN (insn
)) != NULL_RTX
)
288 if (NONJUMP_INSN_P (insn
) || JUMP_P (insn
) || CALL_P (insn
))
291 /* No other active insn all the way to the end of the basic block. */
292 if (insn
== BB_END (curr_bb
))
299 /* Return the basic block reached by falling though the basic block BB. */
302 block_fallthru (basic_block bb
)
304 edge e
= find_fallthru_edge (bb
->succs
);
306 return (e
) ? e
->dest
: NULL_BLOCK
;
309 /* Return true if RTXs A and B can be safely interchanged. */
312 rtx_interchangeable_p (const_rtx a
, const_rtx b
)
314 if (!rtx_equal_p (a
, b
))
317 if (GET_CODE (a
) != MEM
)
320 /* A dead type-unsafe memory reference is legal, but a live type-unsafe memory
321 reference is not. Interchanging a dead type-unsafe memory reference with
322 a live type-safe one creates a live type-unsafe memory reference, in other
323 words, it makes the program illegal.
324 We check here conservatively whether the two memory references have equal
325 memory attributes. */
327 return mem_attrs_eq_p (get_mem_attrs (a
), get_mem_attrs (b
));
331 /* Go through a bunch of insns, converting them to conditional
332 execution format if possible. Return TRUE if all of the non-note
333 insns were processed. */
336 cond_exec_process_insns (ce_if_block
*ce_info ATTRIBUTE_UNUSED
,
337 /* if block information */rtx start
,
338 /* first insn to look at */rtx end
,
339 /* last insn to look at */rtx test
,
340 /* conditional execution test */int prob_val
,
341 /* probability of branch taken. */int mod_ok
)
343 int must_be_last
= FALSE
;
351 for (insn
= start
; ; insn
= NEXT_INSN (insn
))
353 /* dwarf2out can't cope with conditional prologues. */
354 if (NOTE_P (insn
) && NOTE_KIND (insn
) == NOTE_INSN_PROLOGUE_END
)
357 if (NOTE_P (insn
) || DEBUG_INSN_P (insn
))
360 gcc_assert (NONJUMP_INSN_P (insn
) || CALL_P (insn
));
362 /* dwarf2out can't cope with conditional unwind info. */
363 if (RTX_FRAME_RELATED_P (insn
))
366 /* Remove USE insns that get in the way. */
367 if (reload_completed
&& GET_CODE (PATTERN (insn
)) == USE
)
369 /* ??? Ug. Actually unlinking the thing is problematic,
370 given what we'd have to coordinate with our callers. */
371 SET_INSN_DELETED (insn
);
375 /* Last insn wasn't last? */
379 if (modified_in_p (test
, insn
))
386 /* Now build the conditional form of the instruction. */
387 pattern
= PATTERN (insn
);
388 xtest
= copy_rtx (test
);
390 /* If this is already a COND_EXEC, rewrite the test to be an AND of the
392 if (GET_CODE (pattern
) == COND_EXEC
)
394 if (GET_MODE (xtest
) != GET_MODE (COND_EXEC_TEST (pattern
)))
397 xtest
= gen_rtx_AND (GET_MODE (xtest
), xtest
,
398 COND_EXEC_TEST (pattern
));
399 pattern
= COND_EXEC_CODE (pattern
);
402 pattern
= gen_rtx_COND_EXEC (VOIDmode
, xtest
, pattern
);
404 /* If the machine needs to modify the insn being conditionally executed,
405 say for example to force a constant integer operand into a temp
406 register, do so here. */
407 #ifdef IFCVT_MODIFY_INSN
408 IFCVT_MODIFY_INSN (ce_info
, pattern
, insn
);
413 validate_change (insn
, &PATTERN (insn
), pattern
, 1);
415 if (CALL_P (insn
) && prob_val
>= 0)
416 validate_change (insn
, ®_NOTES (insn
),
417 gen_rtx_INT_LIST ((enum machine_mode
) REG_BR_PROB
,
418 prob_val
, REG_NOTES (insn
)), 1);
428 /* Return the condition for a jump. Do not do any special processing. */
431 cond_exec_get_condition (rtx jump
)
435 if (any_condjump_p (jump
))
436 test_if
= SET_SRC (pc_set (jump
));
439 cond
= XEXP (test_if
, 0);
441 /* If this branches to JUMP_LABEL when the condition is false,
442 reverse the condition. */
443 if (GET_CODE (XEXP (test_if
, 2)) == LABEL_REF
444 && XEXP (XEXP (test_if
, 2), 0) == JUMP_LABEL (jump
))
446 enum rtx_code rev
= reversed_comparison_code (cond
, jump
);
450 cond
= gen_rtx_fmt_ee (rev
, GET_MODE (cond
), XEXP (cond
, 0),
457 /* Given a simple IF-THEN or IF-THEN-ELSE block, attempt to convert it
458 to conditional execution. Return TRUE if we were successful at
459 converting the block. */
462 cond_exec_process_if_block (ce_if_block
* ce_info
,
463 /* if block information */int do_multiple_p
)
465 basic_block test_bb
= ce_info
->test_bb
; /* last test block */
466 basic_block then_bb
= ce_info
->then_bb
; /* THEN */
467 basic_block else_bb
= ce_info
->else_bb
; /* ELSE or NULL */
468 rtx test_expr
; /* expression in IF_THEN_ELSE that is tested */
469 rtx then_start
; /* first insn in THEN block */
470 rtx then_end
; /* last insn + 1 in THEN block */
471 rtx else_start
= NULL_RTX
; /* first insn in ELSE block or NULL */
472 rtx else_end
= NULL_RTX
; /* last insn + 1 in ELSE block */
473 int max
; /* max # of insns to convert. */
474 int then_mod_ok
; /* whether conditional mods are ok in THEN */
475 rtx true_expr
; /* test for else block insns */
476 rtx false_expr
; /* test for then block insns */
477 int true_prob_val
; /* probability of else block */
478 int false_prob_val
; /* probability of then block */
479 rtx_insn
*then_last_head
= NULL
; /* Last match at the head of THEN */
480 rtx_insn
*else_last_head
= NULL
; /* Last match at the head of ELSE */
481 rtx_insn
*then_first_tail
= NULL
; /* First match at the tail of THEN */
482 rtx_insn
*else_first_tail
= NULL
; /* First match at the tail of ELSE */
483 int then_n_insns
, else_n_insns
, n_insns
;
484 enum rtx_code false_code
;
487 /* If test is comprised of && or || elements, and we've failed at handling
488 all of them together, just use the last test if it is the special case of
489 && elements without an ELSE block. */
490 if (!do_multiple_p
&& ce_info
->num_multiple_test_blocks
)
492 if (else_bb
|| ! ce_info
->and_and_p
)
495 ce_info
->test_bb
= test_bb
= ce_info
->last_test_bb
;
496 ce_info
->num_multiple_test_blocks
= 0;
497 ce_info
->num_and_and_blocks
= 0;
498 ce_info
->num_or_or_blocks
= 0;
501 /* Find the conditional jump to the ELSE or JOIN part, and isolate
503 test_expr
= cond_exec_get_condition (BB_END (test_bb
));
507 /* If the conditional jump is more than just a conditional jump,
508 then we can not do conditional execution conversion on this block. */
509 if (! onlyjump_p (BB_END (test_bb
)))
512 /* Collect the bounds of where we're to search, skipping any labels, jumps
513 and notes at the beginning and end of the block. Then count the total
514 number of insns and see if it is small enough to convert. */
515 then_start
= first_active_insn (then_bb
);
516 then_end
= last_active_insn (then_bb
, TRUE
);
517 then_n_insns
= ce_info
->num_then_insns
= count_bb_insns (then_bb
);
518 n_insns
= then_n_insns
;
519 max
= MAX_CONDITIONAL_EXECUTE
;
526 else_start
= first_active_insn (else_bb
);
527 else_end
= last_active_insn (else_bb
, TRUE
);
528 else_n_insns
= ce_info
->num_else_insns
= count_bb_insns (else_bb
);
529 n_insns
+= else_n_insns
;
531 /* Look for matching sequences at the head and tail of the two blocks,
532 and limit the range of insns to be converted if possible. */
533 n_matching
= flow_find_cross_jump (then_bb
, else_bb
,
534 &then_first_tail
, &else_first_tail
,
536 if (then_first_tail
== BB_HEAD (then_bb
))
537 then_start
= then_end
= NULL_RTX
;
538 if (else_first_tail
== BB_HEAD (else_bb
))
539 else_start
= else_end
= NULL_RTX
;
544 then_end
= find_active_insn_before (then_bb
, then_first_tail
);
546 else_end
= find_active_insn_before (else_bb
, else_first_tail
);
547 n_insns
-= 2 * n_matching
;
552 && then_n_insns
> n_matching
553 && else_n_insns
> n_matching
)
555 int longest_match
= MIN (then_n_insns
- n_matching
,
556 else_n_insns
- n_matching
);
558 = flow_find_head_matching_sequence (then_bb
, else_bb
,
567 /* We won't pass the insns in the head sequence to
568 cond_exec_process_insns, so we need to test them here
569 to make sure that they don't clobber the condition. */
570 for (insn
= BB_HEAD (then_bb
);
571 insn
!= NEXT_INSN (then_last_head
);
572 insn
= NEXT_INSN (insn
))
573 if (!LABEL_P (insn
) && !NOTE_P (insn
)
574 && !DEBUG_INSN_P (insn
)
575 && modified_in_p (test_expr
, insn
))
579 if (then_last_head
== then_end
)
580 then_start
= then_end
= NULL_RTX
;
581 if (else_last_head
== else_end
)
582 else_start
= else_end
= NULL_RTX
;
587 then_start
= find_active_insn_after (then_bb
, then_last_head
);
589 else_start
= find_active_insn_after (else_bb
, else_last_head
);
590 n_insns
-= 2 * n_matching
;
598 /* Map test_expr/test_jump into the appropriate MD tests to use on
599 the conditionally executed code. */
601 true_expr
= test_expr
;
603 false_code
= reversed_comparison_code (true_expr
, BB_END (test_bb
));
604 if (false_code
!= UNKNOWN
)
605 false_expr
= gen_rtx_fmt_ee (false_code
, GET_MODE (true_expr
),
606 XEXP (true_expr
, 0), XEXP (true_expr
, 1));
608 false_expr
= NULL_RTX
;
610 #ifdef IFCVT_MODIFY_TESTS
611 /* If the machine description needs to modify the tests, such as setting a
612 conditional execution register from a comparison, it can do so here. */
613 IFCVT_MODIFY_TESTS (ce_info
, true_expr
, false_expr
);
615 /* See if the conversion failed. */
616 if (!true_expr
|| !false_expr
)
620 note
= find_reg_note (BB_END (test_bb
), REG_BR_PROB
, NULL_RTX
);
623 true_prob_val
= XINT (note
, 0);
624 false_prob_val
= REG_BR_PROB_BASE
- true_prob_val
;
632 /* If we have && or || tests, do them here. These tests are in the adjacent
633 blocks after the first block containing the test. */
634 if (ce_info
->num_multiple_test_blocks
> 0)
636 basic_block bb
= test_bb
;
637 basic_block last_test_bb
= ce_info
->last_test_bb
;
646 enum rtx_code f_code
;
648 bb
= block_fallthru (bb
);
649 start
= first_active_insn (bb
);
650 end
= last_active_insn (bb
, TRUE
);
652 && ! cond_exec_process_insns (ce_info
, start
, end
, false_expr
,
653 false_prob_val
, FALSE
))
656 /* If the conditional jump is more than just a conditional jump, then
657 we can not do conditional execution conversion on this block. */
658 if (! onlyjump_p (BB_END (bb
)))
661 /* Find the conditional jump and isolate the test. */
662 t
= cond_exec_get_condition (BB_END (bb
));
666 f_code
= reversed_comparison_code (t
, BB_END (bb
));
667 if (f_code
== UNKNOWN
)
670 f
= gen_rtx_fmt_ee (f_code
, GET_MODE (t
), XEXP (t
, 0), XEXP (t
, 1));
671 if (ce_info
->and_and_p
)
673 t
= gen_rtx_AND (GET_MODE (t
), true_expr
, t
);
674 f
= gen_rtx_IOR (GET_MODE (t
), false_expr
, f
);
678 t
= gen_rtx_IOR (GET_MODE (t
), true_expr
, t
);
679 f
= gen_rtx_AND (GET_MODE (t
), false_expr
, f
);
682 /* If the machine description needs to modify the tests, such as
683 setting a conditional execution register from a comparison, it can
685 #ifdef IFCVT_MODIFY_MULTIPLE_TESTS
686 IFCVT_MODIFY_MULTIPLE_TESTS (ce_info
, bb
, t
, f
);
688 /* See if the conversion failed. */
696 while (bb
!= last_test_bb
);
699 /* For IF-THEN-ELSE blocks, we don't allow modifications of the test
700 on then THEN block. */
701 then_mod_ok
= (else_bb
== NULL_BLOCK
);
703 /* Go through the THEN and ELSE blocks converting the insns if possible
704 to conditional execution. */
708 || ! cond_exec_process_insns (ce_info
, then_start
, then_end
,
709 false_expr
, false_prob_val
,
713 if (else_bb
&& else_end
714 && ! cond_exec_process_insns (ce_info
, else_start
, else_end
,
715 true_expr
, true_prob_val
, TRUE
))
718 /* If we cannot apply the changes, fail. Do not go through the normal fail
719 processing, since apply_change_group will call cancel_changes. */
720 if (! apply_change_group ())
722 #ifdef IFCVT_MODIFY_CANCEL
723 /* Cancel any machine dependent changes. */
724 IFCVT_MODIFY_CANCEL (ce_info
);
729 #ifdef IFCVT_MODIFY_FINAL
730 /* Do any machine dependent final modifications. */
731 IFCVT_MODIFY_FINAL (ce_info
);
734 /* Conversion succeeded. */
736 fprintf (dump_file
, "%d insn%s converted to conditional execution.\n",
737 n_insns
, (n_insns
== 1) ? " was" : "s were");
739 /* Merge the blocks! If we had matching sequences, make sure to delete one
740 copy at the appropriate location first: delete the copy in the THEN branch
741 for a tail sequence so that the remaining one is executed last for both
742 branches, and delete the copy in the ELSE branch for a head sequence so
743 that the remaining one is executed first for both branches. */
746 rtx from
= then_first_tail
;
748 from
= find_active_insn_after (then_bb
, from
);
749 delete_insn_chain (from
, BB_END (then_bb
), false);
752 delete_insn_chain (first_active_insn (else_bb
), else_last_head
, false);
754 merge_if_block (ce_info
);
755 cond_exec_changed_p
= TRUE
;
759 #ifdef IFCVT_MODIFY_CANCEL
760 /* Cancel any machine dependent changes. */
761 IFCVT_MODIFY_CANCEL (ce_info
);
768 /* Used by noce_process_if_block to communicate with its subroutines.
770 The subroutines know that A and B may be evaluated freely. They
771 know that X is a register. They should insert new instructions
772 before cond_earliest. */
776 /* The basic blocks that make up the IF-THEN-{ELSE-,}JOIN block. */
777 basic_block test_bb
, then_bb
, else_bb
, join_bb
;
779 /* The jump that ends TEST_BB. */
782 /* The jump condition. */
785 /* New insns should be inserted before this one. */
788 /* Insns in the THEN and ELSE block. There is always just this
789 one insns in those blocks. The insns are single_set insns.
790 If there was no ELSE block, INSN_B is the last insn before
791 COND_EARLIEST, or NULL_RTX. In the former case, the insn
792 operands are still valid, as if INSN_B was moved down below
796 /* The SET_SRC of INSN_A and INSN_B. */
799 /* The SET_DEST of INSN_A. */
802 /* True if this if block is not canonical. In the canonical form of
803 if blocks, the THEN_BB is the block reached via the fallthru edge
804 from TEST_BB. For the noce transformations, we allow the symmetric
806 bool then_else_reversed
;
808 /* Estimated cost of the particular branch instruction. */
812 static rtx
noce_emit_store_flag (struct noce_if_info
*, rtx
, int, int);
813 static int noce_try_move (struct noce_if_info
*);
814 static int noce_try_store_flag (struct noce_if_info
*);
815 static int noce_try_addcc (struct noce_if_info
*);
816 static int noce_try_store_flag_constants (struct noce_if_info
*);
817 static int noce_try_store_flag_mask (struct noce_if_info
*);
818 static rtx
noce_emit_cmove (struct noce_if_info
*, rtx
, enum rtx_code
, rtx
,
820 static int noce_try_cmove (struct noce_if_info
*);
821 static int noce_try_cmove_arith (struct noce_if_info
*);
822 static rtx
noce_get_alt_condition (struct noce_if_info
*, rtx
, rtx
*);
823 static int noce_try_minmax (struct noce_if_info
*);
824 static int noce_try_abs (struct noce_if_info
*);
825 static int noce_try_sign_mask (struct noce_if_info
*);
827 /* Helper function for noce_try_store_flag*. */
830 noce_emit_store_flag (struct noce_if_info
*if_info
, rtx x
, int reversep
,
833 rtx cond
= if_info
->cond
;
837 cond_complex
= (! general_operand (XEXP (cond
, 0), VOIDmode
)
838 || ! general_operand (XEXP (cond
, 1), VOIDmode
));
840 /* If earliest == jump, or when the condition is complex, try to
841 build the store_flag insn directly. */
845 rtx set
= pc_set (if_info
->jump
);
846 cond
= XEXP (SET_SRC (set
), 0);
847 if (GET_CODE (XEXP (SET_SRC (set
), 2)) == LABEL_REF
848 && XEXP (XEXP (SET_SRC (set
), 2), 0) == JUMP_LABEL (if_info
->jump
))
849 reversep
= !reversep
;
850 if (if_info
->then_else_reversed
)
851 reversep
= !reversep
;
855 code
= reversed_comparison_code (cond
, if_info
->jump
);
857 code
= GET_CODE (cond
);
859 if ((if_info
->cond_earliest
== if_info
->jump
|| cond_complex
)
860 && (normalize
== 0 || STORE_FLAG_VALUE
== normalize
))
864 tmp
= gen_rtx_fmt_ee (code
, GET_MODE (x
), XEXP (cond
, 0),
866 tmp
= gen_rtx_SET (VOIDmode
, x
, tmp
);
869 tmp
= emit_insn (tmp
);
871 if (recog_memoized (tmp
) >= 0)
877 if_info
->cond_earliest
= if_info
->jump
;
885 /* Don't even try if the comparison operands or the mode of X are weird. */
886 if (cond_complex
|| !SCALAR_INT_MODE_P (GET_MODE (x
)))
889 return emit_store_flag (x
, code
, XEXP (cond
, 0),
890 XEXP (cond
, 1), VOIDmode
,
891 (code
== LTU
|| code
== LEU
892 || code
== GEU
|| code
== GTU
), normalize
);
895 /* Emit instruction to move an rtx, possibly into STRICT_LOW_PART.
896 X is the destination/target and Y is the value to copy. */
899 noce_emit_move_insn (rtx x
, rtx y
)
901 enum machine_mode outmode
;
905 if (GET_CODE (x
) != STRICT_LOW_PART
)
907 rtx seq
, insn
, target
;
911 /* Check that the SET_SRC is reasonable before calling emit_move_insn,
912 otherwise construct a suitable SET pattern ourselves. */
913 insn
= (OBJECT_P (y
) || CONSTANT_P (y
) || GET_CODE (y
) == SUBREG
)
914 ? emit_move_insn (x
, y
)
915 : emit_insn (gen_rtx_SET (VOIDmode
, x
, y
));
919 if (recog_memoized (insn
) <= 0)
921 if (GET_CODE (x
) == ZERO_EXTRACT
)
923 rtx op
= XEXP (x
, 0);
924 unsigned HOST_WIDE_INT size
= INTVAL (XEXP (x
, 1));
925 unsigned HOST_WIDE_INT start
= INTVAL (XEXP (x
, 2));
927 /* store_bit_field expects START to be relative to
928 BYTES_BIG_ENDIAN and adjusts this value for machines with
929 BITS_BIG_ENDIAN != BYTES_BIG_ENDIAN. In order to be able to
930 invoke store_bit_field again it is necessary to have the START
931 value from the first call. */
932 if (BITS_BIG_ENDIAN
!= BYTES_BIG_ENDIAN
)
935 start
= BITS_PER_UNIT
- start
- size
;
938 gcc_assert (REG_P (op
));
939 start
= BITS_PER_WORD
- start
- size
;
943 gcc_assert (start
< (MEM_P (op
) ? BITS_PER_UNIT
: BITS_PER_WORD
));
944 store_bit_field (op
, size
, start
, 0, 0, GET_MODE (x
), y
);
948 switch (GET_RTX_CLASS (GET_CODE (y
)))
951 ot
= code_to_optab (GET_CODE (y
));
955 target
= expand_unop (GET_MODE (y
), ot
, XEXP (y
, 0), x
, 0);
956 if (target
!= NULL_RTX
)
959 emit_move_insn (x
, target
);
968 ot
= code_to_optab (GET_CODE (y
));
972 target
= expand_binop (GET_MODE (y
), ot
,
973 XEXP (y
, 0), XEXP (y
, 1),
975 if (target
!= NULL_RTX
)
978 emit_move_insn (x
, target
);
995 inner
= XEXP (outer
, 0);
996 outmode
= GET_MODE (outer
);
997 bitpos
= SUBREG_BYTE (outer
) * BITS_PER_UNIT
;
998 store_bit_field (inner
, GET_MODE_BITSIZE (outmode
), bitpos
,
1002 /* Return sequence of instructions generated by if conversion. This
1003 function calls end_sequence() to end the current stream, ensures
1004 that are instructions are unshared, recognizable non-jump insns.
1005 On failure, this function returns a NULL_RTX. */
1008 end_ifcvt_sequence (struct noce_if_info
*if_info
)
1011 rtx seq
= get_insns ();
1013 set_used_flags (if_info
->x
);
1014 set_used_flags (if_info
->cond
);
1015 set_used_flags (if_info
->a
);
1016 set_used_flags (if_info
->b
);
1017 unshare_all_rtl_in_chain (seq
);
1020 /* Make sure that all of the instructions emitted are recognizable,
1021 and that we haven't introduced a new jump instruction.
1022 As an exercise for the reader, build a general mechanism that
1023 allows proper placement of required clobbers. */
1024 for (insn
= seq
; insn
; insn
= NEXT_INSN (insn
))
1026 || recog_memoized (insn
) == -1)
1032 /* Convert "if (a != b) x = a; else x = b" into "x = a" and
1033 "if (a == b) x = a; else x = b" into "x = b". */
1036 noce_try_move (struct noce_if_info
*if_info
)
1038 rtx cond
= if_info
->cond
;
1039 enum rtx_code code
= GET_CODE (cond
);
1042 if (code
!= NE
&& code
!= EQ
)
1045 /* This optimization isn't valid if either A or B could be a NaN
1046 or a signed zero. */
1047 if (HONOR_NANS (GET_MODE (if_info
->x
))
1048 || HONOR_SIGNED_ZEROS (GET_MODE (if_info
->x
)))
1051 /* Check whether the operands of the comparison are A and in
1053 if ((rtx_equal_p (if_info
->a
, XEXP (cond
, 0))
1054 && rtx_equal_p (if_info
->b
, XEXP (cond
, 1)))
1055 || (rtx_equal_p (if_info
->a
, XEXP (cond
, 1))
1056 && rtx_equal_p (if_info
->b
, XEXP (cond
, 0))))
1058 if (!rtx_interchangeable_p (if_info
->a
, if_info
->b
))
1061 y
= (code
== EQ
) ? if_info
->a
: if_info
->b
;
1063 /* Avoid generating the move if the source is the destination. */
1064 if (! rtx_equal_p (if_info
->x
, y
))
1067 noce_emit_move_insn (if_info
->x
, y
);
1068 seq
= end_ifcvt_sequence (if_info
);
1072 emit_insn_before_setloc (seq
, if_info
->jump
,
1073 INSN_LOCATION (if_info
->insn_a
));
1080 /* Convert "if (test) x = 1; else x = 0".
1082 Only try 0 and STORE_FLAG_VALUE here. Other combinations will be
1083 tried in noce_try_store_flag_constants after noce_try_cmove has had
1084 a go at the conversion. */
1087 noce_try_store_flag (struct noce_if_info
*if_info
)
1092 if (CONST_INT_P (if_info
->b
)
1093 && INTVAL (if_info
->b
) == STORE_FLAG_VALUE
1094 && if_info
->a
== const0_rtx
)
1096 else if (if_info
->b
== const0_rtx
1097 && CONST_INT_P (if_info
->a
)
1098 && INTVAL (if_info
->a
) == STORE_FLAG_VALUE
1099 && (reversed_comparison_code (if_info
->cond
, if_info
->jump
)
1107 target
= noce_emit_store_flag (if_info
, if_info
->x
, reversep
, 0);
1110 if (target
!= if_info
->x
)
1111 noce_emit_move_insn (if_info
->x
, target
);
1113 seq
= end_ifcvt_sequence (if_info
);
1117 emit_insn_before_setloc (seq
, if_info
->jump
,
1118 INSN_LOCATION (if_info
->insn_a
));
1128 /* Convert "if (test) x = a; else x = b", for A and B constant. */
1131 noce_try_store_flag_constants (struct noce_if_info
*if_info
)
1135 HOST_WIDE_INT itrue
, ifalse
, diff
, tmp
;
1136 int normalize
, can_reverse
;
1137 enum machine_mode mode
;
1139 if (CONST_INT_P (if_info
->a
)
1140 && CONST_INT_P (if_info
->b
))
1142 mode
= GET_MODE (if_info
->x
);
1143 ifalse
= INTVAL (if_info
->a
);
1144 itrue
= INTVAL (if_info
->b
);
1146 diff
= (unsigned HOST_WIDE_INT
) itrue
- ifalse
;
1147 /* Make sure we can represent the difference between the two values. */
1149 != ((ifalse
< 0) != (itrue
< 0) ? ifalse
< 0 : ifalse
< itrue
))
1152 diff
= trunc_int_for_mode (diff
, mode
);
1154 can_reverse
= (reversed_comparison_code (if_info
->cond
, if_info
->jump
)
1158 if (diff
== STORE_FLAG_VALUE
|| diff
== -STORE_FLAG_VALUE
)
1160 else if (ifalse
== 0 && exact_log2 (itrue
) >= 0
1161 && (STORE_FLAG_VALUE
== 1
1162 || if_info
->branch_cost
>= 2))
1164 else if (itrue
== 0 && exact_log2 (ifalse
) >= 0 && can_reverse
1165 && (STORE_FLAG_VALUE
== 1 || if_info
->branch_cost
>= 2))
1166 normalize
= 1, reversep
= 1;
1167 else if (itrue
== -1
1168 && (STORE_FLAG_VALUE
== -1
1169 || if_info
->branch_cost
>= 2))
1171 else if (ifalse
== -1 && can_reverse
1172 && (STORE_FLAG_VALUE
== -1 || if_info
->branch_cost
>= 2))
1173 normalize
= -1, reversep
= 1;
1174 else if ((if_info
->branch_cost
>= 2 && STORE_FLAG_VALUE
== -1)
1175 || if_info
->branch_cost
>= 3)
1182 tmp
= itrue
; itrue
= ifalse
; ifalse
= tmp
;
1183 diff
= trunc_int_for_mode (-(unsigned HOST_WIDE_INT
) diff
, mode
);
1187 target
= noce_emit_store_flag (if_info
, if_info
->x
, reversep
, normalize
);
1194 /* if (test) x = 3; else x = 4;
1195 => x = 3 + (test == 0); */
1196 if (diff
== STORE_FLAG_VALUE
|| diff
== -STORE_FLAG_VALUE
)
1198 target
= expand_simple_binop (mode
,
1199 (diff
== STORE_FLAG_VALUE
1201 gen_int_mode (ifalse
, mode
), target
,
1202 if_info
->x
, 0, OPTAB_WIDEN
);
1205 /* if (test) x = 8; else x = 0;
1206 => x = (test != 0) << 3; */
1207 else if (ifalse
== 0 && (tmp
= exact_log2 (itrue
)) >= 0)
1209 target
= expand_simple_binop (mode
, ASHIFT
,
1210 target
, GEN_INT (tmp
), if_info
->x
, 0,
1214 /* if (test) x = -1; else x = b;
1215 => x = -(test != 0) | b; */
1216 else if (itrue
== -1)
1218 target
= expand_simple_binop (mode
, IOR
,
1219 target
, gen_int_mode (ifalse
, mode
),
1220 if_info
->x
, 0, OPTAB_WIDEN
);
1223 /* if (test) x = a; else x = b;
1224 => x = (-(test != 0) & (b - a)) + a; */
1227 target
= expand_simple_binop (mode
, AND
,
1228 target
, gen_int_mode (diff
, mode
),
1229 if_info
->x
, 0, OPTAB_WIDEN
);
1231 target
= expand_simple_binop (mode
, PLUS
,
1232 target
, gen_int_mode (ifalse
, mode
),
1233 if_info
->x
, 0, OPTAB_WIDEN
);
1242 if (target
!= if_info
->x
)
1243 noce_emit_move_insn (if_info
->x
, target
);
1245 seq
= end_ifcvt_sequence (if_info
);
1249 emit_insn_before_setloc (seq
, if_info
->jump
,
1250 INSN_LOCATION (if_info
->insn_a
));
1257 /* Convert "if (test) foo++" into "foo += (test != 0)", and
1258 similarly for "foo--". */
1261 noce_try_addcc (struct noce_if_info
*if_info
)
1264 int subtract
, normalize
;
1266 if (GET_CODE (if_info
->a
) == PLUS
1267 && rtx_equal_p (XEXP (if_info
->a
, 0), if_info
->b
)
1268 && (reversed_comparison_code (if_info
->cond
, if_info
->jump
)
1271 rtx cond
= if_info
->cond
;
1272 enum rtx_code code
= reversed_comparison_code (cond
, if_info
->jump
);
1274 /* First try to use addcc pattern. */
1275 if (general_operand (XEXP (cond
, 0), VOIDmode
)
1276 && general_operand (XEXP (cond
, 1), VOIDmode
))
1279 target
= emit_conditional_add (if_info
->x
, code
,
1284 XEXP (if_info
->a
, 1),
1285 GET_MODE (if_info
->x
),
1286 (code
== LTU
|| code
== GEU
1287 || code
== LEU
|| code
== GTU
));
1290 if (target
!= if_info
->x
)
1291 noce_emit_move_insn (if_info
->x
, target
);
1293 seq
= end_ifcvt_sequence (if_info
);
1297 emit_insn_before_setloc (seq
, if_info
->jump
,
1298 INSN_LOCATION (if_info
->insn_a
));
1304 /* If that fails, construct conditional increment or decrement using
1306 if (if_info
->branch_cost
>= 2
1307 && (XEXP (if_info
->a
, 1) == const1_rtx
1308 || XEXP (if_info
->a
, 1) == constm1_rtx
))
1311 if (STORE_FLAG_VALUE
== INTVAL (XEXP (if_info
->a
, 1)))
1312 subtract
= 0, normalize
= 0;
1313 else if (-STORE_FLAG_VALUE
== INTVAL (XEXP (if_info
->a
, 1)))
1314 subtract
= 1, normalize
= 0;
1316 subtract
= 0, normalize
= INTVAL (XEXP (if_info
->a
, 1));
1319 target
= noce_emit_store_flag (if_info
,
1320 gen_reg_rtx (GET_MODE (if_info
->x
)),
1324 target
= expand_simple_binop (GET_MODE (if_info
->x
),
1325 subtract
? MINUS
: PLUS
,
1326 if_info
->b
, target
, if_info
->x
,
1330 if (target
!= if_info
->x
)
1331 noce_emit_move_insn (if_info
->x
, target
);
1333 seq
= end_ifcvt_sequence (if_info
);
1337 emit_insn_before_setloc (seq
, if_info
->jump
,
1338 INSN_LOCATION (if_info
->insn_a
));
1348 /* Convert "if (test) x = 0;" to "x &= -(test == 0);" */
1351 noce_try_store_flag_mask (struct noce_if_info
*if_info
)
1357 if ((if_info
->branch_cost
>= 2
1358 || STORE_FLAG_VALUE
== -1)
1359 && ((if_info
->a
== const0_rtx
1360 && rtx_equal_p (if_info
->b
, if_info
->x
))
1361 || ((reversep
= (reversed_comparison_code (if_info
->cond
,
1364 && if_info
->b
== const0_rtx
1365 && rtx_equal_p (if_info
->a
, if_info
->x
))))
1368 target
= noce_emit_store_flag (if_info
,
1369 gen_reg_rtx (GET_MODE (if_info
->x
)),
1372 target
= expand_simple_binop (GET_MODE (if_info
->x
), AND
,
1374 target
, if_info
->x
, 0,
1379 if (target
!= if_info
->x
)
1380 noce_emit_move_insn (if_info
->x
, target
);
1382 seq
= end_ifcvt_sequence (if_info
);
1386 emit_insn_before_setloc (seq
, if_info
->jump
,
1387 INSN_LOCATION (if_info
->insn_a
));
1397 /* Helper function for noce_try_cmove and noce_try_cmove_arith. */
1400 noce_emit_cmove (struct noce_if_info
*if_info
, rtx x
, enum rtx_code code
,
1401 rtx cmp_a
, rtx cmp_b
, rtx vfalse
, rtx vtrue
)
1403 rtx target ATTRIBUTE_UNUSED
;
1404 int unsignedp ATTRIBUTE_UNUSED
;
1406 /* If earliest == jump, try to build the cmove insn directly.
1407 This is helpful when combine has created some complex condition
1408 (like for alpha's cmovlbs) that we can't hope to regenerate
1409 through the normal interface. */
1411 if (if_info
->cond_earliest
== if_info
->jump
)
1415 tmp
= gen_rtx_fmt_ee (code
, GET_MODE (if_info
->cond
), cmp_a
, cmp_b
);
1416 tmp
= gen_rtx_IF_THEN_ELSE (GET_MODE (x
), tmp
, vtrue
, vfalse
);
1417 tmp
= gen_rtx_SET (VOIDmode
, x
, tmp
);
1420 tmp
= emit_insn (tmp
);
1422 if (recog_memoized (tmp
) >= 0)
1434 /* Don't even try if the comparison operands are weird. */
1435 if (! general_operand (cmp_a
, GET_MODE (cmp_a
))
1436 || ! general_operand (cmp_b
, GET_MODE (cmp_b
)))
1439 #if HAVE_conditional_move
1440 unsignedp
= (code
== LTU
|| code
== GEU
1441 || code
== LEU
|| code
== GTU
);
1443 target
= emit_conditional_move (x
, code
, cmp_a
, cmp_b
, VOIDmode
,
1444 vtrue
, vfalse
, GET_MODE (x
),
1449 /* We might be faced with a situation like:
1452 vtrue = (subreg:M (reg:N VTRUE) BYTE)
1453 vfalse = (subreg:M (reg:N VFALSE) BYTE)
1455 We can't do a conditional move in mode M, but it's possible that we
1456 could do a conditional move in mode N instead and take a subreg of
1459 If we can't create new pseudos, though, don't bother. */
1460 if (reload_completed
)
1463 if (GET_CODE (vtrue
) == SUBREG
&& GET_CODE (vfalse
) == SUBREG
)
1465 rtx reg_vtrue
= SUBREG_REG (vtrue
);
1466 rtx reg_vfalse
= SUBREG_REG (vfalse
);
1467 unsigned int byte_vtrue
= SUBREG_BYTE (vtrue
);
1468 unsigned int byte_vfalse
= SUBREG_BYTE (vfalse
);
1469 rtx promoted_target
;
1471 if (GET_MODE (reg_vtrue
) != GET_MODE (reg_vfalse
)
1472 || byte_vtrue
!= byte_vfalse
1473 || (SUBREG_PROMOTED_VAR_P (vtrue
)
1474 != SUBREG_PROMOTED_VAR_P (vfalse
))
1475 || (SUBREG_PROMOTED_GET (vtrue
)
1476 != SUBREG_PROMOTED_GET (vfalse
)))
1479 promoted_target
= gen_reg_rtx (GET_MODE (reg_vtrue
));
1481 target
= emit_conditional_move (promoted_target
, code
, cmp_a
, cmp_b
,
1482 VOIDmode
, reg_vtrue
, reg_vfalse
,
1483 GET_MODE (reg_vtrue
), unsignedp
);
1484 /* Nope, couldn't do it in that mode either. */
1488 target
= gen_rtx_SUBREG (GET_MODE (vtrue
), promoted_target
, byte_vtrue
);
1489 SUBREG_PROMOTED_VAR_P (target
) = SUBREG_PROMOTED_VAR_P (vtrue
);
1490 SUBREG_PROMOTED_SET (target
, SUBREG_PROMOTED_GET (vtrue
));
1491 emit_move_insn (x
, target
);
1497 /* We'll never get here, as noce_process_if_block doesn't call the
1498 functions involved. Ifdef code, however, should be discouraged
1499 because it leads to typos in the code not selected. However,
1500 emit_conditional_move won't exist either. */
1505 /* Try only simple constants and registers here. More complex cases
1506 are handled in noce_try_cmove_arith after noce_try_store_flag_arith
1507 has had a go at it. */
1510 noce_try_cmove (struct noce_if_info
*if_info
)
1515 if ((CONSTANT_P (if_info
->a
) || register_operand (if_info
->a
, VOIDmode
))
1516 && (CONSTANT_P (if_info
->b
) || register_operand (if_info
->b
, VOIDmode
)))
1520 code
= GET_CODE (if_info
->cond
);
1521 target
= noce_emit_cmove (if_info
, if_info
->x
, code
,
1522 XEXP (if_info
->cond
, 0),
1523 XEXP (if_info
->cond
, 1),
1524 if_info
->a
, if_info
->b
);
1528 if (target
!= if_info
->x
)
1529 noce_emit_move_insn (if_info
->x
, target
);
1531 seq
= end_ifcvt_sequence (if_info
);
1535 emit_insn_before_setloc (seq
, if_info
->jump
,
1536 INSN_LOCATION (if_info
->insn_a
));
1549 /* Try more complex cases involving conditional_move. */
1552 noce_try_cmove_arith (struct noce_if_info
*if_info
)
1564 /* A conditional move from two memory sources is equivalent to a
1565 conditional on their addresses followed by a load. Don't do this
1566 early because it'll screw alias analysis. Note that we've
1567 already checked for no side effects. */
1568 /* ??? FIXME: Magic number 5. */
1569 if (cse_not_expected
1570 && MEM_P (a
) && MEM_P (b
)
1571 && MEM_ADDR_SPACE (a
) == MEM_ADDR_SPACE (b
)
1572 && if_info
->branch_cost
>= 5)
1574 enum machine_mode address_mode
= get_address_mode (a
);
1578 x
= gen_reg_rtx (address_mode
);
1582 /* ??? We could handle this if we knew that a load from A or B could
1583 not trap or fault. This is also true if we've already loaded
1584 from the address along the path from ENTRY. */
1585 else if (may_trap_or_fault_p (a
) || may_trap_or_fault_p (b
))
1588 /* if (test) x = a + b; else x = c - d;
1595 code
= GET_CODE (if_info
->cond
);
1596 insn_a
= if_info
->insn_a
;
1597 insn_b
= if_info
->insn_b
;
1599 /* Total insn_rtx_cost should be smaller than branch cost. Exit
1600 if insn_rtx_cost can't be estimated. */
1604 = insn_rtx_cost (PATTERN (insn_a
),
1605 optimize_bb_for_speed_p (BLOCK_FOR_INSN (insn_a
)));
1606 if (insn_cost
== 0 || insn_cost
> COSTS_N_INSNS (if_info
->branch_cost
))
1615 += insn_rtx_cost (PATTERN (insn_b
),
1616 optimize_bb_for_speed_p (BLOCK_FOR_INSN (insn_b
)));
1617 if (insn_cost
== 0 || insn_cost
> COSTS_N_INSNS (if_info
->branch_cost
))
1621 /* Possibly rearrange operands to make things come out more natural. */
1622 if (reversed_comparison_code (if_info
->cond
, if_info
->jump
) != UNKNOWN
)
1625 if (rtx_equal_p (b
, x
))
1627 else if (general_operand (b
, GET_MODE (b
)))
1632 code
= reversed_comparison_code (if_info
->cond
, if_info
->jump
);
1633 tmp
= a
, a
= b
, b
= tmp
;
1634 tmp
= insn_a
, insn_a
= insn_b
, insn_b
= tmp
;
1643 /* If either operand is complex, load it into a register first.
1644 The best way to do this is to copy the original insn. In this
1645 way we preserve any clobbers etc that the insn may have had.
1646 This is of course not possible in the IS_MEM case. */
1647 if (! general_operand (a
, GET_MODE (a
)))
1653 tmp
= gen_reg_rtx (GET_MODE (a
));
1654 tmp
= emit_insn (gen_rtx_SET (VOIDmode
, tmp
, a
));
1657 goto end_seq_and_fail
;
1660 a
= gen_reg_rtx (GET_MODE (a
));
1661 tmp
= copy_rtx (insn_a
);
1662 set
= single_set (tmp
);
1664 tmp
= emit_insn (PATTERN (tmp
));
1666 if (recog_memoized (tmp
) < 0)
1667 goto end_seq_and_fail
;
1669 if (! general_operand (b
, GET_MODE (b
)))
1675 tmp
= gen_reg_rtx (GET_MODE (b
));
1676 tmp
= gen_rtx_SET (VOIDmode
, tmp
, b
);
1679 goto end_seq_and_fail
;
1682 b
= gen_reg_rtx (GET_MODE (b
));
1683 tmp
= copy_rtx (insn_b
);
1684 set
= single_set (tmp
);
1686 tmp
= PATTERN (tmp
);
1689 /* If insn to set up A clobbers any registers B depends on, try to
1690 swap insn that sets up A with the one that sets up B. If even
1691 that doesn't help, punt. */
1692 last
= get_last_insn ();
1693 if (last
&& modified_in_p (orig_b
, last
))
1695 tmp
= emit_insn_before (tmp
, get_insns ());
1696 if (modified_in_p (orig_a
, tmp
))
1697 goto end_seq_and_fail
;
1700 tmp
= emit_insn (tmp
);
1702 if (recog_memoized (tmp
) < 0)
1703 goto end_seq_and_fail
;
1706 target
= noce_emit_cmove (if_info
, x
, code
, XEXP (if_info
->cond
, 0),
1707 XEXP (if_info
->cond
, 1), a
, b
);
1710 goto end_seq_and_fail
;
1712 /* If we're handling a memory for above, emit the load now. */
1715 tmp
= gen_rtx_MEM (GET_MODE (if_info
->x
), target
);
1717 /* Copy over flags as appropriate. */
1718 if (MEM_VOLATILE_P (if_info
->a
) || MEM_VOLATILE_P (if_info
->b
))
1719 MEM_VOLATILE_P (tmp
) = 1;
1720 if (MEM_ALIAS_SET (if_info
->a
) == MEM_ALIAS_SET (if_info
->b
))
1721 set_mem_alias_set (tmp
, MEM_ALIAS_SET (if_info
->a
));
1723 MIN (MEM_ALIGN (if_info
->a
), MEM_ALIGN (if_info
->b
)));
1725 gcc_assert (MEM_ADDR_SPACE (if_info
->a
) == MEM_ADDR_SPACE (if_info
->b
));
1726 set_mem_addr_space (tmp
, MEM_ADDR_SPACE (if_info
->a
));
1728 noce_emit_move_insn (if_info
->x
, tmp
);
1730 else if (target
!= x
)
1731 noce_emit_move_insn (x
, target
);
1733 tmp
= end_ifcvt_sequence (if_info
);
1737 emit_insn_before_setloc (tmp
, if_info
->jump
, INSN_LOCATION (if_info
->insn_a
));
1745 /* For most cases, the simplified condition we found is the best
1746 choice, but this is not the case for the min/max/abs transforms.
1747 For these we wish to know that it is A or B in the condition. */
1750 noce_get_alt_condition (struct noce_if_info
*if_info
, rtx target
,
1753 rtx cond
, set
, insn
;
1756 /* If target is already mentioned in the known condition, return it. */
1757 if (reg_mentioned_p (target
, if_info
->cond
))
1759 *earliest
= if_info
->cond_earliest
;
1760 return if_info
->cond
;
1763 set
= pc_set (if_info
->jump
);
1764 cond
= XEXP (SET_SRC (set
), 0);
1766 = GET_CODE (XEXP (SET_SRC (set
), 2)) == LABEL_REF
1767 && XEXP (XEXP (SET_SRC (set
), 2), 0) == JUMP_LABEL (if_info
->jump
);
1768 if (if_info
->then_else_reversed
)
1771 /* If we're looking for a constant, try to make the conditional
1772 have that constant in it. There are two reasons why it may
1773 not have the constant we want:
1775 1. GCC may have needed to put the constant in a register, because
1776 the target can't compare directly against that constant. For
1777 this case, we look for a SET immediately before the comparison
1778 that puts a constant in that register.
1780 2. GCC may have canonicalized the conditional, for example
1781 replacing "if x < 4" with "if x <= 3". We can undo that (or
1782 make equivalent types of changes) to get the constants we need
1783 if they're off by one in the right direction. */
1785 if (CONST_INT_P (target
))
1787 enum rtx_code code
= GET_CODE (if_info
->cond
);
1788 rtx op_a
= XEXP (if_info
->cond
, 0);
1789 rtx op_b
= XEXP (if_info
->cond
, 1);
1792 /* First, look to see if we put a constant in a register. */
1793 prev_insn
= prev_nonnote_insn (if_info
->cond_earliest
);
1795 && BLOCK_FOR_INSN (prev_insn
)
1796 == BLOCK_FOR_INSN (if_info
->cond_earliest
)
1797 && INSN_P (prev_insn
)
1798 && GET_CODE (PATTERN (prev_insn
)) == SET
)
1800 rtx src
= find_reg_equal_equiv_note (prev_insn
);
1802 src
= SET_SRC (PATTERN (prev_insn
));
1803 if (CONST_INT_P (src
))
1805 if (rtx_equal_p (op_a
, SET_DEST (PATTERN (prev_insn
))))
1807 else if (rtx_equal_p (op_b
, SET_DEST (PATTERN (prev_insn
))))
1810 if (CONST_INT_P (op_a
))
1815 code
= swap_condition (code
);
1820 /* Now, look to see if we can get the right constant by
1821 adjusting the conditional. */
1822 if (CONST_INT_P (op_b
))
1824 HOST_WIDE_INT desired_val
= INTVAL (target
);
1825 HOST_WIDE_INT actual_val
= INTVAL (op_b
);
1830 if (actual_val
== desired_val
+ 1)
1833 op_b
= GEN_INT (desired_val
);
1837 if (actual_val
== desired_val
- 1)
1840 op_b
= GEN_INT (desired_val
);
1844 if (actual_val
== desired_val
- 1)
1847 op_b
= GEN_INT (desired_val
);
1851 if (actual_val
== desired_val
+ 1)
1854 op_b
= GEN_INT (desired_val
);
1862 /* If we made any changes, generate a new conditional that is
1863 equivalent to what we started with, but has the right
1865 if (code
!= GET_CODE (if_info
->cond
)
1866 || op_a
!= XEXP (if_info
->cond
, 0)
1867 || op_b
!= XEXP (if_info
->cond
, 1))
1869 cond
= gen_rtx_fmt_ee (code
, GET_MODE (cond
), op_a
, op_b
);
1870 *earliest
= if_info
->cond_earliest
;
1875 cond
= canonicalize_condition (if_info
->jump
, cond
, reverse
,
1876 earliest
, target
, false, true);
1877 if (! cond
|| ! reg_mentioned_p (target
, cond
))
1880 /* We almost certainly searched back to a different place.
1881 Need to re-verify correct lifetimes. */
1883 /* X may not be mentioned in the range (cond_earliest, jump]. */
1884 for (insn
= if_info
->jump
; insn
!= *earliest
; insn
= PREV_INSN (insn
))
1885 if (INSN_P (insn
) && reg_overlap_mentioned_p (if_info
->x
, PATTERN (insn
)))
1888 /* A and B may not be modified in the range [cond_earliest, jump). */
1889 for (insn
= *earliest
; insn
!= if_info
->jump
; insn
= NEXT_INSN (insn
))
1891 && (modified_in_p (if_info
->a
, insn
)
1892 || modified_in_p (if_info
->b
, insn
)))
1898 /* Convert "if (a < b) x = a; else x = b;" to "x = min(a, b);", etc. */
1901 noce_try_minmax (struct noce_if_info
*if_info
)
1903 rtx cond
, earliest
, target
, seq
;
1904 enum rtx_code code
, op
;
1907 /* ??? Reject modes with NaNs or signed zeros since we don't know how
1908 they will be resolved with an SMIN/SMAX. It wouldn't be too hard
1909 to get the target to tell us... */
1910 if (HONOR_SIGNED_ZEROS (GET_MODE (if_info
->x
))
1911 || HONOR_NANS (GET_MODE (if_info
->x
)))
1914 cond
= noce_get_alt_condition (if_info
, if_info
->a
, &earliest
);
1918 /* Verify the condition is of the form we expect, and canonicalize
1919 the comparison code. */
1920 code
= GET_CODE (cond
);
1921 if (rtx_equal_p (XEXP (cond
, 0), if_info
->a
))
1923 if (! rtx_equal_p (XEXP (cond
, 1), if_info
->b
))
1926 else if (rtx_equal_p (XEXP (cond
, 1), if_info
->a
))
1928 if (! rtx_equal_p (XEXP (cond
, 0), if_info
->b
))
1930 code
= swap_condition (code
);
1935 /* Determine what sort of operation this is. Note that the code is for
1936 a taken branch, so the code->operation mapping appears backwards. */
1969 target
= expand_simple_binop (GET_MODE (if_info
->x
), op
,
1970 if_info
->a
, if_info
->b
,
1971 if_info
->x
, unsignedp
, OPTAB_WIDEN
);
1977 if (target
!= if_info
->x
)
1978 noce_emit_move_insn (if_info
->x
, target
);
1980 seq
= end_ifcvt_sequence (if_info
);
1984 emit_insn_before_setloc (seq
, if_info
->jump
, INSN_LOCATION (if_info
->insn_a
));
1985 if_info
->cond
= cond
;
1986 if_info
->cond_earliest
= earliest
;
1991 /* Convert "if (a < 0) x = -a; else x = a;" to "x = abs(a);",
1992 "if (a < 0) x = ~a; else x = a;" to "x = one_cmpl_abs(a);",
1996 noce_try_abs (struct noce_if_info
*if_info
)
1998 rtx cond
, earliest
, target
, seq
, a
, b
, c
;
2000 bool one_cmpl
= false;
2002 /* Reject modes with signed zeros. */
2003 if (HONOR_SIGNED_ZEROS (GET_MODE (if_info
->x
)))
2006 /* Recognize A and B as constituting an ABS or NABS. The canonical
2007 form is a branch around the negation, taken when the object is the
2008 first operand of a comparison against 0 that evaluates to true. */
2011 if (GET_CODE (a
) == NEG
&& rtx_equal_p (XEXP (a
, 0), b
))
2013 else if (GET_CODE (b
) == NEG
&& rtx_equal_p (XEXP (b
, 0), a
))
2015 c
= a
; a
= b
; b
= c
;
2018 else if (GET_CODE (a
) == NOT
&& rtx_equal_p (XEXP (a
, 0), b
))
2023 else if (GET_CODE (b
) == NOT
&& rtx_equal_p (XEXP (b
, 0), a
))
2025 c
= a
; a
= b
; b
= c
;
2032 cond
= noce_get_alt_condition (if_info
, b
, &earliest
);
2036 /* Verify the condition is of the form we expect. */
2037 if (rtx_equal_p (XEXP (cond
, 0), b
))
2039 else if (rtx_equal_p (XEXP (cond
, 1), b
))
2047 /* Verify that C is zero. Search one step backward for a
2048 REG_EQUAL note or a simple source if necessary. */
2051 rtx set
, insn
= prev_nonnote_insn (earliest
);
2053 && BLOCK_FOR_INSN (insn
) == BLOCK_FOR_INSN (earliest
)
2054 && (set
= single_set (insn
))
2055 && rtx_equal_p (SET_DEST (set
), c
))
2057 rtx note
= find_reg_equal_equiv_note (insn
);
2067 && GET_CODE (XEXP (c
, 0)) == SYMBOL_REF
2068 && CONSTANT_POOL_ADDRESS_P (XEXP (c
, 0)))
2069 c
= get_pool_constant (XEXP (c
, 0));
2071 /* Work around funny ideas get_condition has wrt canonicalization.
2072 Note that these rtx constants are known to be CONST_INT, and
2073 therefore imply integer comparisons. */
2074 if (c
== constm1_rtx
&& GET_CODE (cond
) == GT
)
2076 else if (c
== const1_rtx
&& GET_CODE (cond
) == LT
)
2078 else if (c
!= CONST0_RTX (GET_MODE (b
)))
2081 /* Determine what sort of operation this is. */
2082 switch (GET_CODE (cond
))
2101 target
= expand_one_cmpl_abs_nojump (GET_MODE (if_info
->x
), b
,
2104 target
= expand_abs_nojump (GET_MODE (if_info
->x
), b
, if_info
->x
, 1);
2106 /* ??? It's a quandary whether cmove would be better here, especially
2107 for integers. Perhaps combine will clean things up. */
2108 if (target
&& negate
)
2111 target
= expand_simple_unop (GET_MODE (target
), NOT
, target
,
2114 target
= expand_simple_unop (GET_MODE (target
), NEG
, target
,
2124 if (target
!= if_info
->x
)
2125 noce_emit_move_insn (if_info
->x
, target
);
2127 seq
= end_ifcvt_sequence (if_info
);
2131 emit_insn_before_setloc (seq
, if_info
->jump
, INSN_LOCATION (if_info
->insn_a
));
2132 if_info
->cond
= cond
;
2133 if_info
->cond_earliest
= earliest
;
2138 /* Convert "if (m < 0) x = b; else x = 0;" to "x = (m >> C) & b;". */
2141 noce_try_sign_mask (struct noce_if_info
*if_info
)
2143 rtx cond
, t
, m
, c
, seq
;
2144 enum machine_mode mode
;
2146 bool t_unconditional
;
2148 cond
= if_info
->cond
;
2149 code
= GET_CODE (cond
);
2154 if (if_info
->a
== const0_rtx
)
2156 if ((code
== LT
&& c
== const0_rtx
)
2157 || (code
== LE
&& c
== constm1_rtx
))
2160 else if (if_info
->b
== const0_rtx
)
2162 if ((code
== GE
&& c
== const0_rtx
)
2163 || (code
== GT
&& c
== constm1_rtx
))
2167 if (! t
|| side_effects_p (t
))
2170 /* We currently don't handle different modes. */
2171 mode
= GET_MODE (t
);
2172 if (GET_MODE (m
) != mode
)
2175 /* This is only profitable if T is unconditionally executed/evaluated in the
2176 original insn sequence or T is cheap. The former happens if B is the
2177 non-zero (T) value and if INSN_B was taken from TEST_BB, or there was no
2178 INSN_B which can happen for e.g. conditional stores to memory. For the
2179 cost computation use the block TEST_BB where the evaluation will end up
2180 after the transformation. */
2183 && (if_info
->insn_b
== NULL_RTX
2184 || BLOCK_FOR_INSN (if_info
->insn_b
) == if_info
->test_bb
));
2185 if (!(t_unconditional
2186 || (set_src_cost (t
, optimize_bb_for_speed_p (if_info
->test_bb
))
2187 < COSTS_N_INSNS (2))))
2191 /* Use emit_store_flag to generate "m < 0 ? -1 : 0" instead of expanding
2192 "(signed) m >> 31" directly. This benefits targets with specialized
2193 insns to obtain the signmask, but still uses ashr_optab otherwise. */
2194 m
= emit_store_flag (gen_reg_rtx (mode
), LT
, m
, const0_rtx
, mode
, 0, -1);
2195 t
= m
? expand_binop (mode
, and_optab
, m
, t
, NULL_RTX
, 0, OPTAB_DIRECT
)
2204 noce_emit_move_insn (if_info
->x
, t
);
2206 seq
= end_ifcvt_sequence (if_info
);
2210 emit_insn_before_setloc (seq
, if_info
->jump
, INSN_LOCATION (if_info
->insn_a
));
2215 /* Optimize away "if (x & C) x |= C" and similar bit manipulation
2219 noce_try_bitop (struct noce_if_info
*if_info
)
2221 rtx cond
, x
, a
, result
, seq
;
2222 enum machine_mode mode
;
2227 cond
= if_info
->cond
;
2228 code
= GET_CODE (cond
);
2230 /* Check for no else condition. */
2231 if (! rtx_equal_p (x
, if_info
->b
))
2234 /* Check for a suitable condition. */
2235 if (code
!= NE
&& code
!= EQ
)
2237 if (XEXP (cond
, 1) != const0_rtx
)
2239 cond
= XEXP (cond
, 0);
2241 /* ??? We could also handle AND here. */
2242 if (GET_CODE (cond
) == ZERO_EXTRACT
)
2244 if (XEXP (cond
, 1) != const1_rtx
2245 || !CONST_INT_P (XEXP (cond
, 2))
2246 || ! rtx_equal_p (x
, XEXP (cond
, 0)))
2248 bitnum
= INTVAL (XEXP (cond
, 2));
2249 mode
= GET_MODE (x
);
2250 if (BITS_BIG_ENDIAN
)
2251 bitnum
= GET_MODE_BITSIZE (mode
) - 1 - bitnum
;
2252 if (bitnum
< 0 || bitnum
>= HOST_BITS_PER_WIDE_INT
)
2259 if (GET_CODE (a
) == IOR
|| GET_CODE (a
) == XOR
)
2261 /* Check for "if (X & C) x = x op C". */
2262 if (! rtx_equal_p (x
, XEXP (a
, 0))
2263 || !CONST_INT_P (XEXP (a
, 1))
2264 || (INTVAL (XEXP (a
, 1)) & GET_MODE_MASK (mode
))
2265 != (unsigned HOST_WIDE_INT
) 1 << bitnum
)
2268 /* if ((x & C) == 0) x |= C; is transformed to x |= C. */
2269 /* if ((x & C) != 0) x |= C; is transformed to nothing. */
2270 if (GET_CODE (a
) == IOR
)
2271 result
= (code
== NE
) ? a
: NULL_RTX
;
2272 else if (code
== NE
)
2274 /* if ((x & C) == 0) x ^= C; is transformed to x |= C. */
2275 result
= gen_int_mode ((HOST_WIDE_INT
) 1 << bitnum
, mode
);
2276 result
= simplify_gen_binary (IOR
, mode
, x
, result
);
2280 /* if ((x & C) != 0) x ^= C; is transformed to x &= ~C. */
2281 result
= gen_int_mode (~((HOST_WIDE_INT
) 1 << bitnum
), mode
);
2282 result
= simplify_gen_binary (AND
, mode
, x
, result
);
2285 else if (GET_CODE (a
) == AND
)
2287 /* Check for "if (X & C) x &= ~C". */
2288 if (! rtx_equal_p (x
, XEXP (a
, 0))
2289 || !CONST_INT_P (XEXP (a
, 1))
2290 || (INTVAL (XEXP (a
, 1)) & GET_MODE_MASK (mode
))
2291 != (~((HOST_WIDE_INT
) 1 << bitnum
) & GET_MODE_MASK (mode
)))
2294 /* if ((x & C) == 0) x &= ~C; is transformed to nothing. */
2295 /* if ((x & C) != 0) x &= ~C; is transformed to x &= ~C. */
2296 result
= (code
== EQ
) ? a
: NULL_RTX
;
2304 noce_emit_move_insn (x
, result
);
2305 seq
= end_ifcvt_sequence (if_info
);
2309 emit_insn_before_setloc (seq
, if_info
->jump
,
2310 INSN_LOCATION (if_info
->insn_a
));
2316 /* Similar to get_condition, only the resulting condition must be
2317 valid at JUMP, instead of at EARLIEST.
2319 If THEN_ELSE_REVERSED is true, the fallthrough does not go to the
2320 THEN block of the caller, and we have to reverse the condition. */
2323 noce_get_condition (rtx jump
, rtx
*earliest
, bool then_else_reversed
)
2328 if (! any_condjump_p (jump
))
2331 set
= pc_set (jump
);
2333 /* If this branches to JUMP_LABEL when the condition is false,
2334 reverse the condition. */
2335 reverse
= (GET_CODE (XEXP (SET_SRC (set
), 2)) == LABEL_REF
2336 && XEXP (XEXP (SET_SRC (set
), 2), 0) == JUMP_LABEL (jump
));
2338 /* We may have to reverse because the caller's if block is not canonical,
2339 i.e. the THEN block isn't the fallthrough block for the TEST block
2340 (see find_if_header). */
2341 if (then_else_reversed
)
2344 /* If the condition variable is a register and is MODE_INT, accept it. */
2346 cond
= XEXP (SET_SRC (set
), 0);
2347 tmp
= XEXP (cond
, 0);
2348 if (REG_P (tmp
) && GET_MODE_CLASS (GET_MODE (tmp
)) == MODE_INT
2349 && (GET_MODE (tmp
) != BImode
2350 || !targetm
.small_register_classes_for_mode_p (BImode
)))
2355 cond
= gen_rtx_fmt_ee (reverse_condition (GET_CODE (cond
)),
2356 GET_MODE (cond
), tmp
, XEXP (cond
, 1));
2360 /* Otherwise, fall back on canonicalize_condition to do the dirty
2361 work of manipulating MODE_CC values and COMPARE rtx codes. */
2362 tmp
= canonicalize_condition (jump
, cond
, reverse
, earliest
,
2363 NULL_RTX
, false, true);
2365 /* We don't handle side-effects in the condition, like handling
2366 REG_INC notes and making sure no duplicate conditions are emitted. */
2367 if (tmp
!= NULL_RTX
&& side_effects_p (tmp
))
2373 /* Return true if OP is ok for if-then-else processing. */
2376 noce_operand_ok (const_rtx op
)
2378 if (side_effects_p (op
))
2381 /* We special-case memories, so handle any of them with
2382 no address side effects. */
2384 return ! side_effects_p (XEXP (op
, 0));
2386 return ! may_trap_p (op
);
2389 /* Return true if a write into MEM may trap or fault. */
2392 noce_mem_write_may_trap_or_fault_p (const_rtx mem
)
2396 if (MEM_READONLY_P (mem
))
2399 if (may_trap_or_fault_p (mem
))
2402 addr
= XEXP (mem
, 0);
2404 /* Call target hook to avoid the effects of -fpic etc.... */
2405 addr
= targetm
.delegitimize_address (addr
);
2408 switch (GET_CODE (addr
))
2416 addr
= XEXP (addr
, 0);
2420 addr
= XEXP (addr
, 1);
2423 if (CONST_INT_P (XEXP (addr
, 1)))
2424 addr
= XEXP (addr
, 0);
2431 if (SYMBOL_REF_DECL (addr
)
2432 && decl_readonly_section (SYMBOL_REF_DECL (addr
), 0))
2442 /* Return whether we can use store speculation for MEM. TOP_BB is the
2443 basic block above the conditional block where we are considering
2444 doing the speculative store. We look for whether MEM is set
2445 unconditionally later in the function. */
2448 noce_can_store_speculate_p (basic_block top_bb
, const_rtx mem
)
2450 basic_block dominator
;
2452 for (dominator
= get_immediate_dominator (CDI_POST_DOMINATORS
, top_bb
);
2454 dominator
= get_immediate_dominator (CDI_POST_DOMINATORS
, dominator
))
2458 FOR_BB_INSNS (dominator
, insn
)
2460 /* If we see something that might be a memory barrier, we
2461 have to stop looking. Even if the MEM is set later in
2462 the function, we still don't want to set it
2463 unconditionally before the barrier. */
2465 && (volatile_insn_p (PATTERN (insn
))
2466 || (CALL_P (insn
) && (!RTL_CONST_CALL_P (insn
)))))
2469 if (memory_must_be_modified_in_insn_p (mem
, insn
))
2471 if (modified_in_p (XEXP (mem
, 0), insn
))
2480 /* Given a simple IF-THEN-JOIN or IF-THEN-ELSE-JOIN block, attempt to convert
2481 it without using conditional execution. Return TRUE if we were successful
2482 at converting the block. */
2485 noce_process_if_block (struct noce_if_info
*if_info
)
2487 basic_block test_bb
= if_info
->test_bb
; /* test block */
2488 basic_block then_bb
= if_info
->then_bb
; /* THEN */
2489 basic_block else_bb
= if_info
->else_bb
; /* ELSE or NULL */
2490 basic_block join_bb
= if_info
->join_bb
; /* JOIN */
2491 rtx jump
= if_info
->jump
;
2492 rtx cond
= if_info
->cond
;
2495 rtx orig_x
, x
, a
, b
;
2497 /* We're looking for patterns of the form
2499 (1) if (...) x = a; else x = b;
2500 (2) x = b; if (...) x = a;
2501 (3) if (...) x = a; // as if with an initial x = x.
2503 The later patterns require jumps to be more expensive.
2505 ??? For future expansion, look for multiple X in such patterns. */
2507 /* Look for one of the potential sets. */
2508 insn_a
= first_active_insn (then_bb
);
2510 || insn_a
!= last_active_insn (then_bb
, FALSE
)
2511 || (set_a
= single_set (insn_a
)) == NULL_RTX
)
2514 x
= SET_DEST (set_a
);
2515 a
= SET_SRC (set_a
);
2517 /* Look for the other potential set. Make sure we've got equivalent
2519 /* ??? This is overconservative. Storing to two different mems is
2520 as easy as conditionally computing the address. Storing to a
2521 single mem merely requires a scratch memory to use as one of the
2522 destination addresses; often the memory immediately below the
2523 stack pointer is available for this. */
2527 insn_b
= first_active_insn (else_bb
);
2529 || insn_b
!= last_active_insn (else_bb
, FALSE
)
2530 || (set_b
= single_set (insn_b
)) == NULL_RTX
2531 || ! rtx_interchangeable_p (x
, SET_DEST (set_b
)))
2536 insn_b
= prev_nonnote_nondebug_insn (if_info
->cond_earliest
);
2537 /* We're going to be moving the evaluation of B down from above
2538 COND_EARLIEST to JUMP. Make sure the relevant data is still
2541 || BLOCK_FOR_INSN (insn_b
) != BLOCK_FOR_INSN (if_info
->cond_earliest
)
2542 || !NONJUMP_INSN_P (insn_b
)
2543 || (set_b
= single_set (insn_b
)) == NULL_RTX
2544 || ! rtx_interchangeable_p (x
, SET_DEST (set_b
))
2545 || ! noce_operand_ok (SET_SRC (set_b
))
2546 || reg_overlap_mentioned_p (x
, SET_SRC (set_b
))
2547 || modified_between_p (SET_SRC (set_b
), insn_b
, jump
)
2548 /* Avoid extending the lifetime of hard registers on small
2549 register class machines. */
2550 || (REG_P (SET_SRC (set_b
))
2551 && HARD_REGISTER_P (SET_SRC (set_b
))
2552 && targetm
.small_register_classes_for_mode_p
2553 (GET_MODE (SET_SRC (set_b
))))
2554 /* Likewise with X. In particular this can happen when
2555 noce_get_condition looks farther back in the instruction
2556 stream than one might expect. */
2557 || reg_overlap_mentioned_p (x
, cond
)
2558 || reg_overlap_mentioned_p (x
, a
)
2559 || modified_between_p (x
, insn_b
, jump
))
2560 insn_b
= set_b
= NULL_RTX
;
2563 /* If x has side effects then only the if-then-else form is safe to
2564 convert. But even in that case we would need to restore any notes
2565 (such as REG_INC) at then end. That can be tricky if
2566 noce_emit_move_insn expands to more than one insn, so disable the
2567 optimization entirely for now if there are side effects. */
2568 if (side_effects_p (x
))
2571 b
= (set_b
? SET_SRC (set_b
) : x
);
2573 /* Only operate on register destinations, and even then avoid extending
2574 the lifetime of hard registers on small register class machines. */
2577 || (HARD_REGISTER_P (x
)
2578 && targetm
.small_register_classes_for_mode_p (GET_MODE (x
))))
2580 if (GET_MODE (x
) == BLKmode
)
2583 if (GET_CODE (x
) == ZERO_EXTRACT
2584 && (!CONST_INT_P (XEXP (x
, 1))
2585 || !CONST_INT_P (XEXP (x
, 2))))
2588 x
= gen_reg_rtx (GET_MODE (GET_CODE (x
) == STRICT_LOW_PART
2589 ? XEXP (x
, 0) : x
));
2592 /* Don't operate on sources that may trap or are volatile. */
2593 if (! noce_operand_ok (a
) || ! noce_operand_ok (b
))
2597 /* Set up the info block for our subroutines. */
2598 if_info
->insn_a
= insn_a
;
2599 if_info
->insn_b
= insn_b
;
2604 /* Try optimizations in some approximation of a useful order. */
2605 /* ??? Should first look to see if X is live incoming at all. If it
2606 isn't, we don't need anything but an unconditional set. */
2608 /* Look and see if A and B are really the same. Avoid creating silly
2609 cmove constructs that no one will fix up later. */
2610 if (rtx_interchangeable_p (a
, b
))
2612 /* If we have an INSN_B, we don't have to create any new rtl. Just
2613 move the instruction that we already have. If we don't have an
2614 INSN_B, that means that A == X, and we've got a noop move. In
2615 that case don't do anything and let the code below delete INSN_A. */
2616 if (insn_b
&& else_bb
)
2620 if (else_bb
&& insn_b
== BB_END (else_bb
))
2621 SET_BB_END (else_bb
) = PREV_INSN (insn_b
);
2622 reorder_insns (insn_b
, insn_b
, PREV_INSN (jump
));
2624 /* If there was a REG_EQUAL note, delete it since it may have been
2625 true due to this insn being after a jump. */
2626 if ((note
= find_reg_note (insn_b
, REG_EQUAL
, NULL_RTX
)) != 0)
2627 remove_note (insn_b
, note
);
2631 /* If we have "x = b; if (...) x = a;", and x has side-effects, then
2632 x must be executed twice. */
2633 else if (insn_b
&& side_effects_p (orig_x
))
2640 if (!set_b
&& MEM_P (orig_x
))
2642 /* Disallow the "if (...) x = a;" form (implicit "else x = x;")
2643 for optimizations if writing to x may trap or fault,
2644 i.e. it's a memory other than a static var or a stack slot,
2645 is misaligned on strict aligned machines or is read-only. If
2646 x is a read-only memory, then the program is valid only if we
2647 avoid the store into it. If there are stores on both the
2648 THEN and ELSE arms, then we can go ahead with the conversion;
2649 either the program is broken, or the condition is always
2650 false such that the other memory is selected. */
2651 if (noce_mem_write_may_trap_or_fault_p (orig_x
))
2654 /* Avoid store speculation: given "if (...) x = a" where x is a
2655 MEM, we only want to do the store if x is always set
2656 somewhere in the function. This avoids cases like
2657 if (pthread_mutex_trylock(mutex))
2659 where we only want global_variable to be changed if the mutex
2660 is held. FIXME: This should ideally be expressed directly in
2662 if (!noce_can_store_speculate_p (test_bb
, orig_x
))
2666 if (noce_try_move (if_info
))
2668 if (noce_try_store_flag (if_info
))
2670 if (noce_try_bitop (if_info
))
2672 if (noce_try_minmax (if_info
))
2674 if (noce_try_abs (if_info
))
2676 if (HAVE_conditional_move
2677 && noce_try_cmove (if_info
))
2679 if (! targetm
.have_conditional_execution ())
2681 if (noce_try_store_flag_constants (if_info
))
2683 if (noce_try_addcc (if_info
))
2685 if (noce_try_store_flag_mask (if_info
))
2687 if (HAVE_conditional_move
2688 && noce_try_cmove_arith (if_info
))
2690 if (noce_try_sign_mask (if_info
))
2694 if (!else_bb
&& set_b
)
2696 insn_b
= set_b
= NULL_RTX
;
2705 /* If we used a temporary, fix it up now. */
2711 noce_emit_move_insn (orig_x
, x
);
2713 set_used_flags (orig_x
);
2714 unshare_all_rtl_in_chain (seq
);
2717 emit_insn_before_setloc (seq
, BB_END (test_bb
), INSN_LOCATION (insn_a
));
2720 /* The original THEN and ELSE blocks may now be removed. The test block
2721 must now jump to the join block. If the test block and the join block
2722 can be merged, do so. */
2725 delete_basic_block (else_bb
);
2729 remove_edge (find_edge (test_bb
, join_bb
));
2731 remove_edge (find_edge (then_bb
, join_bb
));
2732 redirect_edge_and_branch_force (single_succ_edge (test_bb
), join_bb
);
2733 delete_basic_block (then_bb
);
2736 if (can_merge_blocks_p (test_bb
, join_bb
))
2738 merge_blocks (test_bb
, join_bb
);
2742 num_updated_if_blocks
++;
2746 /* Check whether a block is suitable for conditional move conversion.
2747 Every insn must be a simple set of a register to a constant or a
2748 register. For each assignment, store the value in the pointer map
2749 VALS, keyed indexed by register pointer, then store the register
2750 pointer in REGS. COND is the condition we will test. */
2753 check_cond_move_block (basic_block bb
,
2754 hash_map
<rtx
, rtx
> *vals
,
2760 /* We can only handle simple jumps at the end of the basic block.
2761 It is almost impossible to update the CFG otherwise. */
2763 if (JUMP_P (insn
) && !onlyjump_p (insn
))
2766 FOR_BB_INSNS (bb
, insn
)
2770 if (!NONDEBUG_INSN_P (insn
) || JUMP_P (insn
))
2772 set
= single_set (insn
);
2776 dest
= SET_DEST (set
);
2777 src
= SET_SRC (set
);
2779 || (HARD_REGISTER_P (dest
)
2780 && targetm
.small_register_classes_for_mode_p (GET_MODE (dest
))))
2783 if (!CONSTANT_P (src
) && !register_operand (src
, VOIDmode
))
2786 if (side_effects_p (src
) || side_effects_p (dest
))
2789 if (may_trap_p (src
) || may_trap_p (dest
))
2792 /* Don't try to handle this if the source register was
2793 modified earlier in the block. */
2796 || (GET_CODE (src
) == SUBREG
&& REG_P (SUBREG_REG (src
))
2797 && vals
->get (SUBREG_REG (src
))))
2800 /* Don't try to handle this if the destination register was
2801 modified earlier in the block. */
2802 if (vals
->get (dest
))
2805 /* Don't try to handle this if the condition uses the
2806 destination register. */
2807 if (reg_overlap_mentioned_p (dest
, cond
))
2810 /* Don't try to handle this if the source register is modified
2811 later in the block. */
2812 if (!CONSTANT_P (src
)
2813 && modified_between_p (src
, insn
, NEXT_INSN (BB_END (bb
))))
2816 vals
->put (dest
, src
);
2818 regs
->safe_push (dest
);
2824 /* Given a basic block BB suitable for conditional move conversion,
2825 a condition COND, and pointer maps THEN_VALS and ELSE_VALS containing
2826 the register values depending on COND, emit the insns in the block as
2827 conditional moves. If ELSE_BLOCK is true, THEN_BB was already
2828 processed. The caller has started a sequence for the conversion.
2829 Return true if successful, false if something goes wrong. */
2832 cond_move_convert_if_block (struct noce_if_info
*if_infop
,
2833 basic_block bb
, rtx cond
,
2834 hash_map
<rtx
, rtx
> *then_vals
,
2835 hash_map
<rtx
, rtx
> *else_vals
,
2839 rtx insn
, cond_arg0
, cond_arg1
;
2841 code
= GET_CODE (cond
);
2842 cond_arg0
= XEXP (cond
, 0);
2843 cond_arg1
= XEXP (cond
, 1);
2845 FOR_BB_INSNS (bb
, insn
)
2847 rtx set
, target
, dest
, t
, e
;
2849 /* ??? Maybe emit conditional debug insn? */
2850 if (!NONDEBUG_INSN_P (insn
) || JUMP_P (insn
))
2852 set
= single_set (insn
);
2853 gcc_assert (set
&& REG_P (SET_DEST (set
)));
2855 dest
= SET_DEST (set
);
2857 rtx
*then_slot
= then_vals
->get (dest
);
2858 rtx
*else_slot
= else_vals
->get (dest
);
2859 t
= then_slot
? *then_slot
: NULL_RTX
;
2860 e
= else_slot
? *else_slot
: NULL_RTX
;
2864 /* If this register was set in the then block, we already
2865 handled this case there. */
2878 target
= noce_emit_cmove (if_infop
, dest
, code
, cond_arg0
, cond_arg1
,
2884 noce_emit_move_insn (dest
, target
);
2890 /* Given a simple IF-THEN-JOIN or IF-THEN-ELSE-JOIN block, attempt to convert
2891 it using only conditional moves. Return TRUE if we were successful at
2892 converting the block. */
2895 cond_move_process_if_block (struct noce_if_info
*if_info
)
2897 basic_block test_bb
= if_info
->test_bb
;
2898 basic_block then_bb
= if_info
->then_bb
;
2899 basic_block else_bb
= if_info
->else_bb
;
2900 basic_block join_bb
= if_info
->join_bb
;
2901 rtx jump
= if_info
->jump
;
2902 rtx cond
= if_info
->cond
;
2906 vec
<rtx
> then_regs
= vNULL
;
2907 vec
<rtx
> else_regs
= vNULL
;
2909 int success_p
= FALSE
;
2911 /* Build a mapping for each block to the value used for each
2913 hash_map
<rtx
, rtx
> then_vals
;
2914 hash_map
<rtx
, rtx
> else_vals
;
2916 /* Make sure the blocks are suitable. */
2917 if (!check_cond_move_block (then_bb
, &then_vals
, &then_regs
, cond
)
2919 && !check_cond_move_block (else_bb
, &else_vals
, &else_regs
, cond
)))
2922 /* Make sure the blocks can be used together. If the same register
2923 is set in both blocks, and is not set to a constant in both
2924 cases, then both blocks must set it to the same register. We
2925 have already verified that if it is set to a register, that the
2926 source register does not change after the assignment. Also count
2927 the number of registers set in only one of the blocks. */
2929 FOR_EACH_VEC_ELT (then_regs
, i
, reg
)
2931 rtx
*then_slot
= then_vals
.get (reg
);
2932 rtx
*else_slot
= else_vals
.get (reg
);
2934 gcc_checking_assert (then_slot
);
2939 rtx then_val
= *then_slot
;
2940 rtx else_val
= *else_slot
;
2941 if (!CONSTANT_P (then_val
) && !CONSTANT_P (else_val
)
2942 && !rtx_equal_p (then_val
, else_val
))
2947 /* Finish off c for MAX_CONDITIONAL_EXECUTE. */
2948 FOR_EACH_VEC_ELT (else_regs
, i
, reg
)
2950 gcc_checking_assert (else_vals
.get (reg
));
2951 if (!then_vals
.get (reg
))
2955 /* Make sure it is reasonable to convert this block. What matters
2956 is the number of assignments currently made in only one of the
2957 branches, since if we convert we are going to always execute
2959 if (c
> MAX_CONDITIONAL_EXECUTE
)
2962 /* Try to emit the conditional moves. First do the then block,
2963 then do anything left in the else blocks. */
2965 if (!cond_move_convert_if_block (if_info
, then_bb
, cond
,
2966 &then_vals
, &else_vals
, false)
2968 && !cond_move_convert_if_block (if_info
, else_bb
, cond
,
2969 &then_vals
, &else_vals
, true)))
2974 seq
= end_ifcvt_sequence (if_info
);
2978 loc_insn
= first_active_insn (then_bb
);
2981 loc_insn
= first_active_insn (else_bb
);
2982 gcc_assert (loc_insn
);
2984 emit_insn_before_setloc (seq
, jump
, INSN_LOCATION (loc_insn
));
2988 delete_basic_block (else_bb
);
2992 remove_edge (find_edge (test_bb
, join_bb
));
2994 remove_edge (find_edge (then_bb
, join_bb
));
2995 redirect_edge_and_branch_force (single_succ_edge (test_bb
), join_bb
);
2996 delete_basic_block (then_bb
);
2999 if (can_merge_blocks_p (test_bb
, join_bb
))
3001 merge_blocks (test_bb
, join_bb
);
3005 num_updated_if_blocks
++;
3010 then_regs
.release ();
3011 else_regs
.release ();
3016 /* Determine if a given basic block heads a simple IF-THEN-JOIN or an
3017 IF-THEN-ELSE-JOIN block.
3019 If so, we'll try to convert the insns to not require the branch,
3020 using only transformations that do not require conditional execution.
3022 Return TRUE if we were successful at converting the block. */
3025 noce_find_if_block (basic_block test_bb
, edge then_edge
, edge else_edge
,
3028 basic_block then_bb
, else_bb
, join_bb
;
3029 bool then_else_reversed
= false;
3032 struct noce_if_info if_info
;
3034 /* We only ever should get here before reload. */
3035 gcc_assert (!reload_completed
);
3037 /* Recognize an IF-THEN-ELSE-JOIN block. */
3038 if (single_pred_p (then_edge
->dest
)
3039 && single_succ_p (then_edge
->dest
)
3040 && single_pred_p (else_edge
->dest
)
3041 && single_succ_p (else_edge
->dest
)
3042 && single_succ (then_edge
->dest
) == single_succ (else_edge
->dest
))
3044 then_bb
= then_edge
->dest
;
3045 else_bb
= else_edge
->dest
;
3046 join_bb
= single_succ (then_bb
);
3048 /* Recognize an IF-THEN-JOIN block. */
3049 else if (single_pred_p (then_edge
->dest
)
3050 && single_succ_p (then_edge
->dest
)
3051 && single_succ (then_edge
->dest
) == else_edge
->dest
)
3053 then_bb
= then_edge
->dest
;
3054 else_bb
= NULL_BLOCK
;
3055 join_bb
= else_edge
->dest
;
3057 /* Recognize an IF-ELSE-JOIN block. We can have those because the order
3058 of basic blocks in cfglayout mode does not matter, so the fallthrough
3059 edge can go to any basic block (and not just to bb->next_bb, like in
3061 else if (single_pred_p (else_edge
->dest
)
3062 && single_succ_p (else_edge
->dest
)
3063 && single_succ (else_edge
->dest
) == then_edge
->dest
)
3065 /* The noce transformations do not apply to IF-ELSE-JOIN blocks.
3066 To make this work, we have to invert the THEN and ELSE blocks
3067 and reverse the jump condition. */
3068 then_bb
= else_edge
->dest
;
3069 else_bb
= NULL_BLOCK
;
3070 join_bb
= single_succ (then_bb
);
3071 then_else_reversed
= true;
3074 /* Not a form we can handle. */
3077 /* The edges of the THEN and ELSE blocks cannot have complex edges. */
3078 if (single_succ_edge (then_bb
)->flags
& EDGE_COMPLEX
)
3081 && single_succ_edge (else_bb
)->flags
& EDGE_COMPLEX
)
3084 num_possible_if_blocks
++;
3089 "\nIF-THEN%s-JOIN block found, pass %d, test %d, then %d",
3090 (else_bb
) ? "-ELSE" : "",
3091 pass
, test_bb
->index
, then_bb
->index
);
3094 fprintf (dump_file
, ", else %d", else_bb
->index
);
3096 fprintf (dump_file
, ", join %d\n", join_bb
->index
);
3099 /* If the conditional jump is more than just a conditional
3100 jump, then we can not do if-conversion on this block. */
3101 jump
= BB_END (test_bb
);
3102 if (! onlyjump_p (jump
))
3105 /* If this is not a standard conditional jump, we can't parse it. */
3106 cond
= noce_get_condition (jump
, &cond_earliest
, then_else_reversed
);
3110 /* We must be comparing objects whose modes imply the size. */
3111 if (GET_MODE (XEXP (cond
, 0)) == BLKmode
)
3114 /* Initialize an IF_INFO struct to pass around. */
3115 memset (&if_info
, 0, sizeof if_info
);
3116 if_info
.test_bb
= test_bb
;
3117 if_info
.then_bb
= then_bb
;
3118 if_info
.else_bb
= else_bb
;
3119 if_info
.join_bb
= join_bb
;
3120 if_info
.cond
= cond
;
3121 if_info
.cond_earliest
= cond_earliest
;
3122 if_info
.jump
= jump
;
3123 if_info
.then_else_reversed
= then_else_reversed
;
3124 if_info
.branch_cost
= BRANCH_COST (optimize_bb_for_speed_p (test_bb
),
3125 predictable_edge_p (then_edge
));
3127 /* Do the real work. */
3129 if (noce_process_if_block (&if_info
))
3132 if (HAVE_conditional_move
3133 && cond_move_process_if_block (&if_info
))
3140 /* Merge the blocks and mark for local life update. */
3143 merge_if_block (struct ce_if_block
* ce_info
)
3145 basic_block test_bb
= ce_info
->test_bb
; /* last test block */
3146 basic_block then_bb
= ce_info
->then_bb
; /* THEN */
3147 basic_block else_bb
= ce_info
->else_bb
; /* ELSE or NULL */
3148 basic_block join_bb
= ce_info
->join_bb
; /* join block */
3149 basic_block combo_bb
;
3151 /* All block merging is done into the lower block numbers. */
3154 df_set_bb_dirty (test_bb
);
3156 /* Merge any basic blocks to handle && and || subtests. Each of
3157 the blocks are on the fallthru path from the predecessor block. */
3158 if (ce_info
->num_multiple_test_blocks
> 0)
3160 basic_block bb
= test_bb
;
3161 basic_block last_test_bb
= ce_info
->last_test_bb
;
3162 basic_block fallthru
= block_fallthru (bb
);
3167 fallthru
= block_fallthru (bb
);
3168 merge_blocks (combo_bb
, bb
);
3171 while (bb
!= last_test_bb
);
3174 /* Merge TEST block into THEN block. Normally the THEN block won't have a
3175 label, but it might if there were || tests. That label's count should be
3176 zero, and it normally should be removed. */
3180 /* If THEN_BB has no successors, then there's a BARRIER after it.
3181 If COMBO_BB has more than one successor (THEN_BB), then that BARRIER
3182 is no longer needed, and in fact it is incorrect to leave it in
3184 if (EDGE_COUNT (then_bb
->succs
) == 0
3185 && EDGE_COUNT (combo_bb
->succs
) > 1)
3187 rtx end
= NEXT_INSN (BB_END (then_bb
));
3188 while (end
&& NOTE_P (end
) && !NOTE_INSN_BASIC_BLOCK_P (end
))
3189 end
= NEXT_INSN (end
);
3191 if (end
&& BARRIER_P (end
))
3194 merge_blocks (combo_bb
, then_bb
);
3198 /* The ELSE block, if it existed, had a label. That label count
3199 will almost always be zero, but odd things can happen when labels
3200 get their addresses taken. */
3203 /* If ELSE_BB has no successors, then there's a BARRIER after it.
3204 If COMBO_BB has more than one successor (ELSE_BB), then that BARRIER
3205 is no longer needed, and in fact it is incorrect to leave it in
3207 if (EDGE_COUNT (else_bb
->succs
) == 0
3208 && EDGE_COUNT (combo_bb
->succs
) > 1)
3210 rtx end
= NEXT_INSN (BB_END (else_bb
));
3211 while (end
&& NOTE_P (end
) && !NOTE_INSN_BASIC_BLOCK_P (end
))
3212 end
= NEXT_INSN (end
);
3214 if (end
&& BARRIER_P (end
))
3217 merge_blocks (combo_bb
, else_bb
);
3221 /* If there was no join block reported, that means it was not adjacent
3222 to the others, and so we cannot merge them. */
3226 rtx last
= BB_END (combo_bb
);
3228 /* The outgoing edge for the current COMBO block should already
3229 be correct. Verify this. */
3230 if (EDGE_COUNT (combo_bb
->succs
) == 0)
3231 gcc_assert (find_reg_note (last
, REG_NORETURN
, NULL
)
3232 || (NONJUMP_INSN_P (last
)
3233 && GET_CODE (PATTERN (last
)) == TRAP_IF
3234 && (TRAP_CONDITION (PATTERN (last
))
3235 == const_true_rtx
)));
3238 /* There should still be something at the end of the THEN or ELSE
3239 blocks taking us to our final destination. */
3240 gcc_assert (JUMP_P (last
)
3241 || (EDGE_SUCC (combo_bb
, 0)->dest
3242 == EXIT_BLOCK_PTR_FOR_FN (cfun
)
3244 && SIBLING_CALL_P (last
))
3245 || ((EDGE_SUCC (combo_bb
, 0)->flags
& EDGE_EH
)
3246 && can_throw_internal (last
)));
3249 /* The JOIN block may have had quite a number of other predecessors too.
3250 Since we've already merged the TEST, THEN and ELSE blocks, we should
3251 have only one remaining edge from our if-then-else diamond. If there
3252 is more than one remaining edge, it must come from elsewhere. There
3253 may be zero incoming edges if the THEN block didn't actually join
3254 back up (as with a call to a non-return function). */
3255 else if (EDGE_COUNT (join_bb
->preds
) < 2
3256 && join_bb
!= EXIT_BLOCK_PTR_FOR_FN (cfun
))
3258 /* We can merge the JOIN cleanly and update the dataflow try
3259 again on this pass.*/
3260 merge_blocks (combo_bb
, join_bb
);
3265 /* We cannot merge the JOIN. */
3267 /* The outgoing edge for the current COMBO block should already
3268 be correct. Verify this. */
3269 gcc_assert (single_succ_p (combo_bb
)
3270 && single_succ (combo_bb
) == join_bb
);
3272 /* Remove the jump and cruft from the end of the COMBO block. */
3273 if (join_bb
!= EXIT_BLOCK_PTR_FOR_FN (cfun
))
3274 tidy_fallthru_edge (single_succ_edge (combo_bb
));
3277 num_updated_if_blocks
++;
3280 /* Find a block ending in a simple IF condition and try to transform it
3281 in some way. When converting a multi-block condition, put the new code
3282 in the first such block and delete the rest. Return a pointer to this
3283 first block if some transformation was done. Return NULL otherwise. */
3286 find_if_header (basic_block test_bb
, int pass
)
3288 ce_if_block ce_info
;
3292 /* The kind of block we're looking for has exactly two successors. */
3293 if (EDGE_COUNT (test_bb
->succs
) != 2)
3296 then_edge
= EDGE_SUCC (test_bb
, 0);
3297 else_edge
= EDGE_SUCC (test_bb
, 1);
3299 if (df_get_bb_dirty (then_edge
->dest
))
3301 if (df_get_bb_dirty (else_edge
->dest
))
3304 /* Neither edge should be abnormal. */
3305 if ((then_edge
->flags
& EDGE_COMPLEX
)
3306 || (else_edge
->flags
& EDGE_COMPLEX
))
3309 /* Nor exit the loop. */
3310 if ((then_edge
->flags
& EDGE_LOOP_EXIT
)
3311 || (else_edge
->flags
& EDGE_LOOP_EXIT
))
3314 /* The THEN edge is canonically the one that falls through. */
3315 if (then_edge
->flags
& EDGE_FALLTHRU
)
3317 else if (else_edge
->flags
& EDGE_FALLTHRU
)
3320 else_edge
= then_edge
;
3324 /* Otherwise this must be a multiway branch of some sort. */
3327 memset (&ce_info
, 0, sizeof (ce_info
));
3328 ce_info
.test_bb
= test_bb
;
3329 ce_info
.then_bb
= then_edge
->dest
;
3330 ce_info
.else_bb
= else_edge
->dest
;
3331 ce_info
.pass
= pass
;
3333 #ifdef IFCVT_MACHDEP_INIT
3334 IFCVT_MACHDEP_INIT (&ce_info
);
3337 if (!reload_completed
3338 && noce_find_if_block (test_bb
, then_edge
, else_edge
, pass
))
3341 if (reload_completed
3342 && targetm
.have_conditional_execution ()
3343 && cond_exec_find_if_block (&ce_info
))
3347 && optab_handler (ctrap_optab
, word_mode
) != CODE_FOR_nothing
3348 && find_cond_trap (test_bb
, then_edge
, else_edge
))
3351 if (dom_info_state (CDI_POST_DOMINATORS
) >= DOM_NO_FAST_QUERY
3352 && (reload_completed
|| !targetm
.have_conditional_execution ()))
3354 if (find_if_case_1 (test_bb
, then_edge
, else_edge
))
3356 if (find_if_case_2 (test_bb
, then_edge
, else_edge
))
3364 fprintf (dump_file
, "Conversion succeeded on pass %d.\n", pass
);
3365 /* Set this so we continue looking. */
3366 cond_exec_changed_p
= TRUE
;
3367 return ce_info
.test_bb
;
3370 /* Return true if a block has two edges, one of which falls through to the next
3371 block, and the other jumps to a specific block, so that we can tell if the
3372 block is part of an && test or an || test. Returns either -1 or the number
3373 of non-note, non-jump, non-USE/CLOBBER insns in the block. */
3376 block_jumps_and_fallthru_p (basic_block cur_bb
, basic_block target_bb
)
3379 int fallthru_p
= FALSE
;
3386 if (!cur_bb
|| !target_bb
)
3389 /* If no edges, obviously it doesn't jump or fallthru. */
3390 if (EDGE_COUNT (cur_bb
->succs
) == 0)
3393 FOR_EACH_EDGE (cur_edge
, ei
, cur_bb
->succs
)
3395 if (cur_edge
->flags
& EDGE_COMPLEX
)
3396 /* Anything complex isn't what we want. */
3399 else if (cur_edge
->flags
& EDGE_FALLTHRU
)
3402 else if (cur_edge
->dest
== target_bb
)
3409 if ((jump_p
& fallthru_p
) == 0)
3412 /* Don't allow calls in the block, since this is used to group && and ||
3413 together for conditional execution support. ??? we should support
3414 conditional execution support across calls for IA-64 some day, but
3415 for now it makes the code simpler. */
3416 end
= BB_END (cur_bb
);
3417 insn
= BB_HEAD (cur_bb
);
3419 while (insn
!= NULL_RTX
)
3426 && !DEBUG_INSN_P (insn
)
3427 && GET_CODE (PATTERN (insn
)) != USE
3428 && GET_CODE (PATTERN (insn
)) != CLOBBER
)
3434 insn
= NEXT_INSN (insn
);
3440 /* Determine if a given basic block heads a simple IF-THEN or IF-THEN-ELSE
3441 block. If so, we'll try to convert the insns to not require the branch.
3442 Return TRUE if we were successful at converting the block. */
3445 cond_exec_find_if_block (struct ce_if_block
* ce_info
)
3447 basic_block test_bb
= ce_info
->test_bb
;
3448 basic_block then_bb
= ce_info
->then_bb
;
3449 basic_block else_bb
= ce_info
->else_bb
;
3450 basic_block join_bb
= NULL_BLOCK
;
3455 ce_info
->last_test_bb
= test_bb
;
3457 /* We only ever should get here after reload,
3458 and if we have conditional execution. */
3459 gcc_assert (reload_completed
&& targetm
.have_conditional_execution ());
3461 /* Discover if any fall through predecessors of the current test basic block
3462 were && tests (which jump to the else block) or || tests (which jump to
3464 if (single_pred_p (test_bb
)
3465 && single_pred_edge (test_bb
)->flags
== EDGE_FALLTHRU
)
3467 basic_block bb
= single_pred (test_bb
);
3468 basic_block target_bb
;
3469 int max_insns
= MAX_CONDITIONAL_EXECUTE
;
3472 /* Determine if the preceding block is an && or || block. */
3473 if ((n_insns
= block_jumps_and_fallthru_p (bb
, else_bb
)) >= 0)
3475 ce_info
->and_and_p
= TRUE
;
3476 target_bb
= else_bb
;
3478 else if ((n_insns
= block_jumps_and_fallthru_p (bb
, then_bb
)) >= 0)
3480 ce_info
->and_and_p
= FALSE
;
3481 target_bb
= then_bb
;
3484 target_bb
= NULL_BLOCK
;
3486 if (target_bb
&& n_insns
<= max_insns
)
3488 int total_insns
= 0;
3491 ce_info
->last_test_bb
= test_bb
;
3493 /* Found at least one && or || block, look for more. */
3496 ce_info
->test_bb
= test_bb
= bb
;
3497 total_insns
+= n_insns
;
3500 if (!single_pred_p (bb
))
3503 bb
= single_pred (bb
);
3504 n_insns
= block_jumps_and_fallthru_p (bb
, target_bb
);
3506 while (n_insns
>= 0 && (total_insns
+ n_insns
) <= max_insns
);
3508 ce_info
->num_multiple_test_blocks
= blocks
;
3509 ce_info
->num_multiple_test_insns
= total_insns
;
3511 if (ce_info
->and_and_p
)
3512 ce_info
->num_and_and_blocks
= blocks
;
3514 ce_info
->num_or_or_blocks
= blocks
;
3518 /* The THEN block of an IF-THEN combo must have exactly one predecessor,
3519 other than any || blocks which jump to the THEN block. */
3520 if ((EDGE_COUNT (then_bb
->preds
) - ce_info
->num_or_or_blocks
) != 1)
3523 /* The edges of the THEN and ELSE blocks cannot have complex edges. */
3524 FOR_EACH_EDGE (cur_edge
, ei
, then_bb
->preds
)
3526 if (cur_edge
->flags
& EDGE_COMPLEX
)
3530 FOR_EACH_EDGE (cur_edge
, ei
, else_bb
->preds
)
3532 if (cur_edge
->flags
& EDGE_COMPLEX
)
3536 /* The THEN block of an IF-THEN combo must have zero or one successors. */
3537 if (EDGE_COUNT (then_bb
->succs
) > 0
3538 && (!single_succ_p (then_bb
)
3539 || (single_succ_edge (then_bb
)->flags
& EDGE_COMPLEX
)
3540 || (epilogue_completed
3541 && tablejump_p (BB_END (then_bb
), NULL
, NULL
))))
3544 /* If the THEN block has no successors, conditional execution can still
3545 make a conditional call. Don't do this unless the ELSE block has
3546 only one incoming edge -- the CFG manipulation is too ugly otherwise.
3547 Check for the last insn of the THEN block being an indirect jump, which
3548 is listed as not having any successors, but confuses the rest of the CE
3549 code processing. ??? we should fix this in the future. */
3550 if (EDGE_COUNT (then_bb
->succs
) == 0)
3552 if (single_pred_p (else_bb
) && else_bb
!= EXIT_BLOCK_PTR_FOR_FN (cfun
))
3554 rtx last_insn
= BB_END (then_bb
);
3557 && NOTE_P (last_insn
)
3558 && last_insn
!= BB_HEAD (then_bb
))
3559 last_insn
= PREV_INSN (last_insn
);
3562 && JUMP_P (last_insn
)
3563 && ! simplejump_p (last_insn
))
3567 else_bb
= NULL_BLOCK
;
3573 /* If the THEN block's successor is the other edge out of the TEST block,
3574 then we have an IF-THEN combo without an ELSE. */
3575 else if (single_succ (then_bb
) == else_bb
)
3578 else_bb
= NULL_BLOCK
;
3581 /* If the THEN and ELSE block meet in a subsequent block, and the ELSE
3582 has exactly one predecessor and one successor, and the outgoing edge
3583 is not complex, then we have an IF-THEN-ELSE combo. */
3584 else if (single_succ_p (else_bb
)
3585 && single_succ (then_bb
) == single_succ (else_bb
)
3586 && single_pred_p (else_bb
)
3587 && !(single_succ_edge (else_bb
)->flags
& EDGE_COMPLEX
)
3588 && !(epilogue_completed
3589 && tablejump_p (BB_END (else_bb
), NULL
, NULL
)))
3590 join_bb
= single_succ (else_bb
);
3592 /* Otherwise it is not an IF-THEN or IF-THEN-ELSE combination. */
3596 num_possible_if_blocks
++;
3601 "\nIF-THEN%s block found, pass %d, start block %d "
3602 "[insn %d], then %d [%d]",
3603 (else_bb
) ? "-ELSE" : "",
3606 BB_HEAD (test_bb
) ? (int)INSN_UID (BB_HEAD (test_bb
)) : -1,
3608 BB_HEAD (then_bb
) ? (int)INSN_UID (BB_HEAD (then_bb
)) : -1);
3611 fprintf (dump_file
, ", else %d [%d]",
3613 BB_HEAD (else_bb
) ? (int)INSN_UID (BB_HEAD (else_bb
)) : -1);
3615 fprintf (dump_file
, ", join %d [%d]",
3617 BB_HEAD (join_bb
) ? (int)INSN_UID (BB_HEAD (join_bb
)) : -1);
3619 if (ce_info
->num_multiple_test_blocks
> 0)
3620 fprintf (dump_file
, ", %d %s block%s last test %d [%d]",
3621 ce_info
->num_multiple_test_blocks
,
3622 (ce_info
->and_and_p
) ? "&&" : "||",
3623 (ce_info
->num_multiple_test_blocks
== 1) ? "" : "s",
3624 ce_info
->last_test_bb
->index
,
3625 ((BB_HEAD (ce_info
->last_test_bb
))
3626 ? (int)INSN_UID (BB_HEAD (ce_info
->last_test_bb
))
3629 fputc ('\n', dump_file
);
3632 /* Make sure IF, THEN, and ELSE, blocks are adjacent. Actually, we get the
3633 first condition for free, since we've already asserted that there's a
3634 fallthru edge from IF to THEN. Likewise for the && and || blocks, since
3635 we checked the FALLTHRU flag, those are already adjacent to the last IF
3637 /* ??? As an enhancement, move the ELSE block. Have to deal with
3638 BLOCK notes, if by no other means than backing out the merge if they
3639 exist. Sticky enough I don't want to think about it now. */
3641 if (else_bb
&& (next
= next
->next_bb
) != else_bb
)
3643 if ((next
= next
->next_bb
) != join_bb
3644 && join_bb
!= EXIT_BLOCK_PTR_FOR_FN (cfun
))
3652 /* Do the real work. */
3654 ce_info
->else_bb
= else_bb
;
3655 ce_info
->join_bb
= join_bb
;
3657 /* If we have && and || tests, try to first handle combining the && and ||
3658 tests into the conditional code, and if that fails, go back and handle
3659 it without the && and ||, which at present handles the && case if there
3660 was no ELSE block. */
3661 if (cond_exec_process_if_block (ce_info
, TRUE
))
3664 if (ce_info
->num_multiple_test_blocks
)
3668 if (cond_exec_process_if_block (ce_info
, FALSE
))
3675 /* Convert a branch over a trap, or a branch
3676 to a trap, into a conditional trap. */
3679 find_cond_trap (basic_block test_bb
, edge then_edge
, edge else_edge
)
3681 basic_block then_bb
= then_edge
->dest
;
3682 basic_block else_bb
= else_edge
->dest
;
3683 basic_block other_bb
, trap_bb
;
3684 rtx trap
, jump
, cond
, cond_earliest
, seq
;
3687 /* Locate the block with the trap instruction. */
3688 /* ??? While we look for no successors, we really ought to allow
3689 EH successors. Need to fix merge_if_block for that to work. */
3690 if ((trap
= block_has_only_trap (then_bb
)) != NULL
)
3691 trap_bb
= then_bb
, other_bb
= else_bb
;
3692 else if ((trap
= block_has_only_trap (else_bb
)) != NULL
)
3693 trap_bb
= else_bb
, other_bb
= then_bb
;
3699 fprintf (dump_file
, "\nTRAP-IF block found, start %d, trap %d\n",
3700 test_bb
->index
, trap_bb
->index
);
3703 /* If this is not a standard conditional jump, we can't parse it. */
3704 jump
= BB_END (test_bb
);
3705 cond
= noce_get_condition (jump
, &cond_earliest
, false);
3709 /* If the conditional jump is more than just a conditional jump, then
3710 we can not do if-conversion on this block. */
3711 if (! onlyjump_p (jump
))
3714 /* We must be comparing objects whose modes imply the size. */
3715 if (GET_MODE (XEXP (cond
, 0)) == BLKmode
)
3718 /* Reverse the comparison code, if necessary. */
3719 code
= GET_CODE (cond
);
3720 if (then_bb
== trap_bb
)
3722 code
= reversed_comparison_code (cond
, jump
);
3723 if (code
== UNKNOWN
)
3727 /* Attempt to generate the conditional trap. */
3728 seq
= gen_cond_trap (code
, copy_rtx (XEXP (cond
, 0)),
3729 copy_rtx (XEXP (cond
, 1)),
3730 TRAP_CODE (PATTERN (trap
)));
3734 /* Emit the new insns before cond_earliest. */
3735 emit_insn_before_setloc (seq
, cond_earliest
, INSN_LOCATION (trap
));
3737 /* Delete the trap block if possible. */
3738 remove_edge (trap_bb
== then_bb
? then_edge
: else_edge
);
3739 df_set_bb_dirty (test_bb
);
3740 df_set_bb_dirty (then_bb
);
3741 df_set_bb_dirty (else_bb
);
3743 if (EDGE_COUNT (trap_bb
->preds
) == 0)
3745 delete_basic_block (trap_bb
);
3749 /* Wire together the blocks again. */
3750 if (current_ir_type () == IR_RTL_CFGLAYOUT
)
3751 single_succ_edge (test_bb
)->flags
|= EDGE_FALLTHRU
;
3752 else if (trap_bb
== then_bb
)
3756 lab
= JUMP_LABEL (jump
);
3757 newjump
= emit_jump_insn_after (gen_jump (lab
), jump
);
3758 LABEL_NUSES (lab
) += 1;
3759 JUMP_LABEL (newjump
) = lab
;
3760 emit_barrier_after (newjump
);
3764 if (can_merge_blocks_p (test_bb
, other_bb
))
3766 merge_blocks (test_bb
, other_bb
);
3770 num_updated_if_blocks
++;
3774 /* Subroutine of find_cond_trap: if BB contains only a trap insn,
3778 block_has_only_trap (basic_block bb
)
3782 /* We're not the exit block. */
3783 if (bb
== EXIT_BLOCK_PTR_FOR_FN (cfun
))
3786 /* The block must have no successors. */
3787 if (EDGE_COUNT (bb
->succs
) > 0)
3790 /* The only instruction in the THEN block must be the trap. */
3791 trap
= first_active_insn (bb
);
3792 if (! (trap
== BB_END (bb
)
3793 && GET_CODE (PATTERN (trap
)) == TRAP_IF
3794 && TRAP_CONDITION (PATTERN (trap
)) == const_true_rtx
))
3800 /* Look for IF-THEN-ELSE cases in which one of THEN or ELSE is
3801 transformable, but not necessarily the other. There need be no
3804 Return TRUE if we were successful at converting the block.
3806 Cases we'd like to look at:
3809 if (test) goto over; // x not live
3817 if (! test) goto label;
3820 if (test) goto E; // x not live
3834 (3) // This one's really only interesting for targets that can do
3835 // multiway branching, e.g. IA-64 BBB bundles. For other targets
3836 // it results in multiple branches on a cache line, which often
3837 // does not sit well with predictors.
3839 if (test1) goto E; // predicted not taken
3855 (A) Don't do (2) if the branch is predicted against the block we're
3856 eliminating. Do it anyway if we can eliminate a branch; this requires
3857 that the sole successor of the eliminated block postdominate the other
3860 (B) With CE, on (3) we can steal from both sides of the if, creating
3869 Again, this is most useful if J postdominates.
3871 (C) CE substitutes for helpful life information.
3873 (D) These heuristics need a lot of work. */
3875 /* Tests for case 1 above. */
3878 find_if_case_1 (basic_block test_bb
, edge then_edge
, edge else_edge
)
3880 basic_block then_bb
= then_edge
->dest
;
3881 basic_block else_bb
= else_edge
->dest
;
3883 int then_bb_index
, then_prob
;
3884 rtx else_target
= NULL_RTX
;
3886 /* If we are partitioning hot/cold basic blocks, we don't want to
3887 mess up unconditional or indirect jumps that cross between hot
3890 Basic block partitioning may result in some jumps that appear to
3891 be optimizable (or blocks that appear to be mergeable), but which really
3892 must be left untouched (they are required to make it safely across
3893 partition boundaries). See the comments at the top of
3894 bb-reorder.c:partition_hot_cold_basic_blocks for complete details. */
3896 if ((BB_END (then_bb
)
3897 && JUMP_P (BB_END (then_bb
))
3898 && CROSSING_JUMP_P (BB_END (then_bb
)))
3899 || (BB_END (test_bb
)
3900 && JUMP_P (BB_END (test_bb
))
3901 && CROSSING_JUMP_P (BB_END (test_bb
)))
3902 || (BB_END (else_bb
)
3903 && JUMP_P (BB_END (else_bb
))
3904 && CROSSING_JUMP_P (BB_END (else_bb
))))
3907 /* THEN has one successor. */
3908 if (!single_succ_p (then_bb
))
3911 /* THEN does not fall through, but is not strange either. */
3912 if (single_succ_edge (then_bb
)->flags
& (EDGE_COMPLEX
| EDGE_FALLTHRU
))
3915 /* THEN has one predecessor. */
3916 if (!single_pred_p (then_bb
))
3919 /* THEN must do something. */
3920 if (forwarder_block_p (then_bb
))
3923 num_possible_if_blocks
++;
3926 "\nIF-CASE-1 found, start %d, then %d\n",
3927 test_bb
->index
, then_bb
->index
);
3929 if (then_edge
->probability
)
3930 then_prob
= REG_BR_PROB_BASE
- then_edge
->probability
;
3932 then_prob
= REG_BR_PROB_BASE
/ 2;
3934 /* We're speculating from the THEN path, we want to make sure the cost
3935 of speculation is within reason. */
3936 if (! cheap_bb_rtx_cost_p (then_bb
, then_prob
,
3937 COSTS_N_INSNS (BRANCH_COST (optimize_bb_for_speed_p (then_edge
->src
),
3938 predictable_edge_p (then_edge
)))))
3941 if (else_bb
== EXIT_BLOCK_PTR_FOR_FN (cfun
))
3943 rtx jump
= BB_END (else_edge
->src
);
3944 gcc_assert (JUMP_P (jump
));
3945 else_target
= JUMP_LABEL (jump
);
3948 /* Registers set are dead, or are predicable. */
3949 if (! dead_or_predicable (test_bb
, then_bb
, else_bb
,
3950 single_succ_edge (then_bb
), 1))
3953 /* Conversion went ok, including moving the insns and fixing up the
3954 jump. Adjust the CFG to match. */
3956 /* We can avoid creating a new basic block if then_bb is immediately
3957 followed by else_bb, i.e. deleting then_bb allows test_bb to fall
3958 through to else_bb. */
3960 if (then_bb
->next_bb
== else_bb
3961 && then_bb
->prev_bb
== test_bb
3962 && else_bb
!= EXIT_BLOCK_PTR_FOR_FN (cfun
))
3964 redirect_edge_succ (FALLTHRU_EDGE (test_bb
), else_bb
);
3967 else if (else_bb
== EXIT_BLOCK_PTR_FOR_FN (cfun
))
3968 new_bb
= force_nonfallthru_and_redirect (FALLTHRU_EDGE (test_bb
),
3969 else_bb
, else_target
);
3971 new_bb
= redirect_edge_and_branch_force (FALLTHRU_EDGE (test_bb
),
3974 df_set_bb_dirty (test_bb
);
3975 df_set_bb_dirty (else_bb
);
3977 then_bb_index
= then_bb
->index
;
3978 delete_basic_block (then_bb
);
3980 /* Make rest of code believe that the newly created block is the THEN_BB
3981 block we removed. */
3984 df_bb_replace (then_bb_index
, new_bb
);
3985 /* This should have been done above via force_nonfallthru_and_redirect
3986 (possibly called from redirect_edge_and_branch_force). */
3987 gcc_checking_assert (BB_PARTITION (new_bb
) == BB_PARTITION (test_bb
));
3991 num_updated_if_blocks
++;
3996 /* Test for case 2 above. */
3999 find_if_case_2 (basic_block test_bb
, edge then_edge
, edge else_edge
)
4001 basic_block then_bb
= then_edge
->dest
;
4002 basic_block else_bb
= else_edge
->dest
;
4004 int then_prob
, else_prob
;
4006 /* We do not want to speculate (empty) loop latches. */
4008 && else_bb
->loop_father
->latch
== else_bb
)
4011 /* If we are partitioning hot/cold basic blocks, we don't want to
4012 mess up unconditional or indirect jumps that cross between hot
4015 Basic block partitioning may result in some jumps that appear to
4016 be optimizable (or blocks that appear to be mergeable), but which really
4017 must be left untouched (they are required to make it safely across
4018 partition boundaries). See the comments at the top of
4019 bb-reorder.c:partition_hot_cold_basic_blocks for complete details. */
4021 if ((BB_END (then_bb
)
4022 && JUMP_P (BB_END (then_bb
))
4023 && CROSSING_JUMP_P (BB_END (then_bb
)))
4024 || (BB_END (test_bb
)
4025 && JUMP_P (BB_END (test_bb
))
4026 && CROSSING_JUMP_P (BB_END (test_bb
)))
4027 || (BB_END (else_bb
)
4028 && JUMP_P (BB_END (else_bb
))
4029 && CROSSING_JUMP_P (BB_END (else_bb
))))
4032 /* ELSE has one successor. */
4033 if (!single_succ_p (else_bb
))
4036 else_succ
= single_succ_edge (else_bb
);
4038 /* ELSE outgoing edge is not complex. */
4039 if (else_succ
->flags
& EDGE_COMPLEX
)
4042 /* ELSE has one predecessor. */
4043 if (!single_pred_p (else_bb
))
4046 /* THEN is not EXIT. */
4047 if (then_bb
->index
< NUM_FIXED_BLOCKS
)
4050 if (else_edge
->probability
)
4052 else_prob
= else_edge
->probability
;
4053 then_prob
= REG_BR_PROB_BASE
- else_prob
;
4057 else_prob
= REG_BR_PROB_BASE
/ 2;
4058 then_prob
= REG_BR_PROB_BASE
/ 2;
4061 /* ELSE is predicted or SUCC(ELSE) postdominates THEN. */
4062 if (else_prob
> then_prob
)
4064 else if (else_succ
->dest
->index
< NUM_FIXED_BLOCKS
4065 || dominated_by_p (CDI_POST_DOMINATORS
, then_bb
,
4071 num_possible_if_blocks
++;
4074 "\nIF-CASE-2 found, start %d, else %d\n",
4075 test_bb
->index
, else_bb
->index
);
4077 /* We're speculating from the ELSE path, we want to make sure the cost
4078 of speculation is within reason. */
4079 if (! cheap_bb_rtx_cost_p (else_bb
, else_prob
,
4080 COSTS_N_INSNS (BRANCH_COST (optimize_bb_for_speed_p (else_edge
->src
),
4081 predictable_edge_p (else_edge
)))))
4084 /* Registers set are dead, or are predicable. */
4085 if (! dead_or_predicable (test_bb
, else_bb
, then_bb
, else_succ
, 0))
4088 /* Conversion went ok, including moving the insns and fixing up the
4089 jump. Adjust the CFG to match. */
4091 df_set_bb_dirty (test_bb
);
4092 df_set_bb_dirty (then_bb
);
4093 delete_basic_block (else_bb
);
4096 num_updated_if_blocks
++;
4098 /* ??? We may now fallthru from one of THEN's successors into a join
4099 block. Rerun cleanup_cfg? Examine things manually? Wait? */
4104 /* Used by the code above to perform the actual rtl transformations.
4105 Return TRUE if successful.
4107 TEST_BB is the block containing the conditional branch. MERGE_BB
4108 is the block containing the code to manipulate. DEST_EDGE is an
4109 edge representing a jump to the join block; after the conversion,
4110 TEST_BB should be branching to its destination.
4111 REVERSEP is true if the sense of the branch should be reversed. */
4114 dead_or_predicable (basic_block test_bb
, basic_block merge_bb
,
4115 basic_block other_bb
, edge dest_edge
, int reversep
)
4117 basic_block new_dest
= dest_edge
->dest
;
4118 rtx head
, end
, jump
, earliest
= NULL_RTX
, old_dest
;
4119 bitmap merge_set
= NULL
;
4120 /* Number of pending changes. */
4121 int n_validated_changes
= 0;
4122 rtx new_dest_label
= NULL_RTX
;
4124 jump
= BB_END (test_bb
);
4126 /* Find the extent of the real code in the merge block. */
4127 head
= BB_HEAD (merge_bb
);
4128 end
= BB_END (merge_bb
);
4130 while (DEBUG_INSN_P (end
) && end
!= head
)
4131 end
= PREV_INSN (end
);
4133 /* If merge_bb ends with a tablejump, predicating/moving insn's
4134 into test_bb and then deleting merge_bb will result in the jumptable
4135 that follows merge_bb being removed along with merge_bb and then we
4136 get an unresolved reference to the jumptable. */
4137 if (tablejump_p (end
, NULL
, NULL
))
4141 head
= NEXT_INSN (head
);
4142 while (DEBUG_INSN_P (head
) && head
!= end
)
4143 head
= NEXT_INSN (head
);
4148 head
= end
= NULL_RTX
;
4151 head
= NEXT_INSN (head
);
4152 while (DEBUG_INSN_P (head
) && head
!= end
)
4153 head
= NEXT_INSN (head
);
4158 if (!onlyjump_p (end
))
4162 head
= end
= NULL_RTX
;
4165 end
= PREV_INSN (end
);
4166 while (DEBUG_INSN_P (end
) && end
!= head
)
4167 end
= PREV_INSN (end
);
4170 /* Don't move frame-related insn across the conditional branch. This
4171 can lead to one of the paths of the branch having wrong unwind info. */
4172 if (epilogue_completed
)
4177 if (INSN_P (insn
) && RTX_FRAME_RELATED_P (insn
))
4181 insn
= NEXT_INSN (insn
);
4185 /* Disable handling dead code by conditional execution if the machine needs
4186 to do anything funny with the tests, etc. */
4187 #ifndef IFCVT_MODIFY_TESTS
4188 if (targetm
.have_conditional_execution ())
4190 /* In the conditional execution case, we have things easy. We know
4191 the condition is reversible. We don't have to check life info
4192 because we're going to conditionally execute the code anyway.
4193 All that's left is making sure the insns involved can actually
4198 cond
= cond_exec_get_condition (jump
);
4202 rtx note
= find_reg_note (jump
, REG_BR_PROB
, NULL_RTX
);
4203 int prob_val
= (note
? XINT (note
, 0) : -1);
4207 enum rtx_code rev
= reversed_comparison_code (cond
, jump
);
4210 cond
= gen_rtx_fmt_ee (rev
, GET_MODE (cond
), XEXP (cond
, 0),
4213 prob_val
= REG_BR_PROB_BASE
- prob_val
;
4216 if (cond_exec_process_insns (NULL
, head
, end
, cond
, prob_val
, 0)
4217 && verify_changes (0))
4218 n_validated_changes
= num_validated_changes ();
4226 /* If we allocated new pseudos (e.g. in the conditional move
4227 expander called from noce_emit_cmove), we must resize the
4229 if (max_regno
< max_reg_num ())
4230 max_regno
= max_reg_num ();
4232 /* Try the NCE path if the CE path did not result in any changes. */
4233 if (n_validated_changes
== 0)
4239 /* In the non-conditional execution case, we have to verify that there
4240 are no trapping operations, no calls, no references to memory, and
4241 that any registers modified are dead at the branch site. */
4243 if (!any_condjump_p (jump
))
4246 /* Find the extent of the conditional. */
4247 cond
= noce_get_condition (jump
, &earliest
, false);
4251 live
= BITMAP_ALLOC (®_obstack
);
4252 simulate_backwards_to_point (merge_bb
, live
, end
);
4253 success
= can_move_insns_across (head
, end
, earliest
, jump
,
4255 df_get_live_in (other_bb
), NULL
);
4260 /* Collect the set of registers set in MERGE_BB. */
4261 merge_set
= BITMAP_ALLOC (®_obstack
);
4263 FOR_BB_INSNS (merge_bb
, insn
)
4264 if (NONDEBUG_INSN_P (insn
))
4265 df_simulate_find_defs (insn
, merge_set
);
4267 #ifdef HAVE_simple_return
4268 /* If shrink-wrapping, disable this optimization when test_bb is
4269 the first basic block and merge_bb exits. The idea is to not
4270 move code setting up a return register as that may clobber a
4271 register used to pass function parameters, which then must be
4272 saved in caller-saved regs. A caller-saved reg requires the
4273 prologue, killing a shrink-wrap opportunity. */
4274 if ((flag_shrink_wrap
&& HAVE_simple_return
&& !epilogue_completed
)
4275 && ENTRY_BLOCK_PTR_FOR_FN (cfun
)->next_bb
== test_bb
4276 && single_succ_p (new_dest
)
4277 && single_succ (new_dest
) == EXIT_BLOCK_PTR_FOR_FN (cfun
)
4278 && bitmap_intersect_p (df_get_live_in (new_dest
), merge_set
))
4283 return_regs
= BITMAP_ALLOC (®_obstack
);
4285 /* Start off with the intersection of regs used to pass
4286 params and regs used to return values. */
4287 for (i
= 0; i
< FIRST_PSEUDO_REGISTER
; i
++)
4288 if (FUNCTION_ARG_REGNO_P (i
)
4289 && targetm
.calls
.function_value_regno_p (i
))
4290 bitmap_set_bit (return_regs
, INCOMING_REGNO (i
));
4292 bitmap_and_into (return_regs
,
4293 df_get_live_out (ENTRY_BLOCK_PTR_FOR_FN (cfun
)));
4294 bitmap_and_into (return_regs
,
4295 df_get_live_in (EXIT_BLOCK_PTR_FOR_FN (cfun
)));
4296 if (!bitmap_empty_p (return_regs
))
4298 FOR_BB_INSNS_REVERSE (new_dest
, insn
)
4299 if (NONDEBUG_INSN_P (insn
))
4303 /* If this insn sets any reg in return_regs, add all
4304 reg uses to the set of regs we're interested in. */
4305 FOR_EACH_INSN_DEF (def
, insn
)
4306 if (bitmap_bit_p (return_regs
, DF_REF_REGNO (def
)))
4308 df_simulate_uses (insn
, return_regs
);
4312 if (bitmap_intersect_p (merge_set
, return_regs
))
4314 BITMAP_FREE (return_regs
);
4315 BITMAP_FREE (merge_set
);
4319 BITMAP_FREE (return_regs
);
4325 /* We don't want to use normal invert_jump or redirect_jump because
4326 we don't want to delete_insn called. Also, we want to do our own
4327 change group management. */
4329 old_dest
= JUMP_LABEL (jump
);
4330 if (other_bb
!= new_dest
)
4332 if (JUMP_P (BB_END (dest_edge
->src
)))
4333 new_dest_label
= JUMP_LABEL (BB_END (dest_edge
->src
));
4334 else if (new_dest
== EXIT_BLOCK_PTR_FOR_FN (cfun
))
4335 new_dest_label
= ret_rtx
;
4337 new_dest_label
= block_label (new_dest
);
4340 ? ! invert_jump_1 (jump
, new_dest_label
)
4341 : ! redirect_jump_1 (jump
, new_dest_label
))
4345 if (verify_changes (n_validated_changes
))
4346 confirm_change_group ();
4350 if (other_bb
!= new_dest
)
4352 redirect_jump_2 (jump
, old_dest
, new_dest_label
, 0, reversep
);
4354 redirect_edge_succ (BRANCH_EDGE (test_bb
), new_dest
);
4357 gcov_type count
, probability
;
4358 count
= BRANCH_EDGE (test_bb
)->count
;
4359 BRANCH_EDGE (test_bb
)->count
= FALLTHRU_EDGE (test_bb
)->count
;
4360 FALLTHRU_EDGE (test_bb
)->count
= count
;
4361 probability
= BRANCH_EDGE (test_bb
)->probability
;
4362 BRANCH_EDGE (test_bb
)->probability
4363 = FALLTHRU_EDGE (test_bb
)->probability
;
4364 FALLTHRU_EDGE (test_bb
)->probability
= probability
;
4365 update_br_prob_note (test_bb
);
4369 /* Move the insns out of MERGE_BB to before the branch. */
4374 if (end
== BB_END (merge_bb
))
4375 SET_BB_END (merge_bb
) = PREV_INSN (head
);
4377 /* PR 21767: when moving insns above a conditional branch, the REG_EQUAL
4378 notes being moved might become invalid. */
4384 if (! INSN_P (insn
))
4386 note
= find_reg_note (insn
, REG_EQUAL
, NULL_RTX
);
4389 set
= single_set (insn
);
4390 if (!set
|| !function_invariant_p (SET_SRC (set
))
4391 || !function_invariant_p (XEXP (note
, 0)))
4392 remove_note (insn
, note
);
4393 } while (insn
!= end
&& (insn
= NEXT_INSN (insn
)));
4395 /* PR46315: when moving insns above a conditional branch, the REG_EQUAL
4396 notes referring to the registers being set might become invalid. */
4402 EXECUTE_IF_SET_IN_BITMAP (merge_set
, 0, i
, bi
)
4403 remove_reg_equal_equiv_notes_for_regno (i
);
4405 BITMAP_FREE (merge_set
);
4408 reorder_insns (head
, end
, PREV_INSN (earliest
));
4411 /* Remove the jump and edge if we can. */
4412 if (other_bb
== new_dest
)
4415 remove_edge (BRANCH_EDGE (test_bb
));
4416 /* ??? Can't merge blocks here, as then_bb is still in use.
4417 At minimum, the merge will get done just before bb-reorder. */
4426 BITMAP_FREE (merge_set
);
4431 /* Main entry point for all if-conversion. AFTER_COMBINE is true if
4432 we are after combine pass. */
4435 if_convert (bool after_combine
)
4442 df_live_add_problem ();
4443 df_live_set_all_dirty ();
4446 /* Record whether we are after combine pass. */
4447 ifcvt_after_combine
= after_combine
;
4448 num_possible_if_blocks
= 0;
4449 num_updated_if_blocks
= 0;
4450 num_true_changes
= 0;
4452 loop_optimizer_init (AVOID_CFG_MODIFICATIONS
);
4453 mark_loop_exit_edges ();
4454 loop_optimizer_finalize ();
4455 free_dominance_info (CDI_DOMINATORS
);
4457 /* Compute postdominators. */
4458 calculate_dominance_info (CDI_POST_DOMINATORS
);
4460 df_set_flags (DF_LR_RUN_DCE
);
4462 /* Go through each of the basic blocks looking for things to convert. If we
4463 have conditional execution, we make multiple passes to allow us to handle
4464 IF-THEN{-ELSE} blocks within other IF-THEN{-ELSE} blocks. */
4469 /* Only need to do dce on the first pass. */
4470 df_clear_flags (DF_LR_RUN_DCE
);
4471 cond_exec_changed_p
= FALSE
;
4474 #ifdef IFCVT_MULTIPLE_DUMPS
4475 if (dump_file
&& pass
> 1)
4476 fprintf (dump_file
, "\n\n========== Pass %d ==========\n", pass
);
4479 FOR_EACH_BB_FN (bb
, cfun
)
4482 while (!df_get_bb_dirty (bb
)
4483 && (new_bb
= find_if_header (bb
, pass
)) != NULL
)
4487 #ifdef IFCVT_MULTIPLE_DUMPS
4488 if (dump_file
&& cond_exec_changed_p
)
4489 print_rtl_with_bb (dump_file
, get_insns (), dump_flags
);
4492 while (cond_exec_changed_p
);
4494 #ifdef IFCVT_MULTIPLE_DUMPS
4496 fprintf (dump_file
, "\n\n========== no more changes\n");
4499 free_dominance_info (CDI_POST_DOMINATORS
);
4504 clear_aux_for_blocks ();
4506 /* If we allocated new pseudos, we must resize the array for sched1. */
4507 if (max_regno
< max_reg_num ())
4508 max_regno
= max_reg_num ();
4510 /* Write the final stats. */
4511 if (dump_file
&& num_possible_if_blocks
> 0)
4514 "\n%d possible IF blocks searched.\n",
4515 num_possible_if_blocks
);
4517 "%d IF blocks converted.\n",
4518 num_updated_if_blocks
);
4520 "%d true changes made.\n\n\n",
4525 df_remove_problem (df_live
);
4527 #ifdef ENABLE_CHECKING
4528 verify_flow_info ();
4532 /* If-conversion and CFG cleanup. */
4534 rest_of_handle_if_conversion (void)
4536 if (flag_if_conversion
)
4540 dump_reg_info (dump_file
);
4541 dump_flow_info (dump_file
, dump_flags
);
4543 cleanup_cfg (CLEANUP_EXPENSIVE
);
4553 const pass_data pass_data_rtl_ifcvt
=
4555 RTL_PASS
, /* type */
4557 OPTGROUP_NONE
, /* optinfo_flags */
4558 TV_IFCVT
, /* tv_id */
4559 0, /* properties_required */
4560 0, /* properties_provided */
4561 0, /* properties_destroyed */
4562 0, /* todo_flags_start */
4563 TODO_df_finish
, /* todo_flags_finish */
4566 class pass_rtl_ifcvt
: public rtl_opt_pass
4569 pass_rtl_ifcvt (gcc::context
*ctxt
)
4570 : rtl_opt_pass (pass_data_rtl_ifcvt
, ctxt
)
4573 /* opt_pass methods: */
4574 virtual bool gate (function
*)
4576 return (optimize
> 0) && dbg_cnt (if_conversion
);
4579 virtual unsigned int execute (function
*)
4581 return rest_of_handle_if_conversion ();
4584 }; // class pass_rtl_ifcvt
4589 make_pass_rtl_ifcvt (gcc::context
*ctxt
)
4591 return new pass_rtl_ifcvt (ctxt
);
4595 /* Rerun if-conversion, as combine may have simplified things enough
4596 to now meet sequence length restrictions. */
4600 const pass_data pass_data_if_after_combine
=
4602 RTL_PASS
, /* type */
4604 OPTGROUP_NONE
, /* optinfo_flags */
4605 TV_IFCVT
, /* tv_id */
4606 0, /* properties_required */
4607 0, /* properties_provided */
4608 0, /* properties_destroyed */
4609 0, /* todo_flags_start */
4610 TODO_df_finish
, /* todo_flags_finish */
4613 class pass_if_after_combine
: public rtl_opt_pass
4616 pass_if_after_combine (gcc::context
*ctxt
)
4617 : rtl_opt_pass (pass_data_if_after_combine
, ctxt
)
4620 /* opt_pass methods: */
4621 virtual bool gate (function
*)
4623 return optimize
> 0 && flag_if_conversion
4624 && dbg_cnt (if_after_combine
);
4627 virtual unsigned int execute (function
*)
4633 }; // class pass_if_after_combine
4638 make_pass_if_after_combine (gcc::context
*ctxt
)
4640 return new pass_if_after_combine (ctxt
);
4646 const pass_data pass_data_if_after_reload
=
4648 RTL_PASS
, /* type */
4650 OPTGROUP_NONE
, /* optinfo_flags */
4651 TV_IFCVT2
, /* tv_id */
4652 0, /* properties_required */
4653 0, /* properties_provided */
4654 0, /* properties_destroyed */
4655 0, /* todo_flags_start */
4656 TODO_df_finish
, /* todo_flags_finish */
4659 class pass_if_after_reload
: public rtl_opt_pass
4662 pass_if_after_reload (gcc::context
*ctxt
)
4663 : rtl_opt_pass (pass_data_if_after_reload
, ctxt
)
4666 /* opt_pass methods: */
4667 virtual bool gate (function
*)
4669 return optimize
> 0 && flag_if_conversion2
4670 && dbg_cnt (if_after_reload
);
4673 virtual unsigned int execute (function
*)
4679 }; // class pass_if_after_reload
4684 make_pass_if_after_reload (gcc::context
*ctxt
)
4686 return new pass_if_after_reload (ctxt
);