1 /* If-conversion support.
2 Copyright (C) 2000-2017 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"
39 #include "cfgcleanup.h"
43 #include "tree-pass.h"
45 #include "shrink-wrap.h"
50 #ifndef MAX_CONDITIONAL_EXECUTE
51 #define MAX_CONDITIONAL_EXECUTE \
52 (BRANCH_COST (optimize_function_for_speed_p (cfun), false) \
56 #define IFCVT_MULTIPLE_DUMPS 1
58 #define NULL_BLOCK ((basic_block) NULL)
60 /* True if after combine pass. */
61 static bool ifcvt_after_combine
;
63 /* True if the target has the cbranchcc4 optab. */
64 static bool have_cbranchcc4
;
66 /* # of IF-THEN or IF-THEN-ELSE blocks we looked at */
67 static int num_possible_if_blocks
;
69 /* # of IF-THEN or IF-THEN-ELSE blocks were converted to conditional
71 static int num_updated_if_blocks
;
73 /* # of changes made. */
74 static int num_true_changes
;
76 /* Whether conditional execution changes were made. */
77 static int cond_exec_changed_p
;
79 /* Forward references. */
80 static int count_bb_insns (const_basic_block
);
81 static bool cheap_bb_rtx_cost_p (const_basic_block
, int, int);
82 static rtx_insn
*first_active_insn (basic_block
);
83 static rtx_insn
*last_active_insn (basic_block
, int);
84 static rtx_insn
*find_active_insn_before (basic_block
, rtx_insn
*);
85 static rtx_insn
*find_active_insn_after (basic_block
, rtx_insn
*);
86 static basic_block
block_fallthru (basic_block
);
87 static int cond_exec_process_insns (ce_if_block
*, rtx_insn
*, rtx
, rtx
, int,
89 static rtx
cond_exec_get_condition (rtx_insn
*);
90 static rtx
noce_get_condition (rtx_insn
*, rtx_insn
**, bool);
91 static int noce_operand_ok (const_rtx
);
92 static void merge_if_block (ce_if_block
*);
93 static int find_cond_trap (basic_block
, edge
, edge
);
94 static basic_block
find_if_header (basic_block
, int);
95 static int block_jumps_and_fallthru_p (basic_block
, basic_block
);
96 static int noce_find_if_block (basic_block
, edge
, edge
, int);
97 static int cond_exec_find_if_block (ce_if_block
*);
98 static int find_if_case_1 (basic_block
, edge
, edge
);
99 static int find_if_case_2 (basic_block
, edge
, edge
);
100 static int dead_or_predicable (basic_block
, basic_block
, basic_block
,
102 static void noce_emit_move_insn (rtx
, rtx
);
103 static rtx_insn
*block_has_only_trap (basic_block
);
105 /* Count the number of non-jump active insns in BB. */
108 count_bb_insns (const_basic_block bb
)
111 rtx_insn
*insn
= BB_HEAD (bb
);
115 if (active_insn_p (insn
) && !JUMP_P (insn
))
118 if (insn
== BB_END (bb
))
120 insn
= NEXT_INSN (insn
);
126 /* Determine whether the total insn_rtx_cost on non-jump insns in
127 basic block BB is less than MAX_COST. This function returns
128 false if the cost of any instruction could not be estimated.
130 The cost of the non-jump insns in BB is scaled by REG_BR_PROB_BASE
131 as those insns are being speculated. MAX_COST is scaled with SCALE
132 plus a small fudge factor. */
135 cheap_bb_rtx_cost_p (const_basic_block bb
, int scale
, int max_cost
)
138 rtx_insn
*insn
= BB_HEAD (bb
);
139 bool speed
= optimize_bb_for_speed_p (bb
);
141 /* Set scale to REG_BR_PROB_BASE to void the identical scaling
142 applied to insn_rtx_cost when optimizing for size. Only do
143 this after combine because if-conversion might interfere with
144 passes before combine.
146 Use optimize_function_for_speed_p instead of the pre-defined
147 variable speed to make sure it is set to same value for all
148 basic blocks in one if-conversion transformation. */
149 if (!optimize_function_for_speed_p (cfun
) && ifcvt_after_combine
)
150 scale
= REG_BR_PROB_BASE
;
151 /* Our branch probability/scaling factors are just estimates and don't
152 account for cases where we can get speculation for free and other
153 secondary benefits. So we fudge the scale factor to make speculating
154 appear a little more profitable when optimizing for performance. */
156 scale
+= REG_BR_PROB_BASE
/ 8;
163 if (NONJUMP_INSN_P (insn
))
165 int cost
= insn_rtx_cost (PATTERN (insn
), speed
) * REG_BR_PROB_BASE
;
169 /* If this instruction is the load or set of a "stack" register,
170 such as a floating point register on x87, then the cost of
171 speculatively executing this insn may need to include
172 the additional cost of popping its result off of the
173 register stack. Unfortunately, correctly recognizing and
174 accounting for this additional overhead is tricky, so for
175 now we simply prohibit such speculative execution. */
178 rtx set
= single_set (insn
);
179 if (set
&& STACK_REG_P (SET_DEST (set
)))
185 if (count
>= max_cost
)
188 else if (CALL_P (insn
))
191 if (insn
== BB_END (bb
))
193 insn
= NEXT_INSN (insn
);
199 /* Return the first non-jump active insn in the basic block. */
202 first_active_insn (basic_block bb
)
204 rtx_insn
*insn
= BB_HEAD (bb
);
208 if (insn
== BB_END (bb
))
210 insn
= NEXT_INSN (insn
);
213 while (NOTE_P (insn
) || DEBUG_INSN_P (insn
))
215 if (insn
== BB_END (bb
))
217 insn
= NEXT_INSN (insn
);
226 /* Return the last non-jump active (non-jump) insn in the basic block. */
229 last_active_insn (basic_block bb
, int skip_use_p
)
231 rtx_insn
*insn
= BB_END (bb
);
232 rtx_insn
*head
= BB_HEAD (bb
);
236 || DEBUG_INSN_P (insn
)
238 && NONJUMP_INSN_P (insn
)
239 && GET_CODE (PATTERN (insn
)) == USE
))
243 insn
= PREV_INSN (insn
);
252 /* Return the active insn before INSN inside basic block CURR_BB. */
255 find_active_insn_before (basic_block curr_bb
, rtx_insn
*insn
)
257 if (!insn
|| insn
== BB_HEAD (curr_bb
))
260 while ((insn
= PREV_INSN (insn
)) != NULL_RTX
)
262 if (NONJUMP_INSN_P (insn
) || JUMP_P (insn
) || CALL_P (insn
))
265 /* No other active insn all the way to the start of the basic block. */
266 if (insn
== BB_HEAD (curr_bb
))
273 /* Return the active insn after INSN inside basic block CURR_BB. */
276 find_active_insn_after (basic_block curr_bb
, rtx_insn
*insn
)
278 if (!insn
|| insn
== BB_END (curr_bb
))
281 while ((insn
= NEXT_INSN (insn
)) != NULL_RTX
)
283 if (NONJUMP_INSN_P (insn
) || JUMP_P (insn
) || CALL_P (insn
))
286 /* No other active insn all the way to the end of the basic block. */
287 if (insn
== BB_END (curr_bb
))
294 /* Return the basic block reached by falling though the basic block BB. */
297 block_fallthru (basic_block bb
)
299 edge e
= find_fallthru_edge (bb
->succs
);
301 return (e
) ? e
->dest
: NULL_BLOCK
;
304 /* Return true if RTXs A and B can be safely interchanged. */
307 rtx_interchangeable_p (const_rtx a
, const_rtx b
)
309 if (!rtx_equal_p (a
, b
))
312 if (GET_CODE (a
) != MEM
)
315 /* A dead type-unsafe memory reference is legal, but a live type-unsafe memory
316 reference is not. Interchanging a dead type-unsafe memory reference with
317 a live type-safe one creates a live type-unsafe memory reference, in other
318 words, it makes the program illegal.
319 We check here conservatively whether the two memory references have equal
320 memory attributes. */
322 return mem_attrs_eq_p (get_mem_attrs (a
), get_mem_attrs (b
));
326 /* Go through a bunch of insns, converting them to conditional
327 execution format if possible. Return TRUE if all of the non-note
328 insns were processed. */
331 cond_exec_process_insns (ce_if_block
*ce_info ATTRIBUTE_UNUSED
,
332 /* if block information */rtx_insn
*start
,
333 /* first insn to look at */rtx end
,
334 /* last insn to look at */rtx test
,
335 /* conditional execution test */int prob_val
,
336 /* probability of branch taken. */int mod_ok
)
338 int must_be_last
= FALSE
;
346 for (insn
= start
; ; insn
= NEXT_INSN (insn
))
348 /* dwarf2out can't cope with conditional prologues. */
349 if (NOTE_P (insn
) && NOTE_KIND (insn
) == NOTE_INSN_PROLOGUE_END
)
352 if (NOTE_P (insn
) || DEBUG_INSN_P (insn
))
355 gcc_assert (NONJUMP_INSN_P (insn
) || CALL_P (insn
));
357 /* dwarf2out can't cope with conditional unwind info. */
358 if (RTX_FRAME_RELATED_P (insn
))
361 /* Remove USE insns that get in the way. */
362 if (reload_completed
&& GET_CODE (PATTERN (insn
)) == USE
)
364 /* ??? Ug. Actually unlinking the thing is problematic,
365 given what we'd have to coordinate with our callers. */
366 SET_INSN_DELETED (insn
);
370 /* Last insn wasn't last? */
374 if (modified_in_p (test
, insn
))
381 /* Now build the conditional form of the instruction. */
382 pattern
= PATTERN (insn
);
383 xtest
= copy_rtx (test
);
385 /* If this is already a COND_EXEC, rewrite the test to be an AND of the
387 if (GET_CODE (pattern
) == COND_EXEC
)
389 if (GET_MODE (xtest
) != GET_MODE (COND_EXEC_TEST (pattern
)))
392 xtest
= gen_rtx_AND (GET_MODE (xtest
), xtest
,
393 COND_EXEC_TEST (pattern
));
394 pattern
= COND_EXEC_CODE (pattern
);
397 pattern
= gen_rtx_COND_EXEC (VOIDmode
, xtest
, pattern
);
399 /* If the machine needs to modify the insn being conditionally executed,
400 say for example to force a constant integer operand into a temp
401 register, do so here. */
402 #ifdef IFCVT_MODIFY_INSN
403 IFCVT_MODIFY_INSN (ce_info
, pattern
, insn
);
408 validate_change (insn
, &PATTERN (insn
), pattern
, 1);
410 if (CALL_P (insn
) && prob_val
>= 0)
411 validate_change (insn
, ®_NOTES (insn
),
412 gen_rtx_INT_LIST ((machine_mode
) REG_BR_PROB
,
413 prob_val
, REG_NOTES (insn
)), 1);
423 /* Return the condition for a jump. Do not do any special processing. */
426 cond_exec_get_condition (rtx_insn
*jump
)
430 if (any_condjump_p (jump
))
431 test_if
= SET_SRC (pc_set (jump
));
434 cond
= XEXP (test_if
, 0);
436 /* If this branches to JUMP_LABEL when the condition is false,
437 reverse the condition. */
438 if (GET_CODE (XEXP (test_if
, 2)) == LABEL_REF
439 && label_ref_label (XEXP (test_if
, 2)) == JUMP_LABEL (jump
))
441 enum rtx_code rev
= reversed_comparison_code (cond
, jump
);
445 cond
= gen_rtx_fmt_ee (rev
, GET_MODE (cond
), XEXP (cond
, 0),
452 /* Given a simple IF-THEN or IF-THEN-ELSE block, attempt to convert it
453 to conditional execution. Return TRUE if we were successful at
454 converting the block. */
457 cond_exec_process_if_block (ce_if_block
* ce_info
,
458 /* if block information */int do_multiple_p
)
460 basic_block test_bb
= ce_info
->test_bb
; /* last test block */
461 basic_block then_bb
= ce_info
->then_bb
; /* THEN */
462 basic_block else_bb
= ce_info
->else_bb
; /* ELSE or NULL */
463 rtx test_expr
; /* expression in IF_THEN_ELSE that is tested */
464 rtx_insn
*then_start
; /* first insn in THEN block */
465 rtx_insn
*then_end
; /* last insn + 1 in THEN block */
466 rtx_insn
*else_start
= NULL
; /* first insn in ELSE block or NULL */
467 rtx_insn
*else_end
= NULL
; /* last insn + 1 in ELSE block */
468 int max
; /* max # of insns to convert. */
469 int then_mod_ok
; /* whether conditional mods are ok in THEN */
470 rtx true_expr
; /* test for else block insns */
471 rtx false_expr
; /* test for then block insns */
472 int true_prob_val
; /* probability of else block */
473 int false_prob_val
; /* probability of then block */
474 rtx_insn
*then_last_head
= NULL
; /* Last match at the head of THEN */
475 rtx_insn
*else_last_head
= NULL
; /* Last match at the head of ELSE */
476 rtx_insn
*then_first_tail
= NULL
; /* First match at the tail of THEN */
477 rtx_insn
*else_first_tail
= NULL
; /* First match at the tail of ELSE */
478 int then_n_insns
, else_n_insns
, n_insns
;
479 enum rtx_code false_code
;
482 /* If test is comprised of && or || elements, and we've failed at handling
483 all of them together, just use the last test if it is the special case of
484 && elements without an ELSE block. */
485 if (!do_multiple_p
&& ce_info
->num_multiple_test_blocks
)
487 if (else_bb
|| ! ce_info
->and_and_p
)
490 ce_info
->test_bb
= test_bb
= ce_info
->last_test_bb
;
491 ce_info
->num_multiple_test_blocks
= 0;
492 ce_info
->num_and_and_blocks
= 0;
493 ce_info
->num_or_or_blocks
= 0;
496 /* Find the conditional jump to the ELSE or JOIN part, and isolate
498 test_expr
= cond_exec_get_condition (BB_END (test_bb
));
502 /* If the conditional jump is more than just a conditional jump,
503 then we can not do conditional execution conversion on this block. */
504 if (! onlyjump_p (BB_END (test_bb
)))
507 /* Collect the bounds of where we're to search, skipping any labels, jumps
508 and notes at the beginning and end of the block. Then count the total
509 number of insns and see if it is small enough to convert. */
510 then_start
= first_active_insn (then_bb
);
511 then_end
= last_active_insn (then_bb
, TRUE
);
512 then_n_insns
= ce_info
->num_then_insns
= count_bb_insns (then_bb
);
513 n_insns
= then_n_insns
;
514 max
= MAX_CONDITIONAL_EXECUTE
;
521 else_start
= first_active_insn (else_bb
);
522 else_end
= last_active_insn (else_bb
, TRUE
);
523 else_n_insns
= ce_info
->num_else_insns
= count_bb_insns (else_bb
);
524 n_insns
+= else_n_insns
;
526 /* Look for matching sequences at the head and tail of the two blocks,
527 and limit the range of insns to be converted if possible. */
528 n_matching
= flow_find_cross_jump (then_bb
, else_bb
,
529 &then_first_tail
, &else_first_tail
,
531 if (then_first_tail
== BB_HEAD (then_bb
))
532 then_start
= then_end
= NULL
;
533 if (else_first_tail
== BB_HEAD (else_bb
))
534 else_start
= else_end
= NULL
;
539 then_end
= find_active_insn_before (then_bb
, then_first_tail
);
541 else_end
= find_active_insn_before (else_bb
, else_first_tail
);
542 n_insns
-= 2 * n_matching
;
547 && then_n_insns
> n_matching
548 && else_n_insns
> n_matching
)
550 int longest_match
= MIN (then_n_insns
- n_matching
,
551 else_n_insns
- n_matching
);
553 = flow_find_head_matching_sequence (then_bb
, else_bb
,
562 /* We won't pass the insns in the head sequence to
563 cond_exec_process_insns, so we need to test them here
564 to make sure that they don't clobber the condition. */
565 for (insn
= BB_HEAD (then_bb
);
566 insn
!= NEXT_INSN (then_last_head
);
567 insn
= NEXT_INSN (insn
))
568 if (!LABEL_P (insn
) && !NOTE_P (insn
)
569 && !DEBUG_INSN_P (insn
)
570 && modified_in_p (test_expr
, insn
))
574 if (then_last_head
== then_end
)
575 then_start
= then_end
= NULL
;
576 if (else_last_head
== else_end
)
577 else_start
= else_end
= NULL
;
582 then_start
= find_active_insn_after (then_bb
, then_last_head
);
584 else_start
= find_active_insn_after (else_bb
, else_last_head
);
585 n_insns
-= 2 * n_matching
;
593 /* Map test_expr/test_jump into the appropriate MD tests to use on
594 the conditionally executed code. */
596 true_expr
= test_expr
;
598 false_code
= reversed_comparison_code (true_expr
, BB_END (test_bb
));
599 if (false_code
!= UNKNOWN
)
600 false_expr
= gen_rtx_fmt_ee (false_code
, GET_MODE (true_expr
),
601 XEXP (true_expr
, 0), XEXP (true_expr
, 1));
603 false_expr
= NULL_RTX
;
605 #ifdef IFCVT_MODIFY_TESTS
606 /* If the machine description needs to modify the tests, such as setting a
607 conditional execution register from a comparison, it can do so here. */
608 IFCVT_MODIFY_TESTS (ce_info
, true_expr
, false_expr
);
610 /* See if the conversion failed. */
611 if (!true_expr
|| !false_expr
)
615 note
= find_reg_note (BB_END (test_bb
), REG_BR_PROB
, NULL_RTX
);
618 true_prob_val
= XINT (note
, 0);
619 false_prob_val
= REG_BR_PROB_BASE
- true_prob_val
;
627 /* If we have && or || tests, do them here. These tests are in the adjacent
628 blocks after the first block containing the test. */
629 if (ce_info
->num_multiple_test_blocks
> 0)
631 basic_block bb
= test_bb
;
632 basic_block last_test_bb
= ce_info
->last_test_bb
;
639 rtx_insn
*start
, *end
;
641 enum rtx_code f_code
;
643 bb
= block_fallthru (bb
);
644 start
= first_active_insn (bb
);
645 end
= last_active_insn (bb
, TRUE
);
647 && ! cond_exec_process_insns (ce_info
, start
, end
, false_expr
,
648 false_prob_val
, FALSE
))
651 /* If the conditional jump is more than just a conditional jump, then
652 we can not do conditional execution conversion on this block. */
653 if (! onlyjump_p (BB_END (bb
)))
656 /* Find the conditional jump and isolate the test. */
657 t
= cond_exec_get_condition (BB_END (bb
));
661 f_code
= reversed_comparison_code (t
, BB_END (bb
));
662 if (f_code
== UNKNOWN
)
665 f
= gen_rtx_fmt_ee (f_code
, GET_MODE (t
), XEXP (t
, 0), XEXP (t
, 1));
666 if (ce_info
->and_and_p
)
668 t
= gen_rtx_AND (GET_MODE (t
), true_expr
, t
);
669 f
= gen_rtx_IOR (GET_MODE (t
), false_expr
, f
);
673 t
= gen_rtx_IOR (GET_MODE (t
), true_expr
, t
);
674 f
= gen_rtx_AND (GET_MODE (t
), false_expr
, f
);
677 /* If the machine description needs to modify the tests, such as
678 setting a conditional execution register from a comparison, it can
680 #ifdef IFCVT_MODIFY_MULTIPLE_TESTS
681 IFCVT_MODIFY_MULTIPLE_TESTS (ce_info
, bb
, t
, f
);
683 /* See if the conversion failed. */
691 while (bb
!= last_test_bb
);
694 /* For IF-THEN-ELSE blocks, we don't allow modifications of the test
695 on then THEN block. */
696 then_mod_ok
= (else_bb
== NULL_BLOCK
);
698 /* Go through the THEN and ELSE blocks converting the insns if possible
699 to conditional execution. */
703 || ! cond_exec_process_insns (ce_info
, then_start
, then_end
,
704 false_expr
, false_prob_val
,
708 if (else_bb
&& else_end
709 && ! cond_exec_process_insns (ce_info
, else_start
, else_end
,
710 true_expr
, true_prob_val
, TRUE
))
713 /* If we cannot apply the changes, fail. Do not go through the normal fail
714 processing, since apply_change_group will call cancel_changes. */
715 if (! apply_change_group ())
717 #ifdef IFCVT_MODIFY_CANCEL
718 /* Cancel any machine dependent changes. */
719 IFCVT_MODIFY_CANCEL (ce_info
);
724 #ifdef IFCVT_MODIFY_FINAL
725 /* Do any machine dependent final modifications. */
726 IFCVT_MODIFY_FINAL (ce_info
);
729 /* Conversion succeeded. */
731 fprintf (dump_file
, "%d insn%s converted to conditional execution.\n",
732 n_insns
, (n_insns
== 1) ? " was" : "s were");
734 /* Merge the blocks! If we had matching sequences, make sure to delete one
735 copy at the appropriate location first: delete the copy in the THEN branch
736 for a tail sequence so that the remaining one is executed last for both
737 branches, and delete the copy in the ELSE branch for a head sequence so
738 that the remaining one is executed first for both branches. */
741 rtx_insn
*from
= then_first_tail
;
743 from
= find_active_insn_after (then_bb
, from
);
744 delete_insn_chain (from
, get_last_bb_insn (then_bb
), false);
747 delete_insn_chain (first_active_insn (else_bb
), else_last_head
, false);
749 merge_if_block (ce_info
);
750 cond_exec_changed_p
= TRUE
;
754 #ifdef IFCVT_MODIFY_CANCEL
755 /* Cancel any machine dependent changes. */
756 IFCVT_MODIFY_CANCEL (ce_info
);
763 static rtx
noce_emit_store_flag (struct noce_if_info
*, rtx
, int, int);
764 static int noce_try_move (struct noce_if_info
*);
765 static int noce_try_ifelse_collapse (struct noce_if_info
*);
766 static int noce_try_store_flag (struct noce_if_info
*);
767 static int noce_try_addcc (struct noce_if_info
*);
768 static int noce_try_store_flag_constants (struct noce_if_info
*);
769 static int noce_try_store_flag_mask (struct noce_if_info
*);
770 static rtx
noce_emit_cmove (struct noce_if_info
*, rtx
, enum rtx_code
, rtx
,
772 static int noce_try_cmove (struct noce_if_info
*);
773 static int noce_try_cmove_arith (struct noce_if_info
*);
774 static rtx
noce_get_alt_condition (struct noce_if_info
*, rtx
, rtx_insn
**);
775 static int noce_try_minmax (struct noce_if_info
*);
776 static int noce_try_abs (struct noce_if_info
*);
777 static int noce_try_sign_mask (struct noce_if_info
*);
779 /* Return the comparison code for reversed condition for IF_INFO,
780 or UNKNOWN if reversing the condition is not possible. */
782 static inline enum rtx_code
783 noce_reversed_cond_code (struct noce_if_info
*if_info
)
785 if (if_info
->rev_cond
)
786 return GET_CODE (if_info
->rev_cond
);
787 return reversed_comparison_code (if_info
->cond
, if_info
->jump
);
790 /* Return true if SEQ is a good candidate as a replacement for the
791 if-convertible sequence described in IF_INFO.
792 This is the default implementation that targets can override
793 through a target hook. */
796 default_noce_conversion_profitable_p (rtx_insn
*seq
,
797 struct noce_if_info
*if_info
)
799 bool speed_p
= if_info
->speed_p
;
801 /* Cost up the new sequence. */
802 unsigned int cost
= seq_cost (seq
, speed_p
);
804 if (cost
<= if_info
->original_cost
)
807 /* When compiling for size, we can make a reasonably accurately guess
808 at the size growth. When compiling for speed, use the maximum. */
809 return speed_p
&& cost
<= if_info
->max_seq_cost
;
812 /* Helper function for noce_try_store_flag*. */
815 noce_emit_store_flag (struct noce_if_info
*if_info
, rtx x
, int reversep
,
818 rtx cond
= if_info
->cond
;
822 cond_complex
= (! general_operand (XEXP (cond
, 0), VOIDmode
)
823 || ! general_operand (XEXP (cond
, 1), VOIDmode
));
825 /* If earliest == jump, or when the condition is complex, try to
826 build the store_flag insn directly. */
830 rtx set
= pc_set (if_info
->jump
);
831 cond
= XEXP (SET_SRC (set
), 0);
832 if (GET_CODE (XEXP (SET_SRC (set
), 2)) == LABEL_REF
833 && label_ref_label (XEXP (SET_SRC (set
), 2)) == JUMP_LABEL (if_info
->jump
))
834 reversep
= !reversep
;
835 if (if_info
->then_else_reversed
)
836 reversep
= !reversep
;
840 && general_operand (XEXP (if_info
->rev_cond
, 0), VOIDmode
)
841 && general_operand (XEXP (if_info
->rev_cond
, 1), VOIDmode
))
843 cond
= if_info
->rev_cond
;
848 code
= reversed_comparison_code (cond
, if_info
->jump
);
850 code
= GET_CODE (cond
);
852 if ((if_info
->cond_earliest
== if_info
->jump
|| cond_complex
)
853 && (normalize
== 0 || STORE_FLAG_VALUE
== normalize
))
855 rtx src
= gen_rtx_fmt_ee (code
, GET_MODE (x
), XEXP (cond
, 0),
857 rtx set
= gen_rtx_SET (x
, src
);
860 rtx_insn
*insn
= emit_insn (set
);
862 if (recog_memoized (insn
) >= 0)
864 rtx_insn
*seq
= get_insns ();
868 if_info
->cond_earliest
= if_info
->jump
;
876 /* Don't even try if the comparison operands or the mode of X are weird. */
877 if (cond_complex
|| !SCALAR_INT_MODE_P (GET_MODE (x
)))
880 return emit_store_flag (x
, code
, XEXP (cond
, 0),
881 XEXP (cond
, 1), VOIDmode
,
882 (code
== LTU
|| code
== LEU
883 || code
== GEU
|| code
== GTU
), normalize
);
886 /* Emit instruction to move an rtx, possibly into STRICT_LOW_PART.
887 X is the destination/target and Y is the value to copy. */
890 noce_emit_move_insn (rtx x
, rtx y
)
892 machine_mode outmode
;
896 if (GET_CODE (x
) != STRICT_LOW_PART
)
898 rtx_insn
*seq
, *insn
;
903 /* Check that the SET_SRC is reasonable before calling emit_move_insn,
904 otherwise construct a suitable SET pattern ourselves. */
905 insn
= (OBJECT_P (y
) || CONSTANT_P (y
) || GET_CODE (y
) == SUBREG
)
906 ? emit_move_insn (x
, y
)
907 : emit_insn (gen_rtx_SET (x
, y
));
911 if (recog_memoized (insn
) <= 0)
913 if (GET_CODE (x
) == ZERO_EXTRACT
)
915 rtx op
= XEXP (x
, 0);
916 unsigned HOST_WIDE_INT size
= INTVAL (XEXP (x
, 1));
917 unsigned HOST_WIDE_INT start
= INTVAL (XEXP (x
, 2));
919 /* store_bit_field expects START to be relative to
920 BYTES_BIG_ENDIAN and adjusts this value for machines with
921 BITS_BIG_ENDIAN != BYTES_BIG_ENDIAN. In order to be able to
922 invoke store_bit_field again it is necessary to have the START
923 value from the first call. */
924 if (BITS_BIG_ENDIAN
!= BYTES_BIG_ENDIAN
)
927 start
= BITS_PER_UNIT
- start
- size
;
930 gcc_assert (REG_P (op
));
931 start
= BITS_PER_WORD
- start
- size
;
935 gcc_assert (start
< (MEM_P (op
) ? BITS_PER_UNIT
: BITS_PER_WORD
));
936 store_bit_field (op
, size
, start
, 0, 0, GET_MODE (x
), y
, false);
940 switch (GET_RTX_CLASS (GET_CODE (y
)))
943 ot
= code_to_optab (GET_CODE (y
));
947 target
= expand_unop (GET_MODE (y
), ot
, XEXP (y
, 0), x
, 0);
948 if (target
!= NULL_RTX
)
951 emit_move_insn (x
, target
);
960 ot
= code_to_optab (GET_CODE (y
));
964 target
= expand_binop (GET_MODE (y
), ot
,
965 XEXP (y
, 0), XEXP (y
, 1),
967 if (target
!= NULL_RTX
)
970 emit_move_insn (x
, target
);
987 inner
= XEXP (outer
, 0);
988 outmode
= GET_MODE (outer
);
989 bitpos
= SUBREG_BYTE (outer
) * BITS_PER_UNIT
;
990 store_bit_field (inner
, GET_MODE_BITSIZE (outmode
), bitpos
,
991 0, 0, outmode
, y
, false);
994 /* Return the CC reg if it is used in COND. */
997 cc_in_cond (rtx cond
)
999 if (have_cbranchcc4
&& cond
1000 && GET_MODE_CLASS (GET_MODE (XEXP (cond
, 0))) == MODE_CC
)
1001 return XEXP (cond
, 0);
1006 /* Return sequence of instructions generated by if conversion. This
1007 function calls end_sequence() to end the current stream, ensures
1008 that the instructions are unshared, recognizable non-jump insns.
1009 On failure, this function returns a NULL_RTX. */
1012 end_ifcvt_sequence (struct noce_if_info
*if_info
)
1015 rtx_insn
*seq
= get_insns ();
1016 rtx cc
= cc_in_cond (if_info
->cond
);
1018 set_used_flags (if_info
->x
);
1019 set_used_flags (if_info
->cond
);
1020 set_used_flags (if_info
->a
);
1021 set_used_flags (if_info
->b
);
1023 for (insn
= seq
; insn
; insn
= NEXT_INSN (insn
))
1024 set_used_flags (insn
);
1026 unshare_all_rtl_in_chain (seq
);
1029 /* Make sure that all of the instructions emitted are recognizable,
1030 and that we haven't introduced a new jump instruction.
1031 As an exercise for the reader, build a general mechanism that
1032 allows proper placement of required clobbers. */
1033 for (insn
= seq
; insn
; insn
= NEXT_INSN (insn
))
1035 || recog_memoized (insn
) == -1
1036 /* Make sure new generated code does not clobber CC. */
1037 || (cc
&& set_of (cc
, insn
)))
1043 /* Return true iff the then and else basic block (if it exists)
1044 consist of a single simple set instruction. */
1047 noce_simple_bbs (struct noce_if_info
*if_info
)
1049 if (!if_info
->then_simple
)
1052 if (if_info
->else_bb
)
1053 return if_info
->else_simple
;
1058 /* Convert "if (a != b) x = a; else x = b" into "x = a" and
1059 "if (a == b) x = a; else x = b" into "x = b". */
1062 noce_try_move (struct noce_if_info
*if_info
)
1064 rtx cond
= if_info
->cond
;
1065 enum rtx_code code
= GET_CODE (cond
);
1069 if (code
!= NE
&& code
!= EQ
)
1072 if (!noce_simple_bbs (if_info
))
1075 /* This optimization isn't valid if either A or B could be a NaN
1076 or a signed zero. */
1077 if (HONOR_NANS (if_info
->x
)
1078 || HONOR_SIGNED_ZEROS (if_info
->x
))
1081 /* Check whether the operands of the comparison are A and in
1083 if ((rtx_equal_p (if_info
->a
, XEXP (cond
, 0))
1084 && rtx_equal_p (if_info
->b
, XEXP (cond
, 1)))
1085 || (rtx_equal_p (if_info
->a
, XEXP (cond
, 1))
1086 && rtx_equal_p (if_info
->b
, XEXP (cond
, 0))))
1088 if (!rtx_interchangeable_p (if_info
->a
, if_info
->b
))
1091 y
= (code
== EQ
) ? if_info
->a
: if_info
->b
;
1093 /* Avoid generating the move if the source is the destination. */
1094 if (! rtx_equal_p (if_info
->x
, y
))
1097 noce_emit_move_insn (if_info
->x
, y
);
1098 seq
= end_ifcvt_sequence (if_info
);
1102 emit_insn_before_setloc (seq
, if_info
->jump
,
1103 INSN_LOCATION (if_info
->insn_a
));
1105 if_info
->transform_name
= "noce_try_move";
1111 /* Try forming an IF_THEN_ELSE (cond, b, a) and collapsing that
1112 through simplify_rtx. Sometimes that can eliminate the IF_THEN_ELSE.
1113 If that is the case, emit the result into x. */
1116 noce_try_ifelse_collapse (struct noce_if_info
* if_info
)
1118 if (!noce_simple_bbs (if_info
))
1121 machine_mode mode
= GET_MODE (if_info
->x
);
1122 rtx if_then_else
= simplify_gen_ternary (IF_THEN_ELSE
, mode
, mode
,
1123 if_info
->cond
, if_info
->b
,
1126 if (GET_CODE (if_then_else
) == IF_THEN_ELSE
)
1131 noce_emit_move_insn (if_info
->x
, if_then_else
);
1132 seq
= end_ifcvt_sequence (if_info
);
1136 emit_insn_before_setloc (seq
, if_info
->jump
,
1137 INSN_LOCATION (if_info
->insn_a
));
1139 if_info
->transform_name
= "noce_try_ifelse_collapse";
1144 /* Convert "if (test) x = 1; else x = 0".
1146 Only try 0 and STORE_FLAG_VALUE here. Other combinations will be
1147 tried in noce_try_store_flag_constants after noce_try_cmove has had
1148 a go at the conversion. */
1151 noce_try_store_flag (struct noce_if_info
*if_info
)
1157 if (!noce_simple_bbs (if_info
))
1160 if (CONST_INT_P (if_info
->b
)
1161 && INTVAL (if_info
->b
) == STORE_FLAG_VALUE
1162 && if_info
->a
== const0_rtx
)
1164 else if (if_info
->b
== const0_rtx
1165 && CONST_INT_P (if_info
->a
)
1166 && INTVAL (if_info
->a
) == STORE_FLAG_VALUE
1167 && noce_reversed_cond_code (if_info
) != UNKNOWN
)
1174 target
= noce_emit_store_flag (if_info
, if_info
->x
, reversep
, 0);
1177 if (target
!= if_info
->x
)
1178 noce_emit_move_insn (if_info
->x
, target
);
1180 seq
= end_ifcvt_sequence (if_info
);
1184 emit_insn_before_setloc (seq
, if_info
->jump
,
1185 INSN_LOCATION (if_info
->insn_a
));
1186 if_info
->transform_name
= "noce_try_store_flag";
1197 /* Convert "if (test) x = -A; else x = A" into
1198 x = A; if (test) x = -x if the machine can do the
1199 conditional negate form of this cheaply.
1200 Try this before noce_try_cmove that will just load the
1201 immediates into two registers and do a conditional select
1202 between them. If the target has a conditional negate or
1203 conditional invert operation we can save a potentially
1204 expensive constant synthesis. */
1207 noce_try_inverse_constants (struct noce_if_info
*if_info
)
1209 if (!noce_simple_bbs (if_info
))
1212 if (!CONST_INT_P (if_info
->a
)
1213 || !CONST_INT_P (if_info
->b
)
1214 || !REG_P (if_info
->x
))
1217 machine_mode mode
= GET_MODE (if_info
->x
);
1219 HOST_WIDE_INT val_a
= INTVAL (if_info
->a
);
1220 HOST_WIDE_INT val_b
= INTVAL (if_info
->b
);
1222 rtx cond
= if_info
->cond
;
1230 if (val_b
!= HOST_WIDE_INT_MIN
&& val_a
== -val_b
)
1232 else if (val_a
== ~val_b
)
1240 rtx tmp
= gen_reg_rtx (mode
);
1241 noce_emit_move_insn (tmp
, if_info
->a
);
1243 target
= emit_conditional_neg_or_complement (x
, code
, mode
, cond
, tmp
, tmp
);
1247 rtx_insn
*seq
= get_insns ();
1255 if (target
!= if_info
->x
)
1256 noce_emit_move_insn (if_info
->x
, target
);
1258 seq
= end_ifcvt_sequence (if_info
);
1263 emit_insn_before_setloc (seq
, if_info
->jump
,
1264 INSN_LOCATION (if_info
->insn_a
));
1265 if_info
->transform_name
= "noce_try_inverse_constants";
1274 /* Convert "if (test) x = a; else x = b", for A and B constant.
1275 Also allow A = y + c1, B = y + c2, with a common y between A
1279 noce_try_store_flag_constants (struct noce_if_info
*if_info
)
1284 HOST_WIDE_INT itrue
, ifalse
, diff
, tmp
;
1287 machine_mode mode
= GET_MODE (if_info
->x
);;
1288 rtx common
= NULL_RTX
;
1293 /* Handle cases like x := test ? y + 3 : y + 4. */
1294 if (GET_CODE (a
) == PLUS
1295 && GET_CODE (b
) == PLUS
1296 && CONST_INT_P (XEXP (a
, 1))
1297 && CONST_INT_P (XEXP (b
, 1))
1298 && rtx_equal_p (XEXP (a
, 0), XEXP (b
, 0))
1299 /* Allow expressions that are not using the result or plain
1300 registers where we handle overlap below. */
1301 && (REG_P (XEXP (a
, 0))
1302 || (noce_operand_ok (XEXP (a
, 0))
1303 && ! reg_overlap_mentioned_p (if_info
->x
, XEXP (a
, 0)))))
1305 common
= XEXP (a
, 0);
1310 if (!noce_simple_bbs (if_info
))
1316 ifalse
= INTVAL (a
);
1318 bool subtract_flag_p
= false;
1320 diff
= (unsigned HOST_WIDE_INT
) itrue
- ifalse
;
1321 /* Make sure we can represent the difference between the two values. */
1323 != ((ifalse
< 0) != (itrue
< 0) ? ifalse
< 0 : ifalse
< itrue
))
1326 diff
= trunc_int_for_mode (diff
, mode
);
1328 can_reverse
= noce_reversed_cond_code (if_info
) != UNKNOWN
;
1330 if (diff
== STORE_FLAG_VALUE
|| diff
== -STORE_FLAG_VALUE
)
1333 /* We could collapse these cases but it is easier to follow the
1334 diff/STORE_FLAG_VALUE combinations when they are listed
1338 => 4 + (test != 0). */
1339 if (diff
< 0 && STORE_FLAG_VALUE
< 0)
1342 => can_reverse | 4 + (test == 0)
1343 !can_reverse | 3 - (test != 0). */
1344 else if (diff
> 0 && STORE_FLAG_VALUE
< 0)
1346 reversep
= can_reverse
;
1347 subtract_flag_p
= !can_reverse
;
1348 /* If we need to subtract the flag and we have PLUS-immediate
1349 A and B then it is unlikely to be beneficial to play tricks
1351 if (subtract_flag_p
&& common
)
1355 => can_reverse | 3 + (test == 0)
1356 !can_reverse | 4 - (test != 0). */
1357 else if (diff
< 0 && STORE_FLAG_VALUE
> 0)
1359 reversep
= can_reverse
;
1360 subtract_flag_p
= !can_reverse
;
1361 /* If we need to subtract the flag and we have PLUS-immediate
1362 A and B then it is unlikely to be beneficial to play tricks
1364 if (subtract_flag_p
&& common
)
1368 => 4 + (test != 0). */
1369 else if (diff
> 0 && STORE_FLAG_VALUE
> 0)
1374 /* Is this (cond) ? 2^n : 0? */
1375 else if (ifalse
== 0 && pow2p_hwi (itrue
)
1376 && STORE_FLAG_VALUE
== 1)
1378 /* Is this (cond) ? 0 : 2^n? */
1379 else if (itrue
== 0 && pow2p_hwi (ifalse
) && can_reverse
1380 && STORE_FLAG_VALUE
== 1)
1385 /* Is this (cond) ? -1 : x? */
1386 else if (itrue
== -1
1387 && STORE_FLAG_VALUE
== -1)
1389 /* Is this (cond) ? x : -1? */
1390 else if (ifalse
== -1 && can_reverse
1391 && STORE_FLAG_VALUE
== -1)
1401 std::swap (itrue
, ifalse
);
1402 diff
= trunc_int_for_mode (-(unsigned HOST_WIDE_INT
) diff
, mode
);
1407 /* If we have x := test ? x + 3 : x + 4 then move the original
1408 x out of the way while we store flags. */
1409 if (common
&& rtx_equal_p (common
, if_info
->x
))
1411 common
= gen_reg_rtx (mode
);
1412 noce_emit_move_insn (common
, if_info
->x
);
1415 target
= noce_emit_store_flag (if_info
, if_info
->x
, reversep
, normalize
);
1422 /* if (test) x = 3; else x = 4;
1423 => x = 3 + (test == 0); */
1424 if (diff
== STORE_FLAG_VALUE
|| diff
== -STORE_FLAG_VALUE
)
1426 /* Add the common part now. This may allow combine to merge this
1427 with the store flag operation earlier into some sort of conditional
1428 increment/decrement if the target allows it. */
1430 target
= expand_simple_binop (mode
, PLUS
,
1432 target
, 0, OPTAB_WIDEN
);
1434 /* Always use ifalse here. It should have been swapped with itrue
1435 when appropriate when reversep is true. */
1436 target
= expand_simple_binop (mode
, subtract_flag_p
? MINUS
: PLUS
,
1437 gen_int_mode (ifalse
, mode
), target
,
1438 if_info
->x
, 0, OPTAB_WIDEN
);
1440 /* Other cases are not beneficial when the original A and B are PLUS
1447 /* if (test) x = 8; else x = 0;
1448 => x = (test != 0) << 3; */
1449 else if (ifalse
== 0 && (tmp
= exact_log2 (itrue
)) >= 0)
1451 target
= expand_simple_binop (mode
, ASHIFT
,
1452 target
, GEN_INT (tmp
), if_info
->x
, 0,
1456 /* if (test) x = -1; else x = b;
1457 => x = -(test != 0) | b; */
1458 else if (itrue
== -1)
1460 target
= expand_simple_binop (mode
, IOR
,
1461 target
, gen_int_mode (ifalse
, mode
),
1462 if_info
->x
, 0, OPTAB_WIDEN
);
1476 if (target
!= if_info
->x
)
1477 noce_emit_move_insn (if_info
->x
, target
);
1479 seq
= end_ifcvt_sequence (if_info
);
1480 if (!seq
|| !targetm
.noce_conversion_profitable_p (seq
, if_info
))
1483 emit_insn_before_setloc (seq
, if_info
->jump
,
1484 INSN_LOCATION (if_info
->insn_a
));
1485 if_info
->transform_name
= "noce_try_store_flag_constants";
1493 /* Convert "if (test) foo++" into "foo += (test != 0)", and
1494 similarly for "foo--". */
1497 noce_try_addcc (struct noce_if_info
*if_info
)
1501 int subtract
, normalize
;
1503 if (!noce_simple_bbs (if_info
))
1506 if (GET_CODE (if_info
->a
) == PLUS
1507 && rtx_equal_p (XEXP (if_info
->a
, 0), if_info
->b
)
1508 && noce_reversed_cond_code (if_info
) != UNKNOWN
)
1510 rtx cond
= if_info
->rev_cond
;
1513 if (cond
== NULL_RTX
)
1515 cond
= if_info
->cond
;
1516 code
= reversed_comparison_code (cond
, if_info
->jump
);
1519 code
= GET_CODE (cond
);
1521 /* First try to use addcc pattern. */
1522 if (general_operand (XEXP (cond
, 0), VOIDmode
)
1523 && general_operand (XEXP (cond
, 1), VOIDmode
))
1526 target
= emit_conditional_add (if_info
->x
, code
,
1531 XEXP (if_info
->a
, 1),
1532 GET_MODE (if_info
->x
),
1533 (code
== LTU
|| code
== GEU
1534 || code
== LEU
|| code
== GTU
));
1537 if (target
!= if_info
->x
)
1538 noce_emit_move_insn (if_info
->x
, target
);
1540 seq
= end_ifcvt_sequence (if_info
);
1541 if (!seq
|| !targetm
.noce_conversion_profitable_p (seq
, if_info
))
1544 emit_insn_before_setloc (seq
, if_info
->jump
,
1545 INSN_LOCATION (if_info
->insn_a
));
1546 if_info
->transform_name
= "noce_try_addcc";
1553 /* If that fails, construct conditional increment or decrement using
1554 setcc. We're changing a branch and an increment to a comparison and
1556 if (XEXP (if_info
->a
, 1) == const1_rtx
1557 || XEXP (if_info
->a
, 1) == constm1_rtx
)
1560 if (STORE_FLAG_VALUE
== INTVAL (XEXP (if_info
->a
, 1)))
1561 subtract
= 0, normalize
= 0;
1562 else if (-STORE_FLAG_VALUE
== INTVAL (XEXP (if_info
->a
, 1)))
1563 subtract
= 1, normalize
= 0;
1565 subtract
= 0, normalize
= INTVAL (XEXP (if_info
->a
, 1));
1568 target
= noce_emit_store_flag (if_info
,
1569 gen_reg_rtx (GET_MODE (if_info
->x
)),
1573 target
= expand_simple_binop (GET_MODE (if_info
->x
),
1574 subtract
? MINUS
: PLUS
,
1575 if_info
->b
, target
, if_info
->x
,
1579 if (target
!= if_info
->x
)
1580 noce_emit_move_insn (if_info
->x
, target
);
1582 seq
= end_ifcvt_sequence (if_info
);
1583 if (!seq
|| !targetm
.noce_conversion_profitable_p (seq
, if_info
))
1586 emit_insn_before_setloc (seq
, if_info
->jump
,
1587 INSN_LOCATION (if_info
->insn_a
));
1588 if_info
->transform_name
= "noce_try_addcc";
1598 /* Convert "if (test) x = 0;" to "x &= -(test == 0);" */
1601 noce_try_store_flag_mask (struct noce_if_info
*if_info
)
1607 if (!noce_simple_bbs (if_info
))
1612 if ((if_info
->a
== const0_rtx
1613 && rtx_equal_p (if_info
->b
, if_info
->x
))
1614 || ((reversep
= (noce_reversed_cond_code (if_info
) != UNKNOWN
))
1615 && if_info
->b
== const0_rtx
1616 && rtx_equal_p (if_info
->a
, if_info
->x
)))
1619 target
= noce_emit_store_flag (if_info
,
1620 gen_reg_rtx (GET_MODE (if_info
->x
)),
1623 target
= expand_simple_binop (GET_MODE (if_info
->x
), AND
,
1625 target
, if_info
->x
, 0,
1630 if (target
!= if_info
->x
)
1631 noce_emit_move_insn (if_info
->x
, target
);
1633 seq
= end_ifcvt_sequence (if_info
);
1634 if (!seq
|| !targetm
.noce_conversion_profitable_p (seq
, if_info
))
1637 emit_insn_before_setloc (seq
, if_info
->jump
,
1638 INSN_LOCATION (if_info
->insn_a
));
1639 if_info
->transform_name
= "noce_try_store_flag_mask";
1650 /* Helper function for noce_try_cmove and noce_try_cmove_arith. */
1653 noce_emit_cmove (struct noce_if_info
*if_info
, rtx x
, enum rtx_code code
,
1654 rtx cmp_a
, rtx cmp_b
, rtx vfalse
, rtx vtrue
)
1656 rtx target ATTRIBUTE_UNUSED
;
1657 int unsignedp ATTRIBUTE_UNUSED
;
1659 /* If earliest == jump, try to build the cmove insn directly.
1660 This is helpful when combine has created some complex condition
1661 (like for alpha's cmovlbs) that we can't hope to regenerate
1662 through the normal interface. */
1664 if (if_info
->cond_earliest
== if_info
->jump
)
1666 rtx cond
= gen_rtx_fmt_ee (code
, GET_MODE (if_info
->cond
), cmp_a
, cmp_b
);
1667 rtx if_then_else
= gen_rtx_IF_THEN_ELSE (GET_MODE (x
),
1668 cond
, vtrue
, vfalse
);
1669 rtx set
= gen_rtx_SET (x
, if_then_else
);
1672 rtx_insn
*insn
= emit_insn (set
);
1674 if (recog_memoized (insn
) >= 0)
1676 rtx_insn
*seq
= get_insns ();
1686 /* Don't even try if the comparison operands are weird
1687 except that the target supports cbranchcc4. */
1688 if (! general_operand (cmp_a
, GET_MODE (cmp_a
))
1689 || ! general_operand (cmp_b
, GET_MODE (cmp_b
)))
1691 if (!have_cbranchcc4
1692 || GET_MODE_CLASS (GET_MODE (cmp_a
)) != MODE_CC
1693 || cmp_b
!= const0_rtx
)
1697 unsignedp
= (code
== LTU
|| code
== GEU
1698 || code
== LEU
|| code
== GTU
);
1700 target
= emit_conditional_move (x
, code
, cmp_a
, cmp_b
, VOIDmode
,
1701 vtrue
, vfalse
, GET_MODE (x
),
1706 /* We might be faced with a situation like:
1709 vtrue = (subreg:M (reg:N VTRUE) BYTE)
1710 vfalse = (subreg:M (reg:N VFALSE) BYTE)
1712 We can't do a conditional move in mode M, but it's possible that we
1713 could do a conditional move in mode N instead and take a subreg of
1716 If we can't create new pseudos, though, don't bother. */
1717 if (reload_completed
)
1720 if (GET_CODE (vtrue
) == SUBREG
&& GET_CODE (vfalse
) == SUBREG
)
1722 rtx reg_vtrue
= SUBREG_REG (vtrue
);
1723 rtx reg_vfalse
= SUBREG_REG (vfalse
);
1724 unsigned int byte_vtrue
= SUBREG_BYTE (vtrue
);
1725 unsigned int byte_vfalse
= SUBREG_BYTE (vfalse
);
1726 rtx promoted_target
;
1728 if (GET_MODE (reg_vtrue
) != GET_MODE (reg_vfalse
)
1729 || byte_vtrue
!= byte_vfalse
1730 || (SUBREG_PROMOTED_VAR_P (vtrue
)
1731 != SUBREG_PROMOTED_VAR_P (vfalse
))
1732 || (SUBREG_PROMOTED_GET (vtrue
)
1733 != SUBREG_PROMOTED_GET (vfalse
)))
1736 promoted_target
= gen_reg_rtx (GET_MODE (reg_vtrue
));
1738 target
= emit_conditional_move (promoted_target
, code
, cmp_a
, cmp_b
,
1739 VOIDmode
, reg_vtrue
, reg_vfalse
,
1740 GET_MODE (reg_vtrue
), unsignedp
);
1741 /* Nope, couldn't do it in that mode either. */
1745 target
= gen_rtx_SUBREG (GET_MODE (vtrue
), promoted_target
, byte_vtrue
);
1746 SUBREG_PROMOTED_VAR_P (target
) = SUBREG_PROMOTED_VAR_P (vtrue
);
1747 SUBREG_PROMOTED_SET (target
, SUBREG_PROMOTED_GET (vtrue
));
1748 emit_move_insn (x
, target
);
1755 /* Try only simple constants and registers here. More complex cases
1756 are handled in noce_try_cmove_arith after noce_try_store_flag_arith
1757 has had a go at it. */
1760 noce_try_cmove (struct noce_if_info
*if_info
)
1766 if (!noce_simple_bbs (if_info
))
1769 if ((CONSTANT_P (if_info
->a
) || register_operand (if_info
->a
, VOIDmode
))
1770 && (CONSTANT_P (if_info
->b
) || register_operand (if_info
->b
, VOIDmode
)))
1774 code
= GET_CODE (if_info
->cond
);
1775 target
= noce_emit_cmove (if_info
, if_info
->x
, code
,
1776 XEXP (if_info
->cond
, 0),
1777 XEXP (if_info
->cond
, 1),
1778 if_info
->a
, if_info
->b
);
1782 if (target
!= if_info
->x
)
1783 noce_emit_move_insn (if_info
->x
, target
);
1785 seq
= end_ifcvt_sequence (if_info
);
1786 if (!seq
|| !targetm
.noce_conversion_profitable_p (seq
, if_info
))
1789 emit_insn_before_setloc (seq
, if_info
->jump
,
1790 INSN_LOCATION (if_info
->insn_a
));
1791 if_info
->transform_name
= "noce_try_cmove";
1795 /* If both a and b are constants try a last-ditch transformation:
1796 if (test) x = a; else x = b;
1797 => x = (-(test != 0) & (b - a)) + a;
1798 Try this only if the target-specific expansion above has failed.
1799 The target-specific expander may want to generate sequences that
1800 we don't know about, so give them a chance before trying this
1802 else if (!targetm
.have_conditional_execution ()
1803 && CONST_INT_P (if_info
->a
) && CONST_INT_P (if_info
->b
))
1805 machine_mode mode
= GET_MODE (if_info
->x
);
1806 HOST_WIDE_INT ifalse
= INTVAL (if_info
->a
);
1807 HOST_WIDE_INT itrue
= INTVAL (if_info
->b
);
1808 rtx target
= noce_emit_store_flag (if_info
, if_info
->x
, false, -1);
1815 HOST_WIDE_INT diff
= (unsigned HOST_WIDE_INT
) itrue
- ifalse
;
1816 /* Make sure we can represent the difference
1817 between the two values. */
1819 != ((ifalse
< 0) != (itrue
< 0) ? ifalse
< 0 : ifalse
< itrue
))
1825 diff
= trunc_int_for_mode (diff
, mode
);
1826 target
= expand_simple_binop (mode
, AND
,
1827 target
, gen_int_mode (diff
, mode
),
1828 if_info
->x
, 0, OPTAB_WIDEN
);
1830 target
= expand_simple_binop (mode
, PLUS
,
1831 target
, gen_int_mode (ifalse
, mode
),
1832 if_info
->x
, 0, OPTAB_WIDEN
);
1835 if (target
!= if_info
->x
)
1836 noce_emit_move_insn (if_info
->x
, target
);
1838 seq
= end_ifcvt_sequence (if_info
);
1839 if (!seq
|| !targetm
.noce_conversion_profitable_p (seq
, if_info
))
1842 emit_insn_before_setloc (seq
, if_info
->jump
,
1843 INSN_LOCATION (if_info
->insn_a
));
1844 if_info
->transform_name
= "noce_try_cmove";
1860 /* Return true if X contains a conditional code mode rtx. */
1863 contains_ccmode_rtx_p (rtx x
)
1865 subrtx_iterator::array_type array
;
1866 FOR_EACH_SUBRTX (iter
, array
, x
, ALL
)
1867 if (GET_MODE_CLASS (GET_MODE (*iter
)) == MODE_CC
)
1873 /* Helper for bb_valid_for_noce_process_p. Validate that
1874 the rtx insn INSN is a single set that does not set
1875 the conditional register CC and is in general valid for
1879 insn_valid_noce_process_p (rtx_insn
*insn
, rtx cc
)
1882 || !NONJUMP_INSN_P (insn
)
1883 || (cc
&& set_of (cc
, insn
)))
1886 rtx sset
= single_set (insn
);
1888 /* Currently support only simple single sets in test_bb. */
1890 || !noce_operand_ok (SET_DEST (sset
))
1891 || contains_ccmode_rtx_p (SET_DEST (sset
))
1892 || !noce_operand_ok (SET_SRC (sset
)))
1899 /* Return true iff the registers that the insns in BB_A set do not get
1900 used in BB_B. If TO_RENAME is non-NULL then it is a location that will be
1901 renamed later by the caller and so conflicts on it should be ignored
1902 in this function. */
1905 bbs_ok_for_cmove_arith (basic_block bb_a
, basic_block bb_b
, rtx to_rename
)
1908 bitmap bba_sets
= BITMAP_ALLOC (®_obstack
);
1913 FOR_BB_INSNS (bb_a
, a_insn
)
1915 if (!active_insn_p (a_insn
))
1918 rtx sset_a
= single_set (a_insn
);
1922 BITMAP_FREE (bba_sets
);
1925 /* Record all registers that BB_A sets. */
1926 FOR_EACH_INSN_DEF (def
, a_insn
)
1927 if (!(to_rename
&& DF_REF_REG (def
) == to_rename
))
1928 bitmap_set_bit (bba_sets
, DF_REF_REGNO (def
));
1933 FOR_BB_INSNS (bb_b
, b_insn
)
1935 if (!active_insn_p (b_insn
))
1938 rtx sset_b
= single_set (b_insn
);
1942 BITMAP_FREE (bba_sets
);
1946 /* Make sure this is a REG and not some instance
1947 of ZERO_EXTRACT or SUBREG or other dangerous stuff.
1948 If we have a memory destination then we have a pair of simple
1949 basic blocks performing an operation of the form [addr] = c ? a : b.
1950 bb_valid_for_noce_process_p will have ensured that these are
1951 the only stores present. In that case [addr] should be the location
1952 to be renamed. Assert that the callers set this up properly. */
1953 if (MEM_P (SET_DEST (sset_b
)))
1954 gcc_assert (rtx_equal_p (SET_DEST (sset_b
), to_rename
));
1955 else if (!REG_P (SET_DEST (sset_b
)))
1957 BITMAP_FREE (bba_sets
);
1961 /* If the insn uses a reg set in BB_A return false. */
1962 FOR_EACH_INSN_USE (use
, b_insn
)
1964 if (bitmap_bit_p (bba_sets
, DF_REF_REGNO (use
)))
1966 BITMAP_FREE (bba_sets
);
1973 BITMAP_FREE (bba_sets
);
1977 /* Emit copies of all the active instructions in BB except the last.
1978 This is a helper for noce_try_cmove_arith. */
1981 noce_emit_all_but_last (basic_block bb
)
1983 rtx_insn
*last
= last_active_insn (bb
, FALSE
);
1985 FOR_BB_INSNS (bb
, insn
)
1987 if (insn
!= last
&& active_insn_p (insn
))
1989 rtx_insn
*to_emit
= as_a
<rtx_insn
*> (copy_rtx (insn
));
1991 emit_insn (PATTERN (to_emit
));
1996 /* Helper for noce_try_cmove_arith. Emit the pattern TO_EMIT and return
1997 the resulting insn or NULL if it's not a valid insn. */
2000 noce_emit_insn (rtx to_emit
)
2002 gcc_assert (to_emit
);
2003 rtx_insn
*insn
= emit_insn (to_emit
);
2005 if (recog_memoized (insn
) < 0)
2011 /* Helper for noce_try_cmove_arith. Emit a copy of the insns up to
2012 and including the penultimate one in BB if it is not simple
2013 (as indicated by SIMPLE). Then emit LAST_INSN as the last
2014 insn in the block. The reason for that is that LAST_INSN may
2015 have been modified by the preparation in noce_try_cmove_arith. */
2018 noce_emit_bb (rtx last_insn
, basic_block bb
, bool simple
)
2021 noce_emit_all_but_last (bb
);
2023 if (last_insn
&& !noce_emit_insn (last_insn
))
2029 /* Try more complex cases involving conditional_move. */
2032 noce_try_cmove_arith (struct noce_if_info
*if_info
)
2038 rtx_insn
*insn_a
, *insn_b
;
2039 bool a_simple
= if_info
->then_simple
;
2040 bool b_simple
= if_info
->else_simple
;
2041 basic_block then_bb
= if_info
->then_bb
;
2042 basic_block else_bb
= if_info
->else_bb
;
2046 rtx cond
= if_info
->cond
;
2047 rtx_insn
*ifcvt_seq
;
2049 /* A conditional move from two memory sources is equivalent to a
2050 conditional on their addresses followed by a load. Don't do this
2051 early because it'll screw alias analysis. Note that we've
2052 already checked for no side effects. */
2053 if (cse_not_expected
2054 && MEM_P (a
) && MEM_P (b
)
2055 && MEM_ADDR_SPACE (a
) == MEM_ADDR_SPACE (b
))
2057 machine_mode address_mode
= get_address_mode (a
);
2061 x
= gen_reg_rtx (address_mode
);
2065 /* ??? We could handle this if we knew that a load from A or B could
2066 not trap or fault. This is also true if we've already loaded
2067 from the address along the path from ENTRY. */
2068 else if (may_trap_or_fault_p (a
) || may_trap_or_fault_p (b
))
2071 /* if (test) x = a + b; else x = c - d;
2078 code
= GET_CODE (cond
);
2079 insn_a
= if_info
->insn_a
;
2080 insn_b
= if_info
->insn_b
;
2082 machine_mode x_mode
= GET_MODE (x
);
2084 if (!can_conditionally_move_p (x_mode
))
2087 /* Possibly rearrange operands to make things come out more natural. */
2088 if (noce_reversed_cond_code (if_info
) != UNKNOWN
)
2091 if (rtx_equal_p (b
, x
))
2093 else if (general_operand (b
, GET_MODE (b
)))
2098 if (if_info
->rev_cond
)
2100 cond
= if_info
->rev_cond
;
2101 code
= GET_CODE (cond
);
2104 code
= reversed_comparison_code (cond
, if_info
->jump
);
2106 std::swap (insn_a
, insn_b
);
2107 std::swap (a_simple
, b_simple
);
2108 std::swap (then_bb
, else_bb
);
2112 if (then_bb
&& else_bb
2113 && (!bbs_ok_for_cmove_arith (then_bb
, else_bb
, if_info
->orig_x
)
2114 || !bbs_ok_for_cmove_arith (else_bb
, then_bb
, if_info
->orig_x
)))
2119 /* If one of the blocks is empty then the corresponding B or A value
2120 came from the test block. The non-empty complex block that we will
2121 emit might clobber the register used by B or A, so move it to a pseudo
2124 rtx tmp_a
= NULL_RTX
;
2125 rtx tmp_b
= NULL_RTX
;
2127 if (b_simple
|| !else_bb
)
2128 tmp_b
= gen_reg_rtx (x_mode
);
2130 if (a_simple
|| !then_bb
)
2131 tmp_a
= gen_reg_rtx (x_mode
);
2136 rtx emit_a
= NULL_RTX
;
2137 rtx emit_b
= NULL_RTX
;
2138 rtx_insn
*tmp_insn
= NULL
;
2139 bool modified_in_a
= false;
2140 bool modified_in_b
= false;
2141 /* If either operand is complex, load it into a register first.
2142 The best way to do this is to copy the original insn. In this
2143 way we preserve any clobbers etc that the insn may have had.
2144 This is of course not possible in the IS_MEM case. */
2146 if (! general_operand (a
, GET_MODE (a
)) || tmp_a
)
2151 rtx reg
= gen_reg_rtx (GET_MODE (a
));
2152 emit_a
= gen_rtx_SET (reg
, a
);
2158 a
= tmp_a
? tmp_a
: gen_reg_rtx (GET_MODE (a
));
2160 rtx_insn
*copy_of_a
= as_a
<rtx_insn
*> (copy_rtx (insn_a
));
2161 rtx set
= single_set (copy_of_a
);
2164 emit_a
= PATTERN (copy_of_a
);
2168 rtx tmp_reg
= tmp_a
? tmp_a
: gen_reg_rtx (GET_MODE (a
));
2169 emit_a
= gen_rtx_SET (tmp_reg
, a
);
2175 if (! general_operand (b
, GET_MODE (b
)) || tmp_b
)
2179 rtx reg
= gen_reg_rtx (GET_MODE (b
));
2180 emit_b
= gen_rtx_SET (reg
, b
);
2186 b
= tmp_b
? tmp_b
: gen_reg_rtx (GET_MODE (b
));
2187 rtx_insn
*copy_of_b
= as_a
<rtx_insn
*> (copy_rtx (insn_b
));
2188 rtx set
= single_set (copy_of_b
);
2191 emit_b
= PATTERN (copy_of_b
);
2195 rtx tmp_reg
= tmp_b
? tmp_b
: gen_reg_rtx (GET_MODE (b
));
2196 emit_b
= gen_rtx_SET (tmp_reg
, b
);
2202 modified_in_a
= emit_a
!= NULL_RTX
&& modified_in_p (orig_b
, emit_a
);
2203 if (tmp_b
&& then_bb
)
2205 FOR_BB_INSNS (then_bb
, tmp_insn
)
2206 /* Don't check inside insn_a. We will have changed it to emit_a
2207 with a destination that doesn't conflict. */
2208 if (!(insn_a
&& tmp_insn
== insn_a
)
2209 && modified_in_p (orig_b
, tmp_insn
))
2211 modified_in_a
= true;
2217 modified_in_b
= emit_b
!= NULL_RTX
&& modified_in_p (orig_a
, emit_b
);
2218 if (tmp_a
&& else_bb
)
2220 FOR_BB_INSNS (else_bb
, tmp_insn
)
2221 /* Don't check inside insn_b. We will have changed it to emit_b
2222 with a destination that doesn't conflict. */
2223 if (!(insn_b
&& tmp_insn
== insn_b
)
2224 && modified_in_p (orig_a
, tmp_insn
))
2226 modified_in_b
= true;
2231 /* If insn to set up A clobbers any registers B depends on, try to
2232 swap insn that sets up A with the one that sets up B. If even
2233 that doesn't help, punt. */
2234 if (modified_in_a
&& !modified_in_b
)
2236 if (!noce_emit_bb (emit_b
, else_bb
, b_simple
))
2237 goto end_seq_and_fail
;
2239 if (!noce_emit_bb (emit_a
, then_bb
, a_simple
))
2240 goto end_seq_and_fail
;
2242 else if (!modified_in_a
)
2244 if (!noce_emit_bb (emit_a
, then_bb
, a_simple
))
2245 goto end_seq_and_fail
;
2247 if (!noce_emit_bb (emit_b
, else_bb
, b_simple
))
2248 goto end_seq_and_fail
;
2251 goto end_seq_and_fail
;
2253 target
= noce_emit_cmove (if_info
, x
, code
, XEXP (cond
, 0), XEXP (cond
, 1),
2257 goto end_seq_and_fail
;
2259 /* If we're handling a memory for above, emit the load now. */
2262 rtx mem
= gen_rtx_MEM (GET_MODE (if_info
->x
), target
);
2264 /* Copy over flags as appropriate. */
2265 if (MEM_VOLATILE_P (if_info
->a
) || MEM_VOLATILE_P (if_info
->b
))
2266 MEM_VOLATILE_P (mem
) = 1;
2267 if (MEM_ALIAS_SET (if_info
->a
) == MEM_ALIAS_SET (if_info
->b
))
2268 set_mem_alias_set (mem
, MEM_ALIAS_SET (if_info
->a
));
2270 MIN (MEM_ALIGN (if_info
->a
), MEM_ALIGN (if_info
->b
)));
2272 gcc_assert (MEM_ADDR_SPACE (if_info
->a
) == MEM_ADDR_SPACE (if_info
->b
));
2273 set_mem_addr_space (mem
, MEM_ADDR_SPACE (if_info
->a
));
2275 noce_emit_move_insn (if_info
->x
, mem
);
2277 else if (target
!= x
)
2278 noce_emit_move_insn (x
, target
);
2280 ifcvt_seq
= end_ifcvt_sequence (if_info
);
2281 if (!ifcvt_seq
|| !targetm
.noce_conversion_profitable_p (ifcvt_seq
, if_info
))
2284 emit_insn_before_setloc (ifcvt_seq
, if_info
->jump
,
2285 INSN_LOCATION (if_info
->insn_a
));
2286 if_info
->transform_name
= "noce_try_cmove_arith";
2294 /* For most cases, the simplified condition we found is the best
2295 choice, but this is not the case for the min/max/abs transforms.
2296 For these we wish to know that it is A or B in the condition. */
2299 noce_get_alt_condition (struct noce_if_info
*if_info
, rtx target
,
2300 rtx_insn
**earliest
)
2306 /* If target is already mentioned in the known condition, return it. */
2307 if (reg_mentioned_p (target
, if_info
->cond
))
2309 *earliest
= if_info
->cond_earliest
;
2310 return if_info
->cond
;
2313 set
= pc_set (if_info
->jump
);
2314 cond
= XEXP (SET_SRC (set
), 0);
2316 = GET_CODE (XEXP (SET_SRC (set
), 2)) == LABEL_REF
2317 && label_ref_label (XEXP (SET_SRC (set
), 2)) == JUMP_LABEL (if_info
->jump
);
2318 if (if_info
->then_else_reversed
)
2321 /* If we're looking for a constant, try to make the conditional
2322 have that constant in it. There are two reasons why it may
2323 not have the constant we want:
2325 1. GCC may have needed to put the constant in a register, because
2326 the target can't compare directly against that constant. For
2327 this case, we look for a SET immediately before the comparison
2328 that puts a constant in that register.
2330 2. GCC may have canonicalized the conditional, for example
2331 replacing "if x < 4" with "if x <= 3". We can undo that (or
2332 make equivalent types of changes) to get the constants we need
2333 if they're off by one in the right direction. */
2335 if (CONST_INT_P (target
))
2337 enum rtx_code code
= GET_CODE (if_info
->cond
);
2338 rtx op_a
= XEXP (if_info
->cond
, 0);
2339 rtx op_b
= XEXP (if_info
->cond
, 1);
2340 rtx_insn
*prev_insn
;
2342 /* First, look to see if we put a constant in a register. */
2343 prev_insn
= prev_nonnote_insn (if_info
->cond_earliest
);
2345 && BLOCK_FOR_INSN (prev_insn
)
2346 == BLOCK_FOR_INSN (if_info
->cond_earliest
)
2347 && INSN_P (prev_insn
)
2348 && GET_CODE (PATTERN (prev_insn
)) == SET
)
2350 rtx src
= find_reg_equal_equiv_note (prev_insn
);
2352 src
= SET_SRC (PATTERN (prev_insn
));
2353 if (CONST_INT_P (src
))
2355 if (rtx_equal_p (op_a
, SET_DEST (PATTERN (prev_insn
))))
2357 else if (rtx_equal_p (op_b
, SET_DEST (PATTERN (prev_insn
))))
2360 if (CONST_INT_P (op_a
))
2362 std::swap (op_a
, op_b
);
2363 code
= swap_condition (code
);
2368 /* Now, look to see if we can get the right constant by
2369 adjusting the conditional. */
2370 if (CONST_INT_P (op_b
))
2372 HOST_WIDE_INT desired_val
= INTVAL (target
);
2373 HOST_WIDE_INT actual_val
= INTVAL (op_b
);
2378 if (desired_val
!= HOST_WIDE_INT_MAX
2379 && actual_val
== desired_val
+ 1)
2382 op_b
= GEN_INT (desired_val
);
2386 if (desired_val
!= HOST_WIDE_INT_MIN
2387 && actual_val
== desired_val
- 1)
2390 op_b
= GEN_INT (desired_val
);
2394 if (desired_val
!= HOST_WIDE_INT_MIN
2395 && actual_val
== desired_val
- 1)
2398 op_b
= GEN_INT (desired_val
);
2402 if (desired_val
!= HOST_WIDE_INT_MAX
2403 && actual_val
== desired_val
+ 1)
2406 op_b
= GEN_INT (desired_val
);
2414 /* If we made any changes, generate a new conditional that is
2415 equivalent to what we started with, but has the right
2417 if (code
!= GET_CODE (if_info
->cond
)
2418 || op_a
!= XEXP (if_info
->cond
, 0)
2419 || op_b
!= XEXP (if_info
->cond
, 1))
2421 cond
= gen_rtx_fmt_ee (code
, GET_MODE (cond
), op_a
, op_b
);
2422 *earliest
= if_info
->cond_earliest
;
2427 cond
= canonicalize_condition (if_info
->jump
, cond
, reverse
,
2428 earliest
, target
, have_cbranchcc4
, true);
2429 if (! cond
|| ! reg_mentioned_p (target
, cond
))
2432 /* We almost certainly searched back to a different place.
2433 Need to re-verify correct lifetimes. */
2435 /* X may not be mentioned in the range (cond_earliest, jump]. */
2436 for (insn
= if_info
->jump
; insn
!= *earliest
; insn
= PREV_INSN (insn
))
2437 if (INSN_P (insn
) && reg_overlap_mentioned_p (if_info
->x
, PATTERN (insn
)))
2440 /* A and B may not be modified in the range [cond_earliest, jump). */
2441 for (insn
= *earliest
; insn
!= if_info
->jump
; insn
= NEXT_INSN (insn
))
2443 && (modified_in_p (if_info
->a
, insn
)
2444 || modified_in_p (if_info
->b
, insn
)))
2450 /* Convert "if (a < b) x = a; else x = b;" to "x = min(a, b);", etc. */
2453 noce_try_minmax (struct noce_if_info
*if_info
)
2456 rtx_insn
*earliest
, *seq
;
2457 enum rtx_code code
, op
;
2460 if (!noce_simple_bbs (if_info
))
2463 /* ??? Reject modes with NaNs or signed zeros since we don't know how
2464 they will be resolved with an SMIN/SMAX. It wouldn't be too hard
2465 to get the target to tell us... */
2466 if (HONOR_SIGNED_ZEROS (if_info
->x
)
2467 || HONOR_NANS (if_info
->x
))
2470 cond
= noce_get_alt_condition (if_info
, if_info
->a
, &earliest
);
2474 /* Verify the condition is of the form we expect, and canonicalize
2475 the comparison code. */
2476 code
= GET_CODE (cond
);
2477 if (rtx_equal_p (XEXP (cond
, 0), if_info
->a
))
2479 if (! rtx_equal_p (XEXP (cond
, 1), if_info
->b
))
2482 else if (rtx_equal_p (XEXP (cond
, 1), if_info
->a
))
2484 if (! rtx_equal_p (XEXP (cond
, 0), if_info
->b
))
2486 code
= swap_condition (code
);
2491 /* Determine what sort of operation this is. Note that the code is for
2492 a taken branch, so the code->operation mapping appears backwards. */
2525 target
= expand_simple_binop (GET_MODE (if_info
->x
), op
,
2526 if_info
->a
, if_info
->b
,
2527 if_info
->x
, unsignedp
, OPTAB_WIDEN
);
2533 if (target
!= if_info
->x
)
2534 noce_emit_move_insn (if_info
->x
, target
);
2536 seq
= end_ifcvt_sequence (if_info
);
2540 emit_insn_before_setloc (seq
, if_info
->jump
, INSN_LOCATION (if_info
->insn_a
));
2541 if_info
->cond
= cond
;
2542 if_info
->cond_earliest
= earliest
;
2543 if_info
->rev_cond
= NULL_RTX
;
2544 if_info
->transform_name
= "noce_try_minmax";
2549 /* Convert "if (a < 0) x = -a; else x = a;" to "x = abs(a);",
2550 "if (a < 0) x = ~a; else x = a;" to "x = one_cmpl_abs(a);",
2554 noce_try_abs (struct noce_if_info
*if_info
)
2556 rtx cond
, target
, a
, b
, c
;
2557 rtx_insn
*earliest
, *seq
;
2559 bool one_cmpl
= false;
2561 if (!noce_simple_bbs (if_info
))
2564 /* Reject modes with signed zeros. */
2565 if (HONOR_SIGNED_ZEROS (if_info
->x
))
2568 /* Recognize A and B as constituting an ABS or NABS. The canonical
2569 form is a branch around the negation, taken when the object is the
2570 first operand of a comparison against 0 that evaluates to true. */
2573 if (GET_CODE (a
) == NEG
&& rtx_equal_p (XEXP (a
, 0), b
))
2575 else if (GET_CODE (b
) == NEG
&& rtx_equal_p (XEXP (b
, 0), a
))
2580 else if (GET_CODE (a
) == NOT
&& rtx_equal_p (XEXP (a
, 0), b
))
2585 else if (GET_CODE (b
) == NOT
&& rtx_equal_p (XEXP (b
, 0), a
))
2594 cond
= noce_get_alt_condition (if_info
, b
, &earliest
);
2598 /* Verify the condition is of the form we expect. */
2599 if (rtx_equal_p (XEXP (cond
, 0), b
))
2601 else if (rtx_equal_p (XEXP (cond
, 1), b
))
2609 /* Verify that C is zero. Search one step backward for a
2610 REG_EQUAL note or a simple source if necessary. */
2614 rtx_insn
*insn
= prev_nonnote_insn (earliest
);
2616 && BLOCK_FOR_INSN (insn
) == BLOCK_FOR_INSN (earliest
)
2617 && (set
= single_set (insn
))
2618 && rtx_equal_p (SET_DEST (set
), c
))
2620 rtx note
= find_reg_equal_equiv_note (insn
);
2630 && GET_CODE (XEXP (c
, 0)) == SYMBOL_REF
2631 && CONSTANT_POOL_ADDRESS_P (XEXP (c
, 0)))
2632 c
= get_pool_constant (XEXP (c
, 0));
2634 /* Work around funny ideas get_condition has wrt canonicalization.
2635 Note that these rtx constants are known to be CONST_INT, and
2636 therefore imply integer comparisons.
2637 The one_cmpl case is more complicated, as we want to handle
2638 only x < 0 ? ~x : x or x >= 0 ? x : ~x to one_cmpl_abs (x)
2639 and x < 0 ? x : ~x or x >= 0 ? ~x : x to ~one_cmpl_abs (x),
2640 but not other cases (x > -1 is equivalent of x >= 0). */
2641 if (c
== constm1_rtx
&& GET_CODE (cond
) == GT
)
2643 else if (c
== const1_rtx
&& GET_CODE (cond
) == LT
)
2648 else if (c
== CONST0_RTX (GET_MODE (b
)))
2651 && GET_CODE (cond
) != GE
2652 && GET_CODE (cond
) != LT
)
2658 /* Determine what sort of operation this is. */
2659 switch (GET_CODE (cond
))
2678 target
= expand_one_cmpl_abs_nojump (GET_MODE (if_info
->x
), b
,
2681 target
= expand_abs_nojump (GET_MODE (if_info
->x
), b
, if_info
->x
, 1);
2683 /* ??? It's a quandary whether cmove would be better here, especially
2684 for integers. Perhaps combine will clean things up. */
2685 if (target
&& negate
)
2688 target
= expand_simple_unop (GET_MODE (target
), NOT
, target
,
2691 target
= expand_simple_unop (GET_MODE (target
), NEG
, target
,
2701 if (target
!= if_info
->x
)
2702 noce_emit_move_insn (if_info
->x
, target
);
2704 seq
= end_ifcvt_sequence (if_info
);
2708 emit_insn_before_setloc (seq
, if_info
->jump
, INSN_LOCATION (if_info
->insn_a
));
2709 if_info
->cond
= cond
;
2710 if_info
->cond_earliest
= earliest
;
2711 if_info
->rev_cond
= NULL_RTX
;
2712 if_info
->transform_name
= "noce_try_abs";
2717 /* Convert "if (m < 0) x = b; else x = 0;" to "x = (m >> C) & b;". */
2720 noce_try_sign_mask (struct noce_if_info
*if_info
)
2726 bool t_unconditional
;
2728 if (!noce_simple_bbs (if_info
))
2731 cond
= if_info
->cond
;
2732 code
= GET_CODE (cond
);
2737 if (if_info
->a
== const0_rtx
)
2739 if ((code
== LT
&& c
== const0_rtx
)
2740 || (code
== LE
&& c
== constm1_rtx
))
2743 else if (if_info
->b
== const0_rtx
)
2745 if ((code
== GE
&& c
== const0_rtx
)
2746 || (code
== GT
&& c
== constm1_rtx
))
2750 if (! t
|| side_effects_p (t
))
2753 /* We currently don't handle different modes. */
2754 mode
= GET_MODE (t
);
2755 if (GET_MODE (m
) != mode
)
2758 /* This is only profitable if T is unconditionally executed/evaluated in the
2759 original insn sequence or T is cheap. The former happens if B is the
2760 non-zero (T) value and if INSN_B was taken from TEST_BB, or there was no
2761 INSN_B which can happen for e.g. conditional stores to memory. For the
2762 cost computation use the block TEST_BB where the evaluation will end up
2763 after the transformation. */
2766 && (if_info
->insn_b
== NULL_RTX
2767 || BLOCK_FOR_INSN (if_info
->insn_b
) == if_info
->test_bb
));
2768 if (!(t_unconditional
2769 || (set_src_cost (t
, mode
, if_info
->speed_p
)
2770 < COSTS_N_INSNS (2))))
2774 /* Use emit_store_flag to generate "m < 0 ? -1 : 0" instead of expanding
2775 "(signed) m >> 31" directly. This benefits targets with specialized
2776 insns to obtain the signmask, but still uses ashr_optab otherwise. */
2777 m
= emit_store_flag (gen_reg_rtx (mode
), LT
, m
, const0_rtx
, mode
, 0, -1);
2778 t
= m
? expand_binop (mode
, and_optab
, m
, t
, NULL_RTX
, 0, OPTAB_DIRECT
)
2787 noce_emit_move_insn (if_info
->x
, t
);
2789 seq
= end_ifcvt_sequence (if_info
);
2793 emit_insn_before_setloc (seq
, if_info
->jump
, INSN_LOCATION (if_info
->insn_a
));
2794 if_info
->transform_name
= "noce_try_sign_mask";
2800 /* Optimize away "if (x & C) x |= C" and similar bit manipulation
2804 noce_try_bitop (struct noce_if_info
*if_info
)
2806 rtx cond
, x
, a
, result
;
2813 cond
= if_info
->cond
;
2814 code
= GET_CODE (cond
);
2816 if (!noce_simple_bbs (if_info
))
2819 /* Check for no else condition. */
2820 if (! rtx_equal_p (x
, if_info
->b
))
2823 /* Check for a suitable condition. */
2824 if (code
!= NE
&& code
!= EQ
)
2826 if (XEXP (cond
, 1) != const0_rtx
)
2828 cond
= XEXP (cond
, 0);
2830 /* ??? We could also handle AND here. */
2831 if (GET_CODE (cond
) == ZERO_EXTRACT
)
2833 if (XEXP (cond
, 1) != const1_rtx
2834 || !CONST_INT_P (XEXP (cond
, 2))
2835 || ! rtx_equal_p (x
, XEXP (cond
, 0)))
2837 bitnum
= INTVAL (XEXP (cond
, 2));
2838 mode
= GET_MODE (x
);
2839 if (BITS_BIG_ENDIAN
)
2840 bitnum
= GET_MODE_BITSIZE (mode
) - 1 - bitnum
;
2841 if (bitnum
< 0 || bitnum
>= HOST_BITS_PER_WIDE_INT
)
2848 if (GET_CODE (a
) == IOR
|| GET_CODE (a
) == XOR
)
2850 /* Check for "if (X & C) x = x op C". */
2851 if (! rtx_equal_p (x
, XEXP (a
, 0))
2852 || !CONST_INT_P (XEXP (a
, 1))
2853 || (INTVAL (XEXP (a
, 1)) & GET_MODE_MASK (mode
))
2854 != HOST_WIDE_INT_1U
<< bitnum
)
2857 /* if ((x & C) == 0) x |= C; is transformed to x |= C. */
2858 /* if ((x & C) != 0) x |= C; is transformed to nothing. */
2859 if (GET_CODE (a
) == IOR
)
2860 result
= (code
== NE
) ? a
: NULL_RTX
;
2861 else if (code
== NE
)
2863 /* if ((x & C) == 0) x ^= C; is transformed to x |= C. */
2864 result
= gen_int_mode (HOST_WIDE_INT_1
<< bitnum
, mode
);
2865 result
= simplify_gen_binary (IOR
, mode
, x
, result
);
2869 /* if ((x & C) != 0) x ^= C; is transformed to x &= ~C. */
2870 result
= gen_int_mode (~(HOST_WIDE_INT_1
<< bitnum
), mode
);
2871 result
= simplify_gen_binary (AND
, mode
, x
, result
);
2874 else if (GET_CODE (a
) == AND
)
2876 /* Check for "if (X & C) x &= ~C". */
2877 if (! rtx_equal_p (x
, XEXP (a
, 0))
2878 || !CONST_INT_P (XEXP (a
, 1))
2879 || (INTVAL (XEXP (a
, 1)) & GET_MODE_MASK (mode
))
2880 != (~(HOST_WIDE_INT_1
<< bitnum
) & GET_MODE_MASK (mode
)))
2883 /* if ((x & C) == 0) x &= ~C; is transformed to nothing. */
2884 /* if ((x & C) != 0) x &= ~C; is transformed to x &= ~C. */
2885 result
= (code
== EQ
) ? a
: NULL_RTX
;
2893 noce_emit_move_insn (x
, result
);
2894 seq
= end_ifcvt_sequence (if_info
);
2898 emit_insn_before_setloc (seq
, if_info
->jump
,
2899 INSN_LOCATION (if_info
->insn_a
));
2901 if_info
->transform_name
= "noce_try_bitop";
2906 /* Similar to get_condition, only the resulting condition must be
2907 valid at JUMP, instead of at EARLIEST.
2909 If THEN_ELSE_REVERSED is true, the fallthrough does not go to the
2910 THEN block of the caller, and we have to reverse the condition. */
2913 noce_get_condition (rtx_insn
*jump
, rtx_insn
**earliest
, bool then_else_reversed
)
2918 if (! any_condjump_p (jump
))
2921 set
= pc_set (jump
);
2923 /* If this branches to JUMP_LABEL when the condition is false,
2924 reverse the condition. */
2925 reverse
= (GET_CODE (XEXP (SET_SRC (set
), 2)) == LABEL_REF
2926 && label_ref_label (XEXP (SET_SRC (set
), 2)) == JUMP_LABEL (jump
));
2928 /* We may have to reverse because the caller's if block is not canonical,
2929 i.e. the THEN block isn't the fallthrough block for the TEST block
2930 (see find_if_header). */
2931 if (then_else_reversed
)
2934 /* If the condition variable is a register and is MODE_INT, accept it. */
2936 cond
= XEXP (SET_SRC (set
), 0);
2937 tmp
= XEXP (cond
, 0);
2938 if (REG_P (tmp
) && GET_MODE_CLASS (GET_MODE (tmp
)) == MODE_INT
2939 && (GET_MODE (tmp
) != BImode
2940 || !targetm
.small_register_classes_for_mode_p (BImode
)))
2945 cond
= gen_rtx_fmt_ee (reverse_condition (GET_CODE (cond
)),
2946 GET_MODE (cond
), tmp
, XEXP (cond
, 1));
2950 /* Otherwise, fall back on canonicalize_condition to do the dirty
2951 work of manipulating MODE_CC values and COMPARE rtx codes. */
2952 tmp
= canonicalize_condition (jump
, cond
, reverse
, earliest
,
2953 NULL_RTX
, have_cbranchcc4
, true);
2955 /* We don't handle side-effects in the condition, like handling
2956 REG_INC notes and making sure no duplicate conditions are emitted. */
2957 if (tmp
!= NULL_RTX
&& side_effects_p (tmp
))
2963 /* Return true if OP is ok for if-then-else processing. */
2966 noce_operand_ok (const_rtx op
)
2968 if (side_effects_p (op
))
2971 /* We special-case memories, so handle any of them with
2972 no address side effects. */
2974 return ! side_effects_p (XEXP (op
, 0));
2976 return ! may_trap_p (op
);
2979 /* Return true iff basic block TEST_BB is valid for noce if-conversion.
2980 The condition used in this if-conversion is in COND.
2981 In practice, check that TEST_BB ends with a single set
2982 x := a and all previous computations
2983 in TEST_BB don't produce any values that are live after TEST_BB.
2984 In other words, all the insns in TEST_BB are there only
2985 to compute a value for x. Add the rtx cost of the insns
2986 in TEST_BB to COST. Record whether TEST_BB is a single simple
2987 set instruction in SIMPLE_P. */
2990 bb_valid_for_noce_process_p (basic_block test_bb
, rtx cond
,
2991 unsigned int *cost
, bool *simple_p
)
2996 rtx_insn
*last_insn
= last_active_insn (test_bb
, FALSE
);
2997 rtx last_set
= NULL_RTX
;
2999 rtx cc
= cc_in_cond (cond
);
3001 if (!insn_valid_noce_process_p (last_insn
, cc
))
3003 last_set
= single_set (last_insn
);
3005 rtx x
= SET_DEST (last_set
);
3006 rtx_insn
*first_insn
= first_active_insn (test_bb
);
3007 rtx first_set
= single_set (first_insn
);
3012 /* We have a single simple set, that's okay. */
3013 bool speed_p
= optimize_bb_for_speed_p (test_bb
);
3015 if (first_insn
== last_insn
)
3017 *simple_p
= noce_operand_ok (SET_DEST (first_set
));
3018 *cost
+= insn_rtx_cost (first_set
, speed_p
);
3022 rtx_insn
*prev_last_insn
= PREV_INSN (last_insn
);
3023 gcc_assert (prev_last_insn
);
3025 /* For now, disallow setting x multiple times in test_bb. */
3026 if (REG_P (x
) && reg_set_between_p (x
, first_insn
, prev_last_insn
))
3029 bitmap test_bb_temps
= BITMAP_ALLOC (®_obstack
);
3031 /* The regs that are live out of test_bb. */
3032 bitmap test_bb_live_out
= df_get_live_out (test_bb
);
3034 int potential_cost
= insn_rtx_cost (last_set
, speed_p
);
3036 FOR_BB_INSNS (test_bb
, insn
)
3038 if (insn
!= last_insn
)
3040 if (!active_insn_p (insn
))
3043 if (!insn_valid_noce_process_p (insn
, cc
))
3044 goto free_bitmap_and_fail
;
3046 rtx sset
= single_set (insn
);
3049 if (contains_mem_rtx_p (SET_SRC (sset
))
3050 || !REG_P (SET_DEST (sset
))
3051 || reg_overlap_mentioned_p (SET_DEST (sset
), cond
))
3052 goto free_bitmap_and_fail
;
3054 potential_cost
+= insn_rtx_cost (sset
, speed_p
);
3055 bitmap_set_bit (test_bb_temps
, REGNO (SET_DEST (sset
)));
3059 /* If any of the intermediate results in test_bb are live after test_bb
3061 if (bitmap_intersect_p (test_bb_live_out
, test_bb_temps
))
3062 goto free_bitmap_and_fail
;
3064 BITMAP_FREE (test_bb_temps
);
3065 *cost
+= potential_cost
;
3069 free_bitmap_and_fail
:
3070 BITMAP_FREE (test_bb_temps
);
3074 /* We have something like:
3077 { i = a; j = b; k = c; }
3081 tmp_i = (x > y) ? a : i;
3082 tmp_j = (x > y) ? b : j;
3083 tmp_k = (x > y) ? c : k;
3088 Subsequent passes are expected to clean up the extra moves.
3090 Look for special cases such as writes to one register which are
3091 read back in another SET, as might occur in a swap idiom or
3100 Which we want to rewrite to:
3102 tmp_i = (x > y) ? a : i;
3103 tmp_j = (x > y) ? tmp_i : j;
3107 We can catch these when looking at (SET x y) by keeping a list of the
3108 registers we would have targeted before if-conversion and looking back
3109 through it for an overlap with Y. If we find one, we rewire the
3110 conditional set to use the temporary we introduced earlier.
3112 IF_INFO contains the useful information about the block structure and
3113 jump instructions. */
3116 noce_convert_multiple_sets (struct noce_if_info
*if_info
)
3118 basic_block test_bb
= if_info
->test_bb
;
3119 basic_block then_bb
= if_info
->then_bb
;
3120 basic_block join_bb
= if_info
->join_bb
;
3121 rtx_insn
*jump
= if_info
->jump
;
3122 rtx_insn
*cond_earliest
;
3127 /* Decompose the condition attached to the jump. */
3128 rtx cond
= noce_get_condition (jump
, &cond_earliest
, false);
3129 rtx x
= XEXP (cond
, 0);
3130 rtx y
= XEXP (cond
, 1);
3131 rtx_code cond_code
= GET_CODE (cond
);
3133 /* The true targets for a conditional move. */
3134 auto_vec
<rtx
> targets
;
3135 /* The temporaries introduced to allow us to not consider register
3137 auto_vec
<rtx
> temporaries
;
3138 /* The insns we've emitted. */
3139 auto_vec
<rtx_insn
*> unmodified_insns
;
3142 FOR_BB_INSNS (then_bb
, insn
)
3144 /* Skip over non-insns. */
3145 if (!active_insn_p (insn
))
3148 rtx set
= single_set (insn
);
3149 gcc_checking_assert (set
);
3151 rtx target
= SET_DEST (set
);
3152 rtx temp
= gen_reg_rtx (GET_MODE (target
));
3153 rtx new_val
= SET_SRC (set
);
3154 rtx old_val
= target
;
3156 /* If we were supposed to read from an earlier write in this block,
3157 we've changed the register allocation. Rewire the read. While
3158 we are looking, also try to catch a swap idiom. */
3159 for (int i
= count
- 1; i
>= 0; --i
)
3160 if (reg_overlap_mentioned_p (new_val
, targets
[i
]))
3162 /* Catch a "swap" style idiom. */
3163 if (find_reg_note (insn
, REG_DEAD
, new_val
) != NULL_RTX
)
3164 /* The write to targets[i] is only live until the read
3165 here. As the condition codes match, we can propagate
3167 new_val
= SET_SRC (single_set (unmodified_insns
[i
]));
3169 new_val
= temporaries
[i
];
3173 /* If we had a non-canonical conditional jump (i.e. one where
3174 the fallthrough is to the "else" case) we need to reverse
3175 the conditional select. */
3176 if (if_info
->then_else_reversed
)
3177 std::swap (old_val
, new_val
);
3180 /* We allow simple lowpart register subreg SET sources in
3181 bb_ok_for_noce_convert_multiple_sets. Be careful when processing
3183 (set (reg:SI r1) (reg:SI r2))
3184 (set (reg:HI r3) (subreg:HI (r1)))
3185 For the second insn new_val or old_val (r1 in this example) will be
3186 taken from the temporaries and have the wider mode which will not
3187 match with the mode of the other source of the conditional move, so
3188 we'll end up trying to emit r4:HI = cond ? (r1:SI) : (r3:HI).
3189 Wrap the two cmove operands into subregs if appropriate to prevent
3191 if (GET_MODE (new_val
) != GET_MODE (temp
))
3193 machine_mode src_mode
= GET_MODE (new_val
);
3194 machine_mode dst_mode
= GET_MODE (temp
);
3195 if (GET_MODE_SIZE (src_mode
) <= GET_MODE_SIZE (dst_mode
))
3200 new_val
= lowpart_subreg (dst_mode
, new_val
, src_mode
);
3202 if (GET_MODE (old_val
) != GET_MODE (temp
))
3204 machine_mode src_mode
= GET_MODE (old_val
);
3205 machine_mode dst_mode
= GET_MODE (temp
);
3206 if (GET_MODE_SIZE (src_mode
) <= GET_MODE_SIZE (dst_mode
))
3211 old_val
= lowpart_subreg (dst_mode
, old_val
, src_mode
);
3214 /* Actually emit the conditional move. */
3215 rtx temp_dest
= noce_emit_cmove (if_info
, temp
, cond_code
,
3216 x
, y
, new_val
, old_val
);
3218 /* If we failed to expand the conditional move, drop out and don't
3220 if (temp_dest
== NULL_RTX
)
3228 targets
.safe_push (target
);
3229 temporaries
.safe_push (temp_dest
);
3230 unmodified_insns
.safe_push (insn
);
3233 /* We must have seen some sort of insn to insert, otherwise we were
3234 given an empty BB to convert, and we can't handle that. */
3235 gcc_assert (!unmodified_insns
.is_empty ());
3237 /* Now fixup the assignments. */
3238 for (int i
= 0; i
< count
; i
++)
3239 noce_emit_move_insn (targets
[i
], temporaries
[i
]);
3241 /* Actually emit the sequence if it isn't too expensive. */
3242 rtx_insn
*seq
= get_insns ();
3244 if (!targetm
.noce_conversion_profitable_p (seq
, if_info
))
3250 for (insn
= seq
; insn
; insn
= NEXT_INSN (insn
))
3251 set_used_flags (insn
);
3253 /* Mark all our temporaries and targets as used. */
3254 for (int i
= 0; i
< count
; i
++)
3256 set_used_flags (temporaries
[i
]);
3257 set_used_flags (targets
[i
]);
3260 set_used_flags (cond
);
3264 unshare_all_rtl_in_chain (seq
);
3270 for (insn
= seq
; insn
; insn
= NEXT_INSN (insn
))
3272 || recog_memoized (insn
) == -1)
3275 emit_insn_before_setloc (seq
, if_info
->jump
,
3276 INSN_LOCATION (unmodified_insns
.last ()));
3278 /* Clean up THEN_BB and the edges in and out of it. */
3279 remove_edge (find_edge (test_bb
, join_bb
));
3280 remove_edge (find_edge (then_bb
, join_bb
));
3281 redirect_edge_and_branch_force (single_succ_edge (test_bb
), join_bb
);
3282 delete_basic_block (then_bb
);
3285 /* Maybe merge blocks now the jump is simple enough. */
3286 if (can_merge_blocks_p (test_bb
, join_bb
))
3288 merge_blocks (test_bb
, join_bb
);
3292 num_updated_if_blocks
++;
3293 if_info
->transform_name
= "noce_convert_multiple_sets";
3297 /* Return true iff basic block TEST_BB is comprised of only
3298 (SET (REG) (REG)) insns suitable for conversion to a series
3299 of conditional moves. Also check that we have more than one set
3300 (other routines can handle a single set better than we would), and
3301 fewer than PARAM_MAX_RTL_IF_CONVERSION_INSNS sets. */
3304 bb_ok_for_noce_convert_multiple_sets (basic_block test_bb
)
3308 unsigned param
= PARAM_VALUE (PARAM_MAX_RTL_IF_CONVERSION_INSNS
);
3310 FOR_BB_INSNS (test_bb
, insn
)
3312 /* Skip over notes etc. */
3313 if (!active_insn_p (insn
))
3316 /* We only handle SET insns. */
3317 rtx set
= single_set (insn
);
3318 if (set
== NULL_RTX
)
3321 rtx dest
= SET_DEST (set
);
3322 rtx src
= SET_SRC (set
);
3324 /* We can possibly relax this, but for now only handle REG to REG
3325 (including subreg) moves. This avoids any issues that might come
3326 from introducing loads/stores that might violate data-race-freedom
3332 || (GET_CODE (src
) == SUBREG
&& REG_P (SUBREG_REG (src
))
3333 && subreg_lowpart_p (src
))))
3336 /* Destination must be appropriate for a conditional write. */
3337 if (!noce_operand_ok (dest
))
3340 /* We must be able to conditionally move in this mode. */
3341 if (!can_conditionally_move_p (GET_MODE (dest
)))
3347 /* If we would only put out one conditional move, the other strategies
3348 this pass tries are better optimized and will be more appropriate.
3349 Some targets want to strictly limit the number of conditional moves
3350 that are emitted, they set this through PARAM, we need to respect
3352 return count
> 1 && count
<= param
;
3355 /* Given a simple IF-THEN-JOIN or IF-THEN-ELSE-JOIN block, attempt to convert
3356 it without using conditional execution. Return TRUE if we were successful
3357 at converting the block. */
3360 noce_process_if_block (struct noce_if_info
*if_info
)
3362 basic_block test_bb
= if_info
->test_bb
; /* test block */
3363 basic_block then_bb
= if_info
->then_bb
; /* THEN */
3364 basic_block else_bb
= if_info
->else_bb
; /* ELSE or NULL */
3365 basic_block join_bb
= if_info
->join_bb
; /* JOIN */
3366 rtx_insn
*jump
= if_info
->jump
;
3367 rtx cond
= if_info
->cond
;
3368 rtx_insn
*insn_a
, *insn_b
;
3370 rtx orig_x
, x
, a
, b
;
3372 /* We're looking for patterns of the form
3374 (1) if (...) x = a; else x = b;
3375 (2) x = b; if (...) x = a;
3376 (3) if (...) x = a; // as if with an initial x = x.
3377 (4) if (...) { x = a; y = b; z = c; } // Like 3, for multiple SETS.
3378 The later patterns require jumps to be more expensive.
3379 For the if (...) x = a; else x = b; case we allow multiple insns
3380 inside the then and else blocks as long as their only effect is
3381 to calculate a value for x.
3382 ??? For future expansion, further expand the "multiple X" rules. */
3384 /* First look for multiple SETS. */
3386 && HAVE_conditional_move
3388 && bb_ok_for_noce_convert_multiple_sets (then_bb
))
3390 if (noce_convert_multiple_sets (if_info
))
3392 if (dump_file
&& if_info
->transform_name
)
3393 fprintf (dump_file
, "if-conversion succeeded through %s\n",
3394 if_info
->transform_name
);
3399 bool speed_p
= optimize_bb_for_speed_p (test_bb
);
3400 unsigned int then_cost
= 0, else_cost
= 0;
3401 if (!bb_valid_for_noce_process_p (then_bb
, cond
, &then_cost
,
3402 &if_info
->then_simple
))
3406 && !bb_valid_for_noce_process_p (else_bb
, cond
, &else_cost
,
3407 &if_info
->else_simple
))
3410 if (else_bb
== NULL
)
3411 if_info
->original_cost
+= then_cost
;
3413 if_info
->original_cost
+= MIN (then_cost
, else_cost
);
3415 if_info
->original_cost
+= then_cost
+ else_cost
;
3417 insn_a
= last_active_insn (then_bb
, FALSE
);
3418 set_a
= single_set (insn_a
);
3421 x
= SET_DEST (set_a
);
3422 a
= SET_SRC (set_a
);
3424 /* Look for the other potential set. Make sure we've got equivalent
3426 /* ??? This is overconservative. Storing to two different mems is
3427 as easy as conditionally computing the address. Storing to a
3428 single mem merely requires a scratch memory to use as one of the
3429 destination addresses; often the memory immediately below the
3430 stack pointer is available for this. */
3434 insn_b
= last_active_insn (else_bb
, FALSE
);
3435 set_b
= single_set (insn_b
);
3438 if (!rtx_interchangeable_p (x
, SET_DEST (set_b
)))
3443 insn_b
= prev_nonnote_nondebug_insn (if_info
->cond_earliest
);
3444 /* We're going to be moving the evaluation of B down from above
3445 COND_EARLIEST to JUMP. Make sure the relevant data is still
3448 || BLOCK_FOR_INSN (insn_b
) != BLOCK_FOR_INSN (if_info
->cond_earliest
)
3449 || !NONJUMP_INSN_P (insn_b
)
3450 || (set_b
= single_set (insn_b
)) == NULL_RTX
3451 || ! rtx_interchangeable_p (x
, SET_DEST (set_b
))
3452 || ! noce_operand_ok (SET_SRC (set_b
))
3453 || reg_overlap_mentioned_p (x
, SET_SRC (set_b
))
3454 || modified_between_p (SET_SRC (set_b
), insn_b
, jump
)
3455 /* Avoid extending the lifetime of hard registers on small
3456 register class machines. */
3457 || (REG_P (SET_SRC (set_b
))
3458 && HARD_REGISTER_P (SET_SRC (set_b
))
3459 && targetm
.small_register_classes_for_mode_p
3460 (GET_MODE (SET_SRC (set_b
))))
3461 /* Likewise with X. In particular this can happen when
3462 noce_get_condition looks farther back in the instruction
3463 stream than one might expect. */
3464 || reg_overlap_mentioned_p (x
, cond
)
3465 || reg_overlap_mentioned_p (x
, a
)
3466 || modified_between_p (x
, insn_b
, jump
))
3473 /* If x has side effects then only the if-then-else form is safe to
3474 convert. But even in that case we would need to restore any notes
3475 (such as REG_INC) at then end. That can be tricky if
3476 noce_emit_move_insn expands to more than one insn, so disable the
3477 optimization entirely for now if there are side effects. */
3478 if (side_effects_p (x
))
3481 b
= (set_b
? SET_SRC (set_b
) : x
);
3483 /* Only operate on register destinations, and even then avoid extending
3484 the lifetime of hard registers on small register class machines. */
3486 if_info
->orig_x
= orig_x
;
3488 || (HARD_REGISTER_P (x
)
3489 && targetm
.small_register_classes_for_mode_p (GET_MODE (x
))))
3491 if (GET_MODE (x
) == BLKmode
)
3494 if (GET_CODE (x
) == ZERO_EXTRACT
3495 && (!CONST_INT_P (XEXP (x
, 1))
3496 || !CONST_INT_P (XEXP (x
, 2))))
3499 x
= gen_reg_rtx (GET_MODE (GET_CODE (x
) == STRICT_LOW_PART
3500 ? XEXP (x
, 0) : x
));
3503 /* Don't operate on sources that may trap or are volatile. */
3504 if (! noce_operand_ok (a
) || ! noce_operand_ok (b
))
3508 /* Set up the info block for our subroutines. */
3509 if_info
->insn_a
= insn_a
;
3510 if_info
->insn_b
= insn_b
;
3515 /* Try optimizations in some approximation of a useful order. */
3516 /* ??? Should first look to see if X is live incoming at all. If it
3517 isn't, we don't need anything but an unconditional set. */
3519 /* Look and see if A and B are really the same. Avoid creating silly
3520 cmove constructs that no one will fix up later. */
3521 if (noce_simple_bbs (if_info
)
3522 && rtx_interchangeable_p (a
, b
))
3524 /* If we have an INSN_B, we don't have to create any new rtl. Just
3525 move the instruction that we already have. If we don't have an
3526 INSN_B, that means that A == X, and we've got a noop move. In
3527 that case don't do anything and let the code below delete INSN_A. */
3528 if (insn_b
&& else_bb
)
3532 if (else_bb
&& insn_b
== BB_END (else_bb
))
3533 BB_END (else_bb
) = PREV_INSN (insn_b
);
3534 reorder_insns (insn_b
, insn_b
, PREV_INSN (jump
));
3536 /* If there was a REG_EQUAL note, delete it since it may have been
3537 true due to this insn being after a jump. */
3538 if ((note
= find_reg_note (insn_b
, REG_EQUAL
, NULL_RTX
)) != 0)
3539 remove_note (insn_b
, note
);
3543 /* If we have "x = b; if (...) x = a;", and x has side-effects, then
3544 x must be executed twice. */
3545 else if (insn_b
&& side_effects_p (orig_x
))
3552 if (!set_b
&& MEM_P (orig_x
))
3553 /* We want to avoid store speculation to avoid cases like
3554 if (pthread_mutex_trylock(mutex))
3556 Rather than go to much effort here, we rely on the SSA optimizers,
3557 which do a good enough job these days. */
3560 if (noce_try_move (if_info
))
3562 if (noce_try_ifelse_collapse (if_info
))
3564 if (noce_try_store_flag (if_info
))
3566 if (noce_try_bitop (if_info
))
3568 if (noce_try_minmax (if_info
))
3570 if (noce_try_abs (if_info
))
3572 if (noce_try_inverse_constants (if_info
))
3574 if (!targetm
.have_conditional_execution ()
3575 && noce_try_store_flag_constants (if_info
))
3577 if (HAVE_conditional_move
3578 && noce_try_cmove (if_info
))
3580 if (! targetm
.have_conditional_execution ())
3582 if (noce_try_addcc (if_info
))
3584 if (noce_try_store_flag_mask (if_info
))
3586 if (HAVE_conditional_move
3587 && noce_try_cmove_arith (if_info
))
3589 if (noce_try_sign_mask (if_info
))
3593 if (!else_bb
&& set_b
)
3604 if (dump_file
&& if_info
->transform_name
)
3605 fprintf (dump_file
, "if-conversion succeeded through %s\n",
3606 if_info
->transform_name
);
3608 /* If we used a temporary, fix it up now. */
3614 noce_emit_move_insn (orig_x
, x
);
3616 set_used_flags (orig_x
);
3617 unshare_all_rtl_in_chain (seq
);
3620 emit_insn_before_setloc (seq
, BB_END (test_bb
), INSN_LOCATION (insn_a
));
3623 /* The original THEN and ELSE blocks may now be removed. The test block
3624 must now jump to the join block. If the test block and the join block
3625 can be merged, do so. */
3628 delete_basic_block (else_bb
);
3632 remove_edge (find_edge (test_bb
, join_bb
));
3634 remove_edge (find_edge (then_bb
, join_bb
));
3635 redirect_edge_and_branch_force (single_succ_edge (test_bb
), join_bb
);
3636 delete_basic_block (then_bb
);
3639 if (can_merge_blocks_p (test_bb
, join_bb
))
3641 merge_blocks (test_bb
, join_bb
);
3645 num_updated_if_blocks
++;
3649 /* Check whether a block is suitable for conditional move conversion.
3650 Every insn must be a simple set of a register to a constant or a
3651 register. For each assignment, store the value in the pointer map
3652 VALS, keyed indexed by register pointer, then store the register
3653 pointer in REGS. COND is the condition we will test. */
3656 check_cond_move_block (basic_block bb
,
3657 hash_map
<rtx
, rtx
> *vals
,
3662 rtx cc
= cc_in_cond (cond
);
3664 /* We can only handle simple jumps at the end of the basic block.
3665 It is almost impossible to update the CFG otherwise. */
3667 if (JUMP_P (insn
) && !onlyjump_p (insn
))
3670 FOR_BB_INSNS (bb
, insn
)
3674 if (!NONDEBUG_INSN_P (insn
) || JUMP_P (insn
))
3676 set
= single_set (insn
);
3680 dest
= SET_DEST (set
);
3681 src
= SET_SRC (set
);
3683 || (HARD_REGISTER_P (dest
)
3684 && targetm
.small_register_classes_for_mode_p (GET_MODE (dest
))))
3687 if (!CONSTANT_P (src
) && !register_operand (src
, VOIDmode
))
3690 if (side_effects_p (src
) || side_effects_p (dest
))
3693 if (may_trap_p (src
) || may_trap_p (dest
))
3696 /* Don't try to handle this if the source register was
3697 modified earlier in the block. */
3700 || (GET_CODE (src
) == SUBREG
&& REG_P (SUBREG_REG (src
))
3701 && vals
->get (SUBREG_REG (src
))))
3704 /* Don't try to handle this if the destination register was
3705 modified earlier in the block. */
3706 if (vals
->get (dest
))
3709 /* Don't try to handle this if the condition uses the
3710 destination register. */
3711 if (reg_overlap_mentioned_p (dest
, cond
))
3714 /* Don't try to handle this if the source register is modified
3715 later in the block. */
3716 if (!CONSTANT_P (src
)
3717 && modified_between_p (src
, insn
, NEXT_INSN (BB_END (bb
))))
3720 /* Skip it if the instruction to be moved might clobber CC. */
3721 if (cc
&& set_of (cc
, insn
))
3724 vals
->put (dest
, src
);
3726 regs
->safe_push (dest
);
3732 /* Given a basic block BB suitable for conditional move conversion,
3733 a condition COND, and pointer maps THEN_VALS and ELSE_VALS containing
3734 the register values depending on COND, emit the insns in the block as
3735 conditional moves. If ELSE_BLOCK is true, THEN_BB was already
3736 processed. The caller has started a sequence for the conversion.
3737 Return true if successful, false if something goes wrong. */
3740 cond_move_convert_if_block (struct noce_if_info
*if_infop
,
3741 basic_block bb
, rtx cond
,
3742 hash_map
<rtx
, rtx
> *then_vals
,
3743 hash_map
<rtx
, rtx
> *else_vals
,
3748 rtx cond_arg0
, cond_arg1
;
3750 code
= GET_CODE (cond
);
3751 cond_arg0
= XEXP (cond
, 0);
3752 cond_arg1
= XEXP (cond
, 1);
3754 FOR_BB_INSNS (bb
, insn
)
3756 rtx set
, target
, dest
, t
, e
;
3758 /* ??? Maybe emit conditional debug insn? */
3759 if (!NONDEBUG_INSN_P (insn
) || JUMP_P (insn
))
3761 set
= single_set (insn
);
3762 gcc_assert (set
&& REG_P (SET_DEST (set
)));
3764 dest
= SET_DEST (set
);
3766 rtx
*then_slot
= then_vals
->get (dest
);
3767 rtx
*else_slot
= else_vals
->get (dest
);
3768 t
= then_slot
? *then_slot
: NULL_RTX
;
3769 e
= else_slot
? *else_slot
: NULL_RTX
;
3773 /* If this register was set in the then block, we already
3774 handled this case there. */
3787 target
= noce_emit_cmove (if_infop
, dest
, code
, cond_arg0
, cond_arg1
,
3793 noce_emit_move_insn (dest
, target
);
3799 /* Given a simple IF-THEN-JOIN or IF-THEN-ELSE-JOIN block, attempt to convert
3800 it using only conditional moves. Return TRUE if we were successful at
3801 converting the block. */
3804 cond_move_process_if_block (struct noce_if_info
*if_info
)
3806 basic_block test_bb
= if_info
->test_bb
;
3807 basic_block then_bb
= if_info
->then_bb
;
3808 basic_block else_bb
= if_info
->else_bb
;
3809 basic_block join_bb
= if_info
->join_bb
;
3810 rtx_insn
*jump
= if_info
->jump
;
3811 rtx cond
= if_info
->cond
;
3812 rtx_insn
*seq
, *loc_insn
;
3815 vec
<rtx
> then_regs
= vNULL
;
3816 vec
<rtx
> else_regs
= vNULL
;
3818 int success_p
= FALSE
;
3819 int limit
= PARAM_VALUE (PARAM_MAX_RTL_IF_CONVERSION_INSNS
);
3821 /* Build a mapping for each block to the value used for each
3823 hash_map
<rtx
, rtx
> then_vals
;
3824 hash_map
<rtx
, rtx
> else_vals
;
3826 /* Make sure the blocks are suitable. */
3827 if (!check_cond_move_block (then_bb
, &then_vals
, &then_regs
, cond
)
3829 && !check_cond_move_block (else_bb
, &else_vals
, &else_regs
, cond
)))
3832 /* Make sure the blocks can be used together. If the same register
3833 is set in both blocks, and is not set to a constant in both
3834 cases, then both blocks must set it to the same register. We
3835 have already verified that if it is set to a register, that the
3836 source register does not change after the assignment. Also count
3837 the number of registers set in only one of the blocks. */
3839 FOR_EACH_VEC_ELT (then_regs
, i
, reg
)
3841 rtx
*then_slot
= then_vals
.get (reg
);
3842 rtx
*else_slot
= else_vals
.get (reg
);
3844 gcc_checking_assert (then_slot
);
3849 rtx then_val
= *then_slot
;
3850 rtx else_val
= *else_slot
;
3851 if (!CONSTANT_P (then_val
) && !CONSTANT_P (else_val
)
3852 && !rtx_equal_p (then_val
, else_val
))
3857 /* Finish off c for MAX_CONDITIONAL_EXECUTE. */
3858 FOR_EACH_VEC_ELT (else_regs
, i
, reg
)
3860 gcc_checking_assert (else_vals
.get (reg
));
3861 if (!then_vals
.get (reg
))
3865 /* Make sure it is reasonable to convert this block. What matters
3866 is the number of assignments currently made in only one of the
3867 branches, since if we convert we are going to always execute
3869 if (c
> MAX_CONDITIONAL_EXECUTE
3873 /* Try to emit the conditional moves. First do the then block,
3874 then do anything left in the else blocks. */
3876 if (!cond_move_convert_if_block (if_info
, then_bb
, cond
,
3877 &then_vals
, &else_vals
, false)
3879 && !cond_move_convert_if_block (if_info
, else_bb
, cond
,
3880 &then_vals
, &else_vals
, true)))
3885 seq
= end_ifcvt_sequence (if_info
);
3889 loc_insn
= first_active_insn (then_bb
);
3892 loc_insn
= first_active_insn (else_bb
);
3893 gcc_assert (loc_insn
);
3895 emit_insn_before_setloc (seq
, jump
, INSN_LOCATION (loc_insn
));
3899 delete_basic_block (else_bb
);
3903 remove_edge (find_edge (test_bb
, join_bb
));
3905 remove_edge (find_edge (then_bb
, join_bb
));
3906 redirect_edge_and_branch_force (single_succ_edge (test_bb
), join_bb
);
3907 delete_basic_block (then_bb
);
3910 if (can_merge_blocks_p (test_bb
, join_bb
))
3912 merge_blocks (test_bb
, join_bb
);
3916 num_updated_if_blocks
++;
3920 then_regs
.release ();
3921 else_regs
.release ();
3926 /* Determine if a given basic block heads a simple IF-THEN-JOIN or an
3927 IF-THEN-ELSE-JOIN block.
3929 If so, we'll try to convert the insns to not require the branch,
3930 using only transformations that do not require conditional execution.
3932 Return TRUE if we were successful at converting the block. */
3935 noce_find_if_block (basic_block test_bb
, edge then_edge
, edge else_edge
,
3938 basic_block then_bb
, else_bb
, join_bb
;
3939 bool then_else_reversed
= false;
3942 rtx_insn
*cond_earliest
;
3943 struct noce_if_info if_info
;
3944 bool speed_p
= optimize_bb_for_speed_p (test_bb
);
3946 /* We only ever should get here before reload. */
3947 gcc_assert (!reload_completed
);
3949 /* Recognize an IF-THEN-ELSE-JOIN block. */
3950 if (single_pred_p (then_edge
->dest
)
3951 && single_succ_p (then_edge
->dest
)
3952 && single_pred_p (else_edge
->dest
)
3953 && single_succ_p (else_edge
->dest
)
3954 && single_succ (then_edge
->dest
) == single_succ (else_edge
->dest
))
3956 then_bb
= then_edge
->dest
;
3957 else_bb
= else_edge
->dest
;
3958 join_bb
= single_succ (then_bb
);
3960 /* Recognize an IF-THEN-JOIN block. */
3961 else if (single_pred_p (then_edge
->dest
)
3962 && single_succ_p (then_edge
->dest
)
3963 && single_succ (then_edge
->dest
) == else_edge
->dest
)
3965 then_bb
= then_edge
->dest
;
3966 else_bb
= NULL_BLOCK
;
3967 join_bb
= else_edge
->dest
;
3969 /* Recognize an IF-ELSE-JOIN block. We can have those because the order
3970 of basic blocks in cfglayout mode does not matter, so the fallthrough
3971 edge can go to any basic block (and not just to bb->next_bb, like in
3973 else if (single_pred_p (else_edge
->dest
)
3974 && single_succ_p (else_edge
->dest
)
3975 && single_succ (else_edge
->dest
) == then_edge
->dest
)
3977 /* The noce transformations do not apply to IF-ELSE-JOIN blocks.
3978 To make this work, we have to invert the THEN and ELSE blocks
3979 and reverse the jump condition. */
3980 then_bb
= else_edge
->dest
;
3981 else_bb
= NULL_BLOCK
;
3982 join_bb
= single_succ (then_bb
);
3983 then_else_reversed
= true;
3986 /* Not a form we can handle. */
3989 /* The edges of the THEN and ELSE blocks cannot have complex edges. */
3990 if (single_succ_edge (then_bb
)->flags
& EDGE_COMPLEX
)
3993 && single_succ_edge (else_bb
)->flags
& EDGE_COMPLEX
)
3996 num_possible_if_blocks
++;
4001 "\nIF-THEN%s-JOIN block found, pass %d, test %d, then %d",
4002 (else_bb
) ? "-ELSE" : "",
4003 pass
, test_bb
->index
, then_bb
->index
);
4006 fprintf (dump_file
, ", else %d", else_bb
->index
);
4008 fprintf (dump_file
, ", join %d\n", join_bb
->index
);
4011 /* If the conditional jump is more than just a conditional
4012 jump, then we can not do if-conversion on this block. */
4013 jump
= BB_END (test_bb
);
4014 if (! onlyjump_p (jump
))
4017 /* If this is not a standard conditional jump, we can't parse it. */
4018 cond
= noce_get_condition (jump
, &cond_earliest
, then_else_reversed
);
4022 /* We must be comparing objects whose modes imply the size. */
4023 if (GET_MODE (XEXP (cond
, 0)) == BLKmode
)
4026 /* Initialize an IF_INFO struct to pass around. */
4027 memset (&if_info
, 0, sizeof if_info
);
4028 if_info
.test_bb
= test_bb
;
4029 if_info
.then_bb
= then_bb
;
4030 if_info
.else_bb
= else_bb
;
4031 if_info
.join_bb
= join_bb
;
4032 if_info
.cond
= cond
;
4033 rtx_insn
*rev_cond_earliest
;
4034 if_info
.rev_cond
= noce_get_condition (jump
, &rev_cond_earliest
,
4035 !then_else_reversed
);
4036 gcc_assert (if_info
.rev_cond
== NULL_RTX
4037 || rev_cond_earliest
== cond_earliest
);
4038 if_info
.cond_earliest
= cond_earliest
;
4039 if_info
.jump
= jump
;
4040 if_info
.then_else_reversed
= then_else_reversed
;
4041 if_info
.speed_p
= speed_p
;
4042 if_info
.max_seq_cost
4043 = targetm
.max_noce_ifcvt_seq_cost (then_edge
);
4044 /* We'll add in the cost of THEN_BB and ELSE_BB later, when we check
4045 that they are valid to transform. We can't easily get back to the insn
4046 for COND (and it may not exist if we had to canonicalize to get COND),
4047 and jump_insns are always given a cost of 1 by seq_cost, so treat
4048 both instructions as having cost COSTS_N_INSNS (1). */
4049 if_info
.original_cost
= COSTS_N_INSNS (2);
4052 /* Do the real work. */
4054 if (noce_process_if_block (&if_info
))
4057 if (HAVE_conditional_move
4058 && cond_move_process_if_block (&if_info
))
4065 /* Merge the blocks and mark for local life update. */
4068 merge_if_block (struct ce_if_block
* ce_info
)
4070 basic_block test_bb
= ce_info
->test_bb
; /* last test block */
4071 basic_block then_bb
= ce_info
->then_bb
; /* THEN */
4072 basic_block else_bb
= ce_info
->else_bb
; /* ELSE or NULL */
4073 basic_block join_bb
= ce_info
->join_bb
; /* join block */
4074 basic_block combo_bb
;
4076 /* All block merging is done into the lower block numbers. */
4079 df_set_bb_dirty (test_bb
);
4081 /* Merge any basic blocks to handle && and || subtests. Each of
4082 the blocks are on the fallthru path from the predecessor block. */
4083 if (ce_info
->num_multiple_test_blocks
> 0)
4085 basic_block bb
= test_bb
;
4086 basic_block last_test_bb
= ce_info
->last_test_bb
;
4087 basic_block fallthru
= block_fallthru (bb
);
4092 fallthru
= block_fallthru (bb
);
4093 merge_blocks (combo_bb
, bb
);
4096 while (bb
!= last_test_bb
);
4099 /* Merge TEST block into THEN block. Normally the THEN block won't have a
4100 label, but it might if there were || tests. That label's count should be
4101 zero, and it normally should be removed. */
4105 /* If THEN_BB has no successors, then there's a BARRIER after it.
4106 If COMBO_BB has more than one successor (THEN_BB), then that BARRIER
4107 is no longer needed, and in fact it is incorrect to leave it in
4109 if (EDGE_COUNT (then_bb
->succs
) == 0
4110 && EDGE_COUNT (combo_bb
->succs
) > 1)
4112 rtx_insn
*end
= NEXT_INSN (BB_END (then_bb
));
4113 while (end
&& NOTE_P (end
) && !NOTE_INSN_BASIC_BLOCK_P (end
))
4114 end
= NEXT_INSN (end
);
4116 if (end
&& BARRIER_P (end
))
4119 merge_blocks (combo_bb
, then_bb
);
4123 /* The ELSE block, if it existed, had a label. That label count
4124 will almost always be zero, but odd things can happen when labels
4125 get their addresses taken. */
4128 /* If ELSE_BB has no successors, then there's a BARRIER after it.
4129 If COMBO_BB has more than one successor (ELSE_BB), then that BARRIER
4130 is no longer needed, and in fact it is incorrect to leave it in
4132 if (EDGE_COUNT (else_bb
->succs
) == 0
4133 && EDGE_COUNT (combo_bb
->succs
) > 1)
4135 rtx_insn
*end
= NEXT_INSN (BB_END (else_bb
));
4136 while (end
&& NOTE_P (end
) && !NOTE_INSN_BASIC_BLOCK_P (end
))
4137 end
= NEXT_INSN (end
);
4139 if (end
&& BARRIER_P (end
))
4142 merge_blocks (combo_bb
, else_bb
);
4146 /* If there was no join block reported, that means it was not adjacent
4147 to the others, and so we cannot merge them. */
4151 rtx_insn
*last
= BB_END (combo_bb
);
4153 /* The outgoing edge for the current COMBO block should already
4154 be correct. Verify this. */
4155 if (EDGE_COUNT (combo_bb
->succs
) == 0)
4156 gcc_assert (find_reg_note (last
, REG_NORETURN
, NULL
)
4157 || (NONJUMP_INSN_P (last
)
4158 && GET_CODE (PATTERN (last
)) == TRAP_IF
4159 && (TRAP_CONDITION (PATTERN (last
))
4160 == const_true_rtx
)));
4163 /* There should still be something at the end of the THEN or ELSE
4164 blocks taking us to our final destination. */
4165 gcc_assert (JUMP_P (last
)
4166 || (EDGE_SUCC (combo_bb
, 0)->dest
4167 == EXIT_BLOCK_PTR_FOR_FN (cfun
)
4169 && SIBLING_CALL_P (last
))
4170 || ((EDGE_SUCC (combo_bb
, 0)->flags
& EDGE_EH
)
4171 && can_throw_internal (last
)));
4174 /* The JOIN block may have had quite a number of other predecessors too.
4175 Since we've already merged the TEST, THEN and ELSE blocks, we should
4176 have only one remaining edge from our if-then-else diamond. If there
4177 is more than one remaining edge, it must come from elsewhere. There
4178 may be zero incoming edges if the THEN block didn't actually join
4179 back up (as with a call to a non-return function). */
4180 else if (EDGE_COUNT (join_bb
->preds
) < 2
4181 && join_bb
!= EXIT_BLOCK_PTR_FOR_FN (cfun
))
4183 /* We can merge the JOIN cleanly and update the dataflow try
4184 again on this pass.*/
4185 merge_blocks (combo_bb
, join_bb
);
4190 /* We cannot merge the JOIN. */
4192 /* The outgoing edge for the current COMBO block should already
4193 be correct. Verify this. */
4194 gcc_assert (single_succ_p (combo_bb
)
4195 && single_succ (combo_bb
) == join_bb
);
4197 /* Remove the jump and cruft from the end of the COMBO block. */
4198 if (join_bb
!= EXIT_BLOCK_PTR_FOR_FN (cfun
))
4199 tidy_fallthru_edge (single_succ_edge (combo_bb
));
4202 num_updated_if_blocks
++;
4205 /* Find a block ending in a simple IF condition and try to transform it
4206 in some way. When converting a multi-block condition, put the new code
4207 in the first such block and delete the rest. Return a pointer to this
4208 first block if some transformation was done. Return NULL otherwise. */
4211 find_if_header (basic_block test_bb
, int pass
)
4213 ce_if_block ce_info
;
4217 /* The kind of block we're looking for has exactly two successors. */
4218 if (EDGE_COUNT (test_bb
->succs
) != 2)
4221 then_edge
= EDGE_SUCC (test_bb
, 0);
4222 else_edge
= EDGE_SUCC (test_bb
, 1);
4224 if (df_get_bb_dirty (then_edge
->dest
))
4226 if (df_get_bb_dirty (else_edge
->dest
))
4229 /* Neither edge should be abnormal. */
4230 if ((then_edge
->flags
& EDGE_COMPLEX
)
4231 || (else_edge
->flags
& EDGE_COMPLEX
))
4234 /* Nor exit the loop. */
4235 if ((then_edge
->flags
& EDGE_LOOP_EXIT
)
4236 || (else_edge
->flags
& EDGE_LOOP_EXIT
))
4239 /* The THEN edge is canonically the one that falls through. */
4240 if (then_edge
->flags
& EDGE_FALLTHRU
)
4242 else if (else_edge
->flags
& EDGE_FALLTHRU
)
4243 std::swap (then_edge
, else_edge
);
4245 /* Otherwise this must be a multiway branch of some sort. */
4248 memset (&ce_info
, 0, sizeof (ce_info
));
4249 ce_info
.test_bb
= test_bb
;
4250 ce_info
.then_bb
= then_edge
->dest
;
4251 ce_info
.else_bb
= else_edge
->dest
;
4252 ce_info
.pass
= pass
;
4254 #ifdef IFCVT_MACHDEP_INIT
4255 IFCVT_MACHDEP_INIT (&ce_info
);
4258 if (!reload_completed
4259 && noce_find_if_block (test_bb
, then_edge
, else_edge
, pass
))
4262 if (reload_completed
4263 && targetm
.have_conditional_execution ()
4264 && cond_exec_find_if_block (&ce_info
))
4267 if (targetm
.have_trap ()
4268 && optab_handler (ctrap_optab
, word_mode
) != CODE_FOR_nothing
4269 && find_cond_trap (test_bb
, then_edge
, else_edge
))
4272 if (dom_info_state (CDI_POST_DOMINATORS
) >= DOM_NO_FAST_QUERY
4273 && (reload_completed
|| !targetm
.have_conditional_execution ()))
4275 if (find_if_case_1 (test_bb
, then_edge
, else_edge
))
4277 if (find_if_case_2 (test_bb
, then_edge
, else_edge
))
4285 fprintf (dump_file
, "Conversion succeeded on pass %d.\n", pass
);
4286 /* Set this so we continue looking. */
4287 cond_exec_changed_p
= TRUE
;
4288 return ce_info
.test_bb
;
4291 /* Return true if a block has two edges, one of which falls through to the next
4292 block, and the other jumps to a specific block, so that we can tell if the
4293 block is part of an && test or an || test. Returns either -1 or the number
4294 of non-note, non-jump, non-USE/CLOBBER insns in the block. */
4297 block_jumps_and_fallthru_p (basic_block cur_bb
, basic_block target_bb
)
4300 int fallthru_p
= FALSE
;
4307 if (!cur_bb
|| !target_bb
)
4310 /* If no edges, obviously it doesn't jump or fallthru. */
4311 if (EDGE_COUNT (cur_bb
->succs
) == 0)
4314 FOR_EACH_EDGE (cur_edge
, ei
, cur_bb
->succs
)
4316 if (cur_edge
->flags
& EDGE_COMPLEX
)
4317 /* Anything complex isn't what we want. */
4320 else if (cur_edge
->flags
& EDGE_FALLTHRU
)
4323 else if (cur_edge
->dest
== target_bb
)
4330 if ((jump_p
& fallthru_p
) == 0)
4333 /* Don't allow calls in the block, since this is used to group && and ||
4334 together for conditional execution support. ??? we should support
4335 conditional execution support across calls for IA-64 some day, but
4336 for now it makes the code simpler. */
4337 end
= BB_END (cur_bb
);
4338 insn
= BB_HEAD (cur_bb
);
4340 while (insn
!= NULL_RTX
)
4347 && !DEBUG_INSN_P (insn
)
4348 && GET_CODE (PATTERN (insn
)) != USE
4349 && GET_CODE (PATTERN (insn
)) != CLOBBER
)
4355 insn
= NEXT_INSN (insn
);
4361 /* Determine if a given basic block heads a simple IF-THEN or IF-THEN-ELSE
4362 block. If so, we'll try to convert the insns to not require the branch.
4363 Return TRUE if we were successful at converting the block. */
4366 cond_exec_find_if_block (struct ce_if_block
* ce_info
)
4368 basic_block test_bb
= ce_info
->test_bb
;
4369 basic_block then_bb
= ce_info
->then_bb
;
4370 basic_block else_bb
= ce_info
->else_bb
;
4371 basic_block join_bb
= NULL_BLOCK
;
4376 ce_info
->last_test_bb
= test_bb
;
4378 /* We only ever should get here after reload,
4379 and if we have conditional execution. */
4380 gcc_assert (reload_completed
&& targetm
.have_conditional_execution ());
4382 /* Discover if any fall through predecessors of the current test basic block
4383 were && tests (which jump to the else block) or || tests (which jump to
4385 if (single_pred_p (test_bb
)
4386 && single_pred_edge (test_bb
)->flags
== EDGE_FALLTHRU
)
4388 basic_block bb
= single_pred (test_bb
);
4389 basic_block target_bb
;
4390 int max_insns
= MAX_CONDITIONAL_EXECUTE
;
4393 /* Determine if the preceding block is an && or || block. */
4394 if ((n_insns
= block_jumps_and_fallthru_p (bb
, else_bb
)) >= 0)
4396 ce_info
->and_and_p
= TRUE
;
4397 target_bb
= else_bb
;
4399 else if ((n_insns
= block_jumps_and_fallthru_p (bb
, then_bb
)) >= 0)
4401 ce_info
->and_and_p
= FALSE
;
4402 target_bb
= then_bb
;
4405 target_bb
= NULL_BLOCK
;
4407 if (target_bb
&& n_insns
<= max_insns
)
4409 int total_insns
= 0;
4412 ce_info
->last_test_bb
= test_bb
;
4414 /* Found at least one && or || block, look for more. */
4417 ce_info
->test_bb
= test_bb
= bb
;
4418 total_insns
+= n_insns
;
4421 if (!single_pred_p (bb
))
4424 bb
= single_pred (bb
);
4425 n_insns
= block_jumps_and_fallthru_p (bb
, target_bb
);
4427 while (n_insns
>= 0 && (total_insns
+ n_insns
) <= max_insns
);
4429 ce_info
->num_multiple_test_blocks
= blocks
;
4430 ce_info
->num_multiple_test_insns
= total_insns
;
4432 if (ce_info
->and_and_p
)
4433 ce_info
->num_and_and_blocks
= blocks
;
4435 ce_info
->num_or_or_blocks
= blocks
;
4439 /* The THEN block of an IF-THEN combo must have exactly one predecessor,
4440 other than any || blocks which jump to the THEN block. */
4441 if ((EDGE_COUNT (then_bb
->preds
) - ce_info
->num_or_or_blocks
) != 1)
4444 /* The edges of the THEN and ELSE blocks cannot have complex edges. */
4445 FOR_EACH_EDGE (cur_edge
, ei
, then_bb
->preds
)
4447 if (cur_edge
->flags
& EDGE_COMPLEX
)
4451 FOR_EACH_EDGE (cur_edge
, ei
, else_bb
->preds
)
4453 if (cur_edge
->flags
& EDGE_COMPLEX
)
4457 /* The THEN block of an IF-THEN combo must have zero or one successors. */
4458 if (EDGE_COUNT (then_bb
->succs
) > 0
4459 && (!single_succ_p (then_bb
)
4460 || (single_succ_edge (then_bb
)->flags
& EDGE_COMPLEX
)
4461 || (epilogue_completed
4462 && tablejump_p (BB_END (then_bb
), NULL
, NULL
))))
4465 /* If the THEN block has no successors, conditional execution can still
4466 make a conditional call. Don't do this unless the ELSE block has
4467 only one incoming edge -- the CFG manipulation is too ugly otherwise.
4468 Check for the last insn of the THEN block being an indirect jump, which
4469 is listed as not having any successors, but confuses the rest of the CE
4470 code processing. ??? we should fix this in the future. */
4471 if (EDGE_COUNT (then_bb
->succs
) == 0)
4473 if (single_pred_p (else_bb
) && else_bb
!= EXIT_BLOCK_PTR_FOR_FN (cfun
))
4475 rtx_insn
*last_insn
= BB_END (then_bb
);
4478 && NOTE_P (last_insn
)
4479 && last_insn
!= BB_HEAD (then_bb
))
4480 last_insn
= PREV_INSN (last_insn
);
4483 && JUMP_P (last_insn
)
4484 && ! simplejump_p (last_insn
))
4488 else_bb
= NULL_BLOCK
;
4494 /* If the THEN block's successor is the other edge out of the TEST block,
4495 then we have an IF-THEN combo without an ELSE. */
4496 else if (single_succ (then_bb
) == else_bb
)
4499 else_bb
= NULL_BLOCK
;
4502 /* If the THEN and ELSE block meet in a subsequent block, and the ELSE
4503 has exactly one predecessor and one successor, and the outgoing edge
4504 is not complex, then we have an IF-THEN-ELSE combo. */
4505 else if (single_succ_p (else_bb
)
4506 && single_succ (then_bb
) == single_succ (else_bb
)
4507 && single_pred_p (else_bb
)
4508 && !(single_succ_edge (else_bb
)->flags
& EDGE_COMPLEX
)
4509 && !(epilogue_completed
4510 && tablejump_p (BB_END (else_bb
), NULL
, NULL
)))
4511 join_bb
= single_succ (else_bb
);
4513 /* Otherwise it is not an IF-THEN or IF-THEN-ELSE combination. */
4517 num_possible_if_blocks
++;
4522 "\nIF-THEN%s block found, pass %d, start block %d "
4523 "[insn %d], then %d [%d]",
4524 (else_bb
) ? "-ELSE" : "",
4527 BB_HEAD (test_bb
) ? (int)INSN_UID (BB_HEAD (test_bb
)) : -1,
4529 BB_HEAD (then_bb
) ? (int)INSN_UID (BB_HEAD (then_bb
)) : -1);
4532 fprintf (dump_file
, ", else %d [%d]",
4534 BB_HEAD (else_bb
) ? (int)INSN_UID (BB_HEAD (else_bb
)) : -1);
4536 fprintf (dump_file
, ", join %d [%d]",
4538 BB_HEAD (join_bb
) ? (int)INSN_UID (BB_HEAD (join_bb
)) : -1);
4540 if (ce_info
->num_multiple_test_blocks
> 0)
4541 fprintf (dump_file
, ", %d %s block%s last test %d [%d]",
4542 ce_info
->num_multiple_test_blocks
,
4543 (ce_info
->and_and_p
) ? "&&" : "||",
4544 (ce_info
->num_multiple_test_blocks
== 1) ? "" : "s",
4545 ce_info
->last_test_bb
->index
,
4546 ((BB_HEAD (ce_info
->last_test_bb
))
4547 ? (int)INSN_UID (BB_HEAD (ce_info
->last_test_bb
))
4550 fputc ('\n', dump_file
);
4553 /* Make sure IF, THEN, and ELSE, blocks are adjacent. Actually, we get the
4554 first condition for free, since we've already asserted that there's a
4555 fallthru edge from IF to THEN. Likewise for the && and || blocks, since
4556 we checked the FALLTHRU flag, those are already adjacent to the last IF
4558 /* ??? As an enhancement, move the ELSE block. Have to deal with
4559 BLOCK notes, if by no other means than backing out the merge if they
4560 exist. Sticky enough I don't want to think about it now. */
4562 if (else_bb
&& (next
= next
->next_bb
) != else_bb
)
4564 if ((next
= next
->next_bb
) != join_bb
4565 && join_bb
!= EXIT_BLOCK_PTR_FOR_FN (cfun
))
4573 /* Do the real work. */
4575 ce_info
->else_bb
= else_bb
;
4576 ce_info
->join_bb
= join_bb
;
4578 /* If we have && and || tests, try to first handle combining the && and ||
4579 tests into the conditional code, and if that fails, go back and handle
4580 it without the && and ||, which at present handles the && case if there
4581 was no ELSE block. */
4582 if (cond_exec_process_if_block (ce_info
, TRUE
))
4585 if (ce_info
->num_multiple_test_blocks
)
4589 if (cond_exec_process_if_block (ce_info
, FALSE
))
4596 /* Convert a branch over a trap, or a branch
4597 to a trap, into a conditional trap. */
4600 find_cond_trap (basic_block test_bb
, edge then_edge
, edge else_edge
)
4602 basic_block then_bb
= then_edge
->dest
;
4603 basic_block else_bb
= else_edge
->dest
;
4604 basic_block other_bb
, trap_bb
;
4605 rtx_insn
*trap
, *jump
;
4607 rtx_insn
*cond_earliest
;
4609 /* Locate the block with the trap instruction. */
4610 /* ??? While we look for no successors, we really ought to allow
4611 EH successors. Need to fix merge_if_block for that to work. */
4612 if ((trap
= block_has_only_trap (then_bb
)) != NULL
)
4613 trap_bb
= then_bb
, other_bb
= else_bb
;
4614 else if ((trap
= block_has_only_trap (else_bb
)) != NULL
)
4615 trap_bb
= else_bb
, other_bb
= then_bb
;
4621 fprintf (dump_file
, "\nTRAP-IF block found, start %d, trap %d\n",
4622 test_bb
->index
, trap_bb
->index
);
4625 /* If this is not a standard conditional jump, we can't parse it. */
4626 jump
= BB_END (test_bb
);
4627 cond
= noce_get_condition (jump
, &cond_earliest
, then_bb
== trap_bb
);
4631 /* If the conditional jump is more than just a conditional jump, then
4632 we can not do if-conversion on this block. Give up for returnjump_p,
4633 changing a conditional return followed by unconditional trap for
4634 conditional trap followed by unconditional return is likely not
4635 beneficial and harder to handle. */
4636 if (! onlyjump_p (jump
) || returnjump_p (jump
))
4639 /* We must be comparing objects whose modes imply the size. */
4640 if (GET_MODE (XEXP (cond
, 0)) == BLKmode
)
4643 /* Attempt to generate the conditional trap. */
4644 rtx_insn
*seq
= gen_cond_trap (GET_CODE (cond
), copy_rtx (XEXP (cond
, 0)),
4645 copy_rtx (XEXP (cond
, 1)),
4646 TRAP_CODE (PATTERN (trap
)));
4650 /* If that results in an invalid insn, back out. */
4651 for (rtx_insn
*x
= seq
; x
; x
= NEXT_INSN (x
))
4652 if (recog_memoized (x
) < 0)
4655 /* Emit the new insns before cond_earliest. */
4656 emit_insn_before_setloc (seq
, cond_earliest
, INSN_LOCATION (trap
));
4658 /* Delete the trap block if possible. */
4659 remove_edge (trap_bb
== then_bb
? then_edge
: else_edge
);
4660 df_set_bb_dirty (test_bb
);
4661 df_set_bb_dirty (then_bb
);
4662 df_set_bb_dirty (else_bb
);
4664 if (EDGE_COUNT (trap_bb
->preds
) == 0)
4666 delete_basic_block (trap_bb
);
4670 /* Wire together the blocks again. */
4671 if (current_ir_type () == IR_RTL_CFGLAYOUT
)
4672 single_succ_edge (test_bb
)->flags
|= EDGE_FALLTHRU
;
4673 else if (trap_bb
== then_bb
)
4675 rtx lab
= JUMP_LABEL (jump
);
4676 rtx_insn
*seq
= targetm
.gen_jump (lab
);
4677 rtx_jump_insn
*newjump
= emit_jump_insn_after (seq
, jump
);
4678 LABEL_NUSES (lab
) += 1;
4679 JUMP_LABEL (newjump
) = lab
;
4680 emit_barrier_after (newjump
);
4684 if (can_merge_blocks_p (test_bb
, other_bb
))
4686 merge_blocks (test_bb
, other_bb
);
4690 num_updated_if_blocks
++;
4694 /* Subroutine of find_cond_trap: if BB contains only a trap insn,
4698 block_has_only_trap (basic_block bb
)
4702 /* We're not the exit block. */
4703 if (bb
== EXIT_BLOCK_PTR_FOR_FN (cfun
))
4706 /* The block must have no successors. */
4707 if (EDGE_COUNT (bb
->succs
) > 0)
4710 /* The only instruction in the THEN block must be the trap. */
4711 trap
= first_active_insn (bb
);
4712 if (! (trap
== BB_END (bb
)
4713 && GET_CODE (PATTERN (trap
)) == TRAP_IF
4714 && TRAP_CONDITION (PATTERN (trap
)) == const_true_rtx
))
4720 /* Look for IF-THEN-ELSE cases in which one of THEN or ELSE is
4721 transformable, but not necessarily the other. There need be no
4724 Return TRUE if we were successful at converting the block.
4726 Cases we'd like to look at:
4729 if (test) goto over; // x not live
4737 if (! test) goto label;
4740 if (test) goto E; // x not live
4754 (3) // This one's really only interesting for targets that can do
4755 // multiway branching, e.g. IA-64 BBB bundles. For other targets
4756 // it results in multiple branches on a cache line, which often
4757 // does not sit well with predictors.
4759 if (test1) goto E; // predicted not taken
4775 (A) Don't do (2) if the branch is predicted against the block we're
4776 eliminating. Do it anyway if we can eliminate a branch; this requires
4777 that the sole successor of the eliminated block postdominate the other
4780 (B) With CE, on (3) we can steal from both sides of the if, creating
4789 Again, this is most useful if J postdominates.
4791 (C) CE substitutes for helpful life information.
4793 (D) These heuristics need a lot of work. */
4795 /* Tests for case 1 above. */
4798 find_if_case_1 (basic_block test_bb
, edge then_edge
, edge else_edge
)
4800 basic_block then_bb
= then_edge
->dest
;
4801 basic_block else_bb
= else_edge
->dest
;
4803 int then_bb_index
, then_prob
;
4804 rtx else_target
= NULL_RTX
;
4806 /* If we are partitioning hot/cold basic blocks, we don't want to
4807 mess up unconditional or indirect jumps that cross between hot
4810 Basic block partitioning may result in some jumps that appear to
4811 be optimizable (or blocks that appear to be mergeable), but which really
4812 must be left untouched (they are required to make it safely across
4813 partition boundaries). See the comments at the top of
4814 bb-reorder.c:partition_hot_cold_basic_blocks for complete details. */
4816 if ((BB_END (then_bb
)
4817 && JUMP_P (BB_END (then_bb
))
4818 && CROSSING_JUMP_P (BB_END (then_bb
)))
4819 || (BB_END (test_bb
)
4820 && JUMP_P (BB_END (test_bb
))
4821 && CROSSING_JUMP_P (BB_END (test_bb
)))
4822 || (BB_END (else_bb
)
4823 && JUMP_P (BB_END (else_bb
))
4824 && CROSSING_JUMP_P (BB_END (else_bb
))))
4827 /* THEN has one successor. */
4828 if (!single_succ_p (then_bb
))
4831 /* THEN does not fall through, but is not strange either. */
4832 if (single_succ_edge (then_bb
)->flags
& (EDGE_COMPLEX
| EDGE_FALLTHRU
))
4835 /* THEN has one predecessor. */
4836 if (!single_pred_p (then_bb
))
4839 /* THEN must do something. */
4840 if (forwarder_block_p (then_bb
))
4843 num_possible_if_blocks
++;
4846 "\nIF-CASE-1 found, start %d, then %d\n",
4847 test_bb
->index
, then_bb
->index
);
4849 if (then_edge
->probability
)
4850 then_prob
= REG_BR_PROB_BASE
- then_edge
->probability
;
4852 then_prob
= REG_BR_PROB_BASE
/ 2;
4854 /* We're speculating from the THEN path, we want to make sure the cost
4855 of speculation is within reason. */
4856 if (! cheap_bb_rtx_cost_p (then_bb
, then_prob
,
4857 COSTS_N_INSNS (BRANCH_COST (optimize_bb_for_speed_p (then_edge
->src
),
4858 predictable_edge_p (then_edge
)))))
4861 if (else_bb
== EXIT_BLOCK_PTR_FOR_FN (cfun
))
4863 rtx_insn
*jump
= BB_END (else_edge
->src
);
4864 gcc_assert (JUMP_P (jump
));
4865 else_target
= JUMP_LABEL (jump
);
4868 /* Registers set are dead, or are predicable. */
4869 if (! dead_or_predicable (test_bb
, then_bb
, else_bb
,
4870 single_succ_edge (then_bb
), 1))
4873 /* Conversion went ok, including moving the insns and fixing up the
4874 jump. Adjust the CFG to match. */
4876 /* We can avoid creating a new basic block if then_bb is immediately
4877 followed by else_bb, i.e. deleting then_bb allows test_bb to fall
4878 through to else_bb. */
4880 if (then_bb
->next_bb
== else_bb
4881 && then_bb
->prev_bb
== test_bb
4882 && else_bb
!= EXIT_BLOCK_PTR_FOR_FN (cfun
))
4884 redirect_edge_succ (FALLTHRU_EDGE (test_bb
), else_bb
);
4887 else if (else_bb
== EXIT_BLOCK_PTR_FOR_FN (cfun
))
4888 new_bb
= force_nonfallthru_and_redirect (FALLTHRU_EDGE (test_bb
),
4889 else_bb
, else_target
);
4891 new_bb
= redirect_edge_and_branch_force (FALLTHRU_EDGE (test_bb
),
4894 df_set_bb_dirty (test_bb
);
4895 df_set_bb_dirty (else_bb
);
4897 then_bb_index
= then_bb
->index
;
4898 delete_basic_block (then_bb
);
4900 /* Make rest of code believe that the newly created block is the THEN_BB
4901 block we removed. */
4904 df_bb_replace (then_bb_index
, new_bb
);
4905 /* This should have been done above via force_nonfallthru_and_redirect
4906 (possibly called from redirect_edge_and_branch_force). */
4907 gcc_checking_assert (BB_PARTITION (new_bb
) == BB_PARTITION (test_bb
));
4911 num_updated_if_blocks
++;
4915 /* Test for case 2 above. */
4918 find_if_case_2 (basic_block test_bb
, edge then_edge
, edge else_edge
)
4920 basic_block then_bb
= then_edge
->dest
;
4921 basic_block else_bb
= else_edge
->dest
;
4923 int then_prob
, else_prob
;
4925 /* We do not want to speculate (empty) loop latches. */
4927 && else_bb
->loop_father
->latch
== else_bb
)
4930 /* If we are partitioning hot/cold basic blocks, we don't want to
4931 mess up unconditional or indirect jumps that cross between hot
4934 Basic block partitioning may result in some jumps that appear to
4935 be optimizable (or blocks that appear to be mergeable), but which really
4936 must be left untouched (they are required to make it safely across
4937 partition boundaries). See the comments at the top of
4938 bb-reorder.c:partition_hot_cold_basic_blocks for complete details. */
4940 if ((BB_END (then_bb
)
4941 && JUMP_P (BB_END (then_bb
))
4942 && CROSSING_JUMP_P (BB_END (then_bb
)))
4943 || (BB_END (test_bb
)
4944 && JUMP_P (BB_END (test_bb
))
4945 && CROSSING_JUMP_P (BB_END (test_bb
)))
4946 || (BB_END (else_bb
)
4947 && JUMP_P (BB_END (else_bb
))
4948 && CROSSING_JUMP_P (BB_END (else_bb
))))
4951 /* ELSE has one successor. */
4952 if (!single_succ_p (else_bb
))
4955 else_succ
= single_succ_edge (else_bb
);
4957 /* ELSE outgoing edge is not complex. */
4958 if (else_succ
->flags
& EDGE_COMPLEX
)
4961 /* ELSE has one predecessor. */
4962 if (!single_pred_p (else_bb
))
4965 /* THEN is not EXIT. */
4966 if (then_bb
->index
< NUM_FIXED_BLOCKS
)
4969 if (else_edge
->probability
)
4971 else_prob
= else_edge
->probability
;
4972 then_prob
= REG_BR_PROB_BASE
- else_prob
;
4976 else_prob
= REG_BR_PROB_BASE
/ 2;
4977 then_prob
= REG_BR_PROB_BASE
/ 2;
4980 /* ELSE is predicted or SUCC(ELSE) postdominates THEN. */
4981 if (else_prob
> then_prob
)
4983 else if (else_succ
->dest
->index
< NUM_FIXED_BLOCKS
4984 || dominated_by_p (CDI_POST_DOMINATORS
, then_bb
,
4990 num_possible_if_blocks
++;
4993 "\nIF-CASE-2 found, start %d, else %d\n",
4994 test_bb
->index
, else_bb
->index
);
4996 /* We're speculating from the ELSE path, we want to make sure the cost
4997 of speculation is within reason. */
4998 if (! cheap_bb_rtx_cost_p (else_bb
, else_prob
,
4999 COSTS_N_INSNS (BRANCH_COST (optimize_bb_for_speed_p (else_edge
->src
),
5000 predictable_edge_p (else_edge
)))))
5003 /* Registers set are dead, or are predicable. */
5004 if (! dead_or_predicable (test_bb
, else_bb
, then_bb
, else_succ
, 0))
5007 /* Conversion went ok, including moving the insns and fixing up the
5008 jump. Adjust the CFG to match. */
5010 df_set_bb_dirty (test_bb
);
5011 df_set_bb_dirty (then_bb
);
5012 delete_basic_block (else_bb
);
5015 num_updated_if_blocks
++;
5017 /* ??? We may now fallthru from one of THEN's successors into a join
5018 block. Rerun cleanup_cfg? Examine things manually? Wait? */
5023 /* Used by the code above to perform the actual rtl transformations.
5024 Return TRUE if successful.
5026 TEST_BB is the block containing the conditional branch. MERGE_BB
5027 is the block containing the code to manipulate. DEST_EDGE is an
5028 edge representing a jump to the join block; after the conversion,
5029 TEST_BB should be branching to its destination.
5030 REVERSEP is true if the sense of the branch should be reversed. */
5033 dead_or_predicable (basic_block test_bb
, basic_block merge_bb
,
5034 basic_block other_bb
, edge dest_edge
, int reversep
)
5036 basic_block new_dest
= dest_edge
->dest
;
5037 rtx_insn
*head
, *end
, *jump
;
5038 rtx_insn
*earliest
= NULL
;
5040 bitmap merge_set
= NULL
;
5041 /* Number of pending changes. */
5042 int n_validated_changes
= 0;
5043 rtx new_dest_label
= NULL_RTX
;
5045 jump
= BB_END (test_bb
);
5047 /* Find the extent of the real code in the merge block. */
5048 head
= BB_HEAD (merge_bb
);
5049 end
= BB_END (merge_bb
);
5051 while (DEBUG_INSN_P (end
) && end
!= head
)
5052 end
= PREV_INSN (end
);
5054 /* If merge_bb ends with a tablejump, predicating/moving insn's
5055 into test_bb and then deleting merge_bb will result in the jumptable
5056 that follows merge_bb being removed along with merge_bb and then we
5057 get an unresolved reference to the jumptable. */
5058 if (tablejump_p (end
, NULL
, NULL
))
5062 head
= NEXT_INSN (head
);
5063 while (DEBUG_INSN_P (head
) && head
!= end
)
5064 head
= NEXT_INSN (head
);
5072 head
= NEXT_INSN (head
);
5073 while (DEBUG_INSN_P (head
) && head
!= end
)
5074 head
= NEXT_INSN (head
);
5079 if (!onlyjump_p (end
))
5086 end
= PREV_INSN (end
);
5087 while (DEBUG_INSN_P (end
) && end
!= head
)
5088 end
= PREV_INSN (end
);
5091 /* Don't move frame-related insn across the conditional branch. This
5092 can lead to one of the paths of the branch having wrong unwind info. */
5093 if (epilogue_completed
)
5095 rtx_insn
*insn
= head
;
5098 if (INSN_P (insn
) && RTX_FRAME_RELATED_P (insn
))
5102 insn
= NEXT_INSN (insn
);
5106 /* Disable handling dead code by conditional execution if the machine needs
5107 to do anything funny with the tests, etc. */
5108 #ifndef IFCVT_MODIFY_TESTS
5109 if (targetm
.have_conditional_execution ())
5111 /* In the conditional execution case, we have things easy. We know
5112 the condition is reversible. We don't have to check life info
5113 because we're going to conditionally execute the code anyway.
5114 All that's left is making sure the insns involved can actually
5119 cond
= cond_exec_get_condition (jump
);
5123 rtx note
= find_reg_note (jump
, REG_BR_PROB
, NULL_RTX
);
5124 int prob_val
= (note
? XINT (note
, 0) : -1);
5128 enum rtx_code rev
= reversed_comparison_code (cond
, jump
);
5131 cond
= gen_rtx_fmt_ee (rev
, GET_MODE (cond
), XEXP (cond
, 0),
5134 prob_val
= REG_BR_PROB_BASE
- prob_val
;
5137 if (cond_exec_process_insns (NULL
, head
, end
, cond
, prob_val
, 0)
5138 && verify_changes (0))
5139 n_validated_changes
= num_validated_changes ();
5147 /* If we allocated new pseudos (e.g. in the conditional move
5148 expander called from noce_emit_cmove), we must resize the
5150 if (max_regno
< max_reg_num ())
5151 max_regno
= max_reg_num ();
5153 /* Try the NCE path if the CE path did not result in any changes. */
5154 if (n_validated_changes
== 0)
5161 /* In the non-conditional execution case, we have to verify that there
5162 are no trapping operations, no calls, no references to memory, and
5163 that any registers modified are dead at the branch site. */
5165 if (!any_condjump_p (jump
))
5168 /* Find the extent of the conditional. */
5169 cond
= noce_get_condition (jump
, &earliest
, false);
5173 live
= BITMAP_ALLOC (®_obstack
);
5174 simulate_backwards_to_point (merge_bb
, live
, end
);
5175 success
= can_move_insns_across (head
, end
, earliest
, jump
,
5177 df_get_live_in (other_bb
), NULL
);
5182 /* Collect the set of registers set in MERGE_BB. */
5183 merge_set
= BITMAP_ALLOC (®_obstack
);
5185 FOR_BB_INSNS (merge_bb
, insn
)
5186 if (NONDEBUG_INSN_P (insn
))
5187 df_simulate_find_defs (insn
, merge_set
);
5189 /* If shrink-wrapping, disable this optimization when test_bb is
5190 the first basic block and merge_bb exits. The idea is to not
5191 move code setting up a return register as that may clobber a
5192 register used to pass function parameters, which then must be
5193 saved in caller-saved regs. A caller-saved reg requires the
5194 prologue, killing a shrink-wrap opportunity. */
5195 if ((SHRINK_WRAPPING_ENABLED
&& !epilogue_completed
)
5196 && ENTRY_BLOCK_PTR_FOR_FN (cfun
)->next_bb
== test_bb
5197 && single_succ_p (new_dest
)
5198 && single_succ (new_dest
) == EXIT_BLOCK_PTR_FOR_FN (cfun
)
5199 && bitmap_intersect_p (df_get_live_in (new_dest
), merge_set
))
5204 return_regs
= BITMAP_ALLOC (®_obstack
);
5206 /* Start off with the intersection of regs used to pass
5207 params and regs used to return values. */
5208 for (i
= 0; i
< FIRST_PSEUDO_REGISTER
; i
++)
5209 if (FUNCTION_ARG_REGNO_P (i
)
5210 && targetm
.calls
.function_value_regno_p (i
))
5211 bitmap_set_bit (return_regs
, INCOMING_REGNO (i
));
5213 bitmap_and_into (return_regs
,
5214 df_get_live_out (ENTRY_BLOCK_PTR_FOR_FN (cfun
)));
5215 bitmap_and_into (return_regs
,
5216 df_get_live_in (EXIT_BLOCK_PTR_FOR_FN (cfun
)));
5217 if (!bitmap_empty_p (return_regs
))
5219 FOR_BB_INSNS_REVERSE (new_dest
, insn
)
5220 if (NONDEBUG_INSN_P (insn
))
5224 /* If this insn sets any reg in return_regs, add all
5225 reg uses to the set of regs we're interested in. */
5226 FOR_EACH_INSN_DEF (def
, insn
)
5227 if (bitmap_bit_p (return_regs
, DF_REF_REGNO (def
)))
5229 df_simulate_uses (insn
, return_regs
);
5233 if (bitmap_intersect_p (merge_set
, return_regs
))
5235 BITMAP_FREE (return_regs
);
5236 BITMAP_FREE (merge_set
);
5240 BITMAP_FREE (return_regs
);
5245 /* We don't want to use normal invert_jump or redirect_jump because
5246 we don't want to delete_insn called. Also, we want to do our own
5247 change group management. */
5249 old_dest
= JUMP_LABEL (jump
);
5250 if (other_bb
!= new_dest
)
5252 if (!any_condjump_p (jump
))
5255 if (JUMP_P (BB_END (dest_edge
->src
)))
5256 new_dest_label
= JUMP_LABEL (BB_END (dest_edge
->src
));
5257 else if (new_dest
== EXIT_BLOCK_PTR_FOR_FN (cfun
))
5258 new_dest_label
= ret_rtx
;
5260 new_dest_label
= block_label (new_dest
);
5262 rtx_jump_insn
*jump_insn
= as_a
<rtx_jump_insn
*> (jump
);
5264 ? ! invert_jump_1 (jump_insn
, new_dest_label
)
5265 : ! redirect_jump_1 (jump_insn
, new_dest_label
))
5269 if (verify_changes (n_validated_changes
))
5270 confirm_change_group ();
5274 if (other_bb
!= new_dest
)
5276 redirect_jump_2 (as_a
<rtx_jump_insn
*> (jump
), old_dest
, new_dest_label
,
5279 redirect_edge_succ (BRANCH_EDGE (test_bb
), new_dest
);
5282 std::swap (BRANCH_EDGE (test_bb
)->count
,
5283 FALLTHRU_EDGE (test_bb
)->count
);
5284 std::swap (BRANCH_EDGE (test_bb
)->probability
,
5285 FALLTHRU_EDGE (test_bb
)->probability
);
5286 update_br_prob_note (test_bb
);
5290 /* Move the insns out of MERGE_BB to before the branch. */
5295 if (end
== BB_END (merge_bb
))
5296 BB_END (merge_bb
) = PREV_INSN (head
);
5298 /* PR 21767: when moving insns above a conditional branch, the REG_EQUAL
5299 notes being moved might become invalid. */
5305 if (! INSN_P (insn
))
5307 note
= find_reg_note (insn
, REG_EQUAL
, NULL_RTX
);
5310 remove_note (insn
, note
);
5311 } while (insn
!= end
&& (insn
= NEXT_INSN (insn
)));
5313 /* PR46315: when moving insns above a conditional branch, the REG_EQUAL
5314 notes referring to the registers being set might become invalid. */
5320 EXECUTE_IF_SET_IN_BITMAP (merge_set
, 0, i
, bi
)
5321 remove_reg_equal_equiv_notes_for_regno (i
);
5323 BITMAP_FREE (merge_set
);
5326 reorder_insns (head
, end
, PREV_INSN (earliest
));
5329 /* Remove the jump and edge if we can. */
5330 if (other_bb
== new_dest
)
5333 remove_edge (BRANCH_EDGE (test_bb
));
5334 /* ??? Can't merge blocks here, as then_bb is still in use.
5335 At minimum, the merge will get done just before bb-reorder. */
5344 BITMAP_FREE (merge_set
);
5349 /* Main entry point for all if-conversion. AFTER_COMBINE is true if
5350 we are after combine pass. */
5353 if_convert (bool after_combine
)
5360 df_live_add_problem ();
5361 df_live_set_all_dirty ();
5364 /* Record whether we are after combine pass. */
5365 ifcvt_after_combine
= after_combine
;
5366 have_cbranchcc4
= (direct_optab_handler (cbranch_optab
, CCmode
)
5367 != CODE_FOR_nothing
);
5368 num_possible_if_blocks
= 0;
5369 num_updated_if_blocks
= 0;
5370 num_true_changes
= 0;
5372 loop_optimizer_init (AVOID_CFG_MODIFICATIONS
);
5373 mark_loop_exit_edges ();
5374 loop_optimizer_finalize ();
5375 free_dominance_info (CDI_DOMINATORS
);
5377 /* Compute postdominators. */
5378 calculate_dominance_info (CDI_POST_DOMINATORS
);
5380 df_set_flags (DF_LR_RUN_DCE
);
5382 /* Go through each of the basic blocks looking for things to convert. If we
5383 have conditional execution, we make multiple passes to allow us to handle
5384 IF-THEN{-ELSE} blocks within other IF-THEN{-ELSE} blocks. */
5389 /* Only need to do dce on the first pass. */
5390 df_clear_flags (DF_LR_RUN_DCE
);
5391 cond_exec_changed_p
= FALSE
;
5394 #ifdef IFCVT_MULTIPLE_DUMPS
5395 if (dump_file
&& pass
> 1)
5396 fprintf (dump_file
, "\n\n========== Pass %d ==========\n", pass
);
5399 FOR_EACH_BB_FN (bb
, cfun
)
5402 while (!df_get_bb_dirty (bb
)
5403 && (new_bb
= find_if_header (bb
, pass
)) != NULL
)
5407 #ifdef IFCVT_MULTIPLE_DUMPS
5408 if (dump_file
&& cond_exec_changed_p
)
5409 print_rtl_with_bb (dump_file
, get_insns (), dump_flags
);
5412 while (cond_exec_changed_p
);
5414 #ifdef IFCVT_MULTIPLE_DUMPS
5416 fprintf (dump_file
, "\n\n========== no more changes\n");
5419 free_dominance_info (CDI_POST_DOMINATORS
);
5424 clear_aux_for_blocks ();
5426 /* If we allocated new pseudos, we must resize the array for sched1. */
5427 if (max_regno
< max_reg_num ())
5428 max_regno
= max_reg_num ();
5430 /* Write the final stats. */
5431 if (dump_file
&& num_possible_if_blocks
> 0)
5434 "\n%d possible IF blocks searched.\n",
5435 num_possible_if_blocks
);
5437 "%d IF blocks converted.\n",
5438 num_updated_if_blocks
);
5440 "%d true changes made.\n\n\n",
5445 df_remove_problem (df_live
);
5447 checking_verify_flow_info ();
5450 /* If-conversion and CFG cleanup. */
5452 rest_of_handle_if_conversion (void)
5454 if (flag_if_conversion
)
5458 dump_reg_info (dump_file
);
5459 dump_flow_info (dump_file
, dump_flags
);
5461 cleanup_cfg (CLEANUP_EXPENSIVE
);
5471 const pass_data pass_data_rtl_ifcvt
=
5473 RTL_PASS
, /* type */
5475 OPTGROUP_NONE
, /* optinfo_flags */
5476 TV_IFCVT
, /* tv_id */
5477 0, /* properties_required */
5478 0, /* properties_provided */
5479 0, /* properties_destroyed */
5480 0, /* todo_flags_start */
5481 TODO_df_finish
, /* todo_flags_finish */
5484 class pass_rtl_ifcvt
: public rtl_opt_pass
5487 pass_rtl_ifcvt (gcc::context
*ctxt
)
5488 : rtl_opt_pass (pass_data_rtl_ifcvt
, ctxt
)
5491 /* opt_pass methods: */
5492 virtual bool gate (function
*)
5494 return (optimize
> 0) && dbg_cnt (if_conversion
);
5497 virtual unsigned int execute (function
*)
5499 return rest_of_handle_if_conversion ();
5502 }; // class pass_rtl_ifcvt
5507 make_pass_rtl_ifcvt (gcc::context
*ctxt
)
5509 return new pass_rtl_ifcvt (ctxt
);
5513 /* Rerun if-conversion, as combine may have simplified things enough
5514 to now meet sequence length restrictions. */
5518 const pass_data pass_data_if_after_combine
=
5520 RTL_PASS
, /* type */
5522 OPTGROUP_NONE
, /* optinfo_flags */
5523 TV_IFCVT
, /* tv_id */
5524 0, /* properties_required */
5525 0, /* properties_provided */
5526 0, /* properties_destroyed */
5527 0, /* todo_flags_start */
5528 TODO_df_finish
, /* todo_flags_finish */
5531 class pass_if_after_combine
: public rtl_opt_pass
5534 pass_if_after_combine (gcc::context
*ctxt
)
5535 : rtl_opt_pass (pass_data_if_after_combine
, ctxt
)
5538 /* opt_pass methods: */
5539 virtual bool gate (function
*)
5541 return optimize
> 0 && flag_if_conversion
5542 && dbg_cnt (if_after_combine
);
5545 virtual unsigned int execute (function
*)
5551 }; // class pass_if_after_combine
5556 make_pass_if_after_combine (gcc::context
*ctxt
)
5558 return new pass_if_after_combine (ctxt
);
5564 const pass_data pass_data_if_after_reload
=
5566 RTL_PASS
, /* type */
5568 OPTGROUP_NONE
, /* optinfo_flags */
5569 TV_IFCVT2
, /* tv_id */
5570 0, /* properties_required */
5571 0, /* properties_provided */
5572 0, /* properties_destroyed */
5573 0, /* todo_flags_start */
5574 TODO_df_finish
, /* todo_flags_finish */
5577 class pass_if_after_reload
: public rtl_opt_pass
5580 pass_if_after_reload (gcc::context
*ctxt
)
5581 : rtl_opt_pass (pass_data_if_after_reload
, ctxt
)
5584 /* opt_pass methods: */
5585 virtual bool gate (function
*)
5587 return optimize
> 0 && flag_if_conversion2
5588 && dbg_cnt (if_after_reload
);
5591 virtual unsigned int execute (function
*)
5597 }; // class pass_if_after_reload
5602 make_pass_if_after_reload (gcc::context
*ctxt
)
5604 return new pass_if_after_reload (ctxt
);