PR tree-optimization/66718
[official-gcc.git] / gcc / ifcvt.c
blobf796799fc3d92e60c6e4c614942acada381ea493
1 /* If-conversion support.
2 Copyright (C) 2000-2015 Free Software Foundation, Inc.
4 This file is part of GCC.
6 GCC is free software; you can redistribute it and/or modify it
7 under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 3, or (at your option)
9 any later version.
11 GCC is distributed in the hope that it will be useful, but WITHOUT
12 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
13 or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
14 License for more details.
16 You should have received a copy of the GNU General Public License
17 along with GCC; see the file COPYING3. If not see
18 <http://www.gnu.org/licenses/>. */
20 #include "config.h"
21 #include "system.h"
22 #include "coretypes.h"
23 #include "tm.h"
25 #include "rtl.h"
26 #include "regs.h"
27 #include "hard-reg-set.h"
28 #include "function.h"
29 #include "flags.h"
30 #include "insn-config.h"
31 #include "recog.h"
32 #include "except.h"
33 #include "predict.h"
34 #include "dominance.h"
35 #include "cfg.h"
36 #include "cfgrtl.h"
37 #include "cfganal.h"
38 #include "cfgcleanup.h"
39 #include "basic-block.h"
40 #include "symtab.h"
41 #include "alias.h"
42 #include "tree.h"
43 #include "expmed.h"
44 #include "dojump.h"
45 #include "explow.h"
46 #include "calls.h"
47 #include "emit-rtl.h"
48 #include "varasm.h"
49 #include "stmt.h"
50 #include "expr.h"
51 #include "output.h"
52 #include "insn-codes.h"
53 #include "optabs.h"
54 #include "diagnostic-core.h"
55 #include "tm_p.h"
56 #include "cfgloop.h"
57 #include "target.h"
58 #include "tree-pass.h"
59 #include "df.h"
60 #include "dbgcnt.h"
61 #include "shrink-wrap.h"
62 #include "ifcvt.h"
64 #ifndef HAVE_incscc
65 #define HAVE_incscc 0
66 #endif
67 #ifndef HAVE_decscc
68 #define HAVE_decscc 0
69 #endif
71 #ifndef MAX_CONDITIONAL_EXECUTE
72 #define MAX_CONDITIONAL_EXECUTE \
73 (BRANCH_COST (optimize_function_for_speed_p (cfun), false) \
74 + 1)
75 #endif
77 #ifndef HAVE_cbranchcc4
78 #define HAVE_cbranchcc4 0
79 #endif
81 #define IFCVT_MULTIPLE_DUMPS 1
83 #define NULL_BLOCK ((basic_block) NULL)
85 /* True if after combine pass. */
86 static bool ifcvt_after_combine;
88 /* # of IF-THEN or IF-THEN-ELSE blocks we looked at */
89 static int num_possible_if_blocks;
91 /* # of IF-THEN or IF-THEN-ELSE blocks were converted to conditional
92 execution. */
93 static int num_updated_if_blocks;
95 /* # of changes made. */
96 static int num_true_changes;
98 /* Whether conditional execution changes were made. */
99 static int cond_exec_changed_p;
101 /* Forward references. */
102 static int count_bb_insns (const_basic_block);
103 static bool cheap_bb_rtx_cost_p (const_basic_block, int, int);
104 static rtx_insn *first_active_insn (basic_block);
105 static rtx_insn *last_active_insn (basic_block, int);
106 static rtx_insn *find_active_insn_before (basic_block, rtx_insn *);
107 static rtx_insn *find_active_insn_after (basic_block, rtx_insn *);
108 static basic_block block_fallthru (basic_block);
109 static int cond_exec_process_insns (ce_if_block *, rtx_insn *, rtx, rtx, int,
110 int);
111 static rtx cond_exec_get_condition (rtx_insn *);
112 static rtx noce_get_condition (rtx_insn *, rtx_insn **, bool);
113 static int noce_operand_ok (const_rtx);
114 static void merge_if_block (ce_if_block *);
115 static int find_cond_trap (basic_block, edge, edge);
116 static basic_block find_if_header (basic_block, int);
117 static int block_jumps_and_fallthru_p (basic_block, basic_block);
118 static int noce_find_if_block (basic_block, edge, edge, int);
119 static int cond_exec_find_if_block (ce_if_block *);
120 static int find_if_case_1 (basic_block, edge, edge);
121 static int find_if_case_2 (basic_block, edge, edge);
122 static int dead_or_predicable (basic_block, basic_block, basic_block,
123 edge, int);
124 static void noce_emit_move_insn (rtx, rtx);
125 static rtx_insn *block_has_only_trap (basic_block);
127 /* Count the number of non-jump active insns in BB. */
129 static int
130 count_bb_insns (const_basic_block bb)
132 int count = 0;
133 rtx_insn *insn = BB_HEAD (bb);
135 while (1)
137 if (active_insn_p (insn) && !JUMP_P (insn))
138 count++;
140 if (insn == BB_END (bb))
141 break;
142 insn = NEXT_INSN (insn);
145 return count;
148 /* Determine whether the total insn_rtx_cost on non-jump insns in
149 basic block BB is less than MAX_COST. This function returns
150 false if the cost of any instruction could not be estimated.
152 The cost of the non-jump insns in BB is scaled by REG_BR_PROB_BASE
153 as those insns are being speculated. MAX_COST is scaled with SCALE
154 plus a small fudge factor. */
156 static bool
157 cheap_bb_rtx_cost_p (const_basic_block bb, int scale, int max_cost)
159 int count = 0;
160 rtx_insn *insn = BB_HEAD (bb);
161 bool speed = optimize_bb_for_speed_p (bb);
163 /* Set scale to REG_BR_PROB_BASE to void the identical scaling
164 applied to insn_rtx_cost when optimizing for size. Only do
165 this after combine because if-conversion might interfere with
166 passes before combine.
168 Use optimize_function_for_speed_p instead of the pre-defined
169 variable speed to make sure it is set to same value for all
170 basic blocks in one if-conversion transformation. */
171 if (!optimize_function_for_speed_p (cfun) && ifcvt_after_combine)
172 scale = REG_BR_PROB_BASE;
173 /* Our branch probability/scaling factors are just estimates and don't
174 account for cases where we can get speculation for free and other
175 secondary benefits. So we fudge the scale factor to make speculating
176 appear a little more profitable when optimizing for performance. */
177 else
178 scale += REG_BR_PROB_BASE / 8;
181 max_cost *= scale;
183 while (1)
185 if (NONJUMP_INSN_P (insn))
187 int cost = insn_rtx_cost (PATTERN (insn), speed) * REG_BR_PROB_BASE;
188 if (cost == 0)
189 return false;
191 /* If this instruction is the load or set of a "stack" register,
192 such as a floating point register on x87, then the cost of
193 speculatively executing this insn may need to include
194 the additional cost of popping its result off of the
195 register stack. Unfortunately, correctly recognizing and
196 accounting for this additional overhead is tricky, so for
197 now we simply prohibit such speculative execution. */
198 #ifdef STACK_REGS
200 rtx set = single_set (insn);
201 if (set && STACK_REG_P (SET_DEST (set)))
202 return false;
204 #endif
206 count += cost;
207 if (count >= max_cost)
208 return false;
210 else if (CALL_P (insn))
211 return false;
213 if (insn == BB_END (bb))
214 break;
215 insn = NEXT_INSN (insn);
218 return true;
221 /* Return the first non-jump active insn in the basic block. */
223 static rtx_insn *
224 first_active_insn (basic_block bb)
226 rtx_insn *insn = BB_HEAD (bb);
228 if (LABEL_P (insn))
230 if (insn == BB_END (bb))
231 return NULL;
232 insn = NEXT_INSN (insn);
235 while (NOTE_P (insn) || DEBUG_INSN_P (insn))
237 if (insn == BB_END (bb))
238 return NULL;
239 insn = NEXT_INSN (insn);
242 if (JUMP_P (insn))
243 return NULL;
245 return insn;
248 /* Return the last non-jump active (non-jump) insn in the basic block. */
250 static rtx_insn *
251 last_active_insn (basic_block bb, int skip_use_p)
253 rtx_insn *insn = BB_END (bb);
254 rtx_insn *head = BB_HEAD (bb);
256 while (NOTE_P (insn)
257 || JUMP_P (insn)
258 || DEBUG_INSN_P (insn)
259 || (skip_use_p
260 && NONJUMP_INSN_P (insn)
261 && GET_CODE (PATTERN (insn)) == USE))
263 if (insn == head)
264 return NULL;
265 insn = PREV_INSN (insn);
268 if (LABEL_P (insn))
269 return NULL;
271 return insn;
274 /* Return the active insn before INSN inside basic block CURR_BB. */
276 static rtx_insn *
277 find_active_insn_before (basic_block curr_bb, rtx_insn *insn)
279 if (!insn || insn == BB_HEAD (curr_bb))
280 return NULL;
282 while ((insn = PREV_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 start of the basic block. */
288 if (insn == BB_HEAD (curr_bb))
289 return NULL;
292 return insn;
295 /* Return the active insn after INSN inside basic block CURR_BB. */
297 static rtx_insn *
298 find_active_insn_after (basic_block curr_bb, rtx_insn *insn)
300 if (!insn || insn == BB_END (curr_bb))
301 return NULL;
303 while ((insn = NEXT_INSN (insn)) != NULL_RTX)
305 if (NONJUMP_INSN_P (insn) || JUMP_P (insn) || CALL_P (insn))
306 break;
308 /* No other active insn all the way to the end of the basic block. */
309 if (insn == BB_END (curr_bb))
310 return NULL;
313 return insn;
316 /* Return the basic block reached by falling though the basic block BB. */
318 static basic_block
319 block_fallthru (basic_block bb)
321 edge e = find_fallthru_edge (bb->succs);
323 return (e) ? e->dest : NULL_BLOCK;
326 /* Return true if RTXs A and B can be safely interchanged. */
328 static bool
329 rtx_interchangeable_p (const_rtx a, const_rtx b)
331 if (!rtx_equal_p (a, b))
332 return false;
334 if (GET_CODE (a) != MEM)
335 return true;
337 /* A dead type-unsafe memory reference is legal, but a live type-unsafe memory
338 reference is not. Interchanging a dead type-unsafe memory reference with
339 a live type-safe one creates a live type-unsafe memory reference, in other
340 words, it makes the program illegal.
341 We check here conservatively whether the two memory references have equal
342 memory attributes. */
344 return mem_attrs_eq_p (get_mem_attrs (a), get_mem_attrs (b));
348 /* Go through a bunch of insns, converting them to conditional
349 execution format if possible. Return TRUE if all of the non-note
350 insns were processed. */
352 static int
353 cond_exec_process_insns (ce_if_block *ce_info ATTRIBUTE_UNUSED,
354 /* if block information */rtx_insn *start,
355 /* first insn to look at */rtx end,
356 /* last insn to look at */rtx test,
357 /* conditional execution test */int prob_val,
358 /* probability of branch taken. */int mod_ok)
360 int must_be_last = FALSE;
361 rtx_insn *insn;
362 rtx xtest;
363 rtx pattern;
365 if (!start || !end)
366 return FALSE;
368 for (insn = start; ; insn = NEXT_INSN (insn))
370 /* dwarf2out can't cope with conditional prologues. */
371 if (NOTE_P (insn) && NOTE_KIND (insn) == NOTE_INSN_PROLOGUE_END)
372 return FALSE;
374 if (NOTE_P (insn) || DEBUG_INSN_P (insn))
375 goto insn_done;
377 gcc_assert (NONJUMP_INSN_P (insn) || CALL_P (insn));
379 /* dwarf2out can't cope with conditional unwind info. */
380 if (RTX_FRAME_RELATED_P (insn))
381 return FALSE;
383 /* Remove USE insns that get in the way. */
384 if (reload_completed && GET_CODE (PATTERN (insn)) == USE)
386 /* ??? Ug. Actually unlinking the thing is problematic,
387 given what we'd have to coordinate with our callers. */
388 SET_INSN_DELETED (insn);
389 goto insn_done;
392 /* Last insn wasn't last? */
393 if (must_be_last)
394 return FALSE;
396 if (modified_in_p (test, insn))
398 if (!mod_ok)
399 return FALSE;
400 must_be_last = TRUE;
403 /* Now build the conditional form of the instruction. */
404 pattern = PATTERN (insn);
405 xtest = copy_rtx (test);
407 /* If this is already a COND_EXEC, rewrite the test to be an AND of the
408 two conditions. */
409 if (GET_CODE (pattern) == COND_EXEC)
411 if (GET_MODE (xtest) != GET_MODE (COND_EXEC_TEST (pattern)))
412 return FALSE;
414 xtest = gen_rtx_AND (GET_MODE (xtest), xtest,
415 COND_EXEC_TEST (pattern));
416 pattern = COND_EXEC_CODE (pattern);
419 pattern = gen_rtx_COND_EXEC (VOIDmode, xtest, pattern);
421 /* If the machine needs to modify the insn being conditionally executed,
422 say for example to force a constant integer operand into a temp
423 register, do so here. */
424 #ifdef IFCVT_MODIFY_INSN
425 IFCVT_MODIFY_INSN (ce_info, pattern, insn);
426 if (! pattern)
427 return FALSE;
428 #endif
430 validate_change (insn, &PATTERN (insn), pattern, 1);
432 if (CALL_P (insn) && prob_val >= 0)
433 validate_change (insn, &REG_NOTES (insn),
434 gen_rtx_INT_LIST ((machine_mode) REG_BR_PROB,
435 prob_val, REG_NOTES (insn)), 1);
437 insn_done:
438 if (insn == end)
439 break;
442 return TRUE;
445 /* Return the condition for a jump. Do not do any special processing. */
447 static rtx
448 cond_exec_get_condition (rtx_insn *jump)
450 rtx test_if, cond;
452 if (any_condjump_p (jump))
453 test_if = SET_SRC (pc_set (jump));
454 else
455 return NULL_RTX;
456 cond = XEXP (test_if, 0);
458 /* If this branches to JUMP_LABEL when the condition is false,
459 reverse the condition. */
460 if (GET_CODE (XEXP (test_if, 2)) == LABEL_REF
461 && LABEL_REF_LABEL (XEXP (test_if, 2)) == JUMP_LABEL (jump))
463 enum rtx_code rev = reversed_comparison_code (cond, jump);
464 if (rev == UNKNOWN)
465 return NULL_RTX;
467 cond = gen_rtx_fmt_ee (rev, GET_MODE (cond), XEXP (cond, 0),
468 XEXP (cond, 1));
471 return cond;
474 /* Given a simple IF-THEN or IF-THEN-ELSE block, attempt to convert it
475 to conditional execution. Return TRUE if we were successful at
476 converting the block. */
478 static int
479 cond_exec_process_if_block (ce_if_block * ce_info,
480 /* if block information */int do_multiple_p)
482 basic_block test_bb = ce_info->test_bb; /* last test block */
483 basic_block then_bb = ce_info->then_bb; /* THEN */
484 basic_block else_bb = ce_info->else_bb; /* ELSE or NULL */
485 rtx test_expr; /* expression in IF_THEN_ELSE that is tested */
486 rtx_insn *then_start; /* first insn in THEN block */
487 rtx_insn *then_end; /* last insn + 1 in THEN block */
488 rtx_insn *else_start = NULL; /* first insn in ELSE block or NULL */
489 rtx_insn *else_end = NULL; /* last insn + 1 in ELSE block */
490 int max; /* max # of insns to convert. */
491 int then_mod_ok; /* whether conditional mods are ok in THEN */
492 rtx true_expr; /* test for else block insns */
493 rtx false_expr; /* test for then block insns */
494 int true_prob_val; /* probability of else block */
495 int false_prob_val; /* probability of then block */
496 rtx_insn *then_last_head = NULL; /* Last match at the head of THEN */
497 rtx_insn *else_last_head = NULL; /* Last match at the head of ELSE */
498 rtx_insn *then_first_tail = NULL; /* First match at the tail of THEN */
499 rtx_insn *else_first_tail = NULL; /* First match at the tail of ELSE */
500 int then_n_insns, else_n_insns, n_insns;
501 enum rtx_code false_code;
502 rtx note;
504 /* If test is comprised of && or || elements, and we've failed at handling
505 all of them together, just use the last test if it is the special case of
506 && elements without an ELSE block. */
507 if (!do_multiple_p && ce_info->num_multiple_test_blocks)
509 if (else_bb || ! ce_info->and_and_p)
510 return FALSE;
512 ce_info->test_bb = test_bb = ce_info->last_test_bb;
513 ce_info->num_multiple_test_blocks = 0;
514 ce_info->num_and_and_blocks = 0;
515 ce_info->num_or_or_blocks = 0;
518 /* Find the conditional jump to the ELSE or JOIN part, and isolate
519 the test. */
520 test_expr = cond_exec_get_condition (BB_END (test_bb));
521 if (! test_expr)
522 return FALSE;
524 /* If the conditional jump is more than just a conditional jump,
525 then we can not do conditional execution conversion on this block. */
526 if (! onlyjump_p (BB_END (test_bb)))
527 return FALSE;
529 /* Collect the bounds of where we're to search, skipping any labels, jumps
530 and notes at the beginning and end of the block. Then count the total
531 number of insns and see if it is small enough to convert. */
532 then_start = first_active_insn (then_bb);
533 then_end = last_active_insn (then_bb, TRUE);
534 then_n_insns = ce_info->num_then_insns = count_bb_insns (then_bb);
535 n_insns = then_n_insns;
536 max = MAX_CONDITIONAL_EXECUTE;
538 if (else_bb)
540 int n_matching;
542 max *= 2;
543 else_start = first_active_insn (else_bb);
544 else_end = last_active_insn (else_bb, TRUE);
545 else_n_insns = ce_info->num_else_insns = count_bb_insns (else_bb);
546 n_insns += else_n_insns;
548 /* Look for matching sequences at the head and tail of the two blocks,
549 and limit the range of insns to be converted if possible. */
550 n_matching = flow_find_cross_jump (then_bb, else_bb,
551 &then_first_tail, &else_first_tail,
552 NULL);
553 if (then_first_tail == BB_HEAD (then_bb))
554 then_start = then_end = NULL;
555 if (else_first_tail == BB_HEAD (else_bb))
556 else_start = else_end = NULL;
558 if (n_matching > 0)
560 if (then_end)
561 then_end = find_active_insn_before (then_bb, then_first_tail);
562 if (else_end)
563 else_end = find_active_insn_before (else_bb, else_first_tail);
564 n_insns -= 2 * n_matching;
567 if (then_start
568 && else_start
569 && then_n_insns > n_matching
570 && else_n_insns > n_matching)
572 int longest_match = MIN (then_n_insns - n_matching,
573 else_n_insns - n_matching);
574 n_matching
575 = flow_find_head_matching_sequence (then_bb, else_bb,
576 &then_last_head,
577 &else_last_head,
578 longest_match);
580 if (n_matching > 0)
582 rtx_insn *insn;
584 /* We won't pass the insns in the head sequence to
585 cond_exec_process_insns, so we need to test them here
586 to make sure that they don't clobber the condition. */
587 for (insn = BB_HEAD (then_bb);
588 insn != NEXT_INSN (then_last_head);
589 insn = NEXT_INSN (insn))
590 if (!LABEL_P (insn) && !NOTE_P (insn)
591 && !DEBUG_INSN_P (insn)
592 && modified_in_p (test_expr, insn))
593 return FALSE;
596 if (then_last_head == then_end)
597 then_start = then_end = NULL;
598 if (else_last_head == else_end)
599 else_start = else_end = NULL;
601 if (n_matching > 0)
603 if (then_start)
604 then_start = find_active_insn_after (then_bb, then_last_head);
605 if (else_start)
606 else_start = find_active_insn_after (else_bb, else_last_head);
607 n_insns -= 2 * n_matching;
612 if (n_insns > max)
613 return FALSE;
615 /* Map test_expr/test_jump into the appropriate MD tests to use on
616 the conditionally executed code. */
618 true_expr = test_expr;
620 false_code = reversed_comparison_code (true_expr, BB_END (test_bb));
621 if (false_code != UNKNOWN)
622 false_expr = gen_rtx_fmt_ee (false_code, GET_MODE (true_expr),
623 XEXP (true_expr, 0), XEXP (true_expr, 1));
624 else
625 false_expr = NULL_RTX;
627 #ifdef IFCVT_MODIFY_TESTS
628 /* If the machine description needs to modify the tests, such as setting a
629 conditional execution register from a comparison, it can do so here. */
630 IFCVT_MODIFY_TESTS (ce_info, true_expr, false_expr);
632 /* See if the conversion failed. */
633 if (!true_expr || !false_expr)
634 goto fail;
635 #endif
637 note = find_reg_note (BB_END (test_bb), REG_BR_PROB, NULL_RTX);
638 if (note)
640 true_prob_val = XINT (note, 0);
641 false_prob_val = REG_BR_PROB_BASE - true_prob_val;
643 else
645 true_prob_val = -1;
646 false_prob_val = -1;
649 /* If we have && or || tests, do them here. These tests are in the adjacent
650 blocks after the first block containing the test. */
651 if (ce_info->num_multiple_test_blocks > 0)
653 basic_block bb = test_bb;
654 basic_block last_test_bb = ce_info->last_test_bb;
656 if (! false_expr)
657 goto fail;
661 rtx_insn *start, *end;
662 rtx t, f;
663 enum rtx_code f_code;
665 bb = block_fallthru (bb);
666 start = first_active_insn (bb);
667 end = last_active_insn (bb, TRUE);
668 if (start
669 && ! cond_exec_process_insns (ce_info, start, end, false_expr,
670 false_prob_val, FALSE))
671 goto fail;
673 /* If the conditional jump is more than just a conditional jump, then
674 we can not do conditional execution conversion on this block. */
675 if (! onlyjump_p (BB_END (bb)))
676 goto fail;
678 /* Find the conditional jump and isolate the test. */
679 t = cond_exec_get_condition (BB_END (bb));
680 if (! t)
681 goto fail;
683 f_code = reversed_comparison_code (t, BB_END (bb));
684 if (f_code == UNKNOWN)
685 goto fail;
687 f = gen_rtx_fmt_ee (f_code, GET_MODE (t), XEXP (t, 0), XEXP (t, 1));
688 if (ce_info->and_and_p)
690 t = gen_rtx_AND (GET_MODE (t), true_expr, t);
691 f = gen_rtx_IOR (GET_MODE (t), false_expr, f);
693 else
695 t = gen_rtx_IOR (GET_MODE (t), true_expr, t);
696 f = gen_rtx_AND (GET_MODE (t), false_expr, f);
699 /* If the machine description needs to modify the tests, such as
700 setting a conditional execution register from a comparison, it can
701 do so here. */
702 #ifdef IFCVT_MODIFY_MULTIPLE_TESTS
703 IFCVT_MODIFY_MULTIPLE_TESTS (ce_info, bb, t, f);
705 /* See if the conversion failed. */
706 if (!t || !f)
707 goto fail;
708 #endif
710 true_expr = t;
711 false_expr = f;
713 while (bb != last_test_bb);
716 /* For IF-THEN-ELSE blocks, we don't allow modifications of the test
717 on then THEN block. */
718 then_mod_ok = (else_bb == NULL_BLOCK);
720 /* Go through the THEN and ELSE blocks converting the insns if possible
721 to conditional execution. */
723 if (then_end
724 && (! false_expr
725 || ! cond_exec_process_insns (ce_info, then_start, then_end,
726 false_expr, false_prob_val,
727 then_mod_ok)))
728 goto fail;
730 if (else_bb && else_end
731 && ! cond_exec_process_insns (ce_info, else_start, else_end,
732 true_expr, true_prob_val, TRUE))
733 goto fail;
735 /* If we cannot apply the changes, fail. Do not go through the normal fail
736 processing, since apply_change_group will call cancel_changes. */
737 if (! apply_change_group ())
739 #ifdef IFCVT_MODIFY_CANCEL
740 /* Cancel any machine dependent changes. */
741 IFCVT_MODIFY_CANCEL (ce_info);
742 #endif
743 return FALSE;
746 #ifdef IFCVT_MODIFY_FINAL
747 /* Do any machine dependent final modifications. */
748 IFCVT_MODIFY_FINAL (ce_info);
749 #endif
751 /* Conversion succeeded. */
752 if (dump_file)
753 fprintf (dump_file, "%d insn%s converted to conditional execution.\n",
754 n_insns, (n_insns == 1) ? " was" : "s were");
756 /* Merge the blocks! If we had matching sequences, make sure to delete one
757 copy at the appropriate location first: delete the copy in the THEN branch
758 for a tail sequence so that the remaining one is executed last for both
759 branches, and delete the copy in the ELSE branch for a head sequence so
760 that the remaining one is executed first for both branches. */
761 if (then_first_tail)
763 rtx_insn *from = then_first_tail;
764 if (!INSN_P (from))
765 from = find_active_insn_after (then_bb, from);
766 delete_insn_chain (from, BB_END (then_bb), false);
768 if (else_last_head)
769 delete_insn_chain (first_active_insn (else_bb), else_last_head, false);
771 merge_if_block (ce_info);
772 cond_exec_changed_p = TRUE;
773 return TRUE;
775 fail:
776 #ifdef IFCVT_MODIFY_CANCEL
777 /* Cancel any machine dependent changes. */
778 IFCVT_MODIFY_CANCEL (ce_info);
779 #endif
781 cancel_changes (0);
782 return FALSE;
785 /* Used by noce_process_if_block to communicate with its subroutines.
787 The subroutines know that A and B may be evaluated freely. They
788 know that X is a register. They should insert new instructions
789 before cond_earliest. */
791 struct noce_if_info
793 /* The basic blocks that make up the IF-THEN-{ELSE-,}JOIN block. */
794 basic_block test_bb, then_bb, else_bb, join_bb;
796 /* The jump that ends TEST_BB. */
797 rtx_insn *jump;
799 /* The jump condition. */
800 rtx cond;
802 /* New insns should be inserted before this one. */
803 rtx_insn *cond_earliest;
805 /* Insns in the THEN and ELSE block. There is always just this
806 one insns in those blocks. The insns are single_set insns.
807 If there was no ELSE block, INSN_B is the last insn before
808 COND_EARLIEST, or NULL_RTX. In the former case, the insn
809 operands are still valid, as if INSN_B was moved down below
810 the jump. */
811 rtx_insn *insn_a, *insn_b;
813 /* The SET_SRC of INSN_A and INSN_B. */
814 rtx a, b;
816 /* The SET_DEST of INSN_A. */
817 rtx x;
819 /* True if this if block is not canonical. In the canonical form of
820 if blocks, the THEN_BB is the block reached via the fallthru edge
821 from TEST_BB. For the noce transformations, we allow the symmetric
822 form as well. */
823 bool then_else_reversed;
825 /* Estimated cost of the particular branch instruction. */
826 int branch_cost;
829 static rtx noce_emit_store_flag (struct noce_if_info *, rtx, int, int);
830 static int noce_try_move (struct noce_if_info *);
831 static int noce_try_store_flag (struct noce_if_info *);
832 static int noce_try_addcc (struct noce_if_info *);
833 static int noce_try_store_flag_constants (struct noce_if_info *);
834 static int noce_try_store_flag_mask (struct noce_if_info *);
835 static rtx noce_emit_cmove (struct noce_if_info *, rtx, enum rtx_code, rtx,
836 rtx, rtx, rtx);
837 static int noce_try_cmove (struct noce_if_info *);
838 static int noce_try_cmove_arith (struct noce_if_info *);
839 static rtx noce_get_alt_condition (struct noce_if_info *, rtx, rtx_insn **);
840 static int noce_try_minmax (struct noce_if_info *);
841 static int noce_try_abs (struct noce_if_info *);
842 static int noce_try_sign_mask (struct noce_if_info *);
844 /* Helper function for noce_try_store_flag*. */
846 static rtx
847 noce_emit_store_flag (struct noce_if_info *if_info, rtx x, int reversep,
848 int normalize)
850 rtx cond = if_info->cond;
851 int cond_complex;
852 enum rtx_code code;
854 cond_complex = (! general_operand (XEXP (cond, 0), VOIDmode)
855 || ! general_operand (XEXP (cond, 1), VOIDmode));
857 /* If earliest == jump, or when the condition is complex, try to
858 build the store_flag insn directly. */
860 if (cond_complex)
862 rtx set = pc_set (if_info->jump);
863 cond = XEXP (SET_SRC (set), 0);
864 if (GET_CODE (XEXP (SET_SRC (set), 2)) == LABEL_REF
865 && LABEL_REF_LABEL (XEXP (SET_SRC (set), 2)) == JUMP_LABEL (if_info->jump))
866 reversep = !reversep;
867 if (if_info->then_else_reversed)
868 reversep = !reversep;
871 if (reversep)
872 code = reversed_comparison_code (cond, if_info->jump);
873 else
874 code = GET_CODE (cond);
876 if ((if_info->cond_earliest == if_info->jump || cond_complex)
877 && (normalize == 0 || STORE_FLAG_VALUE == normalize))
879 rtx src = gen_rtx_fmt_ee (code, GET_MODE (x), XEXP (cond, 0),
880 XEXP (cond, 1));
881 rtx set = gen_rtx_SET (x, src);
883 start_sequence ();
884 rtx_insn *insn = emit_insn (set);
886 if (recog_memoized (insn) >= 0)
888 rtx_insn *seq = get_insns ();
889 end_sequence ();
890 emit_insn (seq);
892 if_info->cond_earliest = if_info->jump;
894 return x;
897 end_sequence ();
900 /* Don't even try if the comparison operands or the mode of X are weird. */
901 if (cond_complex || !SCALAR_INT_MODE_P (GET_MODE (x)))
902 return NULL_RTX;
904 return emit_store_flag (x, code, XEXP (cond, 0),
905 XEXP (cond, 1), VOIDmode,
906 (code == LTU || code == LEU
907 || code == GEU || code == GTU), normalize);
910 /* Emit instruction to move an rtx, possibly into STRICT_LOW_PART.
911 X is the destination/target and Y is the value to copy. */
913 static void
914 noce_emit_move_insn (rtx x, rtx y)
916 machine_mode outmode;
917 rtx outer, inner;
918 int bitpos;
920 if (GET_CODE (x) != STRICT_LOW_PART)
922 rtx_insn *seq, *insn;
923 rtx target;
924 optab ot;
926 start_sequence ();
927 /* Check that the SET_SRC is reasonable before calling emit_move_insn,
928 otherwise construct a suitable SET pattern ourselves. */
929 insn = (OBJECT_P (y) || CONSTANT_P (y) || GET_CODE (y) == SUBREG)
930 ? emit_move_insn (x, y)
931 : emit_insn (gen_rtx_SET (x, y));
932 seq = get_insns ();
933 end_sequence ();
935 if (recog_memoized (insn) <= 0)
937 if (GET_CODE (x) == ZERO_EXTRACT)
939 rtx op = XEXP (x, 0);
940 unsigned HOST_WIDE_INT size = INTVAL (XEXP (x, 1));
941 unsigned HOST_WIDE_INT start = INTVAL (XEXP (x, 2));
943 /* store_bit_field expects START to be relative to
944 BYTES_BIG_ENDIAN and adjusts this value for machines with
945 BITS_BIG_ENDIAN != BYTES_BIG_ENDIAN. In order to be able to
946 invoke store_bit_field again it is necessary to have the START
947 value from the first call. */
948 if (BITS_BIG_ENDIAN != BYTES_BIG_ENDIAN)
950 if (MEM_P (op))
951 start = BITS_PER_UNIT - start - size;
952 else
954 gcc_assert (REG_P (op));
955 start = BITS_PER_WORD - start - size;
959 gcc_assert (start < (MEM_P (op) ? BITS_PER_UNIT : BITS_PER_WORD));
960 store_bit_field (op, size, start, 0, 0, GET_MODE (x), y);
961 return;
964 switch (GET_RTX_CLASS (GET_CODE (y)))
966 case RTX_UNARY:
967 ot = code_to_optab (GET_CODE (y));
968 if (ot)
970 start_sequence ();
971 target = expand_unop (GET_MODE (y), ot, XEXP (y, 0), x, 0);
972 if (target != NULL_RTX)
974 if (target != x)
975 emit_move_insn (x, target);
976 seq = get_insns ();
978 end_sequence ();
980 break;
982 case RTX_BIN_ARITH:
983 case RTX_COMM_ARITH:
984 ot = code_to_optab (GET_CODE (y));
985 if (ot)
987 start_sequence ();
988 target = expand_binop (GET_MODE (y), ot,
989 XEXP (y, 0), XEXP (y, 1),
990 x, 0, OPTAB_DIRECT);
991 if (target != NULL_RTX)
993 if (target != x)
994 emit_move_insn (x, target);
995 seq = get_insns ();
997 end_sequence ();
999 break;
1001 default:
1002 break;
1006 emit_insn (seq);
1007 return;
1010 outer = XEXP (x, 0);
1011 inner = XEXP (outer, 0);
1012 outmode = GET_MODE (outer);
1013 bitpos = SUBREG_BYTE (outer) * BITS_PER_UNIT;
1014 store_bit_field (inner, GET_MODE_BITSIZE (outmode), bitpos,
1015 0, 0, outmode, y);
1018 /* Return the CC reg if it is used in COND. */
1020 static rtx
1021 cc_in_cond (rtx cond)
1023 if (HAVE_cbranchcc4 && cond
1024 && GET_MODE_CLASS (GET_MODE (XEXP (cond, 0))) == MODE_CC)
1025 return XEXP (cond, 0);
1027 return NULL_RTX;
1030 /* Return sequence of instructions generated by if conversion. This
1031 function calls end_sequence() to end the current stream, ensures
1032 that the instructions are unshared, recognizable non-jump insns.
1033 On failure, this function returns a NULL_RTX. */
1035 static rtx_insn *
1036 end_ifcvt_sequence (struct noce_if_info *if_info)
1038 rtx_insn *insn;
1039 rtx_insn *seq = get_insns ();
1040 rtx cc = cc_in_cond (if_info->cond);
1042 set_used_flags (if_info->x);
1043 set_used_flags (if_info->cond);
1044 set_used_flags (if_info->a);
1045 set_used_flags (if_info->b);
1046 unshare_all_rtl_in_chain (seq);
1047 end_sequence ();
1049 /* Make sure that all of the instructions emitted are recognizable,
1050 and that we haven't introduced a new jump instruction.
1051 As an exercise for the reader, build a general mechanism that
1052 allows proper placement of required clobbers. */
1053 for (insn = seq; insn; insn = NEXT_INSN (insn))
1054 if (JUMP_P (insn)
1055 || recog_memoized (insn) == -1
1056 /* Make sure new generated code does not clobber CC. */
1057 || (cc && set_of (cc, insn)))
1058 return NULL;
1060 return seq;
1063 /* Convert "if (a != b) x = a; else x = b" into "x = a" and
1064 "if (a == b) x = a; else x = b" into "x = b". */
1066 static int
1067 noce_try_move (struct noce_if_info *if_info)
1069 rtx cond = if_info->cond;
1070 enum rtx_code code = GET_CODE (cond);
1071 rtx y;
1072 rtx_insn *seq;
1074 if (code != NE && code != EQ)
1075 return FALSE;
1077 /* This optimization isn't valid if either A or B could be a NaN
1078 or a signed zero. */
1079 if (HONOR_NANS (if_info->x)
1080 || HONOR_SIGNED_ZEROS (if_info->x))
1081 return FALSE;
1083 /* Check whether the operands of the comparison are A and in
1084 either order. */
1085 if ((rtx_equal_p (if_info->a, XEXP (cond, 0))
1086 && rtx_equal_p (if_info->b, XEXP (cond, 1)))
1087 || (rtx_equal_p (if_info->a, XEXP (cond, 1))
1088 && rtx_equal_p (if_info->b, XEXP (cond, 0))))
1090 if (!rtx_interchangeable_p (if_info->a, if_info->b))
1091 return FALSE;
1093 y = (code == EQ) ? if_info->a : if_info->b;
1095 /* Avoid generating the move if the source is the destination. */
1096 if (! rtx_equal_p (if_info->x, y))
1098 start_sequence ();
1099 noce_emit_move_insn (if_info->x, y);
1100 seq = end_ifcvt_sequence (if_info);
1101 if (!seq)
1102 return FALSE;
1104 emit_insn_before_setloc (seq, if_info->jump,
1105 INSN_LOCATION (if_info->insn_a));
1107 return TRUE;
1109 return FALSE;
1112 /* Convert "if (test) x = 1; else x = 0".
1114 Only try 0 and STORE_FLAG_VALUE here. Other combinations will be
1115 tried in noce_try_store_flag_constants after noce_try_cmove has had
1116 a go at the conversion. */
1118 static int
1119 noce_try_store_flag (struct noce_if_info *if_info)
1121 int reversep;
1122 rtx target;
1123 rtx_insn *seq;
1125 if (CONST_INT_P (if_info->b)
1126 && INTVAL (if_info->b) == STORE_FLAG_VALUE
1127 && if_info->a == const0_rtx)
1128 reversep = 0;
1129 else if (if_info->b == const0_rtx
1130 && CONST_INT_P (if_info->a)
1131 && INTVAL (if_info->a) == STORE_FLAG_VALUE
1132 && (reversed_comparison_code (if_info->cond, if_info->jump)
1133 != UNKNOWN))
1134 reversep = 1;
1135 else
1136 return FALSE;
1138 start_sequence ();
1140 target = noce_emit_store_flag (if_info, if_info->x, reversep, 0);
1141 if (target)
1143 if (target != if_info->x)
1144 noce_emit_move_insn (if_info->x, target);
1146 seq = end_ifcvt_sequence (if_info);
1147 if (! seq)
1148 return FALSE;
1150 emit_insn_before_setloc (seq, if_info->jump,
1151 INSN_LOCATION (if_info->insn_a));
1152 return TRUE;
1154 else
1156 end_sequence ();
1157 return FALSE;
1161 /* Convert "if (test) x = a; else x = b", for A and B constant. */
1163 static int
1164 noce_try_store_flag_constants (struct noce_if_info *if_info)
1166 rtx target;
1167 rtx_insn *seq;
1168 int reversep;
1169 HOST_WIDE_INT itrue, ifalse, diff, tmp;
1170 int normalize, can_reverse;
1171 machine_mode mode;
1173 if (CONST_INT_P (if_info->a)
1174 && CONST_INT_P (if_info->b))
1176 mode = GET_MODE (if_info->x);
1177 ifalse = INTVAL (if_info->a);
1178 itrue = INTVAL (if_info->b);
1180 diff = (unsigned HOST_WIDE_INT) itrue - ifalse;
1181 /* Make sure we can represent the difference between the two values. */
1182 if ((diff > 0)
1183 != ((ifalse < 0) != (itrue < 0) ? ifalse < 0 : ifalse < itrue))
1184 return FALSE;
1186 diff = trunc_int_for_mode (diff, mode);
1188 can_reverse = (reversed_comparison_code (if_info->cond, if_info->jump)
1189 != UNKNOWN);
1191 reversep = 0;
1192 if (diff == STORE_FLAG_VALUE || diff == -STORE_FLAG_VALUE)
1193 normalize = 0;
1194 else if (ifalse == 0 && exact_log2 (itrue) >= 0
1195 && (STORE_FLAG_VALUE == 1
1196 || if_info->branch_cost >= 2))
1197 normalize = 1;
1198 else if (itrue == 0 && exact_log2 (ifalse) >= 0 && can_reverse
1199 && (STORE_FLAG_VALUE == 1 || if_info->branch_cost >= 2))
1200 normalize = 1, reversep = 1;
1201 else if (itrue == -1
1202 && (STORE_FLAG_VALUE == -1
1203 || if_info->branch_cost >= 2))
1204 normalize = -1;
1205 else if (ifalse == -1 && can_reverse
1206 && (STORE_FLAG_VALUE == -1 || if_info->branch_cost >= 2))
1207 normalize = -1, reversep = 1;
1208 else if ((if_info->branch_cost >= 2 && STORE_FLAG_VALUE == -1)
1209 || if_info->branch_cost >= 3)
1210 normalize = -1;
1211 else
1212 return FALSE;
1214 if (reversep)
1216 std::swap (itrue, ifalse);
1217 diff = trunc_int_for_mode (-(unsigned HOST_WIDE_INT) diff, mode);
1220 start_sequence ();
1221 target = noce_emit_store_flag (if_info, if_info->x, reversep, normalize);
1222 if (! target)
1224 end_sequence ();
1225 return FALSE;
1228 /* if (test) x = 3; else x = 4;
1229 => x = 3 + (test == 0); */
1230 if (diff == STORE_FLAG_VALUE || diff == -STORE_FLAG_VALUE)
1232 target = expand_simple_binop (mode,
1233 (diff == STORE_FLAG_VALUE
1234 ? PLUS : MINUS),
1235 gen_int_mode (ifalse, mode), target,
1236 if_info->x, 0, OPTAB_WIDEN);
1239 /* if (test) x = 8; else x = 0;
1240 => x = (test != 0) << 3; */
1241 else if (ifalse == 0 && (tmp = exact_log2 (itrue)) >= 0)
1243 target = expand_simple_binop (mode, ASHIFT,
1244 target, GEN_INT (tmp), if_info->x, 0,
1245 OPTAB_WIDEN);
1248 /* if (test) x = -1; else x = b;
1249 => x = -(test != 0) | b; */
1250 else if (itrue == -1)
1252 target = expand_simple_binop (mode, IOR,
1253 target, gen_int_mode (ifalse, mode),
1254 if_info->x, 0, OPTAB_WIDEN);
1257 /* if (test) x = a; else x = b;
1258 => x = (-(test != 0) & (b - a)) + a; */
1259 else
1261 target = expand_simple_binop (mode, AND,
1262 target, gen_int_mode (diff, mode),
1263 if_info->x, 0, OPTAB_WIDEN);
1264 if (target)
1265 target = expand_simple_binop (mode, PLUS,
1266 target, gen_int_mode (ifalse, mode),
1267 if_info->x, 0, OPTAB_WIDEN);
1270 if (! target)
1272 end_sequence ();
1273 return FALSE;
1276 if (target != if_info->x)
1277 noce_emit_move_insn (if_info->x, target);
1279 seq = end_ifcvt_sequence (if_info);
1280 if (!seq)
1281 return FALSE;
1283 emit_insn_before_setloc (seq, if_info->jump,
1284 INSN_LOCATION (if_info->insn_a));
1285 return TRUE;
1288 return FALSE;
1291 /* Convert "if (test) foo++" into "foo += (test != 0)", and
1292 similarly for "foo--". */
1294 static int
1295 noce_try_addcc (struct noce_if_info *if_info)
1297 rtx target;
1298 rtx_insn *seq;
1299 int subtract, normalize;
1301 if (GET_CODE (if_info->a) == PLUS
1302 && rtx_equal_p (XEXP (if_info->a, 0), if_info->b)
1303 && (reversed_comparison_code (if_info->cond, if_info->jump)
1304 != UNKNOWN))
1306 rtx cond = if_info->cond;
1307 enum rtx_code code = reversed_comparison_code (cond, if_info->jump);
1309 /* First try to use addcc pattern. */
1310 if (general_operand (XEXP (cond, 0), VOIDmode)
1311 && general_operand (XEXP (cond, 1), VOIDmode))
1313 start_sequence ();
1314 target = emit_conditional_add (if_info->x, code,
1315 XEXP (cond, 0),
1316 XEXP (cond, 1),
1317 VOIDmode,
1318 if_info->b,
1319 XEXP (if_info->a, 1),
1320 GET_MODE (if_info->x),
1321 (code == LTU || code == GEU
1322 || code == LEU || code == GTU));
1323 if (target)
1325 if (target != if_info->x)
1326 noce_emit_move_insn (if_info->x, target);
1328 seq = end_ifcvt_sequence (if_info);
1329 if (!seq)
1330 return FALSE;
1332 emit_insn_before_setloc (seq, if_info->jump,
1333 INSN_LOCATION (if_info->insn_a));
1334 return TRUE;
1336 end_sequence ();
1339 /* If that fails, construct conditional increment or decrement using
1340 setcc. */
1341 if (if_info->branch_cost >= 2
1342 && (XEXP (if_info->a, 1) == const1_rtx
1343 || XEXP (if_info->a, 1) == constm1_rtx))
1345 start_sequence ();
1346 if (STORE_FLAG_VALUE == INTVAL (XEXP (if_info->a, 1)))
1347 subtract = 0, normalize = 0;
1348 else if (-STORE_FLAG_VALUE == INTVAL (XEXP (if_info->a, 1)))
1349 subtract = 1, normalize = 0;
1350 else
1351 subtract = 0, normalize = INTVAL (XEXP (if_info->a, 1));
1354 target = noce_emit_store_flag (if_info,
1355 gen_reg_rtx (GET_MODE (if_info->x)),
1356 1, normalize);
1358 if (target)
1359 target = expand_simple_binop (GET_MODE (if_info->x),
1360 subtract ? MINUS : PLUS,
1361 if_info->b, target, if_info->x,
1362 0, OPTAB_WIDEN);
1363 if (target)
1365 if (target != if_info->x)
1366 noce_emit_move_insn (if_info->x, target);
1368 seq = end_ifcvt_sequence (if_info);
1369 if (!seq)
1370 return FALSE;
1372 emit_insn_before_setloc (seq, if_info->jump,
1373 INSN_LOCATION (if_info->insn_a));
1374 return TRUE;
1376 end_sequence ();
1380 return FALSE;
1383 /* Convert "if (test) x = 0;" to "x &= -(test == 0);" */
1385 static int
1386 noce_try_store_flag_mask (struct noce_if_info *if_info)
1388 rtx target;
1389 rtx_insn *seq;
1390 int reversep;
1392 reversep = 0;
1393 if ((if_info->branch_cost >= 2
1394 || STORE_FLAG_VALUE == -1)
1395 && ((if_info->a == const0_rtx
1396 && rtx_equal_p (if_info->b, if_info->x))
1397 || ((reversep = (reversed_comparison_code (if_info->cond,
1398 if_info->jump)
1399 != UNKNOWN))
1400 && if_info->b == const0_rtx
1401 && rtx_equal_p (if_info->a, if_info->x))))
1403 start_sequence ();
1404 target = noce_emit_store_flag (if_info,
1405 gen_reg_rtx (GET_MODE (if_info->x)),
1406 reversep, -1);
1407 if (target)
1408 target = expand_simple_binop (GET_MODE (if_info->x), AND,
1409 if_info->x,
1410 target, if_info->x, 0,
1411 OPTAB_WIDEN);
1413 if (target)
1415 int old_cost, new_cost, insn_cost;
1416 int speed_p;
1418 if (target != if_info->x)
1419 noce_emit_move_insn (if_info->x, target);
1421 seq = end_ifcvt_sequence (if_info);
1422 if (!seq)
1423 return FALSE;
1425 speed_p = optimize_bb_for_speed_p (BLOCK_FOR_INSN (if_info->insn_a));
1426 insn_cost = insn_rtx_cost (PATTERN (if_info->insn_a), speed_p);
1427 old_cost = COSTS_N_INSNS (if_info->branch_cost) + insn_cost;
1428 new_cost = seq_cost (seq, speed_p);
1430 if (new_cost > old_cost)
1431 return FALSE;
1433 emit_insn_before_setloc (seq, if_info->jump,
1434 INSN_LOCATION (if_info->insn_a));
1435 return TRUE;
1438 end_sequence ();
1441 return FALSE;
1444 /* Helper function for noce_try_cmove and noce_try_cmove_arith. */
1446 static rtx
1447 noce_emit_cmove (struct noce_if_info *if_info, rtx x, enum rtx_code code,
1448 rtx cmp_a, rtx cmp_b, rtx vfalse, rtx vtrue)
1450 rtx target ATTRIBUTE_UNUSED;
1451 int unsignedp ATTRIBUTE_UNUSED;
1453 /* If earliest == jump, try to build the cmove insn directly.
1454 This is helpful when combine has created some complex condition
1455 (like for alpha's cmovlbs) that we can't hope to regenerate
1456 through the normal interface. */
1458 if (if_info->cond_earliest == if_info->jump)
1460 rtx cond = gen_rtx_fmt_ee (code, GET_MODE (if_info->cond), cmp_a, cmp_b);
1461 rtx if_then_else = gen_rtx_IF_THEN_ELSE (GET_MODE (x),
1462 cond, vtrue, vfalse);
1463 rtx set = gen_rtx_SET (x, if_then_else);
1465 start_sequence ();
1466 rtx_insn *insn = emit_insn (set);
1468 if (recog_memoized (insn) >= 0)
1470 rtx_insn *seq = get_insns ();
1471 end_sequence ();
1472 emit_insn (seq);
1474 return x;
1477 end_sequence ();
1480 /* Don't even try if the comparison operands are weird
1481 except that the target supports cbranchcc4. */
1482 if (! general_operand (cmp_a, GET_MODE (cmp_a))
1483 || ! general_operand (cmp_b, GET_MODE (cmp_b)))
1485 if (!(HAVE_cbranchcc4)
1486 || GET_MODE_CLASS (GET_MODE (cmp_a)) != MODE_CC
1487 || cmp_b != const0_rtx)
1488 return NULL_RTX;
1491 unsignedp = (code == LTU || code == GEU
1492 || code == LEU || code == GTU);
1494 target = emit_conditional_move (x, code, cmp_a, cmp_b, VOIDmode,
1495 vtrue, vfalse, GET_MODE (x),
1496 unsignedp);
1497 if (target)
1498 return target;
1500 /* We might be faced with a situation like:
1502 x = (reg:M TARGET)
1503 vtrue = (subreg:M (reg:N VTRUE) BYTE)
1504 vfalse = (subreg:M (reg:N VFALSE) BYTE)
1506 We can't do a conditional move in mode M, but it's possible that we
1507 could do a conditional move in mode N instead and take a subreg of
1508 the result.
1510 If we can't create new pseudos, though, don't bother. */
1511 if (reload_completed)
1512 return NULL_RTX;
1514 if (GET_CODE (vtrue) == SUBREG && GET_CODE (vfalse) == SUBREG)
1516 rtx reg_vtrue = SUBREG_REG (vtrue);
1517 rtx reg_vfalse = SUBREG_REG (vfalse);
1518 unsigned int byte_vtrue = SUBREG_BYTE (vtrue);
1519 unsigned int byte_vfalse = SUBREG_BYTE (vfalse);
1520 rtx promoted_target;
1522 if (GET_MODE (reg_vtrue) != GET_MODE (reg_vfalse)
1523 || byte_vtrue != byte_vfalse
1524 || (SUBREG_PROMOTED_VAR_P (vtrue)
1525 != SUBREG_PROMOTED_VAR_P (vfalse))
1526 || (SUBREG_PROMOTED_GET (vtrue)
1527 != SUBREG_PROMOTED_GET (vfalse)))
1528 return NULL_RTX;
1530 promoted_target = gen_reg_rtx (GET_MODE (reg_vtrue));
1532 target = emit_conditional_move (promoted_target, code, cmp_a, cmp_b,
1533 VOIDmode, reg_vtrue, reg_vfalse,
1534 GET_MODE (reg_vtrue), unsignedp);
1535 /* Nope, couldn't do it in that mode either. */
1536 if (!target)
1537 return NULL_RTX;
1539 target = gen_rtx_SUBREG (GET_MODE (vtrue), promoted_target, byte_vtrue);
1540 SUBREG_PROMOTED_VAR_P (target) = SUBREG_PROMOTED_VAR_P (vtrue);
1541 SUBREG_PROMOTED_SET (target, SUBREG_PROMOTED_GET (vtrue));
1542 emit_move_insn (x, target);
1543 return x;
1545 else
1546 return NULL_RTX;
1549 /* Try only simple constants and registers here. More complex cases
1550 are handled in noce_try_cmove_arith after noce_try_store_flag_arith
1551 has had a go at it. */
1553 static int
1554 noce_try_cmove (struct noce_if_info *if_info)
1556 enum rtx_code code;
1557 rtx target;
1558 rtx_insn *seq;
1560 if ((CONSTANT_P (if_info->a) || register_operand (if_info->a, VOIDmode))
1561 && (CONSTANT_P (if_info->b) || register_operand (if_info->b, VOIDmode)))
1563 start_sequence ();
1565 code = GET_CODE (if_info->cond);
1566 target = noce_emit_cmove (if_info, if_info->x, code,
1567 XEXP (if_info->cond, 0),
1568 XEXP (if_info->cond, 1),
1569 if_info->a, if_info->b);
1571 if (target)
1573 if (target != if_info->x)
1574 noce_emit_move_insn (if_info->x, target);
1576 seq = end_ifcvt_sequence (if_info);
1577 if (!seq)
1578 return FALSE;
1580 emit_insn_before_setloc (seq, if_info->jump,
1581 INSN_LOCATION (if_info->insn_a));
1582 return TRUE;
1584 else
1586 end_sequence ();
1587 return FALSE;
1591 return FALSE;
1594 /* Try more complex cases involving conditional_move. */
1596 static int
1597 noce_try_cmove_arith (struct noce_if_info *if_info)
1599 rtx a = if_info->a;
1600 rtx b = if_info->b;
1601 rtx x = if_info->x;
1602 rtx orig_a, orig_b;
1603 rtx_insn *insn_a, *insn_b;
1604 rtx target;
1605 int is_mem = 0;
1606 int insn_cost;
1607 enum rtx_code code;
1608 rtx_insn *ifcvt_seq;
1610 /* A conditional move from two memory sources is equivalent to a
1611 conditional on their addresses followed by a load. Don't do this
1612 early because it'll screw alias analysis. Note that we've
1613 already checked for no side effects. */
1614 /* ??? FIXME: Magic number 5. */
1615 if (cse_not_expected
1616 && MEM_P (a) && MEM_P (b)
1617 && MEM_ADDR_SPACE (a) == MEM_ADDR_SPACE (b)
1618 && if_info->branch_cost >= 5)
1620 machine_mode address_mode = get_address_mode (a);
1622 a = XEXP (a, 0);
1623 b = XEXP (b, 0);
1624 x = gen_reg_rtx (address_mode);
1625 is_mem = 1;
1628 /* ??? We could handle this if we knew that a load from A or B could
1629 not trap or fault. This is also true if we've already loaded
1630 from the address along the path from ENTRY. */
1631 else if (may_trap_or_fault_p (a) || may_trap_or_fault_p (b))
1632 return FALSE;
1634 /* if (test) x = a + b; else x = c - d;
1635 => y = a + b;
1636 x = c - d;
1637 if (test)
1638 x = y;
1641 code = GET_CODE (if_info->cond);
1642 insn_a = if_info->insn_a;
1643 insn_b = if_info->insn_b;
1645 /* Total insn_rtx_cost should be smaller than branch cost. Exit
1646 if insn_rtx_cost can't be estimated. */
1647 if (insn_a)
1649 insn_cost
1650 = insn_rtx_cost (PATTERN (insn_a),
1651 optimize_bb_for_speed_p (BLOCK_FOR_INSN (insn_a)));
1652 if (insn_cost == 0 || insn_cost > COSTS_N_INSNS (if_info->branch_cost))
1653 return FALSE;
1655 else
1656 insn_cost = 0;
1658 if (insn_b)
1660 insn_cost
1661 += insn_rtx_cost (PATTERN (insn_b),
1662 optimize_bb_for_speed_p (BLOCK_FOR_INSN (insn_b)));
1663 if (insn_cost == 0 || insn_cost > COSTS_N_INSNS (if_info->branch_cost))
1664 return FALSE;
1667 /* Possibly rearrange operands to make things come out more natural. */
1668 if (reversed_comparison_code (if_info->cond, if_info->jump) != UNKNOWN)
1670 int reversep = 0;
1671 if (rtx_equal_p (b, x))
1672 reversep = 1;
1673 else if (general_operand (b, GET_MODE (b)))
1674 reversep = 1;
1676 if (reversep)
1678 code = reversed_comparison_code (if_info->cond, if_info->jump);
1679 std::swap (a, b);
1680 std::swap (insn_a, insn_b);
1684 start_sequence ();
1686 orig_a = a;
1687 orig_b = b;
1689 /* If either operand is complex, load it into a register first.
1690 The best way to do this is to copy the original insn. In this
1691 way we preserve any clobbers etc that the insn may have had.
1692 This is of course not possible in the IS_MEM case. */
1693 if (! general_operand (a, GET_MODE (a)))
1695 rtx_insn *insn;
1697 if (is_mem)
1699 rtx reg = gen_reg_rtx (GET_MODE (a));
1700 insn = emit_insn (gen_rtx_SET (reg, a));
1702 else if (! insn_a)
1703 goto end_seq_and_fail;
1704 else
1706 a = gen_reg_rtx (GET_MODE (a));
1707 rtx_insn *copy_of_a = as_a <rtx_insn *> (copy_rtx (insn_a));
1708 rtx set = single_set (copy_of_a);
1709 SET_DEST (set) = a;
1710 insn = emit_insn (PATTERN (copy_of_a));
1712 if (recog_memoized (insn) < 0)
1713 goto end_seq_and_fail;
1715 if (! general_operand (b, GET_MODE (b)))
1717 rtx pat;
1718 rtx_insn *last;
1719 rtx_insn *new_insn;
1721 if (is_mem)
1723 rtx reg = gen_reg_rtx (GET_MODE (b));
1724 pat = gen_rtx_SET (reg, b);
1726 else if (! insn_b)
1727 goto end_seq_and_fail;
1728 else
1730 b = gen_reg_rtx (GET_MODE (b));
1731 rtx_insn *copy_of_insn_b = as_a <rtx_insn *> (copy_rtx (insn_b));
1732 rtx set = single_set (copy_of_insn_b);
1733 SET_DEST (set) = b;
1734 pat = PATTERN (copy_of_insn_b);
1737 /* If insn to set up A clobbers any registers B depends on, try to
1738 swap insn that sets up A with the one that sets up B. If even
1739 that doesn't help, punt. */
1740 last = get_last_insn ();
1741 if (last && modified_in_p (orig_b, last))
1743 new_insn = emit_insn_before (pat, get_insns ());
1744 if (modified_in_p (orig_a, new_insn))
1745 goto end_seq_and_fail;
1747 else
1748 new_insn = emit_insn (pat);
1750 if (recog_memoized (new_insn) < 0)
1751 goto end_seq_and_fail;
1754 target = noce_emit_cmove (if_info, x, code, XEXP (if_info->cond, 0),
1755 XEXP (if_info->cond, 1), a, b);
1757 if (! target)
1758 goto end_seq_and_fail;
1760 /* If we're handling a memory for above, emit the load now. */
1761 if (is_mem)
1763 rtx mem = gen_rtx_MEM (GET_MODE (if_info->x), target);
1765 /* Copy over flags as appropriate. */
1766 if (MEM_VOLATILE_P (if_info->a) || MEM_VOLATILE_P (if_info->b))
1767 MEM_VOLATILE_P (mem) = 1;
1768 if (MEM_ALIAS_SET (if_info->a) == MEM_ALIAS_SET (if_info->b))
1769 set_mem_alias_set (mem, MEM_ALIAS_SET (if_info->a));
1770 set_mem_align (mem,
1771 MIN (MEM_ALIGN (if_info->a), MEM_ALIGN (if_info->b)));
1773 gcc_assert (MEM_ADDR_SPACE (if_info->a) == MEM_ADDR_SPACE (if_info->b));
1774 set_mem_addr_space (mem, MEM_ADDR_SPACE (if_info->a));
1776 noce_emit_move_insn (if_info->x, mem);
1778 else if (target != x)
1779 noce_emit_move_insn (x, target);
1781 ifcvt_seq = end_ifcvt_sequence (if_info);
1782 if (!ifcvt_seq)
1783 return FALSE;
1785 emit_insn_before_setloc (ifcvt_seq, if_info->jump,
1786 INSN_LOCATION (if_info->insn_a));
1787 return TRUE;
1789 end_seq_and_fail:
1790 end_sequence ();
1791 return FALSE;
1794 /* For most cases, the simplified condition we found is the best
1795 choice, but this is not the case for the min/max/abs transforms.
1796 For these we wish to know that it is A or B in the condition. */
1798 static rtx
1799 noce_get_alt_condition (struct noce_if_info *if_info, rtx target,
1800 rtx_insn **earliest)
1802 rtx cond, set;
1803 rtx_insn *insn;
1804 int reverse;
1806 /* If target is already mentioned in the known condition, return it. */
1807 if (reg_mentioned_p (target, if_info->cond))
1809 *earliest = if_info->cond_earliest;
1810 return if_info->cond;
1813 set = pc_set (if_info->jump);
1814 cond = XEXP (SET_SRC (set), 0);
1815 reverse
1816 = GET_CODE (XEXP (SET_SRC (set), 2)) == LABEL_REF
1817 && LABEL_REF_LABEL (XEXP (SET_SRC (set), 2)) == JUMP_LABEL (if_info->jump);
1818 if (if_info->then_else_reversed)
1819 reverse = !reverse;
1821 /* If we're looking for a constant, try to make the conditional
1822 have that constant in it. There are two reasons why it may
1823 not have the constant we want:
1825 1. GCC may have needed to put the constant in a register, because
1826 the target can't compare directly against that constant. For
1827 this case, we look for a SET immediately before the comparison
1828 that puts a constant in that register.
1830 2. GCC may have canonicalized the conditional, for example
1831 replacing "if x < 4" with "if x <= 3". We can undo that (or
1832 make equivalent types of changes) to get the constants we need
1833 if they're off by one in the right direction. */
1835 if (CONST_INT_P (target))
1837 enum rtx_code code = GET_CODE (if_info->cond);
1838 rtx op_a = XEXP (if_info->cond, 0);
1839 rtx op_b = XEXP (if_info->cond, 1);
1840 rtx_insn *prev_insn;
1842 /* First, look to see if we put a constant in a register. */
1843 prev_insn = prev_nonnote_insn (if_info->cond_earliest);
1844 if (prev_insn
1845 && BLOCK_FOR_INSN (prev_insn)
1846 == BLOCK_FOR_INSN (if_info->cond_earliest)
1847 && INSN_P (prev_insn)
1848 && GET_CODE (PATTERN (prev_insn)) == SET)
1850 rtx src = find_reg_equal_equiv_note (prev_insn);
1851 if (!src)
1852 src = SET_SRC (PATTERN (prev_insn));
1853 if (CONST_INT_P (src))
1855 if (rtx_equal_p (op_a, SET_DEST (PATTERN (prev_insn))))
1856 op_a = src;
1857 else if (rtx_equal_p (op_b, SET_DEST (PATTERN (prev_insn))))
1858 op_b = src;
1860 if (CONST_INT_P (op_a))
1862 std::swap (op_a, op_b);
1863 code = swap_condition (code);
1868 /* Now, look to see if we can get the right constant by
1869 adjusting the conditional. */
1870 if (CONST_INT_P (op_b))
1872 HOST_WIDE_INT desired_val = INTVAL (target);
1873 HOST_WIDE_INT actual_val = INTVAL (op_b);
1875 switch (code)
1877 case LT:
1878 if (actual_val == desired_val + 1)
1880 code = LE;
1881 op_b = GEN_INT (desired_val);
1883 break;
1884 case LE:
1885 if (actual_val == desired_val - 1)
1887 code = LT;
1888 op_b = GEN_INT (desired_val);
1890 break;
1891 case GT:
1892 if (actual_val == desired_val - 1)
1894 code = GE;
1895 op_b = GEN_INT (desired_val);
1897 break;
1898 case GE:
1899 if (actual_val == desired_val + 1)
1901 code = GT;
1902 op_b = GEN_INT (desired_val);
1904 break;
1905 default:
1906 break;
1910 /* If we made any changes, generate a new conditional that is
1911 equivalent to what we started with, but has the right
1912 constants in it. */
1913 if (code != GET_CODE (if_info->cond)
1914 || op_a != XEXP (if_info->cond, 0)
1915 || op_b != XEXP (if_info->cond, 1))
1917 cond = gen_rtx_fmt_ee (code, GET_MODE (cond), op_a, op_b);
1918 *earliest = if_info->cond_earliest;
1919 return cond;
1923 cond = canonicalize_condition (if_info->jump, cond, reverse,
1924 earliest, target, HAVE_cbranchcc4, true);
1925 if (! cond || ! reg_mentioned_p (target, cond))
1926 return NULL;
1928 /* We almost certainly searched back to a different place.
1929 Need to re-verify correct lifetimes. */
1931 /* X may not be mentioned in the range (cond_earliest, jump]. */
1932 for (insn = if_info->jump; insn != *earliest; insn = PREV_INSN (insn))
1933 if (INSN_P (insn) && reg_overlap_mentioned_p (if_info->x, PATTERN (insn)))
1934 return NULL;
1936 /* A and B may not be modified in the range [cond_earliest, jump). */
1937 for (insn = *earliest; insn != if_info->jump; insn = NEXT_INSN (insn))
1938 if (INSN_P (insn)
1939 && (modified_in_p (if_info->a, insn)
1940 || modified_in_p (if_info->b, insn)))
1941 return NULL;
1943 return cond;
1946 /* Convert "if (a < b) x = a; else x = b;" to "x = min(a, b);", etc. */
1948 static int
1949 noce_try_minmax (struct noce_if_info *if_info)
1951 rtx cond, target;
1952 rtx_insn *earliest, *seq;
1953 enum rtx_code code, op;
1954 int unsignedp;
1956 /* ??? Reject modes with NaNs or signed zeros since we don't know how
1957 they will be resolved with an SMIN/SMAX. It wouldn't be too hard
1958 to get the target to tell us... */
1959 if (HONOR_SIGNED_ZEROS (if_info->x)
1960 || HONOR_NANS (if_info->x))
1961 return FALSE;
1963 cond = noce_get_alt_condition (if_info, if_info->a, &earliest);
1964 if (!cond)
1965 return FALSE;
1967 /* Verify the condition is of the form we expect, and canonicalize
1968 the comparison code. */
1969 code = GET_CODE (cond);
1970 if (rtx_equal_p (XEXP (cond, 0), if_info->a))
1972 if (! rtx_equal_p (XEXP (cond, 1), if_info->b))
1973 return FALSE;
1975 else if (rtx_equal_p (XEXP (cond, 1), if_info->a))
1977 if (! rtx_equal_p (XEXP (cond, 0), if_info->b))
1978 return FALSE;
1979 code = swap_condition (code);
1981 else
1982 return FALSE;
1984 /* Determine what sort of operation this is. Note that the code is for
1985 a taken branch, so the code->operation mapping appears backwards. */
1986 switch (code)
1988 case LT:
1989 case LE:
1990 case UNLT:
1991 case UNLE:
1992 op = SMAX;
1993 unsignedp = 0;
1994 break;
1995 case GT:
1996 case GE:
1997 case UNGT:
1998 case UNGE:
1999 op = SMIN;
2000 unsignedp = 0;
2001 break;
2002 case LTU:
2003 case LEU:
2004 op = UMAX;
2005 unsignedp = 1;
2006 break;
2007 case GTU:
2008 case GEU:
2009 op = UMIN;
2010 unsignedp = 1;
2011 break;
2012 default:
2013 return FALSE;
2016 start_sequence ();
2018 target = expand_simple_binop (GET_MODE (if_info->x), op,
2019 if_info->a, if_info->b,
2020 if_info->x, unsignedp, OPTAB_WIDEN);
2021 if (! target)
2023 end_sequence ();
2024 return FALSE;
2026 if (target != if_info->x)
2027 noce_emit_move_insn (if_info->x, target);
2029 seq = end_ifcvt_sequence (if_info);
2030 if (!seq)
2031 return FALSE;
2033 emit_insn_before_setloc (seq, if_info->jump, INSN_LOCATION (if_info->insn_a));
2034 if_info->cond = cond;
2035 if_info->cond_earliest = earliest;
2037 return TRUE;
2040 /* Convert "if (a < 0) x = -a; else x = a;" to "x = abs(a);",
2041 "if (a < 0) x = ~a; else x = a;" to "x = one_cmpl_abs(a);",
2042 etc. */
2044 static int
2045 noce_try_abs (struct noce_if_info *if_info)
2047 rtx cond, target, a, b, c;
2048 rtx_insn *earliest, *seq;
2049 int negate;
2050 bool one_cmpl = false;
2052 /* Reject modes with signed zeros. */
2053 if (HONOR_SIGNED_ZEROS (if_info->x))
2054 return FALSE;
2056 /* Recognize A and B as constituting an ABS or NABS. The canonical
2057 form is a branch around the negation, taken when the object is the
2058 first operand of a comparison against 0 that evaluates to true. */
2059 a = if_info->a;
2060 b = if_info->b;
2061 if (GET_CODE (a) == NEG && rtx_equal_p (XEXP (a, 0), b))
2062 negate = 0;
2063 else if (GET_CODE (b) == NEG && rtx_equal_p (XEXP (b, 0), a))
2065 std::swap (a, b);
2066 negate = 1;
2068 else if (GET_CODE (a) == NOT && rtx_equal_p (XEXP (a, 0), b))
2070 negate = 0;
2071 one_cmpl = true;
2073 else if (GET_CODE (b) == NOT && rtx_equal_p (XEXP (b, 0), a))
2075 std::swap (a, b);
2076 negate = 1;
2077 one_cmpl = true;
2079 else
2080 return FALSE;
2082 cond = noce_get_alt_condition (if_info, b, &earliest);
2083 if (!cond)
2084 return FALSE;
2086 /* Verify the condition is of the form we expect. */
2087 if (rtx_equal_p (XEXP (cond, 0), b))
2088 c = XEXP (cond, 1);
2089 else if (rtx_equal_p (XEXP (cond, 1), b))
2091 c = XEXP (cond, 0);
2092 negate = !negate;
2094 else
2095 return FALSE;
2097 /* Verify that C is zero. Search one step backward for a
2098 REG_EQUAL note or a simple source if necessary. */
2099 if (REG_P (c))
2101 rtx set;
2102 rtx_insn *insn = prev_nonnote_insn (earliest);
2103 if (insn
2104 && BLOCK_FOR_INSN (insn) == BLOCK_FOR_INSN (earliest)
2105 && (set = single_set (insn))
2106 && rtx_equal_p (SET_DEST (set), c))
2108 rtx note = find_reg_equal_equiv_note (insn);
2109 if (note)
2110 c = XEXP (note, 0);
2111 else
2112 c = SET_SRC (set);
2114 else
2115 return FALSE;
2117 if (MEM_P (c)
2118 && GET_CODE (XEXP (c, 0)) == SYMBOL_REF
2119 && CONSTANT_POOL_ADDRESS_P (XEXP (c, 0)))
2120 c = get_pool_constant (XEXP (c, 0));
2122 /* Work around funny ideas get_condition has wrt canonicalization.
2123 Note that these rtx constants are known to be CONST_INT, and
2124 therefore imply integer comparisons. */
2125 if (c == constm1_rtx && GET_CODE (cond) == GT)
2127 else if (c == const1_rtx && GET_CODE (cond) == LT)
2129 else if (c != CONST0_RTX (GET_MODE (b)))
2130 return FALSE;
2132 /* Determine what sort of operation this is. */
2133 switch (GET_CODE (cond))
2135 case LT:
2136 case LE:
2137 case UNLT:
2138 case UNLE:
2139 negate = !negate;
2140 break;
2141 case GT:
2142 case GE:
2143 case UNGT:
2144 case UNGE:
2145 break;
2146 default:
2147 return FALSE;
2150 start_sequence ();
2151 if (one_cmpl)
2152 target = expand_one_cmpl_abs_nojump (GET_MODE (if_info->x), b,
2153 if_info->x);
2154 else
2155 target = expand_abs_nojump (GET_MODE (if_info->x), b, if_info->x, 1);
2157 /* ??? It's a quandary whether cmove would be better here, especially
2158 for integers. Perhaps combine will clean things up. */
2159 if (target && negate)
2161 if (one_cmpl)
2162 target = expand_simple_unop (GET_MODE (target), NOT, target,
2163 if_info->x, 0);
2164 else
2165 target = expand_simple_unop (GET_MODE (target), NEG, target,
2166 if_info->x, 0);
2169 if (! target)
2171 end_sequence ();
2172 return FALSE;
2175 if (target != if_info->x)
2176 noce_emit_move_insn (if_info->x, target);
2178 seq = end_ifcvt_sequence (if_info);
2179 if (!seq)
2180 return FALSE;
2182 emit_insn_before_setloc (seq, if_info->jump, INSN_LOCATION (if_info->insn_a));
2183 if_info->cond = cond;
2184 if_info->cond_earliest = earliest;
2186 return TRUE;
2189 /* Convert "if (m < 0) x = b; else x = 0;" to "x = (m >> C) & b;". */
2191 static int
2192 noce_try_sign_mask (struct noce_if_info *if_info)
2194 rtx cond, t, m, c;
2195 rtx_insn *seq;
2196 machine_mode mode;
2197 enum rtx_code code;
2198 bool t_unconditional;
2200 cond = if_info->cond;
2201 code = GET_CODE (cond);
2202 m = XEXP (cond, 0);
2203 c = XEXP (cond, 1);
2205 t = NULL_RTX;
2206 if (if_info->a == const0_rtx)
2208 if ((code == LT && c == const0_rtx)
2209 || (code == LE && c == constm1_rtx))
2210 t = if_info->b;
2212 else if (if_info->b == const0_rtx)
2214 if ((code == GE && c == const0_rtx)
2215 || (code == GT && c == constm1_rtx))
2216 t = if_info->a;
2219 if (! t || side_effects_p (t))
2220 return FALSE;
2222 /* We currently don't handle different modes. */
2223 mode = GET_MODE (t);
2224 if (GET_MODE (m) != mode)
2225 return FALSE;
2227 /* This is only profitable if T is unconditionally executed/evaluated in the
2228 original insn sequence or T is cheap. The former happens if B is the
2229 non-zero (T) value and if INSN_B was taken from TEST_BB, or there was no
2230 INSN_B which can happen for e.g. conditional stores to memory. For the
2231 cost computation use the block TEST_BB where the evaluation will end up
2232 after the transformation. */
2233 t_unconditional =
2234 (t == if_info->b
2235 && (if_info->insn_b == NULL_RTX
2236 || BLOCK_FOR_INSN (if_info->insn_b) == if_info->test_bb));
2237 if (!(t_unconditional
2238 || (set_src_cost (t, optimize_bb_for_speed_p (if_info->test_bb))
2239 < COSTS_N_INSNS (2))))
2240 return FALSE;
2242 start_sequence ();
2243 /* Use emit_store_flag to generate "m < 0 ? -1 : 0" instead of expanding
2244 "(signed) m >> 31" directly. This benefits targets with specialized
2245 insns to obtain the signmask, but still uses ashr_optab otherwise. */
2246 m = emit_store_flag (gen_reg_rtx (mode), LT, m, const0_rtx, mode, 0, -1);
2247 t = m ? expand_binop (mode, and_optab, m, t, NULL_RTX, 0, OPTAB_DIRECT)
2248 : NULL_RTX;
2250 if (!t)
2252 end_sequence ();
2253 return FALSE;
2256 noce_emit_move_insn (if_info->x, t);
2258 seq = end_ifcvt_sequence (if_info);
2259 if (!seq)
2260 return FALSE;
2262 emit_insn_before_setloc (seq, if_info->jump, INSN_LOCATION (if_info->insn_a));
2263 return TRUE;
2267 /* Optimize away "if (x & C) x |= C" and similar bit manipulation
2268 transformations. */
2270 static int
2271 noce_try_bitop (struct noce_if_info *if_info)
2273 rtx cond, x, a, result;
2274 rtx_insn *seq;
2275 machine_mode mode;
2276 enum rtx_code code;
2277 int bitnum;
2279 x = if_info->x;
2280 cond = if_info->cond;
2281 code = GET_CODE (cond);
2283 /* Check for no else condition. */
2284 if (! rtx_equal_p (x, if_info->b))
2285 return FALSE;
2287 /* Check for a suitable condition. */
2288 if (code != NE && code != EQ)
2289 return FALSE;
2290 if (XEXP (cond, 1) != const0_rtx)
2291 return FALSE;
2292 cond = XEXP (cond, 0);
2294 /* ??? We could also handle AND here. */
2295 if (GET_CODE (cond) == ZERO_EXTRACT)
2297 if (XEXP (cond, 1) != const1_rtx
2298 || !CONST_INT_P (XEXP (cond, 2))
2299 || ! rtx_equal_p (x, XEXP (cond, 0)))
2300 return FALSE;
2301 bitnum = INTVAL (XEXP (cond, 2));
2302 mode = GET_MODE (x);
2303 if (BITS_BIG_ENDIAN)
2304 bitnum = GET_MODE_BITSIZE (mode) - 1 - bitnum;
2305 if (bitnum < 0 || bitnum >= HOST_BITS_PER_WIDE_INT)
2306 return FALSE;
2308 else
2309 return FALSE;
2311 a = if_info->a;
2312 if (GET_CODE (a) == IOR || GET_CODE (a) == XOR)
2314 /* Check for "if (X & C) x = x op C". */
2315 if (! rtx_equal_p (x, XEXP (a, 0))
2316 || !CONST_INT_P (XEXP (a, 1))
2317 || (INTVAL (XEXP (a, 1)) & GET_MODE_MASK (mode))
2318 != (unsigned HOST_WIDE_INT) 1 << bitnum)
2319 return FALSE;
2321 /* if ((x & C) == 0) x |= C; is transformed to x |= C. */
2322 /* if ((x & C) != 0) x |= C; is transformed to nothing. */
2323 if (GET_CODE (a) == IOR)
2324 result = (code == NE) ? a : NULL_RTX;
2325 else if (code == NE)
2327 /* if ((x & C) == 0) x ^= C; is transformed to x |= C. */
2328 result = gen_int_mode ((HOST_WIDE_INT) 1 << bitnum, mode);
2329 result = simplify_gen_binary (IOR, mode, x, result);
2331 else
2333 /* if ((x & C) != 0) x ^= C; is transformed to x &= ~C. */
2334 result = gen_int_mode (~((HOST_WIDE_INT) 1 << bitnum), mode);
2335 result = simplify_gen_binary (AND, mode, x, result);
2338 else if (GET_CODE (a) == AND)
2340 /* Check for "if (X & C) x &= ~C". */
2341 if (! rtx_equal_p (x, XEXP (a, 0))
2342 || !CONST_INT_P (XEXP (a, 1))
2343 || (INTVAL (XEXP (a, 1)) & GET_MODE_MASK (mode))
2344 != (~((HOST_WIDE_INT) 1 << bitnum) & GET_MODE_MASK (mode)))
2345 return FALSE;
2347 /* if ((x & C) == 0) x &= ~C; is transformed to nothing. */
2348 /* if ((x & C) != 0) x &= ~C; is transformed to x &= ~C. */
2349 result = (code == EQ) ? a : NULL_RTX;
2351 else
2352 return FALSE;
2354 if (result)
2356 start_sequence ();
2357 noce_emit_move_insn (x, result);
2358 seq = end_ifcvt_sequence (if_info);
2359 if (!seq)
2360 return FALSE;
2362 emit_insn_before_setloc (seq, if_info->jump,
2363 INSN_LOCATION (if_info->insn_a));
2365 return TRUE;
2369 /* Similar to get_condition, only the resulting condition must be
2370 valid at JUMP, instead of at EARLIEST.
2372 If THEN_ELSE_REVERSED is true, the fallthrough does not go to the
2373 THEN block of the caller, and we have to reverse the condition. */
2375 static rtx
2376 noce_get_condition (rtx_insn *jump, rtx_insn **earliest, bool then_else_reversed)
2378 rtx cond, set, tmp;
2379 bool reverse;
2381 if (! any_condjump_p (jump))
2382 return NULL_RTX;
2384 set = pc_set (jump);
2386 /* If this branches to JUMP_LABEL when the condition is false,
2387 reverse the condition. */
2388 reverse = (GET_CODE (XEXP (SET_SRC (set), 2)) == LABEL_REF
2389 && LABEL_REF_LABEL (XEXP (SET_SRC (set), 2)) == JUMP_LABEL (jump));
2391 /* We may have to reverse because the caller's if block is not canonical,
2392 i.e. the THEN block isn't the fallthrough block for the TEST block
2393 (see find_if_header). */
2394 if (then_else_reversed)
2395 reverse = !reverse;
2397 /* If the condition variable is a register and is MODE_INT, accept it. */
2399 cond = XEXP (SET_SRC (set), 0);
2400 tmp = XEXP (cond, 0);
2401 if (REG_P (tmp) && GET_MODE_CLASS (GET_MODE (tmp)) == MODE_INT
2402 && (GET_MODE (tmp) != BImode
2403 || !targetm.small_register_classes_for_mode_p (BImode)))
2405 *earliest = jump;
2407 if (reverse)
2408 cond = gen_rtx_fmt_ee (reverse_condition (GET_CODE (cond)),
2409 GET_MODE (cond), tmp, XEXP (cond, 1));
2410 return cond;
2413 /* Otherwise, fall back on canonicalize_condition to do the dirty
2414 work of manipulating MODE_CC values and COMPARE rtx codes. */
2415 tmp = canonicalize_condition (jump, cond, reverse, earliest,
2416 NULL_RTX, HAVE_cbranchcc4, true);
2418 /* We don't handle side-effects in the condition, like handling
2419 REG_INC notes and making sure no duplicate conditions are emitted. */
2420 if (tmp != NULL_RTX && side_effects_p (tmp))
2421 return NULL_RTX;
2423 return tmp;
2426 /* Return true if OP is ok for if-then-else processing. */
2428 static int
2429 noce_operand_ok (const_rtx op)
2431 if (side_effects_p (op))
2432 return FALSE;
2434 /* We special-case memories, so handle any of them with
2435 no address side effects. */
2436 if (MEM_P (op))
2437 return ! side_effects_p (XEXP (op, 0));
2439 return ! may_trap_p (op);
2442 /* Return true if a write into MEM may trap or fault. */
2444 static bool
2445 noce_mem_write_may_trap_or_fault_p (const_rtx mem)
2447 rtx addr;
2449 if (MEM_READONLY_P (mem))
2450 return true;
2452 if (may_trap_or_fault_p (mem))
2453 return true;
2455 addr = XEXP (mem, 0);
2457 /* Call target hook to avoid the effects of -fpic etc.... */
2458 addr = targetm.delegitimize_address (addr);
2460 while (addr)
2461 switch (GET_CODE (addr))
2463 case CONST:
2464 case PRE_DEC:
2465 case PRE_INC:
2466 case POST_DEC:
2467 case POST_INC:
2468 case POST_MODIFY:
2469 addr = XEXP (addr, 0);
2470 break;
2471 case LO_SUM:
2472 case PRE_MODIFY:
2473 addr = XEXP (addr, 1);
2474 break;
2475 case PLUS:
2476 if (CONST_INT_P (XEXP (addr, 1)))
2477 addr = XEXP (addr, 0);
2478 else
2479 return false;
2480 break;
2481 case LABEL_REF:
2482 return true;
2483 case SYMBOL_REF:
2484 if (SYMBOL_REF_DECL (addr)
2485 && decl_readonly_section (SYMBOL_REF_DECL (addr), 0))
2486 return true;
2487 return false;
2488 default:
2489 return false;
2492 return false;
2495 /* Return whether we can use store speculation for MEM. TOP_BB is the
2496 basic block above the conditional block where we are considering
2497 doing the speculative store. We look for whether MEM is set
2498 unconditionally later in the function. */
2500 static bool
2501 noce_can_store_speculate_p (basic_block top_bb, const_rtx mem)
2503 basic_block dominator;
2505 for (dominator = get_immediate_dominator (CDI_POST_DOMINATORS, top_bb);
2506 dominator != NULL;
2507 dominator = get_immediate_dominator (CDI_POST_DOMINATORS, dominator))
2509 rtx_insn *insn;
2511 FOR_BB_INSNS (dominator, insn)
2513 /* If we see something that might be a memory barrier, we
2514 have to stop looking. Even if the MEM is set later in
2515 the function, we still don't want to set it
2516 unconditionally before the barrier. */
2517 if (INSN_P (insn)
2518 && (volatile_insn_p (PATTERN (insn))
2519 || (CALL_P (insn) && (!RTL_CONST_CALL_P (insn)))))
2520 return false;
2522 if (memory_must_be_modified_in_insn_p (mem, insn))
2523 return true;
2524 if (modified_in_p (XEXP (mem, 0), insn))
2525 return false;
2530 return false;
2533 /* Given a simple IF-THEN-JOIN or IF-THEN-ELSE-JOIN block, attempt to convert
2534 it without using conditional execution. Return TRUE if we were successful
2535 at converting the block. */
2537 static int
2538 noce_process_if_block (struct noce_if_info *if_info)
2540 basic_block test_bb = if_info->test_bb; /* test block */
2541 basic_block then_bb = if_info->then_bb; /* THEN */
2542 basic_block else_bb = if_info->else_bb; /* ELSE or NULL */
2543 basic_block join_bb = if_info->join_bb; /* JOIN */
2544 rtx_insn *jump = if_info->jump;
2545 rtx cond = if_info->cond;
2546 rtx_insn *insn_a, *insn_b;
2547 rtx set_a, set_b;
2548 rtx orig_x, x, a, b;
2549 rtx cc;
2551 /* We're looking for patterns of the form
2553 (1) if (...) x = a; else x = b;
2554 (2) x = b; if (...) x = a;
2555 (3) if (...) x = a; // as if with an initial x = x.
2557 The later patterns require jumps to be more expensive.
2559 ??? For future expansion, look for multiple X in such patterns. */
2561 /* Look for one of the potential sets. */
2562 insn_a = first_active_insn (then_bb);
2563 if (! insn_a
2564 || insn_a != last_active_insn (then_bb, FALSE)
2565 || (set_a = single_set (insn_a)) == NULL_RTX)
2566 return FALSE;
2568 x = SET_DEST (set_a);
2569 a = SET_SRC (set_a);
2571 /* Look for the other potential set. Make sure we've got equivalent
2572 destinations. */
2573 /* ??? This is overconservative. Storing to two different mems is
2574 as easy as conditionally computing the address. Storing to a
2575 single mem merely requires a scratch memory to use as one of the
2576 destination addresses; often the memory immediately below the
2577 stack pointer is available for this. */
2578 set_b = NULL_RTX;
2579 if (else_bb)
2581 insn_b = first_active_insn (else_bb);
2582 if (! insn_b
2583 || insn_b != last_active_insn (else_bb, FALSE)
2584 || (set_b = single_set (insn_b)) == NULL_RTX
2585 || ! rtx_interchangeable_p (x, SET_DEST (set_b)))
2586 return FALSE;
2588 else
2590 insn_b = prev_nonnote_nondebug_insn (if_info->cond_earliest);
2591 /* We're going to be moving the evaluation of B down from above
2592 COND_EARLIEST to JUMP. Make sure the relevant data is still
2593 intact. */
2594 if (! insn_b
2595 || BLOCK_FOR_INSN (insn_b) != BLOCK_FOR_INSN (if_info->cond_earliest)
2596 || !NONJUMP_INSN_P (insn_b)
2597 || (set_b = single_set (insn_b)) == NULL_RTX
2598 || ! rtx_interchangeable_p (x, SET_DEST (set_b))
2599 || ! noce_operand_ok (SET_SRC (set_b))
2600 || reg_overlap_mentioned_p (x, SET_SRC (set_b))
2601 || modified_between_p (SET_SRC (set_b), insn_b, jump)
2602 /* Avoid extending the lifetime of hard registers on small
2603 register class machines. */
2604 || (REG_P (SET_SRC (set_b))
2605 && HARD_REGISTER_P (SET_SRC (set_b))
2606 && targetm.small_register_classes_for_mode_p
2607 (GET_MODE (SET_SRC (set_b))))
2608 /* Likewise with X. In particular this can happen when
2609 noce_get_condition looks farther back in the instruction
2610 stream than one might expect. */
2611 || reg_overlap_mentioned_p (x, cond)
2612 || reg_overlap_mentioned_p (x, a)
2613 || modified_between_p (x, insn_b, jump))
2615 insn_b = NULL;
2616 set_b = NULL_RTX;
2620 /* If x has side effects then only the if-then-else form is safe to
2621 convert. But even in that case we would need to restore any notes
2622 (such as REG_INC) at then end. That can be tricky if
2623 noce_emit_move_insn expands to more than one insn, so disable the
2624 optimization entirely for now if there are side effects. */
2625 if (side_effects_p (x))
2626 return FALSE;
2628 b = (set_b ? SET_SRC (set_b) : x);
2630 /* Only operate on register destinations, and even then avoid extending
2631 the lifetime of hard registers on small register class machines. */
2632 orig_x = x;
2633 if (!REG_P (x)
2634 || (HARD_REGISTER_P (x)
2635 && targetm.small_register_classes_for_mode_p (GET_MODE (x))))
2637 if (GET_MODE (x) == BLKmode)
2638 return FALSE;
2640 if (GET_CODE (x) == ZERO_EXTRACT
2641 && (!CONST_INT_P (XEXP (x, 1))
2642 || !CONST_INT_P (XEXP (x, 2))))
2643 return FALSE;
2645 x = gen_reg_rtx (GET_MODE (GET_CODE (x) == STRICT_LOW_PART
2646 ? XEXP (x, 0) : x));
2649 /* Don't operate on sources that may trap or are volatile. */
2650 if (! noce_operand_ok (a) || ! noce_operand_ok (b))
2651 return FALSE;
2653 retry:
2654 /* Set up the info block for our subroutines. */
2655 if_info->insn_a = insn_a;
2656 if_info->insn_b = insn_b;
2657 if_info->x = x;
2658 if_info->a = a;
2659 if_info->b = b;
2661 /* Skip it if the instruction to be moved might clobber CC. */
2662 cc = cc_in_cond (cond);
2663 if (cc
2664 && (set_of (cc, insn_a)
2665 || (insn_b && set_of (cc, insn_b))))
2666 return FALSE;
2668 /* Try optimizations in some approximation of a useful order. */
2669 /* ??? Should first look to see if X is live incoming at all. If it
2670 isn't, we don't need anything but an unconditional set. */
2672 /* Look and see if A and B are really the same. Avoid creating silly
2673 cmove constructs that no one will fix up later. */
2674 if (rtx_interchangeable_p (a, b))
2676 /* If we have an INSN_B, we don't have to create any new rtl. Just
2677 move the instruction that we already have. If we don't have an
2678 INSN_B, that means that A == X, and we've got a noop move. In
2679 that case don't do anything and let the code below delete INSN_A. */
2680 if (insn_b && else_bb)
2682 rtx note;
2684 if (else_bb && insn_b == BB_END (else_bb))
2685 BB_END (else_bb) = PREV_INSN (insn_b);
2686 reorder_insns (insn_b, insn_b, PREV_INSN (jump));
2688 /* If there was a REG_EQUAL note, delete it since it may have been
2689 true due to this insn being after a jump. */
2690 if ((note = find_reg_note (insn_b, REG_EQUAL, NULL_RTX)) != 0)
2691 remove_note (insn_b, note);
2693 insn_b = NULL;
2695 /* If we have "x = b; if (...) x = a;", and x has side-effects, then
2696 x must be executed twice. */
2697 else if (insn_b && side_effects_p (orig_x))
2698 return FALSE;
2700 x = orig_x;
2701 goto success;
2704 if (!set_b && MEM_P (orig_x))
2706 /* Disallow the "if (...) x = a;" form (implicit "else x = x;")
2707 for optimizations if writing to x may trap or fault,
2708 i.e. it's a memory other than a static var or a stack slot,
2709 is misaligned on strict aligned machines or is read-only. If
2710 x is a read-only memory, then the program is valid only if we
2711 avoid the store into it. If there are stores on both the
2712 THEN and ELSE arms, then we can go ahead with the conversion;
2713 either the program is broken, or the condition is always
2714 false such that the other memory is selected. */
2715 if (noce_mem_write_may_trap_or_fault_p (orig_x))
2716 return FALSE;
2718 /* Avoid store speculation: given "if (...) x = a" where x is a
2719 MEM, we only want to do the store if x is always set
2720 somewhere in the function. This avoids cases like
2721 if (pthread_mutex_trylock(mutex))
2722 ++global_variable;
2723 where we only want global_variable to be changed if the mutex
2724 is held. FIXME: This should ideally be expressed directly in
2725 RTL somehow. */
2726 if (!noce_can_store_speculate_p (test_bb, orig_x))
2727 return FALSE;
2730 if (noce_try_move (if_info))
2731 goto success;
2732 if (noce_try_store_flag (if_info))
2733 goto success;
2734 if (noce_try_bitop (if_info))
2735 goto success;
2736 if (noce_try_minmax (if_info))
2737 goto success;
2738 if (noce_try_abs (if_info))
2739 goto success;
2740 if (HAVE_conditional_move
2741 && noce_try_cmove (if_info))
2742 goto success;
2743 if (! targetm.have_conditional_execution ())
2745 if (noce_try_store_flag_constants (if_info))
2746 goto success;
2747 if (noce_try_addcc (if_info))
2748 goto success;
2749 if (noce_try_store_flag_mask (if_info))
2750 goto success;
2751 if (HAVE_conditional_move
2752 && noce_try_cmove_arith (if_info))
2753 goto success;
2754 if (noce_try_sign_mask (if_info))
2755 goto success;
2758 if (!else_bb && set_b)
2760 insn_b = NULL;
2761 set_b = NULL_RTX;
2762 b = orig_x;
2763 goto retry;
2766 return FALSE;
2768 success:
2770 /* If we used a temporary, fix it up now. */
2771 if (orig_x != x)
2773 rtx_insn *seq;
2775 start_sequence ();
2776 noce_emit_move_insn (orig_x, x);
2777 seq = get_insns ();
2778 set_used_flags (orig_x);
2779 unshare_all_rtl_in_chain (seq);
2780 end_sequence ();
2782 emit_insn_before_setloc (seq, BB_END (test_bb), INSN_LOCATION (insn_a));
2785 /* The original THEN and ELSE blocks may now be removed. The test block
2786 must now jump to the join block. If the test block and the join block
2787 can be merged, do so. */
2788 if (else_bb)
2790 delete_basic_block (else_bb);
2791 num_true_changes++;
2793 else
2794 remove_edge (find_edge (test_bb, join_bb));
2796 remove_edge (find_edge (then_bb, join_bb));
2797 redirect_edge_and_branch_force (single_succ_edge (test_bb), join_bb);
2798 delete_basic_block (then_bb);
2799 num_true_changes++;
2801 if (can_merge_blocks_p (test_bb, join_bb))
2803 merge_blocks (test_bb, join_bb);
2804 num_true_changes++;
2807 num_updated_if_blocks++;
2808 return TRUE;
2811 /* Check whether a block is suitable for conditional move conversion.
2812 Every insn must be a simple set of a register to a constant or a
2813 register. For each assignment, store the value in the pointer map
2814 VALS, keyed indexed by register pointer, then store the register
2815 pointer in REGS. COND is the condition we will test. */
2817 static int
2818 check_cond_move_block (basic_block bb,
2819 hash_map<rtx, rtx> *vals,
2820 vec<rtx> *regs,
2821 rtx cond)
2823 rtx_insn *insn;
2824 rtx cc = cc_in_cond (cond);
2826 /* We can only handle simple jumps at the end of the basic block.
2827 It is almost impossible to update the CFG otherwise. */
2828 insn = BB_END (bb);
2829 if (JUMP_P (insn) && !onlyjump_p (insn))
2830 return FALSE;
2832 FOR_BB_INSNS (bb, insn)
2834 rtx set, dest, src;
2836 if (!NONDEBUG_INSN_P (insn) || JUMP_P (insn))
2837 continue;
2838 set = single_set (insn);
2839 if (!set)
2840 return FALSE;
2842 dest = SET_DEST (set);
2843 src = SET_SRC (set);
2844 if (!REG_P (dest)
2845 || (HARD_REGISTER_P (dest)
2846 && targetm.small_register_classes_for_mode_p (GET_MODE (dest))))
2847 return FALSE;
2849 if (!CONSTANT_P (src) && !register_operand (src, VOIDmode))
2850 return FALSE;
2852 if (side_effects_p (src) || side_effects_p (dest))
2853 return FALSE;
2855 if (may_trap_p (src) || may_trap_p (dest))
2856 return FALSE;
2858 /* Don't try to handle this if the source register was
2859 modified earlier in the block. */
2860 if ((REG_P (src)
2861 && vals->get (src))
2862 || (GET_CODE (src) == SUBREG && REG_P (SUBREG_REG (src))
2863 && vals->get (SUBREG_REG (src))))
2864 return FALSE;
2866 /* Don't try to handle this if the destination register was
2867 modified earlier in the block. */
2868 if (vals->get (dest))
2869 return FALSE;
2871 /* Don't try to handle this if the condition uses the
2872 destination register. */
2873 if (reg_overlap_mentioned_p (dest, cond))
2874 return FALSE;
2876 /* Don't try to handle this if the source register is modified
2877 later in the block. */
2878 if (!CONSTANT_P (src)
2879 && modified_between_p (src, insn, NEXT_INSN (BB_END (bb))))
2880 return FALSE;
2882 /* Skip it if the instruction to be moved might clobber CC. */
2883 if (cc && set_of (cc, insn))
2884 return FALSE;
2886 vals->put (dest, src);
2888 regs->safe_push (dest);
2891 return TRUE;
2894 /* Given a basic block BB suitable for conditional move conversion,
2895 a condition COND, and pointer maps THEN_VALS and ELSE_VALS containing
2896 the register values depending on COND, emit the insns in the block as
2897 conditional moves. If ELSE_BLOCK is true, THEN_BB was already
2898 processed. The caller has started a sequence for the conversion.
2899 Return true if successful, false if something goes wrong. */
2901 static bool
2902 cond_move_convert_if_block (struct noce_if_info *if_infop,
2903 basic_block bb, rtx cond,
2904 hash_map<rtx, rtx> *then_vals,
2905 hash_map<rtx, rtx> *else_vals,
2906 bool else_block_p)
2908 enum rtx_code code;
2909 rtx_insn *insn;
2910 rtx cond_arg0, cond_arg1;
2912 code = GET_CODE (cond);
2913 cond_arg0 = XEXP (cond, 0);
2914 cond_arg1 = XEXP (cond, 1);
2916 FOR_BB_INSNS (bb, insn)
2918 rtx set, target, dest, t, e;
2920 /* ??? Maybe emit conditional debug insn? */
2921 if (!NONDEBUG_INSN_P (insn) || JUMP_P (insn))
2922 continue;
2923 set = single_set (insn);
2924 gcc_assert (set && REG_P (SET_DEST (set)));
2926 dest = SET_DEST (set);
2928 rtx *then_slot = then_vals->get (dest);
2929 rtx *else_slot = else_vals->get (dest);
2930 t = then_slot ? *then_slot : NULL_RTX;
2931 e = else_slot ? *else_slot : NULL_RTX;
2933 if (else_block_p)
2935 /* If this register was set in the then block, we already
2936 handled this case there. */
2937 if (t)
2938 continue;
2939 t = dest;
2940 gcc_assert (e);
2942 else
2944 gcc_assert (t);
2945 if (!e)
2946 e = dest;
2949 target = noce_emit_cmove (if_infop, dest, code, cond_arg0, cond_arg1,
2950 t, e);
2951 if (!target)
2952 return false;
2954 if (target != dest)
2955 noce_emit_move_insn (dest, target);
2958 return true;
2961 /* Given a simple IF-THEN-JOIN or IF-THEN-ELSE-JOIN block, attempt to convert
2962 it using only conditional moves. Return TRUE if we were successful at
2963 converting the block. */
2965 static int
2966 cond_move_process_if_block (struct noce_if_info *if_info)
2968 basic_block test_bb = if_info->test_bb;
2969 basic_block then_bb = if_info->then_bb;
2970 basic_block else_bb = if_info->else_bb;
2971 basic_block join_bb = if_info->join_bb;
2972 rtx_insn *jump = if_info->jump;
2973 rtx cond = if_info->cond;
2974 rtx_insn *seq, *loc_insn;
2975 rtx reg;
2976 int c;
2977 vec<rtx> then_regs = vNULL;
2978 vec<rtx> else_regs = vNULL;
2979 unsigned int i;
2980 int success_p = FALSE;
2982 /* Build a mapping for each block to the value used for each
2983 register. */
2984 hash_map<rtx, rtx> then_vals;
2985 hash_map<rtx, rtx> else_vals;
2987 /* Make sure the blocks are suitable. */
2988 if (!check_cond_move_block (then_bb, &then_vals, &then_regs, cond)
2989 || (else_bb
2990 && !check_cond_move_block (else_bb, &else_vals, &else_regs, cond)))
2991 goto done;
2993 /* Make sure the blocks can be used together. If the same register
2994 is set in both blocks, and is not set to a constant in both
2995 cases, then both blocks must set it to the same register. We
2996 have already verified that if it is set to a register, that the
2997 source register does not change after the assignment. Also count
2998 the number of registers set in only one of the blocks. */
2999 c = 0;
3000 FOR_EACH_VEC_ELT (then_regs, i, reg)
3002 rtx *then_slot = then_vals.get (reg);
3003 rtx *else_slot = else_vals.get (reg);
3005 gcc_checking_assert (then_slot);
3006 if (!else_slot)
3007 ++c;
3008 else
3010 rtx then_val = *then_slot;
3011 rtx else_val = *else_slot;
3012 if (!CONSTANT_P (then_val) && !CONSTANT_P (else_val)
3013 && !rtx_equal_p (then_val, else_val))
3014 goto done;
3018 /* Finish off c for MAX_CONDITIONAL_EXECUTE. */
3019 FOR_EACH_VEC_ELT (else_regs, i, reg)
3021 gcc_checking_assert (else_vals.get (reg));
3022 if (!then_vals.get (reg))
3023 ++c;
3026 /* Make sure it is reasonable to convert this block. What matters
3027 is the number of assignments currently made in only one of the
3028 branches, since if we convert we are going to always execute
3029 them. */
3030 if (c > MAX_CONDITIONAL_EXECUTE)
3031 goto done;
3033 /* Try to emit the conditional moves. First do the then block,
3034 then do anything left in the else blocks. */
3035 start_sequence ();
3036 if (!cond_move_convert_if_block (if_info, then_bb, cond,
3037 &then_vals, &else_vals, false)
3038 || (else_bb
3039 && !cond_move_convert_if_block (if_info, else_bb, cond,
3040 &then_vals, &else_vals, true)))
3042 end_sequence ();
3043 goto done;
3045 seq = end_ifcvt_sequence (if_info);
3046 if (!seq)
3047 goto done;
3049 loc_insn = first_active_insn (then_bb);
3050 if (!loc_insn)
3052 loc_insn = first_active_insn (else_bb);
3053 gcc_assert (loc_insn);
3055 emit_insn_before_setloc (seq, jump, INSN_LOCATION (loc_insn));
3057 if (else_bb)
3059 delete_basic_block (else_bb);
3060 num_true_changes++;
3062 else
3063 remove_edge (find_edge (test_bb, join_bb));
3065 remove_edge (find_edge (then_bb, join_bb));
3066 redirect_edge_and_branch_force (single_succ_edge (test_bb), join_bb);
3067 delete_basic_block (then_bb);
3068 num_true_changes++;
3070 if (can_merge_blocks_p (test_bb, join_bb))
3072 merge_blocks (test_bb, join_bb);
3073 num_true_changes++;
3076 num_updated_if_blocks++;
3078 success_p = TRUE;
3080 done:
3081 then_regs.release ();
3082 else_regs.release ();
3083 return success_p;
3087 /* Determine if a given basic block heads a simple IF-THEN-JOIN or an
3088 IF-THEN-ELSE-JOIN block.
3090 If so, we'll try to convert the insns to not require the branch,
3091 using only transformations that do not require conditional execution.
3093 Return TRUE if we were successful at converting the block. */
3095 static int
3096 noce_find_if_block (basic_block test_bb, edge then_edge, edge else_edge,
3097 int pass)
3099 basic_block then_bb, else_bb, join_bb;
3100 bool then_else_reversed = false;
3101 rtx_insn *jump;
3102 rtx cond;
3103 rtx_insn *cond_earliest;
3104 struct noce_if_info if_info;
3106 /* We only ever should get here before reload. */
3107 gcc_assert (!reload_completed);
3109 /* Recognize an IF-THEN-ELSE-JOIN block. */
3110 if (single_pred_p (then_edge->dest)
3111 && single_succ_p (then_edge->dest)
3112 && single_pred_p (else_edge->dest)
3113 && single_succ_p (else_edge->dest)
3114 && single_succ (then_edge->dest) == single_succ (else_edge->dest))
3116 then_bb = then_edge->dest;
3117 else_bb = else_edge->dest;
3118 join_bb = single_succ (then_bb);
3120 /* Recognize an IF-THEN-JOIN block. */
3121 else if (single_pred_p (then_edge->dest)
3122 && single_succ_p (then_edge->dest)
3123 && single_succ (then_edge->dest) == else_edge->dest)
3125 then_bb = then_edge->dest;
3126 else_bb = NULL_BLOCK;
3127 join_bb = else_edge->dest;
3129 /* Recognize an IF-ELSE-JOIN block. We can have those because the order
3130 of basic blocks in cfglayout mode does not matter, so the fallthrough
3131 edge can go to any basic block (and not just to bb->next_bb, like in
3132 cfgrtl mode). */
3133 else if (single_pred_p (else_edge->dest)
3134 && single_succ_p (else_edge->dest)
3135 && single_succ (else_edge->dest) == then_edge->dest)
3137 /* The noce transformations do not apply to IF-ELSE-JOIN blocks.
3138 To make this work, we have to invert the THEN and ELSE blocks
3139 and reverse the jump condition. */
3140 then_bb = else_edge->dest;
3141 else_bb = NULL_BLOCK;
3142 join_bb = single_succ (then_bb);
3143 then_else_reversed = true;
3145 else
3146 /* Not a form we can handle. */
3147 return FALSE;
3149 /* The edges of the THEN and ELSE blocks cannot have complex edges. */
3150 if (single_succ_edge (then_bb)->flags & EDGE_COMPLEX)
3151 return FALSE;
3152 if (else_bb
3153 && single_succ_edge (else_bb)->flags & EDGE_COMPLEX)
3154 return FALSE;
3156 num_possible_if_blocks++;
3158 if (dump_file)
3160 fprintf (dump_file,
3161 "\nIF-THEN%s-JOIN block found, pass %d, test %d, then %d",
3162 (else_bb) ? "-ELSE" : "",
3163 pass, test_bb->index, then_bb->index);
3165 if (else_bb)
3166 fprintf (dump_file, ", else %d", else_bb->index);
3168 fprintf (dump_file, ", join %d\n", join_bb->index);
3171 /* If the conditional jump is more than just a conditional
3172 jump, then we can not do if-conversion on this block. */
3173 jump = BB_END (test_bb);
3174 if (! onlyjump_p (jump))
3175 return FALSE;
3177 /* If this is not a standard conditional jump, we can't parse it. */
3178 cond = noce_get_condition (jump, &cond_earliest, then_else_reversed);
3179 if (!cond)
3180 return FALSE;
3182 /* We must be comparing objects whose modes imply the size. */
3183 if (GET_MODE (XEXP (cond, 0)) == BLKmode)
3184 return FALSE;
3186 /* Initialize an IF_INFO struct to pass around. */
3187 memset (&if_info, 0, sizeof if_info);
3188 if_info.test_bb = test_bb;
3189 if_info.then_bb = then_bb;
3190 if_info.else_bb = else_bb;
3191 if_info.join_bb = join_bb;
3192 if_info.cond = cond;
3193 if_info.cond_earliest = cond_earliest;
3194 if_info.jump = jump;
3195 if_info.then_else_reversed = then_else_reversed;
3196 if_info.branch_cost = BRANCH_COST (optimize_bb_for_speed_p (test_bb),
3197 predictable_edge_p (then_edge));
3199 /* Do the real work. */
3201 if (noce_process_if_block (&if_info))
3202 return TRUE;
3204 if (HAVE_conditional_move
3205 && cond_move_process_if_block (&if_info))
3206 return TRUE;
3208 return FALSE;
3212 /* Merge the blocks and mark for local life update. */
3214 static void
3215 merge_if_block (struct ce_if_block * ce_info)
3217 basic_block test_bb = ce_info->test_bb; /* last test block */
3218 basic_block then_bb = ce_info->then_bb; /* THEN */
3219 basic_block else_bb = ce_info->else_bb; /* ELSE or NULL */
3220 basic_block join_bb = ce_info->join_bb; /* join block */
3221 basic_block combo_bb;
3223 /* All block merging is done into the lower block numbers. */
3225 combo_bb = test_bb;
3226 df_set_bb_dirty (test_bb);
3228 /* Merge any basic blocks to handle && and || subtests. Each of
3229 the blocks are on the fallthru path from the predecessor block. */
3230 if (ce_info->num_multiple_test_blocks > 0)
3232 basic_block bb = test_bb;
3233 basic_block last_test_bb = ce_info->last_test_bb;
3234 basic_block fallthru = block_fallthru (bb);
3238 bb = fallthru;
3239 fallthru = block_fallthru (bb);
3240 merge_blocks (combo_bb, bb);
3241 num_true_changes++;
3243 while (bb != last_test_bb);
3246 /* Merge TEST block into THEN block. Normally the THEN block won't have a
3247 label, but it might if there were || tests. That label's count should be
3248 zero, and it normally should be removed. */
3250 if (then_bb)
3252 /* If THEN_BB has no successors, then there's a BARRIER after it.
3253 If COMBO_BB has more than one successor (THEN_BB), then that BARRIER
3254 is no longer needed, and in fact it is incorrect to leave it in
3255 the insn stream. */
3256 if (EDGE_COUNT (then_bb->succs) == 0
3257 && EDGE_COUNT (combo_bb->succs) > 1)
3259 rtx_insn *end = NEXT_INSN (BB_END (then_bb));
3260 while (end && NOTE_P (end) && !NOTE_INSN_BASIC_BLOCK_P (end))
3261 end = NEXT_INSN (end);
3263 if (end && BARRIER_P (end))
3264 delete_insn (end);
3266 merge_blocks (combo_bb, then_bb);
3267 num_true_changes++;
3270 /* The ELSE block, if it existed, had a label. That label count
3271 will almost always be zero, but odd things can happen when labels
3272 get their addresses taken. */
3273 if (else_bb)
3275 /* If ELSE_BB has no successors, then there's a BARRIER after it.
3276 If COMBO_BB has more than one successor (ELSE_BB), then that BARRIER
3277 is no longer needed, and in fact it is incorrect to leave it in
3278 the insn stream. */
3279 if (EDGE_COUNT (else_bb->succs) == 0
3280 && EDGE_COUNT (combo_bb->succs) > 1)
3282 rtx_insn *end = NEXT_INSN (BB_END (else_bb));
3283 while (end && NOTE_P (end) && !NOTE_INSN_BASIC_BLOCK_P (end))
3284 end = NEXT_INSN (end);
3286 if (end && BARRIER_P (end))
3287 delete_insn (end);
3289 merge_blocks (combo_bb, else_bb);
3290 num_true_changes++;
3293 /* If there was no join block reported, that means it was not adjacent
3294 to the others, and so we cannot merge them. */
3296 if (! join_bb)
3298 rtx_insn *last = BB_END (combo_bb);
3300 /* The outgoing edge for the current COMBO block should already
3301 be correct. Verify this. */
3302 if (EDGE_COUNT (combo_bb->succs) == 0)
3303 gcc_assert (find_reg_note (last, REG_NORETURN, NULL)
3304 || (NONJUMP_INSN_P (last)
3305 && GET_CODE (PATTERN (last)) == TRAP_IF
3306 && (TRAP_CONDITION (PATTERN (last))
3307 == const_true_rtx)));
3309 else
3310 /* There should still be something at the end of the THEN or ELSE
3311 blocks taking us to our final destination. */
3312 gcc_assert (JUMP_P (last)
3313 || (EDGE_SUCC (combo_bb, 0)->dest
3314 == EXIT_BLOCK_PTR_FOR_FN (cfun)
3315 && CALL_P (last)
3316 && SIBLING_CALL_P (last))
3317 || ((EDGE_SUCC (combo_bb, 0)->flags & EDGE_EH)
3318 && can_throw_internal (last)));
3321 /* The JOIN block may have had quite a number of other predecessors too.
3322 Since we've already merged the TEST, THEN and ELSE blocks, we should
3323 have only one remaining edge from our if-then-else diamond. If there
3324 is more than one remaining edge, it must come from elsewhere. There
3325 may be zero incoming edges if the THEN block didn't actually join
3326 back up (as with a call to a non-return function). */
3327 else if (EDGE_COUNT (join_bb->preds) < 2
3328 && join_bb != EXIT_BLOCK_PTR_FOR_FN (cfun))
3330 /* We can merge the JOIN cleanly and update the dataflow try
3331 again on this pass.*/
3332 merge_blocks (combo_bb, join_bb);
3333 num_true_changes++;
3335 else
3337 /* We cannot merge the JOIN. */
3339 /* The outgoing edge for the current COMBO block should already
3340 be correct. Verify this. */
3341 gcc_assert (single_succ_p (combo_bb)
3342 && single_succ (combo_bb) == join_bb);
3344 /* Remove the jump and cruft from the end of the COMBO block. */
3345 if (join_bb != EXIT_BLOCK_PTR_FOR_FN (cfun))
3346 tidy_fallthru_edge (single_succ_edge (combo_bb));
3349 num_updated_if_blocks++;
3352 /* Find a block ending in a simple IF condition and try to transform it
3353 in some way. When converting a multi-block condition, put the new code
3354 in the first such block and delete the rest. Return a pointer to this
3355 first block if some transformation was done. Return NULL otherwise. */
3357 static basic_block
3358 find_if_header (basic_block test_bb, int pass)
3360 ce_if_block ce_info;
3361 edge then_edge;
3362 edge else_edge;
3364 /* The kind of block we're looking for has exactly two successors. */
3365 if (EDGE_COUNT (test_bb->succs) != 2)
3366 return NULL;
3368 then_edge = EDGE_SUCC (test_bb, 0);
3369 else_edge = EDGE_SUCC (test_bb, 1);
3371 if (df_get_bb_dirty (then_edge->dest))
3372 return NULL;
3373 if (df_get_bb_dirty (else_edge->dest))
3374 return NULL;
3376 /* Neither edge should be abnormal. */
3377 if ((then_edge->flags & EDGE_COMPLEX)
3378 || (else_edge->flags & EDGE_COMPLEX))
3379 return NULL;
3381 /* Nor exit the loop. */
3382 if ((then_edge->flags & EDGE_LOOP_EXIT)
3383 || (else_edge->flags & EDGE_LOOP_EXIT))
3384 return NULL;
3386 /* The THEN edge is canonically the one that falls through. */
3387 if (then_edge->flags & EDGE_FALLTHRU)
3389 else if (else_edge->flags & EDGE_FALLTHRU)
3390 std::swap (then_edge, else_edge);
3391 else
3392 /* Otherwise this must be a multiway branch of some sort. */
3393 return NULL;
3395 memset (&ce_info, 0, sizeof (ce_info));
3396 ce_info.test_bb = test_bb;
3397 ce_info.then_bb = then_edge->dest;
3398 ce_info.else_bb = else_edge->dest;
3399 ce_info.pass = pass;
3401 #ifdef IFCVT_MACHDEP_INIT
3402 IFCVT_MACHDEP_INIT (&ce_info);
3403 #endif
3405 if (!reload_completed
3406 && noce_find_if_block (test_bb, then_edge, else_edge, pass))
3407 goto success;
3409 if (reload_completed
3410 && targetm.have_conditional_execution ()
3411 && cond_exec_find_if_block (&ce_info))
3412 goto success;
3414 if (targetm.have_trap ()
3415 && optab_handler (ctrap_optab, word_mode) != CODE_FOR_nothing
3416 && find_cond_trap (test_bb, then_edge, else_edge))
3417 goto success;
3419 if (dom_info_state (CDI_POST_DOMINATORS) >= DOM_NO_FAST_QUERY
3420 && (reload_completed || !targetm.have_conditional_execution ()))
3422 if (find_if_case_1 (test_bb, then_edge, else_edge))
3423 goto success;
3424 if (find_if_case_2 (test_bb, then_edge, else_edge))
3425 goto success;
3428 return NULL;
3430 success:
3431 if (dump_file)
3432 fprintf (dump_file, "Conversion succeeded on pass %d.\n", pass);
3433 /* Set this so we continue looking. */
3434 cond_exec_changed_p = TRUE;
3435 return ce_info.test_bb;
3438 /* Return true if a block has two edges, one of which falls through to the next
3439 block, and the other jumps to a specific block, so that we can tell if the
3440 block is part of an && test or an || test. Returns either -1 or the number
3441 of non-note, non-jump, non-USE/CLOBBER insns in the block. */
3443 static int
3444 block_jumps_and_fallthru_p (basic_block cur_bb, basic_block target_bb)
3446 edge cur_edge;
3447 int fallthru_p = FALSE;
3448 int jump_p = FALSE;
3449 rtx_insn *insn;
3450 rtx_insn *end;
3451 int n_insns = 0;
3452 edge_iterator ei;
3454 if (!cur_bb || !target_bb)
3455 return -1;
3457 /* If no edges, obviously it doesn't jump or fallthru. */
3458 if (EDGE_COUNT (cur_bb->succs) == 0)
3459 return FALSE;
3461 FOR_EACH_EDGE (cur_edge, ei, cur_bb->succs)
3463 if (cur_edge->flags & EDGE_COMPLEX)
3464 /* Anything complex isn't what we want. */
3465 return -1;
3467 else if (cur_edge->flags & EDGE_FALLTHRU)
3468 fallthru_p = TRUE;
3470 else if (cur_edge->dest == target_bb)
3471 jump_p = TRUE;
3473 else
3474 return -1;
3477 if ((jump_p & fallthru_p) == 0)
3478 return -1;
3480 /* Don't allow calls in the block, since this is used to group && and ||
3481 together for conditional execution support. ??? we should support
3482 conditional execution support across calls for IA-64 some day, but
3483 for now it makes the code simpler. */
3484 end = BB_END (cur_bb);
3485 insn = BB_HEAD (cur_bb);
3487 while (insn != NULL_RTX)
3489 if (CALL_P (insn))
3490 return -1;
3492 if (INSN_P (insn)
3493 && !JUMP_P (insn)
3494 && !DEBUG_INSN_P (insn)
3495 && GET_CODE (PATTERN (insn)) != USE
3496 && GET_CODE (PATTERN (insn)) != CLOBBER)
3497 n_insns++;
3499 if (insn == end)
3500 break;
3502 insn = NEXT_INSN (insn);
3505 return n_insns;
3508 /* Determine if a given basic block heads a simple IF-THEN or IF-THEN-ELSE
3509 block. If so, we'll try to convert the insns to not require the branch.
3510 Return TRUE if we were successful at converting the block. */
3512 static int
3513 cond_exec_find_if_block (struct ce_if_block * ce_info)
3515 basic_block test_bb = ce_info->test_bb;
3516 basic_block then_bb = ce_info->then_bb;
3517 basic_block else_bb = ce_info->else_bb;
3518 basic_block join_bb = NULL_BLOCK;
3519 edge cur_edge;
3520 basic_block next;
3521 edge_iterator ei;
3523 ce_info->last_test_bb = test_bb;
3525 /* We only ever should get here after reload,
3526 and if we have conditional execution. */
3527 gcc_assert (reload_completed && targetm.have_conditional_execution ());
3529 /* Discover if any fall through predecessors of the current test basic block
3530 were && tests (which jump to the else block) or || tests (which jump to
3531 the then block). */
3532 if (single_pred_p (test_bb)
3533 && single_pred_edge (test_bb)->flags == EDGE_FALLTHRU)
3535 basic_block bb = single_pred (test_bb);
3536 basic_block target_bb;
3537 int max_insns = MAX_CONDITIONAL_EXECUTE;
3538 int n_insns;
3540 /* Determine if the preceding block is an && or || block. */
3541 if ((n_insns = block_jumps_and_fallthru_p (bb, else_bb)) >= 0)
3543 ce_info->and_and_p = TRUE;
3544 target_bb = else_bb;
3546 else if ((n_insns = block_jumps_and_fallthru_p (bb, then_bb)) >= 0)
3548 ce_info->and_and_p = FALSE;
3549 target_bb = then_bb;
3551 else
3552 target_bb = NULL_BLOCK;
3554 if (target_bb && n_insns <= max_insns)
3556 int total_insns = 0;
3557 int blocks = 0;
3559 ce_info->last_test_bb = test_bb;
3561 /* Found at least one && or || block, look for more. */
3564 ce_info->test_bb = test_bb = bb;
3565 total_insns += n_insns;
3566 blocks++;
3568 if (!single_pred_p (bb))
3569 break;
3571 bb = single_pred (bb);
3572 n_insns = block_jumps_and_fallthru_p (bb, target_bb);
3574 while (n_insns >= 0 && (total_insns + n_insns) <= max_insns);
3576 ce_info->num_multiple_test_blocks = blocks;
3577 ce_info->num_multiple_test_insns = total_insns;
3579 if (ce_info->and_and_p)
3580 ce_info->num_and_and_blocks = blocks;
3581 else
3582 ce_info->num_or_or_blocks = blocks;
3586 /* The THEN block of an IF-THEN combo must have exactly one predecessor,
3587 other than any || blocks which jump to the THEN block. */
3588 if ((EDGE_COUNT (then_bb->preds) - ce_info->num_or_or_blocks) != 1)
3589 return FALSE;
3591 /* The edges of the THEN and ELSE blocks cannot have complex edges. */
3592 FOR_EACH_EDGE (cur_edge, ei, then_bb->preds)
3594 if (cur_edge->flags & EDGE_COMPLEX)
3595 return FALSE;
3598 FOR_EACH_EDGE (cur_edge, ei, else_bb->preds)
3600 if (cur_edge->flags & EDGE_COMPLEX)
3601 return FALSE;
3604 /* The THEN block of an IF-THEN combo must have zero or one successors. */
3605 if (EDGE_COUNT (then_bb->succs) > 0
3606 && (!single_succ_p (then_bb)
3607 || (single_succ_edge (then_bb)->flags & EDGE_COMPLEX)
3608 || (epilogue_completed
3609 && tablejump_p (BB_END (then_bb), NULL, NULL))))
3610 return FALSE;
3612 /* If the THEN block has no successors, conditional execution can still
3613 make a conditional call. Don't do this unless the ELSE block has
3614 only one incoming edge -- the CFG manipulation is too ugly otherwise.
3615 Check for the last insn of the THEN block being an indirect jump, which
3616 is listed as not having any successors, but confuses the rest of the CE
3617 code processing. ??? we should fix this in the future. */
3618 if (EDGE_COUNT (then_bb->succs) == 0)
3620 if (single_pred_p (else_bb) && else_bb != EXIT_BLOCK_PTR_FOR_FN (cfun))
3622 rtx_insn *last_insn = BB_END (then_bb);
3624 while (last_insn
3625 && NOTE_P (last_insn)
3626 && last_insn != BB_HEAD (then_bb))
3627 last_insn = PREV_INSN (last_insn);
3629 if (last_insn
3630 && JUMP_P (last_insn)
3631 && ! simplejump_p (last_insn))
3632 return FALSE;
3634 join_bb = else_bb;
3635 else_bb = NULL_BLOCK;
3637 else
3638 return FALSE;
3641 /* If the THEN block's successor is the other edge out of the TEST block,
3642 then we have an IF-THEN combo without an ELSE. */
3643 else if (single_succ (then_bb) == else_bb)
3645 join_bb = else_bb;
3646 else_bb = NULL_BLOCK;
3649 /* If the THEN and ELSE block meet in a subsequent block, and the ELSE
3650 has exactly one predecessor and one successor, and the outgoing edge
3651 is not complex, then we have an IF-THEN-ELSE combo. */
3652 else if (single_succ_p (else_bb)
3653 && single_succ (then_bb) == single_succ (else_bb)
3654 && single_pred_p (else_bb)
3655 && !(single_succ_edge (else_bb)->flags & EDGE_COMPLEX)
3656 && !(epilogue_completed
3657 && tablejump_p (BB_END (else_bb), NULL, NULL)))
3658 join_bb = single_succ (else_bb);
3660 /* Otherwise it is not an IF-THEN or IF-THEN-ELSE combination. */
3661 else
3662 return FALSE;
3664 num_possible_if_blocks++;
3666 if (dump_file)
3668 fprintf (dump_file,
3669 "\nIF-THEN%s block found, pass %d, start block %d "
3670 "[insn %d], then %d [%d]",
3671 (else_bb) ? "-ELSE" : "",
3672 ce_info->pass,
3673 test_bb->index,
3674 BB_HEAD (test_bb) ? (int)INSN_UID (BB_HEAD (test_bb)) : -1,
3675 then_bb->index,
3676 BB_HEAD (then_bb) ? (int)INSN_UID (BB_HEAD (then_bb)) : -1);
3678 if (else_bb)
3679 fprintf (dump_file, ", else %d [%d]",
3680 else_bb->index,
3681 BB_HEAD (else_bb) ? (int)INSN_UID (BB_HEAD (else_bb)) : -1);
3683 fprintf (dump_file, ", join %d [%d]",
3684 join_bb->index,
3685 BB_HEAD (join_bb) ? (int)INSN_UID (BB_HEAD (join_bb)) : -1);
3687 if (ce_info->num_multiple_test_blocks > 0)
3688 fprintf (dump_file, ", %d %s block%s last test %d [%d]",
3689 ce_info->num_multiple_test_blocks,
3690 (ce_info->and_and_p) ? "&&" : "||",
3691 (ce_info->num_multiple_test_blocks == 1) ? "" : "s",
3692 ce_info->last_test_bb->index,
3693 ((BB_HEAD (ce_info->last_test_bb))
3694 ? (int)INSN_UID (BB_HEAD (ce_info->last_test_bb))
3695 : -1));
3697 fputc ('\n', dump_file);
3700 /* Make sure IF, THEN, and ELSE, blocks are adjacent. Actually, we get the
3701 first condition for free, since we've already asserted that there's a
3702 fallthru edge from IF to THEN. Likewise for the && and || blocks, since
3703 we checked the FALLTHRU flag, those are already adjacent to the last IF
3704 block. */
3705 /* ??? As an enhancement, move the ELSE block. Have to deal with
3706 BLOCK notes, if by no other means than backing out the merge if they
3707 exist. Sticky enough I don't want to think about it now. */
3708 next = then_bb;
3709 if (else_bb && (next = next->next_bb) != else_bb)
3710 return FALSE;
3711 if ((next = next->next_bb) != join_bb
3712 && join_bb != EXIT_BLOCK_PTR_FOR_FN (cfun))
3714 if (else_bb)
3715 join_bb = NULL;
3716 else
3717 return FALSE;
3720 /* Do the real work. */
3722 ce_info->else_bb = else_bb;
3723 ce_info->join_bb = join_bb;
3725 /* If we have && and || tests, try to first handle combining the && and ||
3726 tests into the conditional code, and if that fails, go back and handle
3727 it without the && and ||, which at present handles the && case if there
3728 was no ELSE block. */
3729 if (cond_exec_process_if_block (ce_info, TRUE))
3730 return TRUE;
3732 if (ce_info->num_multiple_test_blocks)
3734 cancel_changes (0);
3736 if (cond_exec_process_if_block (ce_info, FALSE))
3737 return TRUE;
3740 return FALSE;
3743 /* Convert a branch over a trap, or a branch
3744 to a trap, into a conditional trap. */
3746 static int
3747 find_cond_trap (basic_block test_bb, edge then_edge, edge else_edge)
3749 basic_block then_bb = then_edge->dest;
3750 basic_block else_bb = else_edge->dest;
3751 basic_block other_bb, trap_bb;
3752 rtx_insn *trap, *jump;
3753 rtx cond;
3754 rtx_insn *cond_earliest;
3755 enum rtx_code code;
3757 /* Locate the block with the trap instruction. */
3758 /* ??? While we look for no successors, we really ought to allow
3759 EH successors. Need to fix merge_if_block for that to work. */
3760 if ((trap = block_has_only_trap (then_bb)) != NULL)
3761 trap_bb = then_bb, other_bb = else_bb;
3762 else if ((trap = block_has_only_trap (else_bb)) != NULL)
3763 trap_bb = else_bb, other_bb = then_bb;
3764 else
3765 return FALSE;
3767 if (dump_file)
3769 fprintf (dump_file, "\nTRAP-IF block found, start %d, trap %d\n",
3770 test_bb->index, trap_bb->index);
3773 /* If this is not a standard conditional jump, we can't parse it. */
3774 jump = BB_END (test_bb);
3775 cond = noce_get_condition (jump, &cond_earliest, false);
3776 if (! cond)
3777 return FALSE;
3779 /* If the conditional jump is more than just a conditional jump, then
3780 we can not do if-conversion on this block. */
3781 if (! onlyjump_p (jump))
3782 return FALSE;
3784 /* We must be comparing objects whose modes imply the size. */
3785 if (GET_MODE (XEXP (cond, 0)) == BLKmode)
3786 return FALSE;
3788 /* Reverse the comparison code, if necessary. */
3789 code = GET_CODE (cond);
3790 if (then_bb == trap_bb)
3792 code = reversed_comparison_code (cond, jump);
3793 if (code == UNKNOWN)
3794 return FALSE;
3797 /* Attempt to generate the conditional trap. */
3798 rtx_insn *seq = gen_cond_trap (code, copy_rtx (XEXP (cond, 0)),
3799 copy_rtx (XEXP (cond, 1)),
3800 TRAP_CODE (PATTERN (trap)));
3801 if (seq == NULL)
3802 return FALSE;
3804 /* Emit the new insns before cond_earliest. */
3805 emit_insn_before_setloc (seq, cond_earliest, INSN_LOCATION (trap));
3807 /* Delete the trap block if possible. */
3808 remove_edge (trap_bb == then_bb ? then_edge : else_edge);
3809 df_set_bb_dirty (test_bb);
3810 df_set_bb_dirty (then_bb);
3811 df_set_bb_dirty (else_bb);
3813 if (EDGE_COUNT (trap_bb->preds) == 0)
3815 delete_basic_block (trap_bb);
3816 num_true_changes++;
3819 /* Wire together the blocks again. */
3820 if (current_ir_type () == IR_RTL_CFGLAYOUT)
3821 single_succ_edge (test_bb)->flags |= EDGE_FALLTHRU;
3822 else if (trap_bb == then_bb)
3824 rtx lab = JUMP_LABEL (jump);
3825 rtx_insn *seq = targetm.gen_jump (lab);
3826 rtx_jump_insn *newjump = emit_jump_insn_after (seq, jump);
3827 LABEL_NUSES (lab) += 1;
3828 JUMP_LABEL (newjump) = lab;
3829 emit_barrier_after (newjump);
3831 delete_insn (jump);
3833 if (can_merge_blocks_p (test_bb, other_bb))
3835 merge_blocks (test_bb, other_bb);
3836 num_true_changes++;
3839 num_updated_if_blocks++;
3840 return TRUE;
3843 /* Subroutine of find_cond_trap: if BB contains only a trap insn,
3844 return it. */
3846 static rtx_insn *
3847 block_has_only_trap (basic_block bb)
3849 rtx_insn *trap;
3851 /* We're not the exit block. */
3852 if (bb == EXIT_BLOCK_PTR_FOR_FN (cfun))
3853 return NULL;
3855 /* The block must have no successors. */
3856 if (EDGE_COUNT (bb->succs) > 0)
3857 return NULL;
3859 /* The only instruction in the THEN block must be the trap. */
3860 trap = first_active_insn (bb);
3861 if (! (trap == BB_END (bb)
3862 && GET_CODE (PATTERN (trap)) == TRAP_IF
3863 && TRAP_CONDITION (PATTERN (trap)) == const_true_rtx))
3864 return NULL;
3866 return trap;
3869 /* Look for IF-THEN-ELSE cases in which one of THEN or ELSE is
3870 transformable, but not necessarily the other. There need be no
3871 JOIN block.
3873 Return TRUE if we were successful at converting the block.
3875 Cases we'd like to look at:
3878 if (test) goto over; // x not live
3879 x = a;
3880 goto label;
3881 over:
3883 becomes
3885 x = a;
3886 if (! test) goto label;
3889 if (test) goto E; // x not live
3890 x = big();
3891 goto L;
3893 x = b;
3894 goto M;
3896 becomes
3898 x = b;
3899 if (test) goto M;
3900 x = big();
3901 goto L;
3903 (3) // This one's really only interesting for targets that can do
3904 // multiway branching, e.g. IA-64 BBB bundles. For other targets
3905 // it results in multiple branches on a cache line, which often
3906 // does not sit well with predictors.
3908 if (test1) goto E; // predicted not taken
3909 x = a;
3910 if (test2) goto F;
3913 x = b;
3916 becomes
3918 x = a;
3919 if (test1) goto E;
3920 if (test2) goto F;
3922 Notes:
3924 (A) Don't do (2) if the branch is predicted against the block we're
3925 eliminating. Do it anyway if we can eliminate a branch; this requires
3926 that the sole successor of the eliminated block postdominate the other
3927 side of the if.
3929 (B) With CE, on (3) we can steal from both sides of the if, creating
3931 if (test1) x = a;
3932 if (!test1) x = b;
3933 if (test1) goto J;
3934 if (test2) goto F;
3938 Again, this is most useful if J postdominates.
3940 (C) CE substitutes for helpful life information.
3942 (D) These heuristics need a lot of work. */
3944 /* Tests for case 1 above. */
3946 static int
3947 find_if_case_1 (basic_block test_bb, edge then_edge, edge else_edge)
3949 basic_block then_bb = then_edge->dest;
3950 basic_block else_bb = else_edge->dest;
3951 basic_block new_bb;
3952 int then_bb_index, then_prob;
3953 rtx else_target = NULL_RTX;
3955 /* If we are partitioning hot/cold basic blocks, we don't want to
3956 mess up unconditional or indirect jumps that cross between hot
3957 and cold sections.
3959 Basic block partitioning may result in some jumps that appear to
3960 be optimizable (or blocks that appear to be mergeable), but which really
3961 must be left untouched (they are required to make it safely across
3962 partition boundaries). See the comments at the top of
3963 bb-reorder.c:partition_hot_cold_basic_blocks for complete details. */
3965 if ((BB_END (then_bb)
3966 && JUMP_P (BB_END (then_bb))
3967 && CROSSING_JUMP_P (BB_END (then_bb)))
3968 || (BB_END (test_bb)
3969 && JUMP_P (BB_END (test_bb))
3970 && CROSSING_JUMP_P (BB_END (test_bb)))
3971 || (BB_END (else_bb)
3972 && JUMP_P (BB_END (else_bb))
3973 && CROSSING_JUMP_P (BB_END (else_bb))))
3974 return FALSE;
3976 /* THEN has one successor. */
3977 if (!single_succ_p (then_bb))
3978 return FALSE;
3980 /* THEN does not fall through, but is not strange either. */
3981 if (single_succ_edge (then_bb)->flags & (EDGE_COMPLEX | EDGE_FALLTHRU))
3982 return FALSE;
3984 /* THEN has one predecessor. */
3985 if (!single_pred_p (then_bb))
3986 return FALSE;
3988 /* THEN must do something. */
3989 if (forwarder_block_p (then_bb))
3990 return FALSE;
3992 num_possible_if_blocks++;
3993 if (dump_file)
3994 fprintf (dump_file,
3995 "\nIF-CASE-1 found, start %d, then %d\n",
3996 test_bb->index, then_bb->index);
3998 if (then_edge->probability)
3999 then_prob = REG_BR_PROB_BASE - then_edge->probability;
4000 else
4001 then_prob = REG_BR_PROB_BASE / 2;
4003 /* We're speculating from the THEN path, we want to make sure the cost
4004 of speculation is within reason. */
4005 if (! cheap_bb_rtx_cost_p (then_bb, then_prob,
4006 COSTS_N_INSNS (BRANCH_COST (optimize_bb_for_speed_p (then_edge->src),
4007 predictable_edge_p (then_edge)))))
4008 return FALSE;
4010 if (else_bb == EXIT_BLOCK_PTR_FOR_FN (cfun))
4012 rtx_insn *jump = BB_END (else_edge->src);
4013 gcc_assert (JUMP_P (jump));
4014 else_target = JUMP_LABEL (jump);
4017 /* Registers set are dead, or are predicable. */
4018 if (! dead_or_predicable (test_bb, then_bb, else_bb,
4019 single_succ_edge (then_bb), 1))
4020 return FALSE;
4022 /* Conversion went ok, including moving the insns and fixing up the
4023 jump. Adjust the CFG to match. */
4025 /* We can avoid creating a new basic block if then_bb is immediately
4026 followed by else_bb, i.e. deleting then_bb allows test_bb to fall
4027 through to else_bb. */
4029 if (then_bb->next_bb == else_bb
4030 && then_bb->prev_bb == test_bb
4031 && else_bb != EXIT_BLOCK_PTR_FOR_FN (cfun))
4033 redirect_edge_succ (FALLTHRU_EDGE (test_bb), else_bb);
4034 new_bb = 0;
4036 else if (else_bb == EXIT_BLOCK_PTR_FOR_FN (cfun))
4037 new_bb = force_nonfallthru_and_redirect (FALLTHRU_EDGE (test_bb),
4038 else_bb, else_target);
4039 else
4040 new_bb = redirect_edge_and_branch_force (FALLTHRU_EDGE (test_bb),
4041 else_bb);
4043 df_set_bb_dirty (test_bb);
4044 df_set_bb_dirty (else_bb);
4046 then_bb_index = then_bb->index;
4047 delete_basic_block (then_bb);
4049 /* Make rest of code believe that the newly created block is the THEN_BB
4050 block we removed. */
4051 if (new_bb)
4053 df_bb_replace (then_bb_index, new_bb);
4054 /* This should have been done above via force_nonfallthru_and_redirect
4055 (possibly called from redirect_edge_and_branch_force). */
4056 gcc_checking_assert (BB_PARTITION (new_bb) == BB_PARTITION (test_bb));
4059 num_true_changes++;
4060 num_updated_if_blocks++;
4062 return TRUE;
4065 /* Test for case 2 above. */
4067 static int
4068 find_if_case_2 (basic_block test_bb, edge then_edge, edge else_edge)
4070 basic_block then_bb = then_edge->dest;
4071 basic_block else_bb = else_edge->dest;
4072 edge else_succ;
4073 int then_prob, else_prob;
4075 /* We do not want to speculate (empty) loop latches. */
4076 if (current_loops
4077 && else_bb->loop_father->latch == else_bb)
4078 return FALSE;
4080 /* If we are partitioning hot/cold basic blocks, we don't want to
4081 mess up unconditional or indirect jumps that cross between hot
4082 and cold sections.
4084 Basic block partitioning may result in some jumps that appear to
4085 be optimizable (or blocks that appear to be mergeable), but which really
4086 must be left untouched (they are required to make it safely across
4087 partition boundaries). See the comments at the top of
4088 bb-reorder.c:partition_hot_cold_basic_blocks for complete details. */
4090 if ((BB_END (then_bb)
4091 && JUMP_P (BB_END (then_bb))
4092 && CROSSING_JUMP_P (BB_END (then_bb)))
4093 || (BB_END (test_bb)
4094 && JUMP_P (BB_END (test_bb))
4095 && CROSSING_JUMP_P (BB_END (test_bb)))
4096 || (BB_END (else_bb)
4097 && JUMP_P (BB_END (else_bb))
4098 && CROSSING_JUMP_P (BB_END (else_bb))))
4099 return FALSE;
4101 /* ELSE has one successor. */
4102 if (!single_succ_p (else_bb))
4103 return FALSE;
4104 else
4105 else_succ = single_succ_edge (else_bb);
4107 /* ELSE outgoing edge is not complex. */
4108 if (else_succ->flags & EDGE_COMPLEX)
4109 return FALSE;
4111 /* ELSE has one predecessor. */
4112 if (!single_pred_p (else_bb))
4113 return FALSE;
4115 /* THEN is not EXIT. */
4116 if (then_bb->index < NUM_FIXED_BLOCKS)
4117 return FALSE;
4119 if (else_edge->probability)
4121 else_prob = else_edge->probability;
4122 then_prob = REG_BR_PROB_BASE - else_prob;
4124 else
4126 else_prob = REG_BR_PROB_BASE / 2;
4127 then_prob = REG_BR_PROB_BASE / 2;
4130 /* ELSE is predicted or SUCC(ELSE) postdominates THEN. */
4131 if (else_prob > then_prob)
4133 else if (else_succ->dest->index < NUM_FIXED_BLOCKS
4134 || dominated_by_p (CDI_POST_DOMINATORS, then_bb,
4135 else_succ->dest))
4137 else
4138 return FALSE;
4140 num_possible_if_blocks++;
4141 if (dump_file)
4142 fprintf (dump_file,
4143 "\nIF-CASE-2 found, start %d, else %d\n",
4144 test_bb->index, else_bb->index);
4146 /* We're speculating from the ELSE path, we want to make sure the cost
4147 of speculation is within reason. */
4148 if (! cheap_bb_rtx_cost_p (else_bb, else_prob,
4149 COSTS_N_INSNS (BRANCH_COST (optimize_bb_for_speed_p (else_edge->src),
4150 predictable_edge_p (else_edge)))))
4151 return FALSE;
4153 /* Registers set are dead, or are predicable. */
4154 if (! dead_or_predicable (test_bb, else_bb, then_bb, else_succ, 0))
4155 return FALSE;
4157 /* Conversion went ok, including moving the insns and fixing up the
4158 jump. Adjust the CFG to match. */
4160 df_set_bb_dirty (test_bb);
4161 df_set_bb_dirty (then_bb);
4162 delete_basic_block (else_bb);
4164 num_true_changes++;
4165 num_updated_if_blocks++;
4167 /* ??? We may now fallthru from one of THEN's successors into a join
4168 block. Rerun cleanup_cfg? Examine things manually? Wait? */
4170 return TRUE;
4173 /* Used by the code above to perform the actual rtl transformations.
4174 Return TRUE if successful.
4176 TEST_BB is the block containing the conditional branch. MERGE_BB
4177 is the block containing the code to manipulate. DEST_EDGE is an
4178 edge representing a jump to the join block; after the conversion,
4179 TEST_BB should be branching to its destination.
4180 REVERSEP is true if the sense of the branch should be reversed. */
4182 static int
4183 dead_or_predicable (basic_block test_bb, basic_block merge_bb,
4184 basic_block other_bb, edge dest_edge, int reversep)
4186 basic_block new_dest = dest_edge->dest;
4187 rtx_insn *head, *end, *jump;
4188 rtx_insn *earliest = NULL;
4189 rtx old_dest;
4190 bitmap merge_set = NULL;
4191 /* Number of pending changes. */
4192 int n_validated_changes = 0;
4193 rtx new_dest_label = NULL_RTX;
4195 jump = BB_END (test_bb);
4197 /* Find the extent of the real code in the merge block. */
4198 head = BB_HEAD (merge_bb);
4199 end = BB_END (merge_bb);
4201 while (DEBUG_INSN_P (end) && end != head)
4202 end = PREV_INSN (end);
4204 /* If merge_bb ends with a tablejump, predicating/moving insn's
4205 into test_bb and then deleting merge_bb will result in the jumptable
4206 that follows merge_bb being removed along with merge_bb and then we
4207 get an unresolved reference to the jumptable. */
4208 if (tablejump_p (end, NULL, NULL))
4209 return FALSE;
4211 if (LABEL_P (head))
4212 head = NEXT_INSN (head);
4213 while (DEBUG_INSN_P (head) && head != end)
4214 head = NEXT_INSN (head);
4215 if (NOTE_P (head))
4217 if (head == end)
4219 head = end = NULL;
4220 goto no_body;
4222 head = NEXT_INSN (head);
4223 while (DEBUG_INSN_P (head) && head != end)
4224 head = NEXT_INSN (head);
4227 if (JUMP_P (end))
4229 if (!onlyjump_p (end))
4230 return FALSE;
4231 if (head == end)
4233 head = end = NULL;
4234 goto no_body;
4236 end = PREV_INSN (end);
4237 while (DEBUG_INSN_P (end) && end != head)
4238 end = PREV_INSN (end);
4241 /* Don't move frame-related insn across the conditional branch. This
4242 can lead to one of the paths of the branch having wrong unwind info. */
4243 if (epilogue_completed)
4245 rtx_insn *insn = head;
4246 while (1)
4248 if (INSN_P (insn) && RTX_FRAME_RELATED_P (insn))
4249 return FALSE;
4250 if (insn == end)
4251 break;
4252 insn = NEXT_INSN (insn);
4256 /* Disable handling dead code by conditional execution if the machine needs
4257 to do anything funny with the tests, etc. */
4258 #ifndef IFCVT_MODIFY_TESTS
4259 if (targetm.have_conditional_execution ())
4261 /* In the conditional execution case, we have things easy. We know
4262 the condition is reversible. We don't have to check life info
4263 because we're going to conditionally execute the code anyway.
4264 All that's left is making sure the insns involved can actually
4265 be predicated. */
4267 rtx cond;
4269 cond = cond_exec_get_condition (jump);
4270 if (! cond)
4271 return FALSE;
4273 rtx note = find_reg_note (jump, REG_BR_PROB, NULL_RTX);
4274 int prob_val = (note ? XINT (note, 0) : -1);
4276 if (reversep)
4278 enum rtx_code rev = reversed_comparison_code (cond, jump);
4279 if (rev == UNKNOWN)
4280 return FALSE;
4281 cond = gen_rtx_fmt_ee (rev, GET_MODE (cond), XEXP (cond, 0),
4282 XEXP (cond, 1));
4283 if (prob_val >= 0)
4284 prob_val = REG_BR_PROB_BASE - prob_val;
4287 if (cond_exec_process_insns (NULL, head, end, cond, prob_val, 0)
4288 && verify_changes (0))
4289 n_validated_changes = num_validated_changes ();
4290 else
4291 cancel_changes (0);
4293 earliest = jump;
4295 #endif
4297 /* If we allocated new pseudos (e.g. in the conditional move
4298 expander called from noce_emit_cmove), we must resize the
4299 array first. */
4300 if (max_regno < max_reg_num ())
4301 max_regno = max_reg_num ();
4303 /* Try the NCE path if the CE path did not result in any changes. */
4304 if (n_validated_changes == 0)
4306 rtx cond;
4307 rtx_insn *insn;
4308 regset live;
4309 bool success;
4311 /* In the non-conditional execution case, we have to verify that there
4312 are no trapping operations, no calls, no references to memory, and
4313 that any registers modified are dead at the branch site. */
4315 if (!any_condjump_p (jump))
4316 return FALSE;
4318 /* Find the extent of the conditional. */
4319 cond = noce_get_condition (jump, &earliest, false);
4320 if (!cond)
4321 return FALSE;
4323 live = BITMAP_ALLOC (&reg_obstack);
4324 simulate_backwards_to_point (merge_bb, live, end);
4325 success = can_move_insns_across (head, end, earliest, jump,
4326 merge_bb, live,
4327 df_get_live_in (other_bb), NULL);
4328 BITMAP_FREE (live);
4329 if (!success)
4330 return FALSE;
4332 /* Collect the set of registers set in MERGE_BB. */
4333 merge_set = BITMAP_ALLOC (&reg_obstack);
4335 FOR_BB_INSNS (merge_bb, insn)
4336 if (NONDEBUG_INSN_P (insn))
4337 df_simulate_find_defs (insn, merge_set);
4339 /* If shrink-wrapping, disable this optimization when test_bb is
4340 the first basic block and merge_bb exits. The idea is to not
4341 move code setting up a return register as that may clobber a
4342 register used to pass function parameters, which then must be
4343 saved in caller-saved regs. A caller-saved reg requires the
4344 prologue, killing a shrink-wrap opportunity. */
4345 if ((SHRINK_WRAPPING_ENABLED && !epilogue_completed)
4346 && ENTRY_BLOCK_PTR_FOR_FN (cfun)->next_bb == test_bb
4347 && single_succ_p (new_dest)
4348 && single_succ (new_dest) == EXIT_BLOCK_PTR_FOR_FN (cfun)
4349 && bitmap_intersect_p (df_get_live_in (new_dest), merge_set))
4351 regset return_regs;
4352 unsigned int i;
4354 return_regs = BITMAP_ALLOC (&reg_obstack);
4356 /* Start off with the intersection of regs used to pass
4357 params and regs used to return values. */
4358 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
4359 if (FUNCTION_ARG_REGNO_P (i)
4360 && targetm.calls.function_value_regno_p (i))
4361 bitmap_set_bit (return_regs, INCOMING_REGNO (i));
4363 bitmap_and_into (return_regs,
4364 df_get_live_out (ENTRY_BLOCK_PTR_FOR_FN (cfun)));
4365 bitmap_and_into (return_regs,
4366 df_get_live_in (EXIT_BLOCK_PTR_FOR_FN (cfun)));
4367 if (!bitmap_empty_p (return_regs))
4369 FOR_BB_INSNS_REVERSE (new_dest, insn)
4370 if (NONDEBUG_INSN_P (insn))
4372 df_ref def;
4374 /* If this insn sets any reg in return_regs, add all
4375 reg uses to the set of regs we're interested in. */
4376 FOR_EACH_INSN_DEF (def, insn)
4377 if (bitmap_bit_p (return_regs, DF_REF_REGNO (def)))
4379 df_simulate_uses (insn, return_regs);
4380 break;
4383 if (bitmap_intersect_p (merge_set, return_regs))
4385 BITMAP_FREE (return_regs);
4386 BITMAP_FREE (merge_set);
4387 return FALSE;
4390 BITMAP_FREE (return_regs);
4394 no_body:
4395 /* We don't want to use normal invert_jump or redirect_jump because
4396 we don't want to delete_insn called. Also, we want to do our own
4397 change group management. */
4399 old_dest = JUMP_LABEL (jump);
4400 if (other_bb != new_dest)
4402 if (!any_condjump_p (jump))
4403 goto cancel;
4405 if (JUMP_P (BB_END (dest_edge->src)))
4406 new_dest_label = JUMP_LABEL (BB_END (dest_edge->src));
4407 else if (new_dest == EXIT_BLOCK_PTR_FOR_FN (cfun))
4408 new_dest_label = ret_rtx;
4409 else
4410 new_dest_label = block_label (new_dest);
4412 rtx_jump_insn *jump_insn = as_a <rtx_jump_insn *> (jump);
4413 if (reversep
4414 ? ! invert_jump_1 (jump_insn, new_dest_label)
4415 : ! redirect_jump_1 (jump_insn, new_dest_label))
4416 goto cancel;
4419 if (verify_changes (n_validated_changes))
4420 confirm_change_group ();
4421 else
4422 goto cancel;
4424 if (other_bb != new_dest)
4426 redirect_jump_2 (as_a <rtx_jump_insn *> (jump), old_dest, new_dest_label,
4427 0, reversep);
4429 redirect_edge_succ (BRANCH_EDGE (test_bb), new_dest);
4430 if (reversep)
4432 std::swap (BRANCH_EDGE (test_bb)->count,
4433 FALLTHRU_EDGE (test_bb)->count);
4434 std::swap (BRANCH_EDGE (test_bb)->probability,
4435 FALLTHRU_EDGE (test_bb)->probability);
4436 update_br_prob_note (test_bb);
4440 /* Move the insns out of MERGE_BB to before the branch. */
4441 if (head != NULL)
4443 rtx_insn *insn;
4445 if (end == BB_END (merge_bb))
4446 BB_END (merge_bb) = PREV_INSN (head);
4448 /* PR 21767: when moving insns above a conditional branch, the REG_EQUAL
4449 notes being moved might become invalid. */
4450 insn = head;
4453 rtx note;
4455 if (! INSN_P (insn))
4456 continue;
4457 note = find_reg_note (insn, REG_EQUAL, NULL_RTX);
4458 if (! note)
4459 continue;
4460 remove_note (insn, note);
4461 } while (insn != end && (insn = NEXT_INSN (insn)));
4463 /* PR46315: when moving insns above a conditional branch, the REG_EQUAL
4464 notes referring to the registers being set might become invalid. */
4465 if (merge_set)
4467 unsigned i;
4468 bitmap_iterator bi;
4470 EXECUTE_IF_SET_IN_BITMAP (merge_set, 0, i, bi)
4471 remove_reg_equal_equiv_notes_for_regno (i);
4473 BITMAP_FREE (merge_set);
4476 reorder_insns (head, end, PREV_INSN (earliest));
4479 /* Remove the jump and edge if we can. */
4480 if (other_bb == new_dest)
4482 delete_insn (jump);
4483 remove_edge (BRANCH_EDGE (test_bb));
4484 /* ??? Can't merge blocks here, as then_bb is still in use.
4485 At minimum, the merge will get done just before bb-reorder. */
4488 return TRUE;
4490 cancel:
4491 cancel_changes (0);
4493 if (merge_set)
4494 BITMAP_FREE (merge_set);
4496 return FALSE;
4499 /* Main entry point for all if-conversion. AFTER_COMBINE is true if
4500 we are after combine pass. */
4502 static void
4503 if_convert (bool after_combine)
4505 basic_block bb;
4506 int pass;
4508 if (optimize == 1)
4510 df_live_add_problem ();
4511 df_live_set_all_dirty ();
4514 /* Record whether we are after combine pass. */
4515 ifcvt_after_combine = after_combine;
4516 num_possible_if_blocks = 0;
4517 num_updated_if_blocks = 0;
4518 num_true_changes = 0;
4520 loop_optimizer_init (AVOID_CFG_MODIFICATIONS);
4521 mark_loop_exit_edges ();
4522 loop_optimizer_finalize ();
4523 free_dominance_info (CDI_DOMINATORS);
4525 /* Compute postdominators. */
4526 calculate_dominance_info (CDI_POST_DOMINATORS);
4528 df_set_flags (DF_LR_RUN_DCE);
4530 /* Go through each of the basic blocks looking for things to convert. If we
4531 have conditional execution, we make multiple passes to allow us to handle
4532 IF-THEN{-ELSE} blocks within other IF-THEN{-ELSE} blocks. */
4533 pass = 0;
4536 df_analyze ();
4537 /* Only need to do dce on the first pass. */
4538 df_clear_flags (DF_LR_RUN_DCE);
4539 cond_exec_changed_p = FALSE;
4540 pass++;
4542 #ifdef IFCVT_MULTIPLE_DUMPS
4543 if (dump_file && pass > 1)
4544 fprintf (dump_file, "\n\n========== Pass %d ==========\n", pass);
4545 #endif
4547 FOR_EACH_BB_FN (bb, cfun)
4549 basic_block new_bb;
4550 while (!df_get_bb_dirty (bb)
4551 && (new_bb = find_if_header (bb, pass)) != NULL)
4552 bb = new_bb;
4555 #ifdef IFCVT_MULTIPLE_DUMPS
4556 if (dump_file && cond_exec_changed_p)
4557 print_rtl_with_bb (dump_file, get_insns (), dump_flags);
4558 #endif
4560 while (cond_exec_changed_p);
4562 #ifdef IFCVT_MULTIPLE_DUMPS
4563 if (dump_file)
4564 fprintf (dump_file, "\n\n========== no more changes\n");
4565 #endif
4567 free_dominance_info (CDI_POST_DOMINATORS);
4569 if (dump_file)
4570 fflush (dump_file);
4572 clear_aux_for_blocks ();
4574 /* If we allocated new pseudos, we must resize the array for sched1. */
4575 if (max_regno < max_reg_num ())
4576 max_regno = max_reg_num ();
4578 /* Write the final stats. */
4579 if (dump_file && num_possible_if_blocks > 0)
4581 fprintf (dump_file,
4582 "\n%d possible IF blocks searched.\n",
4583 num_possible_if_blocks);
4584 fprintf (dump_file,
4585 "%d IF blocks converted.\n",
4586 num_updated_if_blocks);
4587 fprintf (dump_file,
4588 "%d true changes made.\n\n\n",
4589 num_true_changes);
4592 if (optimize == 1)
4593 df_remove_problem (df_live);
4595 #ifdef ENABLE_CHECKING
4596 verify_flow_info ();
4597 #endif
4600 /* If-conversion and CFG cleanup. */
4601 static unsigned int
4602 rest_of_handle_if_conversion (void)
4604 if (flag_if_conversion)
4606 if (dump_file)
4608 dump_reg_info (dump_file);
4609 dump_flow_info (dump_file, dump_flags);
4611 cleanup_cfg (CLEANUP_EXPENSIVE);
4612 if_convert (false);
4615 cleanup_cfg (0);
4616 return 0;
4619 namespace {
4621 const pass_data pass_data_rtl_ifcvt =
4623 RTL_PASS, /* type */
4624 "ce1", /* name */
4625 OPTGROUP_NONE, /* optinfo_flags */
4626 TV_IFCVT, /* tv_id */
4627 0, /* properties_required */
4628 0, /* properties_provided */
4629 0, /* properties_destroyed */
4630 0, /* todo_flags_start */
4631 TODO_df_finish, /* todo_flags_finish */
4634 class pass_rtl_ifcvt : public rtl_opt_pass
4636 public:
4637 pass_rtl_ifcvt (gcc::context *ctxt)
4638 : rtl_opt_pass (pass_data_rtl_ifcvt, ctxt)
4641 /* opt_pass methods: */
4642 virtual bool gate (function *)
4644 return (optimize > 0) && dbg_cnt (if_conversion);
4647 virtual unsigned int execute (function *)
4649 return rest_of_handle_if_conversion ();
4652 }; // class pass_rtl_ifcvt
4654 } // anon namespace
4656 rtl_opt_pass *
4657 make_pass_rtl_ifcvt (gcc::context *ctxt)
4659 return new pass_rtl_ifcvt (ctxt);
4663 /* Rerun if-conversion, as combine may have simplified things enough
4664 to now meet sequence length restrictions. */
4666 namespace {
4668 const pass_data pass_data_if_after_combine =
4670 RTL_PASS, /* type */
4671 "ce2", /* name */
4672 OPTGROUP_NONE, /* optinfo_flags */
4673 TV_IFCVT, /* tv_id */
4674 0, /* properties_required */
4675 0, /* properties_provided */
4676 0, /* properties_destroyed */
4677 0, /* todo_flags_start */
4678 TODO_df_finish, /* todo_flags_finish */
4681 class pass_if_after_combine : public rtl_opt_pass
4683 public:
4684 pass_if_after_combine (gcc::context *ctxt)
4685 : rtl_opt_pass (pass_data_if_after_combine, ctxt)
4688 /* opt_pass methods: */
4689 virtual bool gate (function *)
4691 return optimize > 0 && flag_if_conversion
4692 && dbg_cnt (if_after_combine);
4695 virtual unsigned int execute (function *)
4697 if_convert (true);
4698 return 0;
4701 }; // class pass_if_after_combine
4703 } // anon namespace
4705 rtl_opt_pass *
4706 make_pass_if_after_combine (gcc::context *ctxt)
4708 return new pass_if_after_combine (ctxt);
4712 namespace {
4714 const pass_data pass_data_if_after_reload =
4716 RTL_PASS, /* type */
4717 "ce3", /* name */
4718 OPTGROUP_NONE, /* optinfo_flags */
4719 TV_IFCVT2, /* tv_id */
4720 0, /* properties_required */
4721 0, /* properties_provided */
4722 0, /* properties_destroyed */
4723 0, /* todo_flags_start */
4724 TODO_df_finish, /* todo_flags_finish */
4727 class pass_if_after_reload : public rtl_opt_pass
4729 public:
4730 pass_if_after_reload (gcc::context *ctxt)
4731 : rtl_opt_pass (pass_data_if_after_reload, ctxt)
4734 /* opt_pass methods: */
4735 virtual bool gate (function *)
4737 return optimize > 0 && flag_if_conversion2
4738 && dbg_cnt (if_after_reload);
4741 virtual unsigned int execute (function *)
4743 if_convert (true);
4744 return 0;
4747 }; // class pass_if_after_reload
4749 } // anon namespace
4751 rtl_opt_pass *
4752 make_pass_if_after_reload (gcc::context *ctxt)
4754 return new pass_if_after_reload (ctxt);