1 /* If-conversion support.
2 Copyright (C) 2000-2015 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"
27 #include "hard-reg-set.h"
30 #include "insn-config.h"
34 #include "dominance.h"
38 #include "cfgcleanup.h"
39 #include "basic-block.h"
52 #include "insn-codes.h"
54 #include "diagnostic-core.h"
58 #include "tree-pass.h"
61 #include "shrink-wrap.h"
71 #ifndef MAX_CONDITIONAL_EXECUTE
72 #define MAX_CONDITIONAL_EXECUTE \
73 (BRANCH_COST (optimize_function_for_speed_p (cfun), false) \
77 #ifndef HAVE_cbranchcc4
78 #define HAVE_cbranchcc4 0
81 #define IFCVT_MULTIPLE_DUMPS 1
83 #define NULL_BLOCK ((basic_block) NULL)
85 /* True if after combine pass. */
86 static bool ifcvt_after_combine
;
88 /* # of IF-THEN or IF-THEN-ELSE blocks we looked at */
89 static int num_possible_if_blocks
;
91 /* # of IF-THEN or IF-THEN-ELSE blocks were converted to conditional
93 static int num_updated_if_blocks
;
95 /* # of changes made. */
96 static int num_true_changes
;
98 /* Whether conditional execution changes were made. */
99 static int cond_exec_changed_p
;
101 /* Forward references. */
102 static int count_bb_insns (const_basic_block
);
103 static bool cheap_bb_rtx_cost_p (const_basic_block
, int, int);
104 static rtx_insn
*first_active_insn (basic_block
);
105 static rtx_insn
*last_active_insn (basic_block
, int);
106 static rtx_insn
*find_active_insn_before (basic_block
, rtx_insn
*);
107 static rtx_insn
*find_active_insn_after (basic_block
, rtx_insn
*);
108 static basic_block
block_fallthru (basic_block
);
109 static int cond_exec_process_insns (ce_if_block
*, rtx_insn
*, rtx
, rtx
, int,
111 static rtx
cond_exec_get_condition (rtx_insn
*);
112 static rtx
noce_get_condition (rtx_insn
*, rtx_insn
**, bool);
113 static int noce_operand_ok (const_rtx
);
114 static void merge_if_block (ce_if_block
*);
115 static int find_cond_trap (basic_block
, edge
, edge
);
116 static basic_block
find_if_header (basic_block
, int);
117 static int block_jumps_and_fallthru_p (basic_block
, basic_block
);
118 static int noce_find_if_block (basic_block
, edge
, edge
, int);
119 static int cond_exec_find_if_block (ce_if_block
*);
120 static int find_if_case_1 (basic_block
, edge
, edge
);
121 static int find_if_case_2 (basic_block
, edge
, edge
);
122 static int dead_or_predicable (basic_block
, basic_block
, basic_block
,
124 static void noce_emit_move_insn (rtx
, rtx
);
125 static rtx_insn
*block_has_only_trap (basic_block
);
127 /* Count the number of non-jump active insns in BB. */
130 count_bb_insns (const_basic_block bb
)
133 rtx_insn
*insn
= BB_HEAD (bb
);
137 if (active_insn_p (insn
) && !JUMP_P (insn
))
140 if (insn
== BB_END (bb
))
142 insn
= NEXT_INSN (insn
);
148 /* Determine whether the total insn_rtx_cost on non-jump insns in
149 basic block BB is less than MAX_COST. This function returns
150 false if the cost of any instruction could not be estimated.
152 The cost of the non-jump insns in BB is scaled by REG_BR_PROB_BASE
153 as those insns are being speculated. MAX_COST is scaled with SCALE
154 plus a small fudge factor. */
157 cheap_bb_rtx_cost_p (const_basic_block bb
, int scale
, int max_cost
)
160 rtx_insn
*insn
= BB_HEAD (bb
);
161 bool speed
= optimize_bb_for_speed_p (bb
);
163 /* Set scale to REG_BR_PROB_BASE to void the identical scaling
164 applied to insn_rtx_cost when optimizing for size. Only do
165 this after combine because if-conversion might interfere with
166 passes before combine.
168 Use optimize_function_for_speed_p instead of the pre-defined
169 variable speed to make sure it is set to same value for all
170 basic blocks in one if-conversion transformation. */
171 if (!optimize_function_for_speed_p (cfun
) && ifcvt_after_combine
)
172 scale
= REG_BR_PROB_BASE
;
173 /* Our branch probability/scaling factors are just estimates and don't
174 account for cases where we can get speculation for free and other
175 secondary benefits. So we fudge the scale factor to make speculating
176 appear a little more profitable when optimizing for performance. */
178 scale
+= REG_BR_PROB_BASE
/ 8;
185 if (NONJUMP_INSN_P (insn
))
187 int cost
= insn_rtx_cost (PATTERN (insn
), speed
) * REG_BR_PROB_BASE
;
191 /* If this instruction is the load or set of a "stack" register,
192 such as a floating point register on x87, then the cost of
193 speculatively executing this insn may need to include
194 the additional cost of popping its result off of the
195 register stack. Unfortunately, correctly recognizing and
196 accounting for this additional overhead is tricky, so for
197 now we simply prohibit such speculative execution. */
200 rtx set
= single_set (insn
);
201 if (set
&& STACK_REG_P (SET_DEST (set
)))
207 if (count
>= max_cost
)
210 else if (CALL_P (insn
))
213 if (insn
== BB_END (bb
))
215 insn
= NEXT_INSN (insn
);
221 /* Return the first non-jump active insn in the basic block. */
224 first_active_insn (basic_block bb
)
226 rtx_insn
*insn
= BB_HEAD (bb
);
230 if (insn
== BB_END (bb
))
232 insn
= NEXT_INSN (insn
);
235 while (NOTE_P (insn
) || DEBUG_INSN_P (insn
))
237 if (insn
== BB_END (bb
))
239 insn
= NEXT_INSN (insn
);
248 /* Return the last non-jump active (non-jump) insn in the basic block. */
251 last_active_insn (basic_block bb
, int skip_use_p
)
253 rtx_insn
*insn
= BB_END (bb
);
254 rtx_insn
*head
= BB_HEAD (bb
);
258 || DEBUG_INSN_P (insn
)
260 && NONJUMP_INSN_P (insn
)
261 && GET_CODE (PATTERN (insn
)) == USE
))
265 insn
= PREV_INSN (insn
);
274 /* Return the active insn before INSN inside basic block CURR_BB. */
277 find_active_insn_before (basic_block curr_bb
, rtx_insn
*insn
)
279 if (!insn
|| insn
== BB_HEAD (curr_bb
))
282 while ((insn
= PREV_INSN (insn
)) != NULL_RTX
)
284 if (NONJUMP_INSN_P (insn
) || JUMP_P (insn
) || CALL_P (insn
))
287 /* No other active insn all the way to the start of the basic block. */
288 if (insn
== BB_HEAD (curr_bb
))
295 /* Return the active insn after INSN inside basic block CURR_BB. */
298 find_active_insn_after (basic_block curr_bb
, rtx_insn
*insn
)
300 if (!insn
|| insn
== BB_END (curr_bb
))
303 while ((insn
= NEXT_INSN (insn
)) != NULL_RTX
)
305 if (NONJUMP_INSN_P (insn
) || JUMP_P (insn
) || CALL_P (insn
))
308 /* No other active insn all the way to the end of the basic block. */
309 if (insn
== BB_END (curr_bb
))
316 /* Return the basic block reached by falling though the basic block BB. */
319 block_fallthru (basic_block bb
)
321 edge e
= find_fallthru_edge (bb
->succs
);
323 return (e
) ? e
->dest
: NULL_BLOCK
;
326 /* Return true if RTXs A and B can be safely interchanged. */
329 rtx_interchangeable_p (const_rtx a
, const_rtx b
)
331 if (!rtx_equal_p (a
, b
))
334 if (GET_CODE (a
) != MEM
)
337 /* A dead type-unsafe memory reference is legal, but a live type-unsafe memory
338 reference is not. Interchanging a dead type-unsafe memory reference with
339 a live type-safe one creates a live type-unsafe memory reference, in other
340 words, it makes the program illegal.
341 We check here conservatively whether the two memory references have equal
342 memory attributes. */
344 return mem_attrs_eq_p (get_mem_attrs (a
), get_mem_attrs (b
));
348 /* Go through a bunch of insns, converting them to conditional
349 execution format if possible. Return TRUE if all of the non-note
350 insns were processed. */
353 cond_exec_process_insns (ce_if_block
*ce_info ATTRIBUTE_UNUSED
,
354 /* if block information */rtx_insn
*start
,
355 /* first insn to look at */rtx end
,
356 /* last insn to look at */rtx test
,
357 /* conditional execution test */int prob_val
,
358 /* probability of branch taken. */int mod_ok
)
360 int must_be_last
= FALSE
;
368 for (insn
= start
; ; insn
= NEXT_INSN (insn
))
370 /* dwarf2out can't cope with conditional prologues. */
371 if (NOTE_P (insn
) && NOTE_KIND (insn
) == NOTE_INSN_PROLOGUE_END
)
374 if (NOTE_P (insn
) || DEBUG_INSN_P (insn
))
377 gcc_assert (NONJUMP_INSN_P (insn
) || CALL_P (insn
));
379 /* dwarf2out can't cope with conditional unwind info. */
380 if (RTX_FRAME_RELATED_P (insn
))
383 /* Remove USE insns that get in the way. */
384 if (reload_completed
&& GET_CODE (PATTERN (insn
)) == USE
)
386 /* ??? Ug. Actually unlinking the thing is problematic,
387 given what we'd have to coordinate with our callers. */
388 SET_INSN_DELETED (insn
);
392 /* Last insn wasn't last? */
396 if (modified_in_p (test
, insn
))
403 /* Now build the conditional form of the instruction. */
404 pattern
= PATTERN (insn
);
405 xtest
= copy_rtx (test
);
407 /* If this is already a COND_EXEC, rewrite the test to be an AND of the
409 if (GET_CODE (pattern
) == COND_EXEC
)
411 if (GET_MODE (xtest
) != GET_MODE (COND_EXEC_TEST (pattern
)))
414 xtest
= gen_rtx_AND (GET_MODE (xtest
), xtest
,
415 COND_EXEC_TEST (pattern
));
416 pattern
= COND_EXEC_CODE (pattern
);
419 pattern
= gen_rtx_COND_EXEC (VOIDmode
, xtest
, pattern
);
421 /* If the machine needs to modify the insn being conditionally executed,
422 say for example to force a constant integer operand into a temp
423 register, do so here. */
424 #ifdef IFCVT_MODIFY_INSN
425 IFCVT_MODIFY_INSN (ce_info
, pattern
, insn
);
430 validate_change (insn
, &PATTERN (insn
), pattern
, 1);
432 if (CALL_P (insn
) && prob_val
>= 0)
433 validate_change (insn
, ®_NOTES (insn
),
434 gen_rtx_INT_LIST ((machine_mode
) REG_BR_PROB
,
435 prob_val
, REG_NOTES (insn
)), 1);
445 /* Return the condition for a jump. Do not do any special processing. */
448 cond_exec_get_condition (rtx_insn
*jump
)
452 if (any_condjump_p (jump
))
453 test_if
= SET_SRC (pc_set (jump
));
456 cond
= XEXP (test_if
, 0);
458 /* If this branches to JUMP_LABEL when the condition is false,
459 reverse the condition. */
460 if (GET_CODE (XEXP (test_if
, 2)) == LABEL_REF
461 && LABEL_REF_LABEL (XEXP (test_if
, 2)) == JUMP_LABEL (jump
))
463 enum rtx_code rev
= reversed_comparison_code (cond
, jump
);
467 cond
= gen_rtx_fmt_ee (rev
, GET_MODE (cond
), XEXP (cond
, 0),
474 /* Given a simple IF-THEN or IF-THEN-ELSE block, attempt to convert it
475 to conditional execution. Return TRUE if we were successful at
476 converting the block. */
479 cond_exec_process_if_block (ce_if_block
* ce_info
,
480 /* if block information */int do_multiple_p
)
482 basic_block test_bb
= ce_info
->test_bb
; /* last test block */
483 basic_block then_bb
= ce_info
->then_bb
; /* THEN */
484 basic_block else_bb
= ce_info
->else_bb
; /* ELSE or NULL */
485 rtx test_expr
; /* expression in IF_THEN_ELSE that is tested */
486 rtx_insn
*then_start
; /* first insn in THEN block */
487 rtx_insn
*then_end
; /* last insn + 1 in THEN block */
488 rtx_insn
*else_start
= NULL
; /* first insn in ELSE block or NULL */
489 rtx_insn
*else_end
= NULL
; /* last insn + 1 in ELSE block */
490 int max
; /* max # of insns to convert. */
491 int then_mod_ok
; /* whether conditional mods are ok in THEN */
492 rtx true_expr
; /* test for else block insns */
493 rtx false_expr
; /* test for then block insns */
494 int true_prob_val
; /* probability of else block */
495 int false_prob_val
; /* probability of then block */
496 rtx_insn
*then_last_head
= NULL
; /* Last match at the head of THEN */
497 rtx_insn
*else_last_head
= NULL
; /* Last match at the head of ELSE */
498 rtx_insn
*then_first_tail
= NULL
; /* First match at the tail of THEN */
499 rtx_insn
*else_first_tail
= NULL
; /* First match at the tail of ELSE */
500 int then_n_insns
, else_n_insns
, n_insns
;
501 enum rtx_code false_code
;
504 /* If test is comprised of && or || elements, and we've failed at handling
505 all of them together, just use the last test if it is the special case of
506 && elements without an ELSE block. */
507 if (!do_multiple_p
&& ce_info
->num_multiple_test_blocks
)
509 if (else_bb
|| ! ce_info
->and_and_p
)
512 ce_info
->test_bb
= test_bb
= ce_info
->last_test_bb
;
513 ce_info
->num_multiple_test_blocks
= 0;
514 ce_info
->num_and_and_blocks
= 0;
515 ce_info
->num_or_or_blocks
= 0;
518 /* Find the conditional jump to the ELSE or JOIN part, and isolate
520 test_expr
= cond_exec_get_condition (BB_END (test_bb
));
524 /* If the conditional jump is more than just a conditional jump,
525 then we can not do conditional execution conversion on this block. */
526 if (! onlyjump_p (BB_END (test_bb
)))
529 /* Collect the bounds of where we're to search, skipping any labels, jumps
530 and notes at the beginning and end of the block. Then count the total
531 number of insns and see if it is small enough to convert. */
532 then_start
= first_active_insn (then_bb
);
533 then_end
= last_active_insn (then_bb
, TRUE
);
534 then_n_insns
= ce_info
->num_then_insns
= count_bb_insns (then_bb
);
535 n_insns
= then_n_insns
;
536 max
= MAX_CONDITIONAL_EXECUTE
;
543 else_start
= first_active_insn (else_bb
);
544 else_end
= last_active_insn (else_bb
, TRUE
);
545 else_n_insns
= ce_info
->num_else_insns
= count_bb_insns (else_bb
);
546 n_insns
+= else_n_insns
;
548 /* Look for matching sequences at the head and tail of the two blocks,
549 and limit the range of insns to be converted if possible. */
550 n_matching
= flow_find_cross_jump (then_bb
, else_bb
,
551 &then_first_tail
, &else_first_tail
,
553 if (then_first_tail
== BB_HEAD (then_bb
))
554 then_start
= then_end
= NULL
;
555 if (else_first_tail
== BB_HEAD (else_bb
))
556 else_start
= else_end
= NULL
;
561 then_end
= find_active_insn_before (then_bb
, then_first_tail
);
563 else_end
= find_active_insn_before (else_bb
, else_first_tail
);
564 n_insns
-= 2 * n_matching
;
569 && then_n_insns
> n_matching
570 && else_n_insns
> n_matching
)
572 int longest_match
= MIN (then_n_insns
- n_matching
,
573 else_n_insns
- n_matching
);
575 = flow_find_head_matching_sequence (then_bb
, else_bb
,
584 /* We won't pass the insns in the head sequence to
585 cond_exec_process_insns, so we need to test them here
586 to make sure that they don't clobber the condition. */
587 for (insn
= BB_HEAD (then_bb
);
588 insn
!= NEXT_INSN (then_last_head
);
589 insn
= NEXT_INSN (insn
))
590 if (!LABEL_P (insn
) && !NOTE_P (insn
)
591 && !DEBUG_INSN_P (insn
)
592 && modified_in_p (test_expr
, insn
))
596 if (then_last_head
== then_end
)
597 then_start
= then_end
= NULL
;
598 if (else_last_head
== else_end
)
599 else_start
= else_end
= NULL
;
604 then_start
= find_active_insn_after (then_bb
, then_last_head
);
606 else_start
= find_active_insn_after (else_bb
, else_last_head
);
607 n_insns
-= 2 * n_matching
;
615 /* Map test_expr/test_jump into the appropriate MD tests to use on
616 the conditionally executed code. */
618 true_expr
= test_expr
;
620 false_code
= reversed_comparison_code (true_expr
, BB_END (test_bb
));
621 if (false_code
!= UNKNOWN
)
622 false_expr
= gen_rtx_fmt_ee (false_code
, GET_MODE (true_expr
),
623 XEXP (true_expr
, 0), XEXP (true_expr
, 1));
625 false_expr
= NULL_RTX
;
627 #ifdef IFCVT_MODIFY_TESTS
628 /* If the machine description needs to modify the tests, such as setting a
629 conditional execution register from a comparison, it can do so here. */
630 IFCVT_MODIFY_TESTS (ce_info
, true_expr
, false_expr
);
632 /* See if the conversion failed. */
633 if (!true_expr
|| !false_expr
)
637 note
= find_reg_note (BB_END (test_bb
), REG_BR_PROB
, NULL_RTX
);
640 true_prob_val
= XINT (note
, 0);
641 false_prob_val
= REG_BR_PROB_BASE
- true_prob_val
;
649 /* If we have && or || tests, do them here. These tests are in the adjacent
650 blocks after the first block containing the test. */
651 if (ce_info
->num_multiple_test_blocks
> 0)
653 basic_block bb
= test_bb
;
654 basic_block last_test_bb
= ce_info
->last_test_bb
;
661 rtx_insn
*start
, *end
;
663 enum rtx_code f_code
;
665 bb
= block_fallthru (bb
);
666 start
= first_active_insn (bb
);
667 end
= last_active_insn (bb
, TRUE
);
669 && ! cond_exec_process_insns (ce_info
, start
, end
, false_expr
,
670 false_prob_val
, FALSE
))
673 /* If the conditional jump is more than just a conditional jump, then
674 we can not do conditional execution conversion on this block. */
675 if (! onlyjump_p (BB_END (bb
)))
678 /* Find the conditional jump and isolate the test. */
679 t
= cond_exec_get_condition (BB_END (bb
));
683 f_code
= reversed_comparison_code (t
, BB_END (bb
));
684 if (f_code
== UNKNOWN
)
687 f
= gen_rtx_fmt_ee (f_code
, GET_MODE (t
), XEXP (t
, 0), XEXP (t
, 1));
688 if (ce_info
->and_and_p
)
690 t
= gen_rtx_AND (GET_MODE (t
), true_expr
, t
);
691 f
= gen_rtx_IOR (GET_MODE (t
), false_expr
, f
);
695 t
= gen_rtx_IOR (GET_MODE (t
), true_expr
, t
);
696 f
= gen_rtx_AND (GET_MODE (t
), false_expr
, f
);
699 /* If the machine description needs to modify the tests, such as
700 setting a conditional execution register from a comparison, it can
702 #ifdef IFCVT_MODIFY_MULTIPLE_TESTS
703 IFCVT_MODIFY_MULTIPLE_TESTS (ce_info
, bb
, t
, f
);
705 /* See if the conversion failed. */
713 while (bb
!= last_test_bb
);
716 /* For IF-THEN-ELSE blocks, we don't allow modifications of the test
717 on then THEN block. */
718 then_mod_ok
= (else_bb
== NULL_BLOCK
);
720 /* Go through the THEN and ELSE blocks converting the insns if possible
721 to conditional execution. */
725 || ! cond_exec_process_insns (ce_info
, then_start
, then_end
,
726 false_expr
, false_prob_val
,
730 if (else_bb
&& else_end
731 && ! cond_exec_process_insns (ce_info
, else_start
, else_end
,
732 true_expr
, true_prob_val
, TRUE
))
735 /* If we cannot apply the changes, fail. Do not go through the normal fail
736 processing, since apply_change_group will call cancel_changes. */
737 if (! apply_change_group ())
739 #ifdef IFCVT_MODIFY_CANCEL
740 /* Cancel any machine dependent changes. */
741 IFCVT_MODIFY_CANCEL (ce_info
);
746 #ifdef IFCVT_MODIFY_FINAL
747 /* Do any machine dependent final modifications. */
748 IFCVT_MODIFY_FINAL (ce_info
);
751 /* Conversion succeeded. */
753 fprintf (dump_file
, "%d insn%s converted to conditional execution.\n",
754 n_insns
, (n_insns
== 1) ? " was" : "s were");
756 /* Merge the blocks! If we had matching sequences, make sure to delete one
757 copy at the appropriate location first: delete the copy in the THEN branch
758 for a tail sequence so that the remaining one is executed last for both
759 branches, and delete the copy in the ELSE branch for a head sequence so
760 that the remaining one is executed first for both branches. */
763 rtx_insn
*from
= then_first_tail
;
765 from
= find_active_insn_after (then_bb
, from
);
766 delete_insn_chain (from
, BB_END (then_bb
), false);
769 delete_insn_chain (first_active_insn (else_bb
), else_last_head
, false);
771 merge_if_block (ce_info
);
772 cond_exec_changed_p
= TRUE
;
776 #ifdef IFCVT_MODIFY_CANCEL
777 /* Cancel any machine dependent changes. */
778 IFCVT_MODIFY_CANCEL (ce_info
);
785 /* Used by noce_process_if_block to communicate with its subroutines.
787 The subroutines know that A and B may be evaluated freely. They
788 know that X is a register. They should insert new instructions
789 before cond_earliest. */
793 /* The basic blocks that make up the IF-THEN-{ELSE-,}JOIN block. */
794 basic_block test_bb
, then_bb
, else_bb
, join_bb
;
796 /* The jump that ends TEST_BB. */
799 /* The jump condition. */
802 /* New insns should be inserted before this one. */
803 rtx_insn
*cond_earliest
;
805 /* Insns in the THEN and ELSE block. There is always just this
806 one insns in those blocks. The insns are single_set insns.
807 If there was no ELSE block, INSN_B is the last insn before
808 COND_EARLIEST, or NULL_RTX. In the former case, the insn
809 operands are still valid, as if INSN_B was moved down below
811 rtx_insn
*insn_a
, *insn_b
;
813 /* The SET_SRC of INSN_A and INSN_B. */
816 /* The SET_DEST of INSN_A. */
819 /* True if this if block is not canonical. In the canonical form of
820 if blocks, the THEN_BB is the block reached via the fallthru edge
821 from TEST_BB. For the noce transformations, we allow the symmetric
823 bool then_else_reversed
;
825 /* Estimated cost of the particular branch instruction. */
829 static rtx
noce_emit_store_flag (struct noce_if_info
*, rtx
, int, int);
830 static int noce_try_move (struct noce_if_info
*);
831 static int noce_try_store_flag (struct noce_if_info
*);
832 static int noce_try_addcc (struct noce_if_info
*);
833 static int noce_try_store_flag_constants (struct noce_if_info
*);
834 static int noce_try_store_flag_mask (struct noce_if_info
*);
835 static rtx
noce_emit_cmove (struct noce_if_info
*, rtx
, enum rtx_code
, rtx
,
837 static int noce_try_cmove (struct noce_if_info
*);
838 static int noce_try_cmove_arith (struct noce_if_info
*);
839 static rtx
noce_get_alt_condition (struct noce_if_info
*, rtx
, rtx_insn
**);
840 static int noce_try_minmax (struct noce_if_info
*);
841 static int noce_try_abs (struct noce_if_info
*);
842 static int noce_try_sign_mask (struct noce_if_info
*);
844 /* Helper function for noce_try_store_flag*. */
847 noce_emit_store_flag (struct noce_if_info
*if_info
, rtx x
, int reversep
,
850 rtx cond
= if_info
->cond
;
854 cond_complex
= (! general_operand (XEXP (cond
, 0), VOIDmode
)
855 || ! general_operand (XEXP (cond
, 1), VOIDmode
));
857 /* If earliest == jump, or when the condition is complex, try to
858 build the store_flag insn directly. */
862 rtx set
= pc_set (if_info
->jump
);
863 cond
= XEXP (SET_SRC (set
), 0);
864 if (GET_CODE (XEXP (SET_SRC (set
), 2)) == LABEL_REF
865 && LABEL_REF_LABEL (XEXP (SET_SRC (set
), 2)) == JUMP_LABEL (if_info
->jump
))
866 reversep
= !reversep
;
867 if (if_info
->then_else_reversed
)
868 reversep
= !reversep
;
872 code
= reversed_comparison_code (cond
, if_info
->jump
);
874 code
= GET_CODE (cond
);
876 if ((if_info
->cond_earliest
== if_info
->jump
|| cond_complex
)
877 && (normalize
== 0 || STORE_FLAG_VALUE
== normalize
))
879 rtx src
= gen_rtx_fmt_ee (code
, GET_MODE (x
), XEXP (cond
, 0),
881 rtx set
= gen_rtx_SET (x
, src
);
884 rtx_insn
*insn
= emit_insn (set
);
886 if (recog_memoized (insn
) >= 0)
888 rtx_insn
*seq
= get_insns ();
892 if_info
->cond_earliest
= if_info
->jump
;
900 /* Don't even try if the comparison operands or the mode of X are weird. */
901 if (cond_complex
|| !SCALAR_INT_MODE_P (GET_MODE (x
)))
904 return emit_store_flag (x
, code
, XEXP (cond
, 0),
905 XEXP (cond
, 1), VOIDmode
,
906 (code
== LTU
|| code
== LEU
907 || code
== GEU
|| code
== GTU
), normalize
);
910 /* Emit instruction to move an rtx, possibly into STRICT_LOW_PART.
911 X is the destination/target and Y is the value to copy. */
914 noce_emit_move_insn (rtx x
, rtx y
)
916 machine_mode outmode
;
920 if (GET_CODE (x
) != STRICT_LOW_PART
)
922 rtx_insn
*seq
, *insn
;
927 /* Check that the SET_SRC is reasonable before calling emit_move_insn,
928 otherwise construct a suitable SET pattern ourselves. */
929 insn
= (OBJECT_P (y
) || CONSTANT_P (y
) || GET_CODE (y
) == SUBREG
)
930 ? emit_move_insn (x
, y
)
931 : emit_insn (gen_rtx_SET (x
, y
));
935 if (recog_memoized (insn
) <= 0)
937 if (GET_CODE (x
) == ZERO_EXTRACT
)
939 rtx op
= XEXP (x
, 0);
940 unsigned HOST_WIDE_INT size
= INTVAL (XEXP (x
, 1));
941 unsigned HOST_WIDE_INT start
= INTVAL (XEXP (x
, 2));
943 /* store_bit_field expects START to be relative to
944 BYTES_BIG_ENDIAN and adjusts this value for machines with
945 BITS_BIG_ENDIAN != BYTES_BIG_ENDIAN. In order to be able to
946 invoke store_bit_field again it is necessary to have the START
947 value from the first call. */
948 if (BITS_BIG_ENDIAN
!= BYTES_BIG_ENDIAN
)
951 start
= BITS_PER_UNIT
- start
- size
;
954 gcc_assert (REG_P (op
));
955 start
= BITS_PER_WORD
- start
- size
;
959 gcc_assert (start
< (MEM_P (op
) ? BITS_PER_UNIT
: BITS_PER_WORD
));
960 store_bit_field (op
, size
, start
, 0, 0, GET_MODE (x
), y
);
964 switch (GET_RTX_CLASS (GET_CODE (y
)))
967 ot
= code_to_optab (GET_CODE (y
));
971 target
= expand_unop (GET_MODE (y
), ot
, XEXP (y
, 0), x
, 0);
972 if (target
!= NULL_RTX
)
975 emit_move_insn (x
, target
);
984 ot
= code_to_optab (GET_CODE (y
));
988 target
= expand_binop (GET_MODE (y
), ot
,
989 XEXP (y
, 0), XEXP (y
, 1),
991 if (target
!= NULL_RTX
)
994 emit_move_insn (x
, target
);
1010 outer
= XEXP (x
, 0);
1011 inner
= XEXP (outer
, 0);
1012 outmode
= GET_MODE (outer
);
1013 bitpos
= SUBREG_BYTE (outer
) * BITS_PER_UNIT
;
1014 store_bit_field (inner
, GET_MODE_BITSIZE (outmode
), bitpos
,
1018 /* Return the CC reg if it is used in COND. */
1021 cc_in_cond (rtx cond
)
1023 if (HAVE_cbranchcc4
&& cond
1024 && GET_MODE_CLASS (GET_MODE (XEXP (cond
, 0))) == MODE_CC
)
1025 return XEXP (cond
, 0);
1030 /* Return sequence of instructions generated by if conversion. This
1031 function calls end_sequence() to end the current stream, ensures
1032 that the instructions are unshared, recognizable non-jump insns.
1033 On failure, this function returns a NULL_RTX. */
1036 end_ifcvt_sequence (struct noce_if_info
*if_info
)
1039 rtx_insn
*seq
= get_insns ();
1040 rtx cc
= cc_in_cond (if_info
->cond
);
1042 set_used_flags (if_info
->x
);
1043 set_used_flags (if_info
->cond
);
1044 set_used_flags (if_info
->a
);
1045 set_used_flags (if_info
->b
);
1046 unshare_all_rtl_in_chain (seq
);
1049 /* Make sure that all of the instructions emitted are recognizable,
1050 and that we haven't introduced a new jump instruction.
1051 As an exercise for the reader, build a general mechanism that
1052 allows proper placement of required clobbers. */
1053 for (insn
= seq
; insn
; insn
= NEXT_INSN (insn
))
1055 || recog_memoized (insn
) == -1
1056 /* Make sure new generated code does not clobber CC. */
1057 || (cc
&& set_of (cc
, insn
)))
1063 /* Convert "if (a != b) x = a; else x = b" into "x = a" and
1064 "if (a == b) x = a; else x = b" into "x = b". */
1067 noce_try_move (struct noce_if_info
*if_info
)
1069 rtx cond
= if_info
->cond
;
1070 enum rtx_code code
= GET_CODE (cond
);
1074 if (code
!= NE
&& code
!= EQ
)
1077 /* This optimization isn't valid if either A or B could be a NaN
1078 or a signed zero. */
1079 if (HONOR_NANS (if_info
->x
)
1080 || HONOR_SIGNED_ZEROS (if_info
->x
))
1083 /* Check whether the operands of the comparison are A and in
1085 if ((rtx_equal_p (if_info
->a
, XEXP (cond
, 0))
1086 && rtx_equal_p (if_info
->b
, XEXP (cond
, 1)))
1087 || (rtx_equal_p (if_info
->a
, XEXP (cond
, 1))
1088 && rtx_equal_p (if_info
->b
, XEXP (cond
, 0))))
1090 if (!rtx_interchangeable_p (if_info
->a
, if_info
->b
))
1093 y
= (code
== EQ
) ? if_info
->a
: if_info
->b
;
1095 /* Avoid generating the move if the source is the destination. */
1096 if (! rtx_equal_p (if_info
->x
, y
))
1099 noce_emit_move_insn (if_info
->x
, y
);
1100 seq
= end_ifcvt_sequence (if_info
);
1104 emit_insn_before_setloc (seq
, if_info
->jump
,
1105 INSN_LOCATION (if_info
->insn_a
));
1112 /* Convert "if (test) x = 1; else x = 0".
1114 Only try 0 and STORE_FLAG_VALUE here. Other combinations will be
1115 tried in noce_try_store_flag_constants after noce_try_cmove has had
1116 a go at the conversion. */
1119 noce_try_store_flag (struct noce_if_info
*if_info
)
1125 if (CONST_INT_P (if_info
->b
)
1126 && INTVAL (if_info
->b
) == STORE_FLAG_VALUE
1127 && if_info
->a
== const0_rtx
)
1129 else if (if_info
->b
== const0_rtx
1130 && CONST_INT_P (if_info
->a
)
1131 && INTVAL (if_info
->a
) == STORE_FLAG_VALUE
1132 && (reversed_comparison_code (if_info
->cond
, if_info
->jump
)
1140 target
= noce_emit_store_flag (if_info
, if_info
->x
, reversep
, 0);
1143 if (target
!= if_info
->x
)
1144 noce_emit_move_insn (if_info
->x
, target
);
1146 seq
= end_ifcvt_sequence (if_info
);
1150 emit_insn_before_setloc (seq
, if_info
->jump
,
1151 INSN_LOCATION (if_info
->insn_a
));
1161 /* Convert "if (test) x = a; else x = b", for A and B constant. */
1164 noce_try_store_flag_constants (struct noce_if_info
*if_info
)
1169 HOST_WIDE_INT itrue
, ifalse
, diff
, tmp
;
1170 int normalize
, can_reverse
;
1173 if (CONST_INT_P (if_info
->a
)
1174 && CONST_INT_P (if_info
->b
))
1176 mode
= GET_MODE (if_info
->x
);
1177 ifalse
= INTVAL (if_info
->a
);
1178 itrue
= INTVAL (if_info
->b
);
1180 diff
= (unsigned HOST_WIDE_INT
) itrue
- ifalse
;
1181 /* Make sure we can represent the difference between the two values. */
1183 != ((ifalse
< 0) != (itrue
< 0) ? ifalse
< 0 : ifalse
< itrue
))
1186 diff
= trunc_int_for_mode (diff
, mode
);
1188 can_reverse
= (reversed_comparison_code (if_info
->cond
, if_info
->jump
)
1192 if (diff
== STORE_FLAG_VALUE
|| diff
== -STORE_FLAG_VALUE
)
1194 else if (ifalse
== 0 && exact_log2 (itrue
) >= 0
1195 && (STORE_FLAG_VALUE
== 1
1196 || if_info
->branch_cost
>= 2))
1198 else if (itrue
== 0 && exact_log2 (ifalse
) >= 0 && can_reverse
1199 && (STORE_FLAG_VALUE
== 1 || if_info
->branch_cost
>= 2))
1200 normalize
= 1, reversep
= 1;
1201 else if (itrue
== -1
1202 && (STORE_FLAG_VALUE
== -1
1203 || if_info
->branch_cost
>= 2))
1205 else if (ifalse
== -1 && can_reverse
1206 && (STORE_FLAG_VALUE
== -1 || if_info
->branch_cost
>= 2))
1207 normalize
= -1, reversep
= 1;
1208 else if ((if_info
->branch_cost
>= 2 && STORE_FLAG_VALUE
== -1)
1209 || if_info
->branch_cost
>= 3)
1216 std::swap (itrue
, ifalse
);
1217 diff
= trunc_int_for_mode (-(unsigned HOST_WIDE_INT
) diff
, mode
);
1221 target
= noce_emit_store_flag (if_info
, if_info
->x
, reversep
, normalize
);
1228 /* if (test) x = 3; else x = 4;
1229 => x = 3 + (test == 0); */
1230 if (diff
== STORE_FLAG_VALUE
|| diff
== -STORE_FLAG_VALUE
)
1232 target
= expand_simple_binop (mode
,
1233 (diff
== STORE_FLAG_VALUE
1235 gen_int_mode (ifalse
, mode
), target
,
1236 if_info
->x
, 0, OPTAB_WIDEN
);
1239 /* if (test) x = 8; else x = 0;
1240 => x = (test != 0) << 3; */
1241 else if (ifalse
== 0 && (tmp
= exact_log2 (itrue
)) >= 0)
1243 target
= expand_simple_binop (mode
, ASHIFT
,
1244 target
, GEN_INT (tmp
), if_info
->x
, 0,
1248 /* if (test) x = -1; else x = b;
1249 => x = -(test != 0) | b; */
1250 else if (itrue
== -1)
1252 target
= expand_simple_binop (mode
, IOR
,
1253 target
, gen_int_mode (ifalse
, mode
),
1254 if_info
->x
, 0, OPTAB_WIDEN
);
1257 /* if (test) x = a; else x = b;
1258 => x = (-(test != 0) & (b - a)) + a; */
1261 target
= expand_simple_binop (mode
, AND
,
1262 target
, gen_int_mode (diff
, mode
),
1263 if_info
->x
, 0, OPTAB_WIDEN
);
1265 target
= expand_simple_binop (mode
, PLUS
,
1266 target
, gen_int_mode (ifalse
, mode
),
1267 if_info
->x
, 0, OPTAB_WIDEN
);
1276 if (target
!= if_info
->x
)
1277 noce_emit_move_insn (if_info
->x
, target
);
1279 seq
= end_ifcvt_sequence (if_info
);
1283 emit_insn_before_setloc (seq
, if_info
->jump
,
1284 INSN_LOCATION (if_info
->insn_a
));
1291 /* Convert "if (test) foo++" into "foo += (test != 0)", and
1292 similarly for "foo--". */
1295 noce_try_addcc (struct noce_if_info
*if_info
)
1299 int subtract
, normalize
;
1301 if (GET_CODE (if_info
->a
) == PLUS
1302 && rtx_equal_p (XEXP (if_info
->a
, 0), if_info
->b
)
1303 && (reversed_comparison_code (if_info
->cond
, if_info
->jump
)
1306 rtx cond
= if_info
->cond
;
1307 enum rtx_code code
= reversed_comparison_code (cond
, if_info
->jump
);
1309 /* First try to use addcc pattern. */
1310 if (general_operand (XEXP (cond
, 0), VOIDmode
)
1311 && general_operand (XEXP (cond
, 1), VOIDmode
))
1314 target
= emit_conditional_add (if_info
->x
, code
,
1319 XEXP (if_info
->a
, 1),
1320 GET_MODE (if_info
->x
),
1321 (code
== LTU
|| code
== GEU
1322 || code
== LEU
|| code
== GTU
));
1325 if (target
!= if_info
->x
)
1326 noce_emit_move_insn (if_info
->x
, target
);
1328 seq
= end_ifcvt_sequence (if_info
);
1332 emit_insn_before_setloc (seq
, if_info
->jump
,
1333 INSN_LOCATION (if_info
->insn_a
));
1339 /* If that fails, construct conditional increment or decrement using
1341 if (if_info
->branch_cost
>= 2
1342 && (XEXP (if_info
->a
, 1) == const1_rtx
1343 || XEXP (if_info
->a
, 1) == constm1_rtx
))
1346 if (STORE_FLAG_VALUE
== INTVAL (XEXP (if_info
->a
, 1)))
1347 subtract
= 0, normalize
= 0;
1348 else if (-STORE_FLAG_VALUE
== INTVAL (XEXP (if_info
->a
, 1)))
1349 subtract
= 1, normalize
= 0;
1351 subtract
= 0, normalize
= INTVAL (XEXP (if_info
->a
, 1));
1354 target
= noce_emit_store_flag (if_info
,
1355 gen_reg_rtx (GET_MODE (if_info
->x
)),
1359 target
= expand_simple_binop (GET_MODE (if_info
->x
),
1360 subtract
? MINUS
: PLUS
,
1361 if_info
->b
, target
, if_info
->x
,
1365 if (target
!= if_info
->x
)
1366 noce_emit_move_insn (if_info
->x
, target
);
1368 seq
= end_ifcvt_sequence (if_info
);
1372 emit_insn_before_setloc (seq
, if_info
->jump
,
1373 INSN_LOCATION (if_info
->insn_a
));
1383 /* Convert "if (test) x = 0;" to "x &= -(test == 0);" */
1386 noce_try_store_flag_mask (struct noce_if_info
*if_info
)
1393 if ((if_info
->branch_cost
>= 2
1394 || STORE_FLAG_VALUE
== -1)
1395 && ((if_info
->a
== const0_rtx
1396 && rtx_equal_p (if_info
->b
, if_info
->x
))
1397 || ((reversep
= (reversed_comparison_code (if_info
->cond
,
1400 && if_info
->b
== const0_rtx
1401 && rtx_equal_p (if_info
->a
, if_info
->x
))))
1404 target
= noce_emit_store_flag (if_info
,
1405 gen_reg_rtx (GET_MODE (if_info
->x
)),
1408 target
= expand_simple_binop (GET_MODE (if_info
->x
), AND
,
1410 target
, if_info
->x
, 0,
1415 int old_cost
, new_cost
, insn_cost
;
1418 if (target
!= if_info
->x
)
1419 noce_emit_move_insn (if_info
->x
, target
);
1421 seq
= end_ifcvt_sequence (if_info
);
1425 speed_p
= optimize_bb_for_speed_p (BLOCK_FOR_INSN (if_info
->insn_a
));
1426 insn_cost
= insn_rtx_cost (PATTERN (if_info
->insn_a
), speed_p
);
1427 old_cost
= COSTS_N_INSNS (if_info
->branch_cost
) + insn_cost
;
1428 new_cost
= seq_cost (seq
, speed_p
);
1430 if (new_cost
> old_cost
)
1433 emit_insn_before_setloc (seq
, if_info
->jump
,
1434 INSN_LOCATION (if_info
->insn_a
));
1444 /* Helper function for noce_try_cmove and noce_try_cmove_arith. */
1447 noce_emit_cmove (struct noce_if_info
*if_info
, rtx x
, enum rtx_code code
,
1448 rtx cmp_a
, rtx cmp_b
, rtx vfalse
, rtx vtrue
)
1450 rtx target ATTRIBUTE_UNUSED
;
1451 int unsignedp ATTRIBUTE_UNUSED
;
1453 /* If earliest == jump, try to build the cmove insn directly.
1454 This is helpful when combine has created some complex condition
1455 (like for alpha's cmovlbs) that we can't hope to regenerate
1456 through the normal interface. */
1458 if (if_info
->cond_earliest
== if_info
->jump
)
1460 rtx cond
= gen_rtx_fmt_ee (code
, GET_MODE (if_info
->cond
), cmp_a
, cmp_b
);
1461 rtx if_then_else
= gen_rtx_IF_THEN_ELSE (GET_MODE (x
),
1462 cond
, vtrue
, vfalse
);
1463 rtx set
= gen_rtx_SET (x
, if_then_else
);
1466 rtx_insn
*insn
= emit_insn (set
);
1468 if (recog_memoized (insn
) >= 0)
1470 rtx_insn
*seq
= get_insns ();
1480 /* Don't even try if the comparison operands are weird
1481 except that the target supports cbranchcc4. */
1482 if (! general_operand (cmp_a
, GET_MODE (cmp_a
))
1483 || ! general_operand (cmp_b
, GET_MODE (cmp_b
)))
1485 if (!(HAVE_cbranchcc4
)
1486 || GET_MODE_CLASS (GET_MODE (cmp_a
)) != MODE_CC
1487 || cmp_b
!= const0_rtx
)
1491 unsignedp
= (code
== LTU
|| code
== GEU
1492 || code
== LEU
|| code
== GTU
);
1494 target
= emit_conditional_move (x
, code
, cmp_a
, cmp_b
, VOIDmode
,
1495 vtrue
, vfalse
, GET_MODE (x
),
1500 /* We might be faced with a situation like:
1503 vtrue = (subreg:M (reg:N VTRUE) BYTE)
1504 vfalse = (subreg:M (reg:N VFALSE) BYTE)
1506 We can't do a conditional move in mode M, but it's possible that we
1507 could do a conditional move in mode N instead and take a subreg of
1510 If we can't create new pseudos, though, don't bother. */
1511 if (reload_completed
)
1514 if (GET_CODE (vtrue
) == SUBREG
&& GET_CODE (vfalse
) == SUBREG
)
1516 rtx reg_vtrue
= SUBREG_REG (vtrue
);
1517 rtx reg_vfalse
= SUBREG_REG (vfalse
);
1518 unsigned int byte_vtrue
= SUBREG_BYTE (vtrue
);
1519 unsigned int byte_vfalse
= SUBREG_BYTE (vfalse
);
1520 rtx promoted_target
;
1522 if (GET_MODE (reg_vtrue
) != GET_MODE (reg_vfalse
)
1523 || byte_vtrue
!= byte_vfalse
1524 || (SUBREG_PROMOTED_VAR_P (vtrue
)
1525 != SUBREG_PROMOTED_VAR_P (vfalse
))
1526 || (SUBREG_PROMOTED_GET (vtrue
)
1527 != SUBREG_PROMOTED_GET (vfalse
)))
1530 promoted_target
= gen_reg_rtx (GET_MODE (reg_vtrue
));
1532 target
= emit_conditional_move (promoted_target
, code
, cmp_a
, cmp_b
,
1533 VOIDmode
, reg_vtrue
, reg_vfalse
,
1534 GET_MODE (reg_vtrue
), unsignedp
);
1535 /* Nope, couldn't do it in that mode either. */
1539 target
= gen_rtx_SUBREG (GET_MODE (vtrue
), promoted_target
, byte_vtrue
);
1540 SUBREG_PROMOTED_VAR_P (target
) = SUBREG_PROMOTED_VAR_P (vtrue
);
1541 SUBREG_PROMOTED_SET (target
, SUBREG_PROMOTED_GET (vtrue
));
1542 emit_move_insn (x
, target
);
1549 /* Try only simple constants and registers here. More complex cases
1550 are handled in noce_try_cmove_arith after noce_try_store_flag_arith
1551 has had a go at it. */
1554 noce_try_cmove (struct noce_if_info
*if_info
)
1560 if ((CONSTANT_P (if_info
->a
) || register_operand (if_info
->a
, VOIDmode
))
1561 && (CONSTANT_P (if_info
->b
) || register_operand (if_info
->b
, VOIDmode
)))
1565 code
= GET_CODE (if_info
->cond
);
1566 target
= noce_emit_cmove (if_info
, if_info
->x
, code
,
1567 XEXP (if_info
->cond
, 0),
1568 XEXP (if_info
->cond
, 1),
1569 if_info
->a
, if_info
->b
);
1573 if (target
!= if_info
->x
)
1574 noce_emit_move_insn (if_info
->x
, target
);
1576 seq
= end_ifcvt_sequence (if_info
);
1580 emit_insn_before_setloc (seq
, if_info
->jump
,
1581 INSN_LOCATION (if_info
->insn_a
));
1594 /* Try more complex cases involving conditional_move. */
1597 noce_try_cmove_arith (struct noce_if_info
*if_info
)
1603 rtx_insn
*insn_a
, *insn_b
;
1608 rtx_insn
*ifcvt_seq
;
1610 /* A conditional move from two memory sources is equivalent to a
1611 conditional on their addresses followed by a load. Don't do this
1612 early because it'll screw alias analysis. Note that we've
1613 already checked for no side effects. */
1614 /* ??? FIXME: Magic number 5. */
1615 if (cse_not_expected
1616 && MEM_P (a
) && MEM_P (b
)
1617 && MEM_ADDR_SPACE (a
) == MEM_ADDR_SPACE (b
)
1618 && if_info
->branch_cost
>= 5)
1620 machine_mode address_mode
= get_address_mode (a
);
1624 x
= gen_reg_rtx (address_mode
);
1628 /* ??? We could handle this if we knew that a load from A or B could
1629 not trap or fault. This is also true if we've already loaded
1630 from the address along the path from ENTRY. */
1631 else if (may_trap_or_fault_p (a
) || may_trap_or_fault_p (b
))
1634 /* if (test) x = a + b; else x = c - d;
1641 code
= GET_CODE (if_info
->cond
);
1642 insn_a
= if_info
->insn_a
;
1643 insn_b
= if_info
->insn_b
;
1645 /* Total insn_rtx_cost should be smaller than branch cost. Exit
1646 if insn_rtx_cost can't be estimated. */
1650 = insn_rtx_cost (PATTERN (insn_a
),
1651 optimize_bb_for_speed_p (BLOCK_FOR_INSN (insn_a
)));
1652 if (insn_cost
== 0 || insn_cost
> COSTS_N_INSNS (if_info
->branch_cost
))
1661 += insn_rtx_cost (PATTERN (insn_b
),
1662 optimize_bb_for_speed_p (BLOCK_FOR_INSN (insn_b
)));
1663 if (insn_cost
== 0 || insn_cost
> COSTS_N_INSNS (if_info
->branch_cost
))
1667 /* Possibly rearrange operands to make things come out more natural. */
1668 if (reversed_comparison_code (if_info
->cond
, if_info
->jump
) != UNKNOWN
)
1671 if (rtx_equal_p (b
, x
))
1673 else if (general_operand (b
, GET_MODE (b
)))
1678 code
= reversed_comparison_code (if_info
->cond
, if_info
->jump
);
1680 std::swap (insn_a
, insn_b
);
1689 /* If either operand is complex, load it into a register first.
1690 The best way to do this is to copy the original insn. In this
1691 way we preserve any clobbers etc that the insn may have had.
1692 This is of course not possible in the IS_MEM case. */
1693 if (! general_operand (a
, GET_MODE (a
)))
1699 rtx reg
= gen_reg_rtx (GET_MODE (a
));
1700 insn
= emit_insn (gen_rtx_SET (reg
, a
));
1703 goto end_seq_and_fail
;
1706 a
= gen_reg_rtx (GET_MODE (a
));
1707 rtx_insn
*copy_of_a
= as_a
<rtx_insn
*> (copy_rtx (insn_a
));
1708 rtx set
= single_set (copy_of_a
);
1710 insn
= emit_insn (PATTERN (copy_of_a
));
1712 if (recog_memoized (insn
) < 0)
1713 goto end_seq_and_fail
;
1715 if (! general_operand (b
, GET_MODE (b
)))
1723 rtx reg
= gen_reg_rtx (GET_MODE (b
));
1724 pat
= gen_rtx_SET (reg
, b
);
1727 goto end_seq_and_fail
;
1730 b
= gen_reg_rtx (GET_MODE (b
));
1731 rtx_insn
*copy_of_insn_b
= as_a
<rtx_insn
*> (copy_rtx (insn_b
));
1732 rtx set
= single_set (copy_of_insn_b
);
1734 pat
= PATTERN (copy_of_insn_b
);
1737 /* If insn to set up A clobbers any registers B depends on, try to
1738 swap insn that sets up A with the one that sets up B. If even
1739 that doesn't help, punt. */
1740 last
= get_last_insn ();
1741 if (last
&& modified_in_p (orig_b
, last
))
1743 new_insn
= emit_insn_before (pat
, get_insns ());
1744 if (modified_in_p (orig_a
, new_insn
))
1745 goto end_seq_and_fail
;
1748 new_insn
= emit_insn (pat
);
1750 if (recog_memoized (new_insn
) < 0)
1751 goto end_seq_and_fail
;
1754 target
= noce_emit_cmove (if_info
, x
, code
, XEXP (if_info
->cond
, 0),
1755 XEXP (if_info
->cond
, 1), a
, b
);
1758 goto end_seq_and_fail
;
1760 /* If we're handling a memory for above, emit the load now. */
1763 rtx mem
= gen_rtx_MEM (GET_MODE (if_info
->x
), target
);
1765 /* Copy over flags as appropriate. */
1766 if (MEM_VOLATILE_P (if_info
->a
) || MEM_VOLATILE_P (if_info
->b
))
1767 MEM_VOLATILE_P (mem
) = 1;
1768 if (MEM_ALIAS_SET (if_info
->a
) == MEM_ALIAS_SET (if_info
->b
))
1769 set_mem_alias_set (mem
, MEM_ALIAS_SET (if_info
->a
));
1771 MIN (MEM_ALIGN (if_info
->a
), MEM_ALIGN (if_info
->b
)));
1773 gcc_assert (MEM_ADDR_SPACE (if_info
->a
) == MEM_ADDR_SPACE (if_info
->b
));
1774 set_mem_addr_space (mem
, MEM_ADDR_SPACE (if_info
->a
));
1776 noce_emit_move_insn (if_info
->x
, mem
);
1778 else if (target
!= x
)
1779 noce_emit_move_insn (x
, target
);
1781 ifcvt_seq
= end_ifcvt_sequence (if_info
);
1785 emit_insn_before_setloc (ifcvt_seq
, if_info
->jump
,
1786 INSN_LOCATION (if_info
->insn_a
));
1794 /* For most cases, the simplified condition we found is the best
1795 choice, but this is not the case for the min/max/abs transforms.
1796 For these we wish to know that it is A or B in the condition. */
1799 noce_get_alt_condition (struct noce_if_info
*if_info
, rtx target
,
1800 rtx_insn
**earliest
)
1806 /* If target is already mentioned in the known condition, return it. */
1807 if (reg_mentioned_p (target
, if_info
->cond
))
1809 *earliest
= if_info
->cond_earliest
;
1810 return if_info
->cond
;
1813 set
= pc_set (if_info
->jump
);
1814 cond
= XEXP (SET_SRC (set
), 0);
1816 = GET_CODE (XEXP (SET_SRC (set
), 2)) == LABEL_REF
1817 && LABEL_REF_LABEL (XEXP (SET_SRC (set
), 2)) == JUMP_LABEL (if_info
->jump
);
1818 if (if_info
->then_else_reversed
)
1821 /* If we're looking for a constant, try to make the conditional
1822 have that constant in it. There are two reasons why it may
1823 not have the constant we want:
1825 1. GCC may have needed to put the constant in a register, because
1826 the target can't compare directly against that constant. For
1827 this case, we look for a SET immediately before the comparison
1828 that puts a constant in that register.
1830 2. GCC may have canonicalized the conditional, for example
1831 replacing "if x < 4" with "if x <= 3". We can undo that (or
1832 make equivalent types of changes) to get the constants we need
1833 if they're off by one in the right direction. */
1835 if (CONST_INT_P (target
))
1837 enum rtx_code code
= GET_CODE (if_info
->cond
);
1838 rtx op_a
= XEXP (if_info
->cond
, 0);
1839 rtx op_b
= XEXP (if_info
->cond
, 1);
1840 rtx_insn
*prev_insn
;
1842 /* First, look to see if we put a constant in a register. */
1843 prev_insn
= prev_nonnote_insn (if_info
->cond_earliest
);
1845 && BLOCK_FOR_INSN (prev_insn
)
1846 == BLOCK_FOR_INSN (if_info
->cond_earliest
)
1847 && INSN_P (prev_insn
)
1848 && GET_CODE (PATTERN (prev_insn
)) == SET
)
1850 rtx src
= find_reg_equal_equiv_note (prev_insn
);
1852 src
= SET_SRC (PATTERN (prev_insn
));
1853 if (CONST_INT_P (src
))
1855 if (rtx_equal_p (op_a
, SET_DEST (PATTERN (prev_insn
))))
1857 else if (rtx_equal_p (op_b
, SET_DEST (PATTERN (prev_insn
))))
1860 if (CONST_INT_P (op_a
))
1862 std::swap (op_a
, op_b
);
1863 code
= swap_condition (code
);
1868 /* Now, look to see if we can get the right constant by
1869 adjusting the conditional. */
1870 if (CONST_INT_P (op_b
))
1872 HOST_WIDE_INT desired_val
= INTVAL (target
);
1873 HOST_WIDE_INT actual_val
= INTVAL (op_b
);
1878 if (actual_val
== desired_val
+ 1)
1881 op_b
= GEN_INT (desired_val
);
1885 if (actual_val
== desired_val
- 1)
1888 op_b
= GEN_INT (desired_val
);
1892 if (actual_val
== desired_val
- 1)
1895 op_b
= GEN_INT (desired_val
);
1899 if (actual_val
== desired_val
+ 1)
1902 op_b
= GEN_INT (desired_val
);
1910 /* If we made any changes, generate a new conditional that is
1911 equivalent to what we started with, but has the right
1913 if (code
!= GET_CODE (if_info
->cond
)
1914 || op_a
!= XEXP (if_info
->cond
, 0)
1915 || op_b
!= XEXP (if_info
->cond
, 1))
1917 cond
= gen_rtx_fmt_ee (code
, GET_MODE (cond
), op_a
, op_b
);
1918 *earliest
= if_info
->cond_earliest
;
1923 cond
= canonicalize_condition (if_info
->jump
, cond
, reverse
,
1924 earliest
, target
, HAVE_cbranchcc4
, true);
1925 if (! cond
|| ! reg_mentioned_p (target
, cond
))
1928 /* We almost certainly searched back to a different place.
1929 Need to re-verify correct lifetimes. */
1931 /* X may not be mentioned in the range (cond_earliest, jump]. */
1932 for (insn
= if_info
->jump
; insn
!= *earliest
; insn
= PREV_INSN (insn
))
1933 if (INSN_P (insn
) && reg_overlap_mentioned_p (if_info
->x
, PATTERN (insn
)))
1936 /* A and B may not be modified in the range [cond_earliest, jump). */
1937 for (insn
= *earliest
; insn
!= if_info
->jump
; insn
= NEXT_INSN (insn
))
1939 && (modified_in_p (if_info
->a
, insn
)
1940 || modified_in_p (if_info
->b
, insn
)))
1946 /* Convert "if (a < b) x = a; else x = b;" to "x = min(a, b);", etc. */
1949 noce_try_minmax (struct noce_if_info
*if_info
)
1952 rtx_insn
*earliest
, *seq
;
1953 enum rtx_code code
, op
;
1956 /* ??? Reject modes with NaNs or signed zeros since we don't know how
1957 they will be resolved with an SMIN/SMAX. It wouldn't be too hard
1958 to get the target to tell us... */
1959 if (HONOR_SIGNED_ZEROS (if_info
->x
)
1960 || HONOR_NANS (if_info
->x
))
1963 cond
= noce_get_alt_condition (if_info
, if_info
->a
, &earliest
);
1967 /* Verify the condition is of the form we expect, and canonicalize
1968 the comparison code. */
1969 code
= GET_CODE (cond
);
1970 if (rtx_equal_p (XEXP (cond
, 0), if_info
->a
))
1972 if (! rtx_equal_p (XEXP (cond
, 1), if_info
->b
))
1975 else if (rtx_equal_p (XEXP (cond
, 1), if_info
->a
))
1977 if (! rtx_equal_p (XEXP (cond
, 0), if_info
->b
))
1979 code
= swap_condition (code
);
1984 /* Determine what sort of operation this is. Note that the code is for
1985 a taken branch, so the code->operation mapping appears backwards. */
2018 target
= expand_simple_binop (GET_MODE (if_info
->x
), op
,
2019 if_info
->a
, if_info
->b
,
2020 if_info
->x
, unsignedp
, OPTAB_WIDEN
);
2026 if (target
!= if_info
->x
)
2027 noce_emit_move_insn (if_info
->x
, target
);
2029 seq
= end_ifcvt_sequence (if_info
);
2033 emit_insn_before_setloc (seq
, if_info
->jump
, INSN_LOCATION (if_info
->insn_a
));
2034 if_info
->cond
= cond
;
2035 if_info
->cond_earliest
= earliest
;
2040 /* Convert "if (a < 0) x = -a; else x = a;" to "x = abs(a);",
2041 "if (a < 0) x = ~a; else x = a;" to "x = one_cmpl_abs(a);",
2045 noce_try_abs (struct noce_if_info
*if_info
)
2047 rtx cond
, target
, a
, b
, c
;
2048 rtx_insn
*earliest
, *seq
;
2050 bool one_cmpl
= false;
2052 /* Reject modes with signed zeros. */
2053 if (HONOR_SIGNED_ZEROS (if_info
->x
))
2056 /* Recognize A and B as constituting an ABS or NABS. The canonical
2057 form is a branch around the negation, taken when the object is the
2058 first operand of a comparison against 0 that evaluates to true. */
2061 if (GET_CODE (a
) == NEG
&& rtx_equal_p (XEXP (a
, 0), b
))
2063 else if (GET_CODE (b
) == NEG
&& rtx_equal_p (XEXP (b
, 0), a
))
2068 else if (GET_CODE (a
) == NOT
&& rtx_equal_p (XEXP (a
, 0), b
))
2073 else if (GET_CODE (b
) == NOT
&& rtx_equal_p (XEXP (b
, 0), a
))
2082 cond
= noce_get_alt_condition (if_info
, b
, &earliest
);
2086 /* Verify the condition is of the form we expect. */
2087 if (rtx_equal_p (XEXP (cond
, 0), b
))
2089 else if (rtx_equal_p (XEXP (cond
, 1), b
))
2097 /* Verify that C is zero. Search one step backward for a
2098 REG_EQUAL note or a simple source if necessary. */
2102 rtx_insn
*insn
= prev_nonnote_insn (earliest
);
2104 && BLOCK_FOR_INSN (insn
) == BLOCK_FOR_INSN (earliest
)
2105 && (set
= single_set (insn
))
2106 && rtx_equal_p (SET_DEST (set
), c
))
2108 rtx note
= find_reg_equal_equiv_note (insn
);
2118 && GET_CODE (XEXP (c
, 0)) == SYMBOL_REF
2119 && CONSTANT_POOL_ADDRESS_P (XEXP (c
, 0)))
2120 c
= get_pool_constant (XEXP (c
, 0));
2122 /* Work around funny ideas get_condition has wrt canonicalization.
2123 Note that these rtx constants are known to be CONST_INT, and
2124 therefore imply integer comparisons. */
2125 if (c
== constm1_rtx
&& GET_CODE (cond
) == GT
)
2127 else if (c
== const1_rtx
&& GET_CODE (cond
) == LT
)
2129 else if (c
!= CONST0_RTX (GET_MODE (b
)))
2132 /* Determine what sort of operation this is. */
2133 switch (GET_CODE (cond
))
2152 target
= expand_one_cmpl_abs_nojump (GET_MODE (if_info
->x
), b
,
2155 target
= expand_abs_nojump (GET_MODE (if_info
->x
), b
, if_info
->x
, 1);
2157 /* ??? It's a quandary whether cmove would be better here, especially
2158 for integers. Perhaps combine will clean things up. */
2159 if (target
&& negate
)
2162 target
= expand_simple_unop (GET_MODE (target
), NOT
, target
,
2165 target
= expand_simple_unop (GET_MODE (target
), NEG
, target
,
2175 if (target
!= if_info
->x
)
2176 noce_emit_move_insn (if_info
->x
, target
);
2178 seq
= end_ifcvt_sequence (if_info
);
2182 emit_insn_before_setloc (seq
, if_info
->jump
, INSN_LOCATION (if_info
->insn_a
));
2183 if_info
->cond
= cond
;
2184 if_info
->cond_earliest
= earliest
;
2189 /* Convert "if (m < 0) x = b; else x = 0;" to "x = (m >> C) & b;". */
2192 noce_try_sign_mask (struct noce_if_info
*if_info
)
2198 bool t_unconditional
;
2200 cond
= if_info
->cond
;
2201 code
= GET_CODE (cond
);
2206 if (if_info
->a
== const0_rtx
)
2208 if ((code
== LT
&& c
== const0_rtx
)
2209 || (code
== LE
&& c
== constm1_rtx
))
2212 else if (if_info
->b
== const0_rtx
)
2214 if ((code
== GE
&& c
== const0_rtx
)
2215 || (code
== GT
&& c
== constm1_rtx
))
2219 if (! t
|| side_effects_p (t
))
2222 /* We currently don't handle different modes. */
2223 mode
= GET_MODE (t
);
2224 if (GET_MODE (m
) != mode
)
2227 /* This is only profitable if T is unconditionally executed/evaluated in the
2228 original insn sequence or T is cheap. The former happens if B is the
2229 non-zero (T) value and if INSN_B was taken from TEST_BB, or there was no
2230 INSN_B which can happen for e.g. conditional stores to memory. For the
2231 cost computation use the block TEST_BB where the evaluation will end up
2232 after the transformation. */
2235 && (if_info
->insn_b
== NULL_RTX
2236 || BLOCK_FOR_INSN (if_info
->insn_b
) == if_info
->test_bb
));
2237 if (!(t_unconditional
2238 || (set_src_cost (t
, optimize_bb_for_speed_p (if_info
->test_bb
))
2239 < COSTS_N_INSNS (2))))
2243 /* Use emit_store_flag to generate "m < 0 ? -1 : 0" instead of expanding
2244 "(signed) m >> 31" directly. This benefits targets with specialized
2245 insns to obtain the signmask, but still uses ashr_optab otherwise. */
2246 m
= emit_store_flag (gen_reg_rtx (mode
), LT
, m
, const0_rtx
, mode
, 0, -1);
2247 t
= m
? expand_binop (mode
, and_optab
, m
, t
, NULL_RTX
, 0, OPTAB_DIRECT
)
2256 noce_emit_move_insn (if_info
->x
, t
);
2258 seq
= end_ifcvt_sequence (if_info
);
2262 emit_insn_before_setloc (seq
, if_info
->jump
, INSN_LOCATION (if_info
->insn_a
));
2267 /* Optimize away "if (x & C) x |= C" and similar bit manipulation
2271 noce_try_bitop (struct noce_if_info
*if_info
)
2273 rtx cond
, x
, a
, result
;
2280 cond
= if_info
->cond
;
2281 code
= GET_CODE (cond
);
2283 /* Check for no else condition. */
2284 if (! rtx_equal_p (x
, if_info
->b
))
2287 /* Check for a suitable condition. */
2288 if (code
!= NE
&& code
!= EQ
)
2290 if (XEXP (cond
, 1) != const0_rtx
)
2292 cond
= XEXP (cond
, 0);
2294 /* ??? We could also handle AND here. */
2295 if (GET_CODE (cond
) == ZERO_EXTRACT
)
2297 if (XEXP (cond
, 1) != const1_rtx
2298 || !CONST_INT_P (XEXP (cond
, 2))
2299 || ! rtx_equal_p (x
, XEXP (cond
, 0)))
2301 bitnum
= INTVAL (XEXP (cond
, 2));
2302 mode
= GET_MODE (x
);
2303 if (BITS_BIG_ENDIAN
)
2304 bitnum
= GET_MODE_BITSIZE (mode
) - 1 - bitnum
;
2305 if (bitnum
< 0 || bitnum
>= HOST_BITS_PER_WIDE_INT
)
2312 if (GET_CODE (a
) == IOR
|| GET_CODE (a
) == XOR
)
2314 /* Check for "if (X & C) x = x op C". */
2315 if (! rtx_equal_p (x
, XEXP (a
, 0))
2316 || !CONST_INT_P (XEXP (a
, 1))
2317 || (INTVAL (XEXP (a
, 1)) & GET_MODE_MASK (mode
))
2318 != (unsigned HOST_WIDE_INT
) 1 << bitnum
)
2321 /* if ((x & C) == 0) x |= C; is transformed to x |= C. */
2322 /* if ((x & C) != 0) x |= C; is transformed to nothing. */
2323 if (GET_CODE (a
) == IOR
)
2324 result
= (code
== NE
) ? a
: NULL_RTX
;
2325 else if (code
== NE
)
2327 /* if ((x & C) == 0) x ^= C; is transformed to x |= C. */
2328 result
= gen_int_mode ((HOST_WIDE_INT
) 1 << bitnum
, mode
);
2329 result
= simplify_gen_binary (IOR
, mode
, x
, result
);
2333 /* if ((x & C) != 0) x ^= C; is transformed to x &= ~C. */
2334 result
= gen_int_mode (~((HOST_WIDE_INT
) 1 << bitnum
), mode
);
2335 result
= simplify_gen_binary (AND
, mode
, x
, result
);
2338 else if (GET_CODE (a
) == AND
)
2340 /* Check for "if (X & C) x &= ~C". */
2341 if (! rtx_equal_p (x
, XEXP (a
, 0))
2342 || !CONST_INT_P (XEXP (a
, 1))
2343 || (INTVAL (XEXP (a
, 1)) & GET_MODE_MASK (mode
))
2344 != (~((HOST_WIDE_INT
) 1 << bitnum
) & GET_MODE_MASK (mode
)))
2347 /* if ((x & C) == 0) x &= ~C; is transformed to nothing. */
2348 /* if ((x & C) != 0) x &= ~C; is transformed to x &= ~C. */
2349 result
= (code
== EQ
) ? a
: NULL_RTX
;
2357 noce_emit_move_insn (x
, result
);
2358 seq
= end_ifcvt_sequence (if_info
);
2362 emit_insn_before_setloc (seq
, if_info
->jump
,
2363 INSN_LOCATION (if_info
->insn_a
));
2369 /* Similar to get_condition, only the resulting condition must be
2370 valid at JUMP, instead of at EARLIEST.
2372 If THEN_ELSE_REVERSED is true, the fallthrough does not go to the
2373 THEN block of the caller, and we have to reverse the condition. */
2376 noce_get_condition (rtx_insn
*jump
, rtx_insn
**earliest
, bool then_else_reversed
)
2381 if (! any_condjump_p (jump
))
2384 set
= pc_set (jump
);
2386 /* If this branches to JUMP_LABEL when the condition is false,
2387 reverse the condition. */
2388 reverse
= (GET_CODE (XEXP (SET_SRC (set
), 2)) == LABEL_REF
2389 && LABEL_REF_LABEL (XEXP (SET_SRC (set
), 2)) == JUMP_LABEL (jump
));
2391 /* We may have to reverse because the caller's if block is not canonical,
2392 i.e. the THEN block isn't the fallthrough block for the TEST block
2393 (see find_if_header). */
2394 if (then_else_reversed
)
2397 /* If the condition variable is a register and is MODE_INT, accept it. */
2399 cond
= XEXP (SET_SRC (set
), 0);
2400 tmp
= XEXP (cond
, 0);
2401 if (REG_P (tmp
) && GET_MODE_CLASS (GET_MODE (tmp
)) == MODE_INT
2402 && (GET_MODE (tmp
) != BImode
2403 || !targetm
.small_register_classes_for_mode_p (BImode
)))
2408 cond
= gen_rtx_fmt_ee (reverse_condition (GET_CODE (cond
)),
2409 GET_MODE (cond
), tmp
, XEXP (cond
, 1));
2413 /* Otherwise, fall back on canonicalize_condition to do the dirty
2414 work of manipulating MODE_CC values and COMPARE rtx codes. */
2415 tmp
= canonicalize_condition (jump
, cond
, reverse
, earliest
,
2416 NULL_RTX
, HAVE_cbranchcc4
, true);
2418 /* We don't handle side-effects in the condition, like handling
2419 REG_INC notes and making sure no duplicate conditions are emitted. */
2420 if (tmp
!= NULL_RTX
&& side_effects_p (tmp
))
2426 /* Return true if OP is ok for if-then-else processing. */
2429 noce_operand_ok (const_rtx op
)
2431 if (side_effects_p (op
))
2434 /* We special-case memories, so handle any of them with
2435 no address side effects. */
2437 return ! side_effects_p (XEXP (op
, 0));
2439 return ! may_trap_p (op
);
2442 /* Return true if a write into MEM may trap or fault. */
2445 noce_mem_write_may_trap_or_fault_p (const_rtx mem
)
2449 if (MEM_READONLY_P (mem
))
2452 if (may_trap_or_fault_p (mem
))
2455 addr
= XEXP (mem
, 0);
2457 /* Call target hook to avoid the effects of -fpic etc.... */
2458 addr
= targetm
.delegitimize_address (addr
);
2461 switch (GET_CODE (addr
))
2469 addr
= XEXP (addr
, 0);
2473 addr
= XEXP (addr
, 1);
2476 if (CONST_INT_P (XEXP (addr
, 1)))
2477 addr
= XEXP (addr
, 0);
2484 if (SYMBOL_REF_DECL (addr
)
2485 && decl_readonly_section (SYMBOL_REF_DECL (addr
), 0))
2495 /* Return whether we can use store speculation for MEM. TOP_BB is the
2496 basic block above the conditional block where we are considering
2497 doing the speculative store. We look for whether MEM is set
2498 unconditionally later in the function. */
2501 noce_can_store_speculate_p (basic_block top_bb
, const_rtx mem
)
2503 basic_block dominator
;
2505 for (dominator
= get_immediate_dominator (CDI_POST_DOMINATORS
, top_bb
);
2507 dominator
= get_immediate_dominator (CDI_POST_DOMINATORS
, dominator
))
2511 FOR_BB_INSNS (dominator
, insn
)
2513 /* If we see something that might be a memory barrier, we
2514 have to stop looking. Even if the MEM is set later in
2515 the function, we still don't want to set it
2516 unconditionally before the barrier. */
2518 && (volatile_insn_p (PATTERN (insn
))
2519 || (CALL_P (insn
) && (!RTL_CONST_CALL_P (insn
)))))
2522 if (memory_must_be_modified_in_insn_p (mem
, insn
))
2524 if (modified_in_p (XEXP (mem
, 0), insn
))
2533 /* Given a simple IF-THEN-JOIN or IF-THEN-ELSE-JOIN block, attempt to convert
2534 it without using conditional execution. Return TRUE if we were successful
2535 at converting the block. */
2538 noce_process_if_block (struct noce_if_info
*if_info
)
2540 basic_block test_bb
= if_info
->test_bb
; /* test block */
2541 basic_block then_bb
= if_info
->then_bb
; /* THEN */
2542 basic_block else_bb
= if_info
->else_bb
; /* ELSE or NULL */
2543 basic_block join_bb
= if_info
->join_bb
; /* JOIN */
2544 rtx_insn
*jump
= if_info
->jump
;
2545 rtx cond
= if_info
->cond
;
2546 rtx_insn
*insn_a
, *insn_b
;
2548 rtx orig_x
, x
, a
, b
;
2551 /* We're looking for patterns of the form
2553 (1) if (...) x = a; else x = b;
2554 (2) x = b; if (...) x = a;
2555 (3) if (...) x = a; // as if with an initial x = x.
2557 The later patterns require jumps to be more expensive.
2559 ??? For future expansion, look for multiple X in such patterns. */
2561 /* Look for one of the potential sets. */
2562 insn_a
= first_active_insn (then_bb
);
2564 || insn_a
!= last_active_insn (then_bb
, FALSE
)
2565 || (set_a
= single_set (insn_a
)) == NULL_RTX
)
2568 x
= SET_DEST (set_a
);
2569 a
= SET_SRC (set_a
);
2571 /* Look for the other potential set. Make sure we've got equivalent
2573 /* ??? This is overconservative. Storing to two different mems is
2574 as easy as conditionally computing the address. Storing to a
2575 single mem merely requires a scratch memory to use as one of the
2576 destination addresses; often the memory immediately below the
2577 stack pointer is available for this. */
2581 insn_b
= first_active_insn (else_bb
);
2583 || insn_b
!= last_active_insn (else_bb
, FALSE
)
2584 || (set_b
= single_set (insn_b
)) == NULL_RTX
2585 || ! rtx_interchangeable_p (x
, SET_DEST (set_b
)))
2590 insn_b
= prev_nonnote_nondebug_insn (if_info
->cond_earliest
);
2591 /* We're going to be moving the evaluation of B down from above
2592 COND_EARLIEST to JUMP. Make sure the relevant data is still
2595 || BLOCK_FOR_INSN (insn_b
) != BLOCK_FOR_INSN (if_info
->cond_earliest
)
2596 || !NONJUMP_INSN_P (insn_b
)
2597 || (set_b
= single_set (insn_b
)) == NULL_RTX
2598 || ! rtx_interchangeable_p (x
, SET_DEST (set_b
))
2599 || ! noce_operand_ok (SET_SRC (set_b
))
2600 || reg_overlap_mentioned_p (x
, SET_SRC (set_b
))
2601 || modified_between_p (SET_SRC (set_b
), insn_b
, jump
)
2602 /* Avoid extending the lifetime of hard registers on small
2603 register class machines. */
2604 || (REG_P (SET_SRC (set_b
))
2605 && HARD_REGISTER_P (SET_SRC (set_b
))
2606 && targetm
.small_register_classes_for_mode_p
2607 (GET_MODE (SET_SRC (set_b
))))
2608 /* Likewise with X. In particular this can happen when
2609 noce_get_condition looks farther back in the instruction
2610 stream than one might expect. */
2611 || reg_overlap_mentioned_p (x
, cond
)
2612 || reg_overlap_mentioned_p (x
, a
)
2613 || modified_between_p (x
, insn_b
, jump
))
2620 /* If x has side effects then only the if-then-else form is safe to
2621 convert. But even in that case we would need to restore any notes
2622 (such as REG_INC) at then end. That can be tricky if
2623 noce_emit_move_insn expands to more than one insn, so disable the
2624 optimization entirely for now if there are side effects. */
2625 if (side_effects_p (x
))
2628 b
= (set_b
? SET_SRC (set_b
) : x
);
2630 /* Only operate on register destinations, and even then avoid extending
2631 the lifetime of hard registers on small register class machines. */
2634 || (HARD_REGISTER_P (x
)
2635 && targetm
.small_register_classes_for_mode_p (GET_MODE (x
))))
2637 if (GET_MODE (x
) == BLKmode
)
2640 if (GET_CODE (x
) == ZERO_EXTRACT
2641 && (!CONST_INT_P (XEXP (x
, 1))
2642 || !CONST_INT_P (XEXP (x
, 2))))
2645 x
= gen_reg_rtx (GET_MODE (GET_CODE (x
) == STRICT_LOW_PART
2646 ? XEXP (x
, 0) : x
));
2649 /* Don't operate on sources that may trap or are volatile. */
2650 if (! noce_operand_ok (a
) || ! noce_operand_ok (b
))
2654 /* Set up the info block for our subroutines. */
2655 if_info
->insn_a
= insn_a
;
2656 if_info
->insn_b
= insn_b
;
2661 /* Skip it if the instruction to be moved might clobber CC. */
2662 cc
= cc_in_cond (cond
);
2664 && (set_of (cc
, insn_a
)
2665 || (insn_b
&& set_of (cc
, insn_b
))))
2668 /* Try optimizations in some approximation of a useful order. */
2669 /* ??? Should first look to see if X is live incoming at all. If it
2670 isn't, we don't need anything but an unconditional set. */
2672 /* Look and see if A and B are really the same. Avoid creating silly
2673 cmove constructs that no one will fix up later. */
2674 if (rtx_interchangeable_p (a
, b
))
2676 /* If we have an INSN_B, we don't have to create any new rtl. Just
2677 move the instruction that we already have. If we don't have an
2678 INSN_B, that means that A == X, and we've got a noop move. In
2679 that case don't do anything and let the code below delete INSN_A. */
2680 if (insn_b
&& else_bb
)
2684 if (else_bb
&& insn_b
== BB_END (else_bb
))
2685 BB_END (else_bb
) = PREV_INSN (insn_b
);
2686 reorder_insns (insn_b
, insn_b
, PREV_INSN (jump
));
2688 /* If there was a REG_EQUAL note, delete it since it may have been
2689 true due to this insn being after a jump. */
2690 if ((note
= find_reg_note (insn_b
, REG_EQUAL
, NULL_RTX
)) != 0)
2691 remove_note (insn_b
, note
);
2695 /* If we have "x = b; if (...) x = a;", and x has side-effects, then
2696 x must be executed twice. */
2697 else if (insn_b
&& side_effects_p (orig_x
))
2704 if (!set_b
&& MEM_P (orig_x
))
2706 /* Disallow the "if (...) x = a;" form (implicit "else x = x;")
2707 for optimizations if writing to x may trap or fault,
2708 i.e. it's a memory other than a static var or a stack slot,
2709 is misaligned on strict aligned machines or is read-only. If
2710 x is a read-only memory, then the program is valid only if we
2711 avoid the store into it. If there are stores on both the
2712 THEN and ELSE arms, then we can go ahead with the conversion;
2713 either the program is broken, or the condition is always
2714 false such that the other memory is selected. */
2715 if (noce_mem_write_may_trap_or_fault_p (orig_x
))
2718 /* Avoid store speculation: given "if (...) x = a" where x is a
2719 MEM, we only want to do the store if x is always set
2720 somewhere in the function. This avoids cases like
2721 if (pthread_mutex_trylock(mutex))
2723 where we only want global_variable to be changed if the mutex
2724 is held. FIXME: This should ideally be expressed directly in
2726 if (!noce_can_store_speculate_p (test_bb
, orig_x
))
2730 if (noce_try_move (if_info
))
2732 if (noce_try_store_flag (if_info
))
2734 if (noce_try_bitop (if_info
))
2736 if (noce_try_minmax (if_info
))
2738 if (noce_try_abs (if_info
))
2740 if (HAVE_conditional_move
2741 && noce_try_cmove (if_info
))
2743 if (! targetm
.have_conditional_execution ())
2745 if (noce_try_store_flag_constants (if_info
))
2747 if (noce_try_addcc (if_info
))
2749 if (noce_try_store_flag_mask (if_info
))
2751 if (HAVE_conditional_move
2752 && noce_try_cmove_arith (if_info
))
2754 if (noce_try_sign_mask (if_info
))
2758 if (!else_bb
&& set_b
)
2770 /* If we used a temporary, fix it up now. */
2776 noce_emit_move_insn (orig_x
, x
);
2778 set_used_flags (orig_x
);
2779 unshare_all_rtl_in_chain (seq
);
2782 emit_insn_before_setloc (seq
, BB_END (test_bb
), INSN_LOCATION (insn_a
));
2785 /* The original THEN and ELSE blocks may now be removed. The test block
2786 must now jump to the join block. If the test block and the join block
2787 can be merged, do so. */
2790 delete_basic_block (else_bb
);
2794 remove_edge (find_edge (test_bb
, join_bb
));
2796 remove_edge (find_edge (then_bb
, join_bb
));
2797 redirect_edge_and_branch_force (single_succ_edge (test_bb
), join_bb
);
2798 delete_basic_block (then_bb
);
2801 if (can_merge_blocks_p (test_bb
, join_bb
))
2803 merge_blocks (test_bb
, join_bb
);
2807 num_updated_if_blocks
++;
2811 /* Check whether a block is suitable for conditional move conversion.
2812 Every insn must be a simple set of a register to a constant or a
2813 register. For each assignment, store the value in the pointer map
2814 VALS, keyed indexed by register pointer, then store the register
2815 pointer in REGS. COND is the condition we will test. */
2818 check_cond_move_block (basic_block bb
,
2819 hash_map
<rtx
, rtx
> *vals
,
2824 rtx cc
= cc_in_cond (cond
);
2826 /* We can only handle simple jumps at the end of the basic block.
2827 It is almost impossible to update the CFG otherwise. */
2829 if (JUMP_P (insn
) && !onlyjump_p (insn
))
2832 FOR_BB_INSNS (bb
, insn
)
2836 if (!NONDEBUG_INSN_P (insn
) || JUMP_P (insn
))
2838 set
= single_set (insn
);
2842 dest
= SET_DEST (set
);
2843 src
= SET_SRC (set
);
2845 || (HARD_REGISTER_P (dest
)
2846 && targetm
.small_register_classes_for_mode_p (GET_MODE (dest
))))
2849 if (!CONSTANT_P (src
) && !register_operand (src
, VOIDmode
))
2852 if (side_effects_p (src
) || side_effects_p (dest
))
2855 if (may_trap_p (src
) || may_trap_p (dest
))
2858 /* Don't try to handle this if the source register was
2859 modified earlier in the block. */
2862 || (GET_CODE (src
) == SUBREG
&& REG_P (SUBREG_REG (src
))
2863 && vals
->get (SUBREG_REG (src
))))
2866 /* Don't try to handle this if the destination register was
2867 modified earlier in the block. */
2868 if (vals
->get (dest
))
2871 /* Don't try to handle this if the condition uses the
2872 destination register. */
2873 if (reg_overlap_mentioned_p (dest
, cond
))
2876 /* Don't try to handle this if the source register is modified
2877 later in the block. */
2878 if (!CONSTANT_P (src
)
2879 && modified_between_p (src
, insn
, NEXT_INSN (BB_END (bb
))))
2882 /* Skip it if the instruction to be moved might clobber CC. */
2883 if (cc
&& set_of (cc
, insn
))
2886 vals
->put (dest
, src
);
2888 regs
->safe_push (dest
);
2894 /* Given a basic block BB suitable for conditional move conversion,
2895 a condition COND, and pointer maps THEN_VALS and ELSE_VALS containing
2896 the register values depending on COND, emit the insns in the block as
2897 conditional moves. If ELSE_BLOCK is true, THEN_BB was already
2898 processed. The caller has started a sequence for the conversion.
2899 Return true if successful, false if something goes wrong. */
2902 cond_move_convert_if_block (struct noce_if_info
*if_infop
,
2903 basic_block bb
, rtx cond
,
2904 hash_map
<rtx
, rtx
> *then_vals
,
2905 hash_map
<rtx
, rtx
> *else_vals
,
2910 rtx cond_arg0
, cond_arg1
;
2912 code
= GET_CODE (cond
);
2913 cond_arg0
= XEXP (cond
, 0);
2914 cond_arg1
= XEXP (cond
, 1);
2916 FOR_BB_INSNS (bb
, insn
)
2918 rtx set
, target
, dest
, t
, e
;
2920 /* ??? Maybe emit conditional debug insn? */
2921 if (!NONDEBUG_INSN_P (insn
) || JUMP_P (insn
))
2923 set
= single_set (insn
);
2924 gcc_assert (set
&& REG_P (SET_DEST (set
)));
2926 dest
= SET_DEST (set
);
2928 rtx
*then_slot
= then_vals
->get (dest
);
2929 rtx
*else_slot
= else_vals
->get (dest
);
2930 t
= then_slot
? *then_slot
: NULL_RTX
;
2931 e
= else_slot
? *else_slot
: NULL_RTX
;
2935 /* If this register was set in the then block, we already
2936 handled this case there. */
2949 target
= noce_emit_cmove (if_infop
, dest
, code
, cond_arg0
, cond_arg1
,
2955 noce_emit_move_insn (dest
, target
);
2961 /* Given a simple IF-THEN-JOIN or IF-THEN-ELSE-JOIN block, attempt to convert
2962 it using only conditional moves. Return TRUE if we were successful at
2963 converting the block. */
2966 cond_move_process_if_block (struct noce_if_info
*if_info
)
2968 basic_block test_bb
= if_info
->test_bb
;
2969 basic_block then_bb
= if_info
->then_bb
;
2970 basic_block else_bb
= if_info
->else_bb
;
2971 basic_block join_bb
= if_info
->join_bb
;
2972 rtx_insn
*jump
= if_info
->jump
;
2973 rtx cond
= if_info
->cond
;
2974 rtx_insn
*seq
, *loc_insn
;
2977 vec
<rtx
> then_regs
= vNULL
;
2978 vec
<rtx
> else_regs
= vNULL
;
2980 int success_p
= FALSE
;
2982 /* Build a mapping for each block to the value used for each
2984 hash_map
<rtx
, rtx
> then_vals
;
2985 hash_map
<rtx
, rtx
> else_vals
;
2987 /* Make sure the blocks are suitable. */
2988 if (!check_cond_move_block (then_bb
, &then_vals
, &then_regs
, cond
)
2990 && !check_cond_move_block (else_bb
, &else_vals
, &else_regs
, cond
)))
2993 /* Make sure the blocks can be used together. If the same register
2994 is set in both blocks, and is not set to a constant in both
2995 cases, then both blocks must set it to the same register. We
2996 have already verified that if it is set to a register, that the
2997 source register does not change after the assignment. Also count
2998 the number of registers set in only one of the blocks. */
3000 FOR_EACH_VEC_ELT (then_regs
, i
, reg
)
3002 rtx
*then_slot
= then_vals
.get (reg
);
3003 rtx
*else_slot
= else_vals
.get (reg
);
3005 gcc_checking_assert (then_slot
);
3010 rtx then_val
= *then_slot
;
3011 rtx else_val
= *else_slot
;
3012 if (!CONSTANT_P (then_val
) && !CONSTANT_P (else_val
)
3013 && !rtx_equal_p (then_val
, else_val
))
3018 /* Finish off c for MAX_CONDITIONAL_EXECUTE. */
3019 FOR_EACH_VEC_ELT (else_regs
, i
, reg
)
3021 gcc_checking_assert (else_vals
.get (reg
));
3022 if (!then_vals
.get (reg
))
3026 /* Make sure it is reasonable to convert this block. What matters
3027 is the number of assignments currently made in only one of the
3028 branches, since if we convert we are going to always execute
3030 if (c
> MAX_CONDITIONAL_EXECUTE
)
3033 /* Try to emit the conditional moves. First do the then block,
3034 then do anything left in the else blocks. */
3036 if (!cond_move_convert_if_block (if_info
, then_bb
, cond
,
3037 &then_vals
, &else_vals
, false)
3039 && !cond_move_convert_if_block (if_info
, else_bb
, cond
,
3040 &then_vals
, &else_vals
, true)))
3045 seq
= end_ifcvt_sequence (if_info
);
3049 loc_insn
= first_active_insn (then_bb
);
3052 loc_insn
= first_active_insn (else_bb
);
3053 gcc_assert (loc_insn
);
3055 emit_insn_before_setloc (seq
, jump
, INSN_LOCATION (loc_insn
));
3059 delete_basic_block (else_bb
);
3063 remove_edge (find_edge (test_bb
, join_bb
));
3065 remove_edge (find_edge (then_bb
, join_bb
));
3066 redirect_edge_and_branch_force (single_succ_edge (test_bb
), join_bb
);
3067 delete_basic_block (then_bb
);
3070 if (can_merge_blocks_p (test_bb
, join_bb
))
3072 merge_blocks (test_bb
, join_bb
);
3076 num_updated_if_blocks
++;
3081 then_regs
.release ();
3082 else_regs
.release ();
3087 /* Determine if a given basic block heads a simple IF-THEN-JOIN or an
3088 IF-THEN-ELSE-JOIN block.
3090 If so, we'll try to convert the insns to not require the branch,
3091 using only transformations that do not require conditional execution.
3093 Return TRUE if we were successful at converting the block. */
3096 noce_find_if_block (basic_block test_bb
, edge then_edge
, edge else_edge
,
3099 basic_block then_bb
, else_bb
, join_bb
;
3100 bool then_else_reversed
= false;
3103 rtx_insn
*cond_earliest
;
3104 struct noce_if_info if_info
;
3106 /* We only ever should get here before reload. */
3107 gcc_assert (!reload_completed
);
3109 /* Recognize an IF-THEN-ELSE-JOIN block. */
3110 if (single_pred_p (then_edge
->dest
)
3111 && single_succ_p (then_edge
->dest
)
3112 && single_pred_p (else_edge
->dest
)
3113 && single_succ_p (else_edge
->dest
)
3114 && single_succ (then_edge
->dest
) == single_succ (else_edge
->dest
))
3116 then_bb
= then_edge
->dest
;
3117 else_bb
= else_edge
->dest
;
3118 join_bb
= single_succ (then_bb
);
3120 /* Recognize an IF-THEN-JOIN block. */
3121 else if (single_pred_p (then_edge
->dest
)
3122 && single_succ_p (then_edge
->dest
)
3123 && single_succ (then_edge
->dest
) == else_edge
->dest
)
3125 then_bb
= then_edge
->dest
;
3126 else_bb
= NULL_BLOCK
;
3127 join_bb
= else_edge
->dest
;
3129 /* Recognize an IF-ELSE-JOIN block. We can have those because the order
3130 of basic blocks in cfglayout mode does not matter, so the fallthrough
3131 edge can go to any basic block (and not just to bb->next_bb, like in
3133 else if (single_pred_p (else_edge
->dest
)
3134 && single_succ_p (else_edge
->dest
)
3135 && single_succ (else_edge
->dest
) == then_edge
->dest
)
3137 /* The noce transformations do not apply to IF-ELSE-JOIN blocks.
3138 To make this work, we have to invert the THEN and ELSE blocks
3139 and reverse the jump condition. */
3140 then_bb
= else_edge
->dest
;
3141 else_bb
= NULL_BLOCK
;
3142 join_bb
= single_succ (then_bb
);
3143 then_else_reversed
= true;
3146 /* Not a form we can handle. */
3149 /* The edges of the THEN and ELSE blocks cannot have complex edges. */
3150 if (single_succ_edge (then_bb
)->flags
& EDGE_COMPLEX
)
3153 && single_succ_edge (else_bb
)->flags
& EDGE_COMPLEX
)
3156 num_possible_if_blocks
++;
3161 "\nIF-THEN%s-JOIN block found, pass %d, test %d, then %d",
3162 (else_bb
) ? "-ELSE" : "",
3163 pass
, test_bb
->index
, then_bb
->index
);
3166 fprintf (dump_file
, ", else %d", else_bb
->index
);
3168 fprintf (dump_file
, ", join %d\n", join_bb
->index
);
3171 /* If the conditional jump is more than just a conditional
3172 jump, then we can not do if-conversion on this block. */
3173 jump
= BB_END (test_bb
);
3174 if (! onlyjump_p (jump
))
3177 /* If this is not a standard conditional jump, we can't parse it. */
3178 cond
= noce_get_condition (jump
, &cond_earliest
, then_else_reversed
);
3182 /* We must be comparing objects whose modes imply the size. */
3183 if (GET_MODE (XEXP (cond
, 0)) == BLKmode
)
3186 /* Initialize an IF_INFO struct to pass around. */
3187 memset (&if_info
, 0, sizeof if_info
);
3188 if_info
.test_bb
= test_bb
;
3189 if_info
.then_bb
= then_bb
;
3190 if_info
.else_bb
= else_bb
;
3191 if_info
.join_bb
= join_bb
;
3192 if_info
.cond
= cond
;
3193 if_info
.cond_earliest
= cond_earliest
;
3194 if_info
.jump
= jump
;
3195 if_info
.then_else_reversed
= then_else_reversed
;
3196 if_info
.branch_cost
= BRANCH_COST (optimize_bb_for_speed_p (test_bb
),
3197 predictable_edge_p (then_edge
));
3199 /* Do the real work. */
3201 if (noce_process_if_block (&if_info
))
3204 if (HAVE_conditional_move
3205 && cond_move_process_if_block (&if_info
))
3212 /* Merge the blocks and mark for local life update. */
3215 merge_if_block (struct ce_if_block
* ce_info
)
3217 basic_block test_bb
= ce_info
->test_bb
; /* last test block */
3218 basic_block then_bb
= ce_info
->then_bb
; /* THEN */
3219 basic_block else_bb
= ce_info
->else_bb
; /* ELSE or NULL */
3220 basic_block join_bb
= ce_info
->join_bb
; /* join block */
3221 basic_block combo_bb
;
3223 /* All block merging is done into the lower block numbers. */
3226 df_set_bb_dirty (test_bb
);
3228 /* Merge any basic blocks to handle && and || subtests. Each of
3229 the blocks are on the fallthru path from the predecessor block. */
3230 if (ce_info
->num_multiple_test_blocks
> 0)
3232 basic_block bb
= test_bb
;
3233 basic_block last_test_bb
= ce_info
->last_test_bb
;
3234 basic_block fallthru
= block_fallthru (bb
);
3239 fallthru
= block_fallthru (bb
);
3240 merge_blocks (combo_bb
, bb
);
3243 while (bb
!= last_test_bb
);
3246 /* Merge TEST block into THEN block. Normally the THEN block won't have a
3247 label, but it might if there were || tests. That label's count should be
3248 zero, and it normally should be removed. */
3252 /* If THEN_BB has no successors, then there's a BARRIER after it.
3253 If COMBO_BB has more than one successor (THEN_BB), then that BARRIER
3254 is no longer needed, and in fact it is incorrect to leave it in
3256 if (EDGE_COUNT (then_bb
->succs
) == 0
3257 && EDGE_COUNT (combo_bb
->succs
) > 1)
3259 rtx_insn
*end
= NEXT_INSN (BB_END (then_bb
));
3260 while (end
&& NOTE_P (end
) && !NOTE_INSN_BASIC_BLOCK_P (end
))
3261 end
= NEXT_INSN (end
);
3263 if (end
&& BARRIER_P (end
))
3266 merge_blocks (combo_bb
, then_bb
);
3270 /* The ELSE block, if it existed, had a label. That label count
3271 will almost always be zero, but odd things can happen when labels
3272 get their addresses taken. */
3275 /* If ELSE_BB has no successors, then there's a BARRIER after it.
3276 If COMBO_BB has more than one successor (ELSE_BB), then that BARRIER
3277 is no longer needed, and in fact it is incorrect to leave it in
3279 if (EDGE_COUNT (else_bb
->succs
) == 0
3280 && EDGE_COUNT (combo_bb
->succs
) > 1)
3282 rtx_insn
*end
= NEXT_INSN (BB_END (else_bb
));
3283 while (end
&& NOTE_P (end
) && !NOTE_INSN_BASIC_BLOCK_P (end
))
3284 end
= NEXT_INSN (end
);
3286 if (end
&& BARRIER_P (end
))
3289 merge_blocks (combo_bb
, else_bb
);
3293 /* If there was no join block reported, that means it was not adjacent
3294 to the others, and so we cannot merge them. */
3298 rtx_insn
*last
= BB_END (combo_bb
);
3300 /* The outgoing edge for the current COMBO block should already
3301 be correct. Verify this. */
3302 if (EDGE_COUNT (combo_bb
->succs
) == 0)
3303 gcc_assert (find_reg_note (last
, REG_NORETURN
, NULL
)
3304 || (NONJUMP_INSN_P (last
)
3305 && GET_CODE (PATTERN (last
)) == TRAP_IF
3306 && (TRAP_CONDITION (PATTERN (last
))
3307 == const_true_rtx
)));
3310 /* There should still be something at the end of the THEN or ELSE
3311 blocks taking us to our final destination. */
3312 gcc_assert (JUMP_P (last
)
3313 || (EDGE_SUCC (combo_bb
, 0)->dest
3314 == EXIT_BLOCK_PTR_FOR_FN (cfun
)
3316 && SIBLING_CALL_P (last
))
3317 || ((EDGE_SUCC (combo_bb
, 0)->flags
& EDGE_EH
)
3318 && can_throw_internal (last
)));
3321 /* The JOIN block may have had quite a number of other predecessors too.
3322 Since we've already merged the TEST, THEN and ELSE blocks, we should
3323 have only one remaining edge from our if-then-else diamond. If there
3324 is more than one remaining edge, it must come from elsewhere. There
3325 may be zero incoming edges if the THEN block didn't actually join
3326 back up (as with a call to a non-return function). */
3327 else if (EDGE_COUNT (join_bb
->preds
) < 2
3328 && join_bb
!= EXIT_BLOCK_PTR_FOR_FN (cfun
))
3330 /* We can merge the JOIN cleanly and update the dataflow try
3331 again on this pass.*/
3332 merge_blocks (combo_bb
, join_bb
);
3337 /* We cannot merge the JOIN. */
3339 /* The outgoing edge for the current COMBO block should already
3340 be correct. Verify this. */
3341 gcc_assert (single_succ_p (combo_bb
)
3342 && single_succ (combo_bb
) == join_bb
);
3344 /* Remove the jump and cruft from the end of the COMBO block. */
3345 if (join_bb
!= EXIT_BLOCK_PTR_FOR_FN (cfun
))
3346 tidy_fallthru_edge (single_succ_edge (combo_bb
));
3349 num_updated_if_blocks
++;
3352 /* Find a block ending in a simple IF condition and try to transform it
3353 in some way. When converting a multi-block condition, put the new code
3354 in the first such block and delete the rest. Return a pointer to this
3355 first block if some transformation was done. Return NULL otherwise. */
3358 find_if_header (basic_block test_bb
, int pass
)
3360 ce_if_block ce_info
;
3364 /* The kind of block we're looking for has exactly two successors. */
3365 if (EDGE_COUNT (test_bb
->succs
) != 2)
3368 then_edge
= EDGE_SUCC (test_bb
, 0);
3369 else_edge
= EDGE_SUCC (test_bb
, 1);
3371 if (df_get_bb_dirty (then_edge
->dest
))
3373 if (df_get_bb_dirty (else_edge
->dest
))
3376 /* Neither edge should be abnormal. */
3377 if ((then_edge
->flags
& EDGE_COMPLEX
)
3378 || (else_edge
->flags
& EDGE_COMPLEX
))
3381 /* Nor exit the loop. */
3382 if ((then_edge
->flags
& EDGE_LOOP_EXIT
)
3383 || (else_edge
->flags
& EDGE_LOOP_EXIT
))
3386 /* The THEN edge is canonically the one that falls through. */
3387 if (then_edge
->flags
& EDGE_FALLTHRU
)
3389 else if (else_edge
->flags
& EDGE_FALLTHRU
)
3390 std::swap (then_edge
, else_edge
);
3392 /* Otherwise this must be a multiway branch of some sort. */
3395 memset (&ce_info
, 0, sizeof (ce_info
));
3396 ce_info
.test_bb
= test_bb
;
3397 ce_info
.then_bb
= then_edge
->dest
;
3398 ce_info
.else_bb
= else_edge
->dest
;
3399 ce_info
.pass
= pass
;
3401 #ifdef IFCVT_MACHDEP_INIT
3402 IFCVT_MACHDEP_INIT (&ce_info
);
3405 if (!reload_completed
3406 && noce_find_if_block (test_bb
, then_edge
, else_edge
, pass
))
3409 if (reload_completed
3410 && targetm
.have_conditional_execution ()
3411 && cond_exec_find_if_block (&ce_info
))
3414 if (targetm
.have_trap ()
3415 && optab_handler (ctrap_optab
, word_mode
) != CODE_FOR_nothing
3416 && find_cond_trap (test_bb
, then_edge
, else_edge
))
3419 if (dom_info_state (CDI_POST_DOMINATORS
) >= DOM_NO_FAST_QUERY
3420 && (reload_completed
|| !targetm
.have_conditional_execution ()))
3422 if (find_if_case_1 (test_bb
, then_edge
, else_edge
))
3424 if (find_if_case_2 (test_bb
, then_edge
, else_edge
))
3432 fprintf (dump_file
, "Conversion succeeded on pass %d.\n", pass
);
3433 /* Set this so we continue looking. */
3434 cond_exec_changed_p
= TRUE
;
3435 return ce_info
.test_bb
;
3438 /* Return true if a block has two edges, one of which falls through to the next
3439 block, and the other jumps to a specific block, so that we can tell if the
3440 block is part of an && test or an || test. Returns either -1 or the number
3441 of non-note, non-jump, non-USE/CLOBBER insns in the block. */
3444 block_jumps_and_fallthru_p (basic_block cur_bb
, basic_block target_bb
)
3447 int fallthru_p
= FALSE
;
3454 if (!cur_bb
|| !target_bb
)
3457 /* If no edges, obviously it doesn't jump or fallthru. */
3458 if (EDGE_COUNT (cur_bb
->succs
) == 0)
3461 FOR_EACH_EDGE (cur_edge
, ei
, cur_bb
->succs
)
3463 if (cur_edge
->flags
& EDGE_COMPLEX
)
3464 /* Anything complex isn't what we want. */
3467 else if (cur_edge
->flags
& EDGE_FALLTHRU
)
3470 else if (cur_edge
->dest
== target_bb
)
3477 if ((jump_p
& fallthru_p
) == 0)
3480 /* Don't allow calls in the block, since this is used to group && and ||
3481 together for conditional execution support. ??? we should support
3482 conditional execution support across calls for IA-64 some day, but
3483 for now it makes the code simpler. */
3484 end
= BB_END (cur_bb
);
3485 insn
= BB_HEAD (cur_bb
);
3487 while (insn
!= NULL_RTX
)
3494 && !DEBUG_INSN_P (insn
)
3495 && GET_CODE (PATTERN (insn
)) != USE
3496 && GET_CODE (PATTERN (insn
)) != CLOBBER
)
3502 insn
= NEXT_INSN (insn
);
3508 /* Determine if a given basic block heads a simple IF-THEN or IF-THEN-ELSE
3509 block. If so, we'll try to convert the insns to not require the branch.
3510 Return TRUE if we were successful at converting the block. */
3513 cond_exec_find_if_block (struct ce_if_block
* ce_info
)
3515 basic_block test_bb
= ce_info
->test_bb
;
3516 basic_block then_bb
= ce_info
->then_bb
;
3517 basic_block else_bb
= ce_info
->else_bb
;
3518 basic_block join_bb
= NULL_BLOCK
;
3523 ce_info
->last_test_bb
= test_bb
;
3525 /* We only ever should get here after reload,
3526 and if we have conditional execution. */
3527 gcc_assert (reload_completed
&& targetm
.have_conditional_execution ());
3529 /* Discover if any fall through predecessors of the current test basic block
3530 were && tests (which jump to the else block) or || tests (which jump to
3532 if (single_pred_p (test_bb
)
3533 && single_pred_edge (test_bb
)->flags
== EDGE_FALLTHRU
)
3535 basic_block bb
= single_pred (test_bb
);
3536 basic_block target_bb
;
3537 int max_insns
= MAX_CONDITIONAL_EXECUTE
;
3540 /* Determine if the preceding block is an && or || block. */
3541 if ((n_insns
= block_jumps_and_fallthru_p (bb
, else_bb
)) >= 0)
3543 ce_info
->and_and_p
= TRUE
;
3544 target_bb
= else_bb
;
3546 else if ((n_insns
= block_jumps_and_fallthru_p (bb
, then_bb
)) >= 0)
3548 ce_info
->and_and_p
= FALSE
;
3549 target_bb
= then_bb
;
3552 target_bb
= NULL_BLOCK
;
3554 if (target_bb
&& n_insns
<= max_insns
)
3556 int total_insns
= 0;
3559 ce_info
->last_test_bb
= test_bb
;
3561 /* Found at least one && or || block, look for more. */
3564 ce_info
->test_bb
= test_bb
= bb
;
3565 total_insns
+= n_insns
;
3568 if (!single_pred_p (bb
))
3571 bb
= single_pred (bb
);
3572 n_insns
= block_jumps_and_fallthru_p (bb
, target_bb
);
3574 while (n_insns
>= 0 && (total_insns
+ n_insns
) <= max_insns
);
3576 ce_info
->num_multiple_test_blocks
= blocks
;
3577 ce_info
->num_multiple_test_insns
= total_insns
;
3579 if (ce_info
->and_and_p
)
3580 ce_info
->num_and_and_blocks
= blocks
;
3582 ce_info
->num_or_or_blocks
= blocks
;
3586 /* The THEN block of an IF-THEN combo must have exactly one predecessor,
3587 other than any || blocks which jump to the THEN block. */
3588 if ((EDGE_COUNT (then_bb
->preds
) - ce_info
->num_or_or_blocks
) != 1)
3591 /* The edges of the THEN and ELSE blocks cannot have complex edges. */
3592 FOR_EACH_EDGE (cur_edge
, ei
, then_bb
->preds
)
3594 if (cur_edge
->flags
& EDGE_COMPLEX
)
3598 FOR_EACH_EDGE (cur_edge
, ei
, else_bb
->preds
)
3600 if (cur_edge
->flags
& EDGE_COMPLEX
)
3604 /* The THEN block of an IF-THEN combo must have zero or one successors. */
3605 if (EDGE_COUNT (then_bb
->succs
) > 0
3606 && (!single_succ_p (then_bb
)
3607 || (single_succ_edge (then_bb
)->flags
& EDGE_COMPLEX
)
3608 || (epilogue_completed
3609 && tablejump_p (BB_END (then_bb
), NULL
, NULL
))))
3612 /* If the THEN block has no successors, conditional execution can still
3613 make a conditional call. Don't do this unless the ELSE block has
3614 only one incoming edge -- the CFG manipulation is too ugly otherwise.
3615 Check for the last insn of the THEN block being an indirect jump, which
3616 is listed as not having any successors, but confuses the rest of the CE
3617 code processing. ??? we should fix this in the future. */
3618 if (EDGE_COUNT (then_bb
->succs
) == 0)
3620 if (single_pred_p (else_bb
) && else_bb
!= EXIT_BLOCK_PTR_FOR_FN (cfun
))
3622 rtx_insn
*last_insn
= BB_END (then_bb
);
3625 && NOTE_P (last_insn
)
3626 && last_insn
!= BB_HEAD (then_bb
))
3627 last_insn
= PREV_INSN (last_insn
);
3630 && JUMP_P (last_insn
)
3631 && ! simplejump_p (last_insn
))
3635 else_bb
= NULL_BLOCK
;
3641 /* If the THEN block's successor is the other edge out of the TEST block,
3642 then we have an IF-THEN combo without an ELSE. */
3643 else if (single_succ (then_bb
) == else_bb
)
3646 else_bb
= NULL_BLOCK
;
3649 /* If the THEN and ELSE block meet in a subsequent block, and the ELSE
3650 has exactly one predecessor and one successor, and the outgoing edge
3651 is not complex, then we have an IF-THEN-ELSE combo. */
3652 else if (single_succ_p (else_bb
)
3653 && single_succ (then_bb
) == single_succ (else_bb
)
3654 && single_pred_p (else_bb
)
3655 && !(single_succ_edge (else_bb
)->flags
& EDGE_COMPLEX
)
3656 && !(epilogue_completed
3657 && tablejump_p (BB_END (else_bb
), NULL
, NULL
)))
3658 join_bb
= single_succ (else_bb
);
3660 /* Otherwise it is not an IF-THEN or IF-THEN-ELSE combination. */
3664 num_possible_if_blocks
++;
3669 "\nIF-THEN%s block found, pass %d, start block %d "
3670 "[insn %d], then %d [%d]",
3671 (else_bb
) ? "-ELSE" : "",
3674 BB_HEAD (test_bb
) ? (int)INSN_UID (BB_HEAD (test_bb
)) : -1,
3676 BB_HEAD (then_bb
) ? (int)INSN_UID (BB_HEAD (then_bb
)) : -1);
3679 fprintf (dump_file
, ", else %d [%d]",
3681 BB_HEAD (else_bb
) ? (int)INSN_UID (BB_HEAD (else_bb
)) : -1);
3683 fprintf (dump_file
, ", join %d [%d]",
3685 BB_HEAD (join_bb
) ? (int)INSN_UID (BB_HEAD (join_bb
)) : -1);
3687 if (ce_info
->num_multiple_test_blocks
> 0)
3688 fprintf (dump_file
, ", %d %s block%s last test %d [%d]",
3689 ce_info
->num_multiple_test_blocks
,
3690 (ce_info
->and_and_p
) ? "&&" : "||",
3691 (ce_info
->num_multiple_test_blocks
== 1) ? "" : "s",
3692 ce_info
->last_test_bb
->index
,
3693 ((BB_HEAD (ce_info
->last_test_bb
))
3694 ? (int)INSN_UID (BB_HEAD (ce_info
->last_test_bb
))
3697 fputc ('\n', dump_file
);
3700 /* Make sure IF, THEN, and ELSE, blocks are adjacent. Actually, we get the
3701 first condition for free, since we've already asserted that there's a
3702 fallthru edge from IF to THEN. Likewise for the && and || blocks, since
3703 we checked the FALLTHRU flag, those are already adjacent to the last IF
3705 /* ??? As an enhancement, move the ELSE block. Have to deal with
3706 BLOCK notes, if by no other means than backing out the merge if they
3707 exist. Sticky enough I don't want to think about it now. */
3709 if (else_bb
&& (next
= next
->next_bb
) != else_bb
)
3711 if ((next
= next
->next_bb
) != join_bb
3712 && join_bb
!= EXIT_BLOCK_PTR_FOR_FN (cfun
))
3720 /* Do the real work. */
3722 ce_info
->else_bb
= else_bb
;
3723 ce_info
->join_bb
= join_bb
;
3725 /* If we have && and || tests, try to first handle combining the && and ||
3726 tests into the conditional code, and if that fails, go back and handle
3727 it without the && and ||, which at present handles the && case if there
3728 was no ELSE block. */
3729 if (cond_exec_process_if_block (ce_info
, TRUE
))
3732 if (ce_info
->num_multiple_test_blocks
)
3736 if (cond_exec_process_if_block (ce_info
, FALSE
))
3743 /* Convert a branch over a trap, or a branch
3744 to a trap, into a conditional trap. */
3747 find_cond_trap (basic_block test_bb
, edge then_edge
, edge else_edge
)
3749 basic_block then_bb
= then_edge
->dest
;
3750 basic_block else_bb
= else_edge
->dest
;
3751 basic_block other_bb
, trap_bb
;
3752 rtx_insn
*trap
, *jump
;
3754 rtx_insn
*cond_earliest
;
3757 /* Locate the block with the trap instruction. */
3758 /* ??? While we look for no successors, we really ought to allow
3759 EH successors. Need to fix merge_if_block for that to work. */
3760 if ((trap
= block_has_only_trap (then_bb
)) != NULL
)
3761 trap_bb
= then_bb
, other_bb
= else_bb
;
3762 else if ((trap
= block_has_only_trap (else_bb
)) != NULL
)
3763 trap_bb
= else_bb
, other_bb
= then_bb
;
3769 fprintf (dump_file
, "\nTRAP-IF block found, start %d, trap %d\n",
3770 test_bb
->index
, trap_bb
->index
);
3773 /* If this is not a standard conditional jump, we can't parse it. */
3774 jump
= BB_END (test_bb
);
3775 cond
= noce_get_condition (jump
, &cond_earliest
, false);
3779 /* If the conditional jump is more than just a conditional jump, then
3780 we can not do if-conversion on this block. */
3781 if (! onlyjump_p (jump
))
3784 /* We must be comparing objects whose modes imply the size. */
3785 if (GET_MODE (XEXP (cond
, 0)) == BLKmode
)
3788 /* Reverse the comparison code, if necessary. */
3789 code
= GET_CODE (cond
);
3790 if (then_bb
== trap_bb
)
3792 code
= reversed_comparison_code (cond
, jump
);
3793 if (code
== UNKNOWN
)
3797 /* Attempt to generate the conditional trap. */
3798 rtx_insn
*seq
= gen_cond_trap (code
, copy_rtx (XEXP (cond
, 0)),
3799 copy_rtx (XEXP (cond
, 1)),
3800 TRAP_CODE (PATTERN (trap
)));
3804 /* Emit the new insns before cond_earliest. */
3805 emit_insn_before_setloc (seq
, cond_earliest
, INSN_LOCATION (trap
));
3807 /* Delete the trap block if possible. */
3808 remove_edge (trap_bb
== then_bb
? then_edge
: else_edge
);
3809 df_set_bb_dirty (test_bb
);
3810 df_set_bb_dirty (then_bb
);
3811 df_set_bb_dirty (else_bb
);
3813 if (EDGE_COUNT (trap_bb
->preds
) == 0)
3815 delete_basic_block (trap_bb
);
3819 /* Wire together the blocks again. */
3820 if (current_ir_type () == IR_RTL_CFGLAYOUT
)
3821 single_succ_edge (test_bb
)->flags
|= EDGE_FALLTHRU
;
3822 else if (trap_bb
== then_bb
)
3824 rtx lab
= JUMP_LABEL (jump
);
3825 rtx_insn
*seq
= targetm
.gen_jump (lab
);
3826 rtx_jump_insn
*newjump
= emit_jump_insn_after (seq
, jump
);
3827 LABEL_NUSES (lab
) += 1;
3828 JUMP_LABEL (newjump
) = lab
;
3829 emit_barrier_after (newjump
);
3833 if (can_merge_blocks_p (test_bb
, other_bb
))
3835 merge_blocks (test_bb
, other_bb
);
3839 num_updated_if_blocks
++;
3843 /* Subroutine of find_cond_trap: if BB contains only a trap insn,
3847 block_has_only_trap (basic_block bb
)
3851 /* We're not the exit block. */
3852 if (bb
== EXIT_BLOCK_PTR_FOR_FN (cfun
))
3855 /* The block must have no successors. */
3856 if (EDGE_COUNT (bb
->succs
) > 0)
3859 /* The only instruction in the THEN block must be the trap. */
3860 trap
= first_active_insn (bb
);
3861 if (! (trap
== BB_END (bb
)
3862 && GET_CODE (PATTERN (trap
)) == TRAP_IF
3863 && TRAP_CONDITION (PATTERN (trap
)) == const_true_rtx
))
3869 /* Look for IF-THEN-ELSE cases in which one of THEN or ELSE is
3870 transformable, but not necessarily the other. There need be no
3873 Return TRUE if we were successful at converting the block.
3875 Cases we'd like to look at:
3878 if (test) goto over; // x not live
3886 if (! test) goto label;
3889 if (test) goto E; // x not live
3903 (3) // This one's really only interesting for targets that can do
3904 // multiway branching, e.g. IA-64 BBB bundles. For other targets
3905 // it results in multiple branches on a cache line, which often
3906 // does not sit well with predictors.
3908 if (test1) goto E; // predicted not taken
3924 (A) Don't do (2) if the branch is predicted against the block we're
3925 eliminating. Do it anyway if we can eliminate a branch; this requires
3926 that the sole successor of the eliminated block postdominate the other
3929 (B) With CE, on (3) we can steal from both sides of the if, creating
3938 Again, this is most useful if J postdominates.
3940 (C) CE substitutes for helpful life information.
3942 (D) These heuristics need a lot of work. */
3944 /* Tests for case 1 above. */
3947 find_if_case_1 (basic_block test_bb
, edge then_edge
, edge else_edge
)
3949 basic_block then_bb
= then_edge
->dest
;
3950 basic_block else_bb
= else_edge
->dest
;
3952 int then_bb_index
, then_prob
;
3953 rtx else_target
= NULL_RTX
;
3955 /* If we are partitioning hot/cold basic blocks, we don't want to
3956 mess up unconditional or indirect jumps that cross between hot
3959 Basic block partitioning may result in some jumps that appear to
3960 be optimizable (or blocks that appear to be mergeable), but which really
3961 must be left untouched (they are required to make it safely across
3962 partition boundaries). See the comments at the top of
3963 bb-reorder.c:partition_hot_cold_basic_blocks for complete details. */
3965 if ((BB_END (then_bb
)
3966 && JUMP_P (BB_END (then_bb
))
3967 && CROSSING_JUMP_P (BB_END (then_bb
)))
3968 || (BB_END (test_bb
)
3969 && JUMP_P (BB_END (test_bb
))
3970 && CROSSING_JUMP_P (BB_END (test_bb
)))
3971 || (BB_END (else_bb
)
3972 && JUMP_P (BB_END (else_bb
))
3973 && CROSSING_JUMP_P (BB_END (else_bb
))))
3976 /* THEN has one successor. */
3977 if (!single_succ_p (then_bb
))
3980 /* THEN does not fall through, but is not strange either. */
3981 if (single_succ_edge (then_bb
)->flags
& (EDGE_COMPLEX
| EDGE_FALLTHRU
))
3984 /* THEN has one predecessor. */
3985 if (!single_pred_p (then_bb
))
3988 /* THEN must do something. */
3989 if (forwarder_block_p (then_bb
))
3992 num_possible_if_blocks
++;
3995 "\nIF-CASE-1 found, start %d, then %d\n",
3996 test_bb
->index
, then_bb
->index
);
3998 if (then_edge
->probability
)
3999 then_prob
= REG_BR_PROB_BASE
- then_edge
->probability
;
4001 then_prob
= REG_BR_PROB_BASE
/ 2;
4003 /* We're speculating from the THEN path, we want to make sure the cost
4004 of speculation is within reason. */
4005 if (! cheap_bb_rtx_cost_p (then_bb
, then_prob
,
4006 COSTS_N_INSNS (BRANCH_COST (optimize_bb_for_speed_p (then_edge
->src
),
4007 predictable_edge_p (then_edge
)))))
4010 if (else_bb
== EXIT_BLOCK_PTR_FOR_FN (cfun
))
4012 rtx_insn
*jump
= BB_END (else_edge
->src
);
4013 gcc_assert (JUMP_P (jump
));
4014 else_target
= JUMP_LABEL (jump
);
4017 /* Registers set are dead, or are predicable. */
4018 if (! dead_or_predicable (test_bb
, then_bb
, else_bb
,
4019 single_succ_edge (then_bb
), 1))
4022 /* Conversion went ok, including moving the insns and fixing up the
4023 jump. Adjust the CFG to match. */
4025 /* We can avoid creating a new basic block if then_bb is immediately
4026 followed by else_bb, i.e. deleting then_bb allows test_bb to fall
4027 through to else_bb. */
4029 if (then_bb
->next_bb
== else_bb
4030 && then_bb
->prev_bb
== test_bb
4031 && else_bb
!= EXIT_BLOCK_PTR_FOR_FN (cfun
))
4033 redirect_edge_succ (FALLTHRU_EDGE (test_bb
), else_bb
);
4036 else if (else_bb
== EXIT_BLOCK_PTR_FOR_FN (cfun
))
4037 new_bb
= force_nonfallthru_and_redirect (FALLTHRU_EDGE (test_bb
),
4038 else_bb
, else_target
);
4040 new_bb
= redirect_edge_and_branch_force (FALLTHRU_EDGE (test_bb
),
4043 df_set_bb_dirty (test_bb
);
4044 df_set_bb_dirty (else_bb
);
4046 then_bb_index
= then_bb
->index
;
4047 delete_basic_block (then_bb
);
4049 /* Make rest of code believe that the newly created block is the THEN_BB
4050 block we removed. */
4053 df_bb_replace (then_bb_index
, new_bb
);
4054 /* This should have been done above via force_nonfallthru_and_redirect
4055 (possibly called from redirect_edge_and_branch_force). */
4056 gcc_checking_assert (BB_PARTITION (new_bb
) == BB_PARTITION (test_bb
));
4060 num_updated_if_blocks
++;
4065 /* Test for case 2 above. */
4068 find_if_case_2 (basic_block test_bb
, edge then_edge
, edge else_edge
)
4070 basic_block then_bb
= then_edge
->dest
;
4071 basic_block else_bb
= else_edge
->dest
;
4073 int then_prob
, else_prob
;
4075 /* We do not want to speculate (empty) loop latches. */
4077 && else_bb
->loop_father
->latch
== else_bb
)
4080 /* If we are partitioning hot/cold basic blocks, we don't want to
4081 mess up unconditional or indirect jumps that cross between hot
4084 Basic block partitioning may result in some jumps that appear to
4085 be optimizable (or blocks that appear to be mergeable), but which really
4086 must be left untouched (they are required to make it safely across
4087 partition boundaries). See the comments at the top of
4088 bb-reorder.c:partition_hot_cold_basic_blocks for complete details. */
4090 if ((BB_END (then_bb
)
4091 && JUMP_P (BB_END (then_bb
))
4092 && CROSSING_JUMP_P (BB_END (then_bb
)))
4093 || (BB_END (test_bb
)
4094 && JUMP_P (BB_END (test_bb
))
4095 && CROSSING_JUMP_P (BB_END (test_bb
)))
4096 || (BB_END (else_bb
)
4097 && JUMP_P (BB_END (else_bb
))
4098 && CROSSING_JUMP_P (BB_END (else_bb
))))
4101 /* ELSE has one successor. */
4102 if (!single_succ_p (else_bb
))
4105 else_succ
= single_succ_edge (else_bb
);
4107 /* ELSE outgoing edge is not complex. */
4108 if (else_succ
->flags
& EDGE_COMPLEX
)
4111 /* ELSE has one predecessor. */
4112 if (!single_pred_p (else_bb
))
4115 /* THEN is not EXIT. */
4116 if (then_bb
->index
< NUM_FIXED_BLOCKS
)
4119 if (else_edge
->probability
)
4121 else_prob
= else_edge
->probability
;
4122 then_prob
= REG_BR_PROB_BASE
- else_prob
;
4126 else_prob
= REG_BR_PROB_BASE
/ 2;
4127 then_prob
= REG_BR_PROB_BASE
/ 2;
4130 /* ELSE is predicted or SUCC(ELSE) postdominates THEN. */
4131 if (else_prob
> then_prob
)
4133 else if (else_succ
->dest
->index
< NUM_FIXED_BLOCKS
4134 || dominated_by_p (CDI_POST_DOMINATORS
, then_bb
,
4140 num_possible_if_blocks
++;
4143 "\nIF-CASE-2 found, start %d, else %d\n",
4144 test_bb
->index
, else_bb
->index
);
4146 /* We're speculating from the ELSE path, we want to make sure the cost
4147 of speculation is within reason. */
4148 if (! cheap_bb_rtx_cost_p (else_bb
, else_prob
,
4149 COSTS_N_INSNS (BRANCH_COST (optimize_bb_for_speed_p (else_edge
->src
),
4150 predictable_edge_p (else_edge
)))))
4153 /* Registers set are dead, or are predicable. */
4154 if (! dead_or_predicable (test_bb
, else_bb
, then_bb
, else_succ
, 0))
4157 /* Conversion went ok, including moving the insns and fixing up the
4158 jump. Adjust the CFG to match. */
4160 df_set_bb_dirty (test_bb
);
4161 df_set_bb_dirty (then_bb
);
4162 delete_basic_block (else_bb
);
4165 num_updated_if_blocks
++;
4167 /* ??? We may now fallthru from one of THEN's successors into a join
4168 block. Rerun cleanup_cfg? Examine things manually? Wait? */
4173 /* Used by the code above to perform the actual rtl transformations.
4174 Return TRUE if successful.
4176 TEST_BB is the block containing the conditional branch. MERGE_BB
4177 is the block containing the code to manipulate. DEST_EDGE is an
4178 edge representing a jump to the join block; after the conversion,
4179 TEST_BB should be branching to its destination.
4180 REVERSEP is true if the sense of the branch should be reversed. */
4183 dead_or_predicable (basic_block test_bb
, basic_block merge_bb
,
4184 basic_block other_bb
, edge dest_edge
, int reversep
)
4186 basic_block new_dest
= dest_edge
->dest
;
4187 rtx_insn
*head
, *end
, *jump
;
4188 rtx_insn
*earliest
= NULL
;
4190 bitmap merge_set
= NULL
;
4191 /* Number of pending changes. */
4192 int n_validated_changes
= 0;
4193 rtx new_dest_label
= NULL_RTX
;
4195 jump
= BB_END (test_bb
);
4197 /* Find the extent of the real code in the merge block. */
4198 head
= BB_HEAD (merge_bb
);
4199 end
= BB_END (merge_bb
);
4201 while (DEBUG_INSN_P (end
) && end
!= head
)
4202 end
= PREV_INSN (end
);
4204 /* If merge_bb ends with a tablejump, predicating/moving insn's
4205 into test_bb and then deleting merge_bb will result in the jumptable
4206 that follows merge_bb being removed along with merge_bb and then we
4207 get an unresolved reference to the jumptable. */
4208 if (tablejump_p (end
, NULL
, NULL
))
4212 head
= NEXT_INSN (head
);
4213 while (DEBUG_INSN_P (head
) && head
!= end
)
4214 head
= NEXT_INSN (head
);
4222 head
= NEXT_INSN (head
);
4223 while (DEBUG_INSN_P (head
) && head
!= end
)
4224 head
= NEXT_INSN (head
);
4229 if (!onlyjump_p (end
))
4236 end
= PREV_INSN (end
);
4237 while (DEBUG_INSN_P (end
) && end
!= head
)
4238 end
= PREV_INSN (end
);
4241 /* Don't move frame-related insn across the conditional branch. This
4242 can lead to one of the paths of the branch having wrong unwind info. */
4243 if (epilogue_completed
)
4245 rtx_insn
*insn
= head
;
4248 if (INSN_P (insn
) && RTX_FRAME_RELATED_P (insn
))
4252 insn
= NEXT_INSN (insn
);
4256 /* Disable handling dead code by conditional execution if the machine needs
4257 to do anything funny with the tests, etc. */
4258 #ifndef IFCVT_MODIFY_TESTS
4259 if (targetm
.have_conditional_execution ())
4261 /* In the conditional execution case, we have things easy. We know
4262 the condition is reversible. We don't have to check life info
4263 because we're going to conditionally execute the code anyway.
4264 All that's left is making sure the insns involved can actually
4269 cond
= cond_exec_get_condition (jump
);
4273 rtx note
= find_reg_note (jump
, REG_BR_PROB
, NULL_RTX
);
4274 int prob_val
= (note
? XINT (note
, 0) : -1);
4278 enum rtx_code rev
= reversed_comparison_code (cond
, jump
);
4281 cond
= gen_rtx_fmt_ee (rev
, GET_MODE (cond
), XEXP (cond
, 0),
4284 prob_val
= REG_BR_PROB_BASE
- prob_val
;
4287 if (cond_exec_process_insns (NULL
, head
, end
, cond
, prob_val
, 0)
4288 && verify_changes (0))
4289 n_validated_changes
= num_validated_changes ();
4297 /* If we allocated new pseudos (e.g. in the conditional move
4298 expander called from noce_emit_cmove), we must resize the
4300 if (max_regno
< max_reg_num ())
4301 max_regno
= max_reg_num ();
4303 /* Try the NCE path if the CE path did not result in any changes. */
4304 if (n_validated_changes
== 0)
4311 /* In the non-conditional execution case, we have to verify that there
4312 are no trapping operations, no calls, no references to memory, and
4313 that any registers modified are dead at the branch site. */
4315 if (!any_condjump_p (jump
))
4318 /* Find the extent of the conditional. */
4319 cond
= noce_get_condition (jump
, &earliest
, false);
4323 live
= BITMAP_ALLOC (®_obstack
);
4324 simulate_backwards_to_point (merge_bb
, live
, end
);
4325 success
= can_move_insns_across (head
, end
, earliest
, jump
,
4327 df_get_live_in (other_bb
), NULL
);
4332 /* Collect the set of registers set in MERGE_BB. */
4333 merge_set
= BITMAP_ALLOC (®_obstack
);
4335 FOR_BB_INSNS (merge_bb
, insn
)
4336 if (NONDEBUG_INSN_P (insn
))
4337 df_simulate_find_defs (insn
, merge_set
);
4339 /* If shrink-wrapping, disable this optimization when test_bb is
4340 the first basic block and merge_bb exits. The idea is to not
4341 move code setting up a return register as that may clobber a
4342 register used to pass function parameters, which then must be
4343 saved in caller-saved regs. A caller-saved reg requires the
4344 prologue, killing a shrink-wrap opportunity. */
4345 if ((SHRINK_WRAPPING_ENABLED
&& !epilogue_completed
)
4346 && ENTRY_BLOCK_PTR_FOR_FN (cfun
)->next_bb
== test_bb
4347 && single_succ_p (new_dest
)
4348 && single_succ (new_dest
) == EXIT_BLOCK_PTR_FOR_FN (cfun
)
4349 && bitmap_intersect_p (df_get_live_in (new_dest
), merge_set
))
4354 return_regs
= BITMAP_ALLOC (®_obstack
);
4356 /* Start off with the intersection of regs used to pass
4357 params and regs used to return values. */
4358 for (i
= 0; i
< FIRST_PSEUDO_REGISTER
; i
++)
4359 if (FUNCTION_ARG_REGNO_P (i
)
4360 && targetm
.calls
.function_value_regno_p (i
))
4361 bitmap_set_bit (return_regs
, INCOMING_REGNO (i
));
4363 bitmap_and_into (return_regs
,
4364 df_get_live_out (ENTRY_BLOCK_PTR_FOR_FN (cfun
)));
4365 bitmap_and_into (return_regs
,
4366 df_get_live_in (EXIT_BLOCK_PTR_FOR_FN (cfun
)));
4367 if (!bitmap_empty_p (return_regs
))
4369 FOR_BB_INSNS_REVERSE (new_dest
, insn
)
4370 if (NONDEBUG_INSN_P (insn
))
4374 /* If this insn sets any reg in return_regs, add all
4375 reg uses to the set of regs we're interested in. */
4376 FOR_EACH_INSN_DEF (def
, insn
)
4377 if (bitmap_bit_p (return_regs
, DF_REF_REGNO (def
)))
4379 df_simulate_uses (insn
, return_regs
);
4383 if (bitmap_intersect_p (merge_set
, return_regs
))
4385 BITMAP_FREE (return_regs
);
4386 BITMAP_FREE (merge_set
);
4390 BITMAP_FREE (return_regs
);
4395 /* We don't want to use normal invert_jump or redirect_jump because
4396 we don't want to delete_insn called. Also, we want to do our own
4397 change group management. */
4399 old_dest
= JUMP_LABEL (jump
);
4400 if (other_bb
!= new_dest
)
4402 if (!any_condjump_p (jump
))
4405 if (JUMP_P (BB_END (dest_edge
->src
)))
4406 new_dest_label
= JUMP_LABEL (BB_END (dest_edge
->src
));
4407 else if (new_dest
== EXIT_BLOCK_PTR_FOR_FN (cfun
))
4408 new_dest_label
= ret_rtx
;
4410 new_dest_label
= block_label (new_dest
);
4412 rtx_jump_insn
*jump_insn
= as_a
<rtx_jump_insn
*> (jump
);
4414 ? ! invert_jump_1 (jump_insn
, new_dest_label
)
4415 : ! redirect_jump_1 (jump_insn
, new_dest_label
))
4419 if (verify_changes (n_validated_changes
))
4420 confirm_change_group ();
4424 if (other_bb
!= new_dest
)
4426 redirect_jump_2 (as_a
<rtx_jump_insn
*> (jump
), old_dest
, new_dest_label
,
4429 redirect_edge_succ (BRANCH_EDGE (test_bb
), new_dest
);
4432 std::swap (BRANCH_EDGE (test_bb
)->count
,
4433 FALLTHRU_EDGE (test_bb
)->count
);
4434 std::swap (BRANCH_EDGE (test_bb
)->probability
,
4435 FALLTHRU_EDGE (test_bb
)->probability
);
4436 update_br_prob_note (test_bb
);
4440 /* Move the insns out of MERGE_BB to before the branch. */
4445 if (end
== BB_END (merge_bb
))
4446 BB_END (merge_bb
) = PREV_INSN (head
);
4448 /* PR 21767: when moving insns above a conditional branch, the REG_EQUAL
4449 notes being moved might become invalid. */
4455 if (! INSN_P (insn
))
4457 note
= find_reg_note (insn
, REG_EQUAL
, NULL_RTX
);
4460 remove_note (insn
, note
);
4461 } while (insn
!= end
&& (insn
= NEXT_INSN (insn
)));
4463 /* PR46315: when moving insns above a conditional branch, the REG_EQUAL
4464 notes referring to the registers being set might become invalid. */
4470 EXECUTE_IF_SET_IN_BITMAP (merge_set
, 0, i
, bi
)
4471 remove_reg_equal_equiv_notes_for_regno (i
);
4473 BITMAP_FREE (merge_set
);
4476 reorder_insns (head
, end
, PREV_INSN (earliest
));
4479 /* Remove the jump and edge if we can. */
4480 if (other_bb
== new_dest
)
4483 remove_edge (BRANCH_EDGE (test_bb
));
4484 /* ??? Can't merge blocks here, as then_bb is still in use.
4485 At minimum, the merge will get done just before bb-reorder. */
4494 BITMAP_FREE (merge_set
);
4499 /* Main entry point for all if-conversion. AFTER_COMBINE is true if
4500 we are after combine pass. */
4503 if_convert (bool after_combine
)
4510 df_live_add_problem ();
4511 df_live_set_all_dirty ();
4514 /* Record whether we are after combine pass. */
4515 ifcvt_after_combine
= after_combine
;
4516 num_possible_if_blocks
= 0;
4517 num_updated_if_blocks
= 0;
4518 num_true_changes
= 0;
4520 loop_optimizer_init (AVOID_CFG_MODIFICATIONS
);
4521 mark_loop_exit_edges ();
4522 loop_optimizer_finalize ();
4523 free_dominance_info (CDI_DOMINATORS
);
4525 /* Compute postdominators. */
4526 calculate_dominance_info (CDI_POST_DOMINATORS
);
4528 df_set_flags (DF_LR_RUN_DCE
);
4530 /* Go through each of the basic blocks looking for things to convert. If we
4531 have conditional execution, we make multiple passes to allow us to handle
4532 IF-THEN{-ELSE} blocks within other IF-THEN{-ELSE} blocks. */
4537 /* Only need to do dce on the first pass. */
4538 df_clear_flags (DF_LR_RUN_DCE
);
4539 cond_exec_changed_p
= FALSE
;
4542 #ifdef IFCVT_MULTIPLE_DUMPS
4543 if (dump_file
&& pass
> 1)
4544 fprintf (dump_file
, "\n\n========== Pass %d ==========\n", pass
);
4547 FOR_EACH_BB_FN (bb
, cfun
)
4550 while (!df_get_bb_dirty (bb
)
4551 && (new_bb
= find_if_header (bb
, pass
)) != NULL
)
4555 #ifdef IFCVT_MULTIPLE_DUMPS
4556 if (dump_file
&& cond_exec_changed_p
)
4557 print_rtl_with_bb (dump_file
, get_insns (), dump_flags
);
4560 while (cond_exec_changed_p
);
4562 #ifdef IFCVT_MULTIPLE_DUMPS
4564 fprintf (dump_file
, "\n\n========== no more changes\n");
4567 free_dominance_info (CDI_POST_DOMINATORS
);
4572 clear_aux_for_blocks ();
4574 /* If we allocated new pseudos, we must resize the array for sched1. */
4575 if (max_regno
< max_reg_num ())
4576 max_regno
= max_reg_num ();
4578 /* Write the final stats. */
4579 if (dump_file
&& num_possible_if_blocks
> 0)
4582 "\n%d possible IF blocks searched.\n",
4583 num_possible_if_blocks
);
4585 "%d IF blocks converted.\n",
4586 num_updated_if_blocks
);
4588 "%d true changes made.\n\n\n",
4593 df_remove_problem (df_live
);
4595 #ifdef ENABLE_CHECKING
4596 verify_flow_info ();
4600 /* If-conversion and CFG cleanup. */
4602 rest_of_handle_if_conversion (void)
4604 if (flag_if_conversion
)
4608 dump_reg_info (dump_file
);
4609 dump_flow_info (dump_file
, dump_flags
);
4611 cleanup_cfg (CLEANUP_EXPENSIVE
);
4621 const pass_data pass_data_rtl_ifcvt
=
4623 RTL_PASS
, /* type */
4625 OPTGROUP_NONE
, /* optinfo_flags */
4626 TV_IFCVT
, /* tv_id */
4627 0, /* properties_required */
4628 0, /* properties_provided */
4629 0, /* properties_destroyed */
4630 0, /* todo_flags_start */
4631 TODO_df_finish
, /* todo_flags_finish */
4634 class pass_rtl_ifcvt
: public rtl_opt_pass
4637 pass_rtl_ifcvt (gcc::context
*ctxt
)
4638 : rtl_opt_pass (pass_data_rtl_ifcvt
, ctxt
)
4641 /* opt_pass methods: */
4642 virtual bool gate (function
*)
4644 return (optimize
> 0) && dbg_cnt (if_conversion
);
4647 virtual unsigned int execute (function
*)
4649 return rest_of_handle_if_conversion ();
4652 }; // class pass_rtl_ifcvt
4657 make_pass_rtl_ifcvt (gcc::context
*ctxt
)
4659 return new pass_rtl_ifcvt (ctxt
);
4663 /* Rerun if-conversion, as combine may have simplified things enough
4664 to now meet sequence length restrictions. */
4668 const pass_data pass_data_if_after_combine
=
4670 RTL_PASS
, /* type */
4672 OPTGROUP_NONE
, /* optinfo_flags */
4673 TV_IFCVT
, /* tv_id */
4674 0, /* properties_required */
4675 0, /* properties_provided */
4676 0, /* properties_destroyed */
4677 0, /* todo_flags_start */
4678 TODO_df_finish
, /* todo_flags_finish */
4681 class pass_if_after_combine
: public rtl_opt_pass
4684 pass_if_after_combine (gcc::context
*ctxt
)
4685 : rtl_opt_pass (pass_data_if_after_combine
, ctxt
)
4688 /* opt_pass methods: */
4689 virtual bool gate (function
*)
4691 return optimize
> 0 && flag_if_conversion
4692 && dbg_cnt (if_after_combine
);
4695 virtual unsigned int execute (function
*)
4701 }; // class pass_if_after_combine
4706 make_pass_if_after_combine (gcc::context
*ctxt
)
4708 return new pass_if_after_combine (ctxt
);
4714 const pass_data pass_data_if_after_reload
=
4716 RTL_PASS
, /* type */
4718 OPTGROUP_NONE
, /* optinfo_flags */
4719 TV_IFCVT2
, /* tv_id */
4720 0, /* properties_required */
4721 0, /* properties_provided */
4722 0, /* properties_destroyed */
4723 0, /* todo_flags_start */
4724 TODO_df_finish
, /* todo_flags_finish */
4727 class pass_if_after_reload
: public rtl_opt_pass
4730 pass_if_after_reload (gcc::context
*ctxt
)
4731 : rtl_opt_pass (pass_data_if_after_reload
, ctxt
)
4734 /* opt_pass methods: */
4735 virtual bool gate (function
*)
4737 return optimize
> 0 && flag_if_conversion2
4738 && dbg_cnt (if_after_reload
);
4741 virtual unsigned int execute (function
*)
4747 }; // class pass_if_after_reload
4752 make_pass_if_after_reload (gcc::context
*ctxt
)
4754 return new pass_if_after_reload (ctxt
);