2014-08-15 Richard Biener <rguenther@suse.de>
[official-gcc.git] / gcc / ifcvt.c
blob521dca0d1ddb13921c73d3f6f8a7ba7d107dd3a9
1 /* If-conversion support.
2 Copyright (C) 2000-2014 Free Software Foundation, Inc.
4 This file is part of GCC.
6 GCC is free software; you can redistribute it and/or modify it
7 under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 3, or (at your option)
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 "dbgcnt.h"
46 #ifndef HAVE_conditional_move
47 #define HAVE_conditional_move 0
48 #endif
49 #ifndef HAVE_incscc
50 #define HAVE_incscc 0
51 #endif
52 #ifndef HAVE_decscc
53 #define HAVE_decscc 0
54 #endif
55 #ifndef HAVE_trap
56 #define HAVE_trap 0
57 #endif
59 #ifndef MAX_CONDITIONAL_EXECUTE
60 #define MAX_CONDITIONAL_EXECUTE \
61 (BRANCH_COST (optimize_function_for_speed_p (cfun), false) \
62 + 1)
63 #endif
65 #define IFCVT_MULTIPLE_DUMPS 1
67 #define NULL_BLOCK ((basic_block) NULL)
69 /* True if after combine pass. */
70 static bool ifcvt_after_combine;
72 /* # of IF-THEN or IF-THEN-ELSE blocks we looked at */
73 static int num_possible_if_blocks;
75 /* # of IF-THEN or IF-THEN-ELSE blocks were converted to conditional
76 execution. */
77 static int num_updated_if_blocks;
79 /* # of changes made. */
80 static int num_true_changes;
82 /* Whether conditional execution changes were made. */
83 static int cond_exec_changed_p;
85 /* Forward references. */
86 static int count_bb_insns (const_basic_block);
87 static bool cheap_bb_rtx_cost_p (const_basic_block, int, int);
88 static rtx first_active_insn (basic_block);
89 static rtx last_active_insn (basic_block, int);
90 static rtx find_active_insn_before (basic_block, rtx);
91 static rtx find_active_insn_after (basic_block, rtx);
92 static basic_block block_fallthru (basic_block);
93 static int cond_exec_process_insns (ce_if_block *, rtx, rtx, rtx, int, int);
94 static rtx cond_exec_get_condition (rtx);
95 static rtx noce_get_condition (rtx, rtx *, bool);
96 static int noce_operand_ok (const_rtx);
97 static void merge_if_block (ce_if_block *);
98 static int find_cond_trap (basic_block, edge, edge);
99 static basic_block find_if_header (basic_block, int);
100 static int block_jumps_and_fallthru_p (basic_block, basic_block);
101 static int noce_find_if_block (basic_block, edge, edge, int);
102 static int cond_exec_find_if_block (ce_if_block *);
103 static int find_if_case_1 (basic_block, edge, edge);
104 static int find_if_case_2 (basic_block, edge, edge);
105 static int dead_or_predicable (basic_block, basic_block, basic_block,
106 edge, int);
107 static void noce_emit_move_insn (rtx, rtx);
108 static rtx block_has_only_trap (basic_block);
110 /* Count the number of non-jump active insns in BB. */
112 static int
113 count_bb_insns (const_basic_block bb)
115 int count = 0;
116 rtx insn = BB_HEAD (bb);
118 while (1)
120 if (active_insn_p (insn) && !JUMP_P (insn))
121 count++;
123 if (insn == BB_END (bb))
124 break;
125 insn = NEXT_INSN (insn);
128 return count;
131 /* Determine whether the total insn_rtx_cost on non-jump insns in
132 basic block BB is less than MAX_COST. This function returns
133 false if the cost of any instruction could not be estimated.
135 The cost of the non-jump insns in BB is scaled by REG_BR_PROB_BASE
136 as those insns are being speculated. MAX_COST is scaled with SCALE
137 plus a small fudge factor. */
139 static bool
140 cheap_bb_rtx_cost_p (const_basic_block bb, int scale, int max_cost)
142 int count = 0;
143 rtx insn = BB_HEAD (bb);
144 bool speed = optimize_bb_for_speed_p (bb);
146 /* Set scale to REG_BR_PROB_BASE to void the identical scaling
147 applied to insn_rtx_cost when optimizing for size. Only do
148 this after combine because if-conversion might interfere with
149 passes before combine.
151 Use optimize_function_for_speed_p instead of the pre-defined
152 variable speed to make sure it is set to same value for all
153 basic blocks in one if-conversion transformation. */
154 if (!optimize_function_for_speed_p (cfun) && ifcvt_after_combine)
155 scale = REG_BR_PROB_BASE;
156 /* Our branch probability/scaling factors are just estimates and don't
157 account for cases where we can get speculation for free and other
158 secondary benefits. So we fudge the scale factor to make speculating
159 appear a little more profitable when optimizing for performance. */
160 else
161 scale += REG_BR_PROB_BASE / 8;
164 max_cost *= scale;
166 while (1)
168 if (NONJUMP_INSN_P (insn))
170 int cost = insn_rtx_cost (PATTERN (insn), speed) * REG_BR_PROB_BASE;
171 if (cost == 0)
172 return false;
174 /* If this instruction is the load or set of a "stack" register,
175 such as a floating point register on x87, then the cost of
176 speculatively executing this insn may need to include
177 the additional cost of popping its result off of the
178 register stack. Unfortunately, correctly recognizing and
179 accounting for this additional overhead is tricky, so for
180 now we simply prohibit such speculative execution. */
181 #ifdef STACK_REGS
183 rtx set = single_set (insn);
184 if (set && STACK_REG_P (SET_DEST (set)))
185 return false;
187 #endif
189 count += cost;
190 if (count >= max_cost)
191 return false;
193 else if (CALL_P (insn))
194 return false;
196 if (insn == BB_END (bb))
197 break;
198 insn = NEXT_INSN (insn);
201 return true;
204 /* Return the first non-jump active insn in the basic block. */
206 static rtx
207 first_active_insn (basic_block bb)
209 rtx insn = BB_HEAD (bb);
211 if (LABEL_P (insn))
213 if (insn == BB_END (bb))
214 return NULL_RTX;
215 insn = NEXT_INSN (insn);
218 while (NOTE_P (insn) || DEBUG_INSN_P (insn))
220 if (insn == BB_END (bb))
221 return NULL_RTX;
222 insn = NEXT_INSN (insn);
225 if (JUMP_P (insn))
226 return NULL_RTX;
228 return insn;
231 /* Return the last non-jump active (non-jump) insn in the basic block. */
233 static rtx
234 last_active_insn (basic_block bb, int skip_use_p)
236 rtx insn = BB_END (bb);
237 rtx head = BB_HEAD (bb);
239 while (NOTE_P (insn)
240 || JUMP_P (insn)
241 || DEBUG_INSN_P (insn)
242 || (skip_use_p
243 && NONJUMP_INSN_P (insn)
244 && GET_CODE (PATTERN (insn)) == USE))
246 if (insn == head)
247 return NULL_RTX;
248 insn = PREV_INSN (insn);
251 if (LABEL_P (insn))
252 return NULL_RTX;
254 return insn;
257 /* Return the active insn before INSN inside basic block CURR_BB. */
259 static rtx
260 find_active_insn_before (basic_block curr_bb, rtx insn)
262 if (!insn || insn == BB_HEAD (curr_bb))
263 return NULL_RTX;
265 while ((insn = PREV_INSN (insn)) != NULL_RTX)
267 if (NONJUMP_INSN_P (insn) || JUMP_P (insn) || CALL_P (insn))
268 break;
270 /* No other active insn all the way to the start of the basic block. */
271 if (insn == BB_HEAD (curr_bb))
272 return NULL_RTX;
275 return insn;
278 /* Return the active insn after INSN inside basic block CURR_BB. */
280 static rtx
281 find_active_insn_after (basic_block curr_bb, rtx insn)
283 if (!insn || insn == BB_END (curr_bb))
284 return NULL_RTX;
286 while ((insn = NEXT_INSN (insn)) != NULL_RTX)
288 if (NONJUMP_INSN_P (insn) || JUMP_P (insn) || CALL_P (insn))
289 break;
291 /* No other active insn all the way to the end of the basic block. */
292 if (insn == BB_END (curr_bb))
293 return NULL_RTX;
296 return insn;
299 /* Return the basic block reached by falling though the basic block BB. */
301 static basic_block
302 block_fallthru (basic_block bb)
304 edge e = find_fallthru_edge (bb->succs);
306 return (e) ? e->dest : NULL_BLOCK;
309 /* Return true if RTXs A and B can be safely interchanged. */
311 static bool
312 rtx_interchangeable_p (const_rtx a, const_rtx b)
314 if (!rtx_equal_p (a, b))
315 return false;
317 if (GET_CODE (a) != MEM)
318 return true;
320 /* A dead type-unsafe memory reference is legal, but a live type-unsafe memory
321 reference is not. Interchanging a dead type-unsafe memory reference with
322 a live type-safe one creates a live type-unsafe memory reference, in other
323 words, it makes the program illegal.
324 We check here conservatively whether the two memory references have equal
325 memory attributes. */
327 return mem_attrs_eq_p (get_mem_attrs (a), get_mem_attrs (b));
331 /* Go through a bunch of insns, converting them to conditional
332 execution format if possible. Return TRUE if all of the non-note
333 insns were processed. */
335 static int
336 cond_exec_process_insns (ce_if_block *ce_info ATTRIBUTE_UNUSED,
337 /* if block information */rtx start,
338 /* first insn to look at */rtx end,
339 /* last insn to look at */rtx test,
340 /* conditional execution test */int prob_val,
341 /* probability of branch taken. */int mod_ok)
343 int must_be_last = FALSE;
344 rtx insn;
345 rtx xtest;
346 rtx pattern;
348 if (!start || !end)
349 return FALSE;
351 for (insn = start; ; insn = NEXT_INSN (insn))
353 /* dwarf2out can't cope with conditional prologues. */
354 if (NOTE_P (insn) && NOTE_KIND (insn) == NOTE_INSN_PROLOGUE_END)
355 return FALSE;
357 if (NOTE_P (insn) || DEBUG_INSN_P (insn))
358 goto insn_done;
360 gcc_assert (NONJUMP_INSN_P (insn) || CALL_P (insn));
362 /* dwarf2out can't cope with conditional unwind info. */
363 if (RTX_FRAME_RELATED_P (insn))
364 return FALSE;
366 /* Remove USE insns that get in the way. */
367 if (reload_completed && GET_CODE (PATTERN (insn)) == USE)
369 /* ??? Ug. Actually unlinking the thing is problematic,
370 given what we'd have to coordinate with our callers. */
371 SET_INSN_DELETED (insn);
372 goto insn_done;
375 /* Last insn wasn't last? */
376 if (must_be_last)
377 return FALSE;
379 if (modified_in_p (test, insn))
381 if (!mod_ok)
382 return FALSE;
383 must_be_last = TRUE;
386 /* Now build the conditional form of the instruction. */
387 pattern = PATTERN (insn);
388 xtest = copy_rtx (test);
390 /* If this is already a COND_EXEC, rewrite the test to be an AND of the
391 two conditions. */
392 if (GET_CODE (pattern) == COND_EXEC)
394 if (GET_MODE (xtest) != GET_MODE (COND_EXEC_TEST (pattern)))
395 return FALSE;
397 xtest = gen_rtx_AND (GET_MODE (xtest), xtest,
398 COND_EXEC_TEST (pattern));
399 pattern = COND_EXEC_CODE (pattern);
402 pattern = gen_rtx_COND_EXEC (VOIDmode, xtest, pattern);
404 /* If the machine needs to modify the insn being conditionally executed,
405 say for example to force a constant integer operand into a temp
406 register, do so here. */
407 #ifdef IFCVT_MODIFY_INSN
408 IFCVT_MODIFY_INSN (ce_info, pattern, insn);
409 if (! pattern)
410 return FALSE;
411 #endif
413 validate_change (insn, &PATTERN (insn), pattern, 1);
415 if (CALL_P (insn) && prob_val >= 0)
416 validate_change (insn, &REG_NOTES (insn),
417 gen_rtx_INT_LIST ((enum machine_mode) REG_BR_PROB,
418 prob_val, REG_NOTES (insn)), 1);
420 insn_done:
421 if (insn == end)
422 break;
425 return TRUE;
428 /* Return the condition for a jump. Do not do any special processing. */
430 static rtx
431 cond_exec_get_condition (rtx jump)
433 rtx test_if, cond;
435 if (any_condjump_p (jump))
436 test_if = SET_SRC (pc_set (jump));
437 else
438 return NULL_RTX;
439 cond = XEXP (test_if, 0);
441 /* If this branches to JUMP_LABEL when the condition is false,
442 reverse the condition. */
443 if (GET_CODE (XEXP (test_if, 2)) == LABEL_REF
444 && XEXP (XEXP (test_if, 2), 0) == JUMP_LABEL (jump))
446 enum rtx_code rev = reversed_comparison_code (cond, jump);
447 if (rev == UNKNOWN)
448 return NULL_RTX;
450 cond = gen_rtx_fmt_ee (rev, GET_MODE (cond), XEXP (cond, 0),
451 XEXP (cond, 1));
454 return cond;
457 /* Given a simple IF-THEN or IF-THEN-ELSE block, attempt to convert it
458 to conditional execution. Return TRUE if we were successful at
459 converting the block. */
461 static int
462 cond_exec_process_if_block (ce_if_block * ce_info,
463 /* if block information */int do_multiple_p)
465 basic_block test_bb = ce_info->test_bb; /* last test block */
466 basic_block then_bb = ce_info->then_bb; /* THEN */
467 basic_block else_bb = ce_info->else_bb; /* ELSE or NULL */
468 rtx test_expr; /* expression in IF_THEN_ELSE that is tested */
469 rtx then_start; /* first insn in THEN block */
470 rtx then_end; /* last insn + 1 in THEN block */
471 rtx else_start = NULL_RTX; /* first insn in ELSE block or NULL */
472 rtx else_end = NULL_RTX; /* last insn + 1 in ELSE block */
473 int max; /* max # of insns to convert. */
474 int then_mod_ok; /* whether conditional mods are ok in THEN */
475 rtx true_expr; /* test for else block insns */
476 rtx false_expr; /* test for then block insns */
477 int true_prob_val; /* probability of else block */
478 int false_prob_val; /* probability of then block */
479 rtx then_last_head = NULL_RTX; /* Last match at the head of THEN */
480 rtx else_last_head = NULL_RTX; /* Last match at the head of ELSE */
481 rtx then_first_tail = NULL_RTX; /* First match at the tail of THEN */
482 rtx else_first_tail = NULL_RTX; /* First match at the tail of ELSE */
483 int then_n_insns, else_n_insns, n_insns;
484 enum rtx_code false_code;
485 rtx note;
487 /* If test is comprised of && or || elements, and we've failed at handling
488 all of them together, just use the last test if it is the special case of
489 && elements without an ELSE block. */
490 if (!do_multiple_p && ce_info->num_multiple_test_blocks)
492 if (else_bb || ! ce_info->and_and_p)
493 return FALSE;
495 ce_info->test_bb = test_bb = ce_info->last_test_bb;
496 ce_info->num_multiple_test_blocks = 0;
497 ce_info->num_and_and_blocks = 0;
498 ce_info->num_or_or_blocks = 0;
501 /* Find the conditional jump to the ELSE or JOIN part, and isolate
502 the test. */
503 test_expr = cond_exec_get_condition (BB_END (test_bb));
504 if (! test_expr)
505 return FALSE;
507 /* If the conditional jump is more than just a conditional jump,
508 then we can not do conditional execution conversion on this block. */
509 if (! onlyjump_p (BB_END (test_bb)))
510 return FALSE;
512 /* Collect the bounds of where we're to search, skipping any labels, jumps
513 and notes at the beginning and end of the block. Then count the total
514 number of insns and see if it is small enough to convert. */
515 then_start = first_active_insn (then_bb);
516 then_end = last_active_insn (then_bb, TRUE);
517 then_n_insns = ce_info->num_then_insns = count_bb_insns (then_bb);
518 n_insns = then_n_insns;
519 max = MAX_CONDITIONAL_EXECUTE;
521 if (else_bb)
523 int n_matching;
525 max *= 2;
526 else_start = first_active_insn (else_bb);
527 else_end = last_active_insn (else_bb, TRUE);
528 else_n_insns = ce_info->num_else_insns = count_bb_insns (else_bb);
529 n_insns += else_n_insns;
531 /* Look for matching sequences at the head and tail of the two blocks,
532 and limit the range of insns to be converted if possible. */
533 n_matching = flow_find_cross_jump (then_bb, else_bb,
534 &then_first_tail, &else_first_tail,
535 NULL);
536 if (then_first_tail == BB_HEAD (then_bb))
537 then_start = then_end = NULL_RTX;
538 if (else_first_tail == BB_HEAD (else_bb))
539 else_start = else_end = NULL_RTX;
541 if (n_matching > 0)
543 if (then_end)
544 then_end = find_active_insn_before (then_bb, then_first_tail);
545 if (else_end)
546 else_end = find_active_insn_before (else_bb, else_first_tail);
547 n_insns -= 2 * n_matching;
550 if (then_start
551 && else_start
552 && then_n_insns > n_matching
553 && else_n_insns > n_matching)
555 int longest_match = MIN (then_n_insns - n_matching,
556 else_n_insns - n_matching);
557 n_matching
558 = flow_find_head_matching_sequence (then_bb, else_bb,
559 &then_last_head,
560 &else_last_head,
561 longest_match);
563 if (n_matching > 0)
565 rtx insn;
567 /* We won't pass the insns in the head sequence to
568 cond_exec_process_insns, so we need to test them here
569 to make sure that they don't clobber the condition. */
570 for (insn = BB_HEAD (then_bb);
571 insn != NEXT_INSN (then_last_head);
572 insn = NEXT_INSN (insn))
573 if (!LABEL_P (insn) && !NOTE_P (insn)
574 && !DEBUG_INSN_P (insn)
575 && modified_in_p (test_expr, insn))
576 return FALSE;
579 if (then_last_head == then_end)
580 then_start = then_end = NULL_RTX;
581 if (else_last_head == else_end)
582 else_start = else_end = NULL_RTX;
584 if (n_matching > 0)
586 if (then_start)
587 then_start = find_active_insn_after (then_bb, then_last_head);
588 if (else_start)
589 else_start = find_active_insn_after (else_bb, else_last_head);
590 n_insns -= 2 * n_matching;
595 if (n_insns > max)
596 return FALSE;
598 /* Map test_expr/test_jump into the appropriate MD tests to use on
599 the conditionally executed code. */
601 true_expr = test_expr;
603 false_code = reversed_comparison_code (true_expr, BB_END (test_bb));
604 if (false_code != UNKNOWN)
605 false_expr = gen_rtx_fmt_ee (false_code, GET_MODE (true_expr),
606 XEXP (true_expr, 0), XEXP (true_expr, 1));
607 else
608 false_expr = NULL_RTX;
610 #ifdef IFCVT_MODIFY_TESTS
611 /* If the machine description needs to modify the tests, such as setting a
612 conditional execution register from a comparison, it can do so here. */
613 IFCVT_MODIFY_TESTS (ce_info, true_expr, false_expr);
615 /* See if the conversion failed. */
616 if (!true_expr || !false_expr)
617 goto fail;
618 #endif
620 note = find_reg_note (BB_END (test_bb), REG_BR_PROB, NULL_RTX);
621 if (note)
623 true_prob_val = XINT (note, 0);
624 false_prob_val = REG_BR_PROB_BASE - true_prob_val;
626 else
628 true_prob_val = -1;
629 false_prob_val = -1;
632 /* If we have && or || tests, do them here. These tests are in the adjacent
633 blocks after the first block containing the test. */
634 if (ce_info->num_multiple_test_blocks > 0)
636 basic_block bb = test_bb;
637 basic_block last_test_bb = ce_info->last_test_bb;
639 if (! false_expr)
640 goto fail;
644 rtx start, end;
645 rtx t, f;
646 enum rtx_code f_code;
648 bb = block_fallthru (bb);
649 start = first_active_insn (bb);
650 end = last_active_insn (bb, TRUE);
651 if (start
652 && ! cond_exec_process_insns (ce_info, start, end, false_expr,
653 false_prob_val, FALSE))
654 goto fail;
656 /* If the conditional jump is more than just a conditional jump, then
657 we can not do conditional execution conversion on this block. */
658 if (! onlyjump_p (BB_END (bb)))
659 goto fail;
661 /* Find the conditional jump and isolate the test. */
662 t = cond_exec_get_condition (BB_END (bb));
663 if (! t)
664 goto fail;
666 f_code = reversed_comparison_code (t, BB_END (bb));
667 if (f_code == UNKNOWN)
668 goto fail;
670 f = gen_rtx_fmt_ee (f_code, GET_MODE (t), XEXP (t, 0), XEXP (t, 1));
671 if (ce_info->and_and_p)
673 t = gen_rtx_AND (GET_MODE (t), true_expr, t);
674 f = gen_rtx_IOR (GET_MODE (t), false_expr, f);
676 else
678 t = gen_rtx_IOR (GET_MODE (t), true_expr, t);
679 f = gen_rtx_AND (GET_MODE (t), false_expr, f);
682 /* If the machine description needs to modify the tests, such as
683 setting a conditional execution register from a comparison, it can
684 do so here. */
685 #ifdef IFCVT_MODIFY_MULTIPLE_TESTS
686 IFCVT_MODIFY_MULTIPLE_TESTS (ce_info, bb, t, f);
688 /* See if the conversion failed. */
689 if (!t || !f)
690 goto fail;
691 #endif
693 true_expr = t;
694 false_expr = f;
696 while (bb != last_test_bb);
699 /* For IF-THEN-ELSE blocks, we don't allow modifications of the test
700 on then THEN block. */
701 then_mod_ok = (else_bb == NULL_BLOCK);
703 /* Go through the THEN and ELSE blocks converting the insns if possible
704 to conditional execution. */
706 if (then_end
707 && (! false_expr
708 || ! cond_exec_process_insns (ce_info, then_start, then_end,
709 false_expr, false_prob_val,
710 then_mod_ok)))
711 goto fail;
713 if (else_bb && else_end
714 && ! cond_exec_process_insns (ce_info, else_start, else_end,
715 true_expr, true_prob_val, TRUE))
716 goto fail;
718 /* If we cannot apply the changes, fail. Do not go through the normal fail
719 processing, since apply_change_group will call cancel_changes. */
720 if (! apply_change_group ())
722 #ifdef IFCVT_MODIFY_CANCEL
723 /* Cancel any machine dependent changes. */
724 IFCVT_MODIFY_CANCEL (ce_info);
725 #endif
726 return FALSE;
729 #ifdef IFCVT_MODIFY_FINAL
730 /* Do any machine dependent final modifications. */
731 IFCVT_MODIFY_FINAL (ce_info);
732 #endif
734 /* Conversion succeeded. */
735 if (dump_file)
736 fprintf (dump_file, "%d insn%s converted to conditional execution.\n",
737 n_insns, (n_insns == 1) ? " was" : "s were");
739 /* Merge the blocks! If we had matching sequences, make sure to delete one
740 copy at the appropriate location first: delete the copy in the THEN branch
741 for a tail sequence so that the remaining one is executed last for both
742 branches, and delete the copy in the ELSE branch for a head sequence so
743 that the remaining one is executed first for both branches. */
744 if (then_first_tail)
746 rtx from = then_first_tail;
747 if (!INSN_P (from))
748 from = find_active_insn_after (then_bb, from);
749 delete_insn_chain (from, BB_END (then_bb), false);
751 if (else_last_head)
752 delete_insn_chain (first_active_insn (else_bb), else_last_head, false);
754 merge_if_block (ce_info);
755 cond_exec_changed_p = TRUE;
756 return TRUE;
758 fail:
759 #ifdef IFCVT_MODIFY_CANCEL
760 /* Cancel any machine dependent changes. */
761 IFCVT_MODIFY_CANCEL (ce_info);
762 #endif
764 cancel_changes (0);
765 return FALSE;
768 /* Used by noce_process_if_block to communicate with its subroutines.
770 The subroutines know that A and B may be evaluated freely. They
771 know that X is a register. They should insert new instructions
772 before cond_earliest. */
774 struct noce_if_info
776 /* The basic blocks that make up the IF-THEN-{ELSE-,}JOIN block. */
777 basic_block test_bb, then_bb, else_bb, join_bb;
779 /* The jump that ends TEST_BB. */
780 rtx jump;
782 /* The jump condition. */
783 rtx cond;
785 /* New insns should be inserted before this one. */
786 rtx cond_earliest;
788 /* Insns in the THEN and ELSE block. There is always just this
789 one insns in those blocks. The insns are single_set insns.
790 If there was no ELSE block, INSN_B is the last insn before
791 COND_EARLIEST, or NULL_RTX. In the former case, the insn
792 operands are still valid, as if INSN_B was moved down below
793 the jump. */
794 rtx insn_a, insn_b;
796 /* The SET_SRC of INSN_A and INSN_B. */
797 rtx a, b;
799 /* The SET_DEST of INSN_A. */
800 rtx x;
802 /* True if this if block is not canonical. In the canonical form of
803 if blocks, the THEN_BB is the block reached via the fallthru edge
804 from TEST_BB. For the noce transformations, we allow the symmetric
805 form as well. */
806 bool then_else_reversed;
808 /* Estimated cost of the particular branch instruction. */
809 int branch_cost;
812 static rtx noce_emit_store_flag (struct noce_if_info *, rtx, int, int);
813 static int noce_try_move (struct noce_if_info *);
814 static int noce_try_store_flag (struct noce_if_info *);
815 static int noce_try_addcc (struct noce_if_info *);
816 static int noce_try_store_flag_constants (struct noce_if_info *);
817 static int noce_try_store_flag_mask (struct noce_if_info *);
818 static rtx noce_emit_cmove (struct noce_if_info *, rtx, enum rtx_code, rtx,
819 rtx, rtx, rtx);
820 static int noce_try_cmove (struct noce_if_info *);
821 static int noce_try_cmove_arith (struct noce_if_info *);
822 static rtx noce_get_alt_condition (struct noce_if_info *, rtx, rtx *);
823 static int noce_try_minmax (struct noce_if_info *);
824 static int noce_try_abs (struct noce_if_info *);
825 static int noce_try_sign_mask (struct noce_if_info *);
827 /* Helper function for noce_try_store_flag*. */
829 static rtx
830 noce_emit_store_flag (struct noce_if_info *if_info, rtx x, int reversep,
831 int normalize)
833 rtx cond = if_info->cond;
834 int cond_complex;
835 enum rtx_code code;
837 cond_complex = (! general_operand (XEXP (cond, 0), VOIDmode)
838 || ! general_operand (XEXP (cond, 1), VOIDmode));
840 /* If earliest == jump, or when the condition is complex, try to
841 build the store_flag insn directly. */
843 if (cond_complex)
845 rtx set = pc_set (if_info->jump);
846 cond = XEXP (SET_SRC (set), 0);
847 if (GET_CODE (XEXP (SET_SRC (set), 2)) == LABEL_REF
848 && XEXP (XEXP (SET_SRC (set), 2), 0) == JUMP_LABEL (if_info->jump))
849 reversep = !reversep;
850 if (if_info->then_else_reversed)
851 reversep = !reversep;
854 if (reversep)
855 code = reversed_comparison_code (cond, if_info->jump);
856 else
857 code = GET_CODE (cond);
859 if ((if_info->cond_earliest == if_info->jump || cond_complex)
860 && (normalize == 0 || STORE_FLAG_VALUE == normalize))
862 rtx tmp;
864 tmp = gen_rtx_fmt_ee (code, GET_MODE (x), XEXP (cond, 0),
865 XEXP (cond, 1));
866 tmp = gen_rtx_SET (VOIDmode, x, tmp);
868 start_sequence ();
869 tmp = emit_insn (tmp);
871 if (recog_memoized (tmp) >= 0)
873 tmp = get_insns ();
874 end_sequence ();
875 emit_insn (tmp);
877 if_info->cond_earliest = if_info->jump;
879 return x;
882 end_sequence ();
885 /* Don't even try if the comparison operands or the mode of X are weird. */
886 if (cond_complex || !SCALAR_INT_MODE_P (GET_MODE (x)))
887 return NULL_RTX;
889 return emit_store_flag (x, code, XEXP (cond, 0),
890 XEXP (cond, 1), VOIDmode,
891 (code == LTU || code == LEU
892 || code == GEU || code == GTU), normalize);
895 /* Emit instruction to move an rtx, possibly into STRICT_LOW_PART.
896 X is the destination/target and Y is the value to copy. */
898 static void
899 noce_emit_move_insn (rtx x, rtx y)
901 enum machine_mode outmode;
902 rtx outer, inner;
903 int bitpos;
905 if (GET_CODE (x) != STRICT_LOW_PART)
907 rtx seq, insn, target;
908 optab ot;
910 start_sequence ();
911 /* Check that the SET_SRC is reasonable before calling emit_move_insn,
912 otherwise construct a suitable SET pattern ourselves. */
913 insn = (OBJECT_P (y) || CONSTANT_P (y) || GET_CODE (y) == SUBREG)
914 ? emit_move_insn (x, y)
915 : emit_insn (gen_rtx_SET (VOIDmode, x, y));
916 seq = get_insns ();
917 end_sequence ();
919 if (recog_memoized (insn) <= 0)
921 if (GET_CODE (x) == ZERO_EXTRACT)
923 rtx op = XEXP (x, 0);
924 unsigned HOST_WIDE_INT size = INTVAL (XEXP (x, 1));
925 unsigned HOST_WIDE_INT start = INTVAL (XEXP (x, 2));
927 /* store_bit_field expects START to be relative to
928 BYTES_BIG_ENDIAN and adjusts this value for machines with
929 BITS_BIG_ENDIAN != BYTES_BIG_ENDIAN. In order to be able to
930 invoke store_bit_field again it is necessary to have the START
931 value from the first call. */
932 if (BITS_BIG_ENDIAN != BYTES_BIG_ENDIAN)
934 if (MEM_P (op))
935 start = BITS_PER_UNIT - start - size;
936 else
938 gcc_assert (REG_P (op));
939 start = BITS_PER_WORD - start - size;
943 gcc_assert (start < (MEM_P (op) ? BITS_PER_UNIT : BITS_PER_WORD));
944 store_bit_field (op, size, start, 0, 0, GET_MODE (x), y);
945 return;
948 switch (GET_RTX_CLASS (GET_CODE (y)))
950 case RTX_UNARY:
951 ot = code_to_optab (GET_CODE (y));
952 if (ot)
954 start_sequence ();
955 target = expand_unop (GET_MODE (y), ot, XEXP (y, 0), x, 0);
956 if (target != NULL_RTX)
958 if (target != x)
959 emit_move_insn (x, target);
960 seq = get_insns ();
962 end_sequence ();
964 break;
966 case RTX_BIN_ARITH:
967 case RTX_COMM_ARITH:
968 ot = code_to_optab (GET_CODE (y));
969 if (ot)
971 start_sequence ();
972 target = expand_binop (GET_MODE (y), ot,
973 XEXP (y, 0), XEXP (y, 1),
974 x, 0, OPTAB_DIRECT);
975 if (target != NULL_RTX)
977 if (target != x)
978 emit_move_insn (x, target);
979 seq = get_insns ();
981 end_sequence ();
983 break;
985 default:
986 break;
990 emit_insn (seq);
991 return;
994 outer = XEXP (x, 0);
995 inner = XEXP (outer, 0);
996 outmode = GET_MODE (outer);
997 bitpos = SUBREG_BYTE (outer) * BITS_PER_UNIT;
998 store_bit_field (inner, GET_MODE_BITSIZE (outmode), bitpos,
999 0, 0, outmode, y);
1002 /* Return sequence of instructions generated by if conversion. This
1003 function calls end_sequence() to end the current stream, ensures
1004 that are instructions are unshared, recognizable non-jump insns.
1005 On failure, this function returns a NULL_RTX. */
1007 static rtx
1008 end_ifcvt_sequence (struct noce_if_info *if_info)
1010 rtx insn;
1011 rtx seq = get_insns ();
1013 set_used_flags (if_info->x);
1014 set_used_flags (if_info->cond);
1015 set_used_flags (if_info->a);
1016 set_used_flags (if_info->b);
1017 unshare_all_rtl_in_chain (seq);
1018 end_sequence ();
1020 /* Make sure that all of the instructions emitted are recognizable,
1021 and that we haven't introduced a new jump instruction.
1022 As an exercise for the reader, build a general mechanism that
1023 allows proper placement of required clobbers. */
1024 for (insn = seq; insn; insn = NEXT_INSN (insn))
1025 if (JUMP_P (insn)
1026 || recog_memoized (insn) == -1)
1027 return NULL_RTX;
1029 return seq;
1032 /* Convert "if (a != b) x = a; else x = b" into "x = a" and
1033 "if (a == b) x = a; else x = b" into "x = b". */
1035 static int
1036 noce_try_move (struct noce_if_info *if_info)
1038 rtx cond = if_info->cond;
1039 enum rtx_code code = GET_CODE (cond);
1040 rtx y, seq;
1042 if (code != NE && code != EQ)
1043 return FALSE;
1045 /* This optimization isn't valid if either A or B could be a NaN
1046 or a signed zero. */
1047 if (HONOR_NANS (GET_MODE (if_info->x))
1048 || HONOR_SIGNED_ZEROS (GET_MODE (if_info->x)))
1049 return FALSE;
1051 /* Check whether the operands of the comparison are A and in
1052 either order. */
1053 if ((rtx_equal_p (if_info->a, XEXP (cond, 0))
1054 && rtx_equal_p (if_info->b, XEXP (cond, 1)))
1055 || (rtx_equal_p (if_info->a, XEXP (cond, 1))
1056 && rtx_equal_p (if_info->b, XEXP (cond, 0))))
1058 if (!rtx_interchangeable_p (if_info->a, if_info->b))
1059 return FALSE;
1061 y = (code == EQ) ? if_info->a : if_info->b;
1063 /* Avoid generating the move if the source is the destination. */
1064 if (! rtx_equal_p (if_info->x, y))
1066 start_sequence ();
1067 noce_emit_move_insn (if_info->x, y);
1068 seq = end_ifcvt_sequence (if_info);
1069 if (!seq)
1070 return FALSE;
1072 emit_insn_before_setloc (seq, if_info->jump,
1073 INSN_LOCATION (if_info->insn_a));
1075 return TRUE;
1077 return FALSE;
1080 /* Convert "if (test) x = 1; else x = 0".
1082 Only try 0 and STORE_FLAG_VALUE here. Other combinations will be
1083 tried in noce_try_store_flag_constants after noce_try_cmove has had
1084 a go at the conversion. */
1086 static int
1087 noce_try_store_flag (struct noce_if_info *if_info)
1089 int reversep;
1090 rtx target, seq;
1092 if (CONST_INT_P (if_info->b)
1093 && INTVAL (if_info->b) == STORE_FLAG_VALUE
1094 && if_info->a == const0_rtx)
1095 reversep = 0;
1096 else if (if_info->b == const0_rtx
1097 && CONST_INT_P (if_info->a)
1098 && INTVAL (if_info->a) == STORE_FLAG_VALUE
1099 && (reversed_comparison_code (if_info->cond, if_info->jump)
1100 != UNKNOWN))
1101 reversep = 1;
1102 else
1103 return FALSE;
1105 start_sequence ();
1107 target = noce_emit_store_flag (if_info, if_info->x, reversep, 0);
1108 if (target)
1110 if (target != if_info->x)
1111 noce_emit_move_insn (if_info->x, target);
1113 seq = end_ifcvt_sequence (if_info);
1114 if (! seq)
1115 return FALSE;
1117 emit_insn_before_setloc (seq, if_info->jump,
1118 INSN_LOCATION (if_info->insn_a));
1119 return TRUE;
1121 else
1123 end_sequence ();
1124 return FALSE;
1128 /* Convert "if (test) x = a; else x = b", for A and B constant. */
1130 static int
1131 noce_try_store_flag_constants (struct noce_if_info *if_info)
1133 rtx target, seq;
1134 int reversep;
1135 HOST_WIDE_INT itrue, ifalse, diff, tmp;
1136 int normalize, can_reverse;
1137 enum machine_mode mode;
1139 if (CONST_INT_P (if_info->a)
1140 && CONST_INT_P (if_info->b))
1142 mode = GET_MODE (if_info->x);
1143 ifalse = INTVAL (if_info->a);
1144 itrue = INTVAL (if_info->b);
1146 diff = (unsigned HOST_WIDE_INT) itrue - ifalse;
1147 /* Make sure we can represent the difference between the two values. */
1148 if ((diff > 0)
1149 != ((ifalse < 0) != (itrue < 0) ? ifalse < 0 : ifalse < itrue))
1150 return FALSE;
1152 diff = trunc_int_for_mode (diff, mode);
1154 can_reverse = (reversed_comparison_code (if_info->cond, if_info->jump)
1155 != UNKNOWN);
1157 reversep = 0;
1158 if (diff == STORE_FLAG_VALUE || diff == -STORE_FLAG_VALUE)
1159 normalize = 0;
1160 else if (ifalse == 0 && exact_log2 (itrue) >= 0
1161 && (STORE_FLAG_VALUE == 1
1162 || if_info->branch_cost >= 2))
1163 normalize = 1;
1164 else if (itrue == 0 && exact_log2 (ifalse) >= 0 && can_reverse
1165 && (STORE_FLAG_VALUE == 1 || if_info->branch_cost >= 2))
1166 normalize = 1, reversep = 1;
1167 else if (itrue == -1
1168 && (STORE_FLAG_VALUE == -1
1169 || if_info->branch_cost >= 2))
1170 normalize = -1;
1171 else if (ifalse == -1 && can_reverse
1172 && (STORE_FLAG_VALUE == -1 || if_info->branch_cost >= 2))
1173 normalize = -1, reversep = 1;
1174 else if ((if_info->branch_cost >= 2 && STORE_FLAG_VALUE == -1)
1175 || if_info->branch_cost >= 3)
1176 normalize = -1;
1177 else
1178 return FALSE;
1180 if (reversep)
1182 tmp = itrue; itrue = ifalse; ifalse = tmp;
1183 diff = trunc_int_for_mode (-(unsigned HOST_WIDE_INT) diff, mode);
1186 start_sequence ();
1187 target = noce_emit_store_flag (if_info, if_info->x, reversep, normalize);
1188 if (! target)
1190 end_sequence ();
1191 return FALSE;
1194 /* if (test) x = 3; else x = 4;
1195 => x = 3 + (test == 0); */
1196 if (diff == STORE_FLAG_VALUE || diff == -STORE_FLAG_VALUE)
1198 target = expand_simple_binop (mode,
1199 (diff == STORE_FLAG_VALUE
1200 ? PLUS : MINUS),
1201 gen_int_mode (ifalse, mode), target,
1202 if_info->x, 0, OPTAB_WIDEN);
1205 /* if (test) x = 8; else x = 0;
1206 => x = (test != 0) << 3; */
1207 else if (ifalse == 0 && (tmp = exact_log2 (itrue)) >= 0)
1209 target = expand_simple_binop (mode, ASHIFT,
1210 target, GEN_INT (tmp), if_info->x, 0,
1211 OPTAB_WIDEN);
1214 /* if (test) x = -1; else x = b;
1215 => x = -(test != 0) | b; */
1216 else if (itrue == -1)
1218 target = expand_simple_binop (mode, IOR,
1219 target, gen_int_mode (ifalse, mode),
1220 if_info->x, 0, OPTAB_WIDEN);
1223 /* if (test) x = a; else x = b;
1224 => x = (-(test != 0) & (b - a)) + a; */
1225 else
1227 target = expand_simple_binop (mode, AND,
1228 target, gen_int_mode (diff, mode),
1229 if_info->x, 0, OPTAB_WIDEN);
1230 if (target)
1231 target = expand_simple_binop (mode, PLUS,
1232 target, gen_int_mode (ifalse, mode),
1233 if_info->x, 0, OPTAB_WIDEN);
1236 if (! target)
1238 end_sequence ();
1239 return FALSE;
1242 if (target != if_info->x)
1243 noce_emit_move_insn (if_info->x, target);
1245 seq = end_ifcvt_sequence (if_info);
1246 if (!seq)
1247 return FALSE;
1249 emit_insn_before_setloc (seq, if_info->jump,
1250 INSN_LOCATION (if_info->insn_a));
1251 return TRUE;
1254 return FALSE;
1257 /* Convert "if (test) foo++" into "foo += (test != 0)", and
1258 similarly for "foo--". */
1260 static int
1261 noce_try_addcc (struct noce_if_info *if_info)
1263 rtx target, seq;
1264 int subtract, normalize;
1266 if (GET_CODE (if_info->a) == PLUS
1267 && rtx_equal_p (XEXP (if_info->a, 0), if_info->b)
1268 && (reversed_comparison_code (if_info->cond, if_info->jump)
1269 != UNKNOWN))
1271 rtx cond = if_info->cond;
1272 enum rtx_code code = reversed_comparison_code (cond, if_info->jump);
1274 /* First try to use addcc pattern. */
1275 if (general_operand (XEXP (cond, 0), VOIDmode)
1276 && general_operand (XEXP (cond, 1), VOIDmode))
1278 start_sequence ();
1279 target = emit_conditional_add (if_info->x, code,
1280 XEXP (cond, 0),
1281 XEXP (cond, 1),
1282 VOIDmode,
1283 if_info->b,
1284 XEXP (if_info->a, 1),
1285 GET_MODE (if_info->x),
1286 (code == LTU || code == GEU
1287 || code == LEU || code == GTU));
1288 if (target)
1290 if (target != if_info->x)
1291 noce_emit_move_insn (if_info->x, target);
1293 seq = end_ifcvt_sequence (if_info);
1294 if (!seq)
1295 return FALSE;
1297 emit_insn_before_setloc (seq, if_info->jump,
1298 INSN_LOCATION (if_info->insn_a));
1299 return TRUE;
1301 end_sequence ();
1304 /* If that fails, construct conditional increment or decrement using
1305 setcc. */
1306 if (if_info->branch_cost >= 2
1307 && (XEXP (if_info->a, 1) == const1_rtx
1308 || XEXP (if_info->a, 1) == constm1_rtx))
1310 start_sequence ();
1311 if (STORE_FLAG_VALUE == INTVAL (XEXP (if_info->a, 1)))
1312 subtract = 0, normalize = 0;
1313 else if (-STORE_FLAG_VALUE == INTVAL (XEXP (if_info->a, 1)))
1314 subtract = 1, normalize = 0;
1315 else
1316 subtract = 0, normalize = INTVAL (XEXP (if_info->a, 1));
1319 target = noce_emit_store_flag (if_info,
1320 gen_reg_rtx (GET_MODE (if_info->x)),
1321 1, normalize);
1323 if (target)
1324 target = expand_simple_binop (GET_MODE (if_info->x),
1325 subtract ? MINUS : PLUS,
1326 if_info->b, target, if_info->x,
1327 0, OPTAB_WIDEN);
1328 if (target)
1330 if (target != if_info->x)
1331 noce_emit_move_insn (if_info->x, target);
1333 seq = end_ifcvt_sequence (if_info);
1334 if (!seq)
1335 return FALSE;
1337 emit_insn_before_setloc (seq, if_info->jump,
1338 INSN_LOCATION (if_info->insn_a));
1339 return TRUE;
1341 end_sequence ();
1345 return FALSE;
1348 /* Convert "if (test) x = 0;" to "x &= -(test == 0);" */
1350 static int
1351 noce_try_store_flag_mask (struct noce_if_info *if_info)
1353 rtx target, seq;
1354 int reversep;
1356 reversep = 0;
1357 if ((if_info->branch_cost >= 2
1358 || STORE_FLAG_VALUE == -1)
1359 && ((if_info->a == const0_rtx
1360 && rtx_equal_p (if_info->b, if_info->x))
1361 || ((reversep = (reversed_comparison_code (if_info->cond,
1362 if_info->jump)
1363 != UNKNOWN))
1364 && if_info->b == const0_rtx
1365 && rtx_equal_p (if_info->a, if_info->x))))
1367 start_sequence ();
1368 target = noce_emit_store_flag (if_info,
1369 gen_reg_rtx (GET_MODE (if_info->x)),
1370 reversep, -1);
1371 if (target)
1372 target = expand_simple_binop (GET_MODE (if_info->x), AND,
1373 if_info->x,
1374 target, if_info->x, 0,
1375 OPTAB_WIDEN);
1377 if (target)
1379 if (target != if_info->x)
1380 noce_emit_move_insn (if_info->x, target);
1382 seq = end_ifcvt_sequence (if_info);
1383 if (!seq)
1384 return FALSE;
1386 emit_insn_before_setloc (seq, if_info->jump,
1387 INSN_LOCATION (if_info->insn_a));
1388 return TRUE;
1391 end_sequence ();
1394 return FALSE;
1397 /* Helper function for noce_try_cmove and noce_try_cmove_arith. */
1399 static rtx
1400 noce_emit_cmove (struct noce_if_info *if_info, rtx x, enum rtx_code code,
1401 rtx cmp_a, rtx cmp_b, rtx vfalse, rtx vtrue)
1403 rtx target ATTRIBUTE_UNUSED;
1404 int unsignedp ATTRIBUTE_UNUSED;
1406 /* If earliest == jump, try to build the cmove insn directly.
1407 This is helpful when combine has created some complex condition
1408 (like for alpha's cmovlbs) that we can't hope to regenerate
1409 through the normal interface. */
1411 if (if_info->cond_earliest == if_info->jump)
1413 rtx tmp;
1415 tmp = gen_rtx_fmt_ee (code, GET_MODE (if_info->cond), cmp_a, cmp_b);
1416 tmp = gen_rtx_IF_THEN_ELSE (GET_MODE (x), tmp, vtrue, vfalse);
1417 tmp = gen_rtx_SET (VOIDmode, x, tmp);
1419 start_sequence ();
1420 tmp = emit_insn (tmp);
1422 if (recog_memoized (tmp) >= 0)
1424 tmp = get_insns ();
1425 end_sequence ();
1426 emit_insn (tmp);
1428 return x;
1431 end_sequence ();
1434 /* Don't even try if the comparison operands are weird. */
1435 if (! general_operand (cmp_a, GET_MODE (cmp_a))
1436 || ! general_operand (cmp_b, GET_MODE (cmp_b)))
1437 return NULL_RTX;
1439 #if HAVE_conditional_move
1440 unsignedp = (code == LTU || code == GEU
1441 || code == LEU || code == GTU);
1443 target = emit_conditional_move (x, code, cmp_a, cmp_b, VOIDmode,
1444 vtrue, vfalse, GET_MODE (x),
1445 unsignedp);
1446 if (target)
1447 return target;
1449 /* We might be faced with a situation like:
1451 x = (reg:M TARGET)
1452 vtrue = (subreg:M (reg:N VTRUE) BYTE)
1453 vfalse = (subreg:M (reg:N VFALSE) BYTE)
1455 We can't do a conditional move in mode M, but it's possible that we
1456 could do a conditional move in mode N instead and take a subreg of
1457 the result.
1459 If we can't create new pseudos, though, don't bother. */
1460 if (reload_completed)
1461 return NULL_RTX;
1463 if (GET_CODE (vtrue) == SUBREG && GET_CODE (vfalse) == SUBREG)
1465 rtx reg_vtrue = SUBREG_REG (vtrue);
1466 rtx reg_vfalse = SUBREG_REG (vfalse);
1467 unsigned int byte_vtrue = SUBREG_BYTE (vtrue);
1468 unsigned int byte_vfalse = SUBREG_BYTE (vfalse);
1469 rtx promoted_target;
1471 if (GET_MODE (reg_vtrue) != GET_MODE (reg_vfalse)
1472 || byte_vtrue != byte_vfalse
1473 || (SUBREG_PROMOTED_VAR_P (vtrue)
1474 != SUBREG_PROMOTED_VAR_P (vfalse))
1475 || (SUBREG_PROMOTED_GET (vtrue)
1476 != SUBREG_PROMOTED_GET (vfalse)))
1477 return NULL_RTX;
1479 promoted_target = gen_reg_rtx (GET_MODE (reg_vtrue));
1481 target = emit_conditional_move (promoted_target, code, cmp_a, cmp_b,
1482 VOIDmode, reg_vtrue, reg_vfalse,
1483 GET_MODE (reg_vtrue), unsignedp);
1484 /* Nope, couldn't do it in that mode either. */
1485 if (!target)
1486 return NULL_RTX;
1488 target = gen_rtx_SUBREG (GET_MODE (vtrue), promoted_target, byte_vtrue);
1489 SUBREG_PROMOTED_VAR_P (target) = SUBREG_PROMOTED_VAR_P (vtrue);
1490 SUBREG_PROMOTED_SET (target, SUBREG_PROMOTED_GET (vtrue));
1491 emit_move_insn (x, target);
1492 return x;
1494 else
1495 return NULL_RTX;
1496 #else
1497 /* We'll never get here, as noce_process_if_block doesn't call the
1498 functions involved. Ifdef code, however, should be discouraged
1499 because it leads to typos in the code not selected. However,
1500 emit_conditional_move won't exist either. */
1501 return NULL_RTX;
1502 #endif
1505 /* Try only simple constants and registers here. More complex cases
1506 are handled in noce_try_cmove_arith after noce_try_store_flag_arith
1507 has had a go at it. */
1509 static int
1510 noce_try_cmove (struct noce_if_info *if_info)
1512 enum rtx_code code;
1513 rtx target, seq;
1515 if ((CONSTANT_P (if_info->a) || register_operand (if_info->a, VOIDmode))
1516 && (CONSTANT_P (if_info->b) || register_operand (if_info->b, VOIDmode)))
1518 start_sequence ();
1520 code = GET_CODE (if_info->cond);
1521 target = noce_emit_cmove (if_info, if_info->x, code,
1522 XEXP (if_info->cond, 0),
1523 XEXP (if_info->cond, 1),
1524 if_info->a, if_info->b);
1526 if (target)
1528 if (target != if_info->x)
1529 noce_emit_move_insn (if_info->x, target);
1531 seq = end_ifcvt_sequence (if_info);
1532 if (!seq)
1533 return FALSE;
1535 emit_insn_before_setloc (seq, if_info->jump,
1536 INSN_LOCATION (if_info->insn_a));
1537 return TRUE;
1539 else
1541 end_sequence ();
1542 return FALSE;
1546 return FALSE;
1549 /* Try more complex cases involving conditional_move. */
1551 static int
1552 noce_try_cmove_arith (struct noce_if_info *if_info)
1554 rtx a = if_info->a;
1555 rtx b = if_info->b;
1556 rtx x = if_info->x;
1557 rtx orig_a, orig_b;
1558 rtx insn_a, insn_b;
1559 rtx tmp, target;
1560 int is_mem = 0;
1561 int insn_cost;
1562 enum rtx_code code;
1564 /* A conditional move from two memory sources is equivalent to a
1565 conditional on their addresses followed by a load. Don't do this
1566 early because it'll screw alias analysis. Note that we've
1567 already checked for no side effects. */
1568 /* ??? FIXME: Magic number 5. */
1569 if (cse_not_expected
1570 && MEM_P (a) && MEM_P (b)
1571 && MEM_ADDR_SPACE (a) == MEM_ADDR_SPACE (b)
1572 && if_info->branch_cost >= 5)
1574 enum machine_mode address_mode = get_address_mode (a);
1576 a = XEXP (a, 0);
1577 b = XEXP (b, 0);
1578 x = gen_reg_rtx (address_mode);
1579 is_mem = 1;
1582 /* ??? We could handle this if we knew that a load from A or B could
1583 not trap or fault. This is also true if we've already loaded
1584 from the address along the path from ENTRY. */
1585 else if (may_trap_or_fault_p (a) || may_trap_or_fault_p (b))
1586 return FALSE;
1588 /* if (test) x = a + b; else x = c - d;
1589 => y = a + b;
1590 x = c - d;
1591 if (test)
1592 x = y;
1595 code = GET_CODE (if_info->cond);
1596 insn_a = if_info->insn_a;
1597 insn_b = if_info->insn_b;
1599 /* Total insn_rtx_cost should be smaller than branch cost. Exit
1600 if insn_rtx_cost can't be estimated. */
1601 if (insn_a)
1603 insn_cost
1604 = insn_rtx_cost (PATTERN (insn_a),
1605 optimize_bb_for_speed_p (BLOCK_FOR_INSN (insn_a)));
1606 if (insn_cost == 0 || insn_cost > COSTS_N_INSNS (if_info->branch_cost))
1607 return FALSE;
1609 else
1610 insn_cost = 0;
1612 if (insn_b)
1614 insn_cost
1615 += insn_rtx_cost (PATTERN (insn_b),
1616 optimize_bb_for_speed_p (BLOCK_FOR_INSN (insn_b)));
1617 if (insn_cost == 0 || insn_cost > COSTS_N_INSNS (if_info->branch_cost))
1618 return FALSE;
1621 /* Possibly rearrange operands to make things come out more natural. */
1622 if (reversed_comparison_code (if_info->cond, if_info->jump) != UNKNOWN)
1624 int reversep = 0;
1625 if (rtx_equal_p (b, x))
1626 reversep = 1;
1627 else if (general_operand (b, GET_MODE (b)))
1628 reversep = 1;
1630 if (reversep)
1632 code = reversed_comparison_code (if_info->cond, if_info->jump);
1633 tmp = a, a = b, b = tmp;
1634 tmp = insn_a, insn_a = insn_b, insn_b = tmp;
1638 start_sequence ();
1640 orig_a = a;
1641 orig_b = b;
1643 /* If either operand is complex, load it into a register first.
1644 The best way to do this is to copy the original insn. In this
1645 way we preserve any clobbers etc that the insn may have had.
1646 This is of course not possible in the IS_MEM case. */
1647 if (! general_operand (a, GET_MODE (a)))
1649 rtx set;
1651 if (is_mem)
1653 tmp = gen_reg_rtx (GET_MODE (a));
1654 tmp = emit_insn (gen_rtx_SET (VOIDmode, tmp, a));
1656 else if (! insn_a)
1657 goto end_seq_and_fail;
1658 else
1660 a = gen_reg_rtx (GET_MODE (a));
1661 tmp = copy_rtx (insn_a);
1662 set = single_set (tmp);
1663 SET_DEST (set) = a;
1664 tmp = emit_insn (PATTERN (tmp));
1666 if (recog_memoized (tmp) < 0)
1667 goto end_seq_and_fail;
1669 if (! general_operand (b, GET_MODE (b)))
1671 rtx set, last;
1673 if (is_mem)
1675 tmp = gen_reg_rtx (GET_MODE (b));
1676 tmp = gen_rtx_SET (VOIDmode, tmp, b);
1678 else if (! insn_b)
1679 goto end_seq_and_fail;
1680 else
1682 b = gen_reg_rtx (GET_MODE (b));
1683 tmp = copy_rtx (insn_b);
1684 set = single_set (tmp);
1685 SET_DEST (set) = b;
1686 tmp = PATTERN (tmp);
1689 /* If insn to set up A clobbers any registers B depends on, try to
1690 swap insn that sets up A with the one that sets up B. If even
1691 that doesn't help, punt. */
1692 last = get_last_insn ();
1693 if (last && modified_in_p (orig_b, last))
1695 tmp = emit_insn_before (tmp, get_insns ());
1696 if (modified_in_p (orig_a, tmp))
1697 goto end_seq_and_fail;
1699 else
1700 tmp = emit_insn (tmp);
1702 if (recog_memoized (tmp) < 0)
1703 goto end_seq_and_fail;
1706 target = noce_emit_cmove (if_info, x, code, XEXP (if_info->cond, 0),
1707 XEXP (if_info->cond, 1), a, b);
1709 if (! target)
1710 goto end_seq_and_fail;
1712 /* If we're handling a memory for above, emit the load now. */
1713 if (is_mem)
1715 tmp = gen_rtx_MEM (GET_MODE (if_info->x), target);
1717 /* Copy over flags as appropriate. */
1718 if (MEM_VOLATILE_P (if_info->a) || MEM_VOLATILE_P (if_info->b))
1719 MEM_VOLATILE_P (tmp) = 1;
1720 if (MEM_ALIAS_SET (if_info->a) == MEM_ALIAS_SET (if_info->b))
1721 set_mem_alias_set (tmp, MEM_ALIAS_SET (if_info->a));
1722 set_mem_align (tmp,
1723 MIN (MEM_ALIGN (if_info->a), MEM_ALIGN (if_info->b)));
1725 gcc_assert (MEM_ADDR_SPACE (if_info->a) == MEM_ADDR_SPACE (if_info->b));
1726 set_mem_addr_space (tmp, MEM_ADDR_SPACE (if_info->a));
1728 noce_emit_move_insn (if_info->x, tmp);
1730 else if (target != x)
1731 noce_emit_move_insn (x, target);
1733 tmp = end_ifcvt_sequence (if_info);
1734 if (!tmp)
1735 return FALSE;
1737 emit_insn_before_setloc (tmp, if_info->jump, INSN_LOCATION (if_info->insn_a));
1738 return TRUE;
1740 end_seq_and_fail:
1741 end_sequence ();
1742 return FALSE;
1745 /* For most cases, the simplified condition we found is the best
1746 choice, but this is not the case for the min/max/abs transforms.
1747 For these we wish to know that it is A or B in the condition. */
1749 static rtx
1750 noce_get_alt_condition (struct noce_if_info *if_info, rtx target,
1751 rtx *earliest)
1753 rtx cond, set, insn;
1754 int reverse;
1756 /* If target is already mentioned in the known condition, return it. */
1757 if (reg_mentioned_p (target, if_info->cond))
1759 *earliest = if_info->cond_earliest;
1760 return if_info->cond;
1763 set = pc_set (if_info->jump);
1764 cond = XEXP (SET_SRC (set), 0);
1765 reverse
1766 = GET_CODE (XEXP (SET_SRC (set), 2)) == LABEL_REF
1767 && XEXP (XEXP (SET_SRC (set), 2), 0) == JUMP_LABEL (if_info->jump);
1768 if (if_info->then_else_reversed)
1769 reverse = !reverse;
1771 /* If we're looking for a constant, try to make the conditional
1772 have that constant in it. There are two reasons why it may
1773 not have the constant we want:
1775 1. GCC may have needed to put the constant in a register, because
1776 the target can't compare directly against that constant. For
1777 this case, we look for a SET immediately before the comparison
1778 that puts a constant in that register.
1780 2. GCC may have canonicalized the conditional, for example
1781 replacing "if x < 4" with "if x <= 3". We can undo that (or
1782 make equivalent types of changes) to get the constants we need
1783 if they're off by one in the right direction. */
1785 if (CONST_INT_P (target))
1787 enum rtx_code code = GET_CODE (if_info->cond);
1788 rtx op_a = XEXP (if_info->cond, 0);
1789 rtx op_b = XEXP (if_info->cond, 1);
1790 rtx prev_insn;
1792 /* First, look to see if we put a constant in a register. */
1793 prev_insn = prev_nonnote_insn (if_info->cond_earliest);
1794 if (prev_insn
1795 && BLOCK_FOR_INSN (prev_insn)
1796 == BLOCK_FOR_INSN (if_info->cond_earliest)
1797 && INSN_P (prev_insn)
1798 && GET_CODE (PATTERN (prev_insn)) == SET)
1800 rtx src = find_reg_equal_equiv_note (prev_insn);
1801 if (!src)
1802 src = SET_SRC (PATTERN (prev_insn));
1803 if (CONST_INT_P (src))
1805 if (rtx_equal_p (op_a, SET_DEST (PATTERN (prev_insn))))
1806 op_a = src;
1807 else if (rtx_equal_p (op_b, SET_DEST (PATTERN (prev_insn))))
1808 op_b = src;
1810 if (CONST_INT_P (op_a))
1812 rtx tmp = op_a;
1813 op_a = op_b;
1814 op_b = tmp;
1815 code = swap_condition (code);
1820 /* Now, look to see if we can get the right constant by
1821 adjusting the conditional. */
1822 if (CONST_INT_P (op_b))
1824 HOST_WIDE_INT desired_val = INTVAL (target);
1825 HOST_WIDE_INT actual_val = INTVAL (op_b);
1827 switch (code)
1829 case LT:
1830 if (actual_val == desired_val + 1)
1832 code = LE;
1833 op_b = GEN_INT (desired_val);
1835 break;
1836 case LE:
1837 if (actual_val == desired_val - 1)
1839 code = LT;
1840 op_b = GEN_INT (desired_val);
1842 break;
1843 case GT:
1844 if (actual_val == desired_val - 1)
1846 code = GE;
1847 op_b = GEN_INT (desired_val);
1849 break;
1850 case GE:
1851 if (actual_val == desired_val + 1)
1853 code = GT;
1854 op_b = GEN_INT (desired_val);
1856 break;
1857 default:
1858 break;
1862 /* If we made any changes, generate a new conditional that is
1863 equivalent to what we started with, but has the right
1864 constants in it. */
1865 if (code != GET_CODE (if_info->cond)
1866 || op_a != XEXP (if_info->cond, 0)
1867 || op_b != XEXP (if_info->cond, 1))
1869 cond = gen_rtx_fmt_ee (code, GET_MODE (cond), op_a, op_b);
1870 *earliest = if_info->cond_earliest;
1871 return cond;
1875 cond = canonicalize_condition (if_info->jump, cond, reverse,
1876 earliest, target, false, true);
1877 if (! cond || ! reg_mentioned_p (target, cond))
1878 return NULL;
1880 /* We almost certainly searched back to a different place.
1881 Need to re-verify correct lifetimes. */
1883 /* X may not be mentioned in the range (cond_earliest, jump]. */
1884 for (insn = if_info->jump; insn != *earliest; insn = PREV_INSN (insn))
1885 if (INSN_P (insn) && reg_overlap_mentioned_p (if_info->x, PATTERN (insn)))
1886 return NULL;
1888 /* A and B may not be modified in the range [cond_earliest, jump). */
1889 for (insn = *earliest; insn != if_info->jump; insn = NEXT_INSN (insn))
1890 if (INSN_P (insn)
1891 && (modified_in_p (if_info->a, insn)
1892 || modified_in_p (if_info->b, insn)))
1893 return NULL;
1895 return cond;
1898 /* Convert "if (a < b) x = a; else x = b;" to "x = min(a, b);", etc. */
1900 static int
1901 noce_try_minmax (struct noce_if_info *if_info)
1903 rtx cond, earliest, target, seq;
1904 enum rtx_code code, op;
1905 int unsignedp;
1907 /* ??? Reject modes with NaNs or signed zeros since we don't know how
1908 they will be resolved with an SMIN/SMAX. It wouldn't be too hard
1909 to get the target to tell us... */
1910 if (HONOR_SIGNED_ZEROS (GET_MODE (if_info->x))
1911 || HONOR_NANS (GET_MODE (if_info->x)))
1912 return FALSE;
1914 cond = noce_get_alt_condition (if_info, if_info->a, &earliest);
1915 if (!cond)
1916 return FALSE;
1918 /* Verify the condition is of the form we expect, and canonicalize
1919 the comparison code. */
1920 code = GET_CODE (cond);
1921 if (rtx_equal_p (XEXP (cond, 0), if_info->a))
1923 if (! rtx_equal_p (XEXP (cond, 1), if_info->b))
1924 return FALSE;
1926 else if (rtx_equal_p (XEXP (cond, 1), if_info->a))
1928 if (! rtx_equal_p (XEXP (cond, 0), if_info->b))
1929 return FALSE;
1930 code = swap_condition (code);
1932 else
1933 return FALSE;
1935 /* Determine what sort of operation this is. Note that the code is for
1936 a taken branch, so the code->operation mapping appears backwards. */
1937 switch (code)
1939 case LT:
1940 case LE:
1941 case UNLT:
1942 case UNLE:
1943 op = SMAX;
1944 unsignedp = 0;
1945 break;
1946 case GT:
1947 case GE:
1948 case UNGT:
1949 case UNGE:
1950 op = SMIN;
1951 unsignedp = 0;
1952 break;
1953 case LTU:
1954 case LEU:
1955 op = UMAX;
1956 unsignedp = 1;
1957 break;
1958 case GTU:
1959 case GEU:
1960 op = UMIN;
1961 unsignedp = 1;
1962 break;
1963 default:
1964 return FALSE;
1967 start_sequence ();
1969 target = expand_simple_binop (GET_MODE (if_info->x), op,
1970 if_info->a, if_info->b,
1971 if_info->x, unsignedp, OPTAB_WIDEN);
1972 if (! target)
1974 end_sequence ();
1975 return FALSE;
1977 if (target != if_info->x)
1978 noce_emit_move_insn (if_info->x, target);
1980 seq = end_ifcvt_sequence (if_info);
1981 if (!seq)
1982 return FALSE;
1984 emit_insn_before_setloc (seq, if_info->jump, INSN_LOCATION (if_info->insn_a));
1985 if_info->cond = cond;
1986 if_info->cond_earliest = earliest;
1988 return TRUE;
1991 /* Convert "if (a < 0) x = -a; else x = a;" to "x = abs(a);",
1992 "if (a < 0) x = ~a; else x = a;" to "x = one_cmpl_abs(a);",
1993 etc. */
1995 static int
1996 noce_try_abs (struct noce_if_info *if_info)
1998 rtx cond, earliest, target, seq, a, b, c;
1999 int negate;
2000 bool one_cmpl = false;
2002 /* Reject modes with signed zeros. */
2003 if (HONOR_SIGNED_ZEROS (GET_MODE (if_info->x)))
2004 return FALSE;
2006 /* Recognize A and B as constituting an ABS or NABS. The canonical
2007 form is a branch around the negation, taken when the object is the
2008 first operand of a comparison against 0 that evaluates to true. */
2009 a = if_info->a;
2010 b = if_info->b;
2011 if (GET_CODE (a) == NEG && rtx_equal_p (XEXP (a, 0), b))
2012 negate = 0;
2013 else if (GET_CODE (b) == NEG && rtx_equal_p (XEXP (b, 0), a))
2015 c = a; a = b; b = c;
2016 negate = 1;
2018 else if (GET_CODE (a) == NOT && rtx_equal_p (XEXP (a, 0), b))
2020 negate = 0;
2021 one_cmpl = true;
2023 else if (GET_CODE (b) == NOT && rtx_equal_p (XEXP (b, 0), a))
2025 c = a; a = b; b = c;
2026 negate = 1;
2027 one_cmpl = true;
2029 else
2030 return FALSE;
2032 cond = noce_get_alt_condition (if_info, b, &earliest);
2033 if (!cond)
2034 return FALSE;
2036 /* Verify the condition is of the form we expect. */
2037 if (rtx_equal_p (XEXP (cond, 0), b))
2038 c = XEXP (cond, 1);
2039 else if (rtx_equal_p (XEXP (cond, 1), b))
2041 c = XEXP (cond, 0);
2042 negate = !negate;
2044 else
2045 return FALSE;
2047 /* Verify that C is zero. Search one step backward for a
2048 REG_EQUAL note or a simple source if necessary. */
2049 if (REG_P (c))
2051 rtx set, insn = prev_nonnote_insn (earliest);
2052 if (insn
2053 && BLOCK_FOR_INSN (insn) == BLOCK_FOR_INSN (earliest)
2054 && (set = single_set (insn))
2055 && rtx_equal_p (SET_DEST (set), c))
2057 rtx note = find_reg_equal_equiv_note (insn);
2058 if (note)
2059 c = XEXP (note, 0);
2060 else
2061 c = SET_SRC (set);
2063 else
2064 return FALSE;
2066 if (MEM_P (c)
2067 && GET_CODE (XEXP (c, 0)) == SYMBOL_REF
2068 && CONSTANT_POOL_ADDRESS_P (XEXP (c, 0)))
2069 c = get_pool_constant (XEXP (c, 0));
2071 /* Work around funny ideas get_condition has wrt canonicalization.
2072 Note that these rtx constants are known to be CONST_INT, and
2073 therefore imply integer comparisons. */
2074 if (c == constm1_rtx && GET_CODE (cond) == GT)
2076 else if (c == const1_rtx && GET_CODE (cond) == LT)
2078 else if (c != CONST0_RTX (GET_MODE (b)))
2079 return FALSE;
2081 /* Determine what sort of operation this is. */
2082 switch (GET_CODE (cond))
2084 case LT:
2085 case LE:
2086 case UNLT:
2087 case UNLE:
2088 negate = !negate;
2089 break;
2090 case GT:
2091 case GE:
2092 case UNGT:
2093 case UNGE:
2094 break;
2095 default:
2096 return FALSE;
2099 start_sequence ();
2100 if (one_cmpl)
2101 target = expand_one_cmpl_abs_nojump (GET_MODE (if_info->x), b,
2102 if_info->x);
2103 else
2104 target = expand_abs_nojump (GET_MODE (if_info->x), b, if_info->x, 1);
2106 /* ??? It's a quandary whether cmove would be better here, especially
2107 for integers. Perhaps combine will clean things up. */
2108 if (target && negate)
2110 if (one_cmpl)
2111 target = expand_simple_unop (GET_MODE (target), NOT, target,
2112 if_info->x, 0);
2113 else
2114 target = expand_simple_unop (GET_MODE (target), NEG, target,
2115 if_info->x, 0);
2118 if (! target)
2120 end_sequence ();
2121 return FALSE;
2124 if (target != if_info->x)
2125 noce_emit_move_insn (if_info->x, target);
2127 seq = end_ifcvt_sequence (if_info);
2128 if (!seq)
2129 return FALSE;
2131 emit_insn_before_setloc (seq, if_info->jump, INSN_LOCATION (if_info->insn_a));
2132 if_info->cond = cond;
2133 if_info->cond_earliest = earliest;
2135 return TRUE;
2138 /* Convert "if (m < 0) x = b; else x = 0;" to "x = (m >> C) & b;". */
2140 static int
2141 noce_try_sign_mask (struct noce_if_info *if_info)
2143 rtx cond, t, m, c, seq;
2144 enum machine_mode mode;
2145 enum rtx_code code;
2146 bool t_unconditional;
2148 cond = if_info->cond;
2149 code = GET_CODE (cond);
2150 m = XEXP (cond, 0);
2151 c = XEXP (cond, 1);
2153 t = NULL_RTX;
2154 if (if_info->a == const0_rtx)
2156 if ((code == LT && c == const0_rtx)
2157 || (code == LE && c == constm1_rtx))
2158 t = if_info->b;
2160 else if (if_info->b == const0_rtx)
2162 if ((code == GE && c == const0_rtx)
2163 || (code == GT && c == constm1_rtx))
2164 t = if_info->a;
2167 if (! t || side_effects_p (t))
2168 return FALSE;
2170 /* We currently don't handle different modes. */
2171 mode = GET_MODE (t);
2172 if (GET_MODE (m) != mode)
2173 return FALSE;
2175 /* This is only profitable if T is unconditionally executed/evaluated in the
2176 original insn sequence or T is cheap. The former happens if B is the
2177 non-zero (T) value and if INSN_B was taken from TEST_BB, or there was no
2178 INSN_B which can happen for e.g. conditional stores to memory. For the
2179 cost computation use the block TEST_BB where the evaluation will end up
2180 after the transformation. */
2181 t_unconditional =
2182 (t == if_info->b
2183 && (if_info->insn_b == NULL_RTX
2184 || BLOCK_FOR_INSN (if_info->insn_b) == if_info->test_bb));
2185 if (!(t_unconditional
2186 || (set_src_cost (t, optimize_bb_for_speed_p (if_info->test_bb))
2187 < COSTS_N_INSNS (2))))
2188 return FALSE;
2190 start_sequence ();
2191 /* Use emit_store_flag to generate "m < 0 ? -1 : 0" instead of expanding
2192 "(signed) m >> 31" directly. This benefits targets with specialized
2193 insns to obtain the signmask, but still uses ashr_optab otherwise. */
2194 m = emit_store_flag (gen_reg_rtx (mode), LT, m, const0_rtx, mode, 0, -1);
2195 t = m ? expand_binop (mode, and_optab, m, t, NULL_RTX, 0, OPTAB_DIRECT)
2196 : NULL_RTX;
2198 if (!t)
2200 end_sequence ();
2201 return FALSE;
2204 noce_emit_move_insn (if_info->x, t);
2206 seq = end_ifcvt_sequence (if_info);
2207 if (!seq)
2208 return FALSE;
2210 emit_insn_before_setloc (seq, if_info->jump, INSN_LOCATION (if_info->insn_a));
2211 return TRUE;
2215 /* Optimize away "if (x & C) x |= C" and similar bit manipulation
2216 transformations. */
2218 static int
2219 noce_try_bitop (struct noce_if_info *if_info)
2221 rtx cond, x, a, result, seq;
2222 enum machine_mode mode;
2223 enum rtx_code code;
2224 int bitnum;
2226 x = if_info->x;
2227 cond = if_info->cond;
2228 code = GET_CODE (cond);
2230 /* Check for no else condition. */
2231 if (! rtx_equal_p (x, if_info->b))
2232 return FALSE;
2234 /* Check for a suitable condition. */
2235 if (code != NE && code != EQ)
2236 return FALSE;
2237 if (XEXP (cond, 1) != const0_rtx)
2238 return FALSE;
2239 cond = XEXP (cond, 0);
2241 /* ??? We could also handle AND here. */
2242 if (GET_CODE (cond) == ZERO_EXTRACT)
2244 if (XEXP (cond, 1) != const1_rtx
2245 || !CONST_INT_P (XEXP (cond, 2))
2246 || ! rtx_equal_p (x, XEXP (cond, 0)))
2247 return FALSE;
2248 bitnum = INTVAL (XEXP (cond, 2));
2249 mode = GET_MODE (x);
2250 if (BITS_BIG_ENDIAN)
2251 bitnum = GET_MODE_BITSIZE (mode) - 1 - bitnum;
2252 if (bitnum < 0 || bitnum >= HOST_BITS_PER_WIDE_INT)
2253 return FALSE;
2255 else
2256 return FALSE;
2258 a = if_info->a;
2259 if (GET_CODE (a) == IOR || GET_CODE (a) == XOR)
2261 /* Check for "if (X & C) x = x op C". */
2262 if (! rtx_equal_p (x, XEXP (a, 0))
2263 || !CONST_INT_P (XEXP (a, 1))
2264 || (INTVAL (XEXP (a, 1)) & GET_MODE_MASK (mode))
2265 != (unsigned HOST_WIDE_INT) 1 << bitnum)
2266 return FALSE;
2268 /* if ((x & C) == 0) x |= C; is transformed to x |= C. */
2269 /* if ((x & C) != 0) x |= C; is transformed to nothing. */
2270 if (GET_CODE (a) == IOR)
2271 result = (code == NE) ? a : NULL_RTX;
2272 else if (code == NE)
2274 /* if ((x & C) == 0) x ^= C; is transformed to x |= C. */
2275 result = gen_int_mode ((HOST_WIDE_INT) 1 << bitnum, mode);
2276 result = simplify_gen_binary (IOR, mode, x, result);
2278 else
2280 /* if ((x & C) != 0) x ^= C; is transformed to x &= ~C. */
2281 result = gen_int_mode (~((HOST_WIDE_INT) 1 << bitnum), mode);
2282 result = simplify_gen_binary (AND, mode, x, result);
2285 else if (GET_CODE (a) == AND)
2287 /* Check for "if (X & C) x &= ~C". */
2288 if (! rtx_equal_p (x, XEXP (a, 0))
2289 || !CONST_INT_P (XEXP (a, 1))
2290 || (INTVAL (XEXP (a, 1)) & GET_MODE_MASK (mode))
2291 != (~((HOST_WIDE_INT) 1 << bitnum) & GET_MODE_MASK (mode)))
2292 return FALSE;
2294 /* if ((x & C) == 0) x &= ~C; is transformed to nothing. */
2295 /* if ((x & C) != 0) x &= ~C; is transformed to x &= ~C. */
2296 result = (code == EQ) ? a : NULL_RTX;
2298 else
2299 return FALSE;
2301 if (result)
2303 start_sequence ();
2304 noce_emit_move_insn (x, result);
2305 seq = end_ifcvt_sequence (if_info);
2306 if (!seq)
2307 return FALSE;
2309 emit_insn_before_setloc (seq, if_info->jump,
2310 INSN_LOCATION (if_info->insn_a));
2312 return TRUE;
2316 /* Similar to get_condition, only the resulting condition must be
2317 valid at JUMP, instead of at EARLIEST.
2319 If THEN_ELSE_REVERSED is true, the fallthrough does not go to the
2320 THEN block of the caller, and we have to reverse the condition. */
2322 static rtx
2323 noce_get_condition (rtx jump, rtx *earliest, bool then_else_reversed)
2325 rtx cond, set, tmp;
2326 bool reverse;
2328 if (! any_condjump_p (jump))
2329 return NULL_RTX;
2331 set = pc_set (jump);
2333 /* If this branches to JUMP_LABEL when the condition is false,
2334 reverse the condition. */
2335 reverse = (GET_CODE (XEXP (SET_SRC (set), 2)) == LABEL_REF
2336 && XEXP (XEXP (SET_SRC (set), 2), 0) == JUMP_LABEL (jump));
2338 /* We may have to reverse because the caller's if block is not canonical,
2339 i.e. the THEN block isn't the fallthrough block for the TEST block
2340 (see find_if_header). */
2341 if (then_else_reversed)
2342 reverse = !reverse;
2344 /* If the condition variable is a register and is MODE_INT, accept it. */
2346 cond = XEXP (SET_SRC (set), 0);
2347 tmp = XEXP (cond, 0);
2348 if (REG_P (tmp) && GET_MODE_CLASS (GET_MODE (tmp)) == MODE_INT
2349 && (GET_MODE (tmp) != BImode
2350 || !targetm.small_register_classes_for_mode_p (BImode)))
2352 *earliest = jump;
2354 if (reverse)
2355 cond = gen_rtx_fmt_ee (reverse_condition (GET_CODE (cond)),
2356 GET_MODE (cond), tmp, XEXP (cond, 1));
2357 return cond;
2360 /* Otherwise, fall back on canonicalize_condition to do the dirty
2361 work of manipulating MODE_CC values and COMPARE rtx codes. */
2362 tmp = canonicalize_condition (jump, cond, reverse, earliest,
2363 NULL_RTX, false, true);
2365 /* We don't handle side-effects in the condition, like handling
2366 REG_INC notes and making sure no duplicate conditions are emitted. */
2367 if (tmp != NULL_RTX && side_effects_p (tmp))
2368 return NULL_RTX;
2370 return tmp;
2373 /* Return true if OP is ok for if-then-else processing. */
2375 static int
2376 noce_operand_ok (const_rtx op)
2378 if (side_effects_p (op))
2379 return FALSE;
2381 /* We special-case memories, so handle any of them with
2382 no address side effects. */
2383 if (MEM_P (op))
2384 return ! side_effects_p (XEXP (op, 0));
2386 return ! may_trap_p (op);
2389 /* Return true if a write into MEM may trap or fault. */
2391 static bool
2392 noce_mem_write_may_trap_or_fault_p (const_rtx mem)
2394 rtx addr;
2396 if (MEM_READONLY_P (mem))
2397 return true;
2399 if (may_trap_or_fault_p (mem))
2400 return true;
2402 addr = XEXP (mem, 0);
2404 /* Call target hook to avoid the effects of -fpic etc.... */
2405 addr = targetm.delegitimize_address (addr);
2407 while (addr)
2408 switch (GET_CODE (addr))
2410 case CONST:
2411 case PRE_DEC:
2412 case PRE_INC:
2413 case POST_DEC:
2414 case POST_INC:
2415 case POST_MODIFY:
2416 addr = XEXP (addr, 0);
2417 break;
2418 case LO_SUM:
2419 case PRE_MODIFY:
2420 addr = XEXP (addr, 1);
2421 break;
2422 case PLUS:
2423 if (CONST_INT_P (XEXP (addr, 1)))
2424 addr = XEXP (addr, 0);
2425 else
2426 return false;
2427 break;
2428 case LABEL_REF:
2429 return true;
2430 case SYMBOL_REF:
2431 if (SYMBOL_REF_DECL (addr)
2432 && decl_readonly_section (SYMBOL_REF_DECL (addr), 0))
2433 return true;
2434 return false;
2435 default:
2436 return false;
2439 return false;
2442 /* Return whether we can use store speculation for MEM. TOP_BB is the
2443 basic block above the conditional block where we are considering
2444 doing the speculative store. We look for whether MEM is set
2445 unconditionally later in the function. */
2447 static bool
2448 noce_can_store_speculate_p (basic_block top_bb, const_rtx mem)
2450 basic_block dominator;
2452 for (dominator = get_immediate_dominator (CDI_POST_DOMINATORS, top_bb);
2453 dominator != NULL;
2454 dominator = get_immediate_dominator (CDI_POST_DOMINATORS, dominator))
2456 rtx insn;
2458 FOR_BB_INSNS (dominator, insn)
2460 /* If we see something that might be a memory barrier, we
2461 have to stop looking. Even if the MEM is set later in
2462 the function, we still don't want to set it
2463 unconditionally before the barrier. */
2464 if (INSN_P (insn)
2465 && (volatile_insn_p (PATTERN (insn))
2466 || (CALL_P (insn) && (!RTL_CONST_CALL_P (insn)))))
2467 return false;
2469 if (memory_must_be_modified_in_insn_p (mem, insn))
2470 return true;
2471 if (modified_in_p (XEXP (mem, 0), insn))
2472 return false;
2477 return false;
2480 /* Given a simple IF-THEN-JOIN or IF-THEN-ELSE-JOIN block, attempt to convert
2481 it without using conditional execution. Return TRUE if we were successful
2482 at converting the block. */
2484 static int
2485 noce_process_if_block (struct noce_if_info *if_info)
2487 basic_block test_bb = if_info->test_bb; /* test block */
2488 basic_block then_bb = if_info->then_bb; /* THEN */
2489 basic_block else_bb = if_info->else_bb; /* ELSE or NULL */
2490 basic_block join_bb = if_info->join_bb; /* JOIN */
2491 rtx jump = if_info->jump;
2492 rtx cond = if_info->cond;
2493 rtx insn_a, insn_b;
2494 rtx set_a, set_b;
2495 rtx orig_x, x, a, b;
2497 /* We're looking for patterns of the form
2499 (1) if (...) x = a; else x = b;
2500 (2) x = b; if (...) x = a;
2501 (3) if (...) x = a; // as if with an initial x = x.
2503 The later patterns require jumps to be more expensive.
2505 ??? For future expansion, look for multiple X in such patterns. */
2507 /* Look for one of the potential sets. */
2508 insn_a = first_active_insn (then_bb);
2509 if (! insn_a
2510 || insn_a != last_active_insn (then_bb, FALSE)
2511 || (set_a = single_set (insn_a)) == NULL_RTX)
2512 return FALSE;
2514 x = SET_DEST (set_a);
2515 a = SET_SRC (set_a);
2517 /* Look for the other potential set. Make sure we've got equivalent
2518 destinations. */
2519 /* ??? This is overconservative. Storing to two different mems is
2520 as easy as conditionally computing the address. Storing to a
2521 single mem merely requires a scratch memory to use as one of the
2522 destination addresses; often the memory immediately below the
2523 stack pointer is available for this. */
2524 set_b = NULL_RTX;
2525 if (else_bb)
2527 insn_b = first_active_insn (else_bb);
2528 if (! insn_b
2529 || insn_b != last_active_insn (else_bb, FALSE)
2530 || (set_b = single_set (insn_b)) == NULL_RTX
2531 || ! rtx_interchangeable_p (x, SET_DEST (set_b)))
2532 return FALSE;
2534 else
2536 insn_b = prev_nonnote_nondebug_insn (if_info->cond_earliest);
2537 /* We're going to be moving the evaluation of B down from above
2538 COND_EARLIEST to JUMP. Make sure the relevant data is still
2539 intact. */
2540 if (! insn_b
2541 || BLOCK_FOR_INSN (insn_b) != BLOCK_FOR_INSN (if_info->cond_earliest)
2542 || !NONJUMP_INSN_P (insn_b)
2543 || (set_b = single_set (insn_b)) == NULL_RTX
2544 || ! rtx_interchangeable_p (x, SET_DEST (set_b))
2545 || ! noce_operand_ok (SET_SRC (set_b))
2546 || reg_overlap_mentioned_p (x, SET_SRC (set_b))
2547 || modified_between_p (SET_SRC (set_b), insn_b, jump)
2548 /* Avoid extending the lifetime of hard registers on small
2549 register class machines. */
2550 || (REG_P (SET_SRC (set_b))
2551 && HARD_REGISTER_P (SET_SRC (set_b))
2552 && targetm.small_register_classes_for_mode_p
2553 (GET_MODE (SET_SRC (set_b))))
2554 /* Likewise with X. In particular this can happen when
2555 noce_get_condition looks farther back in the instruction
2556 stream than one might expect. */
2557 || reg_overlap_mentioned_p (x, cond)
2558 || reg_overlap_mentioned_p (x, a)
2559 || modified_between_p (x, insn_b, jump))
2560 insn_b = set_b = NULL_RTX;
2563 /* If x has side effects then only the if-then-else form is safe to
2564 convert. But even in that case we would need to restore any notes
2565 (such as REG_INC) at then end. That can be tricky if
2566 noce_emit_move_insn expands to more than one insn, so disable the
2567 optimization entirely for now if there are side effects. */
2568 if (side_effects_p (x))
2569 return FALSE;
2571 b = (set_b ? SET_SRC (set_b) : x);
2573 /* Only operate on register destinations, and even then avoid extending
2574 the lifetime of hard registers on small register class machines. */
2575 orig_x = x;
2576 if (!REG_P (x)
2577 || (HARD_REGISTER_P (x)
2578 && targetm.small_register_classes_for_mode_p (GET_MODE (x))))
2580 if (GET_MODE (x) == BLKmode)
2581 return FALSE;
2583 if (GET_CODE (x) == ZERO_EXTRACT
2584 && (!CONST_INT_P (XEXP (x, 1))
2585 || !CONST_INT_P (XEXP (x, 2))))
2586 return FALSE;
2588 x = gen_reg_rtx (GET_MODE (GET_CODE (x) == STRICT_LOW_PART
2589 ? XEXP (x, 0) : x));
2592 /* Don't operate on sources that may trap or are volatile. */
2593 if (! noce_operand_ok (a) || ! noce_operand_ok (b))
2594 return FALSE;
2596 retry:
2597 /* Set up the info block for our subroutines. */
2598 if_info->insn_a = insn_a;
2599 if_info->insn_b = insn_b;
2600 if_info->x = x;
2601 if_info->a = a;
2602 if_info->b = b;
2604 /* Try optimizations in some approximation of a useful order. */
2605 /* ??? Should first look to see if X is live incoming at all. If it
2606 isn't, we don't need anything but an unconditional set. */
2608 /* Look and see if A and B are really the same. Avoid creating silly
2609 cmove constructs that no one will fix up later. */
2610 if (rtx_interchangeable_p (a, b))
2612 /* If we have an INSN_B, we don't have to create any new rtl. Just
2613 move the instruction that we already have. If we don't have an
2614 INSN_B, that means that A == X, and we've got a noop move. In
2615 that case don't do anything and let the code below delete INSN_A. */
2616 if (insn_b && else_bb)
2618 rtx note;
2620 if (else_bb && insn_b == BB_END (else_bb))
2621 BB_END (else_bb) = PREV_INSN (insn_b);
2622 reorder_insns (insn_b, insn_b, PREV_INSN (jump));
2624 /* If there was a REG_EQUAL note, delete it since it may have been
2625 true due to this insn being after a jump. */
2626 if ((note = find_reg_note (insn_b, REG_EQUAL, NULL_RTX)) != 0)
2627 remove_note (insn_b, note);
2629 insn_b = NULL_RTX;
2631 /* If we have "x = b; if (...) x = a;", and x has side-effects, then
2632 x must be executed twice. */
2633 else if (insn_b && side_effects_p (orig_x))
2634 return FALSE;
2636 x = orig_x;
2637 goto success;
2640 if (!set_b && MEM_P (orig_x))
2642 /* Disallow the "if (...) x = a;" form (implicit "else x = x;")
2643 for optimizations if writing to x may trap or fault,
2644 i.e. it's a memory other than a static var or a stack slot,
2645 is misaligned on strict aligned machines or is read-only. If
2646 x is a read-only memory, then the program is valid only if we
2647 avoid the store into it. If there are stores on both the
2648 THEN and ELSE arms, then we can go ahead with the conversion;
2649 either the program is broken, or the condition is always
2650 false such that the other memory is selected. */
2651 if (noce_mem_write_may_trap_or_fault_p (orig_x))
2652 return FALSE;
2654 /* Avoid store speculation: given "if (...) x = a" where x is a
2655 MEM, we only want to do the store if x is always set
2656 somewhere in the function. This avoids cases like
2657 if (pthread_mutex_trylock(mutex))
2658 ++global_variable;
2659 where we only want global_variable to be changed if the mutex
2660 is held. FIXME: This should ideally be expressed directly in
2661 RTL somehow. */
2662 if (!noce_can_store_speculate_p (test_bb, orig_x))
2663 return FALSE;
2666 if (noce_try_move (if_info))
2667 goto success;
2668 if (noce_try_store_flag (if_info))
2669 goto success;
2670 if (noce_try_bitop (if_info))
2671 goto success;
2672 if (noce_try_minmax (if_info))
2673 goto success;
2674 if (noce_try_abs (if_info))
2675 goto success;
2676 if (HAVE_conditional_move
2677 && noce_try_cmove (if_info))
2678 goto success;
2679 if (! targetm.have_conditional_execution ())
2681 if (noce_try_store_flag_constants (if_info))
2682 goto success;
2683 if (noce_try_addcc (if_info))
2684 goto success;
2685 if (noce_try_store_flag_mask (if_info))
2686 goto success;
2687 if (HAVE_conditional_move
2688 && noce_try_cmove_arith (if_info))
2689 goto success;
2690 if (noce_try_sign_mask (if_info))
2691 goto success;
2694 if (!else_bb && set_b)
2696 insn_b = set_b = NULL_RTX;
2697 b = orig_x;
2698 goto retry;
2701 return FALSE;
2703 success:
2705 /* If we used a temporary, fix it up now. */
2706 if (orig_x != x)
2708 rtx seq;
2710 start_sequence ();
2711 noce_emit_move_insn (orig_x, x);
2712 seq = get_insns ();
2713 set_used_flags (orig_x);
2714 unshare_all_rtl_in_chain (seq);
2715 end_sequence ();
2717 emit_insn_before_setloc (seq, BB_END (test_bb), INSN_LOCATION (insn_a));
2720 /* The original THEN and ELSE blocks may now be removed. The test block
2721 must now jump to the join block. If the test block and the join block
2722 can be merged, do so. */
2723 if (else_bb)
2725 delete_basic_block (else_bb);
2726 num_true_changes++;
2728 else
2729 remove_edge (find_edge (test_bb, join_bb));
2731 remove_edge (find_edge (then_bb, join_bb));
2732 redirect_edge_and_branch_force (single_succ_edge (test_bb), join_bb);
2733 delete_basic_block (then_bb);
2734 num_true_changes++;
2736 if (can_merge_blocks_p (test_bb, join_bb))
2738 merge_blocks (test_bb, join_bb);
2739 num_true_changes++;
2742 num_updated_if_blocks++;
2743 return TRUE;
2746 /* Check whether a block is suitable for conditional move conversion.
2747 Every insn must be a simple set of a register to a constant or a
2748 register. For each assignment, store the value in the pointer map
2749 VALS, keyed indexed by register pointer, then store the register
2750 pointer in REGS. COND is the condition we will test. */
2752 static int
2753 check_cond_move_block (basic_block bb,
2754 hash_map<rtx, rtx> *vals,
2755 vec<rtx> *regs,
2756 rtx cond)
2758 rtx insn;
2760 /* We can only handle simple jumps at the end of the basic block.
2761 It is almost impossible to update the CFG otherwise. */
2762 insn = BB_END (bb);
2763 if (JUMP_P (insn) && !onlyjump_p (insn))
2764 return FALSE;
2766 FOR_BB_INSNS (bb, insn)
2768 rtx set, dest, src;
2770 if (!NONDEBUG_INSN_P (insn) || JUMP_P (insn))
2771 continue;
2772 set = single_set (insn);
2773 if (!set)
2774 return FALSE;
2776 dest = SET_DEST (set);
2777 src = SET_SRC (set);
2778 if (!REG_P (dest)
2779 || (HARD_REGISTER_P (dest)
2780 && targetm.small_register_classes_for_mode_p (GET_MODE (dest))))
2781 return FALSE;
2783 if (!CONSTANT_P (src) && !register_operand (src, VOIDmode))
2784 return FALSE;
2786 if (side_effects_p (src) || side_effects_p (dest))
2787 return FALSE;
2789 if (may_trap_p (src) || may_trap_p (dest))
2790 return FALSE;
2792 /* Don't try to handle this if the source register was
2793 modified earlier in the block. */
2794 if ((REG_P (src)
2795 && vals->get (src))
2796 || (GET_CODE (src) == SUBREG && REG_P (SUBREG_REG (src))
2797 && vals->get (SUBREG_REG (src))))
2798 return FALSE;
2800 /* Don't try to handle this if the destination register was
2801 modified earlier in the block. */
2802 if (vals->get (dest))
2803 return FALSE;
2805 /* Don't try to handle this if the condition uses the
2806 destination register. */
2807 if (reg_overlap_mentioned_p (dest, cond))
2808 return FALSE;
2810 /* Don't try to handle this if the source register is modified
2811 later in the block. */
2812 if (!CONSTANT_P (src)
2813 && modified_between_p (src, insn, NEXT_INSN (BB_END (bb))))
2814 return FALSE;
2816 vals->put (dest, src);
2818 regs->safe_push (dest);
2821 return TRUE;
2824 /* Given a basic block BB suitable for conditional move conversion,
2825 a condition COND, and pointer maps THEN_VALS and ELSE_VALS containing
2826 the register values depending on COND, emit the insns in the block as
2827 conditional moves. If ELSE_BLOCK is true, THEN_BB was already
2828 processed. The caller has started a sequence for the conversion.
2829 Return true if successful, false if something goes wrong. */
2831 static bool
2832 cond_move_convert_if_block (struct noce_if_info *if_infop,
2833 basic_block bb, rtx cond,
2834 hash_map<rtx, rtx> *then_vals,
2835 hash_map<rtx, rtx> *else_vals,
2836 bool else_block_p)
2838 enum rtx_code code;
2839 rtx insn, cond_arg0, cond_arg1;
2841 code = GET_CODE (cond);
2842 cond_arg0 = XEXP (cond, 0);
2843 cond_arg1 = XEXP (cond, 1);
2845 FOR_BB_INSNS (bb, insn)
2847 rtx set, target, dest, t, e;
2849 /* ??? Maybe emit conditional debug insn? */
2850 if (!NONDEBUG_INSN_P (insn) || JUMP_P (insn))
2851 continue;
2852 set = single_set (insn);
2853 gcc_assert (set && REG_P (SET_DEST (set)));
2855 dest = SET_DEST (set);
2857 rtx *then_slot = then_vals->get (dest);
2858 rtx *else_slot = else_vals->get (dest);
2859 t = then_slot ? *then_slot : NULL_RTX;
2860 e = else_slot ? *else_slot : NULL_RTX;
2862 if (else_block_p)
2864 /* If this register was set in the then block, we already
2865 handled this case there. */
2866 if (t)
2867 continue;
2868 t = dest;
2869 gcc_assert (e);
2871 else
2873 gcc_assert (t);
2874 if (!e)
2875 e = dest;
2878 target = noce_emit_cmove (if_infop, dest, code, cond_arg0, cond_arg1,
2879 t, e);
2880 if (!target)
2881 return false;
2883 if (target != dest)
2884 noce_emit_move_insn (dest, target);
2887 return true;
2890 /* Given a simple IF-THEN-JOIN or IF-THEN-ELSE-JOIN block, attempt to convert
2891 it using only conditional moves. Return TRUE if we were successful at
2892 converting the block. */
2894 static int
2895 cond_move_process_if_block (struct noce_if_info *if_info)
2897 basic_block test_bb = if_info->test_bb;
2898 basic_block then_bb = if_info->then_bb;
2899 basic_block else_bb = if_info->else_bb;
2900 basic_block join_bb = if_info->join_bb;
2901 rtx jump = if_info->jump;
2902 rtx cond = if_info->cond;
2903 rtx seq, loc_insn;
2904 rtx reg;
2905 int c;
2906 vec<rtx> then_regs = vNULL;
2907 vec<rtx> else_regs = vNULL;
2908 unsigned int i;
2909 int success_p = FALSE;
2911 /* Build a mapping for each block to the value used for each
2912 register. */
2913 hash_map<rtx, rtx> then_vals;
2914 hash_map<rtx, rtx> else_vals;
2916 /* Make sure the blocks are suitable. */
2917 if (!check_cond_move_block (then_bb, &then_vals, &then_regs, cond)
2918 || (else_bb
2919 && !check_cond_move_block (else_bb, &else_vals, &else_regs, cond)))
2920 goto done;
2922 /* Make sure the blocks can be used together. If the same register
2923 is set in both blocks, and is not set to a constant in both
2924 cases, then both blocks must set it to the same register. We
2925 have already verified that if it is set to a register, that the
2926 source register does not change after the assignment. Also count
2927 the number of registers set in only one of the blocks. */
2928 c = 0;
2929 FOR_EACH_VEC_ELT (then_regs, i, reg)
2931 rtx *then_slot = then_vals.get (reg);
2932 rtx *else_slot = else_vals.get (reg);
2934 gcc_checking_assert (then_slot);
2935 if (!else_slot)
2936 ++c;
2937 else
2939 rtx then_val = *then_slot;
2940 rtx else_val = *else_slot;
2941 if (!CONSTANT_P (then_val) && !CONSTANT_P (else_val)
2942 && !rtx_equal_p (then_val, else_val))
2943 goto done;
2947 /* Finish off c for MAX_CONDITIONAL_EXECUTE. */
2948 FOR_EACH_VEC_ELT (else_regs, i, reg)
2950 gcc_checking_assert (else_vals.get (reg));
2951 if (!then_vals.get (reg))
2952 ++c;
2955 /* Make sure it is reasonable to convert this block. What matters
2956 is the number of assignments currently made in only one of the
2957 branches, since if we convert we are going to always execute
2958 them. */
2959 if (c > MAX_CONDITIONAL_EXECUTE)
2960 goto done;
2962 /* Try to emit the conditional moves. First do the then block,
2963 then do anything left in the else blocks. */
2964 start_sequence ();
2965 if (!cond_move_convert_if_block (if_info, then_bb, cond,
2966 &then_vals, &else_vals, false)
2967 || (else_bb
2968 && !cond_move_convert_if_block (if_info, else_bb, cond,
2969 &then_vals, &else_vals, true)))
2971 end_sequence ();
2972 goto done;
2974 seq = end_ifcvt_sequence (if_info);
2975 if (!seq)
2976 goto done;
2978 loc_insn = first_active_insn (then_bb);
2979 if (!loc_insn)
2981 loc_insn = first_active_insn (else_bb);
2982 gcc_assert (loc_insn);
2984 emit_insn_before_setloc (seq, jump, INSN_LOCATION (loc_insn));
2986 if (else_bb)
2988 delete_basic_block (else_bb);
2989 num_true_changes++;
2991 else
2992 remove_edge (find_edge (test_bb, join_bb));
2994 remove_edge (find_edge (then_bb, join_bb));
2995 redirect_edge_and_branch_force (single_succ_edge (test_bb), join_bb);
2996 delete_basic_block (then_bb);
2997 num_true_changes++;
2999 if (can_merge_blocks_p (test_bb, join_bb))
3001 merge_blocks (test_bb, join_bb);
3002 num_true_changes++;
3005 num_updated_if_blocks++;
3007 success_p = TRUE;
3009 done:
3010 then_regs.release ();
3011 else_regs.release ();
3012 return success_p;
3016 /* Determine if a given basic block heads a simple IF-THEN-JOIN or an
3017 IF-THEN-ELSE-JOIN block.
3019 If so, we'll try to convert the insns to not require the branch,
3020 using only transformations that do not require conditional execution.
3022 Return TRUE if we were successful at converting the block. */
3024 static int
3025 noce_find_if_block (basic_block test_bb, edge then_edge, edge else_edge,
3026 int pass)
3028 basic_block then_bb, else_bb, join_bb;
3029 bool then_else_reversed = false;
3030 rtx jump, cond;
3031 rtx cond_earliest;
3032 struct noce_if_info if_info;
3034 /* We only ever should get here before reload. */
3035 gcc_assert (!reload_completed);
3037 /* Recognize an IF-THEN-ELSE-JOIN block. */
3038 if (single_pred_p (then_edge->dest)
3039 && single_succ_p (then_edge->dest)
3040 && single_pred_p (else_edge->dest)
3041 && single_succ_p (else_edge->dest)
3042 && single_succ (then_edge->dest) == single_succ (else_edge->dest))
3044 then_bb = then_edge->dest;
3045 else_bb = else_edge->dest;
3046 join_bb = single_succ (then_bb);
3048 /* Recognize an IF-THEN-JOIN block. */
3049 else if (single_pred_p (then_edge->dest)
3050 && single_succ_p (then_edge->dest)
3051 && single_succ (then_edge->dest) == else_edge->dest)
3053 then_bb = then_edge->dest;
3054 else_bb = NULL_BLOCK;
3055 join_bb = else_edge->dest;
3057 /* Recognize an IF-ELSE-JOIN block. We can have those because the order
3058 of basic blocks in cfglayout mode does not matter, so the fallthrough
3059 edge can go to any basic block (and not just to bb->next_bb, like in
3060 cfgrtl mode). */
3061 else if (single_pred_p (else_edge->dest)
3062 && single_succ_p (else_edge->dest)
3063 && single_succ (else_edge->dest) == then_edge->dest)
3065 /* The noce transformations do not apply to IF-ELSE-JOIN blocks.
3066 To make this work, we have to invert the THEN and ELSE blocks
3067 and reverse the jump condition. */
3068 then_bb = else_edge->dest;
3069 else_bb = NULL_BLOCK;
3070 join_bb = single_succ (then_bb);
3071 then_else_reversed = true;
3073 else
3074 /* Not a form we can handle. */
3075 return FALSE;
3077 /* The edges of the THEN and ELSE blocks cannot have complex edges. */
3078 if (single_succ_edge (then_bb)->flags & EDGE_COMPLEX)
3079 return FALSE;
3080 if (else_bb
3081 && single_succ_edge (else_bb)->flags & EDGE_COMPLEX)
3082 return FALSE;
3084 num_possible_if_blocks++;
3086 if (dump_file)
3088 fprintf (dump_file,
3089 "\nIF-THEN%s-JOIN block found, pass %d, test %d, then %d",
3090 (else_bb) ? "-ELSE" : "",
3091 pass, test_bb->index, then_bb->index);
3093 if (else_bb)
3094 fprintf (dump_file, ", else %d", else_bb->index);
3096 fprintf (dump_file, ", join %d\n", join_bb->index);
3099 /* If the conditional jump is more than just a conditional
3100 jump, then we can not do if-conversion on this block. */
3101 jump = BB_END (test_bb);
3102 if (! onlyjump_p (jump))
3103 return FALSE;
3105 /* If this is not a standard conditional jump, we can't parse it. */
3106 cond = noce_get_condition (jump, &cond_earliest, then_else_reversed);
3107 if (!cond)
3108 return FALSE;
3110 /* We must be comparing objects whose modes imply the size. */
3111 if (GET_MODE (XEXP (cond, 0)) == BLKmode)
3112 return FALSE;
3114 /* Initialize an IF_INFO struct to pass around. */
3115 memset (&if_info, 0, sizeof if_info);
3116 if_info.test_bb = test_bb;
3117 if_info.then_bb = then_bb;
3118 if_info.else_bb = else_bb;
3119 if_info.join_bb = join_bb;
3120 if_info.cond = cond;
3121 if_info.cond_earliest = cond_earliest;
3122 if_info.jump = jump;
3123 if_info.then_else_reversed = then_else_reversed;
3124 if_info.branch_cost = BRANCH_COST (optimize_bb_for_speed_p (test_bb),
3125 predictable_edge_p (then_edge));
3127 /* Do the real work. */
3129 if (noce_process_if_block (&if_info))
3130 return TRUE;
3132 if (HAVE_conditional_move
3133 && cond_move_process_if_block (&if_info))
3134 return TRUE;
3136 return FALSE;
3140 /* Merge the blocks and mark for local life update. */
3142 static void
3143 merge_if_block (struct ce_if_block * ce_info)
3145 basic_block test_bb = ce_info->test_bb; /* last test block */
3146 basic_block then_bb = ce_info->then_bb; /* THEN */
3147 basic_block else_bb = ce_info->else_bb; /* ELSE or NULL */
3148 basic_block join_bb = ce_info->join_bb; /* join block */
3149 basic_block combo_bb;
3151 /* All block merging is done into the lower block numbers. */
3153 combo_bb = test_bb;
3154 df_set_bb_dirty (test_bb);
3156 /* Merge any basic blocks to handle && and || subtests. Each of
3157 the blocks are on the fallthru path from the predecessor block. */
3158 if (ce_info->num_multiple_test_blocks > 0)
3160 basic_block bb = test_bb;
3161 basic_block last_test_bb = ce_info->last_test_bb;
3162 basic_block fallthru = block_fallthru (bb);
3166 bb = fallthru;
3167 fallthru = block_fallthru (bb);
3168 merge_blocks (combo_bb, bb);
3169 num_true_changes++;
3171 while (bb != last_test_bb);
3174 /* Merge TEST block into THEN block. Normally the THEN block won't have a
3175 label, but it might if there were || tests. That label's count should be
3176 zero, and it normally should be removed. */
3178 if (then_bb)
3180 /* If THEN_BB has no successors, then there's a BARRIER after it.
3181 If COMBO_BB has more than one successor (THEN_BB), then that BARRIER
3182 is no longer needed, and in fact it is incorrect to leave it in
3183 the insn stream. */
3184 if (EDGE_COUNT (then_bb->succs) == 0
3185 && EDGE_COUNT (combo_bb->succs) > 1)
3187 rtx end = NEXT_INSN (BB_END (then_bb));
3188 while (end && NOTE_P (end) && !NOTE_INSN_BASIC_BLOCK_P (end))
3189 end = NEXT_INSN (end);
3191 if (end && BARRIER_P (end))
3192 delete_insn (end);
3194 merge_blocks (combo_bb, then_bb);
3195 num_true_changes++;
3198 /* The ELSE block, if it existed, had a label. That label count
3199 will almost always be zero, but odd things can happen when labels
3200 get their addresses taken. */
3201 if (else_bb)
3203 /* If ELSE_BB has no successors, then there's a BARRIER after it.
3204 If COMBO_BB has more than one successor (ELSE_BB), then that BARRIER
3205 is no longer needed, and in fact it is incorrect to leave it in
3206 the insn stream. */
3207 if (EDGE_COUNT (else_bb->succs) == 0
3208 && EDGE_COUNT (combo_bb->succs) > 1)
3210 rtx end = NEXT_INSN (BB_END (else_bb));
3211 while (end && NOTE_P (end) && !NOTE_INSN_BASIC_BLOCK_P (end))
3212 end = NEXT_INSN (end);
3214 if (end && BARRIER_P (end))
3215 delete_insn (end);
3217 merge_blocks (combo_bb, else_bb);
3218 num_true_changes++;
3221 /* If there was no join block reported, that means it was not adjacent
3222 to the others, and so we cannot merge them. */
3224 if (! join_bb)
3226 rtx last = BB_END (combo_bb);
3228 /* The outgoing edge for the current COMBO block should already
3229 be correct. Verify this. */
3230 if (EDGE_COUNT (combo_bb->succs) == 0)
3231 gcc_assert (find_reg_note (last, REG_NORETURN, NULL)
3232 || (NONJUMP_INSN_P (last)
3233 && GET_CODE (PATTERN (last)) == TRAP_IF
3234 && (TRAP_CONDITION (PATTERN (last))
3235 == const_true_rtx)));
3237 else
3238 /* There should still be something at the end of the THEN or ELSE
3239 blocks taking us to our final destination. */
3240 gcc_assert (JUMP_P (last)
3241 || (EDGE_SUCC (combo_bb, 0)->dest
3242 == EXIT_BLOCK_PTR_FOR_FN (cfun)
3243 && CALL_P (last)
3244 && SIBLING_CALL_P (last))
3245 || ((EDGE_SUCC (combo_bb, 0)->flags & EDGE_EH)
3246 && can_throw_internal (last)));
3249 /* The JOIN block may have had quite a number of other predecessors too.
3250 Since we've already merged the TEST, THEN and ELSE blocks, we should
3251 have only one remaining edge from our if-then-else diamond. If there
3252 is more than one remaining edge, it must come from elsewhere. There
3253 may be zero incoming edges if the THEN block didn't actually join
3254 back up (as with a call to a non-return function). */
3255 else if (EDGE_COUNT (join_bb->preds) < 2
3256 && join_bb != EXIT_BLOCK_PTR_FOR_FN (cfun))
3258 /* We can merge the JOIN cleanly and update the dataflow try
3259 again on this pass.*/
3260 merge_blocks (combo_bb, join_bb);
3261 num_true_changes++;
3263 else
3265 /* We cannot merge the JOIN. */
3267 /* The outgoing edge for the current COMBO block should already
3268 be correct. Verify this. */
3269 gcc_assert (single_succ_p (combo_bb)
3270 && single_succ (combo_bb) == join_bb);
3272 /* Remove the jump and cruft from the end of the COMBO block. */
3273 if (join_bb != EXIT_BLOCK_PTR_FOR_FN (cfun))
3274 tidy_fallthru_edge (single_succ_edge (combo_bb));
3277 num_updated_if_blocks++;
3280 /* Find a block ending in a simple IF condition and try to transform it
3281 in some way. When converting a multi-block condition, put the new code
3282 in the first such block and delete the rest. Return a pointer to this
3283 first block if some transformation was done. Return NULL otherwise. */
3285 static basic_block
3286 find_if_header (basic_block test_bb, int pass)
3288 ce_if_block ce_info;
3289 edge then_edge;
3290 edge else_edge;
3292 /* The kind of block we're looking for has exactly two successors. */
3293 if (EDGE_COUNT (test_bb->succs) != 2)
3294 return NULL;
3296 then_edge = EDGE_SUCC (test_bb, 0);
3297 else_edge = EDGE_SUCC (test_bb, 1);
3299 if (df_get_bb_dirty (then_edge->dest))
3300 return NULL;
3301 if (df_get_bb_dirty (else_edge->dest))
3302 return NULL;
3304 /* Neither edge should be abnormal. */
3305 if ((then_edge->flags & EDGE_COMPLEX)
3306 || (else_edge->flags & EDGE_COMPLEX))
3307 return NULL;
3309 /* Nor exit the loop. */
3310 if ((then_edge->flags & EDGE_LOOP_EXIT)
3311 || (else_edge->flags & EDGE_LOOP_EXIT))
3312 return NULL;
3314 /* The THEN edge is canonically the one that falls through. */
3315 if (then_edge->flags & EDGE_FALLTHRU)
3317 else if (else_edge->flags & EDGE_FALLTHRU)
3319 edge e = else_edge;
3320 else_edge = then_edge;
3321 then_edge = e;
3323 else
3324 /* Otherwise this must be a multiway branch of some sort. */
3325 return NULL;
3327 memset (&ce_info, 0, sizeof (ce_info));
3328 ce_info.test_bb = test_bb;
3329 ce_info.then_bb = then_edge->dest;
3330 ce_info.else_bb = else_edge->dest;
3331 ce_info.pass = pass;
3333 #ifdef IFCVT_MACHDEP_INIT
3334 IFCVT_MACHDEP_INIT (&ce_info);
3335 #endif
3337 if (!reload_completed
3338 && noce_find_if_block (test_bb, then_edge, else_edge, pass))
3339 goto success;
3341 if (reload_completed
3342 && targetm.have_conditional_execution ()
3343 && cond_exec_find_if_block (&ce_info))
3344 goto success;
3346 if (HAVE_trap
3347 && optab_handler (ctrap_optab, word_mode) != CODE_FOR_nothing
3348 && find_cond_trap (test_bb, then_edge, else_edge))
3349 goto success;
3351 if (dom_info_state (CDI_POST_DOMINATORS) >= DOM_NO_FAST_QUERY
3352 && (reload_completed || !targetm.have_conditional_execution ()))
3354 if (find_if_case_1 (test_bb, then_edge, else_edge))
3355 goto success;
3356 if (find_if_case_2 (test_bb, then_edge, else_edge))
3357 goto success;
3360 return NULL;
3362 success:
3363 if (dump_file)
3364 fprintf (dump_file, "Conversion succeeded on pass %d.\n", pass);
3365 /* Set this so we continue looking. */
3366 cond_exec_changed_p = TRUE;
3367 return ce_info.test_bb;
3370 /* Return true if a block has two edges, one of which falls through to the next
3371 block, and the other jumps to a specific block, so that we can tell if the
3372 block is part of an && test or an || test. Returns either -1 or the number
3373 of non-note, non-jump, non-USE/CLOBBER insns in the block. */
3375 static int
3376 block_jumps_and_fallthru_p (basic_block cur_bb, basic_block target_bb)
3378 edge cur_edge;
3379 int fallthru_p = FALSE;
3380 int jump_p = FALSE;
3381 rtx insn;
3382 rtx end;
3383 int n_insns = 0;
3384 edge_iterator ei;
3386 if (!cur_bb || !target_bb)
3387 return -1;
3389 /* If no edges, obviously it doesn't jump or fallthru. */
3390 if (EDGE_COUNT (cur_bb->succs) == 0)
3391 return FALSE;
3393 FOR_EACH_EDGE (cur_edge, ei, cur_bb->succs)
3395 if (cur_edge->flags & EDGE_COMPLEX)
3396 /* Anything complex isn't what we want. */
3397 return -1;
3399 else if (cur_edge->flags & EDGE_FALLTHRU)
3400 fallthru_p = TRUE;
3402 else if (cur_edge->dest == target_bb)
3403 jump_p = TRUE;
3405 else
3406 return -1;
3409 if ((jump_p & fallthru_p) == 0)
3410 return -1;
3412 /* Don't allow calls in the block, since this is used to group && and ||
3413 together for conditional execution support. ??? we should support
3414 conditional execution support across calls for IA-64 some day, but
3415 for now it makes the code simpler. */
3416 end = BB_END (cur_bb);
3417 insn = BB_HEAD (cur_bb);
3419 while (insn != NULL_RTX)
3421 if (CALL_P (insn))
3422 return -1;
3424 if (INSN_P (insn)
3425 && !JUMP_P (insn)
3426 && !DEBUG_INSN_P (insn)
3427 && GET_CODE (PATTERN (insn)) != USE
3428 && GET_CODE (PATTERN (insn)) != CLOBBER)
3429 n_insns++;
3431 if (insn == end)
3432 break;
3434 insn = NEXT_INSN (insn);
3437 return n_insns;
3440 /* Determine if a given basic block heads a simple IF-THEN or IF-THEN-ELSE
3441 block. If so, we'll try to convert the insns to not require the branch.
3442 Return TRUE if we were successful at converting the block. */
3444 static int
3445 cond_exec_find_if_block (struct ce_if_block * ce_info)
3447 basic_block test_bb = ce_info->test_bb;
3448 basic_block then_bb = ce_info->then_bb;
3449 basic_block else_bb = ce_info->else_bb;
3450 basic_block join_bb = NULL_BLOCK;
3451 edge cur_edge;
3452 basic_block next;
3453 edge_iterator ei;
3455 ce_info->last_test_bb = test_bb;
3457 /* We only ever should get here after reload,
3458 and if we have conditional execution. */
3459 gcc_assert (reload_completed && targetm.have_conditional_execution ());
3461 /* Discover if any fall through predecessors of the current test basic block
3462 were && tests (which jump to the else block) or || tests (which jump to
3463 the then block). */
3464 if (single_pred_p (test_bb)
3465 && single_pred_edge (test_bb)->flags == EDGE_FALLTHRU)
3467 basic_block bb = single_pred (test_bb);
3468 basic_block target_bb;
3469 int max_insns = MAX_CONDITIONAL_EXECUTE;
3470 int n_insns;
3472 /* Determine if the preceding block is an && or || block. */
3473 if ((n_insns = block_jumps_and_fallthru_p (bb, else_bb)) >= 0)
3475 ce_info->and_and_p = TRUE;
3476 target_bb = else_bb;
3478 else if ((n_insns = block_jumps_and_fallthru_p (bb, then_bb)) >= 0)
3480 ce_info->and_and_p = FALSE;
3481 target_bb = then_bb;
3483 else
3484 target_bb = NULL_BLOCK;
3486 if (target_bb && n_insns <= max_insns)
3488 int total_insns = 0;
3489 int blocks = 0;
3491 ce_info->last_test_bb = test_bb;
3493 /* Found at least one && or || block, look for more. */
3496 ce_info->test_bb = test_bb = bb;
3497 total_insns += n_insns;
3498 blocks++;
3500 if (!single_pred_p (bb))
3501 break;
3503 bb = single_pred (bb);
3504 n_insns = block_jumps_and_fallthru_p (bb, target_bb);
3506 while (n_insns >= 0 && (total_insns + n_insns) <= max_insns);
3508 ce_info->num_multiple_test_blocks = blocks;
3509 ce_info->num_multiple_test_insns = total_insns;
3511 if (ce_info->and_and_p)
3512 ce_info->num_and_and_blocks = blocks;
3513 else
3514 ce_info->num_or_or_blocks = blocks;
3518 /* The THEN block of an IF-THEN combo must have exactly one predecessor,
3519 other than any || blocks which jump to the THEN block. */
3520 if ((EDGE_COUNT (then_bb->preds) - ce_info->num_or_or_blocks) != 1)
3521 return FALSE;
3523 /* The edges of the THEN and ELSE blocks cannot have complex edges. */
3524 FOR_EACH_EDGE (cur_edge, ei, then_bb->preds)
3526 if (cur_edge->flags & EDGE_COMPLEX)
3527 return FALSE;
3530 FOR_EACH_EDGE (cur_edge, ei, else_bb->preds)
3532 if (cur_edge->flags & EDGE_COMPLEX)
3533 return FALSE;
3536 /* The THEN block of an IF-THEN combo must have zero or one successors. */
3537 if (EDGE_COUNT (then_bb->succs) > 0
3538 && (!single_succ_p (then_bb)
3539 || (single_succ_edge (then_bb)->flags & EDGE_COMPLEX)
3540 || (epilogue_completed
3541 && tablejump_p (BB_END (then_bb), NULL, NULL))))
3542 return FALSE;
3544 /* If the THEN block has no successors, conditional execution can still
3545 make a conditional call. Don't do this unless the ELSE block has
3546 only one incoming edge -- the CFG manipulation is too ugly otherwise.
3547 Check for the last insn of the THEN block being an indirect jump, which
3548 is listed as not having any successors, but confuses the rest of the CE
3549 code processing. ??? we should fix this in the future. */
3550 if (EDGE_COUNT (then_bb->succs) == 0)
3552 if (single_pred_p (else_bb) && else_bb != EXIT_BLOCK_PTR_FOR_FN (cfun))
3554 rtx last_insn = BB_END (then_bb);
3556 while (last_insn
3557 && NOTE_P (last_insn)
3558 && last_insn != BB_HEAD (then_bb))
3559 last_insn = PREV_INSN (last_insn);
3561 if (last_insn
3562 && JUMP_P (last_insn)
3563 && ! simplejump_p (last_insn))
3564 return FALSE;
3566 join_bb = else_bb;
3567 else_bb = NULL_BLOCK;
3569 else
3570 return FALSE;
3573 /* If the THEN block's successor is the other edge out of the TEST block,
3574 then we have an IF-THEN combo without an ELSE. */
3575 else if (single_succ (then_bb) == else_bb)
3577 join_bb = else_bb;
3578 else_bb = NULL_BLOCK;
3581 /* If the THEN and ELSE block meet in a subsequent block, and the ELSE
3582 has exactly one predecessor and one successor, and the outgoing edge
3583 is not complex, then we have an IF-THEN-ELSE combo. */
3584 else if (single_succ_p (else_bb)
3585 && single_succ (then_bb) == single_succ (else_bb)
3586 && single_pred_p (else_bb)
3587 && !(single_succ_edge (else_bb)->flags & EDGE_COMPLEX)
3588 && !(epilogue_completed
3589 && tablejump_p (BB_END (else_bb), NULL, NULL)))
3590 join_bb = single_succ (else_bb);
3592 /* Otherwise it is not an IF-THEN or IF-THEN-ELSE combination. */
3593 else
3594 return FALSE;
3596 num_possible_if_blocks++;
3598 if (dump_file)
3600 fprintf (dump_file,
3601 "\nIF-THEN%s block found, pass %d, start block %d "
3602 "[insn %d], then %d [%d]",
3603 (else_bb) ? "-ELSE" : "",
3604 ce_info->pass,
3605 test_bb->index,
3606 BB_HEAD (test_bb) ? (int)INSN_UID (BB_HEAD (test_bb)) : -1,
3607 then_bb->index,
3608 BB_HEAD (then_bb) ? (int)INSN_UID (BB_HEAD (then_bb)) : -1);
3610 if (else_bb)
3611 fprintf (dump_file, ", else %d [%d]",
3612 else_bb->index,
3613 BB_HEAD (else_bb) ? (int)INSN_UID (BB_HEAD (else_bb)) : -1);
3615 fprintf (dump_file, ", join %d [%d]",
3616 join_bb->index,
3617 BB_HEAD (join_bb) ? (int)INSN_UID (BB_HEAD (join_bb)) : -1);
3619 if (ce_info->num_multiple_test_blocks > 0)
3620 fprintf (dump_file, ", %d %s block%s last test %d [%d]",
3621 ce_info->num_multiple_test_blocks,
3622 (ce_info->and_and_p) ? "&&" : "||",
3623 (ce_info->num_multiple_test_blocks == 1) ? "" : "s",
3624 ce_info->last_test_bb->index,
3625 ((BB_HEAD (ce_info->last_test_bb))
3626 ? (int)INSN_UID (BB_HEAD (ce_info->last_test_bb))
3627 : -1));
3629 fputc ('\n', dump_file);
3632 /* Make sure IF, THEN, and ELSE, blocks are adjacent. Actually, we get the
3633 first condition for free, since we've already asserted that there's a
3634 fallthru edge from IF to THEN. Likewise for the && and || blocks, since
3635 we checked the FALLTHRU flag, those are already adjacent to the last IF
3636 block. */
3637 /* ??? As an enhancement, move the ELSE block. Have to deal with
3638 BLOCK notes, if by no other means than backing out the merge if they
3639 exist. Sticky enough I don't want to think about it now. */
3640 next = then_bb;
3641 if (else_bb && (next = next->next_bb) != else_bb)
3642 return FALSE;
3643 if ((next = next->next_bb) != join_bb
3644 && join_bb != EXIT_BLOCK_PTR_FOR_FN (cfun))
3646 if (else_bb)
3647 join_bb = NULL;
3648 else
3649 return FALSE;
3652 /* Do the real work. */
3654 ce_info->else_bb = else_bb;
3655 ce_info->join_bb = join_bb;
3657 /* If we have && and || tests, try to first handle combining the && and ||
3658 tests into the conditional code, and if that fails, go back and handle
3659 it without the && and ||, which at present handles the && case if there
3660 was no ELSE block. */
3661 if (cond_exec_process_if_block (ce_info, TRUE))
3662 return TRUE;
3664 if (ce_info->num_multiple_test_blocks)
3666 cancel_changes (0);
3668 if (cond_exec_process_if_block (ce_info, FALSE))
3669 return TRUE;
3672 return FALSE;
3675 /* Convert a branch over a trap, or a branch
3676 to a trap, into a conditional trap. */
3678 static int
3679 find_cond_trap (basic_block test_bb, edge then_edge, edge else_edge)
3681 basic_block then_bb = then_edge->dest;
3682 basic_block else_bb = else_edge->dest;
3683 basic_block other_bb, trap_bb;
3684 rtx trap, jump, cond, cond_earliest, seq;
3685 enum rtx_code code;
3687 /* Locate the block with the trap instruction. */
3688 /* ??? While we look for no successors, we really ought to allow
3689 EH successors. Need to fix merge_if_block for that to work. */
3690 if ((trap = block_has_only_trap (then_bb)) != NULL)
3691 trap_bb = then_bb, other_bb = else_bb;
3692 else if ((trap = block_has_only_trap (else_bb)) != NULL)
3693 trap_bb = else_bb, other_bb = then_bb;
3694 else
3695 return FALSE;
3697 if (dump_file)
3699 fprintf (dump_file, "\nTRAP-IF block found, start %d, trap %d\n",
3700 test_bb->index, trap_bb->index);
3703 /* If this is not a standard conditional jump, we can't parse it. */
3704 jump = BB_END (test_bb);
3705 cond = noce_get_condition (jump, &cond_earliest, false);
3706 if (! cond)
3707 return FALSE;
3709 /* If the conditional jump is more than just a conditional jump, then
3710 we can not do if-conversion on this block. */
3711 if (! onlyjump_p (jump))
3712 return FALSE;
3714 /* We must be comparing objects whose modes imply the size. */
3715 if (GET_MODE (XEXP (cond, 0)) == BLKmode)
3716 return FALSE;
3718 /* Reverse the comparison code, if necessary. */
3719 code = GET_CODE (cond);
3720 if (then_bb == trap_bb)
3722 code = reversed_comparison_code (cond, jump);
3723 if (code == UNKNOWN)
3724 return FALSE;
3727 /* Attempt to generate the conditional trap. */
3728 seq = gen_cond_trap (code, copy_rtx (XEXP (cond, 0)),
3729 copy_rtx (XEXP (cond, 1)),
3730 TRAP_CODE (PATTERN (trap)));
3731 if (seq == NULL)
3732 return FALSE;
3734 /* Emit the new insns before cond_earliest. */
3735 emit_insn_before_setloc (seq, cond_earliest, INSN_LOCATION (trap));
3737 /* Delete the trap block if possible. */
3738 remove_edge (trap_bb == then_bb ? then_edge : else_edge);
3739 df_set_bb_dirty (test_bb);
3740 df_set_bb_dirty (then_bb);
3741 df_set_bb_dirty (else_bb);
3743 if (EDGE_COUNT (trap_bb->preds) == 0)
3745 delete_basic_block (trap_bb);
3746 num_true_changes++;
3749 /* Wire together the blocks again. */
3750 if (current_ir_type () == IR_RTL_CFGLAYOUT)
3751 single_succ_edge (test_bb)->flags |= EDGE_FALLTHRU;
3752 else if (trap_bb == then_bb)
3754 rtx lab, newjump;
3756 lab = JUMP_LABEL (jump);
3757 newjump = emit_jump_insn_after (gen_jump (lab), jump);
3758 LABEL_NUSES (lab) += 1;
3759 JUMP_LABEL (newjump) = lab;
3760 emit_barrier_after (newjump);
3762 delete_insn (jump);
3764 if (can_merge_blocks_p (test_bb, other_bb))
3766 merge_blocks (test_bb, other_bb);
3767 num_true_changes++;
3770 num_updated_if_blocks++;
3771 return TRUE;
3774 /* Subroutine of find_cond_trap: if BB contains only a trap insn,
3775 return it. */
3777 static rtx
3778 block_has_only_trap (basic_block bb)
3780 rtx trap;
3782 /* We're not the exit block. */
3783 if (bb == EXIT_BLOCK_PTR_FOR_FN (cfun))
3784 return NULL_RTX;
3786 /* The block must have no successors. */
3787 if (EDGE_COUNT (bb->succs) > 0)
3788 return NULL_RTX;
3790 /* The only instruction in the THEN block must be the trap. */
3791 trap = first_active_insn (bb);
3792 if (! (trap == BB_END (bb)
3793 && GET_CODE (PATTERN (trap)) == TRAP_IF
3794 && TRAP_CONDITION (PATTERN (trap)) == const_true_rtx))
3795 return NULL_RTX;
3797 return trap;
3800 /* Look for IF-THEN-ELSE cases in which one of THEN or ELSE is
3801 transformable, but not necessarily the other. There need be no
3802 JOIN block.
3804 Return TRUE if we were successful at converting the block.
3806 Cases we'd like to look at:
3809 if (test) goto over; // x not live
3810 x = a;
3811 goto label;
3812 over:
3814 becomes
3816 x = a;
3817 if (! test) goto label;
3820 if (test) goto E; // x not live
3821 x = big();
3822 goto L;
3824 x = b;
3825 goto M;
3827 becomes
3829 x = b;
3830 if (test) goto M;
3831 x = big();
3832 goto L;
3834 (3) // This one's really only interesting for targets that can do
3835 // multiway branching, e.g. IA-64 BBB bundles. For other targets
3836 // it results in multiple branches on a cache line, which often
3837 // does not sit well with predictors.
3839 if (test1) goto E; // predicted not taken
3840 x = a;
3841 if (test2) goto F;
3844 x = b;
3847 becomes
3849 x = a;
3850 if (test1) goto E;
3851 if (test2) goto F;
3853 Notes:
3855 (A) Don't do (2) if the branch is predicted against the block we're
3856 eliminating. Do it anyway if we can eliminate a branch; this requires
3857 that the sole successor of the eliminated block postdominate the other
3858 side of the if.
3860 (B) With CE, on (3) we can steal from both sides of the if, creating
3862 if (test1) x = a;
3863 if (!test1) x = b;
3864 if (test1) goto J;
3865 if (test2) goto F;
3869 Again, this is most useful if J postdominates.
3871 (C) CE substitutes for helpful life information.
3873 (D) These heuristics need a lot of work. */
3875 /* Tests for case 1 above. */
3877 static int
3878 find_if_case_1 (basic_block test_bb, edge then_edge, edge else_edge)
3880 basic_block then_bb = then_edge->dest;
3881 basic_block else_bb = else_edge->dest;
3882 basic_block new_bb;
3883 int then_bb_index, then_prob;
3884 rtx else_target = NULL_RTX;
3886 /* If we are partitioning hot/cold basic blocks, we don't want to
3887 mess up unconditional or indirect jumps that cross between hot
3888 and cold sections.
3890 Basic block partitioning may result in some jumps that appear to
3891 be optimizable (or blocks that appear to be mergeable), but which really
3892 must be left untouched (they are required to make it safely across
3893 partition boundaries). See the comments at the top of
3894 bb-reorder.c:partition_hot_cold_basic_blocks for complete details. */
3896 if ((BB_END (then_bb)
3897 && JUMP_P (BB_END (then_bb))
3898 && CROSSING_JUMP_P (BB_END (then_bb)))
3899 || (BB_END (test_bb)
3900 && JUMP_P (BB_END (test_bb))
3901 && CROSSING_JUMP_P (BB_END (test_bb)))
3902 || (BB_END (else_bb)
3903 && JUMP_P (BB_END (else_bb))
3904 && CROSSING_JUMP_P (BB_END (else_bb))))
3905 return FALSE;
3907 /* THEN has one successor. */
3908 if (!single_succ_p (then_bb))
3909 return FALSE;
3911 /* THEN does not fall through, but is not strange either. */
3912 if (single_succ_edge (then_bb)->flags & (EDGE_COMPLEX | EDGE_FALLTHRU))
3913 return FALSE;
3915 /* THEN has one predecessor. */
3916 if (!single_pred_p (then_bb))
3917 return FALSE;
3919 /* THEN must do something. */
3920 if (forwarder_block_p (then_bb))
3921 return FALSE;
3923 num_possible_if_blocks++;
3924 if (dump_file)
3925 fprintf (dump_file,
3926 "\nIF-CASE-1 found, start %d, then %d\n",
3927 test_bb->index, then_bb->index);
3929 if (then_edge->probability)
3930 then_prob = REG_BR_PROB_BASE - then_edge->probability;
3931 else
3932 then_prob = REG_BR_PROB_BASE / 2;
3934 /* We're speculating from the THEN path, we want to make sure the cost
3935 of speculation is within reason. */
3936 if (! cheap_bb_rtx_cost_p (then_bb, then_prob,
3937 COSTS_N_INSNS (BRANCH_COST (optimize_bb_for_speed_p (then_edge->src),
3938 predictable_edge_p (then_edge)))))
3939 return FALSE;
3941 if (else_bb == EXIT_BLOCK_PTR_FOR_FN (cfun))
3943 rtx jump = BB_END (else_edge->src);
3944 gcc_assert (JUMP_P (jump));
3945 else_target = JUMP_LABEL (jump);
3948 /* Registers set are dead, or are predicable. */
3949 if (! dead_or_predicable (test_bb, then_bb, else_bb,
3950 single_succ_edge (then_bb), 1))
3951 return FALSE;
3953 /* Conversion went ok, including moving the insns and fixing up the
3954 jump. Adjust the CFG to match. */
3956 /* We can avoid creating a new basic block if then_bb is immediately
3957 followed by else_bb, i.e. deleting then_bb allows test_bb to fall
3958 through to else_bb. */
3960 if (then_bb->next_bb == else_bb
3961 && then_bb->prev_bb == test_bb
3962 && else_bb != EXIT_BLOCK_PTR_FOR_FN (cfun))
3964 redirect_edge_succ (FALLTHRU_EDGE (test_bb), else_bb);
3965 new_bb = 0;
3967 else if (else_bb == EXIT_BLOCK_PTR_FOR_FN (cfun))
3968 new_bb = force_nonfallthru_and_redirect (FALLTHRU_EDGE (test_bb),
3969 else_bb, else_target);
3970 else
3971 new_bb = redirect_edge_and_branch_force (FALLTHRU_EDGE (test_bb),
3972 else_bb);
3974 df_set_bb_dirty (test_bb);
3975 df_set_bb_dirty (else_bb);
3977 then_bb_index = then_bb->index;
3978 delete_basic_block (then_bb);
3980 /* Make rest of code believe that the newly created block is the THEN_BB
3981 block we removed. */
3982 if (new_bb)
3984 df_bb_replace (then_bb_index, new_bb);
3985 /* This should have been done above via force_nonfallthru_and_redirect
3986 (possibly called from redirect_edge_and_branch_force). */
3987 gcc_checking_assert (BB_PARTITION (new_bb) == BB_PARTITION (test_bb));
3990 num_true_changes++;
3991 num_updated_if_blocks++;
3993 return TRUE;
3996 /* Test for case 2 above. */
3998 static int
3999 find_if_case_2 (basic_block test_bb, edge then_edge, edge else_edge)
4001 basic_block then_bb = then_edge->dest;
4002 basic_block else_bb = else_edge->dest;
4003 edge else_succ;
4004 int then_prob, else_prob;
4006 /* We do not want to speculate (empty) loop latches. */
4007 if (current_loops
4008 && else_bb->loop_father->latch == else_bb)
4009 return FALSE;
4011 /* If we are partitioning hot/cold basic blocks, we don't want to
4012 mess up unconditional or indirect jumps that cross between hot
4013 and cold sections.
4015 Basic block partitioning may result in some jumps that appear to
4016 be optimizable (or blocks that appear to be mergeable), but which really
4017 must be left untouched (they are required to make it safely across
4018 partition boundaries). See the comments at the top of
4019 bb-reorder.c:partition_hot_cold_basic_blocks for complete details. */
4021 if ((BB_END (then_bb)
4022 && JUMP_P (BB_END (then_bb))
4023 && CROSSING_JUMP_P (BB_END (then_bb)))
4024 || (BB_END (test_bb)
4025 && JUMP_P (BB_END (test_bb))
4026 && CROSSING_JUMP_P (BB_END (test_bb)))
4027 || (BB_END (else_bb)
4028 && JUMP_P (BB_END (else_bb))
4029 && CROSSING_JUMP_P (BB_END (else_bb))))
4030 return FALSE;
4032 /* ELSE has one successor. */
4033 if (!single_succ_p (else_bb))
4034 return FALSE;
4035 else
4036 else_succ = single_succ_edge (else_bb);
4038 /* ELSE outgoing edge is not complex. */
4039 if (else_succ->flags & EDGE_COMPLEX)
4040 return FALSE;
4042 /* ELSE has one predecessor. */
4043 if (!single_pred_p (else_bb))
4044 return FALSE;
4046 /* THEN is not EXIT. */
4047 if (then_bb->index < NUM_FIXED_BLOCKS)
4048 return FALSE;
4050 if (else_edge->probability)
4052 else_prob = else_edge->probability;
4053 then_prob = REG_BR_PROB_BASE - else_prob;
4055 else
4057 else_prob = REG_BR_PROB_BASE / 2;
4058 then_prob = REG_BR_PROB_BASE / 2;
4061 /* ELSE is predicted or SUCC(ELSE) postdominates THEN. */
4062 if (else_prob > then_prob)
4064 else if (else_succ->dest->index < NUM_FIXED_BLOCKS
4065 || dominated_by_p (CDI_POST_DOMINATORS, then_bb,
4066 else_succ->dest))
4068 else
4069 return FALSE;
4071 num_possible_if_blocks++;
4072 if (dump_file)
4073 fprintf (dump_file,
4074 "\nIF-CASE-2 found, start %d, else %d\n",
4075 test_bb->index, else_bb->index);
4077 /* We're speculating from the ELSE path, we want to make sure the cost
4078 of speculation is within reason. */
4079 if (! cheap_bb_rtx_cost_p (else_bb, else_prob,
4080 COSTS_N_INSNS (BRANCH_COST (optimize_bb_for_speed_p (else_edge->src),
4081 predictable_edge_p (else_edge)))))
4082 return FALSE;
4084 /* Registers set are dead, or are predicable. */
4085 if (! dead_or_predicable (test_bb, else_bb, then_bb, else_succ, 0))
4086 return FALSE;
4088 /* Conversion went ok, including moving the insns and fixing up the
4089 jump. Adjust the CFG to match. */
4091 df_set_bb_dirty (test_bb);
4092 df_set_bb_dirty (then_bb);
4093 delete_basic_block (else_bb);
4095 num_true_changes++;
4096 num_updated_if_blocks++;
4098 /* ??? We may now fallthru from one of THEN's successors into a join
4099 block. Rerun cleanup_cfg? Examine things manually? Wait? */
4101 return TRUE;
4104 /* Used by the code above to perform the actual rtl transformations.
4105 Return TRUE if successful.
4107 TEST_BB is the block containing the conditional branch. MERGE_BB
4108 is the block containing the code to manipulate. DEST_EDGE is an
4109 edge representing a jump to the join block; after the conversion,
4110 TEST_BB should be branching to its destination.
4111 REVERSEP is true if the sense of the branch should be reversed. */
4113 static int
4114 dead_or_predicable (basic_block test_bb, basic_block merge_bb,
4115 basic_block other_bb, edge dest_edge, int reversep)
4117 basic_block new_dest = dest_edge->dest;
4118 rtx head, end, jump, earliest = NULL_RTX, old_dest;
4119 bitmap merge_set = NULL;
4120 /* Number of pending changes. */
4121 int n_validated_changes = 0;
4122 rtx new_dest_label = NULL_RTX;
4124 jump = BB_END (test_bb);
4126 /* Find the extent of the real code in the merge block. */
4127 head = BB_HEAD (merge_bb);
4128 end = BB_END (merge_bb);
4130 while (DEBUG_INSN_P (end) && end != head)
4131 end = PREV_INSN (end);
4133 /* If merge_bb ends with a tablejump, predicating/moving insn's
4134 into test_bb and then deleting merge_bb will result in the jumptable
4135 that follows merge_bb being removed along with merge_bb and then we
4136 get an unresolved reference to the jumptable. */
4137 if (tablejump_p (end, NULL, NULL))
4138 return FALSE;
4140 if (LABEL_P (head))
4141 head = NEXT_INSN (head);
4142 while (DEBUG_INSN_P (head) && head != end)
4143 head = NEXT_INSN (head);
4144 if (NOTE_P (head))
4146 if (head == end)
4148 head = end = NULL_RTX;
4149 goto no_body;
4151 head = NEXT_INSN (head);
4152 while (DEBUG_INSN_P (head) && head != end)
4153 head = NEXT_INSN (head);
4156 if (JUMP_P (end))
4158 if (!onlyjump_p (end))
4159 return FALSE;
4160 if (head == end)
4162 head = end = NULL_RTX;
4163 goto no_body;
4165 end = PREV_INSN (end);
4166 while (DEBUG_INSN_P (end) && end != head)
4167 end = PREV_INSN (end);
4170 /* Don't move frame-related insn across the conditional branch. This
4171 can lead to one of the paths of the branch having wrong unwind info. */
4172 if (epilogue_completed)
4174 rtx insn = head;
4175 while (1)
4177 if (INSN_P (insn) && RTX_FRAME_RELATED_P (insn))
4178 return FALSE;
4179 if (insn == end)
4180 break;
4181 insn = NEXT_INSN (insn);
4185 /* Disable handling dead code by conditional execution if the machine needs
4186 to do anything funny with the tests, etc. */
4187 #ifndef IFCVT_MODIFY_TESTS
4188 if (targetm.have_conditional_execution ())
4190 /* In the conditional execution case, we have things easy. We know
4191 the condition is reversible. We don't have to check life info
4192 because we're going to conditionally execute the code anyway.
4193 All that's left is making sure the insns involved can actually
4194 be predicated. */
4196 rtx cond;
4198 cond = cond_exec_get_condition (jump);
4199 if (! cond)
4200 return FALSE;
4202 rtx note = find_reg_note (jump, REG_BR_PROB, NULL_RTX);
4203 int prob_val = (note ? XINT (note, 0) : -1);
4205 if (reversep)
4207 enum rtx_code rev = reversed_comparison_code (cond, jump);
4208 if (rev == UNKNOWN)
4209 return FALSE;
4210 cond = gen_rtx_fmt_ee (rev, GET_MODE (cond), XEXP (cond, 0),
4211 XEXP (cond, 1));
4212 if (prob_val >= 0)
4213 prob_val = REG_BR_PROB_BASE - prob_val;
4216 if (cond_exec_process_insns (NULL, head, end, cond, prob_val, 0)
4217 && verify_changes (0))
4218 n_validated_changes = num_validated_changes ();
4219 else
4220 cancel_changes (0);
4222 earliest = jump;
4224 #endif
4226 /* If we allocated new pseudos (e.g. in the conditional move
4227 expander called from noce_emit_cmove), we must resize the
4228 array first. */
4229 if (max_regno < max_reg_num ())
4230 max_regno = max_reg_num ();
4232 /* Try the NCE path if the CE path did not result in any changes. */
4233 if (n_validated_changes == 0)
4235 rtx cond, insn;
4236 regset live;
4237 bool success;
4239 /* In the non-conditional execution case, we have to verify that there
4240 are no trapping operations, no calls, no references to memory, and
4241 that any registers modified are dead at the branch site. */
4243 if (!any_condjump_p (jump))
4244 return FALSE;
4246 /* Find the extent of the conditional. */
4247 cond = noce_get_condition (jump, &earliest, false);
4248 if (!cond)
4249 return FALSE;
4251 live = BITMAP_ALLOC (&reg_obstack);
4252 simulate_backwards_to_point (merge_bb, live, end);
4253 success = can_move_insns_across (head, end, earliest, jump,
4254 merge_bb, live,
4255 df_get_live_in (other_bb), NULL);
4256 BITMAP_FREE (live);
4257 if (!success)
4258 return FALSE;
4260 /* Collect the set of registers set in MERGE_BB. */
4261 merge_set = BITMAP_ALLOC (&reg_obstack);
4263 FOR_BB_INSNS (merge_bb, insn)
4264 if (NONDEBUG_INSN_P (insn))
4265 df_simulate_find_defs (insn, merge_set);
4267 #ifdef HAVE_simple_return
4268 /* If shrink-wrapping, disable this optimization when test_bb is
4269 the first basic block and merge_bb exits. The idea is to not
4270 move code setting up a return register as that may clobber a
4271 register used to pass function parameters, which then must be
4272 saved in caller-saved regs. A caller-saved reg requires the
4273 prologue, killing a shrink-wrap opportunity. */
4274 if ((flag_shrink_wrap && HAVE_simple_return && !epilogue_completed)
4275 && ENTRY_BLOCK_PTR_FOR_FN (cfun)->next_bb == test_bb
4276 && single_succ_p (new_dest)
4277 && single_succ (new_dest) == EXIT_BLOCK_PTR_FOR_FN (cfun)
4278 && bitmap_intersect_p (df_get_live_in (new_dest), merge_set))
4280 regset return_regs;
4281 unsigned int i;
4283 return_regs = BITMAP_ALLOC (&reg_obstack);
4285 /* Start off with the intersection of regs used to pass
4286 params and regs used to return values. */
4287 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
4288 if (FUNCTION_ARG_REGNO_P (i)
4289 && targetm.calls.function_value_regno_p (i))
4290 bitmap_set_bit (return_regs, INCOMING_REGNO (i));
4292 bitmap_and_into (return_regs,
4293 df_get_live_out (ENTRY_BLOCK_PTR_FOR_FN (cfun)));
4294 bitmap_and_into (return_regs,
4295 df_get_live_in (EXIT_BLOCK_PTR_FOR_FN (cfun)));
4296 if (!bitmap_empty_p (return_regs))
4298 FOR_BB_INSNS_REVERSE (new_dest, insn)
4299 if (NONDEBUG_INSN_P (insn))
4301 df_ref def;
4303 /* If this insn sets any reg in return_regs, add all
4304 reg uses to the set of regs we're interested in. */
4305 FOR_EACH_INSN_DEF (def, insn)
4306 if (bitmap_bit_p (return_regs, DF_REF_REGNO (def)))
4308 df_simulate_uses (insn, return_regs);
4309 break;
4312 if (bitmap_intersect_p (merge_set, return_regs))
4314 BITMAP_FREE (return_regs);
4315 BITMAP_FREE (merge_set);
4316 return FALSE;
4319 BITMAP_FREE (return_regs);
4321 #endif
4324 no_body:
4325 /* We don't want to use normal invert_jump or redirect_jump because
4326 we don't want to delete_insn called. Also, we want to do our own
4327 change group management. */
4329 old_dest = JUMP_LABEL (jump);
4330 if (other_bb != new_dest)
4332 if (JUMP_P (BB_END (dest_edge->src)))
4333 new_dest_label = JUMP_LABEL (BB_END (dest_edge->src));
4334 else if (new_dest == EXIT_BLOCK_PTR_FOR_FN (cfun))
4335 new_dest_label = ret_rtx;
4336 else
4337 new_dest_label = block_label (new_dest);
4339 if (reversep
4340 ? ! invert_jump_1 (jump, new_dest_label)
4341 : ! redirect_jump_1 (jump, new_dest_label))
4342 goto cancel;
4345 if (verify_changes (n_validated_changes))
4346 confirm_change_group ();
4347 else
4348 goto cancel;
4350 if (other_bb != new_dest)
4352 redirect_jump_2 (jump, old_dest, new_dest_label, 0, reversep);
4354 redirect_edge_succ (BRANCH_EDGE (test_bb), new_dest);
4355 if (reversep)
4357 gcov_type count, probability;
4358 count = BRANCH_EDGE (test_bb)->count;
4359 BRANCH_EDGE (test_bb)->count = FALLTHRU_EDGE (test_bb)->count;
4360 FALLTHRU_EDGE (test_bb)->count = count;
4361 probability = BRANCH_EDGE (test_bb)->probability;
4362 BRANCH_EDGE (test_bb)->probability
4363 = FALLTHRU_EDGE (test_bb)->probability;
4364 FALLTHRU_EDGE (test_bb)->probability = probability;
4365 update_br_prob_note (test_bb);
4369 /* Move the insns out of MERGE_BB to before the branch. */
4370 if (head != NULL)
4372 rtx insn;
4374 if (end == BB_END (merge_bb))
4375 BB_END (merge_bb) = PREV_INSN (head);
4377 /* PR 21767: when moving insns above a conditional branch, the REG_EQUAL
4378 notes being moved might become invalid. */
4379 insn = head;
4382 rtx note, set;
4384 if (! INSN_P (insn))
4385 continue;
4386 note = find_reg_note (insn, REG_EQUAL, NULL_RTX);
4387 if (! note)
4388 continue;
4389 set = single_set (insn);
4390 if (!set || !function_invariant_p (SET_SRC (set))
4391 || !function_invariant_p (XEXP (note, 0)))
4392 remove_note (insn, note);
4393 } while (insn != end && (insn = NEXT_INSN (insn)));
4395 /* PR46315: when moving insns above a conditional branch, the REG_EQUAL
4396 notes referring to the registers being set might become invalid. */
4397 if (merge_set)
4399 unsigned i;
4400 bitmap_iterator bi;
4402 EXECUTE_IF_SET_IN_BITMAP (merge_set, 0, i, bi)
4403 remove_reg_equal_equiv_notes_for_regno (i);
4405 BITMAP_FREE (merge_set);
4408 reorder_insns (head, end, PREV_INSN (earliest));
4411 /* Remove the jump and edge if we can. */
4412 if (other_bb == new_dest)
4414 delete_insn (jump);
4415 remove_edge (BRANCH_EDGE (test_bb));
4416 /* ??? Can't merge blocks here, as then_bb is still in use.
4417 At minimum, the merge will get done just before bb-reorder. */
4420 return TRUE;
4422 cancel:
4423 cancel_changes (0);
4425 if (merge_set)
4426 BITMAP_FREE (merge_set);
4428 return FALSE;
4431 /* Main entry point for all if-conversion. AFTER_COMBINE is true if
4432 we are after combine pass. */
4434 static void
4435 if_convert (bool after_combine)
4437 basic_block bb;
4438 int pass;
4440 if (optimize == 1)
4442 df_live_add_problem ();
4443 df_live_set_all_dirty ();
4446 /* Record whether we are after combine pass. */
4447 ifcvt_after_combine = after_combine;
4448 num_possible_if_blocks = 0;
4449 num_updated_if_blocks = 0;
4450 num_true_changes = 0;
4452 loop_optimizer_init (AVOID_CFG_MODIFICATIONS);
4453 mark_loop_exit_edges ();
4454 loop_optimizer_finalize ();
4455 free_dominance_info (CDI_DOMINATORS);
4457 /* Compute postdominators. */
4458 calculate_dominance_info (CDI_POST_DOMINATORS);
4460 df_set_flags (DF_LR_RUN_DCE);
4462 /* Go through each of the basic blocks looking for things to convert. If we
4463 have conditional execution, we make multiple passes to allow us to handle
4464 IF-THEN{-ELSE} blocks within other IF-THEN{-ELSE} blocks. */
4465 pass = 0;
4468 df_analyze ();
4469 /* Only need to do dce on the first pass. */
4470 df_clear_flags (DF_LR_RUN_DCE);
4471 cond_exec_changed_p = FALSE;
4472 pass++;
4474 #ifdef IFCVT_MULTIPLE_DUMPS
4475 if (dump_file && pass > 1)
4476 fprintf (dump_file, "\n\n========== Pass %d ==========\n", pass);
4477 #endif
4479 FOR_EACH_BB_FN (bb, cfun)
4481 basic_block new_bb;
4482 while (!df_get_bb_dirty (bb)
4483 && (new_bb = find_if_header (bb, pass)) != NULL)
4484 bb = new_bb;
4487 #ifdef IFCVT_MULTIPLE_DUMPS
4488 if (dump_file && cond_exec_changed_p)
4489 print_rtl_with_bb (dump_file, get_insns (), dump_flags);
4490 #endif
4492 while (cond_exec_changed_p);
4494 #ifdef IFCVT_MULTIPLE_DUMPS
4495 if (dump_file)
4496 fprintf (dump_file, "\n\n========== no more changes\n");
4497 #endif
4499 free_dominance_info (CDI_POST_DOMINATORS);
4501 if (dump_file)
4502 fflush (dump_file);
4504 clear_aux_for_blocks ();
4506 /* If we allocated new pseudos, we must resize the array for sched1. */
4507 if (max_regno < max_reg_num ())
4508 max_regno = max_reg_num ();
4510 /* Write the final stats. */
4511 if (dump_file && num_possible_if_blocks > 0)
4513 fprintf (dump_file,
4514 "\n%d possible IF blocks searched.\n",
4515 num_possible_if_blocks);
4516 fprintf (dump_file,
4517 "%d IF blocks converted.\n",
4518 num_updated_if_blocks);
4519 fprintf (dump_file,
4520 "%d true changes made.\n\n\n",
4521 num_true_changes);
4524 if (optimize == 1)
4525 df_remove_problem (df_live);
4527 #ifdef ENABLE_CHECKING
4528 verify_flow_info ();
4529 #endif
4532 /* If-conversion and CFG cleanup. */
4533 static unsigned int
4534 rest_of_handle_if_conversion (void)
4536 if (flag_if_conversion)
4538 if (dump_file)
4540 dump_reg_info (dump_file);
4541 dump_flow_info (dump_file, dump_flags);
4543 cleanup_cfg (CLEANUP_EXPENSIVE);
4544 if_convert (false);
4547 cleanup_cfg (0);
4548 return 0;
4551 namespace {
4553 const pass_data pass_data_rtl_ifcvt =
4555 RTL_PASS, /* type */
4556 "ce1", /* name */
4557 OPTGROUP_NONE, /* optinfo_flags */
4558 TV_IFCVT, /* tv_id */
4559 0, /* properties_required */
4560 0, /* properties_provided */
4561 0, /* properties_destroyed */
4562 0, /* todo_flags_start */
4563 TODO_df_finish, /* todo_flags_finish */
4566 class pass_rtl_ifcvt : public rtl_opt_pass
4568 public:
4569 pass_rtl_ifcvt (gcc::context *ctxt)
4570 : rtl_opt_pass (pass_data_rtl_ifcvt, ctxt)
4573 /* opt_pass methods: */
4574 virtual bool gate (function *)
4576 return (optimize > 0) && dbg_cnt (if_conversion);
4579 virtual unsigned int execute (function *)
4581 return rest_of_handle_if_conversion ();
4584 }; // class pass_rtl_ifcvt
4586 } // anon namespace
4588 rtl_opt_pass *
4589 make_pass_rtl_ifcvt (gcc::context *ctxt)
4591 return new pass_rtl_ifcvt (ctxt);
4595 /* Rerun if-conversion, as combine may have simplified things enough
4596 to now meet sequence length restrictions. */
4598 namespace {
4600 const pass_data pass_data_if_after_combine =
4602 RTL_PASS, /* type */
4603 "ce2", /* name */
4604 OPTGROUP_NONE, /* optinfo_flags */
4605 TV_IFCVT, /* tv_id */
4606 0, /* properties_required */
4607 0, /* properties_provided */
4608 0, /* properties_destroyed */
4609 0, /* todo_flags_start */
4610 TODO_df_finish, /* todo_flags_finish */
4613 class pass_if_after_combine : public rtl_opt_pass
4615 public:
4616 pass_if_after_combine (gcc::context *ctxt)
4617 : rtl_opt_pass (pass_data_if_after_combine, ctxt)
4620 /* opt_pass methods: */
4621 virtual bool gate (function *)
4623 return optimize > 0 && flag_if_conversion
4624 && dbg_cnt (if_after_combine);
4627 virtual unsigned int execute (function *)
4629 if_convert (true);
4630 return 0;
4633 }; // class pass_if_after_combine
4635 } // anon namespace
4637 rtl_opt_pass *
4638 make_pass_if_after_combine (gcc::context *ctxt)
4640 return new pass_if_after_combine (ctxt);
4644 namespace {
4646 const pass_data pass_data_if_after_reload =
4648 RTL_PASS, /* type */
4649 "ce3", /* name */
4650 OPTGROUP_NONE, /* optinfo_flags */
4651 TV_IFCVT2, /* tv_id */
4652 0, /* properties_required */
4653 0, /* properties_provided */
4654 0, /* properties_destroyed */
4655 0, /* todo_flags_start */
4656 TODO_df_finish, /* todo_flags_finish */
4659 class pass_if_after_reload : public rtl_opt_pass
4661 public:
4662 pass_if_after_reload (gcc::context *ctxt)
4663 : rtl_opt_pass (pass_data_if_after_reload, ctxt)
4666 /* opt_pass methods: */
4667 virtual bool gate (function *)
4669 return optimize > 0 && flag_if_conversion2
4670 && dbg_cnt (if_after_reload);
4673 virtual unsigned int execute (function *)
4675 if_convert (true);
4676 return 0;
4679 }; // class pass_if_after_reload
4681 } // anon namespace
4683 rtl_opt_pass *
4684 make_pass_if_after_reload (gcc::context *ctxt)
4686 return new pass_if_after_reload (ctxt);