Implement -mmemcpy-strategy= and -mmemset-strategy= options
[official-gcc.git] / gcc / ifcvt.c
blob5698b4f5e7242a63449b5fcd1444a7673aefa015
1 /* If-conversion support.
2 Copyright (C) 2000-2013 Free Software Foundation, Inc.
4 This file is part of GCC.
6 GCC is free software; you can redistribute it and/or modify it
7 under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 3, or (at your option)
9 any later version.
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/>. */
20 #include "config.h"
21 #include "system.h"
22 #include "coretypes.h"
23 #include "tm.h"
25 #include "rtl.h"
26 #include "regs.h"
27 #include "function.h"
28 #include "flags.h"
29 #include "insn-config.h"
30 #include "recog.h"
31 #include "except.h"
32 #include "hard-reg-set.h"
33 #include "basic-block.h"
34 #include "expr.h"
35 #include "output.h"
36 #include "optabs.h"
37 #include "diagnostic-core.h"
38 #include "tm_p.h"
39 #include "cfgloop.h"
40 #include "target.h"
41 #include "tree-pass.h"
42 #include "df.h"
43 #include "vec.h"
44 #include "pointer-set.h"
45 #include "dbgcnt.h"
47 #ifndef HAVE_conditional_move
48 #define HAVE_conditional_move 0
49 #endif
50 #ifndef HAVE_incscc
51 #define HAVE_incscc 0
52 #endif
53 #ifndef HAVE_decscc
54 #define HAVE_decscc 0
55 #endif
56 #ifndef HAVE_trap
57 #define HAVE_trap 0
58 #endif
60 #ifndef MAX_CONDITIONAL_EXECUTE
61 #define MAX_CONDITIONAL_EXECUTE \
62 (BRANCH_COST (optimize_function_for_speed_p (cfun), false) \
63 + 1)
64 #endif
66 #define IFCVT_MULTIPLE_DUMPS 1
68 #define NULL_BLOCK ((basic_block) NULL)
70 /* True if after combine pass. */
71 static bool ifcvt_after_combine;
73 /* # of IF-THEN or IF-THEN-ELSE blocks we looked at */
74 static int num_possible_if_blocks;
76 /* # of IF-THEN or IF-THEN-ELSE blocks were converted to conditional
77 execution. */
78 static int num_updated_if_blocks;
80 /* # of changes made. */
81 static int num_true_changes;
83 /* Whether conditional execution changes were made. */
84 static int cond_exec_changed_p;
86 /* Forward references. */
87 static int count_bb_insns (const_basic_block);
88 static bool cheap_bb_rtx_cost_p (const_basic_block, int, int);
89 static rtx first_active_insn (basic_block);
90 static rtx last_active_insn (basic_block, int);
91 static rtx find_active_insn_before (basic_block, rtx);
92 static rtx find_active_insn_after (basic_block, rtx);
93 static basic_block block_fallthru (basic_block);
94 static int cond_exec_process_insns (ce_if_block_t *, rtx, rtx, rtx, rtx, int);
95 static rtx cond_exec_get_condition (rtx);
96 static rtx noce_get_condition (rtx, rtx *, bool);
97 static int noce_operand_ok (const_rtx);
98 static void merge_if_block (ce_if_block_t *);
99 static int find_cond_trap (basic_block, edge, edge);
100 static basic_block find_if_header (basic_block, int);
101 static int block_jumps_and_fallthru_p (basic_block, basic_block);
102 static int noce_find_if_block (basic_block, edge, edge, int);
103 static int cond_exec_find_if_block (ce_if_block_t *);
104 static int find_if_case_1 (basic_block, edge, edge);
105 static int find_if_case_2 (basic_block, edge, edge);
106 static int dead_or_predicable (basic_block, basic_block, basic_block,
107 edge, int);
108 static void noce_emit_move_insn (rtx, rtx);
109 static rtx block_has_only_trap (basic_block);
111 /* Count the number of non-jump active insns in BB. */
113 static int
114 count_bb_insns (const_basic_block bb)
116 int count = 0;
117 rtx insn = BB_HEAD (bb);
119 while (1)
121 if (CALL_P (insn) || NONJUMP_INSN_P (insn))
122 count++;
124 if (insn == BB_END (bb))
125 break;
126 insn = NEXT_INSN (insn);
129 return count;
132 /* Determine whether the total insn_rtx_cost on non-jump insns in
133 basic block BB is less than MAX_COST. This function returns
134 false if the cost of any instruction could not be estimated.
136 The cost of the non-jump insns in BB is scaled by REG_BR_PROB_BASE
137 as those insns are being speculated. MAX_COST is scaled with SCALE
138 plus a small fudge factor. */
140 static bool
141 cheap_bb_rtx_cost_p (const_basic_block bb, int scale, int max_cost)
143 int count = 0;
144 rtx insn = BB_HEAD (bb);
145 bool speed = optimize_bb_for_speed_p (bb);
147 /* Set scale to REG_BR_PROB_BASE to void the identical scaling
148 applied to insn_rtx_cost when optimizing for size. Only do
149 this after combine because if-conversion might interfere with
150 passes before combine.
152 Use optimize_function_for_speed_p instead of the pre-defined
153 variable speed to make sure it is set to same value for all
154 basic blocks in one if-conversion transformation. */
155 if (!optimize_function_for_speed_p (cfun) && ifcvt_after_combine)
156 scale = REG_BR_PROB_BASE;
157 /* Our branch probability/scaling factors are just estimates and don't
158 account for cases where we can get speculation for free and other
159 secondary benefits. So we fudge the scale factor to make speculating
160 appear a little more profitable when optimizing for performance. */
161 else
162 scale += REG_BR_PROB_BASE / 8;
165 max_cost *= scale;
167 while (1)
169 if (NONJUMP_INSN_P (insn))
171 int cost = insn_rtx_cost (PATTERN (insn), speed) * REG_BR_PROB_BASE;
172 if (cost == 0)
173 return false;
175 /* If this instruction is the load or set of a "stack" register,
176 such as a floating point register on x87, then the cost of
177 speculatively executing this insn may need to include
178 the additional cost of popping its result off of the
179 register stack. Unfortunately, correctly recognizing and
180 accounting for this additional overhead is tricky, so for
181 now we simply prohibit such speculative execution. */
182 #ifdef STACK_REGS
184 rtx set = single_set (insn);
185 if (set && STACK_REG_P (SET_DEST (set)))
186 return false;
188 #endif
190 count += cost;
191 if (count >= max_cost)
192 return false;
194 else if (CALL_P (insn))
195 return false;
197 if (insn == BB_END (bb))
198 break;
199 insn = NEXT_INSN (insn);
202 return true;
205 /* Return the first non-jump active insn in the basic block. */
207 static rtx
208 first_active_insn (basic_block bb)
210 rtx insn = BB_HEAD (bb);
212 if (LABEL_P (insn))
214 if (insn == BB_END (bb))
215 return NULL_RTX;
216 insn = NEXT_INSN (insn);
219 while (NOTE_P (insn) || DEBUG_INSN_P (insn))
221 if (insn == BB_END (bb))
222 return NULL_RTX;
223 insn = NEXT_INSN (insn);
226 if (JUMP_P (insn))
227 return NULL_RTX;
229 return insn;
232 /* Return the last non-jump active (non-jump) insn in the basic block. */
234 static rtx
235 last_active_insn (basic_block bb, int skip_use_p)
237 rtx insn = BB_END (bb);
238 rtx head = BB_HEAD (bb);
240 while (NOTE_P (insn)
241 || JUMP_P (insn)
242 || DEBUG_INSN_P (insn)
243 || (skip_use_p
244 && NONJUMP_INSN_P (insn)
245 && GET_CODE (PATTERN (insn)) == USE))
247 if (insn == head)
248 return NULL_RTX;
249 insn = PREV_INSN (insn);
252 if (LABEL_P (insn))
253 return NULL_RTX;
255 return insn;
258 /* Return the active insn before INSN inside basic block CURR_BB. */
260 static rtx
261 find_active_insn_before (basic_block curr_bb, rtx insn)
263 if (!insn || insn == BB_HEAD (curr_bb))
264 return NULL_RTX;
266 while ((insn = PREV_INSN (insn)) != NULL_RTX)
268 if (NONJUMP_INSN_P (insn) || JUMP_P (insn) || CALL_P (insn))
269 break;
271 /* No other active insn all the way to the start of the basic block. */
272 if (insn == BB_HEAD (curr_bb))
273 return NULL_RTX;
276 return insn;
279 /* Return the active insn after INSN inside basic block CURR_BB. */
281 static rtx
282 find_active_insn_after (basic_block curr_bb, rtx insn)
284 if (!insn || insn == BB_END (curr_bb))
285 return NULL_RTX;
287 while ((insn = NEXT_INSN (insn)) != NULL_RTX)
289 if (NONJUMP_INSN_P (insn) || JUMP_P (insn) || CALL_P (insn))
290 break;
292 /* No other active insn all the way to the end of the basic block. */
293 if (insn == BB_END (curr_bb))
294 return NULL_RTX;
297 return insn;
300 /* Return the basic block reached by falling though the basic block BB. */
302 static basic_block
303 block_fallthru (basic_block bb)
305 edge e = find_fallthru_edge (bb->succs);
307 return (e) ? e->dest : NULL_BLOCK;
310 /* Go through a bunch of insns, converting them to conditional
311 execution format if possible. Return TRUE if all of the non-note
312 insns were processed. */
314 static int
315 cond_exec_process_insns (ce_if_block_t *ce_info ATTRIBUTE_UNUSED,
316 /* if block information */rtx start,
317 /* first insn to look at */rtx end,
318 /* last insn to look at */rtx test,
319 /* conditional execution test */rtx prob_val,
320 /* probability of branch taken. */int mod_ok)
322 int must_be_last = FALSE;
323 rtx insn;
324 rtx xtest;
325 rtx pattern;
327 if (!start || !end)
328 return FALSE;
330 for (insn = start; ; insn = NEXT_INSN (insn))
332 /* dwarf2out can't cope with conditional prologues. */
333 if (NOTE_P (insn) && NOTE_KIND (insn) == NOTE_INSN_PROLOGUE_END)
334 return FALSE;
336 if (NOTE_P (insn) || DEBUG_INSN_P (insn))
337 goto insn_done;
339 gcc_assert(NONJUMP_INSN_P (insn) || CALL_P (insn));
341 /* Remove USE insns that get in the way. */
342 if (reload_completed && GET_CODE (PATTERN (insn)) == USE)
344 /* ??? Ug. Actually unlinking the thing is problematic,
345 given what we'd have to coordinate with our callers. */
346 SET_INSN_DELETED (insn);
347 goto insn_done;
350 /* Last insn wasn't last? */
351 if (must_be_last)
352 return FALSE;
354 if (modified_in_p (test, insn))
356 if (!mod_ok)
357 return FALSE;
358 must_be_last = TRUE;
361 /* Now build the conditional form of the instruction. */
362 pattern = PATTERN (insn);
363 xtest = copy_rtx (test);
365 /* If this is already a COND_EXEC, rewrite the test to be an AND of the
366 two conditions. */
367 if (GET_CODE (pattern) == COND_EXEC)
369 if (GET_MODE (xtest) != GET_MODE (COND_EXEC_TEST (pattern)))
370 return FALSE;
372 xtest = gen_rtx_AND (GET_MODE (xtest), xtest,
373 COND_EXEC_TEST (pattern));
374 pattern = COND_EXEC_CODE (pattern);
377 pattern = gen_rtx_COND_EXEC (VOIDmode, xtest, pattern);
379 /* If the machine needs to modify the insn being conditionally executed,
380 say for example to force a constant integer operand into a temp
381 register, do so here. */
382 #ifdef IFCVT_MODIFY_INSN
383 IFCVT_MODIFY_INSN (ce_info, pattern, insn);
384 if (! pattern)
385 return FALSE;
386 #endif
388 validate_change (insn, &PATTERN (insn), pattern, 1);
390 if (CALL_P (insn) && prob_val)
391 validate_change (insn, &REG_NOTES (insn),
392 alloc_EXPR_LIST (REG_BR_PROB, prob_val,
393 REG_NOTES (insn)), 1);
395 insn_done:
396 if (insn == end)
397 break;
400 return TRUE;
403 /* Return the condition for a jump. Do not do any special processing. */
405 static rtx
406 cond_exec_get_condition (rtx jump)
408 rtx test_if, cond;
410 if (any_condjump_p (jump))
411 test_if = SET_SRC (pc_set (jump));
412 else
413 return NULL_RTX;
414 cond = XEXP (test_if, 0);
416 /* If this branches to JUMP_LABEL when the condition is false,
417 reverse the condition. */
418 if (GET_CODE (XEXP (test_if, 2)) == LABEL_REF
419 && XEXP (XEXP (test_if, 2), 0) == JUMP_LABEL (jump))
421 enum rtx_code rev = reversed_comparison_code (cond, jump);
422 if (rev == UNKNOWN)
423 return NULL_RTX;
425 cond = gen_rtx_fmt_ee (rev, GET_MODE (cond), XEXP (cond, 0),
426 XEXP (cond, 1));
429 return cond;
432 /* Given a simple IF-THEN or IF-THEN-ELSE block, attempt to convert it
433 to conditional execution. Return TRUE if we were successful at
434 converting the block. */
436 static int
437 cond_exec_process_if_block (ce_if_block_t * ce_info,
438 /* if block information */int do_multiple_p)
440 basic_block test_bb = ce_info->test_bb; /* last test block */
441 basic_block then_bb = ce_info->then_bb; /* THEN */
442 basic_block else_bb = ce_info->else_bb; /* ELSE or NULL */
443 rtx test_expr; /* expression in IF_THEN_ELSE that is tested */
444 rtx then_start; /* first insn in THEN block */
445 rtx then_end; /* last insn + 1 in THEN block */
446 rtx else_start = NULL_RTX; /* first insn in ELSE block or NULL */
447 rtx else_end = NULL_RTX; /* last insn + 1 in ELSE block */
448 int max; /* max # of insns to convert. */
449 int then_mod_ok; /* whether conditional mods are ok in THEN */
450 rtx true_expr; /* test for else block insns */
451 rtx false_expr; /* test for then block insns */
452 rtx true_prob_val; /* probability of else block */
453 rtx false_prob_val; /* probability of then block */
454 rtx then_last_head = NULL_RTX; /* Last match at the head of THEN */
455 rtx else_last_head = NULL_RTX; /* Last match at the head of ELSE */
456 rtx then_first_tail = NULL_RTX; /* First match at the tail of THEN */
457 rtx else_first_tail = NULL_RTX; /* First match at the tail of ELSE */
458 int then_n_insns, else_n_insns, n_insns;
459 enum rtx_code false_code;
461 /* If test is comprised of && or || elements, and we've failed at handling
462 all of them together, just use the last test if it is the special case of
463 && elements without an ELSE block. */
464 if (!do_multiple_p && ce_info->num_multiple_test_blocks)
466 if (else_bb || ! ce_info->and_and_p)
467 return FALSE;
469 ce_info->test_bb = test_bb = ce_info->last_test_bb;
470 ce_info->num_multiple_test_blocks = 0;
471 ce_info->num_and_and_blocks = 0;
472 ce_info->num_or_or_blocks = 0;
475 /* Find the conditional jump to the ELSE or JOIN part, and isolate
476 the test. */
477 test_expr = cond_exec_get_condition (BB_END (test_bb));
478 if (! test_expr)
479 return FALSE;
481 /* If the conditional jump is more than just a conditional jump,
482 then we can not do conditional execution conversion on this block. */
483 if (! onlyjump_p (BB_END (test_bb)))
484 return FALSE;
486 /* Collect the bounds of where we're to search, skipping any labels, jumps
487 and notes at the beginning and end of the block. Then count the total
488 number of insns and see if it is small enough to convert. */
489 then_start = first_active_insn (then_bb);
490 then_end = last_active_insn (then_bb, TRUE);
491 then_n_insns = ce_info->num_then_insns = count_bb_insns (then_bb);
492 n_insns = then_n_insns;
493 max = MAX_CONDITIONAL_EXECUTE;
495 if (else_bb)
497 int n_matching;
499 max *= 2;
500 else_start = first_active_insn (else_bb);
501 else_end = last_active_insn (else_bb, TRUE);
502 else_n_insns = ce_info->num_else_insns = count_bb_insns (else_bb);
503 n_insns += else_n_insns;
505 /* Look for matching sequences at the head and tail of the two blocks,
506 and limit the range of insns to be converted if possible. */
507 n_matching = flow_find_cross_jump (then_bb, else_bb,
508 &then_first_tail, &else_first_tail,
509 NULL);
510 if (then_first_tail == BB_HEAD (then_bb))
511 then_start = then_end = NULL_RTX;
512 if (else_first_tail == BB_HEAD (else_bb))
513 else_start = else_end = NULL_RTX;
515 if (n_matching > 0)
517 if (then_end)
518 then_end = find_active_insn_before (then_bb, then_first_tail);
519 if (else_end)
520 else_end = find_active_insn_before (else_bb, else_first_tail);
521 n_insns -= 2 * n_matching;
524 if (then_start && else_start)
526 int longest_match = MIN (then_n_insns - n_matching,
527 else_n_insns - n_matching);
528 n_matching
529 = flow_find_head_matching_sequence (then_bb, else_bb,
530 &then_last_head,
531 &else_last_head,
532 longest_match);
534 if (n_matching > 0)
536 rtx insn;
538 /* We won't pass the insns in the head sequence to
539 cond_exec_process_insns, so we need to test them here
540 to make sure that they don't clobber the condition. */
541 for (insn = BB_HEAD (then_bb);
542 insn != NEXT_INSN (then_last_head);
543 insn = NEXT_INSN (insn))
544 if (!LABEL_P (insn) && !NOTE_P (insn)
545 && !DEBUG_INSN_P (insn)
546 && modified_in_p (test_expr, insn))
547 return FALSE;
550 if (then_last_head == then_end)
551 then_start = then_end = NULL_RTX;
552 if (else_last_head == else_end)
553 else_start = else_end = NULL_RTX;
555 if (n_matching > 0)
557 if (then_start)
558 then_start = find_active_insn_after (then_bb, then_last_head);
559 if (else_start)
560 else_start = find_active_insn_after (else_bb, else_last_head);
561 n_insns -= 2 * n_matching;
566 if (n_insns > max)
567 return FALSE;
569 /* Map test_expr/test_jump into the appropriate MD tests to use on
570 the conditionally executed code. */
572 true_expr = test_expr;
574 false_code = reversed_comparison_code (true_expr, BB_END (test_bb));
575 if (false_code != UNKNOWN)
576 false_expr = gen_rtx_fmt_ee (false_code, GET_MODE (true_expr),
577 XEXP (true_expr, 0), XEXP (true_expr, 1));
578 else
579 false_expr = NULL_RTX;
581 #ifdef IFCVT_MODIFY_TESTS
582 /* If the machine description needs to modify the tests, such as setting a
583 conditional execution register from a comparison, it can do so here. */
584 IFCVT_MODIFY_TESTS (ce_info, true_expr, false_expr);
586 /* See if the conversion failed. */
587 if (!true_expr || !false_expr)
588 goto fail;
589 #endif
591 true_prob_val = find_reg_note (BB_END (test_bb), REG_BR_PROB, NULL_RTX);
592 if (true_prob_val)
594 true_prob_val = XEXP (true_prob_val, 0);
595 false_prob_val = GEN_INT (REG_BR_PROB_BASE - INTVAL (true_prob_val));
597 else
598 false_prob_val = NULL_RTX;
600 /* If we have && or || tests, do them here. These tests are in the adjacent
601 blocks after the first block containing the test. */
602 if (ce_info->num_multiple_test_blocks > 0)
604 basic_block bb = test_bb;
605 basic_block last_test_bb = ce_info->last_test_bb;
607 if (! false_expr)
608 goto fail;
612 rtx start, end;
613 rtx t, f;
614 enum rtx_code f_code;
616 bb = block_fallthru (bb);
617 start = first_active_insn (bb);
618 end = last_active_insn (bb, TRUE);
619 if (start
620 && ! cond_exec_process_insns (ce_info, start, end, false_expr,
621 false_prob_val, FALSE))
622 goto fail;
624 /* If the conditional jump is more than just a conditional jump, then
625 we can not do conditional execution conversion on this block. */
626 if (! onlyjump_p (BB_END (bb)))
627 goto fail;
629 /* Find the conditional jump and isolate the test. */
630 t = cond_exec_get_condition (BB_END (bb));
631 if (! t)
632 goto fail;
634 f_code = reversed_comparison_code (t, BB_END (bb));
635 if (f_code == UNKNOWN)
636 goto fail;
638 f = gen_rtx_fmt_ee (f_code, GET_MODE (t), XEXP (t, 0), XEXP (t, 1));
639 if (ce_info->and_and_p)
641 t = gen_rtx_AND (GET_MODE (t), true_expr, t);
642 f = gen_rtx_IOR (GET_MODE (t), false_expr, f);
644 else
646 t = gen_rtx_IOR (GET_MODE (t), true_expr, t);
647 f = gen_rtx_AND (GET_MODE (t), false_expr, f);
650 /* If the machine description needs to modify the tests, such as
651 setting a conditional execution register from a comparison, it can
652 do so here. */
653 #ifdef IFCVT_MODIFY_MULTIPLE_TESTS
654 IFCVT_MODIFY_MULTIPLE_TESTS (ce_info, bb, t, f);
656 /* See if the conversion failed. */
657 if (!t || !f)
658 goto fail;
659 #endif
661 true_expr = t;
662 false_expr = f;
664 while (bb != last_test_bb);
667 /* For IF-THEN-ELSE blocks, we don't allow modifications of the test
668 on then THEN block. */
669 then_mod_ok = (else_bb == NULL_BLOCK);
671 /* Go through the THEN and ELSE blocks converting the insns if possible
672 to conditional execution. */
674 if (then_end
675 && (! false_expr
676 || ! cond_exec_process_insns (ce_info, then_start, then_end,
677 false_expr, false_prob_val,
678 then_mod_ok)))
679 goto fail;
681 if (else_bb && else_end
682 && ! cond_exec_process_insns (ce_info, else_start, else_end,
683 true_expr, true_prob_val, TRUE))
684 goto fail;
686 /* If we cannot apply the changes, fail. Do not go through the normal fail
687 processing, since apply_change_group will call cancel_changes. */
688 if (! apply_change_group ())
690 #ifdef IFCVT_MODIFY_CANCEL
691 /* Cancel any machine dependent changes. */
692 IFCVT_MODIFY_CANCEL (ce_info);
693 #endif
694 return FALSE;
697 #ifdef IFCVT_MODIFY_FINAL
698 /* Do any machine dependent final modifications. */
699 IFCVT_MODIFY_FINAL (ce_info);
700 #endif
702 /* Conversion succeeded. */
703 if (dump_file)
704 fprintf (dump_file, "%d insn%s converted to conditional execution.\n",
705 n_insns, (n_insns == 1) ? " was" : "s were");
707 /* Merge the blocks! If we had matching sequences, make sure to delete one
708 copy at the appropriate location first: delete the copy in the THEN branch
709 for a tail sequence so that the remaining one is executed last for both
710 branches, and delete the copy in the ELSE branch for a head sequence so
711 that the remaining one is executed first for both branches. */
712 if (then_first_tail)
714 rtx from = then_first_tail;
715 if (!INSN_P (from))
716 from = find_active_insn_after (then_bb, from);
717 delete_insn_chain (from, BB_END (then_bb), false);
719 if (else_last_head)
720 delete_insn_chain (first_active_insn (else_bb), else_last_head, false);
722 merge_if_block (ce_info);
723 cond_exec_changed_p = TRUE;
724 return TRUE;
726 fail:
727 #ifdef IFCVT_MODIFY_CANCEL
728 /* Cancel any machine dependent changes. */
729 IFCVT_MODIFY_CANCEL (ce_info);
730 #endif
732 cancel_changes (0);
733 return FALSE;
736 /* Used by noce_process_if_block to communicate with its subroutines.
738 The subroutines know that A and B may be evaluated freely. They
739 know that X is a register. They should insert new instructions
740 before cond_earliest. */
742 struct noce_if_info
744 /* The basic blocks that make up the IF-THEN-{ELSE-,}JOIN block. */
745 basic_block test_bb, then_bb, else_bb, join_bb;
747 /* The jump that ends TEST_BB. */
748 rtx jump;
750 /* The jump condition. */
751 rtx cond;
753 /* New insns should be inserted before this one. */
754 rtx cond_earliest;
756 /* Insns in the THEN and ELSE block. There is always just this
757 one insns in those blocks. The insns are single_set insns.
758 If there was no ELSE block, INSN_B is the last insn before
759 COND_EARLIEST, or NULL_RTX. In the former case, the insn
760 operands are still valid, as if INSN_B was moved down below
761 the jump. */
762 rtx insn_a, insn_b;
764 /* The SET_SRC of INSN_A and INSN_B. */
765 rtx a, b;
767 /* The SET_DEST of INSN_A. */
768 rtx x;
770 /* True if this if block is not canonical. In the canonical form of
771 if blocks, the THEN_BB is the block reached via the fallthru edge
772 from TEST_BB. For the noce transformations, we allow the symmetric
773 form as well. */
774 bool then_else_reversed;
776 /* Estimated cost of the particular branch instruction. */
777 int branch_cost;
780 static rtx noce_emit_store_flag (struct noce_if_info *, rtx, int, int);
781 static int noce_try_move (struct noce_if_info *);
782 static int noce_try_store_flag (struct noce_if_info *);
783 static int noce_try_addcc (struct noce_if_info *);
784 static int noce_try_store_flag_constants (struct noce_if_info *);
785 static int noce_try_store_flag_mask (struct noce_if_info *);
786 static rtx noce_emit_cmove (struct noce_if_info *, rtx, enum rtx_code, rtx,
787 rtx, rtx, rtx);
788 static int noce_try_cmove (struct noce_if_info *);
789 static int noce_try_cmove_arith (struct noce_if_info *);
790 static rtx noce_get_alt_condition (struct noce_if_info *, rtx, rtx *);
791 static int noce_try_minmax (struct noce_if_info *);
792 static int noce_try_abs (struct noce_if_info *);
793 static int noce_try_sign_mask (struct noce_if_info *);
795 /* Helper function for noce_try_store_flag*. */
797 static rtx
798 noce_emit_store_flag (struct noce_if_info *if_info, rtx x, int reversep,
799 int normalize)
801 rtx cond = if_info->cond;
802 int cond_complex;
803 enum rtx_code code;
805 cond_complex = (! general_operand (XEXP (cond, 0), VOIDmode)
806 || ! general_operand (XEXP (cond, 1), VOIDmode));
808 /* If earliest == jump, or when the condition is complex, try to
809 build the store_flag insn directly. */
811 if (cond_complex)
813 rtx set = pc_set (if_info->jump);
814 cond = XEXP (SET_SRC (set), 0);
815 if (GET_CODE (XEXP (SET_SRC (set), 2)) == LABEL_REF
816 && XEXP (XEXP (SET_SRC (set), 2), 0) == JUMP_LABEL (if_info->jump))
817 reversep = !reversep;
818 if (if_info->then_else_reversed)
819 reversep = !reversep;
822 if (reversep)
823 code = reversed_comparison_code (cond, if_info->jump);
824 else
825 code = GET_CODE (cond);
827 if ((if_info->cond_earliest == if_info->jump || cond_complex)
828 && (normalize == 0 || STORE_FLAG_VALUE == normalize))
830 rtx tmp;
832 tmp = gen_rtx_fmt_ee (code, GET_MODE (x), XEXP (cond, 0),
833 XEXP (cond, 1));
834 tmp = gen_rtx_SET (VOIDmode, x, tmp);
836 start_sequence ();
837 tmp = emit_insn (tmp);
839 if (recog_memoized (tmp) >= 0)
841 tmp = get_insns ();
842 end_sequence ();
843 emit_insn (tmp);
845 if_info->cond_earliest = if_info->jump;
847 return x;
850 end_sequence ();
853 /* Don't even try if the comparison operands or the mode of X are weird. */
854 if (cond_complex || !SCALAR_INT_MODE_P (GET_MODE (x)))
855 return NULL_RTX;
857 return emit_store_flag (x, code, XEXP (cond, 0),
858 XEXP (cond, 1), VOIDmode,
859 (code == LTU || code == LEU
860 || code == GEU || code == GTU), normalize);
863 /* Emit instruction to move an rtx, possibly into STRICT_LOW_PART.
864 X is the destination/target and Y is the value to copy. */
866 static void
867 noce_emit_move_insn (rtx x, rtx y)
869 enum machine_mode outmode;
870 rtx outer, inner;
871 int bitpos;
873 if (GET_CODE (x) != STRICT_LOW_PART)
875 rtx seq, insn, target;
876 optab ot;
878 start_sequence ();
879 /* Check that the SET_SRC is reasonable before calling emit_move_insn,
880 otherwise construct a suitable SET pattern ourselves. */
881 insn = (OBJECT_P (y) || CONSTANT_P (y) || GET_CODE (y) == SUBREG)
882 ? emit_move_insn (x, y)
883 : emit_insn (gen_rtx_SET (VOIDmode, x, y));
884 seq = get_insns ();
885 end_sequence ();
887 if (recog_memoized (insn) <= 0)
889 if (GET_CODE (x) == ZERO_EXTRACT)
891 rtx op = XEXP (x, 0);
892 unsigned HOST_WIDE_INT size = INTVAL (XEXP (x, 1));
893 unsigned HOST_WIDE_INT start = INTVAL (XEXP (x, 2));
895 /* store_bit_field expects START to be relative to
896 BYTES_BIG_ENDIAN and adjusts this value for machines with
897 BITS_BIG_ENDIAN != BYTES_BIG_ENDIAN. In order to be able to
898 invoke store_bit_field again it is necessary to have the START
899 value from the first call. */
900 if (BITS_BIG_ENDIAN != BYTES_BIG_ENDIAN)
902 if (MEM_P (op))
903 start = BITS_PER_UNIT - start - size;
904 else
906 gcc_assert (REG_P (op));
907 start = BITS_PER_WORD - start - size;
911 gcc_assert (start < (MEM_P (op) ? BITS_PER_UNIT : BITS_PER_WORD));
912 store_bit_field (op, size, start, 0, 0, GET_MODE (x), y);
913 return;
916 switch (GET_RTX_CLASS (GET_CODE (y)))
918 case RTX_UNARY:
919 ot = code_to_optab (GET_CODE (y));
920 if (ot)
922 start_sequence ();
923 target = expand_unop (GET_MODE (y), ot, XEXP (y, 0), x, 0);
924 if (target != NULL_RTX)
926 if (target != x)
927 emit_move_insn (x, target);
928 seq = get_insns ();
930 end_sequence ();
932 break;
934 case RTX_BIN_ARITH:
935 case RTX_COMM_ARITH:
936 ot = code_to_optab (GET_CODE (y));
937 if (ot)
939 start_sequence ();
940 target = expand_binop (GET_MODE (y), ot,
941 XEXP (y, 0), XEXP (y, 1),
942 x, 0, OPTAB_DIRECT);
943 if (target != NULL_RTX)
945 if (target != x)
946 emit_move_insn (x, target);
947 seq = get_insns ();
949 end_sequence ();
951 break;
953 default:
954 break;
958 emit_insn (seq);
959 return;
962 outer = XEXP (x, 0);
963 inner = XEXP (outer, 0);
964 outmode = GET_MODE (outer);
965 bitpos = SUBREG_BYTE (outer) * BITS_PER_UNIT;
966 store_bit_field (inner, GET_MODE_BITSIZE (outmode), bitpos,
967 0, 0, outmode, y);
970 /* Return sequence of instructions generated by if conversion. This
971 function calls end_sequence() to end the current stream, ensures
972 that are instructions are unshared, recognizable non-jump insns.
973 On failure, this function returns a NULL_RTX. */
975 static rtx
976 end_ifcvt_sequence (struct noce_if_info *if_info)
978 rtx insn;
979 rtx seq = get_insns ();
981 set_used_flags (if_info->x);
982 set_used_flags (if_info->cond);
983 set_used_flags (if_info->a);
984 set_used_flags (if_info->b);
985 unshare_all_rtl_in_chain (seq);
986 end_sequence ();
988 /* Make sure that all of the instructions emitted are recognizable,
989 and that we haven't introduced a new jump instruction.
990 As an exercise for the reader, build a general mechanism that
991 allows proper placement of required clobbers. */
992 for (insn = seq; insn; insn = NEXT_INSN (insn))
993 if (JUMP_P (insn)
994 || recog_memoized (insn) == -1)
995 return NULL_RTX;
997 return seq;
1000 /* Convert "if (a != b) x = a; else x = b" into "x = a" and
1001 "if (a == b) x = a; else x = b" into "x = b". */
1003 static int
1004 noce_try_move (struct noce_if_info *if_info)
1006 rtx cond = if_info->cond;
1007 enum rtx_code code = GET_CODE (cond);
1008 rtx y, seq;
1010 if (code != NE && code != EQ)
1011 return FALSE;
1013 /* This optimization isn't valid if either A or B could be a NaN
1014 or a signed zero. */
1015 if (HONOR_NANS (GET_MODE (if_info->x))
1016 || HONOR_SIGNED_ZEROS (GET_MODE (if_info->x)))
1017 return FALSE;
1019 /* Check whether the operands of the comparison are A and in
1020 either order. */
1021 if ((rtx_equal_p (if_info->a, XEXP (cond, 0))
1022 && rtx_equal_p (if_info->b, XEXP (cond, 1)))
1023 || (rtx_equal_p (if_info->a, XEXP (cond, 1))
1024 && rtx_equal_p (if_info->b, XEXP (cond, 0))))
1026 y = (code == EQ) ? if_info->a : if_info->b;
1028 /* Avoid generating the move if the source is the destination. */
1029 if (! rtx_equal_p (if_info->x, y))
1031 start_sequence ();
1032 noce_emit_move_insn (if_info->x, y);
1033 seq = end_ifcvt_sequence (if_info);
1034 if (!seq)
1035 return FALSE;
1037 emit_insn_before_setloc (seq, if_info->jump,
1038 INSN_LOCATION (if_info->insn_a));
1040 return TRUE;
1042 return FALSE;
1045 /* Convert "if (test) x = 1; else x = 0".
1047 Only try 0 and STORE_FLAG_VALUE here. Other combinations will be
1048 tried in noce_try_store_flag_constants after noce_try_cmove has had
1049 a go at the conversion. */
1051 static int
1052 noce_try_store_flag (struct noce_if_info *if_info)
1054 int reversep;
1055 rtx target, seq;
1057 if (CONST_INT_P (if_info->b)
1058 && INTVAL (if_info->b) == STORE_FLAG_VALUE
1059 && if_info->a == const0_rtx)
1060 reversep = 0;
1061 else if (if_info->b == const0_rtx
1062 && CONST_INT_P (if_info->a)
1063 && INTVAL (if_info->a) == STORE_FLAG_VALUE
1064 && (reversed_comparison_code (if_info->cond, if_info->jump)
1065 != UNKNOWN))
1066 reversep = 1;
1067 else
1068 return FALSE;
1070 start_sequence ();
1072 target = noce_emit_store_flag (if_info, if_info->x, reversep, 0);
1073 if (target)
1075 if (target != if_info->x)
1076 noce_emit_move_insn (if_info->x, target);
1078 seq = end_ifcvt_sequence (if_info);
1079 if (! seq)
1080 return FALSE;
1082 emit_insn_before_setloc (seq, if_info->jump,
1083 INSN_LOCATION (if_info->insn_a));
1084 return TRUE;
1086 else
1088 end_sequence ();
1089 return FALSE;
1093 /* Convert "if (test) x = a; else x = b", for A and B constant. */
1095 static int
1096 noce_try_store_flag_constants (struct noce_if_info *if_info)
1098 rtx target, seq;
1099 int reversep;
1100 HOST_WIDE_INT itrue, ifalse, diff, tmp;
1101 int normalize, can_reverse;
1102 enum machine_mode mode;
1104 if (CONST_INT_P (if_info->a)
1105 && CONST_INT_P (if_info->b))
1107 mode = GET_MODE (if_info->x);
1108 ifalse = INTVAL (if_info->a);
1109 itrue = INTVAL (if_info->b);
1111 /* Make sure we can represent the difference between the two values. */
1112 if ((itrue - ifalse > 0)
1113 != ((ifalse < 0) != (itrue < 0) ? ifalse < 0 : ifalse < itrue))
1114 return FALSE;
1116 diff = trunc_int_for_mode (itrue - ifalse, mode);
1118 can_reverse = (reversed_comparison_code (if_info->cond, if_info->jump)
1119 != UNKNOWN);
1121 reversep = 0;
1122 if (diff == STORE_FLAG_VALUE || diff == -STORE_FLAG_VALUE)
1123 normalize = 0;
1124 else if (ifalse == 0 && exact_log2 (itrue) >= 0
1125 && (STORE_FLAG_VALUE == 1
1126 || if_info->branch_cost >= 2))
1127 normalize = 1;
1128 else if (itrue == 0 && exact_log2 (ifalse) >= 0 && can_reverse
1129 && (STORE_FLAG_VALUE == 1 || if_info->branch_cost >= 2))
1130 normalize = 1, reversep = 1;
1131 else if (itrue == -1
1132 && (STORE_FLAG_VALUE == -1
1133 || if_info->branch_cost >= 2))
1134 normalize = -1;
1135 else if (ifalse == -1 && can_reverse
1136 && (STORE_FLAG_VALUE == -1 || if_info->branch_cost >= 2))
1137 normalize = -1, reversep = 1;
1138 else if ((if_info->branch_cost >= 2 && STORE_FLAG_VALUE == -1)
1139 || if_info->branch_cost >= 3)
1140 normalize = -1;
1141 else
1142 return FALSE;
1144 if (reversep)
1146 tmp = itrue; itrue = ifalse; ifalse = tmp;
1147 diff = trunc_int_for_mode (-diff, mode);
1150 start_sequence ();
1151 target = noce_emit_store_flag (if_info, if_info->x, reversep, normalize);
1152 if (! target)
1154 end_sequence ();
1155 return FALSE;
1158 /* if (test) x = 3; else x = 4;
1159 => x = 3 + (test == 0); */
1160 if (diff == STORE_FLAG_VALUE || diff == -STORE_FLAG_VALUE)
1162 target = expand_simple_binop (mode,
1163 (diff == STORE_FLAG_VALUE
1164 ? PLUS : MINUS),
1165 GEN_INT (ifalse), target, if_info->x, 0,
1166 OPTAB_WIDEN);
1169 /* if (test) x = 8; else x = 0;
1170 => x = (test != 0) << 3; */
1171 else if (ifalse == 0 && (tmp = exact_log2 (itrue)) >= 0)
1173 target = expand_simple_binop (mode, ASHIFT,
1174 target, GEN_INT (tmp), if_info->x, 0,
1175 OPTAB_WIDEN);
1178 /* if (test) x = -1; else x = b;
1179 => x = -(test != 0) | b; */
1180 else if (itrue == -1)
1182 target = expand_simple_binop (mode, IOR,
1183 target, GEN_INT (ifalse), if_info->x, 0,
1184 OPTAB_WIDEN);
1187 /* if (test) x = a; else x = b;
1188 => x = (-(test != 0) & (b - a)) + a; */
1189 else
1191 target = expand_simple_binop (mode, AND,
1192 target, GEN_INT (diff), if_info->x, 0,
1193 OPTAB_WIDEN);
1194 if (target)
1195 target = expand_simple_binop (mode, PLUS,
1196 target, GEN_INT (ifalse),
1197 if_info->x, 0, OPTAB_WIDEN);
1200 if (! target)
1202 end_sequence ();
1203 return FALSE;
1206 if (target != if_info->x)
1207 noce_emit_move_insn (if_info->x, target);
1209 seq = end_ifcvt_sequence (if_info);
1210 if (!seq)
1211 return FALSE;
1213 emit_insn_before_setloc (seq, if_info->jump,
1214 INSN_LOCATION (if_info->insn_a));
1215 return TRUE;
1218 return FALSE;
1221 /* Convert "if (test) foo++" into "foo += (test != 0)", and
1222 similarly for "foo--". */
1224 static int
1225 noce_try_addcc (struct noce_if_info *if_info)
1227 rtx target, seq;
1228 int subtract, normalize;
1230 if (GET_CODE (if_info->a) == PLUS
1231 && rtx_equal_p (XEXP (if_info->a, 0), if_info->b)
1232 && (reversed_comparison_code (if_info->cond, if_info->jump)
1233 != UNKNOWN))
1235 rtx cond = if_info->cond;
1236 enum rtx_code code = reversed_comparison_code (cond, if_info->jump);
1238 /* First try to use addcc pattern. */
1239 if (general_operand (XEXP (cond, 0), VOIDmode)
1240 && general_operand (XEXP (cond, 1), VOIDmode))
1242 start_sequence ();
1243 target = emit_conditional_add (if_info->x, code,
1244 XEXP (cond, 0),
1245 XEXP (cond, 1),
1246 VOIDmode,
1247 if_info->b,
1248 XEXP (if_info->a, 1),
1249 GET_MODE (if_info->x),
1250 (code == LTU || code == GEU
1251 || code == LEU || code == GTU));
1252 if (target)
1254 if (target != if_info->x)
1255 noce_emit_move_insn (if_info->x, target);
1257 seq = end_ifcvt_sequence (if_info);
1258 if (!seq)
1259 return FALSE;
1261 emit_insn_before_setloc (seq, if_info->jump,
1262 INSN_LOCATION (if_info->insn_a));
1263 return TRUE;
1265 end_sequence ();
1268 /* If that fails, construct conditional increment or decrement using
1269 setcc. */
1270 if (if_info->branch_cost >= 2
1271 && (XEXP (if_info->a, 1) == const1_rtx
1272 || XEXP (if_info->a, 1) == constm1_rtx))
1274 start_sequence ();
1275 if (STORE_FLAG_VALUE == INTVAL (XEXP (if_info->a, 1)))
1276 subtract = 0, normalize = 0;
1277 else if (-STORE_FLAG_VALUE == INTVAL (XEXP (if_info->a, 1)))
1278 subtract = 1, normalize = 0;
1279 else
1280 subtract = 0, normalize = INTVAL (XEXP (if_info->a, 1));
1283 target = noce_emit_store_flag (if_info,
1284 gen_reg_rtx (GET_MODE (if_info->x)),
1285 1, normalize);
1287 if (target)
1288 target = expand_simple_binop (GET_MODE (if_info->x),
1289 subtract ? MINUS : PLUS,
1290 if_info->b, target, if_info->x,
1291 0, OPTAB_WIDEN);
1292 if (target)
1294 if (target != if_info->x)
1295 noce_emit_move_insn (if_info->x, target);
1297 seq = end_ifcvt_sequence (if_info);
1298 if (!seq)
1299 return FALSE;
1301 emit_insn_before_setloc (seq, if_info->jump,
1302 INSN_LOCATION (if_info->insn_a));
1303 return TRUE;
1305 end_sequence ();
1309 return FALSE;
1312 /* Convert "if (test) x = 0;" to "x &= -(test == 0);" */
1314 static int
1315 noce_try_store_flag_mask (struct noce_if_info *if_info)
1317 rtx target, seq;
1318 int reversep;
1320 reversep = 0;
1321 if ((if_info->branch_cost >= 2
1322 || STORE_FLAG_VALUE == -1)
1323 && ((if_info->a == const0_rtx
1324 && rtx_equal_p (if_info->b, if_info->x))
1325 || ((reversep = (reversed_comparison_code (if_info->cond,
1326 if_info->jump)
1327 != UNKNOWN))
1328 && if_info->b == const0_rtx
1329 && rtx_equal_p (if_info->a, if_info->x))))
1331 start_sequence ();
1332 target = noce_emit_store_flag (if_info,
1333 gen_reg_rtx (GET_MODE (if_info->x)),
1334 reversep, -1);
1335 if (target)
1336 target = expand_simple_binop (GET_MODE (if_info->x), AND,
1337 if_info->x,
1338 target, if_info->x, 0,
1339 OPTAB_WIDEN);
1341 if (target)
1343 if (target != if_info->x)
1344 noce_emit_move_insn (if_info->x, target);
1346 seq = end_ifcvt_sequence (if_info);
1347 if (!seq)
1348 return FALSE;
1350 emit_insn_before_setloc (seq, if_info->jump,
1351 INSN_LOCATION (if_info->insn_a));
1352 return TRUE;
1355 end_sequence ();
1358 return FALSE;
1361 /* Helper function for noce_try_cmove and noce_try_cmove_arith. */
1363 static rtx
1364 noce_emit_cmove (struct noce_if_info *if_info, rtx x, enum rtx_code code,
1365 rtx cmp_a, rtx cmp_b, rtx vfalse, rtx vtrue)
1367 rtx target ATTRIBUTE_UNUSED;
1368 int unsignedp ATTRIBUTE_UNUSED;
1370 /* If earliest == jump, try to build the cmove insn directly.
1371 This is helpful when combine has created some complex condition
1372 (like for alpha's cmovlbs) that we can't hope to regenerate
1373 through the normal interface. */
1375 if (if_info->cond_earliest == if_info->jump)
1377 rtx tmp;
1379 tmp = gen_rtx_fmt_ee (code, GET_MODE (if_info->cond), cmp_a, cmp_b);
1380 tmp = gen_rtx_IF_THEN_ELSE (GET_MODE (x), tmp, vtrue, vfalse);
1381 tmp = gen_rtx_SET (VOIDmode, x, tmp);
1383 start_sequence ();
1384 tmp = emit_insn (tmp);
1386 if (recog_memoized (tmp) >= 0)
1388 tmp = get_insns ();
1389 end_sequence ();
1390 emit_insn (tmp);
1392 return x;
1395 end_sequence ();
1398 /* Don't even try if the comparison operands are weird. */
1399 if (! general_operand (cmp_a, GET_MODE (cmp_a))
1400 || ! general_operand (cmp_b, GET_MODE (cmp_b)))
1401 return NULL_RTX;
1403 #if HAVE_conditional_move
1404 unsignedp = (code == LTU || code == GEU
1405 || code == LEU || code == GTU);
1407 target = emit_conditional_move (x, code, cmp_a, cmp_b, VOIDmode,
1408 vtrue, vfalse, GET_MODE (x),
1409 unsignedp);
1410 if (target)
1411 return target;
1413 /* We might be faced with a situation like:
1415 x = (reg:M TARGET)
1416 vtrue = (subreg:M (reg:N VTRUE) BYTE)
1417 vfalse = (subreg:M (reg:N VFALSE) BYTE)
1419 We can't do a conditional move in mode M, but it's possible that we
1420 could do a conditional move in mode N instead and take a subreg of
1421 the result.
1423 If we can't create new pseudos, though, don't bother. */
1424 if (reload_completed)
1425 return NULL_RTX;
1427 if (GET_CODE (vtrue) == SUBREG && GET_CODE (vfalse) == SUBREG)
1429 rtx reg_vtrue = SUBREG_REG (vtrue);
1430 rtx reg_vfalse = SUBREG_REG (vfalse);
1431 unsigned int byte_vtrue = SUBREG_BYTE (vtrue);
1432 unsigned int byte_vfalse = SUBREG_BYTE (vfalse);
1433 rtx promoted_target;
1435 if (GET_MODE (reg_vtrue) != GET_MODE (reg_vfalse)
1436 || byte_vtrue != byte_vfalse
1437 || (SUBREG_PROMOTED_VAR_P (vtrue)
1438 != SUBREG_PROMOTED_VAR_P (vfalse))
1439 || (SUBREG_PROMOTED_UNSIGNED_P (vtrue)
1440 != SUBREG_PROMOTED_UNSIGNED_P (vfalse)))
1441 return NULL_RTX;
1443 promoted_target = gen_reg_rtx (GET_MODE (reg_vtrue));
1445 target = emit_conditional_move (promoted_target, code, cmp_a, cmp_b,
1446 VOIDmode, reg_vtrue, reg_vfalse,
1447 GET_MODE (reg_vtrue), unsignedp);
1448 /* Nope, couldn't do it in that mode either. */
1449 if (!target)
1450 return NULL_RTX;
1452 target = gen_rtx_SUBREG (GET_MODE (vtrue), promoted_target, byte_vtrue);
1453 SUBREG_PROMOTED_VAR_P (target) = SUBREG_PROMOTED_VAR_P (vtrue);
1454 SUBREG_PROMOTED_UNSIGNED_SET (target, SUBREG_PROMOTED_UNSIGNED_P (vtrue));
1455 emit_move_insn (x, target);
1456 return x;
1458 else
1459 return NULL_RTX;
1460 #else
1461 /* We'll never get here, as noce_process_if_block doesn't call the
1462 functions involved. Ifdef code, however, should be discouraged
1463 because it leads to typos in the code not selected. However,
1464 emit_conditional_move won't exist either. */
1465 return NULL_RTX;
1466 #endif
1469 /* Try only simple constants and registers here. More complex cases
1470 are handled in noce_try_cmove_arith after noce_try_store_flag_arith
1471 has had a go at it. */
1473 static int
1474 noce_try_cmove (struct noce_if_info *if_info)
1476 enum rtx_code code;
1477 rtx target, seq;
1479 if ((CONSTANT_P (if_info->a) || register_operand (if_info->a, VOIDmode))
1480 && (CONSTANT_P (if_info->b) || register_operand (if_info->b, VOIDmode)))
1482 start_sequence ();
1484 code = GET_CODE (if_info->cond);
1485 target = noce_emit_cmove (if_info, if_info->x, code,
1486 XEXP (if_info->cond, 0),
1487 XEXP (if_info->cond, 1),
1488 if_info->a, if_info->b);
1490 if (target)
1492 if (target != if_info->x)
1493 noce_emit_move_insn (if_info->x, target);
1495 seq = end_ifcvt_sequence (if_info);
1496 if (!seq)
1497 return FALSE;
1499 emit_insn_before_setloc (seq, if_info->jump,
1500 INSN_LOCATION (if_info->insn_a));
1501 return TRUE;
1503 else
1505 end_sequence ();
1506 return FALSE;
1510 return FALSE;
1513 /* Try more complex cases involving conditional_move. */
1515 static int
1516 noce_try_cmove_arith (struct noce_if_info *if_info)
1518 rtx a = if_info->a;
1519 rtx b = if_info->b;
1520 rtx x = if_info->x;
1521 rtx orig_a, orig_b;
1522 rtx insn_a, insn_b;
1523 rtx tmp, target;
1524 int is_mem = 0;
1525 int insn_cost;
1526 enum rtx_code code;
1528 /* A conditional move from two memory sources is equivalent to a
1529 conditional on their addresses followed by a load. Don't do this
1530 early because it'll screw alias analysis. Note that we've
1531 already checked for no side effects. */
1532 /* ??? FIXME: Magic number 5. */
1533 if (cse_not_expected
1534 && MEM_P (a) && MEM_P (b)
1535 && MEM_ADDR_SPACE (a) == MEM_ADDR_SPACE (b)
1536 && if_info->branch_cost >= 5)
1538 enum machine_mode address_mode = get_address_mode (a);
1540 a = XEXP (a, 0);
1541 b = XEXP (b, 0);
1542 x = gen_reg_rtx (address_mode);
1543 is_mem = 1;
1546 /* ??? We could handle this if we knew that a load from A or B could
1547 not trap or fault. This is also true if we've already loaded
1548 from the address along the path from ENTRY. */
1549 else if (may_trap_or_fault_p (a) || may_trap_or_fault_p (b))
1550 return FALSE;
1552 /* if (test) x = a + b; else x = c - d;
1553 => y = a + b;
1554 x = c - d;
1555 if (test)
1556 x = y;
1559 code = GET_CODE (if_info->cond);
1560 insn_a = if_info->insn_a;
1561 insn_b = if_info->insn_b;
1563 /* Total insn_rtx_cost should be smaller than branch cost. Exit
1564 if insn_rtx_cost can't be estimated. */
1565 if (insn_a)
1567 insn_cost
1568 = insn_rtx_cost (PATTERN (insn_a),
1569 optimize_bb_for_speed_p (BLOCK_FOR_INSN (insn_a)));
1570 if (insn_cost == 0 || insn_cost > COSTS_N_INSNS (if_info->branch_cost))
1571 return FALSE;
1573 else
1574 insn_cost = 0;
1576 if (insn_b)
1578 insn_cost
1579 += insn_rtx_cost (PATTERN (insn_b),
1580 optimize_bb_for_speed_p (BLOCK_FOR_INSN (insn_b)));
1581 if (insn_cost == 0 || insn_cost > COSTS_N_INSNS (if_info->branch_cost))
1582 return FALSE;
1585 /* Possibly rearrange operands to make things come out more natural. */
1586 if (reversed_comparison_code (if_info->cond, if_info->jump) != UNKNOWN)
1588 int reversep = 0;
1589 if (rtx_equal_p (b, x))
1590 reversep = 1;
1591 else if (general_operand (b, GET_MODE (b)))
1592 reversep = 1;
1594 if (reversep)
1596 code = reversed_comparison_code (if_info->cond, if_info->jump);
1597 tmp = a, a = b, b = tmp;
1598 tmp = insn_a, insn_a = insn_b, insn_b = tmp;
1602 start_sequence ();
1604 orig_a = a;
1605 orig_b = b;
1607 /* If either operand is complex, load it into a register first.
1608 The best way to do this is to copy the original insn. In this
1609 way we preserve any clobbers etc that the insn may have had.
1610 This is of course not possible in the IS_MEM case. */
1611 if (! general_operand (a, GET_MODE (a)))
1613 rtx set;
1615 if (is_mem)
1617 tmp = gen_reg_rtx (GET_MODE (a));
1618 tmp = emit_insn (gen_rtx_SET (VOIDmode, tmp, a));
1620 else if (! insn_a)
1621 goto end_seq_and_fail;
1622 else
1624 a = gen_reg_rtx (GET_MODE (a));
1625 tmp = copy_rtx (insn_a);
1626 set = single_set (tmp);
1627 SET_DEST (set) = a;
1628 tmp = emit_insn (PATTERN (tmp));
1630 if (recog_memoized (tmp) < 0)
1631 goto end_seq_and_fail;
1633 if (! general_operand (b, GET_MODE (b)))
1635 rtx set, last;
1637 if (is_mem)
1639 tmp = gen_reg_rtx (GET_MODE (b));
1640 tmp = gen_rtx_SET (VOIDmode, tmp, b);
1642 else if (! insn_b)
1643 goto end_seq_and_fail;
1644 else
1646 b = gen_reg_rtx (GET_MODE (b));
1647 tmp = copy_rtx (insn_b);
1648 set = single_set (tmp);
1649 SET_DEST (set) = b;
1650 tmp = PATTERN (tmp);
1653 /* If insn to set up A clobbers any registers B depends on, try to
1654 swap insn that sets up A with the one that sets up B. If even
1655 that doesn't help, punt. */
1656 last = get_last_insn ();
1657 if (last && modified_in_p (orig_b, last))
1659 tmp = emit_insn_before (tmp, get_insns ());
1660 if (modified_in_p (orig_a, tmp))
1661 goto end_seq_and_fail;
1663 else
1664 tmp = emit_insn (tmp);
1666 if (recog_memoized (tmp) < 0)
1667 goto end_seq_and_fail;
1670 target = noce_emit_cmove (if_info, x, code, XEXP (if_info->cond, 0),
1671 XEXP (if_info->cond, 1), a, b);
1673 if (! target)
1674 goto end_seq_and_fail;
1676 /* If we're handling a memory for above, emit the load now. */
1677 if (is_mem)
1679 tmp = gen_rtx_MEM (GET_MODE (if_info->x), target);
1681 /* Copy over flags as appropriate. */
1682 if (MEM_VOLATILE_P (if_info->a) || MEM_VOLATILE_P (if_info->b))
1683 MEM_VOLATILE_P (tmp) = 1;
1684 if (MEM_ALIAS_SET (if_info->a) == MEM_ALIAS_SET (if_info->b))
1685 set_mem_alias_set (tmp, MEM_ALIAS_SET (if_info->a));
1686 set_mem_align (tmp,
1687 MIN (MEM_ALIGN (if_info->a), MEM_ALIGN (if_info->b)));
1689 gcc_assert (MEM_ADDR_SPACE (if_info->a) == MEM_ADDR_SPACE (if_info->b));
1690 set_mem_addr_space (tmp, MEM_ADDR_SPACE (if_info->a));
1692 noce_emit_move_insn (if_info->x, tmp);
1694 else if (target != x)
1695 noce_emit_move_insn (x, target);
1697 tmp = end_ifcvt_sequence (if_info);
1698 if (!tmp)
1699 return FALSE;
1701 emit_insn_before_setloc (tmp, if_info->jump, INSN_LOCATION (if_info->insn_a));
1702 return TRUE;
1704 end_seq_and_fail:
1705 end_sequence ();
1706 return FALSE;
1709 /* For most cases, the simplified condition we found is the best
1710 choice, but this is not the case for the min/max/abs transforms.
1711 For these we wish to know that it is A or B in the condition. */
1713 static rtx
1714 noce_get_alt_condition (struct noce_if_info *if_info, rtx target,
1715 rtx *earliest)
1717 rtx cond, set, insn;
1718 int reverse;
1720 /* If target is already mentioned in the known condition, return it. */
1721 if (reg_mentioned_p (target, if_info->cond))
1723 *earliest = if_info->cond_earliest;
1724 return if_info->cond;
1727 set = pc_set (if_info->jump);
1728 cond = XEXP (SET_SRC (set), 0);
1729 reverse
1730 = GET_CODE (XEXP (SET_SRC (set), 2)) == LABEL_REF
1731 && XEXP (XEXP (SET_SRC (set), 2), 0) == JUMP_LABEL (if_info->jump);
1732 if (if_info->then_else_reversed)
1733 reverse = !reverse;
1735 /* If we're looking for a constant, try to make the conditional
1736 have that constant in it. There are two reasons why it may
1737 not have the constant we want:
1739 1. GCC may have needed to put the constant in a register, because
1740 the target can't compare directly against that constant. For
1741 this case, we look for a SET immediately before the comparison
1742 that puts a constant in that register.
1744 2. GCC may have canonicalized the conditional, for example
1745 replacing "if x < 4" with "if x <= 3". We can undo that (or
1746 make equivalent types of changes) to get the constants we need
1747 if they're off by one in the right direction. */
1749 if (CONST_INT_P (target))
1751 enum rtx_code code = GET_CODE (if_info->cond);
1752 rtx op_a = XEXP (if_info->cond, 0);
1753 rtx op_b = XEXP (if_info->cond, 1);
1754 rtx prev_insn;
1756 /* First, look to see if we put a constant in a register. */
1757 prev_insn = prev_nonnote_insn (if_info->cond_earliest);
1758 if (prev_insn
1759 && BLOCK_FOR_INSN (prev_insn)
1760 == BLOCK_FOR_INSN (if_info->cond_earliest)
1761 && INSN_P (prev_insn)
1762 && GET_CODE (PATTERN (prev_insn)) == SET)
1764 rtx src = find_reg_equal_equiv_note (prev_insn);
1765 if (!src)
1766 src = SET_SRC (PATTERN (prev_insn));
1767 if (CONST_INT_P (src))
1769 if (rtx_equal_p (op_a, SET_DEST (PATTERN (prev_insn))))
1770 op_a = src;
1771 else if (rtx_equal_p (op_b, SET_DEST (PATTERN (prev_insn))))
1772 op_b = src;
1774 if (CONST_INT_P (op_a))
1776 rtx tmp = op_a;
1777 op_a = op_b;
1778 op_b = tmp;
1779 code = swap_condition (code);
1784 /* Now, look to see if we can get the right constant by
1785 adjusting the conditional. */
1786 if (CONST_INT_P (op_b))
1788 HOST_WIDE_INT desired_val = INTVAL (target);
1789 HOST_WIDE_INT actual_val = INTVAL (op_b);
1791 switch (code)
1793 case LT:
1794 if (actual_val == desired_val + 1)
1796 code = LE;
1797 op_b = GEN_INT (desired_val);
1799 break;
1800 case LE:
1801 if (actual_val == desired_val - 1)
1803 code = LT;
1804 op_b = GEN_INT (desired_val);
1806 break;
1807 case GT:
1808 if (actual_val == desired_val - 1)
1810 code = GE;
1811 op_b = GEN_INT (desired_val);
1813 break;
1814 case GE:
1815 if (actual_val == desired_val + 1)
1817 code = GT;
1818 op_b = GEN_INT (desired_val);
1820 break;
1821 default:
1822 break;
1826 /* If we made any changes, generate a new conditional that is
1827 equivalent to what we started with, but has the right
1828 constants in it. */
1829 if (code != GET_CODE (if_info->cond)
1830 || op_a != XEXP (if_info->cond, 0)
1831 || op_b != XEXP (if_info->cond, 1))
1833 cond = gen_rtx_fmt_ee (code, GET_MODE (cond), op_a, op_b);
1834 *earliest = if_info->cond_earliest;
1835 return cond;
1839 cond = canonicalize_condition (if_info->jump, cond, reverse,
1840 earliest, target, false, true);
1841 if (! cond || ! reg_mentioned_p (target, cond))
1842 return NULL;
1844 /* We almost certainly searched back to a different place.
1845 Need to re-verify correct lifetimes. */
1847 /* X may not be mentioned in the range (cond_earliest, jump]. */
1848 for (insn = if_info->jump; insn != *earliest; insn = PREV_INSN (insn))
1849 if (INSN_P (insn) && reg_overlap_mentioned_p (if_info->x, PATTERN (insn)))
1850 return NULL;
1852 /* A and B may not be modified in the range [cond_earliest, jump). */
1853 for (insn = *earliest; insn != if_info->jump; insn = NEXT_INSN (insn))
1854 if (INSN_P (insn)
1855 && (modified_in_p (if_info->a, insn)
1856 || modified_in_p (if_info->b, insn)))
1857 return NULL;
1859 return cond;
1862 /* Convert "if (a < b) x = a; else x = b;" to "x = min(a, b);", etc. */
1864 static int
1865 noce_try_minmax (struct noce_if_info *if_info)
1867 rtx cond, earliest, target, seq;
1868 enum rtx_code code, op;
1869 int unsignedp;
1871 /* ??? Reject modes with NaNs or signed zeros since we don't know how
1872 they will be resolved with an SMIN/SMAX. It wouldn't be too hard
1873 to get the target to tell us... */
1874 if (HONOR_SIGNED_ZEROS (GET_MODE (if_info->x))
1875 || HONOR_NANS (GET_MODE (if_info->x)))
1876 return FALSE;
1878 cond = noce_get_alt_condition (if_info, if_info->a, &earliest);
1879 if (!cond)
1880 return FALSE;
1882 /* Verify the condition is of the form we expect, and canonicalize
1883 the comparison code. */
1884 code = GET_CODE (cond);
1885 if (rtx_equal_p (XEXP (cond, 0), if_info->a))
1887 if (! rtx_equal_p (XEXP (cond, 1), if_info->b))
1888 return FALSE;
1890 else if (rtx_equal_p (XEXP (cond, 1), if_info->a))
1892 if (! rtx_equal_p (XEXP (cond, 0), if_info->b))
1893 return FALSE;
1894 code = swap_condition (code);
1896 else
1897 return FALSE;
1899 /* Determine what sort of operation this is. Note that the code is for
1900 a taken branch, so the code->operation mapping appears backwards. */
1901 switch (code)
1903 case LT:
1904 case LE:
1905 case UNLT:
1906 case UNLE:
1907 op = SMAX;
1908 unsignedp = 0;
1909 break;
1910 case GT:
1911 case GE:
1912 case UNGT:
1913 case UNGE:
1914 op = SMIN;
1915 unsignedp = 0;
1916 break;
1917 case LTU:
1918 case LEU:
1919 op = UMAX;
1920 unsignedp = 1;
1921 break;
1922 case GTU:
1923 case GEU:
1924 op = UMIN;
1925 unsignedp = 1;
1926 break;
1927 default:
1928 return FALSE;
1931 start_sequence ();
1933 target = expand_simple_binop (GET_MODE (if_info->x), op,
1934 if_info->a, if_info->b,
1935 if_info->x, unsignedp, OPTAB_WIDEN);
1936 if (! target)
1938 end_sequence ();
1939 return FALSE;
1941 if (target != if_info->x)
1942 noce_emit_move_insn (if_info->x, target);
1944 seq = end_ifcvt_sequence (if_info);
1945 if (!seq)
1946 return FALSE;
1948 emit_insn_before_setloc (seq, if_info->jump, INSN_LOCATION (if_info->insn_a));
1949 if_info->cond = cond;
1950 if_info->cond_earliest = earliest;
1952 return TRUE;
1955 /* Convert "if (a < 0) x = -a; else x = a;" to "x = abs(a);",
1956 "if (a < 0) x = ~a; else x = a;" to "x = one_cmpl_abs(a);",
1957 etc. */
1959 static int
1960 noce_try_abs (struct noce_if_info *if_info)
1962 rtx cond, earliest, target, seq, a, b, c;
1963 int negate;
1964 bool one_cmpl = false;
1966 /* Reject modes with signed zeros. */
1967 if (HONOR_SIGNED_ZEROS (GET_MODE (if_info->x)))
1968 return FALSE;
1970 /* Recognize A and B as constituting an ABS or NABS. The canonical
1971 form is a branch around the negation, taken when the object is the
1972 first operand of a comparison against 0 that evaluates to true. */
1973 a = if_info->a;
1974 b = if_info->b;
1975 if (GET_CODE (a) == NEG && rtx_equal_p (XEXP (a, 0), b))
1976 negate = 0;
1977 else if (GET_CODE (b) == NEG && rtx_equal_p (XEXP (b, 0), a))
1979 c = a; a = b; b = c;
1980 negate = 1;
1982 else if (GET_CODE (a) == NOT && rtx_equal_p (XEXP (a, 0), b))
1984 negate = 0;
1985 one_cmpl = true;
1987 else if (GET_CODE (b) == NOT && rtx_equal_p (XEXP (b, 0), a))
1989 c = a; a = b; b = c;
1990 negate = 1;
1991 one_cmpl = true;
1993 else
1994 return FALSE;
1996 cond = noce_get_alt_condition (if_info, b, &earliest);
1997 if (!cond)
1998 return FALSE;
2000 /* Verify the condition is of the form we expect. */
2001 if (rtx_equal_p (XEXP (cond, 0), b))
2002 c = XEXP (cond, 1);
2003 else if (rtx_equal_p (XEXP (cond, 1), b))
2005 c = XEXP (cond, 0);
2006 negate = !negate;
2008 else
2009 return FALSE;
2011 /* Verify that C is zero. Search one step backward for a
2012 REG_EQUAL note or a simple source if necessary. */
2013 if (REG_P (c))
2015 rtx set, insn = prev_nonnote_insn (earliest);
2016 if (insn
2017 && BLOCK_FOR_INSN (insn) == BLOCK_FOR_INSN (earliest)
2018 && (set = single_set (insn))
2019 && rtx_equal_p (SET_DEST (set), c))
2021 rtx note = find_reg_equal_equiv_note (insn);
2022 if (note)
2023 c = XEXP (note, 0);
2024 else
2025 c = SET_SRC (set);
2027 else
2028 return FALSE;
2030 if (MEM_P (c)
2031 && GET_CODE (XEXP (c, 0)) == SYMBOL_REF
2032 && CONSTANT_POOL_ADDRESS_P (XEXP (c, 0)))
2033 c = get_pool_constant (XEXP (c, 0));
2035 /* Work around funny ideas get_condition has wrt canonicalization.
2036 Note that these rtx constants are known to be CONST_INT, and
2037 therefore imply integer comparisons. */
2038 if (c == constm1_rtx && GET_CODE (cond) == GT)
2040 else if (c == const1_rtx && GET_CODE (cond) == LT)
2042 else if (c != CONST0_RTX (GET_MODE (b)))
2043 return FALSE;
2045 /* Determine what sort of operation this is. */
2046 switch (GET_CODE (cond))
2048 case LT:
2049 case LE:
2050 case UNLT:
2051 case UNLE:
2052 negate = !negate;
2053 break;
2054 case GT:
2055 case GE:
2056 case UNGT:
2057 case UNGE:
2058 break;
2059 default:
2060 return FALSE;
2063 start_sequence ();
2064 if (one_cmpl)
2065 target = expand_one_cmpl_abs_nojump (GET_MODE (if_info->x), b,
2066 if_info->x);
2067 else
2068 target = expand_abs_nojump (GET_MODE (if_info->x), b, if_info->x, 1);
2070 /* ??? It's a quandary whether cmove would be better here, especially
2071 for integers. Perhaps combine will clean things up. */
2072 if (target && negate)
2074 if (one_cmpl)
2075 target = expand_simple_unop (GET_MODE (target), NOT, target,
2076 if_info->x, 0);
2077 else
2078 target = expand_simple_unop (GET_MODE (target), NEG, target,
2079 if_info->x, 0);
2082 if (! target)
2084 end_sequence ();
2085 return FALSE;
2088 if (target != if_info->x)
2089 noce_emit_move_insn (if_info->x, target);
2091 seq = end_ifcvt_sequence (if_info);
2092 if (!seq)
2093 return FALSE;
2095 emit_insn_before_setloc (seq, if_info->jump, INSN_LOCATION (if_info->insn_a));
2096 if_info->cond = cond;
2097 if_info->cond_earliest = earliest;
2099 return TRUE;
2102 /* Convert "if (m < 0) x = b; else x = 0;" to "x = (m >> C) & b;". */
2104 static int
2105 noce_try_sign_mask (struct noce_if_info *if_info)
2107 rtx cond, t, m, c, seq;
2108 enum machine_mode mode;
2109 enum rtx_code code;
2110 bool t_unconditional;
2112 cond = if_info->cond;
2113 code = GET_CODE (cond);
2114 m = XEXP (cond, 0);
2115 c = XEXP (cond, 1);
2117 t = NULL_RTX;
2118 if (if_info->a == const0_rtx)
2120 if ((code == LT && c == const0_rtx)
2121 || (code == LE && c == constm1_rtx))
2122 t = if_info->b;
2124 else if (if_info->b == const0_rtx)
2126 if ((code == GE && c == const0_rtx)
2127 || (code == GT && c == constm1_rtx))
2128 t = if_info->a;
2131 if (! t || side_effects_p (t))
2132 return FALSE;
2134 /* We currently don't handle different modes. */
2135 mode = GET_MODE (t);
2136 if (GET_MODE (m) != mode)
2137 return FALSE;
2139 /* This is only profitable if T is unconditionally executed/evaluated in the
2140 original insn sequence or T is cheap. The former happens if B is the
2141 non-zero (T) value and if INSN_B was taken from TEST_BB, or there was no
2142 INSN_B which can happen for e.g. conditional stores to memory. For the
2143 cost computation use the block TEST_BB where the evaluation will end up
2144 after the transformation. */
2145 t_unconditional =
2146 (t == if_info->b
2147 && (if_info->insn_b == NULL_RTX
2148 || BLOCK_FOR_INSN (if_info->insn_b) == if_info->test_bb));
2149 if (!(t_unconditional
2150 || (set_src_cost (t, optimize_bb_for_speed_p (if_info->test_bb))
2151 < COSTS_N_INSNS (2))))
2152 return FALSE;
2154 start_sequence ();
2155 /* Use emit_store_flag to generate "m < 0 ? -1 : 0" instead of expanding
2156 "(signed) m >> 31" directly. This benefits targets with specialized
2157 insns to obtain the signmask, but still uses ashr_optab otherwise. */
2158 m = emit_store_flag (gen_reg_rtx (mode), LT, m, const0_rtx, mode, 0, -1);
2159 t = m ? expand_binop (mode, and_optab, m, t, NULL_RTX, 0, OPTAB_DIRECT)
2160 : NULL_RTX;
2162 if (!t)
2164 end_sequence ();
2165 return FALSE;
2168 noce_emit_move_insn (if_info->x, t);
2170 seq = end_ifcvt_sequence (if_info);
2171 if (!seq)
2172 return FALSE;
2174 emit_insn_before_setloc (seq, if_info->jump, INSN_LOCATION (if_info->insn_a));
2175 return TRUE;
2179 /* Optimize away "if (x & C) x |= C" and similar bit manipulation
2180 transformations. */
2182 static int
2183 noce_try_bitop (struct noce_if_info *if_info)
2185 rtx cond, x, a, result, seq;
2186 enum machine_mode mode;
2187 enum rtx_code code;
2188 int bitnum;
2190 x = if_info->x;
2191 cond = if_info->cond;
2192 code = GET_CODE (cond);
2194 /* Check for no else condition. */
2195 if (! rtx_equal_p (x, if_info->b))
2196 return FALSE;
2198 /* Check for a suitable condition. */
2199 if (code != NE && code != EQ)
2200 return FALSE;
2201 if (XEXP (cond, 1) != const0_rtx)
2202 return FALSE;
2203 cond = XEXP (cond, 0);
2205 /* ??? We could also handle AND here. */
2206 if (GET_CODE (cond) == ZERO_EXTRACT)
2208 if (XEXP (cond, 1) != const1_rtx
2209 || !CONST_INT_P (XEXP (cond, 2))
2210 || ! rtx_equal_p (x, XEXP (cond, 0)))
2211 return FALSE;
2212 bitnum = INTVAL (XEXP (cond, 2));
2213 mode = GET_MODE (x);
2214 if (BITS_BIG_ENDIAN)
2215 bitnum = GET_MODE_BITSIZE (mode) - 1 - bitnum;
2216 if (bitnum < 0 || bitnum >= HOST_BITS_PER_WIDE_INT)
2217 return FALSE;
2219 else
2220 return FALSE;
2222 a = if_info->a;
2223 if (GET_CODE (a) == IOR || GET_CODE (a) == XOR)
2225 /* Check for "if (X & C) x = x op C". */
2226 if (! rtx_equal_p (x, XEXP (a, 0))
2227 || !CONST_INT_P (XEXP (a, 1))
2228 || (INTVAL (XEXP (a, 1)) & GET_MODE_MASK (mode))
2229 != (unsigned HOST_WIDE_INT) 1 << bitnum)
2230 return FALSE;
2232 /* if ((x & C) == 0) x |= C; is transformed to x |= C. */
2233 /* if ((x & C) != 0) x |= C; is transformed to nothing. */
2234 if (GET_CODE (a) == IOR)
2235 result = (code == NE) ? a : NULL_RTX;
2236 else if (code == NE)
2238 /* if ((x & C) == 0) x ^= C; is transformed to x |= C. */
2239 result = gen_int_mode ((HOST_WIDE_INT) 1 << bitnum, mode);
2240 result = simplify_gen_binary (IOR, mode, x, result);
2242 else
2244 /* if ((x & C) != 0) x ^= C; is transformed to x &= ~C. */
2245 result = gen_int_mode (~((HOST_WIDE_INT) 1 << bitnum), mode);
2246 result = simplify_gen_binary (AND, mode, x, result);
2249 else if (GET_CODE (a) == AND)
2251 /* Check for "if (X & C) x &= ~C". */
2252 if (! rtx_equal_p (x, XEXP (a, 0))
2253 || !CONST_INT_P (XEXP (a, 1))
2254 || (INTVAL (XEXP (a, 1)) & GET_MODE_MASK (mode))
2255 != (~((HOST_WIDE_INT) 1 << bitnum) & GET_MODE_MASK (mode)))
2256 return FALSE;
2258 /* if ((x & C) == 0) x &= ~C; is transformed to nothing. */
2259 /* if ((x & C) != 0) x &= ~C; is transformed to x &= ~C. */
2260 result = (code == EQ) ? a : NULL_RTX;
2262 else
2263 return FALSE;
2265 if (result)
2267 start_sequence ();
2268 noce_emit_move_insn (x, result);
2269 seq = end_ifcvt_sequence (if_info);
2270 if (!seq)
2271 return FALSE;
2273 emit_insn_before_setloc (seq, if_info->jump,
2274 INSN_LOCATION (if_info->insn_a));
2276 return TRUE;
2280 /* Similar to get_condition, only the resulting condition must be
2281 valid at JUMP, instead of at EARLIEST.
2283 If THEN_ELSE_REVERSED is true, the fallthrough does not go to the
2284 THEN block of the caller, and we have to reverse the condition. */
2286 static rtx
2287 noce_get_condition (rtx jump, rtx *earliest, bool then_else_reversed)
2289 rtx cond, set, tmp;
2290 bool reverse;
2292 if (! any_condjump_p (jump))
2293 return NULL_RTX;
2295 set = pc_set (jump);
2297 /* If this branches to JUMP_LABEL when the condition is false,
2298 reverse the condition. */
2299 reverse = (GET_CODE (XEXP (SET_SRC (set), 2)) == LABEL_REF
2300 && XEXP (XEXP (SET_SRC (set), 2), 0) == JUMP_LABEL (jump));
2302 /* We may have to reverse because the caller's if block is not canonical,
2303 i.e. the THEN block isn't the fallthrough block for the TEST block
2304 (see find_if_header). */
2305 if (then_else_reversed)
2306 reverse = !reverse;
2308 /* If the condition variable is a register and is MODE_INT, accept it. */
2310 cond = XEXP (SET_SRC (set), 0);
2311 tmp = XEXP (cond, 0);
2312 if (REG_P (tmp) && GET_MODE_CLASS (GET_MODE (tmp)) == MODE_INT
2313 && (GET_MODE (tmp) != BImode
2314 || !targetm.small_register_classes_for_mode_p (BImode)))
2316 *earliest = jump;
2318 if (reverse)
2319 cond = gen_rtx_fmt_ee (reverse_condition (GET_CODE (cond)),
2320 GET_MODE (cond), tmp, XEXP (cond, 1));
2321 return cond;
2324 /* Otherwise, fall back on canonicalize_condition to do the dirty
2325 work of manipulating MODE_CC values and COMPARE rtx codes. */
2326 tmp = canonicalize_condition (jump, cond, reverse, earliest,
2327 NULL_RTX, false, true);
2329 /* We don't handle side-effects in the condition, like handling
2330 REG_INC notes and making sure no duplicate conditions are emitted. */
2331 if (tmp != NULL_RTX && side_effects_p (tmp))
2332 return NULL_RTX;
2334 return tmp;
2337 /* Return true if OP is ok for if-then-else processing. */
2339 static int
2340 noce_operand_ok (const_rtx op)
2342 if (side_effects_p (op))
2343 return FALSE;
2345 /* We special-case memories, so handle any of them with
2346 no address side effects. */
2347 if (MEM_P (op))
2348 return ! side_effects_p (XEXP (op, 0));
2350 return ! may_trap_p (op);
2353 /* Return true if a write into MEM may trap or fault. */
2355 static bool
2356 noce_mem_write_may_trap_or_fault_p (const_rtx mem)
2358 rtx addr;
2360 if (MEM_READONLY_P (mem))
2361 return true;
2363 if (may_trap_or_fault_p (mem))
2364 return true;
2366 addr = XEXP (mem, 0);
2368 /* Call target hook to avoid the effects of -fpic etc.... */
2369 addr = targetm.delegitimize_address (addr);
2371 while (addr)
2372 switch (GET_CODE (addr))
2374 case CONST:
2375 case PRE_DEC:
2376 case PRE_INC:
2377 case POST_DEC:
2378 case POST_INC:
2379 case POST_MODIFY:
2380 addr = XEXP (addr, 0);
2381 break;
2382 case LO_SUM:
2383 case PRE_MODIFY:
2384 addr = XEXP (addr, 1);
2385 break;
2386 case PLUS:
2387 if (CONST_INT_P (XEXP (addr, 1)))
2388 addr = XEXP (addr, 0);
2389 else
2390 return false;
2391 break;
2392 case LABEL_REF:
2393 return true;
2394 case SYMBOL_REF:
2395 if (SYMBOL_REF_DECL (addr)
2396 && decl_readonly_section (SYMBOL_REF_DECL (addr), 0))
2397 return true;
2398 return false;
2399 default:
2400 return false;
2403 return false;
2406 /* Return whether we can use store speculation for MEM. TOP_BB is the
2407 basic block above the conditional block where we are considering
2408 doing the speculative store. We look for whether MEM is set
2409 unconditionally later in the function. */
2411 static bool
2412 noce_can_store_speculate_p (basic_block top_bb, const_rtx mem)
2414 basic_block dominator;
2416 for (dominator = get_immediate_dominator (CDI_POST_DOMINATORS, top_bb);
2417 dominator != NULL;
2418 dominator = get_immediate_dominator (CDI_POST_DOMINATORS, dominator))
2420 rtx insn;
2422 FOR_BB_INSNS (dominator, insn)
2424 /* If we see something that might be a memory barrier, we
2425 have to stop looking. Even if the MEM is set later in
2426 the function, we still don't want to set it
2427 unconditionally before the barrier. */
2428 if (INSN_P (insn)
2429 && (volatile_insn_p (PATTERN (insn))
2430 || (CALL_P (insn) && (!RTL_CONST_CALL_P (insn)))))
2431 return false;
2433 if (memory_must_be_modified_in_insn_p (mem, insn))
2434 return true;
2435 if (modified_in_p (XEXP (mem, 0), insn))
2436 return false;
2441 return false;
2444 /* Given a simple IF-THEN-JOIN or IF-THEN-ELSE-JOIN block, attempt to convert
2445 it without using conditional execution. Return TRUE if we were successful
2446 at converting the block. */
2448 static int
2449 noce_process_if_block (struct noce_if_info *if_info)
2451 basic_block test_bb = if_info->test_bb; /* test block */
2452 basic_block then_bb = if_info->then_bb; /* THEN */
2453 basic_block else_bb = if_info->else_bb; /* ELSE or NULL */
2454 basic_block join_bb = if_info->join_bb; /* JOIN */
2455 rtx jump = if_info->jump;
2456 rtx cond = if_info->cond;
2457 rtx insn_a, insn_b;
2458 rtx set_a, set_b;
2459 rtx orig_x, x, a, b;
2461 /* We're looking for patterns of the form
2463 (1) if (...) x = a; else x = b;
2464 (2) x = b; if (...) x = a;
2465 (3) if (...) x = a; // as if with an initial x = x.
2467 The later patterns require jumps to be more expensive.
2469 ??? For future expansion, look for multiple X in such patterns. */
2471 /* Look for one of the potential sets. */
2472 insn_a = first_active_insn (then_bb);
2473 if (! insn_a
2474 || insn_a != last_active_insn (then_bb, FALSE)
2475 || (set_a = single_set (insn_a)) == NULL_RTX)
2476 return FALSE;
2478 x = SET_DEST (set_a);
2479 a = SET_SRC (set_a);
2481 /* Look for the other potential set. Make sure we've got equivalent
2482 destinations. */
2483 /* ??? This is overconservative. Storing to two different mems is
2484 as easy as conditionally computing the address. Storing to a
2485 single mem merely requires a scratch memory to use as one of the
2486 destination addresses; often the memory immediately below the
2487 stack pointer is available for this. */
2488 set_b = NULL_RTX;
2489 if (else_bb)
2491 insn_b = first_active_insn (else_bb);
2492 if (! insn_b
2493 || insn_b != last_active_insn (else_bb, FALSE)
2494 || (set_b = single_set (insn_b)) == NULL_RTX
2495 || ! rtx_equal_p (x, SET_DEST (set_b)))
2496 return FALSE;
2498 else
2500 insn_b = prev_nonnote_nondebug_insn (if_info->cond_earliest);
2501 /* We're going to be moving the evaluation of B down from above
2502 COND_EARLIEST to JUMP. Make sure the relevant data is still
2503 intact. */
2504 if (! insn_b
2505 || BLOCK_FOR_INSN (insn_b) != BLOCK_FOR_INSN (if_info->cond_earliest)
2506 || !NONJUMP_INSN_P (insn_b)
2507 || (set_b = single_set (insn_b)) == NULL_RTX
2508 || ! rtx_equal_p (x, SET_DEST (set_b))
2509 || ! noce_operand_ok (SET_SRC (set_b))
2510 || reg_overlap_mentioned_p (x, SET_SRC (set_b))
2511 || modified_between_p (SET_SRC (set_b), insn_b, jump)
2512 /* Avoid extending the lifetime of hard registers on small
2513 register class machines. */
2514 || (REG_P (SET_SRC (set_b))
2515 && HARD_REGISTER_P (SET_SRC (set_b))
2516 && targetm.small_register_classes_for_mode_p
2517 (GET_MODE (SET_SRC (set_b))))
2518 /* Likewise with X. In particular this can happen when
2519 noce_get_condition looks farther back in the instruction
2520 stream than one might expect. */
2521 || reg_overlap_mentioned_p (x, cond)
2522 || reg_overlap_mentioned_p (x, a)
2523 || modified_between_p (x, insn_b, jump))
2524 insn_b = set_b = NULL_RTX;
2527 /* If x has side effects then only the if-then-else form is safe to
2528 convert. But even in that case we would need to restore any notes
2529 (such as REG_INC) at then end. That can be tricky if
2530 noce_emit_move_insn expands to more than one insn, so disable the
2531 optimization entirely for now if there are side effects. */
2532 if (side_effects_p (x))
2533 return FALSE;
2535 b = (set_b ? SET_SRC (set_b) : x);
2537 /* Only operate on register destinations, and even then avoid extending
2538 the lifetime of hard registers on small register class machines. */
2539 orig_x = x;
2540 if (!REG_P (x)
2541 || (HARD_REGISTER_P (x)
2542 && targetm.small_register_classes_for_mode_p (GET_MODE (x))))
2544 if (GET_MODE (x) == BLKmode)
2545 return FALSE;
2547 if (GET_CODE (x) == ZERO_EXTRACT
2548 && (!CONST_INT_P (XEXP (x, 1))
2549 || !CONST_INT_P (XEXP (x, 2))))
2550 return FALSE;
2552 x = gen_reg_rtx (GET_MODE (GET_CODE (x) == STRICT_LOW_PART
2553 ? XEXP (x, 0) : x));
2556 /* Don't operate on sources that may trap or are volatile. */
2557 if (! noce_operand_ok (a) || ! noce_operand_ok (b))
2558 return FALSE;
2560 retry:
2561 /* Set up the info block for our subroutines. */
2562 if_info->insn_a = insn_a;
2563 if_info->insn_b = insn_b;
2564 if_info->x = x;
2565 if_info->a = a;
2566 if_info->b = b;
2568 /* Try optimizations in some approximation of a useful order. */
2569 /* ??? Should first look to see if X is live incoming at all. If it
2570 isn't, we don't need anything but an unconditional set. */
2572 /* Look and see if A and B are really the same. Avoid creating silly
2573 cmove constructs that no one will fix up later. */
2574 if (rtx_equal_p (a, b))
2576 /* If we have an INSN_B, we don't have to create any new rtl. Just
2577 move the instruction that we already have. If we don't have an
2578 INSN_B, that means that A == X, and we've got a noop move. In
2579 that case don't do anything and let the code below delete INSN_A. */
2580 if (insn_b && else_bb)
2582 rtx note;
2584 if (else_bb && insn_b == BB_END (else_bb))
2585 BB_END (else_bb) = PREV_INSN (insn_b);
2586 reorder_insns (insn_b, insn_b, PREV_INSN (jump));
2588 /* If there was a REG_EQUAL note, delete it since it may have been
2589 true due to this insn being after a jump. */
2590 if ((note = find_reg_note (insn_b, REG_EQUAL, NULL_RTX)) != 0)
2591 remove_note (insn_b, note);
2593 insn_b = NULL_RTX;
2595 /* If we have "x = b; if (...) x = a;", and x has side-effects, then
2596 x must be executed twice. */
2597 else if (insn_b && side_effects_p (orig_x))
2598 return FALSE;
2600 x = orig_x;
2601 goto success;
2604 if (!set_b && MEM_P (orig_x))
2606 /* Disallow the "if (...) x = a;" form (implicit "else x = x;")
2607 for optimizations if writing to x may trap or fault,
2608 i.e. it's a memory other than a static var or a stack slot,
2609 is misaligned on strict aligned machines or is read-only. If
2610 x is a read-only memory, then the program is valid only if we
2611 avoid the store into it. If there are stores on both the
2612 THEN and ELSE arms, then we can go ahead with the conversion;
2613 either the program is broken, or the condition is always
2614 false such that the other memory is selected. */
2615 if (noce_mem_write_may_trap_or_fault_p (orig_x))
2616 return FALSE;
2618 /* Avoid store speculation: given "if (...) x = a" where x is a
2619 MEM, we only want to do the store if x is always set
2620 somewhere in the function. This avoids cases like
2621 if (pthread_mutex_trylock(mutex))
2622 ++global_variable;
2623 where we only want global_variable to be changed if the mutex
2624 is held. FIXME: This should ideally be expressed directly in
2625 RTL somehow. */
2626 if (!noce_can_store_speculate_p (test_bb, orig_x))
2627 return FALSE;
2630 if (noce_try_move (if_info))
2631 goto success;
2632 if (noce_try_store_flag (if_info))
2633 goto success;
2634 if (noce_try_bitop (if_info))
2635 goto success;
2636 if (noce_try_minmax (if_info))
2637 goto success;
2638 if (noce_try_abs (if_info))
2639 goto success;
2640 if (HAVE_conditional_move
2641 && noce_try_cmove (if_info))
2642 goto success;
2643 if (! targetm.have_conditional_execution ())
2645 if (noce_try_store_flag_constants (if_info))
2646 goto success;
2647 if (noce_try_addcc (if_info))
2648 goto success;
2649 if (noce_try_store_flag_mask (if_info))
2650 goto success;
2651 if (HAVE_conditional_move
2652 && noce_try_cmove_arith (if_info))
2653 goto success;
2654 if (noce_try_sign_mask (if_info))
2655 goto success;
2658 if (!else_bb && set_b)
2660 insn_b = set_b = NULL_RTX;
2661 b = orig_x;
2662 goto retry;
2665 return FALSE;
2667 success:
2669 /* If we used a temporary, fix it up now. */
2670 if (orig_x != x)
2672 rtx seq;
2674 start_sequence ();
2675 noce_emit_move_insn (orig_x, x);
2676 seq = get_insns ();
2677 set_used_flags (orig_x);
2678 unshare_all_rtl_in_chain (seq);
2679 end_sequence ();
2681 emit_insn_before_setloc (seq, BB_END (test_bb), INSN_LOCATION (insn_a));
2684 /* The original THEN and ELSE blocks may now be removed. The test block
2685 must now jump to the join block. If the test block and the join block
2686 can be merged, do so. */
2687 if (else_bb)
2689 delete_basic_block (else_bb);
2690 num_true_changes++;
2692 else
2693 remove_edge (find_edge (test_bb, join_bb));
2695 remove_edge (find_edge (then_bb, join_bb));
2696 redirect_edge_and_branch_force (single_succ_edge (test_bb), join_bb);
2697 delete_basic_block (then_bb);
2698 num_true_changes++;
2700 if (can_merge_blocks_p (test_bb, join_bb))
2702 merge_blocks (test_bb, join_bb);
2703 num_true_changes++;
2706 num_updated_if_blocks++;
2707 return TRUE;
2710 /* Check whether a block is suitable for conditional move conversion.
2711 Every insn must be a simple set of a register to a constant or a
2712 register. For each assignment, store the value in the pointer map
2713 VALS, keyed indexed by register pointer, then store the register
2714 pointer in REGS. COND is the condition we will test. */
2716 static int
2717 check_cond_move_block (basic_block bb,
2718 struct pointer_map_t *vals,
2719 vec<rtx> *regs,
2720 rtx cond)
2722 rtx insn;
2724 /* We can only handle simple jumps at the end of the basic block.
2725 It is almost impossible to update the CFG otherwise. */
2726 insn = BB_END (bb);
2727 if (JUMP_P (insn) && !onlyjump_p (insn))
2728 return FALSE;
2730 FOR_BB_INSNS (bb, insn)
2732 rtx set, dest, src;
2733 void **slot;
2735 if (!NONDEBUG_INSN_P (insn) || JUMP_P (insn))
2736 continue;
2737 set = single_set (insn);
2738 if (!set)
2739 return FALSE;
2741 dest = SET_DEST (set);
2742 src = SET_SRC (set);
2743 if (!REG_P (dest)
2744 || (HARD_REGISTER_P (dest)
2745 && targetm.small_register_classes_for_mode_p (GET_MODE (dest))))
2746 return FALSE;
2748 if (!CONSTANT_P (src) && !register_operand (src, VOIDmode))
2749 return FALSE;
2751 if (side_effects_p (src) || side_effects_p (dest))
2752 return FALSE;
2754 if (may_trap_p (src) || may_trap_p (dest))
2755 return FALSE;
2757 /* Don't try to handle this if the source register was
2758 modified earlier in the block. */
2759 if ((REG_P (src)
2760 && pointer_map_contains (vals, src))
2761 || (GET_CODE (src) == SUBREG && REG_P (SUBREG_REG (src))
2762 && pointer_map_contains (vals, SUBREG_REG (src))))
2763 return FALSE;
2765 /* Don't try to handle this if the destination register was
2766 modified earlier in the block. */
2767 if (pointer_map_contains (vals, dest))
2768 return FALSE;
2770 /* Don't try to handle this if the condition uses the
2771 destination register. */
2772 if (reg_overlap_mentioned_p (dest, cond))
2773 return FALSE;
2775 /* Don't try to handle this if the source register is modified
2776 later in the block. */
2777 if (!CONSTANT_P (src)
2778 && modified_between_p (src, insn, NEXT_INSN (BB_END (bb))))
2779 return FALSE;
2781 slot = pointer_map_insert (vals, (void *) dest);
2782 *slot = (void *) src;
2784 regs->safe_push (dest);
2787 return TRUE;
2790 /* Given a basic block BB suitable for conditional move conversion,
2791 a condition COND, and pointer maps THEN_VALS and ELSE_VALS containing
2792 the register values depending on COND, emit the insns in the block as
2793 conditional moves. If ELSE_BLOCK is true, THEN_BB was already
2794 processed. The caller has started a sequence for the conversion.
2795 Return true if successful, false if something goes wrong. */
2797 static bool
2798 cond_move_convert_if_block (struct noce_if_info *if_infop,
2799 basic_block bb, rtx cond,
2800 struct pointer_map_t *then_vals,
2801 struct pointer_map_t *else_vals,
2802 bool else_block_p)
2804 enum rtx_code code;
2805 rtx insn, cond_arg0, cond_arg1;
2807 code = GET_CODE (cond);
2808 cond_arg0 = XEXP (cond, 0);
2809 cond_arg1 = XEXP (cond, 1);
2811 FOR_BB_INSNS (bb, insn)
2813 rtx set, target, dest, t, e;
2814 void **then_slot, **else_slot;
2816 /* ??? Maybe emit conditional debug insn? */
2817 if (!NONDEBUG_INSN_P (insn) || JUMP_P (insn))
2818 continue;
2819 set = single_set (insn);
2820 gcc_assert (set && REG_P (SET_DEST (set)));
2822 dest = SET_DEST (set);
2824 then_slot = pointer_map_contains (then_vals, dest);
2825 else_slot = pointer_map_contains (else_vals, dest);
2826 t = then_slot ? (rtx) *then_slot : NULL_RTX;
2827 e = else_slot ? (rtx) *else_slot : NULL_RTX;
2829 if (else_block_p)
2831 /* If this register was set in the then block, we already
2832 handled this case there. */
2833 if (t)
2834 continue;
2835 t = dest;
2836 gcc_assert (e);
2838 else
2840 gcc_assert (t);
2841 if (!e)
2842 e = dest;
2845 target = noce_emit_cmove (if_infop, dest, code, cond_arg0, cond_arg1,
2846 t, e);
2847 if (!target)
2848 return false;
2850 if (target != dest)
2851 noce_emit_move_insn (dest, target);
2854 return true;
2857 /* Given a simple IF-THEN-JOIN or IF-THEN-ELSE-JOIN block, attempt to convert
2858 it using only conditional moves. Return TRUE if we were successful at
2859 converting the block. */
2861 static int
2862 cond_move_process_if_block (struct noce_if_info *if_info)
2864 basic_block test_bb = if_info->test_bb;
2865 basic_block then_bb = if_info->then_bb;
2866 basic_block else_bb = if_info->else_bb;
2867 basic_block join_bb = if_info->join_bb;
2868 rtx jump = if_info->jump;
2869 rtx cond = if_info->cond;
2870 rtx seq, loc_insn;
2871 rtx reg;
2872 int c;
2873 struct pointer_map_t *then_vals;
2874 struct pointer_map_t *else_vals;
2875 vec<rtx> then_regs = vNULL;
2876 vec<rtx> else_regs = vNULL;
2877 unsigned int i;
2878 int success_p = FALSE;
2880 /* Build a mapping for each block to the value used for each
2881 register. */
2882 then_vals = pointer_map_create ();
2883 else_vals = pointer_map_create ();
2885 /* Make sure the blocks are suitable. */
2886 if (!check_cond_move_block (then_bb, then_vals, &then_regs, cond)
2887 || (else_bb
2888 && !check_cond_move_block (else_bb, else_vals, &else_regs, cond)))
2889 goto done;
2891 /* Make sure the blocks can be used together. If the same register
2892 is set in both blocks, and is not set to a constant in both
2893 cases, then both blocks must set it to the same register. We
2894 have already verified that if it is set to a register, that the
2895 source register does not change after the assignment. Also count
2896 the number of registers set in only one of the blocks. */
2897 c = 0;
2898 FOR_EACH_VEC_ELT (then_regs, i, reg)
2900 void **then_slot = pointer_map_contains (then_vals, reg);
2901 void **else_slot = pointer_map_contains (else_vals, reg);
2903 gcc_checking_assert (then_slot);
2904 if (!else_slot)
2905 ++c;
2906 else
2908 rtx then_val = (rtx) *then_slot;
2909 rtx else_val = (rtx) *else_slot;
2910 if (!CONSTANT_P (then_val) && !CONSTANT_P (else_val)
2911 && !rtx_equal_p (then_val, else_val))
2912 goto done;
2916 /* Finish off c for MAX_CONDITIONAL_EXECUTE. */
2917 FOR_EACH_VEC_ELT (else_regs, i, reg)
2919 gcc_checking_assert (pointer_map_contains (else_vals, reg));
2920 if (!pointer_map_contains (then_vals, reg))
2921 ++c;
2924 /* Make sure it is reasonable to convert this block. What matters
2925 is the number of assignments currently made in only one of the
2926 branches, since if we convert we are going to always execute
2927 them. */
2928 if (c > MAX_CONDITIONAL_EXECUTE)
2929 goto done;
2931 /* Try to emit the conditional moves. First do the then block,
2932 then do anything left in the else blocks. */
2933 start_sequence ();
2934 if (!cond_move_convert_if_block (if_info, then_bb, cond,
2935 then_vals, else_vals, false)
2936 || (else_bb
2937 && !cond_move_convert_if_block (if_info, else_bb, cond,
2938 then_vals, else_vals, true)))
2940 end_sequence ();
2941 goto done;
2943 seq = end_ifcvt_sequence (if_info);
2944 if (!seq)
2945 goto done;
2947 loc_insn = first_active_insn (then_bb);
2948 if (!loc_insn)
2950 loc_insn = first_active_insn (else_bb);
2951 gcc_assert (loc_insn);
2953 emit_insn_before_setloc (seq, jump, INSN_LOCATION (loc_insn));
2955 if (else_bb)
2957 delete_basic_block (else_bb);
2958 num_true_changes++;
2960 else
2961 remove_edge (find_edge (test_bb, join_bb));
2963 remove_edge (find_edge (then_bb, join_bb));
2964 redirect_edge_and_branch_force (single_succ_edge (test_bb), join_bb);
2965 delete_basic_block (then_bb);
2966 num_true_changes++;
2968 if (can_merge_blocks_p (test_bb, join_bb))
2970 merge_blocks (test_bb, join_bb);
2971 num_true_changes++;
2974 num_updated_if_blocks++;
2976 success_p = TRUE;
2978 done:
2979 pointer_map_destroy (then_vals);
2980 pointer_map_destroy (else_vals);
2981 then_regs.release ();
2982 else_regs.release ();
2983 return success_p;
2987 /* Determine if a given basic block heads a simple IF-THEN-JOIN or an
2988 IF-THEN-ELSE-JOIN block.
2990 If so, we'll try to convert the insns to not require the branch,
2991 using only transformations that do not require conditional execution.
2993 Return TRUE if we were successful at converting the block. */
2995 static int
2996 noce_find_if_block (basic_block test_bb, edge then_edge, edge else_edge,
2997 int pass)
2999 basic_block then_bb, else_bb, join_bb;
3000 bool then_else_reversed = false;
3001 rtx jump, cond;
3002 rtx cond_earliest;
3003 struct noce_if_info if_info;
3005 /* We only ever should get here before reload. */
3006 gcc_assert (!reload_completed);
3008 /* Recognize an IF-THEN-ELSE-JOIN block. */
3009 if (single_pred_p (then_edge->dest)
3010 && single_succ_p (then_edge->dest)
3011 && single_pred_p (else_edge->dest)
3012 && single_succ_p (else_edge->dest)
3013 && single_succ (then_edge->dest) == single_succ (else_edge->dest))
3015 then_bb = then_edge->dest;
3016 else_bb = else_edge->dest;
3017 join_bb = single_succ (then_bb);
3019 /* Recognize an IF-THEN-JOIN block. */
3020 else if (single_pred_p (then_edge->dest)
3021 && single_succ_p (then_edge->dest)
3022 && single_succ (then_edge->dest) == else_edge->dest)
3024 then_bb = then_edge->dest;
3025 else_bb = NULL_BLOCK;
3026 join_bb = else_edge->dest;
3028 /* Recognize an IF-ELSE-JOIN block. We can have those because the order
3029 of basic blocks in cfglayout mode does not matter, so the fallthrough
3030 edge can go to any basic block (and not just to bb->next_bb, like in
3031 cfgrtl mode). */
3032 else if (single_pred_p (else_edge->dest)
3033 && single_succ_p (else_edge->dest)
3034 && single_succ (else_edge->dest) == then_edge->dest)
3036 /* The noce transformations do not apply to IF-ELSE-JOIN blocks.
3037 To make this work, we have to invert the THEN and ELSE blocks
3038 and reverse the jump condition. */
3039 then_bb = else_edge->dest;
3040 else_bb = NULL_BLOCK;
3041 join_bb = single_succ (then_bb);
3042 then_else_reversed = true;
3044 else
3045 /* Not a form we can handle. */
3046 return FALSE;
3048 /* The edges of the THEN and ELSE blocks cannot have complex edges. */
3049 if (single_succ_edge (then_bb)->flags & EDGE_COMPLEX)
3050 return FALSE;
3051 if (else_bb
3052 && single_succ_edge (else_bb)->flags & EDGE_COMPLEX)
3053 return FALSE;
3055 num_possible_if_blocks++;
3057 if (dump_file)
3059 fprintf (dump_file,
3060 "\nIF-THEN%s-JOIN block found, pass %d, test %d, then %d",
3061 (else_bb) ? "-ELSE" : "",
3062 pass, test_bb->index, then_bb->index);
3064 if (else_bb)
3065 fprintf (dump_file, ", else %d", else_bb->index);
3067 fprintf (dump_file, ", join %d\n", join_bb->index);
3070 /* If the conditional jump is more than just a conditional
3071 jump, then we can not do if-conversion on this block. */
3072 jump = BB_END (test_bb);
3073 if (! onlyjump_p (jump))
3074 return FALSE;
3076 /* If this is not a standard conditional jump, we can't parse it. */
3077 cond = noce_get_condition (jump, &cond_earliest, then_else_reversed);
3078 if (!cond)
3079 return FALSE;
3081 /* We must be comparing objects whose modes imply the size. */
3082 if (GET_MODE (XEXP (cond, 0)) == BLKmode)
3083 return FALSE;
3085 /* Initialize an IF_INFO struct to pass around. */
3086 memset (&if_info, 0, sizeof if_info);
3087 if_info.test_bb = test_bb;
3088 if_info.then_bb = then_bb;
3089 if_info.else_bb = else_bb;
3090 if_info.join_bb = join_bb;
3091 if_info.cond = cond;
3092 if_info.cond_earliest = cond_earliest;
3093 if_info.jump = jump;
3094 if_info.then_else_reversed = then_else_reversed;
3095 if_info.branch_cost = BRANCH_COST (optimize_bb_for_speed_p (test_bb),
3096 predictable_edge_p (then_edge));
3098 /* Do the real work. */
3100 if (noce_process_if_block (&if_info))
3101 return TRUE;
3103 if (HAVE_conditional_move
3104 && cond_move_process_if_block (&if_info))
3105 return TRUE;
3107 return FALSE;
3111 /* Merge the blocks and mark for local life update. */
3113 static void
3114 merge_if_block (struct ce_if_block * ce_info)
3116 basic_block test_bb = ce_info->test_bb; /* last test block */
3117 basic_block then_bb = ce_info->then_bb; /* THEN */
3118 basic_block else_bb = ce_info->else_bb; /* ELSE or NULL */
3119 basic_block join_bb = ce_info->join_bb; /* join block */
3120 basic_block combo_bb;
3122 /* All block merging is done into the lower block numbers. */
3124 combo_bb = test_bb;
3125 df_set_bb_dirty (test_bb);
3127 /* Merge any basic blocks to handle && and || subtests. Each of
3128 the blocks are on the fallthru path from the predecessor block. */
3129 if (ce_info->num_multiple_test_blocks > 0)
3131 basic_block bb = test_bb;
3132 basic_block last_test_bb = ce_info->last_test_bb;
3133 basic_block fallthru = block_fallthru (bb);
3137 bb = fallthru;
3138 fallthru = block_fallthru (bb);
3139 merge_blocks (combo_bb, bb);
3140 num_true_changes++;
3142 while (bb != last_test_bb);
3145 /* Merge TEST block into THEN block. Normally the THEN block won't have a
3146 label, but it might if there were || tests. That label's count should be
3147 zero, and it normally should be removed. */
3149 if (then_bb)
3151 merge_blocks (combo_bb, then_bb);
3152 num_true_changes++;
3155 /* The ELSE block, if it existed, had a label. That label count
3156 will almost always be zero, but odd things can happen when labels
3157 get their addresses taken. */
3158 if (else_bb)
3160 merge_blocks (combo_bb, else_bb);
3161 num_true_changes++;
3164 /* If there was no join block reported, that means it was not adjacent
3165 to the others, and so we cannot merge them. */
3167 if (! join_bb)
3169 rtx last = BB_END (combo_bb);
3171 /* The outgoing edge for the current COMBO block should already
3172 be correct. Verify this. */
3173 if (EDGE_COUNT (combo_bb->succs) == 0)
3174 gcc_assert (find_reg_note (last, REG_NORETURN, NULL)
3175 || (NONJUMP_INSN_P (last)
3176 && GET_CODE (PATTERN (last)) == TRAP_IF
3177 && (TRAP_CONDITION (PATTERN (last))
3178 == const_true_rtx)));
3180 else
3181 /* There should still be something at the end of the THEN or ELSE
3182 blocks taking us to our final destination. */
3183 gcc_assert (JUMP_P (last)
3184 || (EDGE_SUCC (combo_bb, 0)->dest == EXIT_BLOCK_PTR
3185 && CALL_P (last)
3186 && SIBLING_CALL_P (last))
3187 || ((EDGE_SUCC (combo_bb, 0)->flags & EDGE_EH)
3188 && can_throw_internal (last)));
3191 /* The JOIN block may have had quite a number of other predecessors too.
3192 Since we've already merged the TEST, THEN and ELSE blocks, we should
3193 have only one remaining edge from our if-then-else diamond. If there
3194 is more than one remaining edge, it must come from elsewhere. There
3195 may be zero incoming edges if the THEN block didn't actually join
3196 back up (as with a call to a non-return function). */
3197 else if (EDGE_COUNT (join_bb->preds) < 2
3198 && join_bb != EXIT_BLOCK_PTR)
3200 /* We can merge the JOIN cleanly and update the dataflow try
3201 again on this pass.*/
3202 merge_blocks (combo_bb, join_bb);
3203 num_true_changes++;
3205 else
3207 /* We cannot merge the JOIN. */
3209 /* The outgoing edge for the current COMBO block should already
3210 be correct. Verify this. */
3211 gcc_assert (single_succ_p (combo_bb)
3212 && single_succ (combo_bb) == join_bb);
3214 /* Remove the jump and cruft from the end of the COMBO block. */
3215 if (join_bb != EXIT_BLOCK_PTR)
3216 tidy_fallthru_edge (single_succ_edge (combo_bb));
3219 num_updated_if_blocks++;
3222 /* Find a block ending in a simple IF condition and try to transform it
3223 in some way. When converting a multi-block condition, put the new code
3224 in the first such block and delete the rest. Return a pointer to this
3225 first block if some transformation was done. Return NULL otherwise. */
3227 static basic_block
3228 find_if_header (basic_block test_bb, int pass)
3230 ce_if_block_t ce_info;
3231 edge then_edge;
3232 edge else_edge;
3234 /* The kind of block we're looking for has exactly two successors. */
3235 if (EDGE_COUNT (test_bb->succs) != 2)
3236 return NULL;
3238 then_edge = EDGE_SUCC (test_bb, 0);
3239 else_edge = EDGE_SUCC (test_bb, 1);
3241 if (df_get_bb_dirty (then_edge->dest))
3242 return NULL;
3243 if (df_get_bb_dirty (else_edge->dest))
3244 return NULL;
3246 /* Neither edge should be abnormal. */
3247 if ((then_edge->flags & EDGE_COMPLEX)
3248 || (else_edge->flags & EDGE_COMPLEX))
3249 return NULL;
3251 /* Nor exit the loop. */
3252 if ((then_edge->flags & EDGE_LOOP_EXIT)
3253 || (else_edge->flags & EDGE_LOOP_EXIT))
3254 return NULL;
3256 /* The THEN edge is canonically the one that falls through. */
3257 if (then_edge->flags & EDGE_FALLTHRU)
3259 else if (else_edge->flags & EDGE_FALLTHRU)
3261 edge e = else_edge;
3262 else_edge = then_edge;
3263 then_edge = e;
3265 else
3266 /* Otherwise this must be a multiway branch of some sort. */
3267 return NULL;
3269 memset (&ce_info, 0, sizeof (ce_info));
3270 ce_info.test_bb = test_bb;
3271 ce_info.then_bb = then_edge->dest;
3272 ce_info.else_bb = else_edge->dest;
3273 ce_info.pass = pass;
3275 #ifdef IFCVT_MACHDEP_INIT
3276 IFCVT_MACHDEP_INIT (&ce_info);
3277 #endif
3279 if (!reload_completed
3280 && noce_find_if_block (test_bb, then_edge, else_edge, pass))
3281 goto success;
3283 if (reload_completed
3284 && targetm.have_conditional_execution ()
3285 && cond_exec_find_if_block (&ce_info))
3286 goto success;
3288 if (HAVE_trap
3289 && optab_handler (ctrap_optab, word_mode) != CODE_FOR_nothing
3290 && find_cond_trap (test_bb, then_edge, else_edge))
3291 goto success;
3293 if (dom_info_state (CDI_POST_DOMINATORS) >= DOM_NO_FAST_QUERY
3294 && (reload_completed || !targetm.have_conditional_execution ()))
3296 if (find_if_case_1 (test_bb, then_edge, else_edge))
3297 goto success;
3298 if (find_if_case_2 (test_bb, then_edge, else_edge))
3299 goto success;
3302 return NULL;
3304 success:
3305 if (dump_file)
3306 fprintf (dump_file, "Conversion succeeded on pass %d.\n", pass);
3307 /* Set this so we continue looking. */
3308 cond_exec_changed_p = TRUE;
3309 return ce_info.test_bb;
3312 /* Return true if a block has two edges, one of which falls through to the next
3313 block, and the other jumps to a specific block, so that we can tell if the
3314 block is part of an && test or an || test. Returns either -1 or the number
3315 of non-note, non-jump, non-USE/CLOBBER insns in the block. */
3317 static int
3318 block_jumps_and_fallthru_p (basic_block cur_bb, basic_block target_bb)
3320 edge cur_edge;
3321 int fallthru_p = FALSE;
3322 int jump_p = FALSE;
3323 rtx insn;
3324 rtx end;
3325 int n_insns = 0;
3326 edge_iterator ei;
3328 if (!cur_bb || !target_bb)
3329 return -1;
3331 /* If no edges, obviously it doesn't jump or fallthru. */
3332 if (EDGE_COUNT (cur_bb->succs) == 0)
3333 return FALSE;
3335 FOR_EACH_EDGE (cur_edge, ei, cur_bb->succs)
3337 if (cur_edge->flags & EDGE_COMPLEX)
3338 /* Anything complex isn't what we want. */
3339 return -1;
3341 else if (cur_edge->flags & EDGE_FALLTHRU)
3342 fallthru_p = TRUE;
3344 else if (cur_edge->dest == target_bb)
3345 jump_p = TRUE;
3347 else
3348 return -1;
3351 if ((jump_p & fallthru_p) == 0)
3352 return -1;
3354 /* Don't allow calls in the block, since this is used to group && and ||
3355 together for conditional execution support. ??? we should support
3356 conditional execution support across calls for IA-64 some day, but
3357 for now it makes the code simpler. */
3358 end = BB_END (cur_bb);
3359 insn = BB_HEAD (cur_bb);
3361 while (insn != NULL_RTX)
3363 if (CALL_P (insn))
3364 return -1;
3366 if (INSN_P (insn)
3367 && !JUMP_P (insn)
3368 && !DEBUG_INSN_P (insn)
3369 && GET_CODE (PATTERN (insn)) != USE
3370 && GET_CODE (PATTERN (insn)) != CLOBBER)
3371 n_insns++;
3373 if (insn == end)
3374 break;
3376 insn = NEXT_INSN (insn);
3379 return n_insns;
3382 /* Determine if a given basic block heads a simple IF-THEN or IF-THEN-ELSE
3383 block. If so, we'll try to convert the insns to not require the branch.
3384 Return TRUE if we were successful at converting the block. */
3386 static int
3387 cond_exec_find_if_block (struct ce_if_block * ce_info)
3389 basic_block test_bb = ce_info->test_bb;
3390 basic_block then_bb = ce_info->then_bb;
3391 basic_block else_bb = ce_info->else_bb;
3392 basic_block join_bb = NULL_BLOCK;
3393 edge cur_edge;
3394 basic_block next;
3395 edge_iterator ei;
3397 ce_info->last_test_bb = test_bb;
3399 /* We only ever should get here after reload,
3400 and if we have conditional execution. */
3401 gcc_assert (reload_completed && targetm.have_conditional_execution ());
3403 /* Discover if any fall through predecessors of the current test basic block
3404 were && tests (which jump to the else block) or || tests (which jump to
3405 the then block). */
3406 if (single_pred_p (test_bb)
3407 && single_pred_edge (test_bb)->flags == EDGE_FALLTHRU)
3409 basic_block bb = single_pred (test_bb);
3410 basic_block target_bb;
3411 int max_insns = MAX_CONDITIONAL_EXECUTE;
3412 int n_insns;
3414 /* Determine if the preceding block is an && or || block. */
3415 if ((n_insns = block_jumps_and_fallthru_p (bb, else_bb)) >= 0)
3417 ce_info->and_and_p = TRUE;
3418 target_bb = else_bb;
3420 else if ((n_insns = block_jumps_and_fallthru_p (bb, then_bb)) >= 0)
3422 ce_info->and_and_p = FALSE;
3423 target_bb = then_bb;
3425 else
3426 target_bb = NULL_BLOCK;
3428 if (target_bb && n_insns <= max_insns)
3430 int total_insns = 0;
3431 int blocks = 0;
3433 ce_info->last_test_bb = test_bb;
3435 /* Found at least one && or || block, look for more. */
3438 ce_info->test_bb = test_bb = bb;
3439 total_insns += n_insns;
3440 blocks++;
3442 if (!single_pred_p (bb))
3443 break;
3445 bb = single_pred (bb);
3446 n_insns = block_jumps_and_fallthru_p (bb, target_bb);
3448 while (n_insns >= 0 && (total_insns + n_insns) <= max_insns);
3450 ce_info->num_multiple_test_blocks = blocks;
3451 ce_info->num_multiple_test_insns = total_insns;
3453 if (ce_info->and_and_p)
3454 ce_info->num_and_and_blocks = blocks;
3455 else
3456 ce_info->num_or_or_blocks = blocks;
3460 /* The THEN block of an IF-THEN combo must have exactly one predecessor,
3461 other than any || blocks which jump to the THEN block. */
3462 if ((EDGE_COUNT (then_bb->preds) - ce_info->num_or_or_blocks) != 1)
3463 return FALSE;
3465 /* The edges of the THEN and ELSE blocks cannot have complex edges. */
3466 FOR_EACH_EDGE (cur_edge, ei, then_bb->preds)
3468 if (cur_edge->flags & EDGE_COMPLEX)
3469 return FALSE;
3472 FOR_EACH_EDGE (cur_edge, ei, else_bb->preds)
3474 if (cur_edge->flags & EDGE_COMPLEX)
3475 return FALSE;
3478 /* The THEN block of an IF-THEN combo must have zero or one successors. */
3479 if (EDGE_COUNT (then_bb->succs) > 0
3480 && (!single_succ_p (then_bb)
3481 || (single_succ_edge (then_bb)->flags & EDGE_COMPLEX)
3482 || (epilogue_completed
3483 && tablejump_p (BB_END (then_bb), NULL, NULL))))
3484 return FALSE;
3486 /* If the THEN block has no successors, conditional execution can still
3487 make a conditional call. Don't do this unless the ELSE block has
3488 only one incoming edge -- the CFG manipulation is too ugly otherwise.
3489 Check for the last insn of the THEN block being an indirect jump, which
3490 is listed as not having any successors, but confuses the rest of the CE
3491 code processing. ??? we should fix this in the future. */
3492 if (EDGE_COUNT (then_bb->succs) == 0)
3494 if (single_pred_p (else_bb) && else_bb != EXIT_BLOCK_PTR)
3496 rtx last_insn = BB_END (then_bb);
3498 while (last_insn
3499 && NOTE_P (last_insn)
3500 && last_insn != BB_HEAD (then_bb))
3501 last_insn = PREV_INSN (last_insn);
3503 if (last_insn
3504 && JUMP_P (last_insn)
3505 && ! simplejump_p (last_insn))
3506 return FALSE;
3508 join_bb = else_bb;
3509 else_bb = NULL_BLOCK;
3511 else
3512 return FALSE;
3515 /* If the THEN block's successor is the other edge out of the TEST block,
3516 then we have an IF-THEN combo without an ELSE. */
3517 else if (single_succ (then_bb) == else_bb)
3519 join_bb = else_bb;
3520 else_bb = NULL_BLOCK;
3523 /* If the THEN and ELSE block meet in a subsequent block, and the ELSE
3524 has exactly one predecessor and one successor, and the outgoing edge
3525 is not complex, then we have an IF-THEN-ELSE combo. */
3526 else if (single_succ_p (else_bb)
3527 && single_succ (then_bb) == single_succ (else_bb)
3528 && single_pred_p (else_bb)
3529 && !(single_succ_edge (else_bb)->flags & EDGE_COMPLEX)
3530 && !(epilogue_completed
3531 && tablejump_p (BB_END (else_bb), NULL, NULL)))
3532 join_bb = single_succ (else_bb);
3534 /* Otherwise it is not an IF-THEN or IF-THEN-ELSE combination. */
3535 else
3536 return FALSE;
3538 num_possible_if_blocks++;
3540 if (dump_file)
3542 fprintf (dump_file,
3543 "\nIF-THEN%s block found, pass %d, start block %d "
3544 "[insn %d], then %d [%d]",
3545 (else_bb) ? "-ELSE" : "",
3546 ce_info->pass,
3547 test_bb->index,
3548 BB_HEAD (test_bb) ? (int)INSN_UID (BB_HEAD (test_bb)) : -1,
3549 then_bb->index,
3550 BB_HEAD (then_bb) ? (int)INSN_UID (BB_HEAD (then_bb)) : -1);
3552 if (else_bb)
3553 fprintf (dump_file, ", else %d [%d]",
3554 else_bb->index,
3555 BB_HEAD (else_bb) ? (int)INSN_UID (BB_HEAD (else_bb)) : -1);
3557 fprintf (dump_file, ", join %d [%d]",
3558 join_bb->index,
3559 BB_HEAD (join_bb) ? (int)INSN_UID (BB_HEAD (join_bb)) : -1);
3561 if (ce_info->num_multiple_test_blocks > 0)
3562 fprintf (dump_file, ", %d %s block%s last test %d [%d]",
3563 ce_info->num_multiple_test_blocks,
3564 (ce_info->and_and_p) ? "&&" : "||",
3565 (ce_info->num_multiple_test_blocks == 1) ? "" : "s",
3566 ce_info->last_test_bb->index,
3567 ((BB_HEAD (ce_info->last_test_bb))
3568 ? (int)INSN_UID (BB_HEAD (ce_info->last_test_bb))
3569 : -1));
3571 fputc ('\n', dump_file);
3574 /* Make sure IF, THEN, and ELSE, blocks are adjacent. Actually, we get the
3575 first condition for free, since we've already asserted that there's a
3576 fallthru edge from IF to THEN. Likewise for the && and || blocks, since
3577 we checked the FALLTHRU flag, those are already adjacent to the last IF
3578 block. */
3579 /* ??? As an enhancement, move the ELSE block. Have to deal with
3580 BLOCK notes, if by no other means than backing out the merge if they
3581 exist. Sticky enough I don't want to think about it now. */
3582 next = then_bb;
3583 if (else_bb && (next = next->next_bb) != else_bb)
3584 return FALSE;
3585 if ((next = next->next_bb) != join_bb && join_bb != EXIT_BLOCK_PTR)
3587 if (else_bb)
3588 join_bb = NULL;
3589 else
3590 return FALSE;
3593 /* Do the real work. */
3595 ce_info->else_bb = else_bb;
3596 ce_info->join_bb = join_bb;
3598 /* If we have && and || tests, try to first handle combining the && and ||
3599 tests into the conditional code, and if that fails, go back and handle
3600 it without the && and ||, which at present handles the && case if there
3601 was no ELSE block. */
3602 if (cond_exec_process_if_block (ce_info, TRUE))
3603 return TRUE;
3605 if (ce_info->num_multiple_test_blocks)
3607 cancel_changes (0);
3609 if (cond_exec_process_if_block (ce_info, FALSE))
3610 return TRUE;
3613 return FALSE;
3616 /* Convert a branch over a trap, or a branch
3617 to a trap, into a conditional trap. */
3619 static int
3620 find_cond_trap (basic_block test_bb, edge then_edge, edge else_edge)
3622 basic_block then_bb = then_edge->dest;
3623 basic_block else_bb = else_edge->dest;
3624 basic_block other_bb, trap_bb;
3625 rtx trap, jump, cond, cond_earliest, seq;
3626 enum rtx_code code;
3628 /* Locate the block with the trap instruction. */
3629 /* ??? While we look for no successors, we really ought to allow
3630 EH successors. Need to fix merge_if_block for that to work. */
3631 if ((trap = block_has_only_trap (then_bb)) != NULL)
3632 trap_bb = then_bb, other_bb = else_bb;
3633 else if ((trap = block_has_only_trap (else_bb)) != NULL)
3634 trap_bb = else_bb, other_bb = then_bb;
3635 else
3636 return FALSE;
3638 if (dump_file)
3640 fprintf (dump_file, "\nTRAP-IF block found, start %d, trap %d\n",
3641 test_bb->index, trap_bb->index);
3644 /* If this is not a standard conditional jump, we can't parse it. */
3645 jump = BB_END (test_bb);
3646 cond = noce_get_condition (jump, &cond_earliest, false);
3647 if (! cond)
3648 return FALSE;
3650 /* If the conditional jump is more than just a conditional jump, then
3651 we can not do if-conversion on this block. */
3652 if (! onlyjump_p (jump))
3653 return FALSE;
3655 /* We must be comparing objects whose modes imply the size. */
3656 if (GET_MODE (XEXP (cond, 0)) == BLKmode)
3657 return FALSE;
3659 /* Reverse the comparison code, if necessary. */
3660 code = GET_CODE (cond);
3661 if (then_bb == trap_bb)
3663 code = reversed_comparison_code (cond, jump);
3664 if (code == UNKNOWN)
3665 return FALSE;
3668 /* Attempt to generate the conditional trap. */
3669 seq = gen_cond_trap (code, copy_rtx (XEXP (cond, 0)),
3670 copy_rtx (XEXP (cond, 1)),
3671 TRAP_CODE (PATTERN (trap)));
3672 if (seq == NULL)
3673 return FALSE;
3675 /* Emit the new insns before cond_earliest. */
3676 emit_insn_before_setloc (seq, cond_earliest, INSN_LOCATION (trap));
3678 /* Delete the trap block if possible. */
3679 remove_edge (trap_bb == then_bb ? then_edge : else_edge);
3680 df_set_bb_dirty (test_bb);
3681 df_set_bb_dirty (then_bb);
3682 df_set_bb_dirty (else_bb);
3684 if (EDGE_COUNT (trap_bb->preds) == 0)
3686 delete_basic_block (trap_bb);
3687 num_true_changes++;
3690 /* Wire together the blocks again. */
3691 if (current_ir_type () == IR_RTL_CFGLAYOUT)
3692 single_succ_edge (test_bb)->flags |= EDGE_FALLTHRU;
3693 else
3695 rtx lab, newjump;
3697 lab = JUMP_LABEL (jump);
3698 newjump = emit_jump_insn_after (gen_jump (lab), jump);
3699 LABEL_NUSES (lab) += 1;
3700 JUMP_LABEL (newjump) = lab;
3701 emit_barrier_after (newjump);
3703 delete_insn (jump);
3705 if (can_merge_blocks_p (test_bb, other_bb))
3707 merge_blocks (test_bb, other_bb);
3708 num_true_changes++;
3711 num_updated_if_blocks++;
3712 return TRUE;
3715 /* Subroutine of find_cond_trap: if BB contains only a trap insn,
3716 return it. */
3718 static rtx
3719 block_has_only_trap (basic_block bb)
3721 rtx trap;
3723 /* We're not the exit block. */
3724 if (bb == EXIT_BLOCK_PTR)
3725 return NULL_RTX;
3727 /* The block must have no successors. */
3728 if (EDGE_COUNT (bb->succs) > 0)
3729 return NULL_RTX;
3731 /* The only instruction in the THEN block must be the trap. */
3732 trap = first_active_insn (bb);
3733 if (! (trap == BB_END (bb)
3734 && GET_CODE (PATTERN (trap)) == TRAP_IF
3735 && TRAP_CONDITION (PATTERN (trap)) == const_true_rtx))
3736 return NULL_RTX;
3738 return trap;
3741 /* Look for IF-THEN-ELSE cases in which one of THEN or ELSE is
3742 transformable, but not necessarily the other. There need be no
3743 JOIN block.
3745 Return TRUE if we were successful at converting the block.
3747 Cases we'd like to look at:
3750 if (test) goto over; // x not live
3751 x = a;
3752 goto label;
3753 over:
3755 becomes
3757 x = a;
3758 if (! test) goto label;
3761 if (test) goto E; // x not live
3762 x = big();
3763 goto L;
3765 x = b;
3766 goto M;
3768 becomes
3770 x = b;
3771 if (test) goto M;
3772 x = big();
3773 goto L;
3775 (3) // This one's really only interesting for targets that can do
3776 // multiway branching, e.g. IA-64 BBB bundles. For other targets
3777 // it results in multiple branches on a cache line, which often
3778 // does not sit well with predictors.
3780 if (test1) goto E; // predicted not taken
3781 x = a;
3782 if (test2) goto F;
3785 x = b;
3788 becomes
3790 x = a;
3791 if (test1) goto E;
3792 if (test2) goto F;
3794 Notes:
3796 (A) Don't do (2) if the branch is predicted against the block we're
3797 eliminating. Do it anyway if we can eliminate a branch; this requires
3798 that the sole successor of the eliminated block postdominate the other
3799 side of the if.
3801 (B) With CE, on (3) we can steal from both sides of the if, creating
3803 if (test1) x = a;
3804 if (!test1) x = b;
3805 if (test1) goto J;
3806 if (test2) goto F;
3810 Again, this is most useful if J postdominates.
3812 (C) CE substitutes for helpful life information.
3814 (D) These heuristics need a lot of work. */
3816 /* Tests for case 1 above. */
3818 static int
3819 find_if_case_1 (basic_block test_bb, edge then_edge, edge else_edge)
3821 basic_block then_bb = then_edge->dest;
3822 basic_block else_bb = else_edge->dest;
3823 basic_block new_bb;
3824 int then_bb_index, then_prob;
3825 rtx else_target = NULL_RTX;
3827 /* If we are partitioning hot/cold basic blocks, we don't want to
3828 mess up unconditional or indirect jumps that cross between hot
3829 and cold sections.
3831 Basic block partitioning may result in some jumps that appear to
3832 be optimizable (or blocks that appear to be mergeable), but which really
3833 must be left untouched (they are required to make it safely across
3834 partition boundaries). See the comments at the top of
3835 bb-reorder.c:partition_hot_cold_basic_blocks for complete details. */
3837 if ((BB_END (then_bb)
3838 && find_reg_note (BB_END (then_bb), REG_CROSSING_JUMP, NULL_RTX))
3839 || (BB_END (test_bb)
3840 && find_reg_note (BB_END (test_bb), REG_CROSSING_JUMP, NULL_RTX))
3841 || (BB_END (else_bb)
3842 && find_reg_note (BB_END (else_bb), REG_CROSSING_JUMP,
3843 NULL_RTX)))
3844 return FALSE;
3846 /* THEN has one successor. */
3847 if (!single_succ_p (then_bb))
3848 return FALSE;
3850 /* THEN does not fall through, but is not strange either. */
3851 if (single_succ_edge (then_bb)->flags & (EDGE_COMPLEX | EDGE_FALLTHRU))
3852 return FALSE;
3854 /* THEN has one predecessor. */
3855 if (!single_pred_p (then_bb))
3856 return FALSE;
3858 /* THEN must do something. */
3859 if (forwarder_block_p (then_bb))
3860 return FALSE;
3862 num_possible_if_blocks++;
3863 if (dump_file)
3864 fprintf (dump_file,
3865 "\nIF-CASE-1 found, start %d, then %d\n",
3866 test_bb->index, then_bb->index);
3868 if (then_edge->probability)
3869 then_prob = REG_BR_PROB_BASE - then_edge->probability;
3870 else
3871 then_prob = REG_BR_PROB_BASE / 2;
3873 /* We're speculating from the THEN path, we want to make sure the cost
3874 of speculation is within reason. */
3875 if (! cheap_bb_rtx_cost_p (then_bb, then_prob,
3876 COSTS_N_INSNS (BRANCH_COST (optimize_bb_for_speed_p (then_edge->src),
3877 predictable_edge_p (then_edge)))))
3878 return FALSE;
3880 if (else_bb == EXIT_BLOCK_PTR)
3882 rtx jump = BB_END (else_edge->src);
3883 gcc_assert (JUMP_P (jump));
3884 else_target = JUMP_LABEL (jump);
3887 /* Registers set are dead, or are predicable. */
3888 if (! dead_or_predicable (test_bb, then_bb, else_bb,
3889 single_succ_edge (then_bb), 1))
3890 return FALSE;
3892 /* Conversion went ok, including moving the insns and fixing up the
3893 jump. Adjust the CFG to match. */
3895 /* We can avoid creating a new basic block if then_bb is immediately
3896 followed by else_bb, i.e. deleting then_bb allows test_bb to fall
3897 through to else_bb. */
3899 if (then_bb->next_bb == else_bb
3900 && then_bb->prev_bb == test_bb
3901 && else_bb != EXIT_BLOCK_PTR)
3903 redirect_edge_succ (FALLTHRU_EDGE (test_bb), else_bb);
3904 new_bb = 0;
3906 else if (else_bb == EXIT_BLOCK_PTR)
3907 new_bb = force_nonfallthru_and_redirect (FALLTHRU_EDGE (test_bb),
3908 else_bb, else_target);
3909 else
3910 new_bb = redirect_edge_and_branch_force (FALLTHRU_EDGE (test_bb),
3911 else_bb);
3913 df_set_bb_dirty (test_bb);
3914 df_set_bb_dirty (else_bb);
3916 then_bb_index = then_bb->index;
3917 delete_basic_block (then_bb);
3919 /* Make rest of code believe that the newly created block is the THEN_BB
3920 block we removed. */
3921 if (new_bb)
3923 df_bb_replace (then_bb_index, new_bb);
3924 /* This should have been done above via force_nonfallthru_and_redirect
3925 (possibly called from redirect_edge_and_branch_force). */
3926 gcc_checking_assert (BB_PARTITION (new_bb) == BB_PARTITION (test_bb));
3929 num_true_changes++;
3930 num_updated_if_blocks++;
3932 return TRUE;
3935 /* Test for case 2 above. */
3937 static int
3938 find_if_case_2 (basic_block test_bb, edge then_edge, edge else_edge)
3940 basic_block then_bb = then_edge->dest;
3941 basic_block else_bb = else_edge->dest;
3942 edge else_succ;
3943 int then_prob, else_prob;
3945 /* We do not want to speculate (empty) loop latches. */
3946 if (current_loops
3947 && else_bb->loop_father->latch == else_bb)
3948 return FALSE;
3950 /* If we are partitioning hot/cold basic blocks, we don't want to
3951 mess up unconditional or indirect jumps that cross between hot
3952 and cold sections.
3954 Basic block partitioning may result in some jumps that appear to
3955 be optimizable (or blocks that appear to be mergeable), but which really
3956 must be left untouched (they are required to make it safely across
3957 partition boundaries). See the comments at the top of
3958 bb-reorder.c:partition_hot_cold_basic_blocks for complete details. */
3960 if ((BB_END (then_bb)
3961 && find_reg_note (BB_END (then_bb), REG_CROSSING_JUMP, NULL_RTX))
3962 || (BB_END (test_bb)
3963 && find_reg_note (BB_END (test_bb), REG_CROSSING_JUMP, NULL_RTX))
3964 || (BB_END (else_bb)
3965 && find_reg_note (BB_END (else_bb), REG_CROSSING_JUMP,
3966 NULL_RTX)))
3967 return FALSE;
3969 /* ELSE has one successor. */
3970 if (!single_succ_p (else_bb))
3971 return FALSE;
3972 else
3973 else_succ = single_succ_edge (else_bb);
3975 /* ELSE outgoing edge is not complex. */
3976 if (else_succ->flags & EDGE_COMPLEX)
3977 return FALSE;
3979 /* ELSE has one predecessor. */
3980 if (!single_pred_p (else_bb))
3981 return FALSE;
3983 /* THEN is not EXIT. */
3984 if (then_bb->index < NUM_FIXED_BLOCKS)
3985 return FALSE;
3987 if (else_edge->probability)
3989 else_prob = else_edge->probability;
3990 then_prob = REG_BR_PROB_BASE - else_prob;
3992 else
3994 else_prob = REG_BR_PROB_BASE / 2;
3995 then_prob = REG_BR_PROB_BASE / 2;
3998 /* ELSE is predicted or SUCC(ELSE) postdominates THEN. */
3999 if (else_prob > then_prob)
4001 else if (else_succ->dest->index < NUM_FIXED_BLOCKS
4002 || dominated_by_p (CDI_POST_DOMINATORS, then_bb,
4003 else_succ->dest))
4005 else
4006 return FALSE;
4008 num_possible_if_blocks++;
4009 if (dump_file)
4010 fprintf (dump_file,
4011 "\nIF-CASE-2 found, start %d, else %d\n",
4012 test_bb->index, else_bb->index);
4014 /* We're speculating from the ELSE path, we want to make sure the cost
4015 of speculation is within reason. */
4016 if (! cheap_bb_rtx_cost_p (else_bb, else_prob,
4017 COSTS_N_INSNS (BRANCH_COST (optimize_bb_for_speed_p (else_edge->src),
4018 predictable_edge_p (else_edge)))))
4019 return FALSE;
4021 /* Registers set are dead, or are predicable. */
4022 if (! dead_or_predicable (test_bb, else_bb, then_bb, else_succ, 0))
4023 return FALSE;
4025 /* Conversion went ok, including moving the insns and fixing up the
4026 jump. Adjust the CFG to match. */
4028 df_set_bb_dirty (test_bb);
4029 df_set_bb_dirty (then_bb);
4030 delete_basic_block (else_bb);
4032 num_true_changes++;
4033 num_updated_if_blocks++;
4035 /* ??? We may now fallthru from one of THEN's successors into a join
4036 block. Rerun cleanup_cfg? Examine things manually? Wait? */
4038 return TRUE;
4041 /* Used by the code above to perform the actual rtl transformations.
4042 Return TRUE if successful.
4044 TEST_BB is the block containing the conditional branch. MERGE_BB
4045 is the block containing the code to manipulate. DEST_EDGE is an
4046 edge representing a jump to the join block; after the conversion,
4047 TEST_BB should be branching to its destination.
4048 REVERSEP is true if the sense of the branch should be reversed. */
4050 static int
4051 dead_or_predicable (basic_block test_bb, basic_block merge_bb,
4052 basic_block other_bb, edge dest_edge, int reversep)
4054 basic_block new_dest = dest_edge->dest;
4055 rtx head, end, jump, earliest = NULL_RTX, old_dest;
4056 bitmap merge_set = NULL;
4057 /* Number of pending changes. */
4058 int n_validated_changes = 0;
4059 rtx new_dest_label = NULL_RTX;
4061 jump = BB_END (test_bb);
4063 /* Find the extent of the real code in the merge block. */
4064 head = BB_HEAD (merge_bb);
4065 end = BB_END (merge_bb);
4067 while (DEBUG_INSN_P (end) && end != head)
4068 end = PREV_INSN (end);
4070 /* If merge_bb ends with a tablejump, predicating/moving insn's
4071 into test_bb and then deleting merge_bb will result in the jumptable
4072 that follows merge_bb being removed along with merge_bb and then we
4073 get an unresolved reference to the jumptable. */
4074 if (tablejump_p (end, NULL, NULL))
4075 return FALSE;
4077 if (LABEL_P (head))
4078 head = NEXT_INSN (head);
4079 while (DEBUG_INSN_P (head) && head != end)
4080 head = NEXT_INSN (head);
4081 if (NOTE_P (head))
4083 if (head == end)
4085 head = end = NULL_RTX;
4086 goto no_body;
4088 head = NEXT_INSN (head);
4089 while (DEBUG_INSN_P (head) && head != end)
4090 head = NEXT_INSN (head);
4093 if (JUMP_P (end))
4095 if (head == end)
4097 head = end = NULL_RTX;
4098 goto no_body;
4100 end = PREV_INSN (end);
4101 while (DEBUG_INSN_P (end) && end != head)
4102 end = PREV_INSN (end);
4105 /* Disable handling dead code by conditional execution if the machine needs
4106 to do anything funny with the tests, etc. */
4107 #ifndef IFCVT_MODIFY_TESTS
4108 if (targetm.have_conditional_execution ())
4110 /* In the conditional execution case, we have things easy. We know
4111 the condition is reversible. We don't have to check life info
4112 because we're going to conditionally execute the code anyway.
4113 All that's left is making sure the insns involved can actually
4114 be predicated. */
4116 rtx cond, prob_val;
4118 cond = cond_exec_get_condition (jump);
4119 if (! cond)
4120 return FALSE;
4122 prob_val = find_reg_note (jump, REG_BR_PROB, NULL_RTX);
4123 if (prob_val)
4124 prob_val = XEXP (prob_val, 0);
4126 if (reversep)
4128 enum rtx_code rev = reversed_comparison_code (cond, jump);
4129 if (rev == UNKNOWN)
4130 return FALSE;
4131 cond = gen_rtx_fmt_ee (rev, GET_MODE (cond), XEXP (cond, 0),
4132 XEXP (cond, 1));
4133 if (prob_val)
4134 prob_val = GEN_INT (REG_BR_PROB_BASE - INTVAL (prob_val));
4137 if (cond_exec_process_insns (NULL, head, end, cond, prob_val, 0)
4138 && verify_changes (0))
4139 n_validated_changes = num_validated_changes ();
4140 else
4141 cancel_changes (0);
4143 earliest = jump;
4145 #endif
4147 /* If we allocated new pseudos (e.g. in the conditional move
4148 expander called from noce_emit_cmove), we must resize the
4149 array first. */
4150 if (max_regno < max_reg_num ())
4151 max_regno = max_reg_num ();
4153 /* Try the NCE path if the CE path did not result in any changes. */
4154 if (n_validated_changes == 0)
4156 rtx cond, insn;
4157 regset live;
4158 bool success;
4160 /* In the non-conditional execution case, we have to verify that there
4161 are no trapping operations, no calls, no references to memory, and
4162 that any registers modified are dead at the branch site. */
4164 if (!any_condjump_p (jump))
4165 return FALSE;
4167 /* Find the extent of the conditional. */
4168 cond = noce_get_condition (jump, &earliest, false);
4169 if (!cond)
4170 return FALSE;
4172 live = BITMAP_ALLOC (&reg_obstack);
4173 simulate_backwards_to_point (merge_bb, live, end);
4174 success = can_move_insns_across (head, end, earliest, jump,
4175 merge_bb, live,
4176 df_get_live_in (other_bb), NULL);
4177 BITMAP_FREE (live);
4178 if (!success)
4179 return FALSE;
4181 /* Collect the set of registers set in MERGE_BB. */
4182 merge_set = BITMAP_ALLOC (&reg_obstack);
4184 FOR_BB_INSNS (merge_bb, insn)
4185 if (NONDEBUG_INSN_P (insn))
4186 df_simulate_find_defs (insn, merge_set);
4188 #ifdef HAVE_simple_return
4189 /* If shrink-wrapping, disable this optimization when test_bb is
4190 the first basic block and merge_bb exits. The idea is to not
4191 move code setting up a return register as that may clobber a
4192 register used to pass function parameters, which then must be
4193 saved in caller-saved regs. A caller-saved reg requires the
4194 prologue, killing a shrink-wrap opportunity. */
4195 if ((flag_shrink_wrap && HAVE_simple_return && !epilogue_completed)
4196 && ENTRY_BLOCK_PTR->next_bb == test_bb
4197 && single_succ_p (new_dest)
4198 && single_succ (new_dest) == EXIT_BLOCK_PTR
4199 && bitmap_intersect_p (df_get_live_in (new_dest), merge_set))
4201 regset return_regs;
4202 unsigned int i;
4204 return_regs = BITMAP_ALLOC (&reg_obstack);
4206 /* Start off with the intersection of regs used to pass
4207 params and regs used to return values. */
4208 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
4209 if (FUNCTION_ARG_REGNO_P (i)
4210 && targetm.calls.function_value_regno_p (i))
4211 bitmap_set_bit (return_regs, INCOMING_REGNO (i));
4213 bitmap_and_into (return_regs, df_get_live_out (ENTRY_BLOCK_PTR));
4214 bitmap_and_into (return_regs, df_get_live_in (EXIT_BLOCK_PTR));
4215 if (!bitmap_empty_p (return_regs))
4217 FOR_BB_INSNS_REVERSE (new_dest, insn)
4218 if (NONDEBUG_INSN_P (insn))
4220 df_ref *def_rec;
4221 unsigned int uid = INSN_UID (insn);
4223 /* If this insn sets any reg in return_regs.. */
4224 for (def_rec = DF_INSN_UID_DEFS (uid); *def_rec; def_rec++)
4226 df_ref def = *def_rec;
4227 unsigned r = DF_REF_REGNO (def);
4229 if (bitmap_bit_p (return_regs, r))
4230 break;
4232 /* ..then add all reg uses to the set of regs
4233 we're interested in. */
4234 if (*def_rec)
4235 df_simulate_uses (insn, return_regs);
4237 if (bitmap_intersect_p (merge_set, return_regs))
4239 BITMAP_FREE (return_regs);
4240 BITMAP_FREE (merge_set);
4241 return FALSE;
4244 BITMAP_FREE (return_regs);
4246 #endif
4249 no_body:
4250 /* We don't want to use normal invert_jump or redirect_jump because
4251 we don't want to delete_insn called. Also, we want to do our own
4252 change group management. */
4254 old_dest = JUMP_LABEL (jump);
4255 if (other_bb != new_dest)
4257 if (JUMP_P (BB_END (dest_edge->src)))
4258 new_dest_label = JUMP_LABEL (BB_END (dest_edge->src));
4259 else if (new_dest == EXIT_BLOCK_PTR)
4260 new_dest_label = ret_rtx;
4261 else
4262 new_dest_label = block_label (new_dest);
4264 if (reversep
4265 ? ! invert_jump_1 (jump, new_dest_label)
4266 : ! redirect_jump_1 (jump, new_dest_label))
4267 goto cancel;
4270 if (verify_changes (n_validated_changes))
4271 confirm_change_group ();
4272 else
4273 goto cancel;
4275 if (other_bb != new_dest)
4277 redirect_jump_2 (jump, old_dest, new_dest_label, 0, reversep);
4279 redirect_edge_succ (BRANCH_EDGE (test_bb), new_dest);
4280 if (reversep)
4282 gcov_type count, probability;
4283 count = BRANCH_EDGE (test_bb)->count;
4284 BRANCH_EDGE (test_bb)->count = FALLTHRU_EDGE (test_bb)->count;
4285 FALLTHRU_EDGE (test_bb)->count = count;
4286 probability = BRANCH_EDGE (test_bb)->probability;
4287 BRANCH_EDGE (test_bb)->probability
4288 = FALLTHRU_EDGE (test_bb)->probability;
4289 FALLTHRU_EDGE (test_bb)->probability = probability;
4290 update_br_prob_note (test_bb);
4294 /* Move the insns out of MERGE_BB to before the branch. */
4295 if (head != NULL)
4297 rtx insn;
4299 if (end == BB_END (merge_bb))
4300 BB_END (merge_bb) = PREV_INSN (head);
4302 /* PR 21767: when moving insns above a conditional branch, the REG_EQUAL
4303 notes being moved might become invalid. */
4304 insn = head;
4307 rtx note, set;
4309 if (! INSN_P (insn))
4310 continue;
4311 note = find_reg_note (insn, REG_EQUAL, NULL_RTX);
4312 if (! note)
4313 continue;
4314 set = single_set (insn);
4315 if (!set || !function_invariant_p (SET_SRC (set))
4316 || !function_invariant_p (XEXP (note, 0)))
4317 remove_note (insn, note);
4318 } while (insn != end && (insn = NEXT_INSN (insn)));
4320 /* PR46315: when moving insns above a conditional branch, the REG_EQUAL
4321 notes referring to the registers being set might become invalid. */
4322 if (merge_set)
4324 unsigned i;
4325 bitmap_iterator bi;
4327 EXECUTE_IF_SET_IN_BITMAP (merge_set, 0, i, bi)
4328 remove_reg_equal_equiv_notes_for_regno (i);
4330 BITMAP_FREE (merge_set);
4333 reorder_insns (head, end, PREV_INSN (earliest));
4336 /* Remove the jump and edge if we can. */
4337 if (other_bb == new_dest)
4339 delete_insn (jump);
4340 remove_edge (BRANCH_EDGE (test_bb));
4341 /* ??? Can't merge blocks here, as then_bb is still in use.
4342 At minimum, the merge will get done just before bb-reorder. */
4345 return TRUE;
4347 cancel:
4348 cancel_changes (0);
4350 if (merge_set)
4351 BITMAP_FREE (merge_set);
4353 return FALSE;
4356 /* Main entry point for all if-conversion. AFTER_COMBINE is true if
4357 we are after combine pass. */
4359 static void
4360 if_convert (bool after_combine)
4362 basic_block bb;
4363 int pass;
4365 if (optimize == 1)
4367 df_live_add_problem ();
4368 df_live_set_all_dirty ();
4371 /* Record whether we are after combine pass. */
4372 ifcvt_after_combine = after_combine;
4373 num_possible_if_blocks = 0;
4374 num_updated_if_blocks = 0;
4375 num_true_changes = 0;
4377 loop_optimizer_init (AVOID_CFG_MODIFICATIONS);
4378 mark_loop_exit_edges ();
4379 loop_optimizer_finalize ();
4380 free_dominance_info (CDI_DOMINATORS);
4382 /* Compute postdominators. */
4383 calculate_dominance_info (CDI_POST_DOMINATORS);
4385 df_set_flags (DF_LR_RUN_DCE);
4387 /* Go through each of the basic blocks looking for things to convert. If we
4388 have conditional execution, we make multiple passes to allow us to handle
4389 IF-THEN{-ELSE} blocks within other IF-THEN{-ELSE} blocks. */
4390 pass = 0;
4393 df_analyze ();
4394 /* Only need to do dce on the first pass. */
4395 df_clear_flags (DF_LR_RUN_DCE);
4396 cond_exec_changed_p = FALSE;
4397 pass++;
4399 #ifdef IFCVT_MULTIPLE_DUMPS
4400 if (dump_file && pass > 1)
4401 fprintf (dump_file, "\n\n========== Pass %d ==========\n", pass);
4402 #endif
4404 FOR_EACH_BB (bb)
4406 basic_block new_bb;
4407 while (!df_get_bb_dirty (bb)
4408 && (new_bb = find_if_header (bb, pass)) != NULL)
4409 bb = new_bb;
4412 #ifdef IFCVT_MULTIPLE_DUMPS
4413 if (dump_file && cond_exec_changed_p)
4414 print_rtl_with_bb (dump_file, get_insns (), dump_flags);
4415 #endif
4417 while (cond_exec_changed_p);
4419 #ifdef IFCVT_MULTIPLE_DUMPS
4420 if (dump_file)
4421 fprintf (dump_file, "\n\n========== no more changes\n");
4422 #endif
4424 free_dominance_info (CDI_POST_DOMINATORS);
4426 if (dump_file)
4427 fflush (dump_file);
4429 clear_aux_for_blocks ();
4431 /* If we allocated new pseudos, we must resize the array for sched1. */
4432 if (max_regno < max_reg_num ())
4433 max_regno = max_reg_num ();
4435 /* Write the final stats. */
4436 if (dump_file && num_possible_if_blocks > 0)
4438 fprintf (dump_file,
4439 "\n%d possible IF blocks searched.\n",
4440 num_possible_if_blocks);
4441 fprintf (dump_file,
4442 "%d IF blocks converted.\n",
4443 num_updated_if_blocks);
4444 fprintf (dump_file,
4445 "%d true changes made.\n\n\n",
4446 num_true_changes);
4449 if (optimize == 1)
4450 df_remove_problem (df_live);
4452 #ifdef ENABLE_CHECKING
4453 verify_flow_info ();
4454 #endif
4457 static bool
4458 gate_handle_if_conversion (void)
4460 return (optimize > 0)
4461 && dbg_cnt (if_conversion);
4464 /* If-conversion and CFG cleanup. */
4465 static unsigned int
4466 rest_of_handle_if_conversion (void)
4468 if (flag_if_conversion)
4470 if (dump_file)
4472 dump_reg_info (dump_file);
4473 dump_flow_info (dump_file, dump_flags);
4475 cleanup_cfg (CLEANUP_EXPENSIVE);
4476 if_convert (false);
4479 cleanup_cfg (0);
4480 return 0;
4483 namespace {
4485 const pass_data pass_data_rtl_ifcvt =
4487 RTL_PASS, /* type */
4488 "ce1", /* name */
4489 OPTGROUP_NONE, /* optinfo_flags */
4490 true, /* has_gate */
4491 true, /* has_execute */
4492 TV_IFCVT, /* tv_id */
4493 0, /* properties_required */
4494 0, /* properties_provided */
4495 0, /* properties_destroyed */
4496 0, /* todo_flags_start */
4497 ( TODO_df_finish | TODO_verify_rtl_sharing | 0 ), /* todo_flags_finish */
4500 class pass_rtl_ifcvt : public rtl_opt_pass
4502 public:
4503 pass_rtl_ifcvt(gcc::context *ctxt)
4504 : rtl_opt_pass(pass_data_rtl_ifcvt, ctxt)
4507 /* opt_pass methods: */
4508 bool gate () { return gate_handle_if_conversion (); }
4509 unsigned int execute () { return rest_of_handle_if_conversion (); }
4511 }; // class pass_rtl_ifcvt
4513 } // anon namespace
4515 rtl_opt_pass *
4516 make_pass_rtl_ifcvt (gcc::context *ctxt)
4518 return new pass_rtl_ifcvt (ctxt);
4521 static bool
4522 gate_handle_if_after_combine (void)
4524 return optimize > 0 && flag_if_conversion
4525 && dbg_cnt (if_after_combine);
4529 /* Rerun if-conversion, as combine may have simplified things enough
4530 to now meet sequence length restrictions. */
4531 static unsigned int
4532 rest_of_handle_if_after_combine (void)
4534 if_convert (true);
4535 return 0;
4538 namespace {
4540 const pass_data pass_data_if_after_combine =
4542 RTL_PASS, /* type */
4543 "ce2", /* name */
4544 OPTGROUP_NONE, /* optinfo_flags */
4545 true, /* has_gate */
4546 true, /* has_execute */
4547 TV_IFCVT, /* tv_id */
4548 0, /* properties_required */
4549 0, /* properties_provided */
4550 0, /* properties_destroyed */
4551 0, /* todo_flags_start */
4552 ( TODO_df_finish | TODO_verify_rtl_sharing ), /* todo_flags_finish */
4555 class pass_if_after_combine : public rtl_opt_pass
4557 public:
4558 pass_if_after_combine(gcc::context *ctxt)
4559 : rtl_opt_pass(pass_data_if_after_combine, ctxt)
4562 /* opt_pass methods: */
4563 bool gate () { return gate_handle_if_after_combine (); }
4564 unsigned int execute () { return rest_of_handle_if_after_combine (); }
4566 }; // class pass_if_after_combine
4568 } // anon namespace
4570 rtl_opt_pass *
4571 make_pass_if_after_combine (gcc::context *ctxt)
4573 return new pass_if_after_combine (ctxt);
4577 static bool
4578 gate_handle_if_after_reload (void)
4580 return optimize > 0 && flag_if_conversion2
4581 && dbg_cnt (if_after_reload);
4584 static unsigned int
4585 rest_of_handle_if_after_reload (void)
4587 if_convert (true);
4588 return 0;
4592 namespace {
4594 const pass_data pass_data_if_after_reload =
4596 RTL_PASS, /* type */
4597 "ce3", /* name */
4598 OPTGROUP_NONE, /* optinfo_flags */
4599 true, /* has_gate */
4600 true, /* has_execute */
4601 TV_IFCVT2, /* tv_id */
4602 0, /* properties_required */
4603 0, /* properties_provided */
4604 0, /* properties_destroyed */
4605 0, /* todo_flags_start */
4606 ( TODO_df_finish | TODO_verify_rtl_sharing ), /* todo_flags_finish */
4609 class pass_if_after_reload : public rtl_opt_pass
4611 public:
4612 pass_if_after_reload(gcc::context *ctxt)
4613 : rtl_opt_pass(pass_data_if_after_reload, ctxt)
4616 /* opt_pass methods: */
4617 bool gate () { return gate_handle_if_after_reload (); }
4618 unsigned int execute () { return rest_of_handle_if_after_reload (); }
4620 }; // class pass_if_after_reload
4622 } // anon namespace
4624 rtl_opt_pass *
4625 make_pass_if_after_reload (gcc::context *ctxt)
4627 return new pass_if_after_reload (ctxt);