* gcc-interface/ada-tree.h (TYPE_OBJECT_RECORD_TYPE,
[official-gcc.git] / gcc / ifcvt.c
blob46a13c41c09d930783b0c27938318397179a9e45
1 /* If-conversion support.
2 Copyright (C) 2000-2017 Free Software Foundation, Inc.
4 This file is part of GCC.
6 GCC is free software; you can redistribute it and/or modify it
7 under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 3, or (at your option)
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 "backend.h"
24 #include "target.h"
25 #include "rtl.h"
26 #include "tree.h"
27 #include "cfghooks.h"
28 #include "df.h"
29 #include "memmodel.h"
30 #include "tm_p.h"
31 #include "expmed.h"
32 #include "optabs.h"
33 #include "regs.h"
34 #include "emit-rtl.h"
35 #include "recog.h"
37 #include "cfgrtl.h"
38 #include "cfganal.h"
39 #include "cfgcleanup.h"
40 #include "expr.h"
41 #include "output.h"
42 #include "cfgloop.h"
43 #include "tree-pass.h"
44 #include "dbgcnt.h"
45 #include "shrink-wrap.h"
46 #include "rtl-iter.h"
47 #include "ifcvt.h"
48 #include "params.h"
50 #ifndef MAX_CONDITIONAL_EXECUTE
51 #define MAX_CONDITIONAL_EXECUTE \
52 (BRANCH_COST (optimize_function_for_speed_p (cfun), false) \
53 + 1)
54 #endif
56 #define IFCVT_MULTIPLE_DUMPS 1
58 #define NULL_BLOCK ((basic_block) NULL)
60 /* True if after combine pass. */
61 static bool ifcvt_after_combine;
63 /* True if the target has the cbranchcc4 optab. */
64 static bool have_cbranchcc4;
66 /* # of IF-THEN or IF-THEN-ELSE blocks we looked at */
67 static int num_possible_if_blocks;
69 /* # of IF-THEN or IF-THEN-ELSE blocks were converted to conditional
70 execution. */
71 static int num_updated_if_blocks;
73 /* # of changes made. */
74 static int num_true_changes;
76 /* Whether conditional execution changes were made. */
77 static int cond_exec_changed_p;
79 /* Forward references. */
80 static int count_bb_insns (const_basic_block);
81 static bool cheap_bb_rtx_cost_p (const_basic_block, profile_probability, int);
82 static rtx_insn *first_active_insn (basic_block);
83 static rtx_insn *last_active_insn (basic_block, int);
84 static rtx_insn *find_active_insn_before (basic_block, rtx_insn *);
85 static rtx_insn *find_active_insn_after (basic_block, rtx_insn *);
86 static basic_block block_fallthru (basic_block);
87 static rtx cond_exec_get_condition (rtx_insn *);
88 static rtx noce_get_condition (rtx_insn *, rtx_insn **, bool);
89 static int noce_operand_ok (const_rtx);
90 static void merge_if_block (ce_if_block *);
91 static int find_cond_trap (basic_block, edge, edge);
92 static basic_block find_if_header (basic_block, int);
93 static int block_jumps_and_fallthru_p (basic_block, basic_block);
94 static int noce_find_if_block (basic_block, edge, edge, int);
95 static int cond_exec_find_if_block (ce_if_block *);
96 static int find_if_case_1 (basic_block, edge, edge);
97 static int find_if_case_2 (basic_block, edge, edge);
98 static int dead_or_predicable (basic_block, basic_block, basic_block,
99 edge, int);
100 static void noce_emit_move_insn (rtx, rtx);
101 static rtx_insn *block_has_only_trap (basic_block);
103 /* Count the number of non-jump active insns in BB. */
105 static int
106 count_bb_insns (const_basic_block bb)
108 int count = 0;
109 rtx_insn *insn = BB_HEAD (bb);
111 while (1)
113 if (active_insn_p (insn) && !JUMP_P (insn))
114 count++;
116 if (insn == BB_END (bb))
117 break;
118 insn = NEXT_INSN (insn);
121 return count;
124 /* Determine whether the total insn_rtx_cost on non-jump insns in
125 basic block BB is less than MAX_COST. This function returns
126 false if the cost of any instruction could not be estimated.
128 The cost of the non-jump insns in BB is scaled by REG_BR_PROB_BASE
129 as those insns are being speculated. MAX_COST is scaled with SCALE
130 plus a small fudge factor. */
132 static bool
133 cheap_bb_rtx_cost_p (const_basic_block bb,
134 profile_probability prob, int max_cost)
136 int count = 0;
137 rtx_insn *insn = BB_HEAD (bb);
138 bool speed = optimize_bb_for_speed_p (bb);
139 int scale = prob.initialized_p () ? prob.to_reg_br_prob_base ()
140 : REG_BR_PROB_BASE;
142 /* Set scale to REG_BR_PROB_BASE to void the identical scaling
143 applied to insn_rtx_cost when optimizing for size. Only do
144 this after combine because if-conversion might interfere with
145 passes before combine.
147 Use optimize_function_for_speed_p instead of the pre-defined
148 variable speed to make sure it is set to same value for all
149 basic blocks in one if-conversion transformation. */
150 if (!optimize_function_for_speed_p (cfun) && ifcvt_after_combine)
151 scale = REG_BR_PROB_BASE;
152 /* Our branch probability/scaling factors are just estimates and don't
153 account for cases where we can get speculation for free and other
154 secondary benefits. So we fudge the scale factor to make speculating
155 appear a little more profitable when optimizing for performance. */
156 else
157 scale += REG_BR_PROB_BASE / 8;
160 max_cost *= scale;
162 while (1)
164 if (NONJUMP_INSN_P (insn))
166 int cost = insn_rtx_cost (PATTERN (insn), speed) * REG_BR_PROB_BASE;
167 if (cost == 0)
168 return false;
170 /* If this instruction is the load or set of a "stack" register,
171 such as a floating point register on x87, then the cost of
172 speculatively executing this insn may need to include
173 the additional cost of popping its result off of the
174 register stack. Unfortunately, correctly recognizing and
175 accounting for this additional overhead is tricky, so for
176 now we simply prohibit such speculative execution. */
177 #ifdef STACK_REGS
179 rtx set = single_set (insn);
180 if (set && STACK_REG_P (SET_DEST (set)))
181 return false;
183 #endif
185 count += cost;
186 if (count >= max_cost)
187 return false;
189 else if (CALL_P (insn))
190 return false;
192 if (insn == BB_END (bb))
193 break;
194 insn = NEXT_INSN (insn);
197 return true;
200 /* Return the first non-jump active insn in the basic block. */
202 static rtx_insn *
203 first_active_insn (basic_block bb)
205 rtx_insn *insn = BB_HEAD (bb);
207 if (LABEL_P (insn))
209 if (insn == BB_END (bb))
210 return NULL;
211 insn = NEXT_INSN (insn);
214 while (NOTE_P (insn) || DEBUG_INSN_P (insn))
216 if (insn == BB_END (bb))
217 return NULL;
218 insn = NEXT_INSN (insn);
221 if (JUMP_P (insn))
222 return NULL;
224 return insn;
227 /* Return the last non-jump active (non-jump) insn in the basic block. */
229 static rtx_insn *
230 last_active_insn (basic_block bb, int skip_use_p)
232 rtx_insn *insn = BB_END (bb);
233 rtx_insn *head = BB_HEAD (bb);
235 while (NOTE_P (insn)
236 || JUMP_P (insn)
237 || DEBUG_INSN_P (insn)
238 || (skip_use_p
239 && NONJUMP_INSN_P (insn)
240 && GET_CODE (PATTERN (insn)) == USE))
242 if (insn == head)
243 return NULL;
244 insn = PREV_INSN (insn);
247 if (LABEL_P (insn))
248 return NULL;
250 return insn;
253 /* Return the active insn before INSN inside basic block CURR_BB. */
255 static rtx_insn *
256 find_active_insn_before (basic_block curr_bb, rtx_insn *insn)
258 if (!insn || insn == BB_HEAD (curr_bb))
259 return NULL;
261 while ((insn = PREV_INSN (insn)) != NULL_RTX)
263 if (NONJUMP_INSN_P (insn) || JUMP_P (insn) || CALL_P (insn))
264 break;
266 /* No other active insn all the way to the start of the basic block. */
267 if (insn == BB_HEAD (curr_bb))
268 return NULL;
271 return insn;
274 /* Return the active insn after INSN inside basic block CURR_BB. */
276 static rtx_insn *
277 find_active_insn_after (basic_block curr_bb, rtx_insn *insn)
279 if (!insn || insn == BB_END (curr_bb))
280 return NULL;
282 while ((insn = NEXT_INSN (insn)) != NULL_RTX)
284 if (NONJUMP_INSN_P (insn) || JUMP_P (insn) || CALL_P (insn))
285 break;
287 /* No other active insn all the way to the end of the basic block. */
288 if (insn == BB_END (curr_bb))
289 return NULL;
292 return insn;
295 /* Return the basic block reached by falling though the basic block BB. */
297 static basic_block
298 block_fallthru (basic_block bb)
300 edge e = find_fallthru_edge (bb->succs);
302 return (e) ? e->dest : NULL_BLOCK;
305 /* Return true if RTXs A and B can be safely interchanged. */
307 static bool
308 rtx_interchangeable_p (const_rtx a, const_rtx b)
310 if (!rtx_equal_p (a, b))
311 return false;
313 if (GET_CODE (a) != MEM)
314 return true;
316 /* A dead type-unsafe memory reference is legal, but a live type-unsafe memory
317 reference is not. Interchanging a dead type-unsafe memory reference with
318 a live type-safe one creates a live type-unsafe memory reference, in other
319 words, it makes the program illegal.
320 We check here conservatively whether the two memory references have equal
321 memory attributes. */
323 return mem_attrs_eq_p (get_mem_attrs (a), get_mem_attrs (b));
327 /* Go through a bunch of insns, converting them to conditional
328 execution format if possible. Return TRUE if all of the non-note
329 insns were processed. */
331 static int
332 cond_exec_process_insns (ce_if_block *ce_info ATTRIBUTE_UNUSED,
333 /* if block information */rtx_insn *start,
334 /* first insn to look at */rtx end,
335 /* last insn to look at */rtx test,
336 /* conditional execution test */profile_probability
337 prob_val,
338 /* probability of branch taken. */int mod_ok)
340 int must_be_last = FALSE;
341 rtx_insn *insn;
342 rtx xtest;
343 rtx pattern;
345 if (!start || !end)
346 return FALSE;
348 for (insn = start; ; insn = NEXT_INSN (insn))
350 /* dwarf2out can't cope with conditional prologues. */
351 if (NOTE_P (insn) && NOTE_KIND (insn) == NOTE_INSN_PROLOGUE_END)
352 return FALSE;
354 if (NOTE_P (insn) || DEBUG_INSN_P (insn))
355 goto insn_done;
357 gcc_assert (NONJUMP_INSN_P (insn) || CALL_P (insn));
359 /* dwarf2out can't cope with conditional unwind info. */
360 if (RTX_FRAME_RELATED_P (insn))
361 return FALSE;
363 /* Remove USE insns that get in the way. */
364 if (reload_completed && GET_CODE (PATTERN (insn)) == USE)
366 /* ??? Ug. Actually unlinking the thing is problematic,
367 given what we'd have to coordinate with our callers. */
368 SET_INSN_DELETED (insn);
369 goto insn_done;
372 /* Last insn wasn't last? */
373 if (must_be_last)
374 return FALSE;
376 if (modified_in_p (test, insn))
378 if (!mod_ok)
379 return FALSE;
380 must_be_last = TRUE;
383 /* Now build the conditional form of the instruction. */
384 pattern = PATTERN (insn);
385 xtest = copy_rtx (test);
387 /* If this is already a COND_EXEC, rewrite the test to be an AND of the
388 two conditions. */
389 if (GET_CODE (pattern) == COND_EXEC)
391 if (GET_MODE (xtest) != GET_MODE (COND_EXEC_TEST (pattern)))
392 return FALSE;
394 xtest = gen_rtx_AND (GET_MODE (xtest), xtest,
395 COND_EXEC_TEST (pattern));
396 pattern = COND_EXEC_CODE (pattern);
399 pattern = gen_rtx_COND_EXEC (VOIDmode, xtest, pattern);
401 /* If the machine needs to modify the insn being conditionally executed,
402 say for example to force a constant integer operand into a temp
403 register, do so here. */
404 #ifdef IFCVT_MODIFY_INSN
405 IFCVT_MODIFY_INSN (ce_info, pattern, insn);
406 if (! pattern)
407 return FALSE;
408 #endif
410 validate_change (insn, &PATTERN (insn), pattern, 1);
412 if (CALL_P (insn) && prob_val.initialized_p ())
413 validate_change (insn, &REG_NOTES (insn),
414 gen_rtx_INT_LIST ((machine_mode) REG_BR_PROB,
415 prob_val.to_reg_br_prob_note (),
416 REG_NOTES (insn)), 1);
418 insn_done:
419 if (insn == end)
420 break;
423 return TRUE;
426 /* Return the condition for a jump. Do not do any special processing. */
428 static rtx
429 cond_exec_get_condition (rtx_insn *jump)
431 rtx test_if, cond;
433 if (any_condjump_p (jump))
434 test_if = SET_SRC (pc_set (jump));
435 else
436 return NULL_RTX;
437 cond = XEXP (test_if, 0);
439 /* If this branches to JUMP_LABEL when the condition is false,
440 reverse the condition. */
441 if (GET_CODE (XEXP (test_if, 2)) == LABEL_REF
442 && label_ref_label (XEXP (test_if, 2)) == JUMP_LABEL (jump))
444 enum rtx_code rev = reversed_comparison_code (cond, jump);
445 if (rev == UNKNOWN)
446 return NULL_RTX;
448 cond = gen_rtx_fmt_ee (rev, GET_MODE (cond), XEXP (cond, 0),
449 XEXP (cond, 1));
452 return cond;
455 /* Given a simple IF-THEN or IF-THEN-ELSE block, attempt to convert it
456 to conditional execution. Return TRUE if we were successful at
457 converting the block. */
459 static int
460 cond_exec_process_if_block (ce_if_block * ce_info,
461 /* if block information */int do_multiple_p)
463 basic_block test_bb = ce_info->test_bb; /* last test block */
464 basic_block then_bb = ce_info->then_bb; /* THEN */
465 basic_block else_bb = ce_info->else_bb; /* ELSE or NULL */
466 rtx test_expr; /* expression in IF_THEN_ELSE that is tested */
467 rtx_insn *then_start; /* first insn in THEN block */
468 rtx_insn *then_end; /* last insn + 1 in THEN block */
469 rtx_insn *else_start = NULL; /* first insn in ELSE block or NULL */
470 rtx_insn *else_end = NULL; /* last insn + 1 in ELSE block */
471 int max; /* max # of insns to convert. */
472 int then_mod_ok; /* whether conditional mods are ok in THEN */
473 rtx true_expr; /* test for else block insns */
474 rtx false_expr; /* test for then block insns */
475 profile_probability true_prob_val;/* probability of else block */
476 profile_probability false_prob_val;/* probability of then block */
477 rtx_insn *then_last_head = NULL; /* Last match at the head of THEN */
478 rtx_insn *else_last_head = NULL; /* Last match at the head of ELSE */
479 rtx_insn *then_first_tail = NULL; /* First match at the tail of THEN */
480 rtx_insn *else_first_tail = NULL; /* First match at the tail of ELSE */
481 int then_n_insns, else_n_insns, n_insns;
482 enum rtx_code false_code;
483 rtx note;
485 /* If test is comprised of && or || elements, and we've failed at handling
486 all of them together, just use the last test if it is the special case of
487 && elements without an ELSE block. */
488 if (!do_multiple_p && ce_info->num_multiple_test_blocks)
490 if (else_bb || ! ce_info->and_and_p)
491 return FALSE;
493 ce_info->test_bb = test_bb = ce_info->last_test_bb;
494 ce_info->num_multiple_test_blocks = 0;
495 ce_info->num_and_and_blocks = 0;
496 ce_info->num_or_or_blocks = 0;
499 /* Find the conditional jump to the ELSE or JOIN part, and isolate
500 the test. */
501 test_expr = cond_exec_get_condition (BB_END (test_bb));
502 if (! test_expr)
503 return FALSE;
505 /* If the conditional jump is more than just a conditional jump,
506 then we can not do conditional execution conversion on this block. */
507 if (! onlyjump_p (BB_END (test_bb)))
508 return FALSE;
510 /* Collect the bounds of where we're to search, skipping any labels, jumps
511 and notes at the beginning and end of the block. Then count the total
512 number of insns and see if it is small enough to convert. */
513 then_start = first_active_insn (then_bb);
514 then_end = last_active_insn (then_bb, TRUE);
515 then_n_insns = ce_info->num_then_insns = count_bb_insns (then_bb);
516 n_insns = then_n_insns;
517 max = MAX_CONDITIONAL_EXECUTE;
519 if (else_bb)
521 int n_matching;
523 max *= 2;
524 else_start = first_active_insn (else_bb);
525 else_end = last_active_insn (else_bb, TRUE);
526 else_n_insns = ce_info->num_else_insns = count_bb_insns (else_bb);
527 n_insns += else_n_insns;
529 /* Look for matching sequences at the head and tail of the two blocks,
530 and limit the range of insns to be converted if possible. */
531 n_matching = flow_find_cross_jump (then_bb, else_bb,
532 &then_first_tail, &else_first_tail,
533 NULL);
534 if (then_first_tail == BB_HEAD (then_bb))
535 then_start = then_end = NULL;
536 if (else_first_tail == BB_HEAD (else_bb))
537 else_start = else_end = NULL;
539 if (n_matching > 0)
541 if (then_end)
542 then_end = find_active_insn_before (then_bb, then_first_tail);
543 if (else_end)
544 else_end = find_active_insn_before (else_bb, else_first_tail);
545 n_insns -= 2 * n_matching;
548 if (then_start
549 && else_start
550 && then_n_insns > n_matching
551 && else_n_insns > n_matching)
553 int longest_match = MIN (then_n_insns - n_matching,
554 else_n_insns - n_matching);
555 n_matching
556 = flow_find_head_matching_sequence (then_bb, else_bb,
557 &then_last_head,
558 &else_last_head,
559 longest_match);
561 if (n_matching > 0)
563 rtx_insn *insn;
565 /* We won't pass the insns in the head sequence to
566 cond_exec_process_insns, so we need to test them here
567 to make sure that they don't clobber the condition. */
568 for (insn = BB_HEAD (then_bb);
569 insn != NEXT_INSN (then_last_head);
570 insn = NEXT_INSN (insn))
571 if (!LABEL_P (insn) && !NOTE_P (insn)
572 && !DEBUG_INSN_P (insn)
573 && modified_in_p (test_expr, insn))
574 return FALSE;
577 if (then_last_head == then_end)
578 then_start = then_end = NULL;
579 if (else_last_head == else_end)
580 else_start = else_end = NULL;
582 if (n_matching > 0)
584 if (then_start)
585 then_start = find_active_insn_after (then_bb, then_last_head);
586 if (else_start)
587 else_start = find_active_insn_after (else_bb, else_last_head);
588 n_insns -= 2 * n_matching;
593 if (n_insns > max)
594 return FALSE;
596 /* Map test_expr/test_jump into the appropriate MD tests to use on
597 the conditionally executed code. */
599 true_expr = test_expr;
601 false_code = reversed_comparison_code (true_expr, BB_END (test_bb));
602 if (false_code != UNKNOWN)
603 false_expr = gen_rtx_fmt_ee (false_code, GET_MODE (true_expr),
604 XEXP (true_expr, 0), XEXP (true_expr, 1));
605 else
606 false_expr = NULL_RTX;
608 #ifdef IFCVT_MODIFY_TESTS
609 /* If the machine description needs to modify the tests, such as setting a
610 conditional execution register from a comparison, it can do so here. */
611 IFCVT_MODIFY_TESTS (ce_info, true_expr, false_expr);
613 /* See if the conversion failed. */
614 if (!true_expr || !false_expr)
615 goto fail;
616 #endif
618 note = find_reg_note (BB_END (test_bb), REG_BR_PROB, NULL_RTX);
619 if (note)
621 true_prob_val = profile_probability::from_reg_br_prob_note (XINT (note, 0));
622 false_prob_val = true_prob_val.invert ();
624 else
626 true_prob_val = profile_probability::uninitialized ();
627 false_prob_val = profile_probability::uninitialized ();
630 /* If we have && or || tests, do them here. These tests are in the adjacent
631 blocks after the first block containing the test. */
632 if (ce_info->num_multiple_test_blocks > 0)
634 basic_block bb = test_bb;
635 basic_block last_test_bb = ce_info->last_test_bb;
637 if (! false_expr)
638 goto fail;
642 rtx_insn *start, *end;
643 rtx t, f;
644 enum rtx_code f_code;
646 bb = block_fallthru (bb);
647 start = first_active_insn (bb);
648 end = last_active_insn (bb, TRUE);
649 if (start
650 && ! cond_exec_process_insns (ce_info, start, end, false_expr,
651 false_prob_val, FALSE))
652 goto fail;
654 /* If the conditional jump is more than just a conditional jump, then
655 we can not do conditional execution conversion on this block. */
656 if (! onlyjump_p (BB_END (bb)))
657 goto fail;
659 /* Find the conditional jump and isolate the test. */
660 t = cond_exec_get_condition (BB_END (bb));
661 if (! t)
662 goto fail;
664 f_code = reversed_comparison_code (t, BB_END (bb));
665 if (f_code == UNKNOWN)
666 goto fail;
668 f = gen_rtx_fmt_ee (f_code, GET_MODE (t), XEXP (t, 0), XEXP (t, 1));
669 if (ce_info->and_and_p)
671 t = gen_rtx_AND (GET_MODE (t), true_expr, t);
672 f = gen_rtx_IOR (GET_MODE (t), false_expr, f);
674 else
676 t = gen_rtx_IOR (GET_MODE (t), true_expr, t);
677 f = gen_rtx_AND (GET_MODE (t), false_expr, f);
680 /* If the machine description needs to modify the tests, such as
681 setting a conditional execution register from a comparison, it can
682 do so here. */
683 #ifdef IFCVT_MODIFY_MULTIPLE_TESTS
684 IFCVT_MODIFY_MULTIPLE_TESTS (ce_info, bb, t, f);
686 /* See if the conversion failed. */
687 if (!t || !f)
688 goto fail;
689 #endif
691 true_expr = t;
692 false_expr = f;
694 while (bb != last_test_bb);
697 /* For IF-THEN-ELSE blocks, we don't allow modifications of the test
698 on then THEN block. */
699 then_mod_ok = (else_bb == NULL_BLOCK);
701 /* Go through the THEN and ELSE blocks converting the insns if possible
702 to conditional execution. */
704 if (then_end
705 && (! false_expr
706 || ! cond_exec_process_insns (ce_info, then_start, then_end,
707 false_expr, false_prob_val,
708 then_mod_ok)))
709 goto fail;
711 if (else_bb && else_end
712 && ! cond_exec_process_insns (ce_info, else_start, else_end,
713 true_expr, true_prob_val, TRUE))
714 goto fail;
716 /* If we cannot apply the changes, fail. Do not go through the normal fail
717 processing, since apply_change_group will call cancel_changes. */
718 if (! apply_change_group ())
720 #ifdef IFCVT_MODIFY_CANCEL
721 /* Cancel any machine dependent changes. */
722 IFCVT_MODIFY_CANCEL (ce_info);
723 #endif
724 return FALSE;
727 #ifdef IFCVT_MODIFY_FINAL
728 /* Do any machine dependent final modifications. */
729 IFCVT_MODIFY_FINAL (ce_info);
730 #endif
732 /* Conversion succeeded. */
733 if (dump_file)
734 fprintf (dump_file, "%d insn%s converted to conditional execution.\n",
735 n_insns, (n_insns == 1) ? " was" : "s were");
737 /* Merge the blocks! If we had matching sequences, make sure to delete one
738 copy at the appropriate location first: delete the copy in the THEN branch
739 for a tail sequence so that the remaining one is executed last for both
740 branches, and delete the copy in the ELSE branch for a head sequence so
741 that the remaining one is executed first for both branches. */
742 if (then_first_tail)
744 rtx_insn *from = then_first_tail;
745 if (!INSN_P (from))
746 from = find_active_insn_after (then_bb, from);
747 delete_insn_chain (from, get_last_bb_insn (then_bb), false);
749 if (else_last_head)
750 delete_insn_chain (first_active_insn (else_bb), else_last_head, false);
752 merge_if_block (ce_info);
753 cond_exec_changed_p = TRUE;
754 return TRUE;
756 fail:
757 #ifdef IFCVT_MODIFY_CANCEL
758 /* Cancel any machine dependent changes. */
759 IFCVT_MODIFY_CANCEL (ce_info);
760 #endif
762 cancel_changes (0);
763 return FALSE;
766 static rtx noce_emit_store_flag (struct noce_if_info *, rtx, int, int);
767 static int noce_try_move (struct noce_if_info *);
768 static int noce_try_ifelse_collapse (struct noce_if_info *);
769 static int noce_try_store_flag (struct noce_if_info *);
770 static int noce_try_addcc (struct noce_if_info *);
771 static int noce_try_store_flag_constants (struct noce_if_info *);
772 static int noce_try_store_flag_mask (struct noce_if_info *);
773 static rtx noce_emit_cmove (struct noce_if_info *, rtx, enum rtx_code, rtx,
774 rtx, rtx, rtx);
775 static int noce_try_cmove (struct noce_if_info *);
776 static int noce_try_cmove_arith (struct noce_if_info *);
777 static rtx noce_get_alt_condition (struct noce_if_info *, rtx, rtx_insn **);
778 static int noce_try_minmax (struct noce_if_info *);
779 static int noce_try_abs (struct noce_if_info *);
780 static int noce_try_sign_mask (struct noce_if_info *);
782 /* Return the comparison code for reversed condition for IF_INFO,
783 or UNKNOWN if reversing the condition is not possible. */
785 static inline enum rtx_code
786 noce_reversed_cond_code (struct noce_if_info *if_info)
788 if (if_info->rev_cond)
789 return GET_CODE (if_info->rev_cond);
790 return reversed_comparison_code (if_info->cond, if_info->jump);
793 /* Return true if SEQ is a good candidate as a replacement for the
794 if-convertible sequence described in IF_INFO.
795 This is the default implementation that targets can override
796 through a target hook. */
798 bool
799 default_noce_conversion_profitable_p (rtx_insn *seq,
800 struct noce_if_info *if_info)
802 bool speed_p = if_info->speed_p;
804 /* Cost up the new sequence. */
805 unsigned int cost = seq_cost (seq, speed_p);
807 if (cost <= if_info->original_cost)
808 return true;
810 /* When compiling for size, we can make a reasonably accurately guess
811 at the size growth. When compiling for speed, use the maximum. */
812 return speed_p && cost <= if_info->max_seq_cost;
815 /* Helper function for noce_try_store_flag*. */
817 static rtx
818 noce_emit_store_flag (struct noce_if_info *if_info, rtx x, int reversep,
819 int normalize)
821 rtx cond = if_info->cond;
822 int cond_complex;
823 enum rtx_code code;
825 cond_complex = (! general_operand (XEXP (cond, 0), VOIDmode)
826 || ! general_operand (XEXP (cond, 1), VOIDmode));
828 /* If earliest == jump, or when the condition is complex, try to
829 build the store_flag insn directly. */
831 if (cond_complex)
833 rtx set = pc_set (if_info->jump);
834 cond = XEXP (SET_SRC (set), 0);
835 if (GET_CODE (XEXP (SET_SRC (set), 2)) == LABEL_REF
836 && label_ref_label (XEXP (SET_SRC (set), 2)) == JUMP_LABEL (if_info->jump))
837 reversep = !reversep;
838 if (if_info->then_else_reversed)
839 reversep = !reversep;
841 else if (reversep
842 && if_info->rev_cond
843 && general_operand (XEXP (if_info->rev_cond, 0), VOIDmode)
844 && general_operand (XEXP (if_info->rev_cond, 1), VOIDmode))
846 cond = if_info->rev_cond;
847 reversep = false;
850 if (reversep)
851 code = reversed_comparison_code (cond, if_info->jump);
852 else
853 code = GET_CODE (cond);
855 if ((if_info->cond_earliest == if_info->jump || cond_complex)
856 && (normalize == 0 || STORE_FLAG_VALUE == normalize))
858 rtx src = gen_rtx_fmt_ee (code, GET_MODE (x), XEXP (cond, 0),
859 XEXP (cond, 1));
860 rtx set = gen_rtx_SET (x, src);
862 start_sequence ();
863 rtx_insn *insn = emit_insn (set);
865 if (recog_memoized (insn) >= 0)
867 rtx_insn *seq = get_insns ();
868 end_sequence ();
869 emit_insn (seq);
871 if_info->cond_earliest = if_info->jump;
873 return x;
876 end_sequence ();
879 /* Don't even try if the comparison operands or the mode of X are weird. */
880 if (cond_complex || !SCALAR_INT_MODE_P (GET_MODE (x)))
881 return NULL_RTX;
883 return emit_store_flag (x, code, XEXP (cond, 0),
884 XEXP (cond, 1), VOIDmode,
885 (code == LTU || code == LEU
886 || code == GEU || code == GTU), normalize);
889 /* Emit instruction to move an rtx, possibly into STRICT_LOW_PART.
890 X is the destination/target and Y is the value to copy. */
892 static void
893 noce_emit_move_insn (rtx x, rtx y)
895 machine_mode outmode;
896 rtx outer, inner;
897 int bitpos;
899 if (GET_CODE (x) != STRICT_LOW_PART)
901 rtx_insn *seq, *insn;
902 rtx target;
903 optab ot;
905 start_sequence ();
906 /* Check that the SET_SRC is reasonable before calling emit_move_insn,
907 otherwise construct a suitable SET pattern ourselves. */
908 insn = (OBJECT_P (y) || CONSTANT_P (y) || GET_CODE (y) == SUBREG)
909 ? emit_move_insn (x, y)
910 : emit_insn (gen_rtx_SET (x, y));
911 seq = get_insns ();
912 end_sequence ();
914 if (recog_memoized (insn) <= 0)
916 if (GET_CODE (x) == ZERO_EXTRACT)
918 rtx op = XEXP (x, 0);
919 unsigned HOST_WIDE_INT size = INTVAL (XEXP (x, 1));
920 unsigned HOST_WIDE_INT start = INTVAL (XEXP (x, 2));
922 /* store_bit_field expects START to be relative to
923 BYTES_BIG_ENDIAN and adjusts this value for machines with
924 BITS_BIG_ENDIAN != BYTES_BIG_ENDIAN. In order to be able to
925 invoke store_bit_field again it is necessary to have the START
926 value from the first call. */
927 if (BITS_BIG_ENDIAN != BYTES_BIG_ENDIAN)
929 if (MEM_P (op))
930 start = BITS_PER_UNIT - start - size;
931 else
933 gcc_assert (REG_P (op));
934 start = BITS_PER_WORD - start - size;
938 gcc_assert (start < (MEM_P (op) ? BITS_PER_UNIT : BITS_PER_WORD));
939 store_bit_field (op, size, start, 0, 0, GET_MODE (x), y, false);
940 return;
943 switch (GET_RTX_CLASS (GET_CODE (y)))
945 case RTX_UNARY:
946 ot = code_to_optab (GET_CODE (y));
947 if (ot)
949 start_sequence ();
950 target = expand_unop (GET_MODE (y), ot, XEXP (y, 0), x, 0);
951 if (target != NULL_RTX)
953 if (target != x)
954 emit_move_insn (x, target);
955 seq = get_insns ();
957 end_sequence ();
959 break;
961 case RTX_BIN_ARITH:
962 case RTX_COMM_ARITH:
963 ot = code_to_optab (GET_CODE (y));
964 if (ot)
966 start_sequence ();
967 target = expand_binop (GET_MODE (y), ot,
968 XEXP (y, 0), XEXP (y, 1),
969 x, 0, OPTAB_DIRECT);
970 if (target != NULL_RTX)
972 if (target != x)
973 emit_move_insn (x, target);
974 seq = get_insns ();
976 end_sequence ();
978 break;
980 default:
981 break;
985 emit_insn (seq);
986 return;
989 outer = XEXP (x, 0);
990 inner = XEXP (outer, 0);
991 outmode = GET_MODE (outer);
992 bitpos = SUBREG_BYTE (outer) * BITS_PER_UNIT;
993 store_bit_field (inner, GET_MODE_BITSIZE (outmode), bitpos,
994 0, 0, outmode, y, false);
997 /* Return the CC reg if it is used in COND. */
999 static rtx
1000 cc_in_cond (rtx cond)
1002 if (have_cbranchcc4 && cond
1003 && GET_MODE_CLASS (GET_MODE (XEXP (cond, 0))) == MODE_CC)
1004 return XEXP (cond, 0);
1006 return NULL_RTX;
1009 /* Return sequence of instructions generated by if conversion. This
1010 function calls end_sequence() to end the current stream, ensures
1011 that the instructions are unshared, recognizable non-jump insns.
1012 On failure, this function returns a NULL_RTX. */
1014 static rtx_insn *
1015 end_ifcvt_sequence (struct noce_if_info *if_info)
1017 rtx_insn *insn;
1018 rtx_insn *seq = get_insns ();
1019 rtx cc = cc_in_cond (if_info->cond);
1021 set_used_flags (if_info->x);
1022 set_used_flags (if_info->cond);
1023 set_used_flags (if_info->a);
1024 set_used_flags (if_info->b);
1026 for (insn = seq; insn; insn = NEXT_INSN (insn))
1027 set_used_flags (insn);
1029 unshare_all_rtl_in_chain (seq);
1030 end_sequence ();
1032 /* Make sure that all of the instructions emitted are recognizable,
1033 and that we haven't introduced a new jump instruction.
1034 As an exercise for the reader, build a general mechanism that
1035 allows proper placement of required clobbers. */
1036 for (insn = seq; insn; insn = NEXT_INSN (insn))
1037 if (JUMP_P (insn)
1038 || recog_memoized (insn) == -1
1039 /* Make sure new generated code does not clobber CC. */
1040 || (cc && set_of (cc, insn)))
1041 return NULL;
1043 return seq;
1046 /* Return true iff the then and else basic block (if it exists)
1047 consist of a single simple set instruction. */
1049 static bool
1050 noce_simple_bbs (struct noce_if_info *if_info)
1052 if (!if_info->then_simple)
1053 return false;
1055 if (if_info->else_bb)
1056 return if_info->else_simple;
1058 return true;
1061 /* Convert "if (a != b) x = a; else x = b" into "x = a" and
1062 "if (a == b) x = a; else x = b" into "x = b". */
1064 static int
1065 noce_try_move (struct noce_if_info *if_info)
1067 rtx cond = if_info->cond;
1068 enum rtx_code code = GET_CODE (cond);
1069 rtx y;
1070 rtx_insn *seq;
1072 if (code != NE && code != EQ)
1073 return FALSE;
1075 if (!noce_simple_bbs (if_info))
1076 return FALSE;
1078 /* This optimization isn't valid if either A or B could be a NaN
1079 or a signed zero. */
1080 if (HONOR_NANS (if_info->x)
1081 || HONOR_SIGNED_ZEROS (if_info->x))
1082 return FALSE;
1084 /* Check whether the operands of the comparison are A and in
1085 either order. */
1086 if ((rtx_equal_p (if_info->a, XEXP (cond, 0))
1087 && rtx_equal_p (if_info->b, XEXP (cond, 1)))
1088 || (rtx_equal_p (if_info->a, XEXP (cond, 1))
1089 && rtx_equal_p (if_info->b, XEXP (cond, 0))))
1091 if (!rtx_interchangeable_p (if_info->a, if_info->b))
1092 return FALSE;
1094 y = (code == EQ) ? if_info->a : if_info->b;
1096 /* Avoid generating the move if the source is the destination. */
1097 if (! rtx_equal_p (if_info->x, y))
1099 start_sequence ();
1100 noce_emit_move_insn (if_info->x, y);
1101 seq = end_ifcvt_sequence (if_info);
1102 if (!seq)
1103 return FALSE;
1105 emit_insn_before_setloc (seq, if_info->jump,
1106 INSN_LOCATION (if_info->insn_a));
1108 if_info->transform_name = "noce_try_move";
1109 return TRUE;
1111 return FALSE;
1114 /* Try forming an IF_THEN_ELSE (cond, b, a) and collapsing that
1115 through simplify_rtx. Sometimes that can eliminate the IF_THEN_ELSE.
1116 If that is the case, emit the result into x. */
1118 static int
1119 noce_try_ifelse_collapse (struct noce_if_info * if_info)
1121 if (!noce_simple_bbs (if_info))
1122 return FALSE;
1124 machine_mode mode = GET_MODE (if_info->x);
1125 rtx if_then_else = simplify_gen_ternary (IF_THEN_ELSE, mode, mode,
1126 if_info->cond, if_info->b,
1127 if_info->a);
1129 if (GET_CODE (if_then_else) == IF_THEN_ELSE)
1130 return FALSE;
1132 rtx_insn *seq;
1133 start_sequence ();
1134 noce_emit_move_insn (if_info->x, if_then_else);
1135 seq = end_ifcvt_sequence (if_info);
1136 if (!seq)
1137 return FALSE;
1139 emit_insn_before_setloc (seq, if_info->jump,
1140 INSN_LOCATION (if_info->insn_a));
1142 if_info->transform_name = "noce_try_ifelse_collapse";
1143 return TRUE;
1147 /* Convert "if (test) x = 1; else x = 0".
1149 Only try 0 and STORE_FLAG_VALUE here. Other combinations will be
1150 tried in noce_try_store_flag_constants after noce_try_cmove has had
1151 a go at the conversion. */
1153 static int
1154 noce_try_store_flag (struct noce_if_info *if_info)
1156 int reversep;
1157 rtx target;
1158 rtx_insn *seq;
1160 if (!noce_simple_bbs (if_info))
1161 return FALSE;
1163 if (CONST_INT_P (if_info->b)
1164 && INTVAL (if_info->b) == STORE_FLAG_VALUE
1165 && if_info->a == const0_rtx)
1166 reversep = 0;
1167 else if (if_info->b == const0_rtx
1168 && CONST_INT_P (if_info->a)
1169 && INTVAL (if_info->a) == STORE_FLAG_VALUE
1170 && noce_reversed_cond_code (if_info) != UNKNOWN)
1171 reversep = 1;
1172 else
1173 return FALSE;
1175 start_sequence ();
1177 target = noce_emit_store_flag (if_info, if_info->x, reversep, 0);
1178 if (target)
1180 if (target != if_info->x)
1181 noce_emit_move_insn (if_info->x, target);
1183 seq = end_ifcvt_sequence (if_info);
1184 if (! seq)
1185 return FALSE;
1187 emit_insn_before_setloc (seq, if_info->jump,
1188 INSN_LOCATION (if_info->insn_a));
1189 if_info->transform_name = "noce_try_store_flag";
1190 return TRUE;
1192 else
1194 end_sequence ();
1195 return FALSE;
1200 /* Convert "if (test) x = -A; else x = A" into
1201 x = A; if (test) x = -x if the machine can do the
1202 conditional negate form of this cheaply.
1203 Try this before noce_try_cmove that will just load the
1204 immediates into two registers and do a conditional select
1205 between them. If the target has a conditional negate or
1206 conditional invert operation we can save a potentially
1207 expensive constant synthesis. */
1209 static bool
1210 noce_try_inverse_constants (struct noce_if_info *if_info)
1212 if (!noce_simple_bbs (if_info))
1213 return false;
1215 if (!CONST_INT_P (if_info->a)
1216 || !CONST_INT_P (if_info->b)
1217 || !REG_P (if_info->x))
1218 return false;
1220 machine_mode mode = GET_MODE (if_info->x);
1222 HOST_WIDE_INT val_a = INTVAL (if_info->a);
1223 HOST_WIDE_INT val_b = INTVAL (if_info->b);
1225 rtx cond = if_info->cond;
1227 rtx x = if_info->x;
1228 rtx target;
1230 start_sequence ();
1232 rtx_code code;
1233 if (val_b != HOST_WIDE_INT_MIN && val_a == -val_b)
1234 code = NEG;
1235 else if (val_a == ~val_b)
1236 code = NOT;
1237 else
1239 end_sequence ();
1240 return false;
1243 rtx tmp = gen_reg_rtx (mode);
1244 noce_emit_move_insn (tmp, if_info->a);
1246 target = emit_conditional_neg_or_complement (x, code, mode, cond, tmp, tmp);
1248 if (target)
1250 rtx_insn *seq = get_insns ();
1252 if (!seq)
1254 end_sequence ();
1255 return false;
1258 if (target != if_info->x)
1259 noce_emit_move_insn (if_info->x, target);
1261 seq = end_ifcvt_sequence (if_info);
1263 if (!seq)
1264 return false;
1266 emit_insn_before_setloc (seq, if_info->jump,
1267 INSN_LOCATION (if_info->insn_a));
1268 if_info->transform_name = "noce_try_inverse_constants";
1269 return true;
1272 end_sequence ();
1273 return false;
1277 /* Convert "if (test) x = a; else x = b", for A and B constant.
1278 Also allow A = y + c1, B = y + c2, with a common y between A
1279 and B. */
1281 static int
1282 noce_try_store_flag_constants (struct noce_if_info *if_info)
1284 rtx target;
1285 rtx_insn *seq;
1286 bool reversep;
1287 HOST_WIDE_INT itrue, ifalse, diff, tmp;
1288 int normalize;
1289 bool can_reverse;
1290 machine_mode mode = GET_MODE (if_info->x);;
1291 rtx common = NULL_RTX;
1293 rtx a = if_info->a;
1294 rtx b = if_info->b;
1296 /* Handle cases like x := test ? y + 3 : y + 4. */
1297 if (GET_CODE (a) == PLUS
1298 && GET_CODE (b) == PLUS
1299 && CONST_INT_P (XEXP (a, 1))
1300 && CONST_INT_P (XEXP (b, 1))
1301 && rtx_equal_p (XEXP (a, 0), XEXP (b, 0))
1302 /* Allow expressions that are not using the result or plain
1303 registers where we handle overlap below. */
1304 && (REG_P (XEXP (a, 0))
1305 || (noce_operand_ok (XEXP (a, 0))
1306 && ! reg_overlap_mentioned_p (if_info->x, XEXP (a, 0)))))
1308 common = XEXP (a, 0);
1309 a = XEXP (a, 1);
1310 b = XEXP (b, 1);
1313 if (!noce_simple_bbs (if_info))
1314 return FALSE;
1316 if (CONST_INT_P (a)
1317 && CONST_INT_P (b))
1319 ifalse = INTVAL (a);
1320 itrue = INTVAL (b);
1321 bool subtract_flag_p = false;
1323 diff = (unsigned HOST_WIDE_INT) itrue - ifalse;
1324 /* Make sure we can represent the difference between the two values. */
1325 if ((diff > 0)
1326 != ((ifalse < 0) != (itrue < 0) ? ifalse < 0 : ifalse < itrue))
1327 return FALSE;
1329 diff = trunc_int_for_mode (diff, mode);
1331 can_reverse = noce_reversed_cond_code (if_info) != UNKNOWN;
1332 reversep = false;
1333 if (diff == STORE_FLAG_VALUE || diff == -STORE_FLAG_VALUE)
1335 normalize = 0;
1336 /* We could collapse these cases but it is easier to follow the
1337 diff/STORE_FLAG_VALUE combinations when they are listed
1338 explicitly. */
1340 /* test ? 3 : 4
1341 => 4 + (test != 0). */
1342 if (diff < 0 && STORE_FLAG_VALUE < 0)
1343 reversep = false;
1344 /* test ? 4 : 3
1345 => can_reverse | 4 + (test == 0)
1346 !can_reverse | 3 - (test != 0). */
1347 else if (diff > 0 && STORE_FLAG_VALUE < 0)
1349 reversep = can_reverse;
1350 subtract_flag_p = !can_reverse;
1351 /* If we need to subtract the flag and we have PLUS-immediate
1352 A and B then it is unlikely to be beneficial to play tricks
1353 here. */
1354 if (subtract_flag_p && common)
1355 return FALSE;
1357 /* test ? 3 : 4
1358 => can_reverse | 3 + (test == 0)
1359 !can_reverse | 4 - (test != 0). */
1360 else if (diff < 0 && STORE_FLAG_VALUE > 0)
1362 reversep = can_reverse;
1363 subtract_flag_p = !can_reverse;
1364 /* If we need to subtract the flag and we have PLUS-immediate
1365 A and B then it is unlikely to be beneficial to play tricks
1366 here. */
1367 if (subtract_flag_p && common)
1368 return FALSE;
1370 /* test ? 4 : 3
1371 => 4 + (test != 0). */
1372 else if (diff > 0 && STORE_FLAG_VALUE > 0)
1373 reversep = false;
1374 else
1375 gcc_unreachable ();
1377 /* Is this (cond) ? 2^n : 0? */
1378 else if (ifalse == 0 && pow2p_hwi (itrue)
1379 && STORE_FLAG_VALUE == 1)
1380 normalize = 1;
1381 /* Is this (cond) ? 0 : 2^n? */
1382 else if (itrue == 0 && pow2p_hwi (ifalse) && can_reverse
1383 && STORE_FLAG_VALUE == 1)
1385 normalize = 1;
1386 reversep = true;
1388 /* Is this (cond) ? -1 : x? */
1389 else if (itrue == -1
1390 && STORE_FLAG_VALUE == -1)
1391 normalize = -1;
1392 /* Is this (cond) ? x : -1? */
1393 else if (ifalse == -1 && can_reverse
1394 && STORE_FLAG_VALUE == -1)
1396 normalize = -1;
1397 reversep = true;
1399 else
1400 return FALSE;
1402 if (reversep)
1404 std::swap (itrue, ifalse);
1405 diff = trunc_int_for_mode (-(unsigned HOST_WIDE_INT) diff, mode);
1408 start_sequence ();
1410 /* If we have x := test ? x + 3 : x + 4 then move the original
1411 x out of the way while we store flags. */
1412 if (common && rtx_equal_p (common, if_info->x))
1414 common = gen_reg_rtx (mode);
1415 noce_emit_move_insn (common, if_info->x);
1418 target = noce_emit_store_flag (if_info, if_info->x, reversep, normalize);
1419 if (! target)
1421 end_sequence ();
1422 return FALSE;
1425 /* if (test) x = 3; else x = 4;
1426 => x = 3 + (test == 0); */
1427 if (diff == STORE_FLAG_VALUE || diff == -STORE_FLAG_VALUE)
1429 /* Add the common part now. This may allow combine to merge this
1430 with the store flag operation earlier into some sort of conditional
1431 increment/decrement if the target allows it. */
1432 if (common)
1433 target = expand_simple_binop (mode, PLUS,
1434 target, common,
1435 target, 0, OPTAB_WIDEN);
1437 /* Always use ifalse here. It should have been swapped with itrue
1438 when appropriate when reversep is true. */
1439 target = expand_simple_binop (mode, subtract_flag_p ? MINUS : PLUS,
1440 gen_int_mode (ifalse, mode), target,
1441 if_info->x, 0, OPTAB_WIDEN);
1443 /* Other cases are not beneficial when the original A and B are PLUS
1444 expressions. */
1445 else if (common)
1447 end_sequence ();
1448 return FALSE;
1450 /* if (test) x = 8; else x = 0;
1451 => x = (test != 0) << 3; */
1452 else if (ifalse == 0 && (tmp = exact_log2 (itrue)) >= 0)
1454 target = expand_simple_binop (mode, ASHIFT,
1455 target, GEN_INT (tmp), if_info->x, 0,
1456 OPTAB_WIDEN);
1459 /* if (test) x = -1; else x = b;
1460 => x = -(test != 0) | b; */
1461 else if (itrue == -1)
1463 target = expand_simple_binop (mode, IOR,
1464 target, gen_int_mode (ifalse, mode),
1465 if_info->x, 0, OPTAB_WIDEN);
1467 else
1469 end_sequence ();
1470 return FALSE;
1473 if (! target)
1475 end_sequence ();
1476 return FALSE;
1479 if (target != if_info->x)
1480 noce_emit_move_insn (if_info->x, target);
1482 seq = end_ifcvt_sequence (if_info);
1483 if (!seq || !targetm.noce_conversion_profitable_p (seq, if_info))
1484 return FALSE;
1486 emit_insn_before_setloc (seq, if_info->jump,
1487 INSN_LOCATION (if_info->insn_a));
1488 if_info->transform_name = "noce_try_store_flag_constants";
1490 return TRUE;
1493 return FALSE;
1496 /* Convert "if (test) foo++" into "foo += (test != 0)", and
1497 similarly for "foo--". */
1499 static int
1500 noce_try_addcc (struct noce_if_info *if_info)
1502 rtx target;
1503 rtx_insn *seq;
1504 int subtract, normalize;
1506 if (!noce_simple_bbs (if_info))
1507 return FALSE;
1509 if (GET_CODE (if_info->a) == PLUS
1510 && rtx_equal_p (XEXP (if_info->a, 0), if_info->b)
1511 && noce_reversed_cond_code (if_info) != UNKNOWN)
1513 rtx cond = if_info->rev_cond;
1514 enum rtx_code code;
1516 if (cond == NULL_RTX)
1518 cond = if_info->cond;
1519 code = reversed_comparison_code (cond, if_info->jump);
1521 else
1522 code = GET_CODE (cond);
1524 /* First try to use addcc pattern. */
1525 if (general_operand (XEXP (cond, 0), VOIDmode)
1526 && general_operand (XEXP (cond, 1), VOIDmode))
1528 start_sequence ();
1529 target = emit_conditional_add (if_info->x, code,
1530 XEXP (cond, 0),
1531 XEXP (cond, 1),
1532 VOIDmode,
1533 if_info->b,
1534 XEXP (if_info->a, 1),
1535 GET_MODE (if_info->x),
1536 (code == LTU || code == GEU
1537 || code == LEU || code == GTU));
1538 if (target)
1540 if (target != if_info->x)
1541 noce_emit_move_insn (if_info->x, target);
1543 seq = end_ifcvt_sequence (if_info);
1544 if (!seq || !targetm.noce_conversion_profitable_p (seq, if_info))
1545 return FALSE;
1547 emit_insn_before_setloc (seq, if_info->jump,
1548 INSN_LOCATION (if_info->insn_a));
1549 if_info->transform_name = "noce_try_addcc";
1551 return TRUE;
1553 end_sequence ();
1556 /* If that fails, construct conditional increment or decrement using
1557 setcc. We're changing a branch and an increment to a comparison and
1558 an ADD/SUB. */
1559 if (XEXP (if_info->a, 1) == const1_rtx
1560 || XEXP (if_info->a, 1) == constm1_rtx)
1562 start_sequence ();
1563 if (STORE_FLAG_VALUE == INTVAL (XEXP (if_info->a, 1)))
1564 subtract = 0, normalize = 0;
1565 else if (-STORE_FLAG_VALUE == INTVAL (XEXP (if_info->a, 1)))
1566 subtract = 1, normalize = 0;
1567 else
1568 subtract = 0, normalize = INTVAL (XEXP (if_info->a, 1));
1571 target = noce_emit_store_flag (if_info,
1572 gen_reg_rtx (GET_MODE (if_info->x)),
1573 1, normalize);
1575 if (target)
1576 target = expand_simple_binop (GET_MODE (if_info->x),
1577 subtract ? MINUS : PLUS,
1578 if_info->b, target, if_info->x,
1579 0, OPTAB_WIDEN);
1580 if (target)
1582 if (target != if_info->x)
1583 noce_emit_move_insn (if_info->x, target);
1585 seq = end_ifcvt_sequence (if_info);
1586 if (!seq || !targetm.noce_conversion_profitable_p (seq, if_info))
1587 return FALSE;
1589 emit_insn_before_setloc (seq, if_info->jump,
1590 INSN_LOCATION (if_info->insn_a));
1591 if_info->transform_name = "noce_try_addcc";
1592 return TRUE;
1594 end_sequence ();
1598 return FALSE;
1601 /* Convert "if (test) x = 0;" to "x &= -(test == 0);" */
1603 static int
1604 noce_try_store_flag_mask (struct noce_if_info *if_info)
1606 rtx target;
1607 rtx_insn *seq;
1608 int reversep;
1610 if (!noce_simple_bbs (if_info))
1611 return FALSE;
1613 reversep = 0;
1615 if ((if_info->a == const0_rtx
1616 && rtx_equal_p (if_info->b, if_info->x))
1617 || ((reversep = (noce_reversed_cond_code (if_info) != UNKNOWN))
1618 && if_info->b == const0_rtx
1619 && rtx_equal_p (if_info->a, if_info->x)))
1621 start_sequence ();
1622 target = noce_emit_store_flag (if_info,
1623 gen_reg_rtx (GET_MODE (if_info->x)),
1624 reversep, -1);
1625 if (target)
1626 target = expand_simple_binop (GET_MODE (if_info->x), AND,
1627 if_info->x,
1628 target, if_info->x, 0,
1629 OPTAB_WIDEN);
1631 if (target)
1633 if (target != if_info->x)
1634 noce_emit_move_insn (if_info->x, target);
1636 seq = end_ifcvt_sequence (if_info);
1637 if (!seq || !targetm.noce_conversion_profitable_p (seq, if_info))
1638 return FALSE;
1640 emit_insn_before_setloc (seq, if_info->jump,
1641 INSN_LOCATION (if_info->insn_a));
1642 if_info->transform_name = "noce_try_store_flag_mask";
1644 return TRUE;
1647 end_sequence ();
1650 return FALSE;
1653 /* Helper function for noce_try_cmove and noce_try_cmove_arith. */
1655 static rtx
1656 noce_emit_cmove (struct noce_if_info *if_info, rtx x, enum rtx_code code,
1657 rtx cmp_a, rtx cmp_b, rtx vfalse, rtx vtrue)
1659 rtx target ATTRIBUTE_UNUSED;
1660 int unsignedp ATTRIBUTE_UNUSED;
1662 /* If earliest == jump, try to build the cmove insn directly.
1663 This is helpful when combine has created some complex condition
1664 (like for alpha's cmovlbs) that we can't hope to regenerate
1665 through the normal interface. */
1667 if (if_info->cond_earliest == if_info->jump)
1669 rtx cond = gen_rtx_fmt_ee (code, GET_MODE (if_info->cond), cmp_a, cmp_b);
1670 rtx if_then_else = gen_rtx_IF_THEN_ELSE (GET_MODE (x),
1671 cond, vtrue, vfalse);
1672 rtx set = gen_rtx_SET (x, if_then_else);
1674 start_sequence ();
1675 rtx_insn *insn = emit_insn (set);
1677 if (recog_memoized (insn) >= 0)
1679 rtx_insn *seq = get_insns ();
1680 end_sequence ();
1681 emit_insn (seq);
1683 return x;
1686 end_sequence ();
1689 /* Don't even try if the comparison operands are weird
1690 except that the target supports cbranchcc4. */
1691 if (! general_operand (cmp_a, GET_MODE (cmp_a))
1692 || ! general_operand (cmp_b, GET_MODE (cmp_b)))
1694 if (!have_cbranchcc4
1695 || GET_MODE_CLASS (GET_MODE (cmp_a)) != MODE_CC
1696 || cmp_b != const0_rtx)
1697 return NULL_RTX;
1700 unsignedp = (code == LTU || code == GEU
1701 || code == LEU || code == GTU);
1703 target = emit_conditional_move (x, code, cmp_a, cmp_b, VOIDmode,
1704 vtrue, vfalse, GET_MODE (x),
1705 unsignedp);
1706 if (target)
1707 return target;
1709 /* We might be faced with a situation like:
1711 x = (reg:M TARGET)
1712 vtrue = (subreg:M (reg:N VTRUE) BYTE)
1713 vfalse = (subreg:M (reg:N VFALSE) BYTE)
1715 We can't do a conditional move in mode M, but it's possible that we
1716 could do a conditional move in mode N instead and take a subreg of
1717 the result.
1719 If we can't create new pseudos, though, don't bother. */
1720 if (reload_completed)
1721 return NULL_RTX;
1723 if (GET_CODE (vtrue) == SUBREG && GET_CODE (vfalse) == SUBREG)
1725 rtx reg_vtrue = SUBREG_REG (vtrue);
1726 rtx reg_vfalse = SUBREG_REG (vfalse);
1727 unsigned int byte_vtrue = SUBREG_BYTE (vtrue);
1728 unsigned int byte_vfalse = SUBREG_BYTE (vfalse);
1729 rtx promoted_target;
1731 if (GET_MODE (reg_vtrue) != GET_MODE (reg_vfalse)
1732 || byte_vtrue != byte_vfalse
1733 || (SUBREG_PROMOTED_VAR_P (vtrue)
1734 != SUBREG_PROMOTED_VAR_P (vfalse))
1735 || (SUBREG_PROMOTED_GET (vtrue)
1736 != SUBREG_PROMOTED_GET (vfalse)))
1737 return NULL_RTX;
1739 promoted_target = gen_reg_rtx (GET_MODE (reg_vtrue));
1741 target = emit_conditional_move (promoted_target, code, cmp_a, cmp_b,
1742 VOIDmode, reg_vtrue, reg_vfalse,
1743 GET_MODE (reg_vtrue), unsignedp);
1744 /* Nope, couldn't do it in that mode either. */
1745 if (!target)
1746 return NULL_RTX;
1748 target = gen_rtx_SUBREG (GET_MODE (vtrue), promoted_target, byte_vtrue);
1749 SUBREG_PROMOTED_VAR_P (target) = SUBREG_PROMOTED_VAR_P (vtrue);
1750 SUBREG_PROMOTED_SET (target, SUBREG_PROMOTED_GET (vtrue));
1751 emit_move_insn (x, target);
1752 return x;
1754 else
1755 return NULL_RTX;
1758 /* Try only simple constants and registers here. More complex cases
1759 are handled in noce_try_cmove_arith after noce_try_store_flag_arith
1760 has had a go at it. */
1762 static int
1763 noce_try_cmove (struct noce_if_info *if_info)
1765 enum rtx_code code;
1766 rtx target;
1767 rtx_insn *seq;
1769 if (!noce_simple_bbs (if_info))
1770 return FALSE;
1772 if ((CONSTANT_P (if_info->a) || register_operand (if_info->a, VOIDmode))
1773 && (CONSTANT_P (if_info->b) || register_operand (if_info->b, VOIDmode)))
1775 start_sequence ();
1777 code = GET_CODE (if_info->cond);
1778 target = noce_emit_cmove (if_info, if_info->x, code,
1779 XEXP (if_info->cond, 0),
1780 XEXP (if_info->cond, 1),
1781 if_info->a, if_info->b);
1783 if (target)
1785 if (target != if_info->x)
1786 noce_emit_move_insn (if_info->x, target);
1788 seq = end_ifcvt_sequence (if_info);
1789 if (!seq || !targetm.noce_conversion_profitable_p (seq, if_info))
1790 return FALSE;
1792 emit_insn_before_setloc (seq, if_info->jump,
1793 INSN_LOCATION (if_info->insn_a));
1794 if_info->transform_name = "noce_try_cmove";
1796 return TRUE;
1798 /* If both a and b are constants try a last-ditch transformation:
1799 if (test) x = a; else x = b;
1800 => x = (-(test != 0) & (b - a)) + a;
1801 Try this only if the target-specific expansion above has failed.
1802 The target-specific expander may want to generate sequences that
1803 we don't know about, so give them a chance before trying this
1804 approach. */
1805 else if (!targetm.have_conditional_execution ()
1806 && CONST_INT_P (if_info->a) && CONST_INT_P (if_info->b))
1808 machine_mode mode = GET_MODE (if_info->x);
1809 HOST_WIDE_INT ifalse = INTVAL (if_info->a);
1810 HOST_WIDE_INT itrue = INTVAL (if_info->b);
1811 rtx target = noce_emit_store_flag (if_info, if_info->x, false, -1);
1812 if (!target)
1814 end_sequence ();
1815 return FALSE;
1818 HOST_WIDE_INT diff = (unsigned HOST_WIDE_INT) itrue - ifalse;
1819 /* Make sure we can represent the difference
1820 between the two values. */
1821 if ((diff > 0)
1822 != ((ifalse < 0) != (itrue < 0) ? ifalse < 0 : ifalse < itrue))
1824 end_sequence ();
1825 return FALSE;
1828 diff = trunc_int_for_mode (diff, mode);
1829 target = expand_simple_binop (mode, AND,
1830 target, gen_int_mode (diff, mode),
1831 if_info->x, 0, OPTAB_WIDEN);
1832 if (target)
1833 target = expand_simple_binop (mode, PLUS,
1834 target, gen_int_mode (ifalse, mode),
1835 if_info->x, 0, OPTAB_WIDEN);
1836 if (target)
1838 if (target != if_info->x)
1839 noce_emit_move_insn (if_info->x, target);
1841 seq = end_ifcvt_sequence (if_info);
1842 if (!seq || !targetm.noce_conversion_profitable_p (seq, if_info))
1843 return FALSE;
1845 emit_insn_before_setloc (seq, if_info->jump,
1846 INSN_LOCATION (if_info->insn_a));
1847 if_info->transform_name = "noce_try_cmove";
1848 return TRUE;
1850 else
1852 end_sequence ();
1853 return FALSE;
1856 else
1857 end_sequence ();
1860 return FALSE;
1863 /* Return true if X contains a conditional code mode rtx. */
1865 static bool
1866 contains_ccmode_rtx_p (rtx x)
1868 subrtx_iterator::array_type array;
1869 FOR_EACH_SUBRTX (iter, array, x, ALL)
1870 if (GET_MODE_CLASS (GET_MODE (*iter)) == MODE_CC)
1871 return true;
1873 return false;
1876 /* Helper for bb_valid_for_noce_process_p. Validate that
1877 the rtx insn INSN is a single set that does not set
1878 the conditional register CC and is in general valid for
1879 if-conversion. */
1881 static bool
1882 insn_valid_noce_process_p (rtx_insn *insn, rtx cc)
1884 if (!insn
1885 || !NONJUMP_INSN_P (insn)
1886 || (cc && set_of (cc, insn)))
1887 return false;
1889 rtx sset = single_set (insn);
1891 /* Currently support only simple single sets in test_bb. */
1892 if (!sset
1893 || !noce_operand_ok (SET_DEST (sset))
1894 || contains_ccmode_rtx_p (SET_DEST (sset))
1895 || !noce_operand_ok (SET_SRC (sset)))
1896 return false;
1898 return true;
1902 /* Return true iff the registers that the insns in BB_A set do not get
1903 used in BB_B. If TO_RENAME is non-NULL then it is a location that will be
1904 renamed later by the caller and so conflicts on it should be ignored
1905 in this function. */
1907 static bool
1908 bbs_ok_for_cmove_arith (basic_block bb_a, basic_block bb_b, rtx to_rename)
1910 rtx_insn *a_insn;
1911 bitmap bba_sets = BITMAP_ALLOC (&reg_obstack);
1913 df_ref def;
1914 df_ref use;
1916 FOR_BB_INSNS (bb_a, a_insn)
1918 if (!active_insn_p (a_insn))
1919 continue;
1921 rtx sset_a = single_set (a_insn);
1923 if (!sset_a)
1925 BITMAP_FREE (bba_sets);
1926 return false;
1928 /* Record all registers that BB_A sets. */
1929 FOR_EACH_INSN_DEF (def, a_insn)
1930 if (!(to_rename && DF_REF_REG (def) == to_rename))
1931 bitmap_set_bit (bba_sets, DF_REF_REGNO (def));
1934 rtx_insn *b_insn;
1936 FOR_BB_INSNS (bb_b, b_insn)
1938 if (!active_insn_p (b_insn))
1939 continue;
1941 rtx sset_b = single_set (b_insn);
1943 if (!sset_b)
1945 BITMAP_FREE (bba_sets);
1946 return false;
1949 /* Make sure this is a REG and not some instance
1950 of ZERO_EXTRACT or SUBREG or other dangerous stuff.
1951 If we have a memory destination then we have a pair of simple
1952 basic blocks performing an operation of the form [addr] = c ? a : b.
1953 bb_valid_for_noce_process_p will have ensured that these are
1954 the only stores present. In that case [addr] should be the location
1955 to be renamed. Assert that the callers set this up properly. */
1956 if (MEM_P (SET_DEST (sset_b)))
1957 gcc_assert (rtx_equal_p (SET_DEST (sset_b), to_rename));
1958 else if (!REG_P (SET_DEST (sset_b)))
1960 BITMAP_FREE (bba_sets);
1961 return false;
1964 /* If the insn uses a reg set in BB_A return false. */
1965 FOR_EACH_INSN_USE (use, b_insn)
1967 if (bitmap_bit_p (bba_sets, DF_REF_REGNO (use)))
1969 BITMAP_FREE (bba_sets);
1970 return false;
1976 BITMAP_FREE (bba_sets);
1977 return true;
1980 /* Emit copies of all the active instructions in BB except the last.
1981 This is a helper for noce_try_cmove_arith. */
1983 static void
1984 noce_emit_all_but_last (basic_block bb)
1986 rtx_insn *last = last_active_insn (bb, FALSE);
1987 rtx_insn *insn;
1988 FOR_BB_INSNS (bb, insn)
1990 if (insn != last && active_insn_p (insn))
1992 rtx_insn *to_emit = as_a <rtx_insn *> (copy_rtx (insn));
1994 emit_insn (PATTERN (to_emit));
1999 /* Helper for noce_try_cmove_arith. Emit the pattern TO_EMIT and return
2000 the resulting insn or NULL if it's not a valid insn. */
2002 static rtx_insn *
2003 noce_emit_insn (rtx to_emit)
2005 gcc_assert (to_emit);
2006 rtx_insn *insn = emit_insn (to_emit);
2008 if (recog_memoized (insn) < 0)
2009 return NULL;
2011 return insn;
2014 /* Helper for noce_try_cmove_arith. Emit a copy of the insns up to
2015 and including the penultimate one in BB if it is not simple
2016 (as indicated by SIMPLE). Then emit LAST_INSN as the last
2017 insn in the block. The reason for that is that LAST_INSN may
2018 have been modified by the preparation in noce_try_cmove_arith. */
2020 static bool
2021 noce_emit_bb (rtx last_insn, basic_block bb, bool simple)
2023 if (bb && !simple)
2024 noce_emit_all_but_last (bb);
2026 if (last_insn && !noce_emit_insn (last_insn))
2027 return false;
2029 return true;
2032 /* Try more complex cases involving conditional_move. */
2034 static int
2035 noce_try_cmove_arith (struct noce_if_info *if_info)
2037 rtx a = if_info->a;
2038 rtx b = if_info->b;
2039 rtx x = if_info->x;
2040 rtx orig_a, orig_b;
2041 rtx_insn *insn_a, *insn_b;
2042 bool a_simple = if_info->then_simple;
2043 bool b_simple = if_info->else_simple;
2044 basic_block then_bb = if_info->then_bb;
2045 basic_block else_bb = if_info->else_bb;
2046 rtx target;
2047 int is_mem = 0;
2048 enum rtx_code code;
2049 rtx cond = if_info->cond;
2050 rtx_insn *ifcvt_seq;
2052 /* A conditional move from two memory sources is equivalent to a
2053 conditional on their addresses followed by a load. Don't do this
2054 early because it'll screw alias analysis. Note that we've
2055 already checked for no side effects. */
2056 if (cse_not_expected
2057 && MEM_P (a) && MEM_P (b)
2058 && MEM_ADDR_SPACE (a) == MEM_ADDR_SPACE (b))
2060 machine_mode address_mode = get_address_mode (a);
2062 a = XEXP (a, 0);
2063 b = XEXP (b, 0);
2064 x = gen_reg_rtx (address_mode);
2065 is_mem = 1;
2068 /* ??? We could handle this if we knew that a load from A or B could
2069 not trap or fault. This is also true if we've already loaded
2070 from the address along the path from ENTRY. */
2071 else if (may_trap_or_fault_p (a) || may_trap_or_fault_p (b))
2072 return FALSE;
2074 /* if (test) x = a + b; else x = c - d;
2075 => y = a + b;
2076 x = c - d;
2077 if (test)
2078 x = y;
2081 code = GET_CODE (cond);
2082 insn_a = if_info->insn_a;
2083 insn_b = if_info->insn_b;
2085 machine_mode x_mode = GET_MODE (x);
2087 if (!can_conditionally_move_p (x_mode))
2088 return FALSE;
2090 /* Possibly rearrange operands to make things come out more natural. */
2091 if (noce_reversed_cond_code (if_info) != UNKNOWN)
2093 int reversep = 0;
2094 if (rtx_equal_p (b, x))
2095 reversep = 1;
2096 else if (general_operand (b, GET_MODE (b)))
2097 reversep = 1;
2099 if (reversep)
2101 if (if_info->rev_cond)
2103 cond = if_info->rev_cond;
2104 code = GET_CODE (cond);
2106 else
2107 code = reversed_comparison_code (cond, if_info->jump);
2108 std::swap (a, b);
2109 std::swap (insn_a, insn_b);
2110 std::swap (a_simple, b_simple);
2111 std::swap (then_bb, else_bb);
2115 if (then_bb && else_bb
2116 && (!bbs_ok_for_cmove_arith (then_bb, else_bb, if_info->orig_x)
2117 || !bbs_ok_for_cmove_arith (else_bb, then_bb, if_info->orig_x)))
2118 return FALSE;
2120 start_sequence ();
2122 /* If one of the blocks is empty then the corresponding B or A value
2123 came from the test block. The non-empty complex block that we will
2124 emit might clobber the register used by B or A, so move it to a pseudo
2125 first. */
2127 rtx tmp_a = NULL_RTX;
2128 rtx tmp_b = NULL_RTX;
2130 if (b_simple || !else_bb)
2131 tmp_b = gen_reg_rtx (x_mode);
2133 if (a_simple || !then_bb)
2134 tmp_a = gen_reg_rtx (x_mode);
2136 orig_a = a;
2137 orig_b = b;
2139 rtx emit_a = NULL_RTX;
2140 rtx emit_b = NULL_RTX;
2141 rtx_insn *tmp_insn = NULL;
2142 bool modified_in_a = false;
2143 bool modified_in_b = false;
2144 /* If either operand is complex, load it into a register first.
2145 The best way to do this is to copy the original insn. In this
2146 way we preserve any clobbers etc that the insn may have had.
2147 This is of course not possible in the IS_MEM case. */
2149 if (! general_operand (a, GET_MODE (a)) || tmp_a)
2152 if (is_mem)
2154 rtx reg = gen_reg_rtx (GET_MODE (a));
2155 emit_a = gen_rtx_SET (reg, a);
2157 else
2159 if (insn_a)
2161 a = tmp_a ? tmp_a : gen_reg_rtx (GET_MODE (a));
2163 rtx_insn *copy_of_a = as_a <rtx_insn *> (copy_rtx (insn_a));
2164 rtx set = single_set (copy_of_a);
2165 SET_DEST (set) = a;
2167 emit_a = PATTERN (copy_of_a);
2169 else
2171 rtx tmp_reg = tmp_a ? tmp_a : gen_reg_rtx (GET_MODE (a));
2172 emit_a = gen_rtx_SET (tmp_reg, a);
2173 a = tmp_reg;
2178 if (! general_operand (b, GET_MODE (b)) || tmp_b)
2180 if (is_mem)
2182 rtx reg = gen_reg_rtx (GET_MODE (b));
2183 emit_b = gen_rtx_SET (reg, b);
2185 else
2187 if (insn_b)
2189 b = tmp_b ? tmp_b : gen_reg_rtx (GET_MODE (b));
2190 rtx_insn *copy_of_b = as_a <rtx_insn *> (copy_rtx (insn_b));
2191 rtx set = single_set (copy_of_b);
2193 SET_DEST (set) = b;
2194 emit_b = PATTERN (copy_of_b);
2196 else
2198 rtx tmp_reg = tmp_b ? tmp_b : gen_reg_rtx (GET_MODE (b));
2199 emit_b = gen_rtx_SET (tmp_reg, b);
2200 b = tmp_reg;
2205 modified_in_a = emit_a != NULL_RTX && modified_in_p (orig_b, emit_a);
2206 if (tmp_b && then_bb)
2208 FOR_BB_INSNS (then_bb, tmp_insn)
2209 /* Don't check inside insn_a. We will have changed it to emit_a
2210 with a destination that doesn't conflict. */
2211 if (!(insn_a && tmp_insn == insn_a)
2212 && modified_in_p (orig_b, tmp_insn))
2214 modified_in_a = true;
2215 break;
2220 modified_in_b = emit_b != NULL_RTX && modified_in_p (orig_a, emit_b);
2221 if (tmp_a && else_bb)
2223 FOR_BB_INSNS (else_bb, tmp_insn)
2224 /* Don't check inside insn_b. We will have changed it to emit_b
2225 with a destination that doesn't conflict. */
2226 if (!(insn_b && tmp_insn == insn_b)
2227 && modified_in_p (orig_a, tmp_insn))
2229 modified_in_b = true;
2230 break;
2234 /* If insn to set up A clobbers any registers B depends on, try to
2235 swap insn that sets up A with the one that sets up B. If even
2236 that doesn't help, punt. */
2237 if (modified_in_a && !modified_in_b)
2239 if (!noce_emit_bb (emit_b, else_bb, b_simple))
2240 goto end_seq_and_fail;
2242 if (!noce_emit_bb (emit_a, then_bb, a_simple))
2243 goto end_seq_and_fail;
2245 else if (!modified_in_a)
2247 if (!noce_emit_bb (emit_a, then_bb, a_simple))
2248 goto end_seq_and_fail;
2250 if (!noce_emit_bb (emit_b, else_bb, b_simple))
2251 goto end_seq_and_fail;
2253 else
2254 goto end_seq_and_fail;
2256 target = noce_emit_cmove (if_info, x, code, XEXP (cond, 0), XEXP (cond, 1),
2257 a, b);
2259 if (! target)
2260 goto end_seq_and_fail;
2262 /* If we're handling a memory for above, emit the load now. */
2263 if (is_mem)
2265 rtx mem = gen_rtx_MEM (GET_MODE (if_info->x), target);
2267 /* Copy over flags as appropriate. */
2268 if (MEM_VOLATILE_P (if_info->a) || MEM_VOLATILE_P (if_info->b))
2269 MEM_VOLATILE_P (mem) = 1;
2270 if (MEM_ALIAS_SET (if_info->a) == MEM_ALIAS_SET (if_info->b))
2271 set_mem_alias_set (mem, MEM_ALIAS_SET (if_info->a));
2272 set_mem_align (mem,
2273 MIN (MEM_ALIGN (if_info->a), MEM_ALIGN (if_info->b)));
2275 gcc_assert (MEM_ADDR_SPACE (if_info->a) == MEM_ADDR_SPACE (if_info->b));
2276 set_mem_addr_space (mem, MEM_ADDR_SPACE (if_info->a));
2278 noce_emit_move_insn (if_info->x, mem);
2280 else if (target != x)
2281 noce_emit_move_insn (x, target);
2283 ifcvt_seq = end_ifcvt_sequence (if_info);
2284 if (!ifcvt_seq || !targetm.noce_conversion_profitable_p (ifcvt_seq, if_info))
2285 return FALSE;
2287 emit_insn_before_setloc (ifcvt_seq, if_info->jump,
2288 INSN_LOCATION (if_info->insn_a));
2289 if_info->transform_name = "noce_try_cmove_arith";
2290 return TRUE;
2292 end_seq_and_fail:
2293 end_sequence ();
2294 return FALSE;
2297 /* For most cases, the simplified condition we found is the best
2298 choice, but this is not the case for the min/max/abs transforms.
2299 For these we wish to know that it is A or B in the condition. */
2301 static rtx
2302 noce_get_alt_condition (struct noce_if_info *if_info, rtx target,
2303 rtx_insn **earliest)
2305 rtx cond, set;
2306 rtx_insn *insn;
2307 int reverse;
2309 /* If target is already mentioned in the known condition, return it. */
2310 if (reg_mentioned_p (target, if_info->cond))
2312 *earliest = if_info->cond_earliest;
2313 return if_info->cond;
2316 set = pc_set (if_info->jump);
2317 cond = XEXP (SET_SRC (set), 0);
2318 reverse
2319 = GET_CODE (XEXP (SET_SRC (set), 2)) == LABEL_REF
2320 && label_ref_label (XEXP (SET_SRC (set), 2)) == JUMP_LABEL (if_info->jump);
2321 if (if_info->then_else_reversed)
2322 reverse = !reverse;
2324 /* If we're looking for a constant, try to make the conditional
2325 have that constant in it. There are two reasons why it may
2326 not have the constant we want:
2328 1. GCC may have needed to put the constant in a register, because
2329 the target can't compare directly against that constant. For
2330 this case, we look for a SET immediately before the comparison
2331 that puts a constant in that register.
2333 2. GCC may have canonicalized the conditional, for example
2334 replacing "if x < 4" with "if x <= 3". We can undo that (or
2335 make equivalent types of changes) to get the constants we need
2336 if they're off by one in the right direction. */
2338 if (CONST_INT_P (target))
2340 enum rtx_code code = GET_CODE (if_info->cond);
2341 rtx op_a = XEXP (if_info->cond, 0);
2342 rtx op_b = XEXP (if_info->cond, 1);
2343 rtx_insn *prev_insn;
2345 /* First, look to see if we put a constant in a register. */
2346 prev_insn = prev_nonnote_insn (if_info->cond_earliest);
2347 if (prev_insn
2348 && BLOCK_FOR_INSN (prev_insn)
2349 == BLOCK_FOR_INSN (if_info->cond_earliest)
2350 && INSN_P (prev_insn)
2351 && GET_CODE (PATTERN (prev_insn)) == SET)
2353 rtx src = find_reg_equal_equiv_note (prev_insn);
2354 if (!src)
2355 src = SET_SRC (PATTERN (prev_insn));
2356 if (CONST_INT_P (src))
2358 if (rtx_equal_p (op_a, SET_DEST (PATTERN (prev_insn))))
2359 op_a = src;
2360 else if (rtx_equal_p (op_b, SET_DEST (PATTERN (prev_insn))))
2361 op_b = src;
2363 if (CONST_INT_P (op_a))
2365 std::swap (op_a, op_b);
2366 code = swap_condition (code);
2371 /* Now, look to see if we can get the right constant by
2372 adjusting the conditional. */
2373 if (CONST_INT_P (op_b))
2375 HOST_WIDE_INT desired_val = INTVAL (target);
2376 HOST_WIDE_INT actual_val = INTVAL (op_b);
2378 switch (code)
2380 case LT:
2381 if (desired_val != HOST_WIDE_INT_MAX
2382 && actual_val == desired_val + 1)
2384 code = LE;
2385 op_b = GEN_INT (desired_val);
2387 break;
2388 case LE:
2389 if (desired_val != HOST_WIDE_INT_MIN
2390 && actual_val == desired_val - 1)
2392 code = LT;
2393 op_b = GEN_INT (desired_val);
2395 break;
2396 case GT:
2397 if (desired_val != HOST_WIDE_INT_MIN
2398 && actual_val == desired_val - 1)
2400 code = GE;
2401 op_b = GEN_INT (desired_val);
2403 break;
2404 case GE:
2405 if (desired_val != HOST_WIDE_INT_MAX
2406 && actual_val == desired_val + 1)
2408 code = GT;
2409 op_b = GEN_INT (desired_val);
2411 break;
2412 default:
2413 break;
2417 /* If we made any changes, generate a new conditional that is
2418 equivalent to what we started with, but has the right
2419 constants in it. */
2420 if (code != GET_CODE (if_info->cond)
2421 || op_a != XEXP (if_info->cond, 0)
2422 || op_b != XEXP (if_info->cond, 1))
2424 cond = gen_rtx_fmt_ee (code, GET_MODE (cond), op_a, op_b);
2425 *earliest = if_info->cond_earliest;
2426 return cond;
2430 cond = canonicalize_condition (if_info->jump, cond, reverse,
2431 earliest, target, have_cbranchcc4, true);
2432 if (! cond || ! reg_mentioned_p (target, cond))
2433 return NULL;
2435 /* We almost certainly searched back to a different place.
2436 Need to re-verify correct lifetimes. */
2438 /* X may not be mentioned in the range (cond_earliest, jump]. */
2439 for (insn = if_info->jump; insn != *earliest; insn = PREV_INSN (insn))
2440 if (INSN_P (insn) && reg_overlap_mentioned_p (if_info->x, PATTERN (insn)))
2441 return NULL;
2443 /* A and B may not be modified in the range [cond_earliest, jump). */
2444 for (insn = *earliest; insn != if_info->jump; insn = NEXT_INSN (insn))
2445 if (INSN_P (insn)
2446 && (modified_in_p (if_info->a, insn)
2447 || modified_in_p (if_info->b, insn)))
2448 return NULL;
2450 return cond;
2453 /* Convert "if (a < b) x = a; else x = b;" to "x = min(a, b);", etc. */
2455 static int
2456 noce_try_minmax (struct noce_if_info *if_info)
2458 rtx cond, target;
2459 rtx_insn *earliest, *seq;
2460 enum rtx_code code, op;
2461 int unsignedp;
2463 if (!noce_simple_bbs (if_info))
2464 return FALSE;
2466 /* ??? Reject modes with NaNs or signed zeros since we don't know how
2467 they will be resolved with an SMIN/SMAX. It wouldn't be too hard
2468 to get the target to tell us... */
2469 if (HONOR_SIGNED_ZEROS (if_info->x)
2470 || HONOR_NANS (if_info->x))
2471 return FALSE;
2473 cond = noce_get_alt_condition (if_info, if_info->a, &earliest);
2474 if (!cond)
2475 return FALSE;
2477 /* Verify the condition is of the form we expect, and canonicalize
2478 the comparison code. */
2479 code = GET_CODE (cond);
2480 if (rtx_equal_p (XEXP (cond, 0), if_info->a))
2482 if (! rtx_equal_p (XEXP (cond, 1), if_info->b))
2483 return FALSE;
2485 else if (rtx_equal_p (XEXP (cond, 1), if_info->a))
2487 if (! rtx_equal_p (XEXP (cond, 0), if_info->b))
2488 return FALSE;
2489 code = swap_condition (code);
2491 else
2492 return FALSE;
2494 /* Determine what sort of operation this is. Note that the code is for
2495 a taken branch, so the code->operation mapping appears backwards. */
2496 switch (code)
2498 case LT:
2499 case LE:
2500 case UNLT:
2501 case UNLE:
2502 op = SMAX;
2503 unsignedp = 0;
2504 break;
2505 case GT:
2506 case GE:
2507 case UNGT:
2508 case UNGE:
2509 op = SMIN;
2510 unsignedp = 0;
2511 break;
2512 case LTU:
2513 case LEU:
2514 op = UMAX;
2515 unsignedp = 1;
2516 break;
2517 case GTU:
2518 case GEU:
2519 op = UMIN;
2520 unsignedp = 1;
2521 break;
2522 default:
2523 return FALSE;
2526 start_sequence ();
2528 target = expand_simple_binop (GET_MODE (if_info->x), op,
2529 if_info->a, if_info->b,
2530 if_info->x, unsignedp, OPTAB_WIDEN);
2531 if (! target)
2533 end_sequence ();
2534 return FALSE;
2536 if (target != if_info->x)
2537 noce_emit_move_insn (if_info->x, target);
2539 seq = end_ifcvt_sequence (if_info);
2540 if (!seq)
2541 return FALSE;
2543 emit_insn_before_setloc (seq, if_info->jump, INSN_LOCATION (if_info->insn_a));
2544 if_info->cond = cond;
2545 if_info->cond_earliest = earliest;
2546 if_info->rev_cond = NULL_RTX;
2547 if_info->transform_name = "noce_try_minmax";
2549 return TRUE;
2552 /* Convert "if (a < 0) x = -a; else x = a;" to "x = abs(a);",
2553 "if (a < 0) x = ~a; else x = a;" to "x = one_cmpl_abs(a);",
2554 etc. */
2556 static int
2557 noce_try_abs (struct noce_if_info *if_info)
2559 rtx cond, target, a, b, c;
2560 rtx_insn *earliest, *seq;
2561 int negate;
2562 bool one_cmpl = false;
2564 if (!noce_simple_bbs (if_info))
2565 return FALSE;
2567 /* Reject modes with signed zeros. */
2568 if (HONOR_SIGNED_ZEROS (if_info->x))
2569 return FALSE;
2571 /* Recognize A and B as constituting an ABS or NABS. The canonical
2572 form is a branch around the negation, taken when the object is the
2573 first operand of a comparison against 0 that evaluates to true. */
2574 a = if_info->a;
2575 b = if_info->b;
2576 if (GET_CODE (a) == NEG && rtx_equal_p (XEXP (a, 0), b))
2577 negate = 0;
2578 else if (GET_CODE (b) == NEG && rtx_equal_p (XEXP (b, 0), a))
2580 std::swap (a, b);
2581 negate = 1;
2583 else if (GET_CODE (a) == NOT && rtx_equal_p (XEXP (a, 0), b))
2585 negate = 0;
2586 one_cmpl = true;
2588 else if (GET_CODE (b) == NOT && rtx_equal_p (XEXP (b, 0), a))
2590 std::swap (a, b);
2591 negate = 1;
2592 one_cmpl = true;
2594 else
2595 return FALSE;
2597 cond = noce_get_alt_condition (if_info, b, &earliest);
2598 if (!cond)
2599 return FALSE;
2601 /* Verify the condition is of the form we expect. */
2602 if (rtx_equal_p (XEXP (cond, 0), b))
2603 c = XEXP (cond, 1);
2604 else if (rtx_equal_p (XEXP (cond, 1), b))
2606 c = XEXP (cond, 0);
2607 negate = !negate;
2609 else
2610 return FALSE;
2612 /* Verify that C is zero. Search one step backward for a
2613 REG_EQUAL note or a simple source if necessary. */
2614 if (REG_P (c))
2616 rtx set;
2617 rtx_insn *insn = prev_nonnote_insn (earliest);
2618 if (insn
2619 && BLOCK_FOR_INSN (insn) == BLOCK_FOR_INSN (earliest)
2620 && (set = single_set (insn))
2621 && rtx_equal_p (SET_DEST (set), c))
2623 rtx note = find_reg_equal_equiv_note (insn);
2624 if (note)
2625 c = XEXP (note, 0);
2626 else
2627 c = SET_SRC (set);
2629 else
2630 return FALSE;
2632 if (MEM_P (c)
2633 && GET_CODE (XEXP (c, 0)) == SYMBOL_REF
2634 && CONSTANT_POOL_ADDRESS_P (XEXP (c, 0)))
2635 c = get_pool_constant (XEXP (c, 0));
2637 /* Work around funny ideas get_condition has wrt canonicalization.
2638 Note that these rtx constants are known to be CONST_INT, and
2639 therefore imply integer comparisons.
2640 The one_cmpl case is more complicated, as we want to handle
2641 only x < 0 ? ~x : x or x >= 0 ? x : ~x to one_cmpl_abs (x)
2642 and x < 0 ? x : ~x or x >= 0 ? ~x : x to ~one_cmpl_abs (x),
2643 but not other cases (x > -1 is equivalent of x >= 0). */
2644 if (c == constm1_rtx && GET_CODE (cond) == GT)
2646 else if (c == const1_rtx && GET_CODE (cond) == LT)
2648 if (one_cmpl)
2649 return FALSE;
2651 else if (c == CONST0_RTX (GET_MODE (b)))
2653 if (one_cmpl
2654 && GET_CODE (cond) != GE
2655 && GET_CODE (cond) != LT)
2656 return FALSE;
2658 else
2659 return FALSE;
2661 /* Determine what sort of operation this is. */
2662 switch (GET_CODE (cond))
2664 case LT:
2665 case LE:
2666 case UNLT:
2667 case UNLE:
2668 negate = !negate;
2669 break;
2670 case GT:
2671 case GE:
2672 case UNGT:
2673 case UNGE:
2674 break;
2675 default:
2676 return FALSE;
2679 start_sequence ();
2680 if (one_cmpl)
2681 target = expand_one_cmpl_abs_nojump (GET_MODE (if_info->x), b,
2682 if_info->x);
2683 else
2684 target = expand_abs_nojump (GET_MODE (if_info->x), b, if_info->x, 1);
2686 /* ??? It's a quandary whether cmove would be better here, especially
2687 for integers. Perhaps combine will clean things up. */
2688 if (target && negate)
2690 if (one_cmpl)
2691 target = expand_simple_unop (GET_MODE (target), NOT, target,
2692 if_info->x, 0);
2693 else
2694 target = expand_simple_unop (GET_MODE (target), NEG, target,
2695 if_info->x, 0);
2698 if (! target)
2700 end_sequence ();
2701 return FALSE;
2704 if (target != if_info->x)
2705 noce_emit_move_insn (if_info->x, target);
2707 seq = end_ifcvt_sequence (if_info);
2708 if (!seq)
2709 return FALSE;
2711 emit_insn_before_setloc (seq, if_info->jump, INSN_LOCATION (if_info->insn_a));
2712 if_info->cond = cond;
2713 if_info->cond_earliest = earliest;
2714 if_info->rev_cond = NULL_RTX;
2715 if_info->transform_name = "noce_try_abs";
2717 return TRUE;
2720 /* Convert "if (m < 0) x = b; else x = 0;" to "x = (m >> C) & b;". */
2722 static int
2723 noce_try_sign_mask (struct noce_if_info *if_info)
2725 rtx cond, t, m, c;
2726 rtx_insn *seq;
2727 machine_mode mode;
2728 enum rtx_code code;
2729 bool t_unconditional;
2731 if (!noce_simple_bbs (if_info))
2732 return FALSE;
2734 cond = if_info->cond;
2735 code = GET_CODE (cond);
2736 m = XEXP (cond, 0);
2737 c = XEXP (cond, 1);
2739 t = NULL_RTX;
2740 if (if_info->a == const0_rtx)
2742 if ((code == LT && c == const0_rtx)
2743 || (code == LE && c == constm1_rtx))
2744 t = if_info->b;
2746 else if (if_info->b == const0_rtx)
2748 if ((code == GE && c == const0_rtx)
2749 || (code == GT && c == constm1_rtx))
2750 t = if_info->a;
2753 if (! t || side_effects_p (t))
2754 return FALSE;
2756 /* We currently don't handle different modes. */
2757 mode = GET_MODE (t);
2758 if (GET_MODE (m) != mode)
2759 return FALSE;
2761 /* This is only profitable if T is unconditionally executed/evaluated in the
2762 original insn sequence or T is cheap. The former happens if B is the
2763 non-zero (T) value and if INSN_B was taken from TEST_BB, or there was no
2764 INSN_B which can happen for e.g. conditional stores to memory. For the
2765 cost computation use the block TEST_BB where the evaluation will end up
2766 after the transformation. */
2767 t_unconditional =
2768 (t == if_info->b
2769 && (if_info->insn_b == NULL_RTX
2770 || BLOCK_FOR_INSN (if_info->insn_b) == if_info->test_bb));
2771 if (!(t_unconditional
2772 || (set_src_cost (t, mode, if_info->speed_p)
2773 < COSTS_N_INSNS (2))))
2774 return FALSE;
2776 start_sequence ();
2777 /* Use emit_store_flag to generate "m < 0 ? -1 : 0" instead of expanding
2778 "(signed) m >> 31" directly. This benefits targets with specialized
2779 insns to obtain the signmask, but still uses ashr_optab otherwise. */
2780 m = emit_store_flag (gen_reg_rtx (mode), LT, m, const0_rtx, mode, 0, -1);
2781 t = m ? expand_binop (mode, and_optab, m, t, NULL_RTX, 0, OPTAB_DIRECT)
2782 : NULL_RTX;
2784 if (!t)
2786 end_sequence ();
2787 return FALSE;
2790 noce_emit_move_insn (if_info->x, t);
2792 seq = end_ifcvt_sequence (if_info);
2793 if (!seq)
2794 return FALSE;
2796 emit_insn_before_setloc (seq, if_info->jump, INSN_LOCATION (if_info->insn_a));
2797 if_info->transform_name = "noce_try_sign_mask";
2799 return TRUE;
2803 /* Optimize away "if (x & C) x |= C" and similar bit manipulation
2804 transformations. */
2806 static int
2807 noce_try_bitop (struct noce_if_info *if_info)
2809 rtx cond, x, a, result;
2810 rtx_insn *seq;
2811 machine_mode mode;
2812 enum rtx_code code;
2813 int bitnum;
2815 x = if_info->x;
2816 cond = if_info->cond;
2817 code = GET_CODE (cond);
2819 if (!noce_simple_bbs (if_info))
2820 return FALSE;
2822 /* Check for no else condition. */
2823 if (! rtx_equal_p (x, if_info->b))
2824 return FALSE;
2826 /* Check for a suitable condition. */
2827 if (code != NE && code != EQ)
2828 return FALSE;
2829 if (XEXP (cond, 1) != const0_rtx)
2830 return FALSE;
2831 cond = XEXP (cond, 0);
2833 /* ??? We could also handle AND here. */
2834 if (GET_CODE (cond) == ZERO_EXTRACT)
2836 if (XEXP (cond, 1) != const1_rtx
2837 || !CONST_INT_P (XEXP (cond, 2))
2838 || ! rtx_equal_p (x, XEXP (cond, 0)))
2839 return FALSE;
2840 bitnum = INTVAL (XEXP (cond, 2));
2841 mode = GET_MODE (x);
2842 if (BITS_BIG_ENDIAN)
2843 bitnum = GET_MODE_BITSIZE (mode) - 1 - bitnum;
2844 if (bitnum < 0 || bitnum >= HOST_BITS_PER_WIDE_INT)
2845 return FALSE;
2847 else
2848 return FALSE;
2850 a = if_info->a;
2851 if (GET_CODE (a) == IOR || GET_CODE (a) == XOR)
2853 /* Check for "if (X & C) x = x op C". */
2854 if (! rtx_equal_p (x, XEXP (a, 0))
2855 || !CONST_INT_P (XEXP (a, 1))
2856 || (INTVAL (XEXP (a, 1)) & GET_MODE_MASK (mode))
2857 != HOST_WIDE_INT_1U << bitnum)
2858 return FALSE;
2860 /* if ((x & C) == 0) x |= C; is transformed to x |= C. */
2861 /* if ((x & C) != 0) x |= C; is transformed to nothing. */
2862 if (GET_CODE (a) == IOR)
2863 result = (code == NE) ? a : NULL_RTX;
2864 else if (code == NE)
2866 /* if ((x & C) == 0) x ^= C; is transformed to x |= C. */
2867 result = gen_int_mode (HOST_WIDE_INT_1 << bitnum, mode);
2868 result = simplify_gen_binary (IOR, mode, x, result);
2870 else
2872 /* if ((x & C) != 0) x ^= C; is transformed to x &= ~C. */
2873 result = gen_int_mode (~(HOST_WIDE_INT_1 << bitnum), mode);
2874 result = simplify_gen_binary (AND, mode, x, result);
2877 else if (GET_CODE (a) == AND)
2879 /* Check for "if (X & C) x &= ~C". */
2880 if (! rtx_equal_p (x, XEXP (a, 0))
2881 || !CONST_INT_P (XEXP (a, 1))
2882 || (INTVAL (XEXP (a, 1)) & GET_MODE_MASK (mode))
2883 != (~(HOST_WIDE_INT_1 << bitnum) & GET_MODE_MASK (mode)))
2884 return FALSE;
2886 /* if ((x & C) == 0) x &= ~C; is transformed to nothing. */
2887 /* if ((x & C) != 0) x &= ~C; is transformed to x &= ~C. */
2888 result = (code == EQ) ? a : NULL_RTX;
2890 else
2891 return FALSE;
2893 if (result)
2895 start_sequence ();
2896 noce_emit_move_insn (x, result);
2897 seq = end_ifcvt_sequence (if_info);
2898 if (!seq)
2899 return FALSE;
2901 emit_insn_before_setloc (seq, if_info->jump,
2902 INSN_LOCATION (if_info->insn_a));
2904 if_info->transform_name = "noce_try_bitop";
2905 return TRUE;
2909 /* Similar to get_condition, only the resulting condition must be
2910 valid at JUMP, instead of at EARLIEST.
2912 If THEN_ELSE_REVERSED is true, the fallthrough does not go to the
2913 THEN block of the caller, and we have to reverse the condition. */
2915 static rtx
2916 noce_get_condition (rtx_insn *jump, rtx_insn **earliest, bool then_else_reversed)
2918 rtx cond, set, tmp;
2919 bool reverse;
2921 if (! any_condjump_p (jump))
2922 return NULL_RTX;
2924 set = pc_set (jump);
2926 /* If this branches to JUMP_LABEL when the condition is false,
2927 reverse the condition. */
2928 reverse = (GET_CODE (XEXP (SET_SRC (set), 2)) == LABEL_REF
2929 && label_ref_label (XEXP (SET_SRC (set), 2)) == JUMP_LABEL (jump));
2931 /* We may have to reverse because the caller's if block is not canonical,
2932 i.e. the THEN block isn't the fallthrough block for the TEST block
2933 (see find_if_header). */
2934 if (then_else_reversed)
2935 reverse = !reverse;
2937 /* If the condition variable is a register and is MODE_INT, accept it. */
2939 cond = XEXP (SET_SRC (set), 0);
2940 tmp = XEXP (cond, 0);
2941 if (REG_P (tmp) && GET_MODE_CLASS (GET_MODE (tmp)) == MODE_INT
2942 && (GET_MODE (tmp) != BImode
2943 || !targetm.small_register_classes_for_mode_p (BImode)))
2945 *earliest = jump;
2947 if (reverse)
2948 cond = gen_rtx_fmt_ee (reverse_condition (GET_CODE (cond)),
2949 GET_MODE (cond), tmp, XEXP (cond, 1));
2950 return cond;
2953 /* Otherwise, fall back on canonicalize_condition to do the dirty
2954 work of manipulating MODE_CC values and COMPARE rtx codes. */
2955 tmp = canonicalize_condition (jump, cond, reverse, earliest,
2956 NULL_RTX, have_cbranchcc4, true);
2958 /* We don't handle side-effects in the condition, like handling
2959 REG_INC notes and making sure no duplicate conditions are emitted. */
2960 if (tmp != NULL_RTX && side_effects_p (tmp))
2961 return NULL_RTX;
2963 return tmp;
2966 /* Return true if OP is ok for if-then-else processing. */
2968 static int
2969 noce_operand_ok (const_rtx op)
2971 if (side_effects_p (op))
2972 return FALSE;
2974 /* We special-case memories, so handle any of them with
2975 no address side effects. */
2976 if (MEM_P (op))
2977 return ! side_effects_p (XEXP (op, 0));
2979 return ! may_trap_p (op);
2982 /* Return true iff basic block TEST_BB is valid for noce if-conversion.
2983 The condition used in this if-conversion is in COND.
2984 In practice, check that TEST_BB ends with a single set
2985 x := a and all previous computations
2986 in TEST_BB don't produce any values that are live after TEST_BB.
2987 In other words, all the insns in TEST_BB are there only
2988 to compute a value for x. Add the rtx cost of the insns
2989 in TEST_BB to COST. Record whether TEST_BB is a single simple
2990 set instruction in SIMPLE_P. */
2992 static bool
2993 bb_valid_for_noce_process_p (basic_block test_bb, rtx cond,
2994 unsigned int *cost, bool *simple_p)
2996 if (!test_bb)
2997 return false;
2999 rtx_insn *last_insn = last_active_insn (test_bb, FALSE);
3000 rtx last_set = NULL_RTX;
3002 rtx cc = cc_in_cond (cond);
3004 if (!insn_valid_noce_process_p (last_insn, cc))
3005 return false;
3006 last_set = single_set (last_insn);
3008 rtx x = SET_DEST (last_set);
3009 rtx_insn *first_insn = first_active_insn (test_bb);
3010 rtx first_set = single_set (first_insn);
3012 if (!first_set)
3013 return false;
3015 /* We have a single simple set, that's okay. */
3016 bool speed_p = optimize_bb_for_speed_p (test_bb);
3018 if (first_insn == last_insn)
3020 *simple_p = noce_operand_ok (SET_DEST (first_set));
3021 *cost += insn_rtx_cost (first_set, speed_p);
3022 return *simple_p;
3025 rtx_insn *prev_last_insn = PREV_INSN (last_insn);
3026 gcc_assert (prev_last_insn);
3028 /* For now, disallow setting x multiple times in test_bb. */
3029 if (REG_P (x) && reg_set_between_p (x, first_insn, prev_last_insn))
3030 return false;
3032 bitmap test_bb_temps = BITMAP_ALLOC (&reg_obstack);
3034 /* The regs that are live out of test_bb. */
3035 bitmap test_bb_live_out = df_get_live_out (test_bb);
3037 int potential_cost = insn_rtx_cost (last_set, speed_p);
3038 rtx_insn *insn;
3039 FOR_BB_INSNS (test_bb, insn)
3041 if (insn != last_insn)
3043 if (!active_insn_p (insn))
3044 continue;
3046 if (!insn_valid_noce_process_p (insn, cc))
3047 goto free_bitmap_and_fail;
3049 rtx sset = single_set (insn);
3050 gcc_assert (sset);
3052 if (contains_mem_rtx_p (SET_SRC (sset))
3053 || !REG_P (SET_DEST (sset))
3054 || reg_overlap_mentioned_p (SET_DEST (sset), cond))
3055 goto free_bitmap_and_fail;
3057 potential_cost += insn_rtx_cost (sset, speed_p);
3058 bitmap_set_bit (test_bb_temps, REGNO (SET_DEST (sset)));
3062 /* If any of the intermediate results in test_bb are live after test_bb
3063 then fail. */
3064 if (bitmap_intersect_p (test_bb_live_out, test_bb_temps))
3065 goto free_bitmap_and_fail;
3067 BITMAP_FREE (test_bb_temps);
3068 *cost += potential_cost;
3069 *simple_p = false;
3070 return true;
3072 free_bitmap_and_fail:
3073 BITMAP_FREE (test_bb_temps);
3074 return false;
3077 /* We have something like:
3079 if (x > y)
3080 { i = a; j = b; k = c; }
3082 Make it:
3084 tmp_i = (x > y) ? a : i;
3085 tmp_j = (x > y) ? b : j;
3086 tmp_k = (x > y) ? c : k;
3087 i = tmp_i;
3088 j = tmp_j;
3089 k = tmp_k;
3091 Subsequent passes are expected to clean up the extra moves.
3093 Look for special cases such as writes to one register which are
3094 read back in another SET, as might occur in a swap idiom or
3095 similar.
3097 These look like:
3099 if (x > y)
3100 i = a;
3101 j = i;
3103 Which we want to rewrite to:
3105 tmp_i = (x > y) ? a : i;
3106 tmp_j = (x > y) ? tmp_i : j;
3107 i = tmp_i;
3108 j = tmp_j;
3110 We can catch these when looking at (SET x y) by keeping a list of the
3111 registers we would have targeted before if-conversion and looking back
3112 through it for an overlap with Y. If we find one, we rewire the
3113 conditional set to use the temporary we introduced earlier.
3115 IF_INFO contains the useful information about the block structure and
3116 jump instructions. */
3118 static int
3119 noce_convert_multiple_sets (struct noce_if_info *if_info)
3121 basic_block test_bb = if_info->test_bb;
3122 basic_block then_bb = if_info->then_bb;
3123 basic_block join_bb = if_info->join_bb;
3124 rtx_insn *jump = if_info->jump;
3125 rtx_insn *cond_earliest;
3126 rtx_insn *insn;
3128 start_sequence ();
3130 /* Decompose the condition attached to the jump. */
3131 rtx cond = noce_get_condition (jump, &cond_earliest, false);
3132 rtx x = XEXP (cond, 0);
3133 rtx y = XEXP (cond, 1);
3134 rtx_code cond_code = GET_CODE (cond);
3136 /* The true targets for a conditional move. */
3137 auto_vec<rtx> targets;
3138 /* The temporaries introduced to allow us to not consider register
3139 overlap. */
3140 auto_vec<rtx> temporaries;
3141 /* The insns we've emitted. */
3142 auto_vec<rtx_insn *> unmodified_insns;
3143 int count = 0;
3145 FOR_BB_INSNS (then_bb, insn)
3147 /* Skip over non-insns. */
3148 if (!active_insn_p (insn))
3149 continue;
3151 rtx set = single_set (insn);
3152 gcc_checking_assert (set);
3154 rtx target = SET_DEST (set);
3155 rtx temp = gen_reg_rtx (GET_MODE (target));
3156 rtx new_val = SET_SRC (set);
3157 rtx old_val = target;
3159 /* If we were supposed to read from an earlier write in this block,
3160 we've changed the register allocation. Rewire the read. While
3161 we are looking, also try to catch a swap idiom. */
3162 for (int i = count - 1; i >= 0; --i)
3163 if (reg_overlap_mentioned_p (new_val, targets[i]))
3165 /* Catch a "swap" style idiom. */
3166 if (find_reg_note (insn, REG_DEAD, new_val) != NULL_RTX)
3167 /* The write to targets[i] is only live until the read
3168 here. As the condition codes match, we can propagate
3169 the set to here. */
3170 new_val = SET_SRC (single_set (unmodified_insns[i]));
3171 else
3172 new_val = temporaries[i];
3173 break;
3176 /* If we had a non-canonical conditional jump (i.e. one where
3177 the fallthrough is to the "else" case) we need to reverse
3178 the conditional select. */
3179 if (if_info->then_else_reversed)
3180 std::swap (old_val, new_val);
3183 /* We allow simple lowpart register subreg SET sources in
3184 bb_ok_for_noce_convert_multiple_sets. Be careful when processing
3185 sequences like:
3186 (set (reg:SI r1) (reg:SI r2))
3187 (set (reg:HI r3) (subreg:HI (r1)))
3188 For the second insn new_val or old_val (r1 in this example) will be
3189 taken from the temporaries and have the wider mode which will not
3190 match with the mode of the other source of the conditional move, so
3191 we'll end up trying to emit r4:HI = cond ? (r1:SI) : (r3:HI).
3192 Wrap the two cmove operands into subregs if appropriate to prevent
3193 that. */
3194 if (GET_MODE (new_val) != GET_MODE (temp))
3196 machine_mode src_mode = GET_MODE (new_val);
3197 machine_mode dst_mode = GET_MODE (temp);
3198 if (GET_MODE_SIZE (src_mode) <= GET_MODE_SIZE (dst_mode))
3200 end_sequence ();
3201 return FALSE;
3203 new_val = lowpart_subreg (dst_mode, new_val, src_mode);
3205 if (GET_MODE (old_val) != GET_MODE (temp))
3207 machine_mode src_mode = GET_MODE (old_val);
3208 machine_mode dst_mode = GET_MODE (temp);
3209 if (GET_MODE_SIZE (src_mode) <= GET_MODE_SIZE (dst_mode))
3211 end_sequence ();
3212 return FALSE;
3214 old_val = lowpart_subreg (dst_mode, old_val, src_mode);
3217 /* Actually emit the conditional move. */
3218 rtx temp_dest = noce_emit_cmove (if_info, temp, cond_code,
3219 x, y, new_val, old_val);
3221 /* If we failed to expand the conditional move, drop out and don't
3222 try to continue. */
3223 if (temp_dest == NULL_RTX)
3225 end_sequence ();
3226 return FALSE;
3229 /* Bookkeeping. */
3230 count++;
3231 targets.safe_push (target);
3232 temporaries.safe_push (temp_dest);
3233 unmodified_insns.safe_push (insn);
3236 /* We must have seen some sort of insn to insert, otherwise we were
3237 given an empty BB to convert, and we can't handle that. */
3238 gcc_assert (!unmodified_insns.is_empty ());
3240 /* Now fixup the assignments. */
3241 for (int i = 0; i < count; i++)
3242 noce_emit_move_insn (targets[i], temporaries[i]);
3244 /* Actually emit the sequence if it isn't too expensive. */
3245 rtx_insn *seq = get_insns ();
3247 if (!targetm.noce_conversion_profitable_p (seq, if_info))
3249 end_sequence ();
3250 return FALSE;
3253 for (insn = seq; insn; insn = NEXT_INSN (insn))
3254 set_used_flags (insn);
3256 /* Mark all our temporaries and targets as used. */
3257 for (int i = 0; i < count; i++)
3259 set_used_flags (temporaries[i]);
3260 set_used_flags (targets[i]);
3263 set_used_flags (cond);
3264 set_used_flags (x);
3265 set_used_flags (y);
3267 unshare_all_rtl_in_chain (seq);
3268 end_sequence ();
3270 if (!seq)
3271 return FALSE;
3273 for (insn = seq; insn; insn = NEXT_INSN (insn))
3274 if (JUMP_P (insn)
3275 || recog_memoized (insn) == -1)
3276 return FALSE;
3278 emit_insn_before_setloc (seq, if_info->jump,
3279 INSN_LOCATION (unmodified_insns.last ()));
3281 /* Clean up THEN_BB and the edges in and out of it. */
3282 remove_edge (find_edge (test_bb, join_bb));
3283 remove_edge (find_edge (then_bb, join_bb));
3284 redirect_edge_and_branch_force (single_succ_edge (test_bb), join_bb);
3285 delete_basic_block (then_bb);
3286 num_true_changes++;
3288 /* Maybe merge blocks now the jump is simple enough. */
3289 if (can_merge_blocks_p (test_bb, join_bb))
3291 merge_blocks (test_bb, join_bb);
3292 num_true_changes++;
3295 num_updated_if_blocks++;
3296 if_info->transform_name = "noce_convert_multiple_sets";
3297 return TRUE;
3300 /* Return true iff basic block TEST_BB is comprised of only
3301 (SET (REG) (REG)) insns suitable for conversion to a series
3302 of conditional moves. Also check that we have more than one set
3303 (other routines can handle a single set better than we would), and
3304 fewer than PARAM_MAX_RTL_IF_CONVERSION_INSNS sets. */
3306 static bool
3307 bb_ok_for_noce_convert_multiple_sets (basic_block test_bb)
3309 rtx_insn *insn;
3310 unsigned count = 0;
3311 unsigned param = PARAM_VALUE (PARAM_MAX_RTL_IF_CONVERSION_INSNS);
3313 FOR_BB_INSNS (test_bb, insn)
3315 /* Skip over notes etc. */
3316 if (!active_insn_p (insn))
3317 continue;
3319 /* We only handle SET insns. */
3320 rtx set = single_set (insn);
3321 if (set == NULL_RTX)
3322 return false;
3324 rtx dest = SET_DEST (set);
3325 rtx src = SET_SRC (set);
3327 /* We can possibly relax this, but for now only handle REG to REG
3328 (including subreg) moves. This avoids any issues that might come
3329 from introducing loads/stores that might violate data-race-freedom
3330 guarantees. */
3331 if (!REG_P (dest))
3332 return false;
3334 if (!(REG_P (src)
3335 || (GET_CODE (src) == SUBREG && REG_P (SUBREG_REG (src))
3336 && subreg_lowpart_p (src))))
3337 return false;
3339 /* Destination must be appropriate for a conditional write. */
3340 if (!noce_operand_ok (dest))
3341 return false;
3343 /* We must be able to conditionally move in this mode. */
3344 if (!can_conditionally_move_p (GET_MODE (dest)))
3345 return false;
3347 count++;
3350 /* If we would only put out one conditional move, the other strategies
3351 this pass tries are better optimized and will be more appropriate.
3352 Some targets want to strictly limit the number of conditional moves
3353 that are emitted, they set this through PARAM, we need to respect
3354 that. */
3355 return count > 1 && count <= param;
3358 /* Given a simple IF-THEN-JOIN or IF-THEN-ELSE-JOIN block, attempt to convert
3359 it without using conditional execution. Return TRUE if we were successful
3360 at converting the block. */
3362 static int
3363 noce_process_if_block (struct noce_if_info *if_info)
3365 basic_block test_bb = if_info->test_bb; /* test block */
3366 basic_block then_bb = if_info->then_bb; /* THEN */
3367 basic_block else_bb = if_info->else_bb; /* ELSE or NULL */
3368 basic_block join_bb = if_info->join_bb; /* JOIN */
3369 rtx_insn *jump = if_info->jump;
3370 rtx cond = if_info->cond;
3371 rtx_insn *insn_a, *insn_b;
3372 rtx set_a, set_b;
3373 rtx orig_x, x, a, b;
3375 /* We're looking for patterns of the form
3377 (1) if (...) x = a; else x = b;
3378 (2) x = b; if (...) x = a;
3379 (3) if (...) x = a; // as if with an initial x = x.
3380 (4) if (...) { x = a; y = b; z = c; } // Like 3, for multiple SETS.
3381 The later patterns require jumps to be more expensive.
3382 For the if (...) x = a; else x = b; case we allow multiple insns
3383 inside the then and else blocks as long as their only effect is
3384 to calculate a value for x.
3385 ??? For future expansion, further expand the "multiple X" rules. */
3387 /* First look for multiple SETS. */
3388 if (!else_bb
3389 && HAVE_conditional_move
3390 && !HAVE_cc0
3391 && bb_ok_for_noce_convert_multiple_sets (then_bb))
3393 if (noce_convert_multiple_sets (if_info))
3395 if (dump_file && if_info->transform_name)
3396 fprintf (dump_file, "if-conversion succeeded through %s\n",
3397 if_info->transform_name);
3398 return TRUE;
3402 bool speed_p = optimize_bb_for_speed_p (test_bb);
3403 unsigned int then_cost = 0, else_cost = 0;
3404 if (!bb_valid_for_noce_process_p (then_bb, cond, &then_cost,
3405 &if_info->then_simple))
3406 return false;
3408 if (else_bb
3409 && !bb_valid_for_noce_process_p (else_bb, cond, &else_cost,
3410 &if_info->else_simple))
3411 return false;
3413 if (else_bb == NULL)
3414 if_info->original_cost += then_cost;
3415 else if (speed_p)
3416 if_info->original_cost += MIN (then_cost, else_cost);
3417 else
3418 if_info->original_cost += then_cost + else_cost;
3420 insn_a = last_active_insn (then_bb, FALSE);
3421 set_a = single_set (insn_a);
3422 gcc_assert (set_a);
3424 x = SET_DEST (set_a);
3425 a = SET_SRC (set_a);
3427 /* Look for the other potential set. Make sure we've got equivalent
3428 destinations. */
3429 /* ??? This is overconservative. Storing to two different mems is
3430 as easy as conditionally computing the address. Storing to a
3431 single mem merely requires a scratch memory to use as one of the
3432 destination addresses; often the memory immediately below the
3433 stack pointer is available for this. */
3434 set_b = NULL_RTX;
3435 if (else_bb)
3437 insn_b = last_active_insn (else_bb, FALSE);
3438 set_b = single_set (insn_b);
3439 gcc_assert (set_b);
3441 if (!rtx_interchangeable_p (x, SET_DEST (set_b)))
3442 return FALSE;
3444 else
3446 insn_b = if_info->cond_earliest;
3448 insn_b = prev_nonnote_nondebug_insn (insn_b);
3449 while (insn_b
3450 && (BLOCK_FOR_INSN (insn_b)
3451 == BLOCK_FOR_INSN (if_info->cond_earliest))
3452 && !modified_in_p (x, insn_b));
3454 /* We're going to be moving the evaluation of B down from above
3455 COND_EARLIEST to JUMP. Make sure the relevant data is still
3456 intact. */
3457 if (! insn_b
3458 || BLOCK_FOR_INSN (insn_b) != BLOCK_FOR_INSN (if_info->cond_earliest)
3459 || !NONJUMP_INSN_P (insn_b)
3460 || (set_b = single_set (insn_b)) == NULL_RTX
3461 || ! rtx_interchangeable_p (x, SET_DEST (set_b))
3462 || ! noce_operand_ok (SET_SRC (set_b))
3463 || reg_overlap_mentioned_p (x, SET_SRC (set_b))
3464 || modified_between_p (SET_SRC (set_b), insn_b, jump)
3465 /* Avoid extending the lifetime of hard registers on small
3466 register class machines. */
3467 || (REG_P (SET_SRC (set_b))
3468 && HARD_REGISTER_P (SET_SRC (set_b))
3469 && targetm.small_register_classes_for_mode_p
3470 (GET_MODE (SET_SRC (set_b))))
3471 /* Likewise with X. In particular this can happen when
3472 noce_get_condition looks farther back in the instruction
3473 stream than one might expect. */
3474 || reg_overlap_mentioned_p (x, cond)
3475 || reg_overlap_mentioned_p (x, a)
3476 || modified_between_p (x, insn_b, jump))
3478 insn_b = NULL;
3479 set_b = NULL_RTX;
3483 /* If x has side effects then only the if-then-else form is safe to
3484 convert. But even in that case we would need to restore any notes
3485 (such as REG_INC) at then end. That can be tricky if
3486 noce_emit_move_insn expands to more than one insn, so disable the
3487 optimization entirely for now if there are side effects. */
3488 if (side_effects_p (x))
3489 return FALSE;
3491 b = (set_b ? SET_SRC (set_b) : x);
3493 /* Only operate on register destinations, and even then avoid extending
3494 the lifetime of hard registers on small register class machines. */
3495 orig_x = x;
3496 if_info->orig_x = orig_x;
3497 if (!REG_P (x)
3498 || (HARD_REGISTER_P (x)
3499 && targetm.small_register_classes_for_mode_p (GET_MODE (x))))
3501 if (GET_MODE (x) == BLKmode)
3502 return FALSE;
3504 if (GET_CODE (x) == ZERO_EXTRACT
3505 && (!CONST_INT_P (XEXP (x, 1))
3506 || !CONST_INT_P (XEXP (x, 2))))
3507 return FALSE;
3509 x = gen_reg_rtx (GET_MODE (GET_CODE (x) == STRICT_LOW_PART
3510 ? XEXP (x, 0) : x));
3513 /* Don't operate on sources that may trap or are volatile. */
3514 if (! noce_operand_ok (a) || ! noce_operand_ok (b))
3515 return FALSE;
3517 retry:
3518 /* Set up the info block for our subroutines. */
3519 if_info->insn_a = insn_a;
3520 if_info->insn_b = insn_b;
3521 if_info->x = x;
3522 if_info->a = a;
3523 if_info->b = b;
3525 /* Try optimizations in some approximation of a useful order. */
3526 /* ??? Should first look to see if X is live incoming at all. If it
3527 isn't, we don't need anything but an unconditional set. */
3529 /* Look and see if A and B are really the same. Avoid creating silly
3530 cmove constructs that no one will fix up later. */
3531 if (noce_simple_bbs (if_info)
3532 && rtx_interchangeable_p (a, b))
3534 /* If we have an INSN_B, we don't have to create any new rtl. Just
3535 move the instruction that we already have. If we don't have an
3536 INSN_B, that means that A == X, and we've got a noop move. In
3537 that case don't do anything and let the code below delete INSN_A. */
3538 if (insn_b && else_bb)
3540 rtx note;
3542 if (else_bb && insn_b == BB_END (else_bb))
3543 BB_END (else_bb) = PREV_INSN (insn_b);
3544 reorder_insns (insn_b, insn_b, PREV_INSN (jump));
3546 /* If there was a REG_EQUAL note, delete it since it may have been
3547 true due to this insn being after a jump. */
3548 if ((note = find_reg_note (insn_b, REG_EQUAL, NULL_RTX)) != 0)
3549 remove_note (insn_b, note);
3551 insn_b = NULL;
3553 /* If we have "x = b; if (...) x = a;", and x has side-effects, then
3554 x must be executed twice. */
3555 else if (insn_b && side_effects_p (orig_x))
3556 return FALSE;
3558 x = orig_x;
3559 goto success;
3562 if (!set_b && MEM_P (orig_x))
3563 /* We want to avoid store speculation to avoid cases like
3564 if (pthread_mutex_trylock(mutex))
3565 ++global_variable;
3566 Rather than go to much effort here, we rely on the SSA optimizers,
3567 which do a good enough job these days. */
3568 return FALSE;
3570 if (noce_try_move (if_info))
3571 goto success;
3572 if (noce_try_ifelse_collapse (if_info))
3573 goto success;
3574 if (noce_try_store_flag (if_info))
3575 goto success;
3576 if (noce_try_bitop (if_info))
3577 goto success;
3578 if (noce_try_minmax (if_info))
3579 goto success;
3580 if (noce_try_abs (if_info))
3581 goto success;
3582 if (noce_try_inverse_constants (if_info))
3583 goto success;
3584 if (!targetm.have_conditional_execution ()
3585 && noce_try_store_flag_constants (if_info))
3586 goto success;
3587 if (HAVE_conditional_move
3588 && noce_try_cmove (if_info))
3589 goto success;
3590 if (! targetm.have_conditional_execution ())
3592 if (noce_try_addcc (if_info))
3593 goto success;
3594 if (noce_try_store_flag_mask (if_info))
3595 goto success;
3596 if (HAVE_conditional_move
3597 && noce_try_cmove_arith (if_info))
3598 goto success;
3599 if (noce_try_sign_mask (if_info))
3600 goto success;
3603 if (!else_bb && set_b)
3605 insn_b = NULL;
3606 set_b = NULL_RTX;
3607 b = orig_x;
3608 goto retry;
3611 return FALSE;
3613 success:
3614 if (dump_file && if_info->transform_name)
3615 fprintf (dump_file, "if-conversion succeeded through %s\n",
3616 if_info->transform_name);
3618 /* If we used a temporary, fix it up now. */
3619 if (orig_x != x)
3621 rtx_insn *seq;
3623 start_sequence ();
3624 noce_emit_move_insn (orig_x, x);
3625 seq = get_insns ();
3626 set_used_flags (orig_x);
3627 unshare_all_rtl_in_chain (seq);
3628 end_sequence ();
3630 emit_insn_before_setloc (seq, BB_END (test_bb), INSN_LOCATION (insn_a));
3633 /* The original THEN and ELSE blocks may now be removed. The test block
3634 must now jump to the join block. If the test block and the join block
3635 can be merged, do so. */
3636 if (else_bb)
3638 delete_basic_block (else_bb);
3639 num_true_changes++;
3641 else
3642 remove_edge (find_edge (test_bb, join_bb));
3644 remove_edge (find_edge (then_bb, join_bb));
3645 redirect_edge_and_branch_force (single_succ_edge (test_bb), join_bb);
3646 delete_basic_block (then_bb);
3647 num_true_changes++;
3649 if (can_merge_blocks_p (test_bb, join_bb))
3651 merge_blocks (test_bb, join_bb);
3652 num_true_changes++;
3655 num_updated_if_blocks++;
3656 return TRUE;
3659 /* Check whether a block is suitable for conditional move conversion.
3660 Every insn must be a simple set of a register to a constant or a
3661 register. For each assignment, store the value in the pointer map
3662 VALS, keyed indexed by register pointer, then store the register
3663 pointer in REGS. COND is the condition we will test. */
3665 static int
3666 check_cond_move_block (basic_block bb,
3667 hash_map<rtx, rtx> *vals,
3668 vec<rtx> *regs,
3669 rtx cond)
3671 rtx_insn *insn;
3672 rtx cc = cc_in_cond (cond);
3674 /* We can only handle simple jumps at the end of the basic block.
3675 It is almost impossible to update the CFG otherwise. */
3676 insn = BB_END (bb);
3677 if (JUMP_P (insn) && !onlyjump_p (insn))
3678 return FALSE;
3680 FOR_BB_INSNS (bb, insn)
3682 rtx set, dest, src;
3684 if (!NONDEBUG_INSN_P (insn) || JUMP_P (insn))
3685 continue;
3686 set = single_set (insn);
3687 if (!set)
3688 return FALSE;
3690 dest = SET_DEST (set);
3691 src = SET_SRC (set);
3692 if (!REG_P (dest)
3693 || (HARD_REGISTER_P (dest)
3694 && targetm.small_register_classes_for_mode_p (GET_MODE (dest))))
3695 return FALSE;
3697 if (!CONSTANT_P (src) && !register_operand (src, VOIDmode))
3698 return FALSE;
3700 if (side_effects_p (src) || side_effects_p (dest))
3701 return FALSE;
3703 if (may_trap_p (src) || may_trap_p (dest))
3704 return FALSE;
3706 /* Don't try to handle this if the source register was
3707 modified earlier in the block. */
3708 if ((REG_P (src)
3709 && vals->get (src))
3710 || (GET_CODE (src) == SUBREG && REG_P (SUBREG_REG (src))
3711 && vals->get (SUBREG_REG (src))))
3712 return FALSE;
3714 /* Don't try to handle this if the destination register was
3715 modified earlier in the block. */
3716 if (vals->get (dest))
3717 return FALSE;
3719 /* Don't try to handle this if the condition uses the
3720 destination register. */
3721 if (reg_overlap_mentioned_p (dest, cond))
3722 return FALSE;
3724 /* Don't try to handle this if the source register is modified
3725 later in the block. */
3726 if (!CONSTANT_P (src)
3727 && modified_between_p (src, insn, NEXT_INSN (BB_END (bb))))
3728 return FALSE;
3730 /* Skip it if the instruction to be moved might clobber CC. */
3731 if (cc && set_of (cc, insn))
3732 return FALSE;
3734 vals->put (dest, src);
3736 regs->safe_push (dest);
3739 return TRUE;
3742 /* Given a basic block BB suitable for conditional move conversion,
3743 a condition COND, and pointer maps THEN_VALS and ELSE_VALS containing
3744 the register values depending on COND, emit the insns in the block as
3745 conditional moves. If ELSE_BLOCK is true, THEN_BB was already
3746 processed. The caller has started a sequence for the conversion.
3747 Return true if successful, false if something goes wrong. */
3749 static bool
3750 cond_move_convert_if_block (struct noce_if_info *if_infop,
3751 basic_block bb, rtx cond,
3752 hash_map<rtx, rtx> *then_vals,
3753 hash_map<rtx, rtx> *else_vals,
3754 bool else_block_p)
3756 enum rtx_code code;
3757 rtx_insn *insn;
3758 rtx cond_arg0, cond_arg1;
3760 code = GET_CODE (cond);
3761 cond_arg0 = XEXP (cond, 0);
3762 cond_arg1 = XEXP (cond, 1);
3764 FOR_BB_INSNS (bb, insn)
3766 rtx set, target, dest, t, e;
3768 /* ??? Maybe emit conditional debug insn? */
3769 if (!NONDEBUG_INSN_P (insn) || JUMP_P (insn))
3770 continue;
3771 set = single_set (insn);
3772 gcc_assert (set && REG_P (SET_DEST (set)));
3774 dest = SET_DEST (set);
3776 rtx *then_slot = then_vals->get (dest);
3777 rtx *else_slot = else_vals->get (dest);
3778 t = then_slot ? *then_slot : NULL_RTX;
3779 e = else_slot ? *else_slot : NULL_RTX;
3781 if (else_block_p)
3783 /* If this register was set in the then block, we already
3784 handled this case there. */
3785 if (t)
3786 continue;
3787 t = dest;
3788 gcc_assert (e);
3790 else
3792 gcc_assert (t);
3793 if (!e)
3794 e = dest;
3797 target = noce_emit_cmove (if_infop, dest, code, cond_arg0, cond_arg1,
3798 t, e);
3799 if (!target)
3800 return false;
3802 if (target != dest)
3803 noce_emit_move_insn (dest, target);
3806 return true;
3809 /* Given a simple IF-THEN-JOIN or IF-THEN-ELSE-JOIN block, attempt to convert
3810 it using only conditional moves. Return TRUE if we were successful at
3811 converting the block. */
3813 static int
3814 cond_move_process_if_block (struct noce_if_info *if_info)
3816 basic_block test_bb = if_info->test_bb;
3817 basic_block then_bb = if_info->then_bb;
3818 basic_block else_bb = if_info->else_bb;
3819 basic_block join_bb = if_info->join_bb;
3820 rtx_insn *jump = if_info->jump;
3821 rtx cond = if_info->cond;
3822 rtx_insn *seq, *loc_insn;
3823 rtx reg;
3824 int c;
3825 vec<rtx> then_regs = vNULL;
3826 vec<rtx> else_regs = vNULL;
3827 unsigned int i;
3828 int success_p = FALSE;
3829 int limit = PARAM_VALUE (PARAM_MAX_RTL_IF_CONVERSION_INSNS);
3831 /* Build a mapping for each block to the value used for each
3832 register. */
3833 hash_map<rtx, rtx> then_vals;
3834 hash_map<rtx, rtx> else_vals;
3836 /* Make sure the blocks are suitable. */
3837 if (!check_cond_move_block (then_bb, &then_vals, &then_regs, cond)
3838 || (else_bb
3839 && !check_cond_move_block (else_bb, &else_vals, &else_regs, cond)))
3840 goto done;
3842 /* Make sure the blocks can be used together. If the same register
3843 is set in both blocks, and is not set to a constant in both
3844 cases, then both blocks must set it to the same register. We
3845 have already verified that if it is set to a register, that the
3846 source register does not change after the assignment. Also count
3847 the number of registers set in only one of the blocks. */
3848 c = 0;
3849 FOR_EACH_VEC_ELT (then_regs, i, reg)
3851 rtx *then_slot = then_vals.get (reg);
3852 rtx *else_slot = else_vals.get (reg);
3854 gcc_checking_assert (then_slot);
3855 if (!else_slot)
3856 ++c;
3857 else
3859 rtx then_val = *then_slot;
3860 rtx else_val = *else_slot;
3861 if (!CONSTANT_P (then_val) && !CONSTANT_P (else_val)
3862 && !rtx_equal_p (then_val, else_val))
3863 goto done;
3867 /* Finish off c for MAX_CONDITIONAL_EXECUTE. */
3868 FOR_EACH_VEC_ELT (else_regs, i, reg)
3870 gcc_checking_assert (else_vals.get (reg));
3871 if (!then_vals.get (reg))
3872 ++c;
3875 /* Make sure it is reasonable to convert this block. What matters
3876 is the number of assignments currently made in only one of the
3877 branches, since if we convert we are going to always execute
3878 them. */
3879 if (c > MAX_CONDITIONAL_EXECUTE
3880 || c > limit)
3881 goto done;
3883 /* Try to emit the conditional moves. First do the then block,
3884 then do anything left in the else blocks. */
3885 start_sequence ();
3886 if (!cond_move_convert_if_block (if_info, then_bb, cond,
3887 &then_vals, &else_vals, false)
3888 || (else_bb
3889 && !cond_move_convert_if_block (if_info, else_bb, cond,
3890 &then_vals, &else_vals, true)))
3892 end_sequence ();
3893 goto done;
3895 seq = end_ifcvt_sequence (if_info);
3896 if (!seq)
3897 goto done;
3899 loc_insn = first_active_insn (then_bb);
3900 if (!loc_insn)
3902 loc_insn = first_active_insn (else_bb);
3903 gcc_assert (loc_insn);
3905 emit_insn_before_setloc (seq, jump, INSN_LOCATION (loc_insn));
3907 if (else_bb)
3909 delete_basic_block (else_bb);
3910 num_true_changes++;
3912 else
3913 remove_edge (find_edge (test_bb, join_bb));
3915 remove_edge (find_edge (then_bb, join_bb));
3916 redirect_edge_and_branch_force (single_succ_edge (test_bb), join_bb);
3917 delete_basic_block (then_bb);
3918 num_true_changes++;
3920 if (can_merge_blocks_p (test_bb, join_bb))
3922 merge_blocks (test_bb, join_bb);
3923 num_true_changes++;
3926 num_updated_if_blocks++;
3927 success_p = TRUE;
3929 done:
3930 then_regs.release ();
3931 else_regs.release ();
3932 return success_p;
3936 /* Determine if a given basic block heads a simple IF-THEN-JOIN or an
3937 IF-THEN-ELSE-JOIN block.
3939 If so, we'll try to convert the insns to not require the branch,
3940 using only transformations that do not require conditional execution.
3942 Return TRUE if we were successful at converting the block. */
3944 static int
3945 noce_find_if_block (basic_block test_bb, edge then_edge, edge else_edge,
3946 int pass)
3948 basic_block then_bb, else_bb, join_bb;
3949 bool then_else_reversed = false;
3950 rtx_insn *jump;
3951 rtx cond;
3952 rtx_insn *cond_earliest;
3953 struct noce_if_info if_info;
3954 bool speed_p = optimize_bb_for_speed_p (test_bb);
3956 /* We only ever should get here before reload. */
3957 gcc_assert (!reload_completed);
3959 /* Recognize an IF-THEN-ELSE-JOIN block. */
3960 if (single_pred_p (then_edge->dest)
3961 && single_succ_p (then_edge->dest)
3962 && single_pred_p (else_edge->dest)
3963 && single_succ_p (else_edge->dest)
3964 && single_succ (then_edge->dest) == single_succ (else_edge->dest))
3966 then_bb = then_edge->dest;
3967 else_bb = else_edge->dest;
3968 join_bb = single_succ (then_bb);
3970 /* Recognize an IF-THEN-JOIN block. */
3971 else if (single_pred_p (then_edge->dest)
3972 && single_succ_p (then_edge->dest)
3973 && single_succ (then_edge->dest) == else_edge->dest)
3975 then_bb = then_edge->dest;
3976 else_bb = NULL_BLOCK;
3977 join_bb = else_edge->dest;
3979 /* Recognize an IF-ELSE-JOIN block. We can have those because the order
3980 of basic blocks in cfglayout mode does not matter, so the fallthrough
3981 edge can go to any basic block (and not just to bb->next_bb, like in
3982 cfgrtl mode). */
3983 else if (single_pred_p (else_edge->dest)
3984 && single_succ_p (else_edge->dest)
3985 && single_succ (else_edge->dest) == then_edge->dest)
3987 /* The noce transformations do not apply to IF-ELSE-JOIN blocks.
3988 To make this work, we have to invert the THEN and ELSE blocks
3989 and reverse the jump condition. */
3990 then_bb = else_edge->dest;
3991 else_bb = NULL_BLOCK;
3992 join_bb = single_succ (then_bb);
3993 then_else_reversed = true;
3995 else
3996 /* Not a form we can handle. */
3997 return FALSE;
3999 /* The edges of the THEN and ELSE blocks cannot have complex edges. */
4000 if (single_succ_edge (then_bb)->flags & EDGE_COMPLEX)
4001 return FALSE;
4002 if (else_bb
4003 && single_succ_edge (else_bb)->flags & EDGE_COMPLEX)
4004 return FALSE;
4006 num_possible_if_blocks++;
4008 if (dump_file)
4010 fprintf (dump_file,
4011 "\nIF-THEN%s-JOIN block found, pass %d, test %d, then %d",
4012 (else_bb) ? "-ELSE" : "",
4013 pass, test_bb->index, then_bb->index);
4015 if (else_bb)
4016 fprintf (dump_file, ", else %d", else_bb->index);
4018 fprintf (dump_file, ", join %d\n", join_bb->index);
4021 /* If the conditional jump is more than just a conditional
4022 jump, then we can not do if-conversion on this block. */
4023 jump = BB_END (test_bb);
4024 if (! onlyjump_p (jump))
4025 return FALSE;
4027 /* If this is not a standard conditional jump, we can't parse it. */
4028 cond = noce_get_condition (jump, &cond_earliest, then_else_reversed);
4029 if (!cond)
4030 return FALSE;
4032 /* We must be comparing objects whose modes imply the size. */
4033 if (GET_MODE (XEXP (cond, 0)) == BLKmode)
4034 return FALSE;
4036 /* Initialize an IF_INFO struct to pass around. */
4037 memset (&if_info, 0, sizeof if_info);
4038 if_info.test_bb = test_bb;
4039 if_info.then_bb = then_bb;
4040 if_info.else_bb = else_bb;
4041 if_info.join_bb = join_bb;
4042 if_info.cond = cond;
4043 rtx_insn *rev_cond_earliest;
4044 if_info.rev_cond = noce_get_condition (jump, &rev_cond_earliest,
4045 !then_else_reversed);
4046 gcc_assert (if_info.rev_cond == NULL_RTX
4047 || rev_cond_earliest == cond_earliest);
4048 if_info.cond_earliest = cond_earliest;
4049 if_info.jump = jump;
4050 if_info.then_else_reversed = then_else_reversed;
4051 if_info.speed_p = speed_p;
4052 if_info.max_seq_cost
4053 = targetm.max_noce_ifcvt_seq_cost (then_edge);
4054 /* We'll add in the cost of THEN_BB and ELSE_BB later, when we check
4055 that they are valid to transform. We can't easily get back to the insn
4056 for COND (and it may not exist if we had to canonicalize to get COND),
4057 and jump_insns are always given a cost of 1 by seq_cost, so treat
4058 both instructions as having cost COSTS_N_INSNS (1). */
4059 if_info.original_cost = COSTS_N_INSNS (2);
4062 /* Do the real work. */
4064 if (noce_process_if_block (&if_info))
4065 return TRUE;
4067 if (HAVE_conditional_move
4068 && cond_move_process_if_block (&if_info))
4069 return TRUE;
4071 return FALSE;
4075 /* Merge the blocks and mark for local life update. */
4077 static void
4078 merge_if_block (struct ce_if_block * ce_info)
4080 basic_block test_bb = ce_info->test_bb; /* last test block */
4081 basic_block then_bb = ce_info->then_bb; /* THEN */
4082 basic_block else_bb = ce_info->else_bb; /* ELSE or NULL */
4083 basic_block join_bb = ce_info->join_bb; /* join block */
4084 basic_block combo_bb;
4086 /* All block merging is done into the lower block numbers. */
4088 combo_bb = test_bb;
4089 df_set_bb_dirty (test_bb);
4091 /* Merge any basic blocks to handle && and || subtests. Each of
4092 the blocks are on the fallthru path from the predecessor block. */
4093 if (ce_info->num_multiple_test_blocks > 0)
4095 basic_block bb = test_bb;
4096 basic_block last_test_bb = ce_info->last_test_bb;
4097 basic_block fallthru = block_fallthru (bb);
4101 bb = fallthru;
4102 fallthru = block_fallthru (bb);
4103 merge_blocks (combo_bb, bb);
4104 num_true_changes++;
4106 while (bb != last_test_bb);
4109 /* Merge TEST block into THEN block. Normally the THEN block won't have a
4110 label, but it might if there were || tests. That label's count should be
4111 zero, and it normally should be removed. */
4113 if (then_bb)
4115 /* If THEN_BB has no successors, then there's a BARRIER after it.
4116 If COMBO_BB has more than one successor (THEN_BB), then that BARRIER
4117 is no longer needed, and in fact it is incorrect to leave it in
4118 the insn stream. */
4119 if (EDGE_COUNT (then_bb->succs) == 0
4120 && EDGE_COUNT (combo_bb->succs) > 1)
4122 rtx_insn *end = NEXT_INSN (BB_END (then_bb));
4123 while (end && NOTE_P (end) && !NOTE_INSN_BASIC_BLOCK_P (end))
4124 end = NEXT_INSN (end);
4126 if (end && BARRIER_P (end))
4127 delete_insn (end);
4129 merge_blocks (combo_bb, then_bb);
4130 num_true_changes++;
4133 /* The ELSE block, if it existed, had a label. That label count
4134 will almost always be zero, but odd things can happen when labels
4135 get their addresses taken. */
4136 if (else_bb)
4138 /* If ELSE_BB has no successors, then there's a BARRIER after it.
4139 If COMBO_BB has more than one successor (ELSE_BB), then that BARRIER
4140 is no longer needed, and in fact it is incorrect to leave it in
4141 the insn stream. */
4142 if (EDGE_COUNT (else_bb->succs) == 0
4143 && EDGE_COUNT (combo_bb->succs) > 1)
4145 rtx_insn *end = NEXT_INSN (BB_END (else_bb));
4146 while (end && NOTE_P (end) && !NOTE_INSN_BASIC_BLOCK_P (end))
4147 end = NEXT_INSN (end);
4149 if (end && BARRIER_P (end))
4150 delete_insn (end);
4152 merge_blocks (combo_bb, else_bb);
4153 num_true_changes++;
4156 /* If there was no join block reported, that means it was not adjacent
4157 to the others, and so we cannot merge them. */
4159 if (! join_bb)
4161 rtx_insn *last = BB_END (combo_bb);
4163 /* The outgoing edge for the current COMBO block should already
4164 be correct. Verify this. */
4165 if (EDGE_COUNT (combo_bb->succs) == 0)
4166 gcc_assert (find_reg_note (last, REG_NORETURN, NULL)
4167 || (NONJUMP_INSN_P (last)
4168 && GET_CODE (PATTERN (last)) == TRAP_IF
4169 && (TRAP_CONDITION (PATTERN (last))
4170 == const_true_rtx)));
4172 else
4173 /* There should still be something at the end of the THEN or ELSE
4174 blocks taking us to our final destination. */
4175 gcc_assert (JUMP_P (last)
4176 || (EDGE_SUCC (combo_bb, 0)->dest
4177 == EXIT_BLOCK_PTR_FOR_FN (cfun)
4178 && CALL_P (last)
4179 && SIBLING_CALL_P (last))
4180 || ((EDGE_SUCC (combo_bb, 0)->flags & EDGE_EH)
4181 && can_throw_internal (last)));
4184 /* The JOIN block may have had quite a number of other predecessors too.
4185 Since we've already merged the TEST, THEN and ELSE blocks, we should
4186 have only one remaining edge from our if-then-else diamond. If there
4187 is more than one remaining edge, it must come from elsewhere. There
4188 may be zero incoming edges if the THEN block didn't actually join
4189 back up (as with a call to a non-return function). */
4190 else if (EDGE_COUNT (join_bb->preds) < 2
4191 && join_bb != EXIT_BLOCK_PTR_FOR_FN (cfun))
4193 /* We can merge the JOIN cleanly and update the dataflow try
4194 again on this pass.*/
4195 merge_blocks (combo_bb, join_bb);
4196 num_true_changes++;
4198 else
4200 /* We cannot merge the JOIN. */
4202 /* The outgoing edge for the current COMBO block should already
4203 be correct. Verify this. */
4204 gcc_assert (single_succ_p (combo_bb)
4205 && single_succ (combo_bb) == join_bb);
4207 /* Remove the jump and cruft from the end of the COMBO block. */
4208 if (join_bb != EXIT_BLOCK_PTR_FOR_FN (cfun))
4209 tidy_fallthru_edge (single_succ_edge (combo_bb));
4212 num_updated_if_blocks++;
4215 /* Find a block ending in a simple IF condition and try to transform it
4216 in some way. When converting a multi-block condition, put the new code
4217 in the first such block and delete the rest. Return a pointer to this
4218 first block if some transformation was done. Return NULL otherwise. */
4220 static basic_block
4221 find_if_header (basic_block test_bb, int pass)
4223 ce_if_block ce_info;
4224 edge then_edge;
4225 edge else_edge;
4227 /* The kind of block we're looking for has exactly two successors. */
4228 if (EDGE_COUNT (test_bb->succs) != 2)
4229 return NULL;
4231 then_edge = EDGE_SUCC (test_bb, 0);
4232 else_edge = EDGE_SUCC (test_bb, 1);
4234 if (df_get_bb_dirty (then_edge->dest))
4235 return NULL;
4236 if (df_get_bb_dirty (else_edge->dest))
4237 return NULL;
4239 /* Neither edge should be abnormal. */
4240 if ((then_edge->flags & EDGE_COMPLEX)
4241 || (else_edge->flags & EDGE_COMPLEX))
4242 return NULL;
4244 /* Nor exit the loop. */
4245 if ((then_edge->flags & EDGE_LOOP_EXIT)
4246 || (else_edge->flags & EDGE_LOOP_EXIT))
4247 return NULL;
4249 /* The THEN edge is canonically the one that falls through. */
4250 if (then_edge->flags & EDGE_FALLTHRU)
4252 else if (else_edge->flags & EDGE_FALLTHRU)
4253 std::swap (then_edge, else_edge);
4254 else
4255 /* Otherwise this must be a multiway branch of some sort. */
4256 return NULL;
4258 memset (&ce_info, 0, sizeof (ce_info));
4259 ce_info.test_bb = test_bb;
4260 ce_info.then_bb = then_edge->dest;
4261 ce_info.else_bb = else_edge->dest;
4262 ce_info.pass = pass;
4264 #ifdef IFCVT_MACHDEP_INIT
4265 IFCVT_MACHDEP_INIT (&ce_info);
4266 #endif
4268 if (!reload_completed
4269 && noce_find_if_block (test_bb, then_edge, else_edge, pass))
4270 goto success;
4272 if (reload_completed
4273 && targetm.have_conditional_execution ()
4274 && cond_exec_find_if_block (&ce_info))
4275 goto success;
4277 if (targetm.have_trap ()
4278 && optab_handler (ctrap_optab, word_mode) != CODE_FOR_nothing
4279 && find_cond_trap (test_bb, then_edge, else_edge))
4280 goto success;
4282 if (dom_info_state (CDI_POST_DOMINATORS) >= DOM_NO_FAST_QUERY
4283 && (reload_completed || !targetm.have_conditional_execution ()))
4285 if (find_if_case_1 (test_bb, then_edge, else_edge))
4286 goto success;
4287 if (find_if_case_2 (test_bb, then_edge, else_edge))
4288 goto success;
4291 return NULL;
4293 success:
4294 if (dump_file)
4295 fprintf (dump_file, "Conversion succeeded on pass %d.\n", pass);
4296 /* Set this so we continue looking. */
4297 cond_exec_changed_p = TRUE;
4298 return ce_info.test_bb;
4301 /* Return true if a block has two edges, one of which falls through to the next
4302 block, and the other jumps to a specific block, so that we can tell if the
4303 block is part of an && test or an || test. Returns either -1 or the number
4304 of non-note, non-jump, non-USE/CLOBBER insns in the block. */
4306 static int
4307 block_jumps_and_fallthru_p (basic_block cur_bb, basic_block target_bb)
4309 edge cur_edge;
4310 int fallthru_p = FALSE;
4311 int jump_p = FALSE;
4312 rtx_insn *insn;
4313 rtx_insn *end;
4314 int n_insns = 0;
4315 edge_iterator ei;
4317 if (!cur_bb || !target_bb)
4318 return -1;
4320 /* If no edges, obviously it doesn't jump or fallthru. */
4321 if (EDGE_COUNT (cur_bb->succs) == 0)
4322 return FALSE;
4324 FOR_EACH_EDGE (cur_edge, ei, cur_bb->succs)
4326 if (cur_edge->flags & EDGE_COMPLEX)
4327 /* Anything complex isn't what we want. */
4328 return -1;
4330 else if (cur_edge->flags & EDGE_FALLTHRU)
4331 fallthru_p = TRUE;
4333 else if (cur_edge->dest == target_bb)
4334 jump_p = TRUE;
4336 else
4337 return -1;
4340 if ((jump_p & fallthru_p) == 0)
4341 return -1;
4343 /* Don't allow calls in the block, since this is used to group && and ||
4344 together for conditional execution support. ??? we should support
4345 conditional execution support across calls for IA-64 some day, but
4346 for now it makes the code simpler. */
4347 end = BB_END (cur_bb);
4348 insn = BB_HEAD (cur_bb);
4350 while (insn != NULL_RTX)
4352 if (CALL_P (insn))
4353 return -1;
4355 if (INSN_P (insn)
4356 && !JUMP_P (insn)
4357 && !DEBUG_INSN_P (insn)
4358 && GET_CODE (PATTERN (insn)) != USE
4359 && GET_CODE (PATTERN (insn)) != CLOBBER)
4360 n_insns++;
4362 if (insn == end)
4363 break;
4365 insn = NEXT_INSN (insn);
4368 return n_insns;
4371 /* Determine if a given basic block heads a simple IF-THEN or IF-THEN-ELSE
4372 block. If so, we'll try to convert the insns to not require the branch.
4373 Return TRUE if we were successful at converting the block. */
4375 static int
4376 cond_exec_find_if_block (struct ce_if_block * ce_info)
4378 basic_block test_bb = ce_info->test_bb;
4379 basic_block then_bb = ce_info->then_bb;
4380 basic_block else_bb = ce_info->else_bb;
4381 basic_block join_bb = NULL_BLOCK;
4382 edge cur_edge;
4383 basic_block next;
4384 edge_iterator ei;
4386 ce_info->last_test_bb = test_bb;
4388 /* We only ever should get here after reload,
4389 and if we have conditional execution. */
4390 gcc_assert (reload_completed && targetm.have_conditional_execution ());
4392 /* Discover if any fall through predecessors of the current test basic block
4393 were && tests (which jump to the else block) or || tests (which jump to
4394 the then block). */
4395 if (single_pred_p (test_bb)
4396 && single_pred_edge (test_bb)->flags == EDGE_FALLTHRU)
4398 basic_block bb = single_pred (test_bb);
4399 basic_block target_bb;
4400 int max_insns = MAX_CONDITIONAL_EXECUTE;
4401 int n_insns;
4403 /* Determine if the preceding block is an && or || block. */
4404 if ((n_insns = block_jumps_and_fallthru_p (bb, else_bb)) >= 0)
4406 ce_info->and_and_p = TRUE;
4407 target_bb = else_bb;
4409 else if ((n_insns = block_jumps_and_fallthru_p (bb, then_bb)) >= 0)
4411 ce_info->and_and_p = FALSE;
4412 target_bb = then_bb;
4414 else
4415 target_bb = NULL_BLOCK;
4417 if (target_bb && n_insns <= max_insns)
4419 int total_insns = 0;
4420 int blocks = 0;
4422 ce_info->last_test_bb = test_bb;
4424 /* Found at least one && or || block, look for more. */
4427 ce_info->test_bb = test_bb = bb;
4428 total_insns += n_insns;
4429 blocks++;
4431 if (!single_pred_p (bb))
4432 break;
4434 bb = single_pred (bb);
4435 n_insns = block_jumps_and_fallthru_p (bb, target_bb);
4437 while (n_insns >= 0 && (total_insns + n_insns) <= max_insns);
4439 ce_info->num_multiple_test_blocks = blocks;
4440 ce_info->num_multiple_test_insns = total_insns;
4442 if (ce_info->and_and_p)
4443 ce_info->num_and_and_blocks = blocks;
4444 else
4445 ce_info->num_or_or_blocks = blocks;
4449 /* The THEN block of an IF-THEN combo must have exactly one predecessor,
4450 other than any || blocks which jump to the THEN block. */
4451 if ((EDGE_COUNT (then_bb->preds) - ce_info->num_or_or_blocks) != 1)
4452 return FALSE;
4454 /* The edges of the THEN and ELSE blocks cannot have complex edges. */
4455 FOR_EACH_EDGE (cur_edge, ei, then_bb->preds)
4457 if (cur_edge->flags & EDGE_COMPLEX)
4458 return FALSE;
4461 FOR_EACH_EDGE (cur_edge, ei, else_bb->preds)
4463 if (cur_edge->flags & EDGE_COMPLEX)
4464 return FALSE;
4467 /* The THEN block of an IF-THEN combo must have zero or one successors. */
4468 if (EDGE_COUNT (then_bb->succs) > 0
4469 && (!single_succ_p (then_bb)
4470 || (single_succ_edge (then_bb)->flags & EDGE_COMPLEX)
4471 || (epilogue_completed
4472 && tablejump_p (BB_END (then_bb), NULL, NULL))))
4473 return FALSE;
4475 /* If the THEN block has no successors, conditional execution can still
4476 make a conditional call. Don't do this unless the ELSE block has
4477 only one incoming edge -- the CFG manipulation is too ugly otherwise.
4478 Check for the last insn of the THEN block being an indirect jump, which
4479 is listed as not having any successors, but confuses the rest of the CE
4480 code processing. ??? we should fix this in the future. */
4481 if (EDGE_COUNT (then_bb->succs) == 0)
4483 if (single_pred_p (else_bb) && else_bb != EXIT_BLOCK_PTR_FOR_FN (cfun))
4485 rtx_insn *last_insn = BB_END (then_bb);
4487 while (last_insn
4488 && NOTE_P (last_insn)
4489 && last_insn != BB_HEAD (then_bb))
4490 last_insn = PREV_INSN (last_insn);
4492 if (last_insn
4493 && JUMP_P (last_insn)
4494 && ! simplejump_p (last_insn))
4495 return FALSE;
4497 join_bb = else_bb;
4498 else_bb = NULL_BLOCK;
4500 else
4501 return FALSE;
4504 /* If the THEN block's successor is the other edge out of the TEST block,
4505 then we have an IF-THEN combo without an ELSE. */
4506 else if (single_succ (then_bb) == else_bb)
4508 join_bb = else_bb;
4509 else_bb = NULL_BLOCK;
4512 /* If the THEN and ELSE block meet in a subsequent block, and the ELSE
4513 has exactly one predecessor and one successor, and the outgoing edge
4514 is not complex, then we have an IF-THEN-ELSE combo. */
4515 else if (single_succ_p (else_bb)
4516 && single_succ (then_bb) == single_succ (else_bb)
4517 && single_pred_p (else_bb)
4518 && !(single_succ_edge (else_bb)->flags & EDGE_COMPLEX)
4519 && !(epilogue_completed
4520 && tablejump_p (BB_END (else_bb), NULL, NULL)))
4521 join_bb = single_succ (else_bb);
4523 /* Otherwise it is not an IF-THEN or IF-THEN-ELSE combination. */
4524 else
4525 return FALSE;
4527 num_possible_if_blocks++;
4529 if (dump_file)
4531 fprintf (dump_file,
4532 "\nIF-THEN%s block found, pass %d, start block %d "
4533 "[insn %d], then %d [%d]",
4534 (else_bb) ? "-ELSE" : "",
4535 ce_info->pass,
4536 test_bb->index,
4537 BB_HEAD (test_bb) ? (int)INSN_UID (BB_HEAD (test_bb)) : -1,
4538 then_bb->index,
4539 BB_HEAD (then_bb) ? (int)INSN_UID (BB_HEAD (then_bb)) : -1);
4541 if (else_bb)
4542 fprintf (dump_file, ", else %d [%d]",
4543 else_bb->index,
4544 BB_HEAD (else_bb) ? (int)INSN_UID (BB_HEAD (else_bb)) : -1);
4546 fprintf (dump_file, ", join %d [%d]",
4547 join_bb->index,
4548 BB_HEAD (join_bb) ? (int)INSN_UID (BB_HEAD (join_bb)) : -1);
4550 if (ce_info->num_multiple_test_blocks > 0)
4551 fprintf (dump_file, ", %d %s block%s last test %d [%d]",
4552 ce_info->num_multiple_test_blocks,
4553 (ce_info->and_and_p) ? "&&" : "||",
4554 (ce_info->num_multiple_test_blocks == 1) ? "" : "s",
4555 ce_info->last_test_bb->index,
4556 ((BB_HEAD (ce_info->last_test_bb))
4557 ? (int)INSN_UID (BB_HEAD (ce_info->last_test_bb))
4558 : -1));
4560 fputc ('\n', dump_file);
4563 /* Make sure IF, THEN, and ELSE, blocks are adjacent. Actually, we get the
4564 first condition for free, since we've already asserted that there's a
4565 fallthru edge from IF to THEN. Likewise for the && and || blocks, since
4566 we checked the FALLTHRU flag, those are already adjacent to the last IF
4567 block. */
4568 /* ??? As an enhancement, move the ELSE block. Have to deal with
4569 BLOCK notes, if by no other means than backing out the merge if they
4570 exist. Sticky enough I don't want to think about it now. */
4571 next = then_bb;
4572 if (else_bb && (next = next->next_bb) != else_bb)
4573 return FALSE;
4574 if ((next = next->next_bb) != join_bb
4575 && join_bb != EXIT_BLOCK_PTR_FOR_FN (cfun))
4577 if (else_bb)
4578 join_bb = NULL;
4579 else
4580 return FALSE;
4583 /* Do the real work. */
4585 ce_info->else_bb = else_bb;
4586 ce_info->join_bb = join_bb;
4588 /* If we have && and || tests, try to first handle combining the && and ||
4589 tests into the conditional code, and if that fails, go back and handle
4590 it without the && and ||, which at present handles the && case if there
4591 was no ELSE block. */
4592 if (cond_exec_process_if_block (ce_info, TRUE))
4593 return TRUE;
4595 if (ce_info->num_multiple_test_blocks)
4597 cancel_changes (0);
4599 if (cond_exec_process_if_block (ce_info, FALSE))
4600 return TRUE;
4603 return FALSE;
4606 /* Convert a branch over a trap, or a branch
4607 to a trap, into a conditional trap. */
4609 static int
4610 find_cond_trap (basic_block test_bb, edge then_edge, edge else_edge)
4612 basic_block then_bb = then_edge->dest;
4613 basic_block else_bb = else_edge->dest;
4614 basic_block other_bb, trap_bb;
4615 rtx_insn *trap, *jump;
4616 rtx cond;
4617 rtx_insn *cond_earliest;
4619 /* Locate the block with the trap instruction. */
4620 /* ??? While we look for no successors, we really ought to allow
4621 EH successors. Need to fix merge_if_block for that to work. */
4622 if ((trap = block_has_only_trap (then_bb)) != NULL)
4623 trap_bb = then_bb, other_bb = else_bb;
4624 else if ((trap = block_has_only_trap (else_bb)) != NULL)
4625 trap_bb = else_bb, other_bb = then_bb;
4626 else
4627 return FALSE;
4629 if (dump_file)
4631 fprintf (dump_file, "\nTRAP-IF block found, start %d, trap %d\n",
4632 test_bb->index, trap_bb->index);
4635 /* If this is not a standard conditional jump, we can't parse it. */
4636 jump = BB_END (test_bb);
4637 cond = noce_get_condition (jump, &cond_earliest, then_bb == trap_bb);
4638 if (! cond)
4639 return FALSE;
4641 /* If the conditional jump is more than just a conditional jump, then
4642 we can not do if-conversion on this block. Give up for returnjump_p,
4643 changing a conditional return followed by unconditional trap for
4644 conditional trap followed by unconditional return is likely not
4645 beneficial and harder to handle. */
4646 if (! onlyjump_p (jump) || returnjump_p (jump))
4647 return FALSE;
4649 /* We must be comparing objects whose modes imply the size. */
4650 if (GET_MODE (XEXP (cond, 0)) == BLKmode)
4651 return FALSE;
4653 /* Attempt to generate the conditional trap. */
4654 rtx_insn *seq = gen_cond_trap (GET_CODE (cond), copy_rtx (XEXP (cond, 0)),
4655 copy_rtx (XEXP (cond, 1)),
4656 TRAP_CODE (PATTERN (trap)));
4657 if (seq == NULL)
4658 return FALSE;
4660 /* If that results in an invalid insn, back out. */
4661 for (rtx_insn *x = seq; x; x = NEXT_INSN (x))
4662 if (recog_memoized (x) < 0)
4663 return FALSE;
4665 /* Emit the new insns before cond_earliest. */
4666 emit_insn_before_setloc (seq, cond_earliest, INSN_LOCATION (trap));
4668 /* Delete the trap block if possible. */
4669 remove_edge (trap_bb == then_bb ? then_edge : else_edge);
4670 df_set_bb_dirty (test_bb);
4671 df_set_bb_dirty (then_bb);
4672 df_set_bb_dirty (else_bb);
4674 if (EDGE_COUNT (trap_bb->preds) == 0)
4676 delete_basic_block (trap_bb);
4677 num_true_changes++;
4680 /* Wire together the blocks again. */
4681 if (current_ir_type () == IR_RTL_CFGLAYOUT)
4682 single_succ_edge (test_bb)->flags |= EDGE_FALLTHRU;
4683 else if (trap_bb == then_bb)
4685 rtx lab = JUMP_LABEL (jump);
4686 rtx_insn *seq = targetm.gen_jump (lab);
4687 rtx_jump_insn *newjump = emit_jump_insn_after (seq, jump);
4688 LABEL_NUSES (lab) += 1;
4689 JUMP_LABEL (newjump) = lab;
4690 emit_barrier_after (newjump);
4692 delete_insn (jump);
4694 if (can_merge_blocks_p (test_bb, other_bb))
4696 merge_blocks (test_bb, other_bb);
4697 num_true_changes++;
4700 num_updated_if_blocks++;
4701 return TRUE;
4704 /* Subroutine of find_cond_trap: if BB contains only a trap insn,
4705 return it. */
4707 static rtx_insn *
4708 block_has_only_trap (basic_block bb)
4710 rtx_insn *trap;
4712 /* We're not the exit block. */
4713 if (bb == EXIT_BLOCK_PTR_FOR_FN (cfun))
4714 return NULL;
4716 /* The block must have no successors. */
4717 if (EDGE_COUNT (bb->succs) > 0)
4718 return NULL;
4720 /* The only instruction in the THEN block must be the trap. */
4721 trap = first_active_insn (bb);
4722 if (! (trap == BB_END (bb)
4723 && GET_CODE (PATTERN (trap)) == TRAP_IF
4724 && TRAP_CONDITION (PATTERN (trap)) == const_true_rtx))
4725 return NULL;
4727 return trap;
4730 /* Look for IF-THEN-ELSE cases in which one of THEN or ELSE is
4731 transformable, but not necessarily the other. There need be no
4732 JOIN block.
4734 Return TRUE if we were successful at converting the block.
4736 Cases we'd like to look at:
4739 if (test) goto over; // x not live
4740 x = a;
4741 goto label;
4742 over:
4744 becomes
4746 x = a;
4747 if (! test) goto label;
4750 if (test) goto E; // x not live
4751 x = big();
4752 goto L;
4754 x = b;
4755 goto M;
4757 becomes
4759 x = b;
4760 if (test) goto M;
4761 x = big();
4762 goto L;
4764 (3) // This one's really only interesting for targets that can do
4765 // multiway branching, e.g. IA-64 BBB bundles. For other targets
4766 // it results in multiple branches on a cache line, which often
4767 // does not sit well with predictors.
4769 if (test1) goto E; // predicted not taken
4770 x = a;
4771 if (test2) goto F;
4774 x = b;
4777 becomes
4779 x = a;
4780 if (test1) goto E;
4781 if (test2) goto F;
4783 Notes:
4785 (A) Don't do (2) if the branch is predicted against the block we're
4786 eliminating. Do it anyway if we can eliminate a branch; this requires
4787 that the sole successor of the eliminated block postdominate the other
4788 side of the if.
4790 (B) With CE, on (3) we can steal from both sides of the if, creating
4792 if (test1) x = a;
4793 if (!test1) x = b;
4794 if (test1) goto J;
4795 if (test2) goto F;
4799 Again, this is most useful if J postdominates.
4801 (C) CE substitutes for helpful life information.
4803 (D) These heuristics need a lot of work. */
4805 /* Tests for case 1 above. */
4807 static int
4808 find_if_case_1 (basic_block test_bb, edge then_edge, edge else_edge)
4810 basic_block then_bb = then_edge->dest;
4811 basic_block else_bb = else_edge->dest;
4812 basic_block new_bb;
4813 int then_bb_index;
4814 profile_probability then_prob;
4815 rtx else_target = NULL_RTX;
4817 /* If we are partitioning hot/cold basic blocks, we don't want to
4818 mess up unconditional or indirect jumps that cross between hot
4819 and cold sections.
4821 Basic block partitioning may result in some jumps that appear to
4822 be optimizable (or blocks that appear to be mergeable), but which really
4823 must be left untouched (they are required to make it safely across
4824 partition boundaries). See the comments at the top of
4825 bb-reorder.c:partition_hot_cold_basic_blocks for complete details. */
4827 if ((BB_END (then_bb)
4828 && JUMP_P (BB_END (then_bb))
4829 && CROSSING_JUMP_P (BB_END (then_bb)))
4830 || (BB_END (test_bb)
4831 && JUMP_P (BB_END (test_bb))
4832 && CROSSING_JUMP_P (BB_END (test_bb)))
4833 || (BB_END (else_bb)
4834 && JUMP_P (BB_END (else_bb))
4835 && CROSSING_JUMP_P (BB_END (else_bb))))
4836 return FALSE;
4838 /* THEN has one successor. */
4839 if (!single_succ_p (then_bb))
4840 return FALSE;
4842 /* THEN does not fall through, but is not strange either. */
4843 if (single_succ_edge (then_bb)->flags & (EDGE_COMPLEX | EDGE_FALLTHRU))
4844 return FALSE;
4846 /* THEN has one predecessor. */
4847 if (!single_pred_p (then_bb))
4848 return FALSE;
4850 /* THEN must do something. */
4851 if (forwarder_block_p (then_bb))
4852 return FALSE;
4854 num_possible_if_blocks++;
4855 if (dump_file)
4856 fprintf (dump_file,
4857 "\nIF-CASE-1 found, start %d, then %d\n",
4858 test_bb->index, then_bb->index);
4860 then_prob = then_edge->probability.invert ();
4862 /* We're speculating from the THEN path, we want to make sure the cost
4863 of speculation is within reason. */
4864 if (! cheap_bb_rtx_cost_p (then_bb, then_prob,
4865 COSTS_N_INSNS (BRANCH_COST (optimize_bb_for_speed_p (then_edge->src),
4866 predictable_edge_p (then_edge)))))
4867 return FALSE;
4869 if (else_bb == EXIT_BLOCK_PTR_FOR_FN (cfun))
4871 rtx_insn *jump = BB_END (else_edge->src);
4872 gcc_assert (JUMP_P (jump));
4873 else_target = JUMP_LABEL (jump);
4876 /* Registers set are dead, or are predicable. */
4877 if (! dead_or_predicable (test_bb, then_bb, else_bb,
4878 single_succ_edge (then_bb), 1))
4879 return FALSE;
4881 /* Conversion went ok, including moving the insns and fixing up the
4882 jump. Adjust the CFG to match. */
4884 /* We can avoid creating a new basic block if then_bb is immediately
4885 followed by else_bb, i.e. deleting then_bb allows test_bb to fall
4886 through to else_bb. */
4888 if (then_bb->next_bb == else_bb
4889 && then_bb->prev_bb == test_bb
4890 && else_bb != EXIT_BLOCK_PTR_FOR_FN (cfun))
4892 redirect_edge_succ (FALLTHRU_EDGE (test_bb), else_bb);
4893 new_bb = 0;
4895 else if (else_bb == EXIT_BLOCK_PTR_FOR_FN (cfun))
4896 new_bb = force_nonfallthru_and_redirect (FALLTHRU_EDGE (test_bb),
4897 else_bb, else_target);
4898 else
4899 new_bb = redirect_edge_and_branch_force (FALLTHRU_EDGE (test_bb),
4900 else_bb);
4902 df_set_bb_dirty (test_bb);
4903 df_set_bb_dirty (else_bb);
4905 then_bb_index = then_bb->index;
4906 delete_basic_block (then_bb);
4908 /* Make rest of code believe that the newly created block is the THEN_BB
4909 block we removed. */
4910 if (new_bb)
4912 df_bb_replace (then_bb_index, new_bb);
4913 /* This should have been done above via force_nonfallthru_and_redirect
4914 (possibly called from redirect_edge_and_branch_force). */
4915 gcc_checking_assert (BB_PARTITION (new_bb) == BB_PARTITION (test_bb));
4918 num_true_changes++;
4919 num_updated_if_blocks++;
4920 return TRUE;
4923 /* Test for case 2 above. */
4925 static int
4926 find_if_case_2 (basic_block test_bb, edge then_edge, edge else_edge)
4928 basic_block then_bb = then_edge->dest;
4929 basic_block else_bb = else_edge->dest;
4930 edge else_succ;
4931 profile_probability then_prob, else_prob;
4933 /* We do not want to speculate (empty) loop latches. */
4934 if (current_loops
4935 && else_bb->loop_father->latch == else_bb)
4936 return FALSE;
4938 /* If we are partitioning hot/cold basic blocks, we don't want to
4939 mess up unconditional or indirect jumps that cross between hot
4940 and cold sections.
4942 Basic block partitioning may result in some jumps that appear to
4943 be optimizable (or blocks that appear to be mergeable), but which really
4944 must be left untouched (they are required to make it safely across
4945 partition boundaries). See the comments at the top of
4946 bb-reorder.c:partition_hot_cold_basic_blocks for complete details. */
4948 if ((BB_END (then_bb)
4949 && JUMP_P (BB_END (then_bb))
4950 && CROSSING_JUMP_P (BB_END (then_bb)))
4951 || (BB_END (test_bb)
4952 && JUMP_P (BB_END (test_bb))
4953 && CROSSING_JUMP_P (BB_END (test_bb)))
4954 || (BB_END (else_bb)
4955 && JUMP_P (BB_END (else_bb))
4956 && CROSSING_JUMP_P (BB_END (else_bb))))
4957 return FALSE;
4959 /* ELSE has one successor. */
4960 if (!single_succ_p (else_bb))
4961 return FALSE;
4962 else
4963 else_succ = single_succ_edge (else_bb);
4965 /* ELSE outgoing edge is not complex. */
4966 if (else_succ->flags & EDGE_COMPLEX)
4967 return FALSE;
4969 /* ELSE has one predecessor. */
4970 if (!single_pred_p (else_bb))
4971 return FALSE;
4973 /* THEN is not EXIT. */
4974 if (then_bb->index < NUM_FIXED_BLOCKS)
4975 return FALSE;
4977 else_prob = else_edge->probability;
4978 then_prob = else_prob.invert ();
4980 /* ELSE is predicted or SUCC(ELSE) postdominates THEN. */
4981 if (else_prob > then_prob)
4983 else if (else_succ->dest->index < NUM_FIXED_BLOCKS
4984 || dominated_by_p (CDI_POST_DOMINATORS, then_bb,
4985 else_succ->dest))
4987 else
4988 return FALSE;
4990 num_possible_if_blocks++;
4991 if (dump_file)
4992 fprintf (dump_file,
4993 "\nIF-CASE-2 found, start %d, else %d\n",
4994 test_bb->index, else_bb->index);
4996 /* We're speculating from the ELSE path, we want to make sure the cost
4997 of speculation is within reason. */
4998 if (! cheap_bb_rtx_cost_p (else_bb, else_prob,
4999 COSTS_N_INSNS (BRANCH_COST (optimize_bb_for_speed_p (else_edge->src),
5000 predictable_edge_p (else_edge)))))
5001 return FALSE;
5003 /* Registers set are dead, or are predicable. */
5004 if (! dead_or_predicable (test_bb, else_bb, then_bb, else_succ, 0))
5005 return FALSE;
5007 /* Conversion went ok, including moving the insns and fixing up the
5008 jump. Adjust the CFG to match. */
5010 df_set_bb_dirty (test_bb);
5011 df_set_bb_dirty (then_bb);
5012 delete_basic_block (else_bb);
5014 num_true_changes++;
5015 num_updated_if_blocks++;
5017 /* ??? We may now fallthru from one of THEN's successors into a join
5018 block. Rerun cleanup_cfg? Examine things manually? Wait? */
5020 return TRUE;
5023 /* Used by the code above to perform the actual rtl transformations.
5024 Return TRUE if successful.
5026 TEST_BB is the block containing the conditional branch. MERGE_BB
5027 is the block containing the code to manipulate. DEST_EDGE is an
5028 edge representing a jump to the join block; after the conversion,
5029 TEST_BB should be branching to its destination.
5030 REVERSEP is true if the sense of the branch should be reversed. */
5032 static int
5033 dead_or_predicable (basic_block test_bb, basic_block merge_bb,
5034 basic_block other_bb, edge dest_edge, int reversep)
5036 basic_block new_dest = dest_edge->dest;
5037 rtx_insn *head, *end, *jump;
5038 rtx_insn *earliest = NULL;
5039 rtx old_dest;
5040 bitmap merge_set = NULL;
5041 /* Number of pending changes. */
5042 int n_validated_changes = 0;
5043 rtx new_dest_label = NULL_RTX;
5045 jump = BB_END (test_bb);
5047 /* Find the extent of the real code in the merge block. */
5048 head = BB_HEAD (merge_bb);
5049 end = BB_END (merge_bb);
5051 while (DEBUG_INSN_P (end) && end != head)
5052 end = PREV_INSN (end);
5054 /* If merge_bb ends with a tablejump, predicating/moving insn's
5055 into test_bb and then deleting merge_bb will result in the jumptable
5056 that follows merge_bb being removed along with merge_bb and then we
5057 get an unresolved reference to the jumptable. */
5058 if (tablejump_p (end, NULL, NULL))
5059 return FALSE;
5061 if (LABEL_P (head))
5062 head = NEXT_INSN (head);
5063 while (DEBUG_INSN_P (head) && head != end)
5064 head = NEXT_INSN (head);
5065 if (NOTE_P (head))
5067 if (head == end)
5069 head = end = NULL;
5070 goto no_body;
5072 head = NEXT_INSN (head);
5073 while (DEBUG_INSN_P (head) && head != end)
5074 head = NEXT_INSN (head);
5077 if (JUMP_P (end))
5079 if (!onlyjump_p (end))
5080 return FALSE;
5081 if (head == end)
5083 head = end = NULL;
5084 goto no_body;
5086 end = PREV_INSN (end);
5087 while (DEBUG_INSN_P (end) && end != head)
5088 end = PREV_INSN (end);
5091 /* Don't move frame-related insn across the conditional branch. This
5092 can lead to one of the paths of the branch having wrong unwind info. */
5093 if (epilogue_completed)
5095 rtx_insn *insn = head;
5096 while (1)
5098 if (INSN_P (insn) && RTX_FRAME_RELATED_P (insn))
5099 return FALSE;
5100 if (insn == end)
5101 break;
5102 insn = NEXT_INSN (insn);
5106 /* Disable handling dead code by conditional execution if the machine needs
5107 to do anything funny with the tests, etc. */
5108 #ifndef IFCVT_MODIFY_TESTS
5109 if (targetm.have_conditional_execution ())
5111 /* In the conditional execution case, we have things easy. We know
5112 the condition is reversible. We don't have to check life info
5113 because we're going to conditionally execute the code anyway.
5114 All that's left is making sure the insns involved can actually
5115 be predicated. */
5117 rtx cond;
5119 cond = cond_exec_get_condition (jump);
5120 if (! cond)
5121 return FALSE;
5123 rtx note = find_reg_note (jump, REG_BR_PROB, NULL_RTX);
5124 profile_probability prob_val
5125 = (note ? profile_probability::from_reg_br_prob_note (XINT (note, 0))
5126 : profile_probability::uninitialized ());
5128 if (reversep)
5130 enum rtx_code rev = reversed_comparison_code (cond, jump);
5131 if (rev == UNKNOWN)
5132 return FALSE;
5133 cond = gen_rtx_fmt_ee (rev, GET_MODE (cond), XEXP (cond, 0),
5134 XEXP (cond, 1));
5135 prob_val = prob_val.invert ();
5138 if (cond_exec_process_insns (NULL, head, end, cond, prob_val, 0)
5139 && verify_changes (0))
5140 n_validated_changes = num_validated_changes ();
5141 else
5142 cancel_changes (0);
5144 earliest = jump;
5146 #endif
5148 /* If we allocated new pseudos (e.g. in the conditional move
5149 expander called from noce_emit_cmove), we must resize the
5150 array first. */
5151 if (max_regno < max_reg_num ())
5152 max_regno = max_reg_num ();
5154 /* Try the NCE path if the CE path did not result in any changes. */
5155 if (n_validated_changes == 0)
5157 rtx cond;
5158 rtx_insn *insn;
5159 regset live;
5160 bool success;
5162 /* In the non-conditional execution case, we have to verify that there
5163 are no trapping operations, no calls, no references to memory, and
5164 that any registers modified are dead at the branch site. */
5166 if (!any_condjump_p (jump))
5167 return FALSE;
5169 /* Find the extent of the conditional. */
5170 cond = noce_get_condition (jump, &earliest, false);
5171 if (!cond)
5172 return FALSE;
5174 live = BITMAP_ALLOC (&reg_obstack);
5175 simulate_backwards_to_point (merge_bb, live, end);
5176 success = can_move_insns_across (head, end, earliest, jump,
5177 merge_bb, live,
5178 df_get_live_in (other_bb), NULL);
5179 BITMAP_FREE (live);
5180 if (!success)
5181 return FALSE;
5183 /* Collect the set of registers set in MERGE_BB. */
5184 merge_set = BITMAP_ALLOC (&reg_obstack);
5186 FOR_BB_INSNS (merge_bb, insn)
5187 if (NONDEBUG_INSN_P (insn))
5188 df_simulate_find_defs (insn, merge_set);
5190 /* If shrink-wrapping, disable this optimization when test_bb is
5191 the first basic block and merge_bb exits. The idea is to not
5192 move code setting up a return register as that may clobber a
5193 register used to pass function parameters, which then must be
5194 saved in caller-saved regs. A caller-saved reg requires the
5195 prologue, killing a shrink-wrap opportunity. */
5196 if ((SHRINK_WRAPPING_ENABLED && !epilogue_completed)
5197 && ENTRY_BLOCK_PTR_FOR_FN (cfun)->next_bb == test_bb
5198 && single_succ_p (new_dest)
5199 && single_succ (new_dest) == EXIT_BLOCK_PTR_FOR_FN (cfun)
5200 && bitmap_intersect_p (df_get_live_in (new_dest), merge_set))
5202 regset return_regs;
5203 unsigned int i;
5205 return_regs = BITMAP_ALLOC (&reg_obstack);
5207 /* Start off with the intersection of regs used to pass
5208 params and regs used to return values. */
5209 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
5210 if (FUNCTION_ARG_REGNO_P (i)
5211 && targetm.calls.function_value_regno_p (i))
5212 bitmap_set_bit (return_regs, INCOMING_REGNO (i));
5214 bitmap_and_into (return_regs,
5215 df_get_live_out (ENTRY_BLOCK_PTR_FOR_FN (cfun)));
5216 bitmap_and_into (return_regs,
5217 df_get_live_in (EXIT_BLOCK_PTR_FOR_FN (cfun)));
5218 if (!bitmap_empty_p (return_regs))
5220 FOR_BB_INSNS_REVERSE (new_dest, insn)
5221 if (NONDEBUG_INSN_P (insn))
5223 df_ref def;
5225 /* If this insn sets any reg in return_regs, add all
5226 reg uses to the set of regs we're interested in. */
5227 FOR_EACH_INSN_DEF (def, insn)
5228 if (bitmap_bit_p (return_regs, DF_REF_REGNO (def)))
5230 df_simulate_uses (insn, return_regs);
5231 break;
5234 if (bitmap_intersect_p (merge_set, return_regs))
5236 BITMAP_FREE (return_regs);
5237 BITMAP_FREE (merge_set);
5238 return FALSE;
5241 BITMAP_FREE (return_regs);
5245 no_body:
5246 /* We don't want to use normal invert_jump or redirect_jump because
5247 we don't want to delete_insn called. Also, we want to do our own
5248 change group management. */
5250 old_dest = JUMP_LABEL (jump);
5251 if (other_bb != new_dest)
5253 if (!any_condjump_p (jump))
5254 goto cancel;
5256 if (JUMP_P (BB_END (dest_edge->src)))
5257 new_dest_label = JUMP_LABEL (BB_END (dest_edge->src));
5258 else if (new_dest == EXIT_BLOCK_PTR_FOR_FN (cfun))
5259 new_dest_label = ret_rtx;
5260 else
5261 new_dest_label = block_label (new_dest);
5263 rtx_jump_insn *jump_insn = as_a <rtx_jump_insn *> (jump);
5264 if (reversep
5265 ? ! invert_jump_1 (jump_insn, new_dest_label)
5266 : ! redirect_jump_1 (jump_insn, new_dest_label))
5267 goto cancel;
5270 if (verify_changes (n_validated_changes))
5271 confirm_change_group ();
5272 else
5273 goto cancel;
5275 if (other_bb != new_dest)
5277 redirect_jump_2 (as_a <rtx_jump_insn *> (jump), old_dest, new_dest_label,
5278 0, reversep);
5280 redirect_edge_succ (BRANCH_EDGE (test_bb), new_dest);
5281 if (reversep)
5283 std::swap (BRANCH_EDGE (test_bb)->count,
5284 FALLTHRU_EDGE (test_bb)->count);
5285 std::swap (BRANCH_EDGE (test_bb)->probability,
5286 FALLTHRU_EDGE (test_bb)->probability);
5287 update_br_prob_note (test_bb);
5291 /* Move the insns out of MERGE_BB to before the branch. */
5292 if (head != NULL)
5294 rtx_insn *insn;
5296 if (end == BB_END (merge_bb))
5297 BB_END (merge_bb) = PREV_INSN (head);
5299 /* PR 21767: when moving insns above a conditional branch, the REG_EQUAL
5300 notes being moved might become invalid. */
5301 insn = head;
5304 rtx note;
5306 if (! INSN_P (insn))
5307 continue;
5308 note = find_reg_note (insn, REG_EQUAL, NULL_RTX);
5309 if (! note)
5310 continue;
5311 remove_note (insn, note);
5312 } while (insn != end && (insn = NEXT_INSN (insn)));
5314 /* PR46315: when moving insns above a conditional branch, the REG_EQUAL
5315 notes referring to the registers being set might become invalid. */
5316 if (merge_set)
5318 unsigned i;
5319 bitmap_iterator bi;
5321 EXECUTE_IF_SET_IN_BITMAP (merge_set, 0, i, bi)
5322 remove_reg_equal_equiv_notes_for_regno (i);
5324 BITMAP_FREE (merge_set);
5327 reorder_insns (head, end, PREV_INSN (earliest));
5330 /* Remove the jump and edge if we can. */
5331 if (other_bb == new_dest)
5333 delete_insn (jump);
5334 remove_edge (BRANCH_EDGE (test_bb));
5335 /* ??? Can't merge blocks here, as then_bb is still in use.
5336 At minimum, the merge will get done just before bb-reorder. */
5339 return TRUE;
5341 cancel:
5342 cancel_changes (0);
5344 if (merge_set)
5345 BITMAP_FREE (merge_set);
5347 return FALSE;
5350 /* Main entry point for all if-conversion. AFTER_COMBINE is true if
5351 we are after combine pass. */
5353 static void
5354 if_convert (bool after_combine)
5356 basic_block bb;
5357 int pass;
5359 if (optimize == 1)
5361 df_live_add_problem ();
5362 df_live_set_all_dirty ();
5365 /* Record whether we are after combine pass. */
5366 ifcvt_after_combine = after_combine;
5367 have_cbranchcc4 = (direct_optab_handler (cbranch_optab, CCmode)
5368 != CODE_FOR_nothing);
5369 num_possible_if_blocks = 0;
5370 num_updated_if_blocks = 0;
5371 num_true_changes = 0;
5373 loop_optimizer_init (AVOID_CFG_MODIFICATIONS);
5374 mark_loop_exit_edges ();
5375 loop_optimizer_finalize ();
5376 free_dominance_info (CDI_DOMINATORS);
5378 /* Compute postdominators. */
5379 calculate_dominance_info (CDI_POST_DOMINATORS);
5381 df_set_flags (DF_LR_RUN_DCE);
5383 /* Go through each of the basic blocks looking for things to convert. If we
5384 have conditional execution, we make multiple passes to allow us to handle
5385 IF-THEN{-ELSE} blocks within other IF-THEN{-ELSE} blocks. */
5386 pass = 0;
5389 df_analyze ();
5390 /* Only need to do dce on the first pass. */
5391 df_clear_flags (DF_LR_RUN_DCE);
5392 cond_exec_changed_p = FALSE;
5393 pass++;
5395 #ifdef IFCVT_MULTIPLE_DUMPS
5396 if (dump_file && pass > 1)
5397 fprintf (dump_file, "\n\n========== Pass %d ==========\n", pass);
5398 #endif
5400 FOR_EACH_BB_FN (bb, cfun)
5402 basic_block new_bb;
5403 while (!df_get_bb_dirty (bb)
5404 && (new_bb = find_if_header (bb, pass)) != NULL)
5405 bb = new_bb;
5408 #ifdef IFCVT_MULTIPLE_DUMPS
5409 if (dump_file && cond_exec_changed_p)
5410 print_rtl_with_bb (dump_file, get_insns (), dump_flags);
5411 #endif
5413 while (cond_exec_changed_p);
5415 #ifdef IFCVT_MULTIPLE_DUMPS
5416 if (dump_file)
5417 fprintf (dump_file, "\n\n========== no more changes\n");
5418 #endif
5420 free_dominance_info (CDI_POST_DOMINATORS);
5422 if (dump_file)
5423 fflush (dump_file);
5425 clear_aux_for_blocks ();
5427 /* If we allocated new pseudos, we must resize the array for sched1. */
5428 if (max_regno < max_reg_num ())
5429 max_regno = max_reg_num ();
5431 /* Write the final stats. */
5432 if (dump_file && num_possible_if_blocks > 0)
5434 fprintf (dump_file,
5435 "\n%d possible IF blocks searched.\n",
5436 num_possible_if_blocks);
5437 fprintf (dump_file,
5438 "%d IF blocks converted.\n",
5439 num_updated_if_blocks);
5440 fprintf (dump_file,
5441 "%d true changes made.\n\n\n",
5442 num_true_changes);
5445 if (optimize == 1)
5446 df_remove_problem (df_live);
5448 checking_verify_flow_info ();
5451 /* If-conversion and CFG cleanup. */
5452 static unsigned int
5453 rest_of_handle_if_conversion (void)
5455 if (flag_if_conversion)
5457 if (dump_file)
5459 dump_reg_info (dump_file);
5460 dump_flow_info (dump_file, dump_flags);
5462 cleanup_cfg (CLEANUP_EXPENSIVE);
5463 if_convert (false);
5466 cleanup_cfg (0);
5467 return 0;
5470 namespace {
5472 const pass_data pass_data_rtl_ifcvt =
5474 RTL_PASS, /* type */
5475 "ce1", /* name */
5476 OPTGROUP_NONE, /* optinfo_flags */
5477 TV_IFCVT, /* tv_id */
5478 0, /* properties_required */
5479 0, /* properties_provided */
5480 0, /* properties_destroyed */
5481 0, /* todo_flags_start */
5482 TODO_df_finish, /* todo_flags_finish */
5485 class pass_rtl_ifcvt : public rtl_opt_pass
5487 public:
5488 pass_rtl_ifcvt (gcc::context *ctxt)
5489 : rtl_opt_pass (pass_data_rtl_ifcvt, ctxt)
5492 /* opt_pass methods: */
5493 virtual bool gate (function *)
5495 return (optimize > 0) && dbg_cnt (if_conversion);
5498 virtual unsigned int execute (function *)
5500 return rest_of_handle_if_conversion ();
5503 }; // class pass_rtl_ifcvt
5505 } // anon namespace
5507 rtl_opt_pass *
5508 make_pass_rtl_ifcvt (gcc::context *ctxt)
5510 return new pass_rtl_ifcvt (ctxt);
5514 /* Rerun if-conversion, as combine may have simplified things enough
5515 to now meet sequence length restrictions. */
5517 namespace {
5519 const pass_data pass_data_if_after_combine =
5521 RTL_PASS, /* type */
5522 "ce2", /* name */
5523 OPTGROUP_NONE, /* optinfo_flags */
5524 TV_IFCVT, /* tv_id */
5525 0, /* properties_required */
5526 0, /* properties_provided */
5527 0, /* properties_destroyed */
5528 0, /* todo_flags_start */
5529 TODO_df_finish, /* todo_flags_finish */
5532 class pass_if_after_combine : public rtl_opt_pass
5534 public:
5535 pass_if_after_combine (gcc::context *ctxt)
5536 : rtl_opt_pass (pass_data_if_after_combine, ctxt)
5539 /* opt_pass methods: */
5540 virtual bool gate (function *)
5542 return optimize > 0 && flag_if_conversion
5543 && dbg_cnt (if_after_combine);
5546 virtual unsigned int execute (function *)
5548 if_convert (true);
5549 return 0;
5552 }; // class pass_if_after_combine
5554 } // anon namespace
5556 rtl_opt_pass *
5557 make_pass_if_after_combine (gcc::context *ctxt)
5559 return new pass_if_after_combine (ctxt);
5563 namespace {
5565 const pass_data pass_data_if_after_reload =
5567 RTL_PASS, /* type */
5568 "ce3", /* name */
5569 OPTGROUP_NONE, /* optinfo_flags */
5570 TV_IFCVT2, /* tv_id */
5571 0, /* properties_required */
5572 0, /* properties_provided */
5573 0, /* properties_destroyed */
5574 0, /* todo_flags_start */
5575 TODO_df_finish, /* todo_flags_finish */
5578 class pass_if_after_reload : public rtl_opt_pass
5580 public:
5581 pass_if_after_reload (gcc::context *ctxt)
5582 : rtl_opt_pass (pass_data_if_after_reload, ctxt)
5585 /* opt_pass methods: */
5586 virtual bool gate (function *)
5588 return optimize > 0 && flag_if_conversion2
5589 && dbg_cnt (if_after_reload);
5592 virtual unsigned int execute (function *)
5594 if_convert (true);
5595 return 0;
5598 }; // class pass_if_after_reload
5600 } // anon namespace
5602 rtl_opt_pass *
5603 make_pass_if_after_reload (gcc::context *ctxt)
5605 return new pass_if_after_reload (ctxt);