Fix gnu11 fallout on SPARC
[official-gcc.git] / gcc / ifcvt.c
bloba28f5c13992f0a52c292390cd31111fb0ec8ec9c
1 /* If-conversion support.
2 Copyright (C) 2000-2014 Free Software Foundation, Inc.
4 This file is part of GCC.
6 GCC is free software; you can redistribute it and/or modify it
7 under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 3, or (at your option)
9 any later version.
11 GCC is distributed in the hope that it will be useful, but WITHOUT
12 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
13 or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
14 License for more details.
16 You should have received a copy of the GNU General Public License
17 along with GCC; see the file COPYING3. If not see
18 <http://www.gnu.org/licenses/>. */
20 #include "config.h"
21 #include "system.h"
22 #include "coretypes.h"
23 #include "tm.h"
25 #include "rtl.h"
26 #include "regs.h"
27 #include "hashtab.h"
28 #include "hash-set.h"
29 #include "vec.h"
30 #include "machmode.h"
31 #include "hard-reg-set.h"
32 #include "input.h"
33 #include "function.h"
34 #include "flags.h"
35 #include "insn-config.h"
36 #include "recog.h"
37 #include "except.h"
38 #include "basic-block.h"
39 #include "expr.h"
40 #include "output.h"
41 #include "optabs.h"
42 #include "diagnostic-core.h"
43 #include "tm_p.h"
44 #include "cfgloop.h"
45 #include "target.h"
46 #include "tree-pass.h"
47 #include "df.h"
48 #include "dbgcnt.h"
49 #include "shrink-wrap.h"
50 #include "ifcvt.h"
52 #ifndef HAVE_conditional_move
53 #define HAVE_conditional_move 0
54 #endif
55 #ifndef HAVE_incscc
56 #define HAVE_incscc 0
57 #endif
58 #ifndef HAVE_decscc
59 #define HAVE_decscc 0
60 #endif
61 #ifndef HAVE_trap
62 #define HAVE_trap 0
63 #endif
65 #ifndef MAX_CONDITIONAL_EXECUTE
66 #define MAX_CONDITIONAL_EXECUTE \
67 (BRANCH_COST (optimize_function_for_speed_p (cfun), false) \
68 + 1)
69 #endif
71 #define IFCVT_MULTIPLE_DUMPS 1
73 #define NULL_BLOCK ((basic_block) NULL)
75 /* True if after combine pass. */
76 static bool ifcvt_after_combine;
78 /* # of IF-THEN or IF-THEN-ELSE blocks we looked at */
79 static int num_possible_if_blocks;
81 /* # of IF-THEN or IF-THEN-ELSE blocks were converted to conditional
82 execution. */
83 static int num_updated_if_blocks;
85 /* # of changes made. */
86 static int num_true_changes;
88 /* Whether conditional execution changes were made. */
89 static int cond_exec_changed_p;
91 /* Forward references. */
92 static int count_bb_insns (const_basic_block);
93 static bool cheap_bb_rtx_cost_p (const_basic_block, int, int);
94 static rtx_insn *first_active_insn (basic_block);
95 static rtx_insn *last_active_insn (basic_block, int);
96 static rtx_insn *find_active_insn_before (basic_block, rtx_insn *);
97 static rtx_insn *find_active_insn_after (basic_block, rtx_insn *);
98 static basic_block block_fallthru (basic_block);
99 static int cond_exec_process_insns (ce_if_block *, rtx_insn *, rtx, rtx, int,
100 int);
101 static rtx cond_exec_get_condition (rtx_insn *);
102 static rtx noce_get_condition (rtx_insn *, rtx_insn **, bool);
103 static int noce_operand_ok (const_rtx);
104 static void merge_if_block (ce_if_block *);
105 static int find_cond_trap (basic_block, edge, edge);
106 static basic_block find_if_header (basic_block, int);
107 static int block_jumps_and_fallthru_p (basic_block, basic_block);
108 static int noce_find_if_block (basic_block, edge, edge, int);
109 static int cond_exec_find_if_block (ce_if_block *);
110 static int find_if_case_1 (basic_block, edge, edge);
111 static int find_if_case_2 (basic_block, edge, edge);
112 static int dead_or_predicable (basic_block, basic_block, basic_block,
113 edge, int);
114 static void noce_emit_move_insn (rtx, rtx);
115 static rtx_insn *block_has_only_trap (basic_block);
117 /* Count the number of non-jump active insns in BB. */
119 static int
120 count_bb_insns (const_basic_block bb)
122 int count = 0;
123 rtx_insn *insn = BB_HEAD (bb);
125 while (1)
127 if (active_insn_p (insn) && !JUMP_P (insn))
128 count++;
130 if (insn == BB_END (bb))
131 break;
132 insn = NEXT_INSN (insn);
135 return count;
138 /* Determine whether the total insn_rtx_cost on non-jump insns in
139 basic block BB is less than MAX_COST. This function returns
140 false if the cost of any instruction could not be estimated.
142 The cost of the non-jump insns in BB is scaled by REG_BR_PROB_BASE
143 as those insns are being speculated. MAX_COST is scaled with SCALE
144 plus a small fudge factor. */
146 static bool
147 cheap_bb_rtx_cost_p (const_basic_block bb, int scale, int max_cost)
149 int count = 0;
150 rtx_insn *insn = BB_HEAD (bb);
151 bool speed = optimize_bb_for_speed_p (bb);
153 /* Set scale to REG_BR_PROB_BASE to void the identical scaling
154 applied to insn_rtx_cost when optimizing for size. Only do
155 this after combine because if-conversion might interfere with
156 passes before combine.
158 Use optimize_function_for_speed_p instead of the pre-defined
159 variable speed to make sure it is set to same value for all
160 basic blocks in one if-conversion transformation. */
161 if (!optimize_function_for_speed_p (cfun) && ifcvt_after_combine)
162 scale = REG_BR_PROB_BASE;
163 /* Our branch probability/scaling factors are just estimates and don't
164 account for cases where we can get speculation for free and other
165 secondary benefits. So we fudge the scale factor to make speculating
166 appear a little more profitable when optimizing for performance. */
167 else
168 scale += REG_BR_PROB_BASE / 8;
171 max_cost *= scale;
173 while (1)
175 if (NONJUMP_INSN_P (insn))
177 int cost = insn_rtx_cost (PATTERN (insn), speed) * REG_BR_PROB_BASE;
178 if (cost == 0)
179 return false;
181 /* If this instruction is the load or set of a "stack" register,
182 such as a floating point register on x87, then the cost of
183 speculatively executing this insn may need to include
184 the additional cost of popping its result off of the
185 register stack. Unfortunately, correctly recognizing and
186 accounting for this additional overhead is tricky, so for
187 now we simply prohibit such speculative execution. */
188 #ifdef STACK_REGS
190 rtx set = single_set (insn);
191 if (set && STACK_REG_P (SET_DEST (set)))
192 return false;
194 #endif
196 count += cost;
197 if (count >= max_cost)
198 return false;
200 else if (CALL_P (insn))
201 return false;
203 if (insn == BB_END (bb))
204 break;
205 insn = NEXT_INSN (insn);
208 return true;
211 /* Return the first non-jump active insn in the basic block. */
213 static rtx_insn *
214 first_active_insn (basic_block bb)
216 rtx_insn *insn = BB_HEAD (bb);
218 if (LABEL_P (insn))
220 if (insn == BB_END (bb))
221 return NULL;
222 insn = NEXT_INSN (insn);
225 while (NOTE_P (insn) || DEBUG_INSN_P (insn))
227 if (insn == BB_END (bb))
228 return NULL;
229 insn = NEXT_INSN (insn);
232 if (JUMP_P (insn))
233 return NULL;
235 return insn;
238 /* Return the last non-jump active (non-jump) insn in the basic block. */
240 static rtx_insn *
241 last_active_insn (basic_block bb, int skip_use_p)
243 rtx_insn *insn = BB_END (bb);
244 rtx_insn *head = BB_HEAD (bb);
246 while (NOTE_P (insn)
247 || JUMP_P (insn)
248 || DEBUG_INSN_P (insn)
249 || (skip_use_p
250 && NONJUMP_INSN_P (insn)
251 && GET_CODE (PATTERN (insn)) == USE))
253 if (insn == head)
254 return NULL;
255 insn = PREV_INSN (insn);
258 if (LABEL_P (insn))
259 return NULL;
261 return insn;
264 /* Return the active insn before INSN inside basic block CURR_BB. */
266 static rtx_insn *
267 find_active_insn_before (basic_block curr_bb, rtx_insn *insn)
269 if (!insn || insn == BB_HEAD (curr_bb))
270 return NULL;
272 while ((insn = PREV_INSN (insn)) != NULL_RTX)
274 if (NONJUMP_INSN_P (insn) || JUMP_P (insn) || CALL_P (insn))
275 break;
277 /* No other active insn all the way to the start of the basic block. */
278 if (insn == BB_HEAD (curr_bb))
279 return NULL;
282 return insn;
285 /* Return the active insn after INSN inside basic block CURR_BB. */
287 static rtx_insn *
288 find_active_insn_after (basic_block curr_bb, rtx_insn *insn)
290 if (!insn || insn == BB_END (curr_bb))
291 return NULL;
293 while ((insn = NEXT_INSN (insn)) != NULL_RTX)
295 if (NONJUMP_INSN_P (insn) || JUMP_P (insn) || CALL_P (insn))
296 break;
298 /* No other active insn all the way to the end of the basic block. */
299 if (insn == BB_END (curr_bb))
300 return NULL;
303 return insn;
306 /* Return the basic block reached by falling though the basic block BB. */
308 static basic_block
309 block_fallthru (basic_block bb)
311 edge e = find_fallthru_edge (bb->succs);
313 return (e) ? e->dest : NULL_BLOCK;
316 /* Return true if RTXs A and B can be safely interchanged. */
318 static bool
319 rtx_interchangeable_p (const_rtx a, const_rtx b)
321 if (!rtx_equal_p (a, b))
322 return false;
324 if (GET_CODE (a) != MEM)
325 return true;
327 /* A dead type-unsafe memory reference is legal, but a live type-unsafe memory
328 reference is not. Interchanging a dead type-unsafe memory reference with
329 a live type-safe one creates a live type-unsafe memory reference, in other
330 words, it makes the program illegal.
331 We check here conservatively whether the two memory references have equal
332 memory attributes. */
334 return mem_attrs_eq_p (get_mem_attrs (a), get_mem_attrs (b));
338 /* Go through a bunch of insns, converting them to conditional
339 execution format if possible. Return TRUE if all of the non-note
340 insns were processed. */
342 static int
343 cond_exec_process_insns (ce_if_block *ce_info ATTRIBUTE_UNUSED,
344 /* if block information */rtx_insn *start,
345 /* first insn to look at */rtx end,
346 /* last insn to look at */rtx test,
347 /* conditional execution test */int prob_val,
348 /* probability of branch taken. */int mod_ok)
350 int must_be_last = FALSE;
351 rtx_insn *insn;
352 rtx xtest;
353 rtx pattern;
355 if (!start || !end)
356 return FALSE;
358 for (insn = start; ; insn = NEXT_INSN (insn))
360 /* dwarf2out can't cope with conditional prologues. */
361 if (NOTE_P (insn) && NOTE_KIND (insn) == NOTE_INSN_PROLOGUE_END)
362 return FALSE;
364 if (NOTE_P (insn) || DEBUG_INSN_P (insn))
365 goto insn_done;
367 gcc_assert (NONJUMP_INSN_P (insn) || CALL_P (insn));
369 /* dwarf2out can't cope with conditional unwind info. */
370 if (RTX_FRAME_RELATED_P (insn))
371 return FALSE;
373 /* Remove USE insns that get in the way. */
374 if (reload_completed && GET_CODE (PATTERN (insn)) == USE)
376 /* ??? Ug. Actually unlinking the thing is problematic,
377 given what we'd have to coordinate with our callers. */
378 SET_INSN_DELETED (insn);
379 goto insn_done;
382 /* Last insn wasn't last? */
383 if (must_be_last)
384 return FALSE;
386 if (modified_in_p (test, insn))
388 if (!mod_ok)
389 return FALSE;
390 must_be_last = TRUE;
393 /* Now build the conditional form of the instruction. */
394 pattern = PATTERN (insn);
395 xtest = copy_rtx (test);
397 /* If this is already a COND_EXEC, rewrite the test to be an AND of the
398 two conditions. */
399 if (GET_CODE (pattern) == COND_EXEC)
401 if (GET_MODE (xtest) != GET_MODE (COND_EXEC_TEST (pattern)))
402 return FALSE;
404 xtest = gen_rtx_AND (GET_MODE (xtest), xtest,
405 COND_EXEC_TEST (pattern));
406 pattern = COND_EXEC_CODE (pattern);
409 pattern = gen_rtx_COND_EXEC (VOIDmode, xtest, pattern);
411 /* If the machine needs to modify the insn being conditionally executed,
412 say for example to force a constant integer operand into a temp
413 register, do so here. */
414 #ifdef IFCVT_MODIFY_INSN
415 IFCVT_MODIFY_INSN (ce_info, pattern, insn);
416 if (! pattern)
417 return FALSE;
418 #endif
420 validate_change (insn, &PATTERN (insn), pattern, 1);
422 if (CALL_P (insn) && prob_val >= 0)
423 validate_change (insn, &REG_NOTES (insn),
424 gen_rtx_INT_LIST ((enum machine_mode) REG_BR_PROB,
425 prob_val, REG_NOTES (insn)), 1);
427 insn_done:
428 if (insn == end)
429 break;
432 return TRUE;
435 /* Return the condition for a jump. Do not do any special processing. */
437 static rtx
438 cond_exec_get_condition (rtx_insn *jump)
440 rtx test_if, cond;
442 if (any_condjump_p (jump))
443 test_if = SET_SRC (pc_set (jump));
444 else
445 return NULL_RTX;
446 cond = XEXP (test_if, 0);
448 /* If this branches to JUMP_LABEL when the condition is false,
449 reverse the condition. */
450 if (GET_CODE (XEXP (test_if, 2)) == LABEL_REF
451 && LABEL_REF_LABEL (XEXP (test_if, 2)) == JUMP_LABEL (jump))
453 enum rtx_code rev = reversed_comparison_code (cond, jump);
454 if (rev == UNKNOWN)
455 return NULL_RTX;
457 cond = gen_rtx_fmt_ee (rev, GET_MODE (cond), XEXP (cond, 0),
458 XEXP (cond, 1));
461 return cond;
464 /* Given a simple IF-THEN or IF-THEN-ELSE block, attempt to convert it
465 to conditional execution. Return TRUE if we were successful at
466 converting the block. */
468 static int
469 cond_exec_process_if_block (ce_if_block * ce_info,
470 /* if block information */int do_multiple_p)
472 basic_block test_bb = ce_info->test_bb; /* last test block */
473 basic_block then_bb = ce_info->then_bb; /* THEN */
474 basic_block else_bb = ce_info->else_bb; /* ELSE or NULL */
475 rtx test_expr; /* expression in IF_THEN_ELSE that is tested */
476 rtx_insn *then_start; /* first insn in THEN block */
477 rtx_insn *then_end; /* last insn + 1 in THEN block */
478 rtx_insn *else_start = NULL; /* first insn in ELSE block or NULL */
479 rtx_insn *else_end = NULL; /* last insn + 1 in ELSE block */
480 int max; /* max # of insns to convert. */
481 int then_mod_ok; /* whether conditional mods are ok in THEN */
482 rtx true_expr; /* test for else block insns */
483 rtx false_expr; /* test for then block insns */
484 int true_prob_val; /* probability of else block */
485 int false_prob_val; /* probability of then block */
486 rtx_insn *then_last_head = NULL; /* Last match at the head of THEN */
487 rtx_insn *else_last_head = NULL; /* Last match at the head of ELSE */
488 rtx_insn *then_first_tail = NULL; /* First match at the tail of THEN */
489 rtx_insn *else_first_tail = NULL; /* First match at the tail of ELSE */
490 int then_n_insns, else_n_insns, n_insns;
491 enum rtx_code false_code;
492 rtx note;
494 /* If test is comprised of && or || elements, and we've failed at handling
495 all of them together, just use the last test if it is the special case of
496 && elements without an ELSE block. */
497 if (!do_multiple_p && ce_info->num_multiple_test_blocks)
499 if (else_bb || ! ce_info->and_and_p)
500 return FALSE;
502 ce_info->test_bb = test_bb = ce_info->last_test_bb;
503 ce_info->num_multiple_test_blocks = 0;
504 ce_info->num_and_and_blocks = 0;
505 ce_info->num_or_or_blocks = 0;
508 /* Find the conditional jump to the ELSE or JOIN part, and isolate
509 the test. */
510 test_expr = cond_exec_get_condition (BB_END (test_bb));
511 if (! test_expr)
512 return FALSE;
514 /* If the conditional jump is more than just a conditional jump,
515 then we can not do conditional execution conversion on this block. */
516 if (! onlyjump_p (BB_END (test_bb)))
517 return FALSE;
519 /* Collect the bounds of where we're to search, skipping any labels, jumps
520 and notes at the beginning and end of the block. Then count the total
521 number of insns and see if it is small enough to convert. */
522 then_start = first_active_insn (then_bb);
523 then_end = last_active_insn (then_bb, TRUE);
524 then_n_insns = ce_info->num_then_insns = count_bb_insns (then_bb);
525 n_insns = then_n_insns;
526 max = MAX_CONDITIONAL_EXECUTE;
528 if (else_bb)
530 int n_matching;
532 max *= 2;
533 else_start = first_active_insn (else_bb);
534 else_end = last_active_insn (else_bb, TRUE);
535 else_n_insns = ce_info->num_else_insns = count_bb_insns (else_bb);
536 n_insns += else_n_insns;
538 /* Look for matching sequences at the head and tail of the two blocks,
539 and limit the range of insns to be converted if possible. */
540 n_matching = flow_find_cross_jump (then_bb, else_bb,
541 &then_first_tail, &else_first_tail,
542 NULL);
543 if (then_first_tail == BB_HEAD (then_bb))
544 then_start = then_end = NULL;
545 if (else_first_tail == BB_HEAD (else_bb))
546 else_start = else_end = NULL;
548 if (n_matching > 0)
550 if (then_end)
551 then_end = find_active_insn_before (then_bb, then_first_tail);
552 if (else_end)
553 else_end = find_active_insn_before (else_bb, else_first_tail);
554 n_insns -= 2 * n_matching;
557 if (then_start
558 && else_start
559 && then_n_insns > n_matching
560 && else_n_insns > n_matching)
562 int longest_match = MIN (then_n_insns - n_matching,
563 else_n_insns - n_matching);
564 n_matching
565 = flow_find_head_matching_sequence (then_bb, else_bb,
566 &then_last_head,
567 &else_last_head,
568 longest_match);
570 if (n_matching > 0)
572 rtx_insn *insn;
574 /* We won't pass the insns in the head sequence to
575 cond_exec_process_insns, so we need to test them here
576 to make sure that they don't clobber the condition. */
577 for (insn = BB_HEAD (then_bb);
578 insn != NEXT_INSN (then_last_head);
579 insn = NEXT_INSN (insn))
580 if (!LABEL_P (insn) && !NOTE_P (insn)
581 && !DEBUG_INSN_P (insn)
582 && modified_in_p (test_expr, insn))
583 return FALSE;
586 if (then_last_head == then_end)
587 then_start = then_end = NULL;
588 if (else_last_head == else_end)
589 else_start = else_end = NULL;
591 if (n_matching > 0)
593 if (then_start)
594 then_start = find_active_insn_after (then_bb, then_last_head);
595 if (else_start)
596 else_start = find_active_insn_after (else_bb, else_last_head);
597 n_insns -= 2 * n_matching;
602 if (n_insns > max)
603 return FALSE;
605 /* Map test_expr/test_jump into the appropriate MD tests to use on
606 the conditionally executed code. */
608 true_expr = test_expr;
610 false_code = reversed_comparison_code (true_expr, BB_END (test_bb));
611 if (false_code != UNKNOWN)
612 false_expr = gen_rtx_fmt_ee (false_code, GET_MODE (true_expr),
613 XEXP (true_expr, 0), XEXP (true_expr, 1));
614 else
615 false_expr = NULL_RTX;
617 #ifdef IFCVT_MODIFY_TESTS
618 /* If the machine description needs to modify the tests, such as setting a
619 conditional execution register from a comparison, it can do so here. */
620 IFCVT_MODIFY_TESTS (ce_info, true_expr, false_expr);
622 /* See if the conversion failed. */
623 if (!true_expr || !false_expr)
624 goto fail;
625 #endif
627 note = find_reg_note (BB_END (test_bb), REG_BR_PROB, NULL_RTX);
628 if (note)
630 true_prob_val = XINT (note, 0);
631 false_prob_val = REG_BR_PROB_BASE - true_prob_val;
633 else
635 true_prob_val = -1;
636 false_prob_val = -1;
639 /* If we have && or || tests, do them here. These tests are in the adjacent
640 blocks after the first block containing the test. */
641 if (ce_info->num_multiple_test_blocks > 0)
643 basic_block bb = test_bb;
644 basic_block last_test_bb = ce_info->last_test_bb;
646 if (! false_expr)
647 goto fail;
651 rtx_insn *start, *end;
652 rtx t, f;
653 enum rtx_code f_code;
655 bb = block_fallthru (bb);
656 start = first_active_insn (bb);
657 end = last_active_insn (bb, TRUE);
658 if (start
659 && ! cond_exec_process_insns (ce_info, start, end, false_expr,
660 false_prob_val, FALSE))
661 goto fail;
663 /* If the conditional jump is more than just a conditional jump, then
664 we can not do conditional execution conversion on this block. */
665 if (! onlyjump_p (BB_END (bb)))
666 goto fail;
668 /* Find the conditional jump and isolate the test. */
669 t = cond_exec_get_condition (BB_END (bb));
670 if (! t)
671 goto fail;
673 f_code = reversed_comparison_code (t, BB_END (bb));
674 if (f_code == UNKNOWN)
675 goto fail;
677 f = gen_rtx_fmt_ee (f_code, GET_MODE (t), XEXP (t, 0), XEXP (t, 1));
678 if (ce_info->and_and_p)
680 t = gen_rtx_AND (GET_MODE (t), true_expr, t);
681 f = gen_rtx_IOR (GET_MODE (t), false_expr, f);
683 else
685 t = gen_rtx_IOR (GET_MODE (t), true_expr, t);
686 f = gen_rtx_AND (GET_MODE (t), false_expr, f);
689 /* If the machine description needs to modify the tests, such as
690 setting a conditional execution register from a comparison, it can
691 do so here. */
692 #ifdef IFCVT_MODIFY_MULTIPLE_TESTS
693 IFCVT_MODIFY_MULTIPLE_TESTS (ce_info, bb, t, f);
695 /* See if the conversion failed. */
696 if (!t || !f)
697 goto fail;
698 #endif
700 true_expr = t;
701 false_expr = f;
703 while (bb != last_test_bb);
706 /* For IF-THEN-ELSE blocks, we don't allow modifications of the test
707 on then THEN block. */
708 then_mod_ok = (else_bb == NULL_BLOCK);
710 /* Go through the THEN and ELSE blocks converting the insns if possible
711 to conditional execution. */
713 if (then_end
714 && (! false_expr
715 || ! cond_exec_process_insns (ce_info, then_start, then_end,
716 false_expr, false_prob_val,
717 then_mod_ok)))
718 goto fail;
720 if (else_bb && else_end
721 && ! cond_exec_process_insns (ce_info, else_start, else_end,
722 true_expr, true_prob_val, TRUE))
723 goto fail;
725 /* If we cannot apply the changes, fail. Do not go through the normal fail
726 processing, since apply_change_group will call cancel_changes. */
727 if (! apply_change_group ())
729 #ifdef IFCVT_MODIFY_CANCEL
730 /* Cancel any machine dependent changes. */
731 IFCVT_MODIFY_CANCEL (ce_info);
732 #endif
733 return FALSE;
736 #ifdef IFCVT_MODIFY_FINAL
737 /* Do any machine dependent final modifications. */
738 IFCVT_MODIFY_FINAL (ce_info);
739 #endif
741 /* Conversion succeeded. */
742 if (dump_file)
743 fprintf (dump_file, "%d insn%s converted to conditional execution.\n",
744 n_insns, (n_insns == 1) ? " was" : "s were");
746 /* Merge the blocks! If we had matching sequences, make sure to delete one
747 copy at the appropriate location first: delete the copy in the THEN branch
748 for a tail sequence so that the remaining one is executed last for both
749 branches, and delete the copy in the ELSE branch for a head sequence so
750 that the remaining one is executed first for both branches. */
751 if (then_first_tail)
753 rtx_insn *from = then_first_tail;
754 if (!INSN_P (from))
755 from = find_active_insn_after (then_bb, from);
756 delete_insn_chain (from, BB_END (then_bb), false);
758 if (else_last_head)
759 delete_insn_chain (first_active_insn (else_bb), else_last_head, false);
761 merge_if_block (ce_info);
762 cond_exec_changed_p = TRUE;
763 return TRUE;
765 fail:
766 #ifdef IFCVT_MODIFY_CANCEL
767 /* Cancel any machine dependent changes. */
768 IFCVT_MODIFY_CANCEL (ce_info);
769 #endif
771 cancel_changes (0);
772 return FALSE;
775 /* Used by noce_process_if_block to communicate with its subroutines.
777 The subroutines know that A and B may be evaluated freely. They
778 know that X is a register. They should insert new instructions
779 before cond_earliest. */
781 struct noce_if_info
783 /* The basic blocks that make up the IF-THEN-{ELSE-,}JOIN block. */
784 basic_block test_bb, then_bb, else_bb, join_bb;
786 /* The jump that ends TEST_BB. */
787 rtx_insn *jump;
789 /* The jump condition. */
790 rtx cond;
792 /* New insns should be inserted before this one. */
793 rtx_insn *cond_earliest;
795 /* Insns in the THEN and ELSE block. There is always just this
796 one insns in those blocks. The insns are single_set insns.
797 If there was no ELSE block, INSN_B is the last insn before
798 COND_EARLIEST, or NULL_RTX. In the former case, the insn
799 operands are still valid, as if INSN_B was moved down below
800 the jump. */
801 rtx_insn *insn_a, *insn_b;
803 /* The SET_SRC of INSN_A and INSN_B. */
804 rtx a, b;
806 /* The SET_DEST of INSN_A. */
807 rtx x;
809 /* True if this if block is not canonical. In the canonical form of
810 if blocks, the THEN_BB is the block reached via the fallthru edge
811 from TEST_BB. For the noce transformations, we allow the symmetric
812 form as well. */
813 bool then_else_reversed;
815 /* Estimated cost of the particular branch instruction. */
816 int branch_cost;
819 static rtx noce_emit_store_flag (struct noce_if_info *, rtx, int, int);
820 static int noce_try_move (struct noce_if_info *);
821 static int noce_try_store_flag (struct noce_if_info *);
822 static int noce_try_addcc (struct noce_if_info *);
823 static int noce_try_store_flag_constants (struct noce_if_info *);
824 static int noce_try_store_flag_mask (struct noce_if_info *);
825 static rtx noce_emit_cmove (struct noce_if_info *, rtx, enum rtx_code, rtx,
826 rtx, rtx, rtx);
827 static int noce_try_cmove (struct noce_if_info *);
828 static int noce_try_cmove_arith (struct noce_if_info *);
829 static rtx noce_get_alt_condition (struct noce_if_info *, rtx, rtx_insn **);
830 static int noce_try_minmax (struct noce_if_info *);
831 static int noce_try_abs (struct noce_if_info *);
832 static int noce_try_sign_mask (struct noce_if_info *);
834 /* Helper function for noce_try_store_flag*. */
836 static rtx
837 noce_emit_store_flag (struct noce_if_info *if_info, rtx x, int reversep,
838 int normalize)
840 rtx cond = if_info->cond;
841 int cond_complex;
842 enum rtx_code code;
844 cond_complex = (! general_operand (XEXP (cond, 0), VOIDmode)
845 || ! general_operand (XEXP (cond, 1), VOIDmode));
847 /* If earliest == jump, or when the condition is complex, try to
848 build the store_flag insn directly. */
850 if (cond_complex)
852 rtx set = pc_set (if_info->jump);
853 cond = XEXP (SET_SRC (set), 0);
854 if (GET_CODE (XEXP (SET_SRC (set), 2)) == LABEL_REF
855 && LABEL_REF_LABEL (XEXP (SET_SRC (set), 2)) == JUMP_LABEL (if_info->jump))
856 reversep = !reversep;
857 if (if_info->then_else_reversed)
858 reversep = !reversep;
861 if (reversep)
862 code = reversed_comparison_code (cond, if_info->jump);
863 else
864 code = GET_CODE (cond);
866 if ((if_info->cond_earliest == if_info->jump || cond_complex)
867 && (normalize == 0 || STORE_FLAG_VALUE == normalize))
869 rtx src = gen_rtx_fmt_ee (code, GET_MODE (x), XEXP (cond, 0),
870 XEXP (cond, 1));
871 rtx set = gen_rtx_SET (VOIDmode, x, src);
873 start_sequence ();
874 rtx_insn *insn = emit_insn (set);
876 if (recog_memoized (insn) >= 0)
878 rtx_insn *seq = get_insns ();
879 end_sequence ();
880 emit_insn (seq);
882 if_info->cond_earliest = if_info->jump;
884 return x;
887 end_sequence ();
890 /* Don't even try if the comparison operands or the mode of X are weird. */
891 if (cond_complex || !SCALAR_INT_MODE_P (GET_MODE (x)))
892 return NULL_RTX;
894 return emit_store_flag (x, code, XEXP (cond, 0),
895 XEXP (cond, 1), VOIDmode,
896 (code == LTU || code == LEU
897 || code == GEU || code == GTU), normalize);
900 /* Emit instruction to move an rtx, possibly into STRICT_LOW_PART.
901 X is the destination/target and Y is the value to copy. */
903 static void
904 noce_emit_move_insn (rtx x, rtx y)
906 enum machine_mode outmode;
907 rtx outer, inner;
908 int bitpos;
910 if (GET_CODE (x) != STRICT_LOW_PART)
912 rtx_insn *seq, *insn;
913 rtx target;
914 optab ot;
916 start_sequence ();
917 /* Check that the SET_SRC is reasonable before calling emit_move_insn,
918 otherwise construct a suitable SET pattern ourselves. */
919 insn = (OBJECT_P (y) || CONSTANT_P (y) || GET_CODE (y) == SUBREG)
920 ? emit_move_insn (x, y)
921 : emit_insn (gen_rtx_SET (VOIDmode, x, y));
922 seq = get_insns ();
923 end_sequence ();
925 if (recog_memoized (insn) <= 0)
927 if (GET_CODE (x) == ZERO_EXTRACT)
929 rtx op = XEXP (x, 0);
930 unsigned HOST_WIDE_INT size = INTVAL (XEXP (x, 1));
931 unsigned HOST_WIDE_INT start = INTVAL (XEXP (x, 2));
933 /* store_bit_field expects START to be relative to
934 BYTES_BIG_ENDIAN and adjusts this value for machines with
935 BITS_BIG_ENDIAN != BYTES_BIG_ENDIAN. In order to be able to
936 invoke store_bit_field again it is necessary to have the START
937 value from the first call. */
938 if (BITS_BIG_ENDIAN != BYTES_BIG_ENDIAN)
940 if (MEM_P (op))
941 start = BITS_PER_UNIT - start - size;
942 else
944 gcc_assert (REG_P (op));
945 start = BITS_PER_WORD - start - size;
949 gcc_assert (start < (MEM_P (op) ? BITS_PER_UNIT : BITS_PER_WORD));
950 store_bit_field (op, size, start, 0, 0, GET_MODE (x), y);
951 return;
954 switch (GET_RTX_CLASS (GET_CODE (y)))
956 case RTX_UNARY:
957 ot = code_to_optab (GET_CODE (y));
958 if (ot)
960 start_sequence ();
961 target = expand_unop (GET_MODE (y), ot, XEXP (y, 0), x, 0);
962 if (target != NULL_RTX)
964 if (target != x)
965 emit_move_insn (x, target);
966 seq = get_insns ();
968 end_sequence ();
970 break;
972 case RTX_BIN_ARITH:
973 case RTX_COMM_ARITH:
974 ot = code_to_optab (GET_CODE (y));
975 if (ot)
977 start_sequence ();
978 target = expand_binop (GET_MODE (y), ot,
979 XEXP (y, 0), XEXP (y, 1),
980 x, 0, OPTAB_DIRECT);
981 if (target != NULL_RTX)
983 if (target != x)
984 emit_move_insn (x, target);
985 seq = get_insns ();
987 end_sequence ();
989 break;
991 default:
992 break;
996 emit_insn (seq);
997 return;
1000 outer = XEXP (x, 0);
1001 inner = XEXP (outer, 0);
1002 outmode = GET_MODE (outer);
1003 bitpos = SUBREG_BYTE (outer) * BITS_PER_UNIT;
1004 store_bit_field (inner, GET_MODE_BITSIZE (outmode), bitpos,
1005 0, 0, outmode, y);
1008 /* Return sequence of instructions generated by if conversion. This
1009 function calls end_sequence() to end the current stream, ensures
1010 that are instructions are unshared, recognizable non-jump insns.
1011 On failure, this function returns a NULL_RTX. */
1013 static rtx_insn *
1014 end_ifcvt_sequence (struct noce_if_info *if_info)
1016 rtx_insn *insn;
1017 rtx_insn *seq = get_insns ();
1019 set_used_flags (if_info->x);
1020 set_used_flags (if_info->cond);
1021 set_used_flags (if_info->a);
1022 set_used_flags (if_info->b);
1023 unshare_all_rtl_in_chain (seq);
1024 end_sequence ();
1026 /* Make sure that all of the instructions emitted are recognizable,
1027 and that we haven't introduced a new jump instruction.
1028 As an exercise for the reader, build a general mechanism that
1029 allows proper placement of required clobbers. */
1030 for (insn = seq; insn; insn = NEXT_INSN (insn))
1031 if (JUMP_P (insn)
1032 || recog_memoized (insn) == -1)
1033 return NULL;
1035 return seq;
1038 /* Convert "if (a != b) x = a; else x = b" into "x = a" and
1039 "if (a == b) x = a; else x = b" into "x = b". */
1041 static int
1042 noce_try_move (struct noce_if_info *if_info)
1044 rtx cond = if_info->cond;
1045 enum rtx_code code = GET_CODE (cond);
1046 rtx y;
1047 rtx_insn *seq;
1049 if (code != NE && code != EQ)
1050 return FALSE;
1052 /* This optimization isn't valid if either A or B could be a NaN
1053 or a signed zero. */
1054 if (HONOR_NANS (GET_MODE (if_info->x))
1055 || HONOR_SIGNED_ZEROS (GET_MODE (if_info->x)))
1056 return FALSE;
1058 /* Check whether the operands of the comparison are A and in
1059 either order. */
1060 if ((rtx_equal_p (if_info->a, XEXP (cond, 0))
1061 && rtx_equal_p (if_info->b, XEXP (cond, 1)))
1062 || (rtx_equal_p (if_info->a, XEXP (cond, 1))
1063 && rtx_equal_p (if_info->b, XEXP (cond, 0))))
1065 if (!rtx_interchangeable_p (if_info->a, if_info->b))
1066 return FALSE;
1068 y = (code == EQ) ? if_info->a : if_info->b;
1070 /* Avoid generating the move if the source is the destination. */
1071 if (! rtx_equal_p (if_info->x, y))
1073 start_sequence ();
1074 noce_emit_move_insn (if_info->x, y);
1075 seq = end_ifcvt_sequence (if_info);
1076 if (!seq)
1077 return FALSE;
1079 emit_insn_before_setloc (seq, if_info->jump,
1080 INSN_LOCATION (if_info->insn_a));
1082 return TRUE;
1084 return FALSE;
1087 /* Convert "if (test) x = 1; else x = 0".
1089 Only try 0 and STORE_FLAG_VALUE here. Other combinations will be
1090 tried in noce_try_store_flag_constants after noce_try_cmove has had
1091 a go at the conversion. */
1093 static int
1094 noce_try_store_flag (struct noce_if_info *if_info)
1096 int reversep;
1097 rtx target;
1098 rtx_insn *seq;
1100 if (CONST_INT_P (if_info->b)
1101 && INTVAL (if_info->b) == STORE_FLAG_VALUE
1102 && if_info->a == const0_rtx)
1103 reversep = 0;
1104 else if (if_info->b == const0_rtx
1105 && CONST_INT_P (if_info->a)
1106 && INTVAL (if_info->a) == STORE_FLAG_VALUE
1107 && (reversed_comparison_code (if_info->cond, if_info->jump)
1108 != UNKNOWN))
1109 reversep = 1;
1110 else
1111 return FALSE;
1113 start_sequence ();
1115 target = noce_emit_store_flag (if_info, if_info->x, reversep, 0);
1116 if (target)
1118 if (target != if_info->x)
1119 noce_emit_move_insn (if_info->x, target);
1121 seq = end_ifcvt_sequence (if_info);
1122 if (! seq)
1123 return FALSE;
1125 emit_insn_before_setloc (seq, if_info->jump,
1126 INSN_LOCATION (if_info->insn_a));
1127 return TRUE;
1129 else
1131 end_sequence ();
1132 return FALSE;
1136 /* Convert "if (test) x = a; else x = b", for A and B constant. */
1138 static int
1139 noce_try_store_flag_constants (struct noce_if_info *if_info)
1141 rtx target;
1142 rtx_insn *seq;
1143 int reversep;
1144 HOST_WIDE_INT itrue, ifalse, diff, tmp;
1145 int normalize, can_reverse;
1146 enum machine_mode mode;
1148 if (CONST_INT_P (if_info->a)
1149 && CONST_INT_P (if_info->b))
1151 mode = GET_MODE (if_info->x);
1152 ifalse = INTVAL (if_info->a);
1153 itrue = INTVAL (if_info->b);
1155 diff = (unsigned HOST_WIDE_INT) itrue - ifalse;
1156 /* Make sure we can represent the difference between the two values. */
1157 if ((diff > 0)
1158 != ((ifalse < 0) != (itrue < 0) ? ifalse < 0 : ifalse < itrue))
1159 return FALSE;
1161 diff = trunc_int_for_mode (diff, mode);
1163 can_reverse = (reversed_comparison_code (if_info->cond, if_info->jump)
1164 != UNKNOWN);
1166 reversep = 0;
1167 if (diff == STORE_FLAG_VALUE || diff == -STORE_FLAG_VALUE)
1168 normalize = 0;
1169 else if (ifalse == 0 && exact_log2 (itrue) >= 0
1170 && (STORE_FLAG_VALUE == 1
1171 || if_info->branch_cost >= 2))
1172 normalize = 1;
1173 else if (itrue == 0 && exact_log2 (ifalse) >= 0 && can_reverse
1174 && (STORE_FLAG_VALUE == 1 || if_info->branch_cost >= 2))
1175 normalize = 1, reversep = 1;
1176 else if (itrue == -1
1177 && (STORE_FLAG_VALUE == -1
1178 || if_info->branch_cost >= 2))
1179 normalize = -1;
1180 else if (ifalse == -1 && can_reverse
1181 && (STORE_FLAG_VALUE == -1 || if_info->branch_cost >= 2))
1182 normalize = -1, reversep = 1;
1183 else if ((if_info->branch_cost >= 2 && STORE_FLAG_VALUE == -1)
1184 || if_info->branch_cost >= 3)
1185 normalize = -1;
1186 else
1187 return FALSE;
1189 if (reversep)
1191 tmp = itrue; itrue = ifalse; ifalse = tmp;
1192 diff = trunc_int_for_mode (-(unsigned HOST_WIDE_INT) diff, mode);
1195 start_sequence ();
1196 target = noce_emit_store_flag (if_info, if_info->x, reversep, normalize);
1197 if (! target)
1199 end_sequence ();
1200 return FALSE;
1203 /* if (test) x = 3; else x = 4;
1204 => x = 3 + (test == 0); */
1205 if (diff == STORE_FLAG_VALUE || diff == -STORE_FLAG_VALUE)
1207 target = expand_simple_binop (mode,
1208 (diff == STORE_FLAG_VALUE
1209 ? PLUS : MINUS),
1210 gen_int_mode (ifalse, mode), target,
1211 if_info->x, 0, OPTAB_WIDEN);
1214 /* if (test) x = 8; else x = 0;
1215 => x = (test != 0) << 3; */
1216 else if (ifalse == 0 && (tmp = exact_log2 (itrue)) >= 0)
1218 target = expand_simple_binop (mode, ASHIFT,
1219 target, GEN_INT (tmp), if_info->x, 0,
1220 OPTAB_WIDEN);
1223 /* if (test) x = -1; else x = b;
1224 => x = -(test != 0) | b; */
1225 else if (itrue == -1)
1227 target = expand_simple_binop (mode, IOR,
1228 target, gen_int_mode (ifalse, mode),
1229 if_info->x, 0, OPTAB_WIDEN);
1232 /* if (test) x = a; else x = b;
1233 => x = (-(test != 0) & (b - a)) + a; */
1234 else
1236 target = expand_simple_binop (mode, AND,
1237 target, gen_int_mode (diff, mode),
1238 if_info->x, 0, OPTAB_WIDEN);
1239 if (target)
1240 target = expand_simple_binop (mode, PLUS,
1241 target, gen_int_mode (ifalse, mode),
1242 if_info->x, 0, OPTAB_WIDEN);
1245 if (! target)
1247 end_sequence ();
1248 return FALSE;
1251 if (target != if_info->x)
1252 noce_emit_move_insn (if_info->x, target);
1254 seq = end_ifcvt_sequence (if_info);
1255 if (!seq)
1256 return FALSE;
1258 emit_insn_before_setloc (seq, if_info->jump,
1259 INSN_LOCATION (if_info->insn_a));
1260 return TRUE;
1263 return FALSE;
1266 /* Convert "if (test) foo++" into "foo += (test != 0)", and
1267 similarly for "foo--". */
1269 static int
1270 noce_try_addcc (struct noce_if_info *if_info)
1272 rtx target;
1273 rtx_insn *seq;
1274 int subtract, normalize;
1276 if (GET_CODE (if_info->a) == PLUS
1277 && rtx_equal_p (XEXP (if_info->a, 0), if_info->b)
1278 && (reversed_comparison_code (if_info->cond, if_info->jump)
1279 != UNKNOWN))
1281 rtx cond = if_info->cond;
1282 enum rtx_code code = reversed_comparison_code (cond, if_info->jump);
1284 /* First try to use addcc pattern. */
1285 if (general_operand (XEXP (cond, 0), VOIDmode)
1286 && general_operand (XEXP (cond, 1), VOIDmode))
1288 start_sequence ();
1289 target = emit_conditional_add (if_info->x, code,
1290 XEXP (cond, 0),
1291 XEXP (cond, 1),
1292 VOIDmode,
1293 if_info->b,
1294 XEXP (if_info->a, 1),
1295 GET_MODE (if_info->x),
1296 (code == LTU || code == GEU
1297 || code == LEU || code == GTU));
1298 if (target)
1300 if (target != if_info->x)
1301 noce_emit_move_insn (if_info->x, target);
1303 seq = end_ifcvt_sequence (if_info);
1304 if (!seq)
1305 return FALSE;
1307 emit_insn_before_setloc (seq, if_info->jump,
1308 INSN_LOCATION (if_info->insn_a));
1309 return TRUE;
1311 end_sequence ();
1314 /* If that fails, construct conditional increment or decrement using
1315 setcc. */
1316 if (if_info->branch_cost >= 2
1317 && (XEXP (if_info->a, 1) == const1_rtx
1318 || XEXP (if_info->a, 1) == constm1_rtx))
1320 start_sequence ();
1321 if (STORE_FLAG_VALUE == INTVAL (XEXP (if_info->a, 1)))
1322 subtract = 0, normalize = 0;
1323 else if (-STORE_FLAG_VALUE == INTVAL (XEXP (if_info->a, 1)))
1324 subtract = 1, normalize = 0;
1325 else
1326 subtract = 0, normalize = INTVAL (XEXP (if_info->a, 1));
1329 target = noce_emit_store_flag (if_info,
1330 gen_reg_rtx (GET_MODE (if_info->x)),
1331 1, normalize);
1333 if (target)
1334 target = expand_simple_binop (GET_MODE (if_info->x),
1335 subtract ? MINUS : PLUS,
1336 if_info->b, target, if_info->x,
1337 0, OPTAB_WIDEN);
1338 if (target)
1340 if (target != if_info->x)
1341 noce_emit_move_insn (if_info->x, target);
1343 seq = end_ifcvt_sequence (if_info);
1344 if (!seq)
1345 return FALSE;
1347 emit_insn_before_setloc (seq, if_info->jump,
1348 INSN_LOCATION (if_info->insn_a));
1349 return TRUE;
1351 end_sequence ();
1355 return FALSE;
1358 /* Convert "if (test) x = 0;" to "x &= -(test == 0);" */
1360 static int
1361 noce_try_store_flag_mask (struct noce_if_info *if_info)
1363 rtx target;
1364 rtx_insn *seq;
1365 int reversep;
1367 reversep = 0;
1368 if ((if_info->branch_cost >= 2
1369 || STORE_FLAG_VALUE == -1)
1370 && ((if_info->a == const0_rtx
1371 && rtx_equal_p (if_info->b, if_info->x))
1372 || ((reversep = (reversed_comparison_code (if_info->cond,
1373 if_info->jump)
1374 != UNKNOWN))
1375 && if_info->b == const0_rtx
1376 && rtx_equal_p (if_info->a, if_info->x))))
1378 start_sequence ();
1379 target = noce_emit_store_flag (if_info,
1380 gen_reg_rtx (GET_MODE (if_info->x)),
1381 reversep, -1);
1382 if (target)
1383 target = expand_simple_binop (GET_MODE (if_info->x), AND,
1384 if_info->x,
1385 target, if_info->x, 0,
1386 OPTAB_WIDEN);
1388 if (target)
1390 if (target != if_info->x)
1391 noce_emit_move_insn (if_info->x, target);
1393 seq = end_ifcvt_sequence (if_info);
1394 if (!seq)
1395 return FALSE;
1397 emit_insn_before_setloc (seq, if_info->jump,
1398 INSN_LOCATION (if_info->insn_a));
1399 return TRUE;
1402 end_sequence ();
1405 return FALSE;
1408 /* Helper function for noce_try_cmove and noce_try_cmove_arith. */
1410 static rtx
1411 noce_emit_cmove (struct noce_if_info *if_info, rtx x, enum rtx_code code,
1412 rtx cmp_a, rtx cmp_b, rtx vfalse, rtx vtrue)
1414 rtx target ATTRIBUTE_UNUSED;
1415 int unsignedp ATTRIBUTE_UNUSED;
1417 /* If earliest == jump, try to build the cmove insn directly.
1418 This is helpful when combine has created some complex condition
1419 (like for alpha's cmovlbs) that we can't hope to regenerate
1420 through the normal interface. */
1422 if (if_info->cond_earliest == if_info->jump)
1424 rtx cond = gen_rtx_fmt_ee (code, GET_MODE (if_info->cond), cmp_a, cmp_b);
1425 rtx if_then_else = gen_rtx_IF_THEN_ELSE (GET_MODE (x),
1426 cond, vtrue, vfalse);
1427 rtx set = gen_rtx_SET (VOIDmode, x, if_then_else);
1429 start_sequence ();
1430 rtx_insn *insn = emit_insn (set);
1432 if (recog_memoized (insn) >= 0)
1434 rtx_insn *seq = get_insns ();
1435 end_sequence ();
1436 emit_insn (seq);
1438 return x;
1441 end_sequence ();
1444 /* Don't even try if the comparison operands are weird. */
1445 if (! general_operand (cmp_a, GET_MODE (cmp_a))
1446 || ! general_operand (cmp_b, GET_MODE (cmp_b)))
1447 return NULL_RTX;
1449 #if HAVE_conditional_move
1450 unsignedp = (code == LTU || code == GEU
1451 || code == LEU || code == GTU);
1453 target = emit_conditional_move (x, code, cmp_a, cmp_b, VOIDmode,
1454 vtrue, vfalse, GET_MODE (x),
1455 unsignedp);
1456 if (target)
1457 return target;
1459 /* We might be faced with a situation like:
1461 x = (reg:M TARGET)
1462 vtrue = (subreg:M (reg:N VTRUE) BYTE)
1463 vfalse = (subreg:M (reg:N VFALSE) BYTE)
1465 We can't do a conditional move in mode M, but it's possible that we
1466 could do a conditional move in mode N instead and take a subreg of
1467 the result.
1469 If we can't create new pseudos, though, don't bother. */
1470 if (reload_completed)
1471 return NULL_RTX;
1473 if (GET_CODE (vtrue) == SUBREG && GET_CODE (vfalse) == SUBREG)
1475 rtx reg_vtrue = SUBREG_REG (vtrue);
1476 rtx reg_vfalse = SUBREG_REG (vfalse);
1477 unsigned int byte_vtrue = SUBREG_BYTE (vtrue);
1478 unsigned int byte_vfalse = SUBREG_BYTE (vfalse);
1479 rtx promoted_target;
1481 if (GET_MODE (reg_vtrue) != GET_MODE (reg_vfalse)
1482 || byte_vtrue != byte_vfalse
1483 || (SUBREG_PROMOTED_VAR_P (vtrue)
1484 != SUBREG_PROMOTED_VAR_P (vfalse))
1485 || (SUBREG_PROMOTED_GET (vtrue)
1486 != SUBREG_PROMOTED_GET (vfalse)))
1487 return NULL_RTX;
1489 promoted_target = gen_reg_rtx (GET_MODE (reg_vtrue));
1491 target = emit_conditional_move (promoted_target, code, cmp_a, cmp_b,
1492 VOIDmode, reg_vtrue, reg_vfalse,
1493 GET_MODE (reg_vtrue), unsignedp);
1494 /* Nope, couldn't do it in that mode either. */
1495 if (!target)
1496 return NULL_RTX;
1498 target = gen_rtx_SUBREG (GET_MODE (vtrue), promoted_target, byte_vtrue);
1499 SUBREG_PROMOTED_VAR_P (target) = SUBREG_PROMOTED_VAR_P (vtrue);
1500 SUBREG_PROMOTED_SET (target, SUBREG_PROMOTED_GET (vtrue));
1501 emit_move_insn (x, target);
1502 return x;
1504 else
1505 return NULL_RTX;
1506 #else
1507 /* We'll never get here, as noce_process_if_block doesn't call the
1508 functions involved. Ifdef code, however, should be discouraged
1509 because it leads to typos in the code not selected. However,
1510 emit_conditional_move won't exist either. */
1511 return NULL_RTX;
1512 #endif
1515 /* Try only simple constants and registers here. More complex cases
1516 are handled in noce_try_cmove_arith after noce_try_store_flag_arith
1517 has had a go at it. */
1519 static int
1520 noce_try_cmove (struct noce_if_info *if_info)
1522 enum rtx_code code;
1523 rtx target;
1524 rtx_insn *seq;
1526 if ((CONSTANT_P (if_info->a) || register_operand (if_info->a, VOIDmode))
1527 && (CONSTANT_P (if_info->b) || register_operand (if_info->b, VOIDmode)))
1529 start_sequence ();
1531 code = GET_CODE (if_info->cond);
1532 target = noce_emit_cmove (if_info, if_info->x, code,
1533 XEXP (if_info->cond, 0),
1534 XEXP (if_info->cond, 1),
1535 if_info->a, if_info->b);
1537 if (target)
1539 if (target != if_info->x)
1540 noce_emit_move_insn (if_info->x, target);
1542 seq = end_ifcvt_sequence (if_info);
1543 if (!seq)
1544 return FALSE;
1546 emit_insn_before_setloc (seq, if_info->jump,
1547 INSN_LOCATION (if_info->insn_a));
1548 return TRUE;
1550 else
1552 end_sequence ();
1553 return FALSE;
1557 return FALSE;
1560 /* Try more complex cases involving conditional_move. */
1562 static int
1563 noce_try_cmove_arith (struct noce_if_info *if_info)
1565 rtx a = if_info->a;
1566 rtx b = if_info->b;
1567 rtx x = if_info->x;
1568 rtx orig_a, orig_b;
1569 rtx_insn *insn_a, *insn_b;
1570 rtx target;
1571 int is_mem = 0;
1572 int insn_cost;
1573 enum rtx_code code;
1574 rtx_insn *ifcvt_seq;
1576 /* A conditional move from two memory sources is equivalent to a
1577 conditional on their addresses followed by a load. Don't do this
1578 early because it'll screw alias analysis. Note that we've
1579 already checked for no side effects. */
1580 /* ??? FIXME: Magic number 5. */
1581 if (cse_not_expected
1582 && MEM_P (a) && MEM_P (b)
1583 && MEM_ADDR_SPACE (a) == MEM_ADDR_SPACE (b)
1584 && if_info->branch_cost >= 5)
1586 enum machine_mode address_mode = get_address_mode (a);
1588 a = XEXP (a, 0);
1589 b = XEXP (b, 0);
1590 x = gen_reg_rtx (address_mode);
1591 is_mem = 1;
1594 /* ??? We could handle this if we knew that a load from A or B could
1595 not trap or fault. This is also true if we've already loaded
1596 from the address along the path from ENTRY. */
1597 else if (may_trap_or_fault_p (a) || may_trap_or_fault_p (b))
1598 return FALSE;
1600 /* if (test) x = a + b; else x = c - d;
1601 => y = a + b;
1602 x = c - d;
1603 if (test)
1604 x = y;
1607 code = GET_CODE (if_info->cond);
1608 insn_a = if_info->insn_a;
1609 insn_b = if_info->insn_b;
1611 /* Total insn_rtx_cost should be smaller than branch cost. Exit
1612 if insn_rtx_cost can't be estimated. */
1613 if (insn_a)
1615 insn_cost
1616 = insn_rtx_cost (PATTERN (insn_a),
1617 optimize_bb_for_speed_p (BLOCK_FOR_INSN (insn_a)));
1618 if (insn_cost == 0 || insn_cost > COSTS_N_INSNS (if_info->branch_cost))
1619 return FALSE;
1621 else
1622 insn_cost = 0;
1624 if (insn_b)
1626 insn_cost
1627 += insn_rtx_cost (PATTERN (insn_b),
1628 optimize_bb_for_speed_p (BLOCK_FOR_INSN (insn_b)));
1629 if (insn_cost == 0 || insn_cost > COSTS_N_INSNS (if_info->branch_cost))
1630 return FALSE;
1633 /* Possibly rearrange operands to make things come out more natural. */
1634 if (reversed_comparison_code (if_info->cond, if_info->jump) != UNKNOWN)
1636 int reversep = 0;
1637 if (rtx_equal_p (b, x))
1638 reversep = 1;
1639 else if (general_operand (b, GET_MODE (b)))
1640 reversep = 1;
1642 if (reversep)
1644 rtx tmp;
1645 rtx_insn *tmp_insn;
1646 code = reversed_comparison_code (if_info->cond, if_info->jump);
1647 tmp = a, a = b, b = tmp;
1648 tmp_insn = insn_a, insn_a = insn_b, insn_b = tmp_insn;
1652 start_sequence ();
1654 orig_a = a;
1655 orig_b = b;
1657 /* If either operand is complex, load it into a register first.
1658 The best way to do this is to copy the original insn. In this
1659 way we preserve any clobbers etc that the insn may have had.
1660 This is of course not possible in the IS_MEM case. */
1661 if (! general_operand (a, GET_MODE (a)))
1663 rtx_insn *insn;
1665 if (is_mem)
1667 rtx reg = gen_reg_rtx (GET_MODE (a));
1668 insn = emit_insn (gen_rtx_SET (VOIDmode, reg, a));
1670 else if (! insn_a)
1671 goto end_seq_and_fail;
1672 else
1674 a = gen_reg_rtx (GET_MODE (a));
1675 rtx_insn *copy_of_a = as_a <rtx_insn *> (copy_rtx (insn_a));
1676 rtx set = single_set (copy_of_a);
1677 SET_DEST (set) = a;
1678 insn = emit_insn (PATTERN (copy_of_a));
1680 if (recog_memoized (insn) < 0)
1681 goto end_seq_and_fail;
1683 if (! general_operand (b, GET_MODE (b)))
1685 rtx pat;
1686 rtx_insn *last;
1687 rtx_insn *new_insn;
1689 if (is_mem)
1691 rtx reg = gen_reg_rtx (GET_MODE (b));
1692 pat = gen_rtx_SET (VOIDmode, reg, b);
1694 else if (! insn_b)
1695 goto end_seq_and_fail;
1696 else
1698 b = gen_reg_rtx (GET_MODE (b));
1699 rtx_insn *copy_of_insn_b = as_a <rtx_insn *> (copy_rtx (insn_b));
1700 rtx set = single_set (copy_of_insn_b);
1701 SET_DEST (set) = b;
1702 pat = PATTERN (copy_of_insn_b);
1705 /* If insn to set up A clobbers any registers B depends on, try to
1706 swap insn that sets up A with the one that sets up B. If even
1707 that doesn't help, punt. */
1708 last = get_last_insn ();
1709 if (last && modified_in_p (orig_b, last))
1711 new_insn = emit_insn_before (pat, get_insns ());
1712 if (modified_in_p (orig_a, new_insn))
1713 goto end_seq_and_fail;
1715 else
1716 new_insn = emit_insn (pat);
1718 if (recog_memoized (new_insn) < 0)
1719 goto end_seq_and_fail;
1722 target = noce_emit_cmove (if_info, x, code, XEXP (if_info->cond, 0),
1723 XEXP (if_info->cond, 1), a, b);
1725 if (! target)
1726 goto end_seq_and_fail;
1728 /* If we're handling a memory for above, emit the load now. */
1729 if (is_mem)
1731 rtx mem = gen_rtx_MEM (GET_MODE (if_info->x), target);
1733 /* Copy over flags as appropriate. */
1734 if (MEM_VOLATILE_P (if_info->a) || MEM_VOLATILE_P (if_info->b))
1735 MEM_VOLATILE_P (mem) = 1;
1736 if (MEM_ALIAS_SET (if_info->a) == MEM_ALIAS_SET (if_info->b))
1737 set_mem_alias_set (mem, MEM_ALIAS_SET (if_info->a));
1738 set_mem_align (mem,
1739 MIN (MEM_ALIGN (if_info->a), MEM_ALIGN (if_info->b)));
1741 gcc_assert (MEM_ADDR_SPACE (if_info->a) == MEM_ADDR_SPACE (if_info->b));
1742 set_mem_addr_space (mem, MEM_ADDR_SPACE (if_info->a));
1744 noce_emit_move_insn (if_info->x, mem);
1746 else if (target != x)
1747 noce_emit_move_insn (x, target);
1749 ifcvt_seq = end_ifcvt_sequence (if_info);
1750 if (!ifcvt_seq)
1751 return FALSE;
1753 emit_insn_before_setloc (ifcvt_seq, if_info->jump,
1754 INSN_LOCATION (if_info->insn_a));
1755 return TRUE;
1757 end_seq_and_fail:
1758 end_sequence ();
1759 return FALSE;
1762 /* For most cases, the simplified condition we found is the best
1763 choice, but this is not the case for the min/max/abs transforms.
1764 For these we wish to know that it is A or B in the condition. */
1766 static rtx
1767 noce_get_alt_condition (struct noce_if_info *if_info, rtx target,
1768 rtx_insn **earliest)
1770 rtx cond, set;
1771 rtx_insn *insn;
1772 int reverse;
1774 /* If target is already mentioned in the known condition, return it. */
1775 if (reg_mentioned_p (target, if_info->cond))
1777 *earliest = if_info->cond_earliest;
1778 return if_info->cond;
1781 set = pc_set (if_info->jump);
1782 cond = XEXP (SET_SRC (set), 0);
1783 reverse
1784 = GET_CODE (XEXP (SET_SRC (set), 2)) == LABEL_REF
1785 && LABEL_REF_LABEL (XEXP (SET_SRC (set), 2)) == JUMP_LABEL (if_info->jump);
1786 if (if_info->then_else_reversed)
1787 reverse = !reverse;
1789 /* If we're looking for a constant, try to make the conditional
1790 have that constant in it. There are two reasons why it may
1791 not have the constant we want:
1793 1. GCC may have needed to put the constant in a register, because
1794 the target can't compare directly against that constant. For
1795 this case, we look for a SET immediately before the comparison
1796 that puts a constant in that register.
1798 2. GCC may have canonicalized the conditional, for example
1799 replacing "if x < 4" with "if x <= 3". We can undo that (or
1800 make equivalent types of changes) to get the constants we need
1801 if they're off by one in the right direction. */
1803 if (CONST_INT_P (target))
1805 enum rtx_code code = GET_CODE (if_info->cond);
1806 rtx op_a = XEXP (if_info->cond, 0);
1807 rtx op_b = XEXP (if_info->cond, 1);
1808 rtx prev_insn;
1810 /* First, look to see if we put a constant in a register. */
1811 prev_insn = prev_nonnote_insn (if_info->cond_earliest);
1812 if (prev_insn
1813 && BLOCK_FOR_INSN (prev_insn)
1814 == BLOCK_FOR_INSN (if_info->cond_earliest)
1815 && INSN_P (prev_insn)
1816 && GET_CODE (PATTERN (prev_insn)) == SET)
1818 rtx src = find_reg_equal_equiv_note (prev_insn);
1819 if (!src)
1820 src = SET_SRC (PATTERN (prev_insn));
1821 if (CONST_INT_P (src))
1823 if (rtx_equal_p (op_a, SET_DEST (PATTERN (prev_insn))))
1824 op_a = src;
1825 else if (rtx_equal_p (op_b, SET_DEST (PATTERN (prev_insn))))
1826 op_b = src;
1828 if (CONST_INT_P (op_a))
1830 rtx tmp = op_a;
1831 op_a = op_b;
1832 op_b = tmp;
1833 code = swap_condition (code);
1838 /* Now, look to see if we can get the right constant by
1839 adjusting the conditional. */
1840 if (CONST_INT_P (op_b))
1842 HOST_WIDE_INT desired_val = INTVAL (target);
1843 HOST_WIDE_INT actual_val = INTVAL (op_b);
1845 switch (code)
1847 case LT:
1848 if (actual_val == desired_val + 1)
1850 code = LE;
1851 op_b = GEN_INT (desired_val);
1853 break;
1854 case LE:
1855 if (actual_val == desired_val - 1)
1857 code = LT;
1858 op_b = GEN_INT (desired_val);
1860 break;
1861 case GT:
1862 if (actual_val == desired_val - 1)
1864 code = GE;
1865 op_b = GEN_INT (desired_val);
1867 break;
1868 case GE:
1869 if (actual_val == desired_val + 1)
1871 code = GT;
1872 op_b = GEN_INT (desired_val);
1874 break;
1875 default:
1876 break;
1880 /* If we made any changes, generate a new conditional that is
1881 equivalent to what we started with, but has the right
1882 constants in it. */
1883 if (code != GET_CODE (if_info->cond)
1884 || op_a != XEXP (if_info->cond, 0)
1885 || op_b != XEXP (if_info->cond, 1))
1887 cond = gen_rtx_fmt_ee (code, GET_MODE (cond), op_a, op_b);
1888 *earliest = if_info->cond_earliest;
1889 return cond;
1893 cond = canonicalize_condition (if_info->jump, cond, reverse,
1894 earliest, target, false, true);
1895 if (! cond || ! reg_mentioned_p (target, cond))
1896 return NULL;
1898 /* We almost certainly searched back to a different place.
1899 Need to re-verify correct lifetimes. */
1901 /* X may not be mentioned in the range (cond_earliest, jump]. */
1902 for (insn = if_info->jump; insn != *earliest; insn = PREV_INSN (insn))
1903 if (INSN_P (insn) && reg_overlap_mentioned_p (if_info->x, PATTERN (insn)))
1904 return NULL;
1906 /* A and B may not be modified in the range [cond_earliest, jump). */
1907 for (insn = *earliest; insn != if_info->jump; insn = NEXT_INSN (insn))
1908 if (INSN_P (insn)
1909 && (modified_in_p (if_info->a, insn)
1910 || modified_in_p (if_info->b, insn)))
1911 return NULL;
1913 return cond;
1916 /* Convert "if (a < b) x = a; else x = b;" to "x = min(a, b);", etc. */
1918 static int
1919 noce_try_minmax (struct noce_if_info *if_info)
1921 rtx cond, target;
1922 rtx_insn *earliest, *seq;
1923 enum rtx_code code, op;
1924 int unsignedp;
1926 /* ??? Reject modes with NaNs or signed zeros since we don't know how
1927 they will be resolved with an SMIN/SMAX. It wouldn't be too hard
1928 to get the target to tell us... */
1929 if (HONOR_SIGNED_ZEROS (GET_MODE (if_info->x))
1930 || HONOR_NANS (GET_MODE (if_info->x)))
1931 return FALSE;
1933 cond = noce_get_alt_condition (if_info, if_info->a, &earliest);
1934 if (!cond)
1935 return FALSE;
1937 /* Verify the condition is of the form we expect, and canonicalize
1938 the comparison code. */
1939 code = GET_CODE (cond);
1940 if (rtx_equal_p (XEXP (cond, 0), if_info->a))
1942 if (! rtx_equal_p (XEXP (cond, 1), if_info->b))
1943 return FALSE;
1945 else if (rtx_equal_p (XEXP (cond, 1), if_info->a))
1947 if (! rtx_equal_p (XEXP (cond, 0), if_info->b))
1948 return FALSE;
1949 code = swap_condition (code);
1951 else
1952 return FALSE;
1954 /* Determine what sort of operation this is. Note that the code is for
1955 a taken branch, so the code->operation mapping appears backwards. */
1956 switch (code)
1958 case LT:
1959 case LE:
1960 case UNLT:
1961 case UNLE:
1962 op = SMAX;
1963 unsignedp = 0;
1964 break;
1965 case GT:
1966 case GE:
1967 case UNGT:
1968 case UNGE:
1969 op = SMIN;
1970 unsignedp = 0;
1971 break;
1972 case LTU:
1973 case LEU:
1974 op = UMAX;
1975 unsignedp = 1;
1976 break;
1977 case GTU:
1978 case GEU:
1979 op = UMIN;
1980 unsignedp = 1;
1981 break;
1982 default:
1983 return FALSE;
1986 start_sequence ();
1988 target = expand_simple_binop (GET_MODE (if_info->x), op,
1989 if_info->a, if_info->b,
1990 if_info->x, unsignedp, OPTAB_WIDEN);
1991 if (! target)
1993 end_sequence ();
1994 return FALSE;
1996 if (target != if_info->x)
1997 noce_emit_move_insn (if_info->x, target);
1999 seq = end_ifcvt_sequence (if_info);
2000 if (!seq)
2001 return FALSE;
2003 emit_insn_before_setloc (seq, if_info->jump, INSN_LOCATION (if_info->insn_a));
2004 if_info->cond = cond;
2005 if_info->cond_earliest = earliest;
2007 return TRUE;
2010 /* Convert "if (a < 0) x = -a; else x = a;" to "x = abs(a);",
2011 "if (a < 0) x = ~a; else x = a;" to "x = one_cmpl_abs(a);",
2012 etc. */
2014 static int
2015 noce_try_abs (struct noce_if_info *if_info)
2017 rtx cond, target, a, b, c;
2018 rtx_insn *earliest, *seq;
2019 int negate;
2020 bool one_cmpl = false;
2022 /* Reject modes with signed zeros. */
2023 if (HONOR_SIGNED_ZEROS (GET_MODE (if_info->x)))
2024 return FALSE;
2026 /* Recognize A and B as constituting an ABS or NABS. The canonical
2027 form is a branch around the negation, taken when the object is the
2028 first operand of a comparison against 0 that evaluates to true. */
2029 a = if_info->a;
2030 b = if_info->b;
2031 if (GET_CODE (a) == NEG && rtx_equal_p (XEXP (a, 0), b))
2032 negate = 0;
2033 else if (GET_CODE (b) == NEG && rtx_equal_p (XEXP (b, 0), a))
2035 c = a; a = b; b = c;
2036 negate = 1;
2038 else if (GET_CODE (a) == NOT && rtx_equal_p (XEXP (a, 0), b))
2040 negate = 0;
2041 one_cmpl = true;
2043 else if (GET_CODE (b) == NOT && rtx_equal_p (XEXP (b, 0), a))
2045 c = a; a = b; b = c;
2046 negate = 1;
2047 one_cmpl = true;
2049 else
2050 return FALSE;
2052 cond = noce_get_alt_condition (if_info, b, &earliest);
2053 if (!cond)
2054 return FALSE;
2056 /* Verify the condition is of the form we expect. */
2057 if (rtx_equal_p (XEXP (cond, 0), b))
2058 c = XEXP (cond, 1);
2059 else if (rtx_equal_p (XEXP (cond, 1), b))
2061 c = XEXP (cond, 0);
2062 negate = !negate;
2064 else
2065 return FALSE;
2067 /* Verify that C is zero. Search one step backward for a
2068 REG_EQUAL note or a simple source if necessary. */
2069 if (REG_P (c))
2071 rtx set;
2072 rtx_insn *insn = prev_nonnote_insn (earliest);
2073 if (insn
2074 && BLOCK_FOR_INSN (insn) == BLOCK_FOR_INSN (earliest)
2075 && (set = single_set (insn))
2076 && rtx_equal_p (SET_DEST (set), c))
2078 rtx note = find_reg_equal_equiv_note (insn);
2079 if (note)
2080 c = XEXP (note, 0);
2081 else
2082 c = SET_SRC (set);
2084 else
2085 return FALSE;
2087 if (MEM_P (c)
2088 && GET_CODE (XEXP (c, 0)) == SYMBOL_REF
2089 && CONSTANT_POOL_ADDRESS_P (XEXP (c, 0)))
2090 c = get_pool_constant (XEXP (c, 0));
2092 /* Work around funny ideas get_condition has wrt canonicalization.
2093 Note that these rtx constants are known to be CONST_INT, and
2094 therefore imply integer comparisons. */
2095 if (c == constm1_rtx && GET_CODE (cond) == GT)
2097 else if (c == const1_rtx && GET_CODE (cond) == LT)
2099 else if (c != CONST0_RTX (GET_MODE (b)))
2100 return FALSE;
2102 /* Determine what sort of operation this is. */
2103 switch (GET_CODE (cond))
2105 case LT:
2106 case LE:
2107 case UNLT:
2108 case UNLE:
2109 negate = !negate;
2110 break;
2111 case GT:
2112 case GE:
2113 case UNGT:
2114 case UNGE:
2115 break;
2116 default:
2117 return FALSE;
2120 start_sequence ();
2121 if (one_cmpl)
2122 target = expand_one_cmpl_abs_nojump (GET_MODE (if_info->x), b,
2123 if_info->x);
2124 else
2125 target = expand_abs_nojump (GET_MODE (if_info->x), b, if_info->x, 1);
2127 /* ??? It's a quandary whether cmove would be better here, especially
2128 for integers. Perhaps combine will clean things up. */
2129 if (target && negate)
2131 if (one_cmpl)
2132 target = expand_simple_unop (GET_MODE (target), NOT, target,
2133 if_info->x, 0);
2134 else
2135 target = expand_simple_unop (GET_MODE (target), NEG, target,
2136 if_info->x, 0);
2139 if (! target)
2141 end_sequence ();
2142 return FALSE;
2145 if (target != if_info->x)
2146 noce_emit_move_insn (if_info->x, target);
2148 seq = end_ifcvt_sequence (if_info);
2149 if (!seq)
2150 return FALSE;
2152 emit_insn_before_setloc (seq, if_info->jump, INSN_LOCATION (if_info->insn_a));
2153 if_info->cond = cond;
2154 if_info->cond_earliest = earliest;
2156 return TRUE;
2159 /* Convert "if (m < 0) x = b; else x = 0;" to "x = (m >> C) & b;". */
2161 static int
2162 noce_try_sign_mask (struct noce_if_info *if_info)
2164 rtx cond, t, m, c;
2165 rtx_insn *seq;
2166 enum machine_mode mode;
2167 enum rtx_code code;
2168 bool t_unconditional;
2170 cond = if_info->cond;
2171 code = GET_CODE (cond);
2172 m = XEXP (cond, 0);
2173 c = XEXP (cond, 1);
2175 t = NULL_RTX;
2176 if (if_info->a == const0_rtx)
2178 if ((code == LT && c == const0_rtx)
2179 || (code == LE && c == constm1_rtx))
2180 t = if_info->b;
2182 else if (if_info->b == const0_rtx)
2184 if ((code == GE && c == const0_rtx)
2185 || (code == GT && c == constm1_rtx))
2186 t = if_info->a;
2189 if (! t || side_effects_p (t))
2190 return FALSE;
2192 /* We currently don't handle different modes. */
2193 mode = GET_MODE (t);
2194 if (GET_MODE (m) != mode)
2195 return FALSE;
2197 /* This is only profitable if T is unconditionally executed/evaluated in the
2198 original insn sequence or T is cheap. The former happens if B is the
2199 non-zero (T) value and if INSN_B was taken from TEST_BB, or there was no
2200 INSN_B which can happen for e.g. conditional stores to memory. For the
2201 cost computation use the block TEST_BB where the evaluation will end up
2202 after the transformation. */
2203 t_unconditional =
2204 (t == if_info->b
2205 && (if_info->insn_b == NULL_RTX
2206 || BLOCK_FOR_INSN (if_info->insn_b) == if_info->test_bb));
2207 if (!(t_unconditional
2208 || (set_src_cost (t, optimize_bb_for_speed_p (if_info->test_bb))
2209 < COSTS_N_INSNS (2))))
2210 return FALSE;
2212 start_sequence ();
2213 /* Use emit_store_flag to generate "m < 0 ? -1 : 0" instead of expanding
2214 "(signed) m >> 31" directly. This benefits targets with specialized
2215 insns to obtain the signmask, but still uses ashr_optab otherwise. */
2216 m = emit_store_flag (gen_reg_rtx (mode), LT, m, const0_rtx, mode, 0, -1);
2217 t = m ? expand_binop (mode, and_optab, m, t, NULL_RTX, 0, OPTAB_DIRECT)
2218 : NULL_RTX;
2220 if (!t)
2222 end_sequence ();
2223 return FALSE;
2226 noce_emit_move_insn (if_info->x, t);
2228 seq = end_ifcvt_sequence (if_info);
2229 if (!seq)
2230 return FALSE;
2232 emit_insn_before_setloc (seq, if_info->jump, INSN_LOCATION (if_info->insn_a));
2233 return TRUE;
2237 /* Optimize away "if (x & C) x |= C" and similar bit manipulation
2238 transformations. */
2240 static int
2241 noce_try_bitop (struct noce_if_info *if_info)
2243 rtx cond, x, a, result;
2244 rtx_insn *seq;
2245 enum machine_mode mode;
2246 enum rtx_code code;
2247 int bitnum;
2249 x = if_info->x;
2250 cond = if_info->cond;
2251 code = GET_CODE (cond);
2253 /* Check for no else condition. */
2254 if (! rtx_equal_p (x, if_info->b))
2255 return FALSE;
2257 /* Check for a suitable condition. */
2258 if (code != NE && code != EQ)
2259 return FALSE;
2260 if (XEXP (cond, 1) != const0_rtx)
2261 return FALSE;
2262 cond = XEXP (cond, 0);
2264 /* ??? We could also handle AND here. */
2265 if (GET_CODE (cond) == ZERO_EXTRACT)
2267 if (XEXP (cond, 1) != const1_rtx
2268 || !CONST_INT_P (XEXP (cond, 2))
2269 || ! rtx_equal_p (x, XEXP (cond, 0)))
2270 return FALSE;
2271 bitnum = INTVAL (XEXP (cond, 2));
2272 mode = GET_MODE (x);
2273 if (BITS_BIG_ENDIAN)
2274 bitnum = GET_MODE_BITSIZE (mode) - 1 - bitnum;
2275 if (bitnum < 0 || bitnum >= HOST_BITS_PER_WIDE_INT)
2276 return FALSE;
2278 else
2279 return FALSE;
2281 a = if_info->a;
2282 if (GET_CODE (a) == IOR || GET_CODE (a) == XOR)
2284 /* Check for "if (X & C) x = x op C". */
2285 if (! rtx_equal_p (x, XEXP (a, 0))
2286 || !CONST_INT_P (XEXP (a, 1))
2287 || (INTVAL (XEXP (a, 1)) & GET_MODE_MASK (mode))
2288 != (unsigned HOST_WIDE_INT) 1 << bitnum)
2289 return FALSE;
2291 /* if ((x & C) == 0) x |= C; is transformed to x |= C. */
2292 /* if ((x & C) != 0) x |= C; is transformed to nothing. */
2293 if (GET_CODE (a) == IOR)
2294 result = (code == NE) ? a : NULL_RTX;
2295 else if (code == NE)
2297 /* if ((x & C) == 0) x ^= C; is transformed to x |= C. */
2298 result = gen_int_mode ((HOST_WIDE_INT) 1 << bitnum, mode);
2299 result = simplify_gen_binary (IOR, mode, x, result);
2301 else
2303 /* if ((x & C) != 0) x ^= C; is transformed to x &= ~C. */
2304 result = gen_int_mode (~((HOST_WIDE_INT) 1 << bitnum), mode);
2305 result = simplify_gen_binary (AND, mode, x, result);
2308 else if (GET_CODE (a) == AND)
2310 /* Check for "if (X & C) x &= ~C". */
2311 if (! rtx_equal_p (x, XEXP (a, 0))
2312 || !CONST_INT_P (XEXP (a, 1))
2313 || (INTVAL (XEXP (a, 1)) & GET_MODE_MASK (mode))
2314 != (~((HOST_WIDE_INT) 1 << bitnum) & GET_MODE_MASK (mode)))
2315 return FALSE;
2317 /* if ((x & C) == 0) x &= ~C; is transformed to nothing. */
2318 /* if ((x & C) != 0) x &= ~C; is transformed to x &= ~C. */
2319 result = (code == EQ) ? a : NULL_RTX;
2321 else
2322 return FALSE;
2324 if (result)
2326 start_sequence ();
2327 noce_emit_move_insn (x, result);
2328 seq = end_ifcvt_sequence (if_info);
2329 if (!seq)
2330 return FALSE;
2332 emit_insn_before_setloc (seq, if_info->jump,
2333 INSN_LOCATION (if_info->insn_a));
2335 return TRUE;
2339 /* Similar to get_condition, only the resulting condition must be
2340 valid at JUMP, instead of at EARLIEST.
2342 If THEN_ELSE_REVERSED is true, the fallthrough does not go to the
2343 THEN block of the caller, and we have to reverse the condition. */
2345 static rtx
2346 noce_get_condition (rtx_insn *jump, rtx_insn **earliest, bool then_else_reversed)
2348 rtx cond, set, tmp;
2349 bool reverse;
2351 if (! any_condjump_p (jump))
2352 return NULL_RTX;
2354 set = pc_set (jump);
2356 /* If this branches to JUMP_LABEL when the condition is false,
2357 reverse the condition. */
2358 reverse = (GET_CODE (XEXP (SET_SRC (set), 2)) == LABEL_REF
2359 && LABEL_REF_LABEL (XEXP (SET_SRC (set), 2)) == JUMP_LABEL (jump));
2361 /* We may have to reverse because the caller's if block is not canonical,
2362 i.e. the THEN block isn't the fallthrough block for the TEST block
2363 (see find_if_header). */
2364 if (then_else_reversed)
2365 reverse = !reverse;
2367 /* If the condition variable is a register and is MODE_INT, accept it. */
2369 cond = XEXP (SET_SRC (set), 0);
2370 tmp = XEXP (cond, 0);
2371 if (REG_P (tmp) && GET_MODE_CLASS (GET_MODE (tmp)) == MODE_INT
2372 && (GET_MODE (tmp) != BImode
2373 || !targetm.small_register_classes_for_mode_p (BImode)))
2375 *earliest = jump;
2377 if (reverse)
2378 cond = gen_rtx_fmt_ee (reverse_condition (GET_CODE (cond)),
2379 GET_MODE (cond), tmp, XEXP (cond, 1));
2380 return cond;
2383 /* Otherwise, fall back on canonicalize_condition to do the dirty
2384 work of manipulating MODE_CC values and COMPARE rtx codes. */
2385 tmp = canonicalize_condition (jump, cond, reverse, earliest,
2386 NULL_RTX, false, true);
2388 /* We don't handle side-effects in the condition, like handling
2389 REG_INC notes and making sure no duplicate conditions are emitted. */
2390 if (tmp != NULL_RTX && side_effects_p (tmp))
2391 return NULL_RTX;
2393 return tmp;
2396 /* Return true if OP is ok for if-then-else processing. */
2398 static int
2399 noce_operand_ok (const_rtx op)
2401 if (side_effects_p (op))
2402 return FALSE;
2404 /* We special-case memories, so handle any of them with
2405 no address side effects. */
2406 if (MEM_P (op))
2407 return ! side_effects_p (XEXP (op, 0));
2409 return ! may_trap_p (op);
2412 /* Return true if a write into MEM may trap or fault. */
2414 static bool
2415 noce_mem_write_may_trap_or_fault_p (const_rtx mem)
2417 rtx addr;
2419 if (MEM_READONLY_P (mem))
2420 return true;
2422 if (may_trap_or_fault_p (mem))
2423 return true;
2425 addr = XEXP (mem, 0);
2427 /* Call target hook to avoid the effects of -fpic etc.... */
2428 addr = targetm.delegitimize_address (addr);
2430 while (addr)
2431 switch (GET_CODE (addr))
2433 case CONST:
2434 case PRE_DEC:
2435 case PRE_INC:
2436 case POST_DEC:
2437 case POST_INC:
2438 case POST_MODIFY:
2439 addr = XEXP (addr, 0);
2440 break;
2441 case LO_SUM:
2442 case PRE_MODIFY:
2443 addr = XEXP (addr, 1);
2444 break;
2445 case PLUS:
2446 if (CONST_INT_P (XEXP (addr, 1)))
2447 addr = XEXP (addr, 0);
2448 else
2449 return false;
2450 break;
2451 case LABEL_REF:
2452 return true;
2453 case SYMBOL_REF:
2454 if (SYMBOL_REF_DECL (addr)
2455 && decl_readonly_section (SYMBOL_REF_DECL (addr), 0))
2456 return true;
2457 return false;
2458 default:
2459 return false;
2462 return false;
2465 /* Return whether we can use store speculation for MEM. TOP_BB is the
2466 basic block above the conditional block where we are considering
2467 doing the speculative store. We look for whether MEM is set
2468 unconditionally later in the function. */
2470 static bool
2471 noce_can_store_speculate_p (basic_block top_bb, const_rtx mem)
2473 basic_block dominator;
2475 for (dominator = get_immediate_dominator (CDI_POST_DOMINATORS, top_bb);
2476 dominator != NULL;
2477 dominator = get_immediate_dominator (CDI_POST_DOMINATORS, dominator))
2479 rtx_insn *insn;
2481 FOR_BB_INSNS (dominator, insn)
2483 /* If we see something that might be a memory barrier, we
2484 have to stop looking. Even if the MEM is set later in
2485 the function, we still don't want to set it
2486 unconditionally before the barrier. */
2487 if (INSN_P (insn)
2488 && (volatile_insn_p (PATTERN (insn))
2489 || (CALL_P (insn) && (!RTL_CONST_CALL_P (insn)))))
2490 return false;
2492 if (memory_must_be_modified_in_insn_p (mem, insn))
2493 return true;
2494 if (modified_in_p (XEXP (mem, 0), insn))
2495 return false;
2500 return false;
2503 /* Given a simple IF-THEN-JOIN or IF-THEN-ELSE-JOIN block, attempt to convert
2504 it without using conditional execution. Return TRUE if we were successful
2505 at converting the block. */
2507 static int
2508 noce_process_if_block (struct noce_if_info *if_info)
2510 basic_block test_bb = if_info->test_bb; /* test block */
2511 basic_block then_bb = if_info->then_bb; /* THEN */
2512 basic_block else_bb = if_info->else_bb; /* ELSE or NULL */
2513 basic_block join_bb = if_info->join_bb; /* JOIN */
2514 rtx_insn *jump = if_info->jump;
2515 rtx cond = if_info->cond;
2516 rtx_insn *insn_a, *insn_b;
2517 rtx set_a, set_b;
2518 rtx orig_x, x, a, b;
2520 /* We're looking for patterns of the form
2522 (1) if (...) x = a; else x = b;
2523 (2) x = b; if (...) x = a;
2524 (3) if (...) x = a; // as if with an initial x = x.
2526 The later patterns require jumps to be more expensive.
2528 ??? For future expansion, look for multiple X in such patterns. */
2530 /* Look for one of the potential sets. */
2531 insn_a = first_active_insn (then_bb);
2532 if (! insn_a
2533 || insn_a != last_active_insn (then_bb, FALSE)
2534 || (set_a = single_set (insn_a)) == NULL_RTX)
2535 return FALSE;
2537 x = SET_DEST (set_a);
2538 a = SET_SRC (set_a);
2540 /* Look for the other potential set. Make sure we've got equivalent
2541 destinations. */
2542 /* ??? This is overconservative. Storing to two different mems is
2543 as easy as conditionally computing the address. Storing to a
2544 single mem merely requires a scratch memory to use as one of the
2545 destination addresses; often the memory immediately below the
2546 stack pointer is available for this. */
2547 set_b = NULL_RTX;
2548 if (else_bb)
2550 insn_b = first_active_insn (else_bb);
2551 if (! insn_b
2552 || insn_b != last_active_insn (else_bb, FALSE)
2553 || (set_b = single_set (insn_b)) == NULL_RTX
2554 || ! rtx_interchangeable_p (x, SET_DEST (set_b)))
2555 return FALSE;
2557 else
2559 insn_b = prev_nonnote_nondebug_insn (if_info->cond_earliest);
2560 /* We're going to be moving the evaluation of B down from above
2561 COND_EARLIEST to JUMP. Make sure the relevant data is still
2562 intact. */
2563 if (! insn_b
2564 || BLOCK_FOR_INSN (insn_b) != BLOCK_FOR_INSN (if_info->cond_earliest)
2565 || !NONJUMP_INSN_P (insn_b)
2566 || (set_b = single_set (insn_b)) == NULL_RTX
2567 || ! rtx_interchangeable_p (x, SET_DEST (set_b))
2568 || ! noce_operand_ok (SET_SRC (set_b))
2569 || reg_overlap_mentioned_p (x, SET_SRC (set_b))
2570 || modified_between_p (SET_SRC (set_b), insn_b, jump)
2571 /* Avoid extending the lifetime of hard registers on small
2572 register class machines. */
2573 || (REG_P (SET_SRC (set_b))
2574 && HARD_REGISTER_P (SET_SRC (set_b))
2575 && targetm.small_register_classes_for_mode_p
2576 (GET_MODE (SET_SRC (set_b))))
2577 /* Likewise with X. In particular this can happen when
2578 noce_get_condition looks farther back in the instruction
2579 stream than one might expect. */
2580 || reg_overlap_mentioned_p (x, cond)
2581 || reg_overlap_mentioned_p (x, a)
2582 || modified_between_p (x, insn_b, jump))
2584 insn_b = NULL;
2585 set_b = NULL_RTX;
2589 /* If x has side effects then only the if-then-else form is safe to
2590 convert. But even in that case we would need to restore any notes
2591 (such as REG_INC) at then end. That can be tricky if
2592 noce_emit_move_insn expands to more than one insn, so disable the
2593 optimization entirely for now if there are side effects. */
2594 if (side_effects_p (x))
2595 return FALSE;
2597 b = (set_b ? SET_SRC (set_b) : x);
2599 /* Only operate on register destinations, and even then avoid extending
2600 the lifetime of hard registers on small register class machines. */
2601 orig_x = x;
2602 if (!REG_P (x)
2603 || (HARD_REGISTER_P (x)
2604 && targetm.small_register_classes_for_mode_p (GET_MODE (x))))
2606 if (GET_MODE (x) == BLKmode)
2607 return FALSE;
2609 if (GET_CODE (x) == ZERO_EXTRACT
2610 && (!CONST_INT_P (XEXP (x, 1))
2611 || !CONST_INT_P (XEXP (x, 2))))
2612 return FALSE;
2614 x = gen_reg_rtx (GET_MODE (GET_CODE (x) == STRICT_LOW_PART
2615 ? XEXP (x, 0) : x));
2618 /* Don't operate on sources that may trap or are volatile. */
2619 if (! noce_operand_ok (a) || ! noce_operand_ok (b))
2620 return FALSE;
2622 retry:
2623 /* Set up the info block for our subroutines. */
2624 if_info->insn_a = insn_a;
2625 if_info->insn_b = insn_b;
2626 if_info->x = x;
2627 if_info->a = a;
2628 if_info->b = b;
2630 /* Try optimizations in some approximation of a useful order. */
2631 /* ??? Should first look to see if X is live incoming at all. If it
2632 isn't, we don't need anything but an unconditional set. */
2634 /* Look and see if A and B are really the same. Avoid creating silly
2635 cmove constructs that no one will fix up later. */
2636 if (rtx_interchangeable_p (a, b))
2638 /* If we have an INSN_B, we don't have to create any new rtl. Just
2639 move the instruction that we already have. If we don't have an
2640 INSN_B, that means that A == X, and we've got a noop move. In
2641 that case don't do anything and let the code below delete INSN_A. */
2642 if (insn_b && else_bb)
2644 rtx note;
2646 if (else_bb && insn_b == BB_END (else_bb))
2647 BB_END (else_bb) = PREV_INSN (insn_b);
2648 reorder_insns (insn_b, insn_b, PREV_INSN (jump));
2650 /* If there was a REG_EQUAL note, delete it since it may have been
2651 true due to this insn being after a jump. */
2652 if ((note = find_reg_note (insn_b, REG_EQUAL, NULL_RTX)) != 0)
2653 remove_note (insn_b, note);
2655 insn_b = NULL;
2657 /* If we have "x = b; if (...) x = a;", and x has side-effects, then
2658 x must be executed twice. */
2659 else if (insn_b && side_effects_p (orig_x))
2660 return FALSE;
2662 x = orig_x;
2663 goto success;
2666 if (!set_b && MEM_P (orig_x))
2668 /* Disallow the "if (...) x = a;" form (implicit "else x = x;")
2669 for optimizations if writing to x may trap or fault,
2670 i.e. it's a memory other than a static var or a stack slot,
2671 is misaligned on strict aligned machines or is read-only. If
2672 x is a read-only memory, then the program is valid only if we
2673 avoid the store into it. If there are stores on both the
2674 THEN and ELSE arms, then we can go ahead with the conversion;
2675 either the program is broken, or the condition is always
2676 false such that the other memory is selected. */
2677 if (noce_mem_write_may_trap_or_fault_p (orig_x))
2678 return FALSE;
2680 /* Avoid store speculation: given "if (...) x = a" where x is a
2681 MEM, we only want to do the store if x is always set
2682 somewhere in the function. This avoids cases like
2683 if (pthread_mutex_trylock(mutex))
2684 ++global_variable;
2685 where we only want global_variable to be changed if the mutex
2686 is held. FIXME: This should ideally be expressed directly in
2687 RTL somehow. */
2688 if (!noce_can_store_speculate_p (test_bb, orig_x))
2689 return FALSE;
2692 if (noce_try_move (if_info))
2693 goto success;
2694 if (noce_try_store_flag (if_info))
2695 goto success;
2696 if (noce_try_bitop (if_info))
2697 goto success;
2698 if (noce_try_minmax (if_info))
2699 goto success;
2700 if (noce_try_abs (if_info))
2701 goto success;
2702 if (HAVE_conditional_move
2703 && noce_try_cmove (if_info))
2704 goto success;
2705 if (! targetm.have_conditional_execution ())
2707 if (noce_try_store_flag_constants (if_info))
2708 goto success;
2709 if (noce_try_addcc (if_info))
2710 goto success;
2711 if (noce_try_store_flag_mask (if_info))
2712 goto success;
2713 if (HAVE_conditional_move
2714 && noce_try_cmove_arith (if_info))
2715 goto success;
2716 if (noce_try_sign_mask (if_info))
2717 goto success;
2720 if (!else_bb && set_b)
2722 insn_b = NULL;
2723 set_b = NULL_RTX;
2724 b = orig_x;
2725 goto retry;
2728 return FALSE;
2730 success:
2732 /* If we used a temporary, fix it up now. */
2733 if (orig_x != x)
2735 rtx_insn *seq;
2737 start_sequence ();
2738 noce_emit_move_insn (orig_x, x);
2739 seq = get_insns ();
2740 set_used_flags (orig_x);
2741 unshare_all_rtl_in_chain (seq);
2742 end_sequence ();
2744 emit_insn_before_setloc (seq, BB_END (test_bb), INSN_LOCATION (insn_a));
2747 /* The original THEN and ELSE blocks may now be removed. The test block
2748 must now jump to the join block. If the test block and the join block
2749 can be merged, do so. */
2750 if (else_bb)
2752 delete_basic_block (else_bb);
2753 num_true_changes++;
2755 else
2756 remove_edge (find_edge (test_bb, join_bb));
2758 remove_edge (find_edge (then_bb, join_bb));
2759 redirect_edge_and_branch_force (single_succ_edge (test_bb), join_bb);
2760 delete_basic_block (then_bb);
2761 num_true_changes++;
2763 if (can_merge_blocks_p (test_bb, join_bb))
2765 merge_blocks (test_bb, join_bb);
2766 num_true_changes++;
2769 num_updated_if_blocks++;
2770 return TRUE;
2773 /* Check whether a block is suitable for conditional move conversion.
2774 Every insn must be a simple set of a register to a constant or a
2775 register. For each assignment, store the value in the pointer map
2776 VALS, keyed indexed by register pointer, then store the register
2777 pointer in REGS. COND is the condition we will test. */
2779 static int
2780 check_cond_move_block (basic_block bb,
2781 hash_map<rtx, rtx> *vals,
2782 vec<rtx> *regs,
2783 rtx cond)
2785 rtx_insn *insn;
2787 /* We can only handle simple jumps at the end of the basic block.
2788 It is almost impossible to update the CFG otherwise. */
2789 insn = BB_END (bb);
2790 if (JUMP_P (insn) && !onlyjump_p (insn))
2791 return FALSE;
2793 FOR_BB_INSNS (bb, insn)
2795 rtx set, dest, src;
2797 if (!NONDEBUG_INSN_P (insn) || JUMP_P (insn))
2798 continue;
2799 set = single_set (insn);
2800 if (!set)
2801 return FALSE;
2803 dest = SET_DEST (set);
2804 src = SET_SRC (set);
2805 if (!REG_P (dest)
2806 || (HARD_REGISTER_P (dest)
2807 && targetm.small_register_classes_for_mode_p (GET_MODE (dest))))
2808 return FALSE;
2810 if (!CONSTANT_P (src) && !register_operand (src, VOIDmode))
2811 return FALSE;
2813 if (side_effects_p (src) || side_effects_p (dest))
2814 return FALSE;
2816 if (may_trap_p (src) || may_trap_p (dest))
2817 return FALSE;
2819 /* Don't try to handle this if the source register was
2820 modified earlier in the block. */
2821 if ((REG_P (src)
2822 && vals->get (src))
2823 || (GET_CODE (src) == SUBREG && REG_P (SUBREG_REG (src))
2824 && vals->get (SUBREG_REG (src))))
2825 return FALSE;
2827 /* Don't try to handle this if the destination register was
2828 modified earlier in the block. */
2829 if (vals->get (dest))
2830 return FALSE;
2832 /* Don't try to handle this if the condition uses the
2833 destination register. */
2834 if (reg_overlap_mentioned_p (dest, cond))
2835 return FALSE;
2837 /* Don't try to handle this if the source register is modified
2838 later in the block. */
2839 if (!CONSTANT_P (src)
2840 && modified_between_p (src, insn, NEXT_INSN (BB_END (bb))))
2841 return FALSE;
2843 vals->put (dest, src);
2845 regs->safe_push (dest);
2848 return TRUE;
2851 /* Given a basic block BB suitable for conditional move conversion,
2852 a condition COND, and pointer maps THEN_VALS and ELSE_VALS containing
2853 the register values depending on COND, emit the insns in the block as
2854 conditional moves. If ELSE_BLOCK is true, THEN_BB was already
2855 processed. The caller has started a sequence for the conversion.
2856 Return true if successful, false if something goes wrong. */
2858 static bool
2859 cond_move_convert_if_block (struct noce_if_info *if_infop,
2860 basic_block bb, rtx cond,
2861 hash_map<rtx, rtx> *then_vals,
2862 hash_map<rtx, rtx> *else_vals,
2863 bool else_block_p)
2865 enum rtx_code code;
2866 rtx_insn *insn;
2867 rtx cond_arg0, cond_arg1;
2869 code = GET_CODE (cond);
2870 cond_arg0 = XEXP (cond, 0);
2871 cond_arg1 = XEXP (cond, 1);
2873 FOR_BB_INSNS (bb, insn)
2875 rtx set, target, dest, t, e;
2877 /* ??? Maybe emit conditional debug insn? */
2878 if (!NONDEBUG_INSN_P (insn) || JUMP_P (insn))
2879 continue;
2880 set = single_set (insn);
2881 gcc_assert (set && REG_P (SET_DEST (set)));
2883 dest = SET_DEST (set);
2885 rtx *then_slot = then_vals->get (dest);
2886 rtx *else_slot = else_vals->get (dest);
2887 t = then_slot ? *then_slot : NULL_RTX;
2888 e = else_slot ? *else_slot : NULL_RTX;
2890 if (else_block_p)
2892 /* If this register was set in the then block, we already
2893 handled this case there. */
2894 if (t)
2895 continue;
2896 t = dest;
2897 gcc_assert (e);
2899 else
2901 gcc_assert (t);
2902 if (!e)
2903 e = dest;
2906 target = noce_emit_cmove (if_infop, dest, code, cond_arg0, cond_arg1,
2907 t, e);
2908 if (!target)
2909 return false;
2911 if (target != dest)
2912 noce_emit_move_insn (dest, target);
2915 return true;
2918 /* Given a simple IF-THEN-JOIN or IF-THEN-ELSE-JOIN block, attempt to convert
2919 it using only conditional moves. Return TRUE if we were successful at
2920 converting the block. */
2922 static int
2923 cond_move_process_if_block (struct noce_if_info *if_info)
2925 basic_block test_bb = if_info->test_bb;
2926 basic_block then_bb = if_info->then_bb;
2927 basic_block else_bb = if_info->else_bb;
2928 basic_block join_bb = if_info->join_bb;
2929 rtx_insn *jump = if_info->jump;
2930 rtx cond = if_info->cond;
2931 rtx_insn *seq, *loc_insn;
2932 rtx reg;
2933 int c;
2934 vec<rtx> then_regs = vNULL;
2935 vec<rtx> else_regs = vNULL;
2936 unsigned int i;
2937 int success_p = FALSE;
2939 /* Build a mapping for each block to the value used for each
2940 register. */
2941 hash_map<rtx, rtx> then_vals;
2942 hash_map<rtx, rtx> else_vals;
2944 /* Make sure the blocks are suitable. */
2945 if (!check_cond_move_block (then_bb, &then_vals, &then_regs, cond)
2946 || (else_bb
2947 && !check_cond_move_block (else_bb, &else_vals, &else_regs, cond)))
2948 goto done;
2950 /* Make sure the blocks can be used together. If the same register
2951 is set in both blocks, and is not set to a constant in both
2952 cases, then both blocks must set it to the same register. We
2953 have already verified that if it is set to a register, that the
2954 source register does not change after the assignment. Also count
2955 the number of registers set in only one of the blocks. */
2956 c = 0;
2957 FOR_EACH_VEC_ELT (then_regs, i, reg)
2959 rtx *then_slot = then_vals.get (reg);
2960 rtx *else_slot = else_vals.get (reg);
2962 gcc_checking_assert (then_slot);
2963 if (!else_slot)
2964 ++c;
2965 else
2967 rtx then_val = *then_slot;
2968 rtx else_val = *else_slot;
2969 if (!CONSTANT_P (then_val) && !CONSTANT_P (else_val)
2970 && !rtx_equal_p (then_val, else_val))
2971 goto done;
2975 /* Finish off c for MAX_CONDITIONAL_EXECUTE. */
2976 FOR_EACH_VEC_ELT (else_regs, i, reg)
2978 gcc_checking_assert (else_vals.get (reg));
2979 if (!then_vals.get (reg))
2980 ++c;
2983 /* Make sure it is reasonable to convert this block. What matters
2984 is the number of assignments currently made in only one of the
2985 branches, since if we convert we are going to always execute
2986 them. */
2987 if (c > MAX_CONDITIONAL_EXECUTE)
2988 goto done;
2990 /* Try to emit the conditional moves. First do the then block,
2991 then do anything left in the else blocks. */
2992 start_sequence ();
2993 if (!cond_move_convert_if_block (if_info, then_bb, cond,
2994 &then_vals, &else_vals, false)
2995 || (else_bb
2996 && !cond_move_convert_if_block (if_info, else_bb, cond,
2997 &then_vals, &else_vals, true)))
2999 end_sequence ();
3000 goto done;
3002 seq = end_ifcvt_sequence (if_info);
3003 if (!seq)
3004 goto done;
3006 loc_insn = first_active_insn (then_bb);
3007 if (!loc_insn)
3009 loc_insn = first_active_insn (else_bb);
3010 gcc_assert (loc_insn);
3012 emit_insn_before_setloc (seq, jump, INSN_LOCATION (loc_insn));
3014 if (else_bb)
3016 delete_basic_block (else_bb);
3017 num_true_changes++;
3019 else
3020 remove_edge (find_edge (test_bb, join_bb));
3022 remove_edge (find_edge (then_bb, join_bb));
3023 redirect_edge_and_branch_force (single_succ_edge (test_bb), join_bb);
3024 delete_basic_block (then_bb);
3025 num_true_changes++;
3027 if (can_merge_blocks_p (test_bb, join_bb))
3029 merge_blocks (test_bb, join_bb);
3030 num_true_changes++;
3033 num_updated_if_blocks++;
3035 success_p = TRUE;
3037 done:
3038 then_regs.release ();
3039 else_regs.release ();
3040 return success_p;
3044 /* Determine if a given basic block heads a simple IF-THEN-JOIN or an
3045 IF-THEN-ELSE-JOIN block.
3047 If so, we'll try to convert the insns to not require the branch,
3048 using only transformations that do not require conditional execution.
3050 Return TRUE if we were successful at converting the block. */
3052 static int
3053 noce_find_if_block (basic_block test_bb, edge then_edge, edge else_edge,
3054 int pass)
3056 basic_block then_bb, else_bb, join_bb;
3057 bool then_else_reversed = false;
3058 rtx_insn *jump;
3059 rtx cond;
3060 rtx_insn *cond_earliest;
3061 struct noce_if_info if_info;
3063 /* We only ever should get here before reload. */
3064 gcc_assert (!reload_completed);
3066 /* Recognize an IF-THEN-ELSE-JOIN block. */
3067 if (single_pred_p (then_edge->dest)
3068 && single_succ_p (then_edge->dest)
3069 && single_pred_p (else_edge->dest)
3070 && single_succ_p (else_edge->dest)
3071 && single_succ (then_edge->dest) == single_succ (else_edge->dest))
3073 then_bb = then_edge->dest;
3074 else_bb = else_edge->dest;
3075 join_bb = single_succ (then_bb);
3077 /* Recognize an IF-THEN-JOIN block. */
3078 else if (single_pred_p (then_edge->dest)
3079 && single_succ_p (then_edge->dest)
3080 && single_succ (then_edge->dest) == else_edge->dest)
3082 then_bb = then_edge->dest;
3083 else_bb = NULL_BLOCK;
3084 join_bb = else_edge->dest;
3086 /* Recognize an IF-ELSE-JOIN block. We can have those because the order
3087 of basic blocks in cfglayout mode does not matter, so the fallthrough
3088 edge can go to any basic block (and not just to bb->next_bb, like in
3089 cfgrtl mode). */
3090 else if (single_pred_p (else_edge->dest)
3091 && single_succ_p (else_edge->dest)
3092 && single_succ (else_edge->dest) == then_edge->dest)
3094 /* The noce transformations do not apply to IF-ELSE-JOIN blocks.
3095 To make this work, we have to invert the THEN and ELSE blocks
3096 and reverse the jump condition. */
3097 then_bb = else_edge->dest;
3098 else_bb = NULL_BLOCK;
3099 join_bb = single_succ (then_bb);
3100 then_else_reversed = true;
3102 else
3103 /* Not a form we can handle. */
3104 return FALSE;
3106 /* The edges of the THEN and ELSE blocks cannot have complex edges. */
3107 if (single_succ_edge (then_bb)->flags & EDGE_COMPLEX)
3108 return FALSE;
3109 if (else_bb
3110 && single_succ_edge (else_bb)->flags & EDGE_COMPLEX)
3111 return FALSE;
3113 num_possible_if_blocks++;
3115 if (dump_file)
3117 fprintf (dump_file,
3118 "\nIF-THEN%s-JOIN block found, pass %d, test %d, then %d",
3119 (else_bb) ? "-ELSE" : "",
3120 pass, test_bb->index, then_bb->index);
3122 if (else_bb)
3123 fprintf (dump_file, ", else %d", else_bb->index);
3125 fprintf (dump_file, ", join %d\n", join_bb->index);
3128 /* If the conditional jump is more than just a conditional
3129 jump, then we can not do if-conversion on this block. */
3130 jump = BB_END (test_bb);
3131 if (! onlyjump_p (jump))
3132 return FALSE;
3134 /* If this is not a standard conditional jump, we can't parse it. */
3135 cond = noce_get_condition (jump, &cond_earliest, then_else_reversed);
3136 if (!cond)
3137 return FALSE;
3139 /* We must be comparing objects whose modes imply the size. */
3140 if (GET_MODE (XEXP (cond, 0)) == BLKmode)
3141 return FALSE;
3143 /* Initialize an IF_INFO struct to pass around. */
3144 memset (&if_info, 0, sizeof if_info);
3145 if_info.test_bb = test_bb;
3146 if_info.then_bb = then_bb;
3147 if_info.else_bb = else_bb;
3148 if_info.join_bb = join_bb;
3149 if_info.cond = cond;
3150 if_info.cond_earliest = cond_earliest;
3151 if_info.jump = jump;
3152 if_info.then_else_reversed = then_else_reversed;
3153 if_info.branch_cost = BRANCH_COST (optimize_bb_for_speed_p (test_bb),
3154 predictable_edge_p (then_edge));
3156 /* Do the real work. */
3158 if (noce_process_if_block (&if_info))
3159 return TRUE;
3161 if (HAVE_conditional_move
3162 && cond_move_process_if_block (&if_info))
3163 return TRUE;
3165 return FALSE;
3169 /* Merge the blocks and mark for local life update. */
3171 static void
3172 merge_if_block (struct ce_if_block * ce_info)
3174 basic_block test_bb = ce_info->test_bb; /* last test block */
3175 basic_block then_bb = ce_info->then_bb; /* THEN */
3176 basic_block else_bb = ce_info->else_bb; /* ELSE or NULL */
3177 basic_block join_bb = ce_info->join_bb; /* join block */
3178 basic_block combo_bb;
3180 /* All block merging is done into the lower block numbers. */
3182 combo_bb = test_bb;
3183 df_set_bb_dirty (test_bb);
3185 /* Merge any basic blocks to handle && and || subtests. Each of
3186 the blocks are on the fallthru path from the predecessor block. */
3187 if (ce_info->num_multiple_test_blocks > 0)
3189 basic_block bb = test_bb;
3190 basic_block last_test_bb = ce_info->last_test_bb;
3191 basic_block fallthru = block_fallthru (bb);
3195 bb = fallthru;
3196 fallthru = block_fallthru (bb);
3197 merge_blocks (combo_bb, bb);
3198 num_true_changes++;
3200 while (bb != last_test_bb);
3203 /* Merge TEST block into THEN block. Normally the THEN block won't have a
3204 label, but it might if there were || tests. That label's count should be
3205 zero, and it normally should be removed. */
3207 if (then_bb)
3209 /* If THEN_BB has no successors, then there's a BARRIER after it.
3210 If COMBO_BB has more than one successor (THEN_BB), then that BARRIER
3211 is no longer needed, and in fact it is incorrect to leave it in
3212 the insn stream. */
3213 if (EDGE_COUNT (then_bb->succs) == 0
3214 && EDGE_COUNT (combo_bb->succs) > 1)
3216 rtx_insn *end = NEXT_INSN (BB_END (then_bb));
3217 while (end && NOTE_P (end) && !NOTE_INSN_BASIC_BLOCK_P (end))
3218 end = NEXT_INSN (end);
3220 if (end && BARRIER_P (end))
3221 delete_insn (end);
3223 merge_blocks (combo_bb, then_bb);
3224 num_true_changes++;
3227 /* The ELSE block, if it existed, had a label. That label count
3228 will almost always be zero, but odd things can happen when labels
3229 get their addresses taken. */
3230 if (else_bb)
3232 /* If ELSE_BB has no successors, then there's a BARRIER after it.
3233 If COMBO_BB has more than one successor (ELSE_BB), then that BARRIER
3234 is no longer needed, and in fact it is incorrect to leave it in
3235 the insn stream. */
3236 if (EDGE_COUNT (else_bb->succs) == 0
3237 && EDGE_COUNT (combo_bb->succs) > 1)
3239 rtx_insn *end = NEXT_INSN (BB_END (else_bb));
3240 while (end && NOTE_P (end) && !NOTE_INSN_BASIC_BLOCK_P (end))
3241 end = NEXT_INSN (end);
3243 if (end && BARRIER_P (end))
3244 delete_insn (end);
3246 merge_blocks (combo_bb, else_bb);
3247 num_true_changes++;
3250 /* If there was no join block reported, that means it was not adjacent
3251 to the others, and so we cannot merge them. */
3253 if (! join_bb)
3255 rtx_insn *last = BB_END (combo_bb);
3257 /* The outgoing edge for the current COMBO block should already
3258 be correct. Verify this. */
3259 if (EDGE_COUNT (combo_bb->succs) == 0)
3260 gcc_assert (find_reg_note (last, REG_NORETURN, NULL)
3261 || (NONJUMP_INSN_P (last)
3262 && GET_CODE (PATTERN (last)) == TRAP_IF
3263 && (TRAP_CONDITION (PATTERN (last))
3264 == const_true_rtx)));
3266 else
3267 /* There should still be something at the end of the THEN or ELSE
3268 blocks taking us to our final destination. */
3269 gcc_assert (JUMP_P (last)
3270 || (EDGE_SUCC (combo_bb, 0)->dest
3271 == EXIT_BLOCK_PTR_FOR_FN (cfun)
3272 && CALL_P (last)
3273 && SIBLING_CALL_P (last))
3274 || ((EDGE_SUCC (combo_bb, 0)->flags & EDGE_EH)
3275 && can_throw_internal (last)));
3278 /* The JOIN block may have had quite a number of other predecessors too.
3279 Since we've already merged the TEST, THEN and ELSE blocks, we should
3280 have only one remaining edge from our if-then-else diamond. If there
3281 is more than one remaining edge, it must come from elsewhere. There
3282 may be zero incoming edges if the THEN block didn't actually join
3283 back up (as with a call to a non-return function). */
3284 else if (EDGE_COUNT (join_bb->preds) < 2
3285 && join_bb != EXIT_BLOCK_PTR_FOR_FN (cfun))
3287 /* We can merge the JOIN cleanly and update the dataflow try
3288 again on this pass.*/
3289 merge_blocks (combo_bb, join_bb);
3290 num_true_changes++;
3292 else
3294 /* We cannot merge the JOIN. */
3296 /* The outgoing edge for the current COMBO block should already
3297 be correct. Verify this. */
3298 gcc_assert (single_succ_p (combo_bb)
3299 && single_succ (combo_bb) == join_bb);
3301 /* Remove the jump and cruft from the end of the COMBO block. */
3302 if (join_bb != EXIT_BLOCK_PTR_FOR_FN (cfun))
3303 tidy_fallthru_edge (single_succ_edge (combo_bb));
3306 num_updated_if_blocks++;
3309 /* Find a block ending in a simple IF condition and try to transform it
3310 in some way. When converting a multi-block condition, put the new code
3311 in the first such block and delete the rest. Return a pointer to this
3312 first block if some transformation was done. Return NULL otherwise. */
3314 static basic_block
3315 find_if_header (basic_block test_bb, int pass)
3317 ce_if_block ce_info;
3318 edge then_edge;
3319 edge else_edge;
3321 /* The kind of block we're looking for has exactly two successors. */
3322 if (EDGE_COUNT (test_bb->succs) != 2)
3323 return NULL;
3325 then_edge = EDGE_SUCC (test_bb, 0);
3326 else_edge = EDGE_SUCC (test_bb, 1);
3328 if (df_get_bb_dirty (then_edge->dest))
3329 return NULL;
3330 if (df_get_bb_dirty (else_edge->dest))
3331 return NULL;
3333 /* Neither edge should be abnormal. */
3334 if ((then_edge->flags & EDGE_COMPLEX)
3335 || (else_edge->flags & EDGE_COMPLEX))
3336 return NULL;
3338 /* Nor exit the loop. */
3339 if ((then_edge->flags & EDGE_LOOP_EXIT)
3340 || (else_edge->flags & EDGE_LOOP_EXIT))
3341 return NULL;
3343 /* The THEN edge is canonically the one that falls through. */
3344 if (then_edge->flags & EDGE_FALLTHRU)
3346 else if (else_edge->flags & EDGE_FALLTHRU)
3348 edge e = else_edge;
3349 else_edge = then_edge;
3350 then_edge = e;
3352 else
3353 /* Otherwise this must be a multiway branch of some sort. */
3354 return NULL;
3356 memset (&ce_info, 0, sizeof (ce_info));
3357 ce_info.test_bb = test_bb;
3358 ce_info.then_bb = then_edge->dest;
3359 ce_info.else_bb = else_edge->dest;
3360 ce_info.pass = pass;
3362 #ifdef IFCVT_MACHDEP_INIT
3363 IFCVT_MACHDEP_INIT (&ce_info);
3364 #endif
3366 if (!reload_completed
3367 && noce_find_if_block (test_bb, then_edge, else_edge, pass))
3368 goto success;
3370 if (reload_completed
3371 && targetm.have_conditional_execution ()
3372 && cond_exec_find_if_block (&ce_info))
3373 goto success;
3375 if (HAVE_trap
3376 && optab_handler (ctrap_optab, word_mode) != CODE_FOR_nothing
3377 && find_cond_trap (test_bb, then_edge, else_edge))
3378 goto success;
3380 if (dom_info_state (CDI_POST_DOMINATORS) >= DOM_NO_FAST_QUERY
3381 && (reload_completed || !targetm.have_conditional_execution ()))
3383 if (find_if_case_1 (test_bb, then_edge, else_edge))
3384 goto success;
3385 if (find_if_case_2 (test_bb, then_edge, else_edge))
3386 goto success;
3389 return NULL;
3391 success:
3392 if (dump_file)
3393 fprintf (dump_file, "Conversion succeeded on pass %d.\n", pass);
3394 /* Set this so we continue looking. */
3395 cond_exec_changed_p = TRUE;
3396 return ce_info.test_bb;
3399 /* Return true if a block has two edges, one of which falls through to the next
3400 block, and the other jumps to a specific block, so that we can tell if the
3401 block is part of an && test or an || test. Returns either -1 or the number
3402 of non-note, non-jump, non-USE/CLOBBER insns in the block. */
3404 static int
3405 block_jumps_and_fallthru_p (basic_block cur_bb, basic_block target_bb)
3407 edge cur_edge;
3408 int fallthru_p = FALSE;
3409 int jump_p = FALSE;
3410 rtx_insn *insn;
3411 rtx_insn *end;
3412 int n_insns = 0;
3413 edge_iterator ei;
3415 if (!cur_bb || !target_bb)
3416 return -1;
3418 /* If no edges, obviously it doesn't jump or fallthru. */
3419 if (EDGE_COUNT (cur_bb->succs) == 0)
3420 return FALSE;
3422 FOR_EACH_EDGE (cur_edge, ei, cur_bb->succs)
3424 if (cur_edge->flags & EDGE_COMPLEX)
3425 /* Anything complex isn't what we want. */
3426 return -1;
3428 else if (cur_edge->flags & EDGE_FALLTHRU)
3429 fallthru_p = TRUE;
3431 else if (cur_edge->dest == target_bb)
3432 jump_p = TRUE;
3434 else
3435 return -1;
3438 if ((jump_p & fallthru_p) == 0)
3439 return -1;
3441 /* Don't allow calls in the block, since this is used to group && and ||
3442 together for conditional execution support. ??? we should support
3443 conditional execution support across calls for IA-64 some day, but
3444 for now it makes the code simpler. */
3445 end = BB_END (cur_bb);
3446 insn = BB_HEAD (cur_bb);
3448 while (insn != NULL_RTX)
3450 if (CALL_P (insn))
3451 return -1;
3453 if (INSN_P (insn)
3454 && !JUMP_P (insn)
3455 && !DEBUG_INSN_P (insn)
3456 && GET_CODE (PATTERN (insn)) != USE
3457 && GET_CODE (PATTERN (insn)) != CLOBBER)
3458 n_insns++;
3460 if (insn == end)
3461 break;
3463 insn = NEXT_INSN (insn);
3466 return n_insns;
3469 /* Determine if a given basic block heads a simple IF-THEN or IF-THEN-ELSE
3470 block. If so, we'll try to convert the insns to not require the branch.
3471 Return TRUE if we were successful at converting the block. */
3473 static int
3474 cond_exec_find_if_block (struct ce_if_block * ce_info)
3476 basic_block test_bb = ce_info->test_bb;
3477 basic_block then_bb = ce_info->then_bb;
3478 basic_block else_bb = ce_info->else_bb;
3479 basic_block join_bb = NULL_BLOCK;
3480 edge cur_edge;
3481 basic_block next;
3482 edge_iterator ei;
3484 ce_info->last_test_bb = test_bb;
3486 /* We only ever should get here after reload,
3487 and if we have conditional execution. */
3488 gcc_assert (reload_completed && targetm.have_conditional_execution ());
3490 /* Discover if any fall through predecessors of the current test basic block
3491 were && tests (which jump to the else block) or || tests (which jump to
3492 the then block). */
3493 if (single_pred_p (test_bb)
3494 && single_pred_edge (test_bb)->flags == EDGE_FALLTHRU)
3496 basic_block bb = single_pred (test_bb);
3497 basic_block target_bb;
3498 int max_insns = MAX_CONDITIONAL_EXECUTE;
3499 int n_insns;
3501 /* Determine if the preceding block is an && or || block. */
3502 if ((n_insns = block_jumps_and_fallthru_p (bb, else_bb)) >= 0)
3504 ce_info->and_and_p = TRUE;
3505 target_bb = else_bb;
3507 else if ((n_insns = block_jumps_and_fallthru_p (bb, then_bb)) >= 0)
3509 ce_info->and_and_p = FALSE;
3510 target_bb = then_bb;
3512 else
3513 target_bb = NULL_BLOCK;
3515 if (target_bb && n_insns <= max_insns)
3517 int total_insns = 0;
3518 int blocks = 0;
3520 ce_info->last_test_bb = test_bb;
3522 /* Found at least one && or || block, look for more. */
3525 ce_info->test_bb = test_bb = bb;
3526 total_insns += n_insns;
3527 blocks++;
3529 if (!single_pred_p (bb))
3530 break;
3532 bb = single_pred (bb);
3533 n_insns = block_jumps_and_fallthru_p (bb, target_bb);
3535 while (n_insns >= 0 && (total_insns + n_insns) <= max_insns);
3537 ce_info->num_multiple_test_blocks = blocks;
3538 ce_info->num_multiple_test_insns = total_insns;
3540 if (ce_info->and_and_p)
3541 ce_info->num_and_and_blocks = blocks;
3542 else
3543 ce_info->num_or_or_blocks = blocks;
3547 /* The THEN block of an IF-THEN combo must have exactly one predecessor,
3548 other than any || blocks which jump to the THEN block. */
3549 if ((EDGE_COUNT (then_bb->preds) - ce_info->num_or_or_blocks) != 1)
3550 return FALSE;
3552 /* The edges of the THEN and ELSE blocks cannot have complex edges. */
3553 FOR_EACH_EDGE (cur_edge, ei, then_bb->preds)
3555 if (cur_edge->flags & EDGE_COMPLEX)
3556 return FALSE;
3559 FOR_EACH_EDGE (cur_edge, ei, else_bb->preds)
3561 if (cur_edge->flags & EDGE_COMPLEX)
3562 return FALSE;
3565 /* The THEN block of an IF-THEN combo must have zero or one successors. */
3566 if (EDGE_COUNT (then_bb->succs) > 0
3567 && (!single_succ_p (then_bb)
3568 || (single_succ_edge (then_bb)->flags & EDGE_COMPLEX)
3569 || (epilogue_completed
3570 && tablejump_p (BB_END (then_bb), NULL, NULL))))
3571 return FALSE;
3573 /* If the THEN block has no successors, conditional execution can still
3574 make a conditional call. Don't do this unless the ELSE block has
3575 only one incoming edge -- the CFG manipulation is too ugly otherwise.
3576 Check for the last insn of the THEN block being an indirect jump, which
3577 is listed as not having any successors, but confuses the rest of the CE
3578 code processing. ??? we should fix this in the future. */
3579 if (EDGE_COUNT (then_bb->succs) == 0)
3581 if (single_pred_p (else_bb) && else_bb != EXIT_BLOCK_PTR_FOR_FN (cfun))
3583 rtx_insn *last_insn = BB_END (then_bb);
3585 while (last_insn
3586 && NOTE_P (last_insn)
3587 && last_insn != BB_HEAD (then_bb))
3588 last_insn = PREV_INSN (last_insn);
3590 if (last_insn
3591 && JUMP_P (last_insn)
3592 && ! simplejump_p (last_insn))
3593 return FALSE;
3595 join_bb = else_bb;
3596 else_bb = NULL_BLOCK;
3598 else
3599 return FALSE;
3602 /* If the THEN block's successor is the other edge out of the TEST block,
3603 then we have an IF-THEN combo without an ELSE. */
3604 else if (single_succ (then_bb) == else_bb)
3606 join_bb = else_bb;
3607 else_bb = NULL_BLOCK;
3610 /* If the THEN and ELSE block meet in a subsequent block, and the ELSE
3611 has exactly one predecessor and one successor, and the outgoing edge
3612 is not complex, then we have an IF-THEN-ELSE combo. */
3613 else if (single_succ_p (else_bb)
3614 && single_succ (then_bb) == single_succ (else_bb)
3615 && single_pred_p (else_bb)
3616 && !(single_succ_edge (else_bb)->flags & EDGE_COMPLEX)
3617 && !(epilogue_completed
3618 && tablejump_p (BB_END (else_bb), NULL, NULL)))
3619 join_bb = single_succ (else_bb);
3621 /* Otherwise it is not an IF-THEN or IF-THEN-ELSE combination. */
3622 else
3623 return FALSE;
3625 num_possible_if_blocks++;
3627 if (dump_file)
3629 fprintf (dump_file,
3630 "\nIF-THEN%s block found, pass %d, start block %d "
3631 "[insn %d], then %d [%d]",
3632 (else_bb) ? "-ELSE" : "",
3633 ce_info->pass,
3634 test_bb->index,
3635 BB_HEAD (test_bb) ? (int)INSN_UID (BB_HEAD (test_bb)) : -1,
3636 then_bb->index,
3637 BB_HEAD (then_bb) ? (int)INSN_UID (BB_HEAD (then_bb)) : -1);
3639 if (else_bb)
3640 fprintf (dump_file, ", else %d [%d]",
3641 else_bb->index,
3642 BB_HEAD (else_bb) ? (int)INSN_UID (BB_HEAD (else_bb)) : -1);
3644 fprintf (dump_file, ", join %d [%d]",
3645 join_bb->index,
3646 BB_HEAD (join_bb) ? (int)INSN_UID (BB_HEAD (join_bb)) : -1);
3648 if (ce_info->num_multiple_test_blocks > 0)
3649 fprintf (dump_file, ", %d %s block%s last test %d [%d]",
3650 ce_info->num_multiple_test_blocks,
3651 (ce_info->and_and_p) ? "&&" : "||",
3652 (ce_info->num_multiple_test_blocks == 1) ? "" : "s",
3653 ce_info->last_test_bb->index,
3654 ((BB_HEAD (ce_info->last_test_bb))
3655 ? (int)INSN_UID (BB_HEAD (ce_info->last_test_bb))
3656 : -1));
3658 fputc ('\n', dump_file);
3661 /* Make sure IF, THEN, and ELSE, blocks are adjacent. Actually, we get the
3662 first condition for free, since we've already asserted that there's a
3663 fallthru edge from IF to THEN. Likewise for the && and || blocks, since
3664 we checked the FALLTHRU flag, those are already adjacent to the last IF
3665 block. */
3666 /* ??? As an enhancement, move the ELSE block. Have to deal with
3667 BLOCK notes, if by no other means than backing out the merge if they
3668 exist. Sticky enough I don't want to think about it now. */
3669 next = then_bb;
3670 if (else_bb && (next = next->next_bb) != else_bb)
3671 return FALSE;
3672 if ((next = next->next_bb) != join_bb
3673 && join_bb != EXIT_BLOCK_PTR_FOR_FN (cfun))
3675 if (else_bb)
3676 join_bb = NULL;
3677 else
3678 return FALSE;
3681 /* Do the real work. */
3683 ce_info->else_bb = else_bb;
3684 ce_info->join_bb = join_bb;
3686 /* If we have && and || tests, try to first handle combining the && and ||
3687 tests into the conditional code, and if that fails, go back and handle
3688 it without the && and ||, which at present handles the && case if there
3689 was no ELSE block. */
3690 if (cond_exec_process_if_block (ce_info, TRUE))
3691 return TRUE;
3693 if (ce_info->num_multiple_test_blocks)
3695 cancel_changes (0);
3697 if (cond_exec_process_if_block (ce_info, FALSE))
3698 return TRUE;
3701 return FALSE;
3704 /* Convert a branch over a trap, or a branch
3705 to a trap, into a conditional trap. */
3707 static int
3708 find_cond_trap (basic_block test_bb, edge then_edge, edge else_edge)
3710 basic_block then_bb = then_edge->dest;
3711 basic_block else_bb = else_edge->dest;
3712 basic_block other_bb, trap_bb;
3713 rtx_insn *trap, *jump;
3714 rtx cond, seq;
3715 rtx_insn *cond_earliest;
3716 enum rtx_code code;
3718 /* Locate the block with the trap instruction. */
3719 /* ??? While we look for no successors, we really ought to allow
3720 EH successors. Need to fix merge_if_block for that to work. */
3721 if ((trap = block_has_only_trap (then_bb)) != NULL)
3722 trap_bb = then_bb, other_bb = else_bb;
3723 else if ((trap = block_has_only_trap (else_bb)) != NULL)
3724 trap_bb = else_bb, other_bb = then_bb;
3725 else
3726 return FALSE;
3728 if (dump_file)
3730 fprintf (dump_file, "\nTRAP-IF block found, start %d, trap %d\n",
3731 test_bb->index, trap_bb->index);
3734 /* If this is not a standard conditional jump, we can't parse it. */
3735 jump = BB_END (test_bb);
3736 cond = noce_get_condition (jump, &cond_earliest, false);
3737 if (! cond)
3738 return FALSE;
3740 /* If the conditional jump is more than just a conditional jump, then
3741 we can not do if-conversion on this block. */
3742 if (! onlyjump_p (jump))
3743 return FALSE;
3745 /* We must be comparing objects whose modes imply the size. */
3746 if (GET_MODE (XEXP (cond, 0)) == BLKmode)
3747 return FALSE;
3749 /* Reverse the comparison code, if necessary. */
3750 code = GET_CODE (cond);
3751 if (then_bb == trap_bb)
3753 code = reversed_comparison_code (cond, jump);
3754 if (code == UNKNOWN)
3755 return FALSE;
3758 /* Attempt to generate the conditional trap. */
3759 seq = gen_cond_trap (code, copy_rtx (XEXP (cond, 0)),
3760 copy_rtx (XEXP (cond, 1)),
3761 TRAP_CODE (PATTERN (trap)));
3762 if (seq == NULL)
3763 return FALSE;
3765 /* Emit the new insns before cond_earliest. */
3766 emit_insn_before_setloc (seq, cond_earliest, INSN_LOCATION (trap));
3768 /* Delete the trap block if possible. */
3769 remove_edge (trap_bb == then_bb ? then_edge : else_edge);
3770 df_set_bb_dirty (test_bb);
3771 df_set_bb_dirty (then_bb);
3772 df_set_bb_dirty (else_bb);
3774 if (EDGE_COUNT (trap_bb->preds) == 0)
3776 delete_basic_block (trap_bb);
3777 num_true_changes++;
3780 /* Wire together the blocks again. */
3781 if (current_ir_type () == IR_RTL_CFGLAYOUT)
3782 single_succ_edge (test_bb)->flags |= EDGE_FALLTHRU;
3783 else if (trap_bb == then_bb)
3785 rtx lab;
3786 rtx_insn *newjump;
3788 lab = JUMP_LABEL (jump);
3789 newjump = emit_jump_insn_after (gen_jump (lab), jump);
3790 LABEL_NUSES (lab) += 1;
3791 JUMP_LABEL (newjump) = lab;
3792 emit_barrier_after (newjump);
3794 delete_insn (jump);
3796 if (can_merge_blocks_p (test_bb, other_bb))
3798 merge_blocks (test_bb, other_bb);
3799 num_true_changes++;
3802 num_updated_if_blocks++;
3803 return TRUE;
3806 /* Subroutine of find_cond_trap: if BB contains only a trap insn,
3807 return it. */
3809 static rtx_insn *
3810 block_has_only_trap (basic_block bb)
3812 rtx_insn *trap;
3814 /* We're not the exit block. */
3815 if (bb == EXIT_BLOCK_PTR_FOR_FN (cfun))
3816 return NULL;
3818 /* The block must have no successors. */
3819 if (EDGE_COUNT (bb->succs) > 0)
3820 return NULL;
3822 /* The only instruction in the THEN block must be the trap. */
3823 trap = first_active_insn (bb);
3824 if (! (trap == BB_END (bb)
3825 && GET_CODE (PATTERN (trap)) == TRAP_IF
3826 && TRAP_CONDITION (PATTERN (trap)) == const_true_rtx))
3827 return NULL;
3829 return trap;
3832 /* Look for IF-THEN-ELSE cases in which one of THEN or ELSE is
3833 transformable, but not necessarily the other. There need be no
3834 JOIN block.
3836 Return TRUE if we were successful at converting the block.
3838 Cases we'd like to look at:
3841 if (test) goto over; // x not live
3842 x = a;
3843 goto label;
3844 over:
3846 becomes
3848 x = a;
3849 if (! test) goto label;
3852 if (test) goto E; // x not live
3853 x = big();
3854 goto L;
3856 x = b;
3857 goto M;
3859 becomes
3861 x = b;
3862 if (test) goto M;
3863 x = big();
3864 goto L;
3866 (3) // This one's really only interesting for targets that can do
3867 // multiway branching, e.g. IA-64 BBB bundles. For other targets
3868 // it results in multiple branches on a cache line, which often
3869 // does not sit well with predictors.
3871 if (test1) goto E; // predicted not taken
3872 x = a;
3873 if (test2) goto F;
3876 x = b;
3879 becomes
3881 x = a;
3882 if (test1) goto E;
3883 if (test2) goto F;
3885 Notes:
3887 (A) Don't do (2) if the branch is predicted against the block we're
3888 eliminating. Do it anyway if we can eliminate a branch; this requires
3889 that the sole successor of the eliminated block postdominate the other
3890 side of the if.
3892 (B) With CE, on (3) we can steal from both sides of the if, creating
3894 if (test1) x = a;
3895 if (!test1) x = b;
3896 if (test1) goto J;
3897 if (test2) goto F;
3901 Again, this is most useful if J postdominates.
3903 (C) CE substitutes for helpful life information.
3905 (D) These heuristics need a lot of work. */
3907 /* Tests for case 1 above. */
3909 static int
3910 find_if_case_1 (basic_block test_bb, edge then_edge, edge else_edge)
3912 basic_block then_bb = then_edge->dest;
3913 basic_block else_bb = else_edge->dest;
3914 basic_block new_bb;
3915 int then_bb_index, then_prob;
3916 rtx else_target = NULL_RTX;
3918 /* If we are partitioning hot/cold basic blocks, we don't want to
3919 mess up unconditional or indirect jumps that cross between hot
3920 and cold sections.
3922 Basic block partitioning may result in some jumps that appear to
3923 be optimizable (or blocks that appear to be mergeable), but which really
3924 must be left untouched (they are required to make it safely across
3925 partition boundaries). See the comments at the top of
3926 bb-reorder.c:partition_hot_cold_basic_blocks for complete details. */
3928 if ((BB_END (then_bb)
3929 && JUMP_P (BB_END (then_bb))
3930 && CROSSING_JUMP_P (BB_END (then_bb)))
3931 || (BB_END (test_bb)
3932 && JUMP_P (BB_END (test_bb))
3933 && CROSSING_JUMP_P (BB_END (test_bb)))
3934 || (BB_END (else_bb)
3935 && JUMP_P (BB_END (else_bb))
3936 && CROSSING_JUMP_P (BB_END (else_bb))))
3937 return FALSE;
3939 /* THEN has one successor. */
3940 if (!single_succ_p (then_bb))
3941 return FALSE;
3943 /* THEN does not fall through, but is not strange either. */
3944 if (single_succ_edge (then_bb)->flags & (EDGE_COMPLEX | EDGE_FALLTHRU))
3945 return FALSE;
3947 /* THEN has one predecessor. */
3948 if (!single_pred_p (then_bb))
3949 return FALSE;
3951 /* THEN must do something. */
3952 if (forwarder_block_p (then_bb))
3953 return FALSE;
3955 num_possible_if_blocks++;
3956 if (dump_file)
3957 fprintf (dump_file,
3958 "\nIF-CASE-1 found, start %d, then %d\n",
3959 test_bb->index, then_bb->index);
3961 if (then_edge->probability)
3962 then_prob = REG_BR_PROB_BASE - then_edge->probability;
3963 else
3964 then_prob = REG_BR_PROB_BASE / 2;
3966 /* We're speculating from the THEN path, we want to make sure the cost
3967 of speculation is within reason. */
3968 if (! cheap_bb_rtx_cost_p (then_bb, then_prob,
3969 COSTS_N_INSNS (BRANCH_COST (optimize_bb_for_speed_p (then_edge->src),
3970 predictable_edge_p (then_edge)))))
3971 return FALSE;
3973 if (else_bb == EXIT_BLOCK_PTR_FOR_FN (cfun))
3975 rtx_insn *jump = BB_END (else_edge->src);
3976 gcc_assert (JUMP_P (jump));
3977 else_target = JUMP_LABEL (jump);
3980 /* Registers set are dead, or are predicable. */
3981 if (! dead_or_predicable (test_bb, then_bb, else_bb,
3982 single_succ_edge (then_bb), 1))
3983 return FALSE;
3985 /* Conversion went ok, including moving the insns and fixing up the
3986 jump. Adjust the CFG to match. */
3988 /* We can avoid creating a new basic block if then_bb is immediately
3989 followed by else_bb, i.e. deleting then_bb allows test_bb to fall
3990 through to else_bb. */
3992 if (then_bb->next_bb == else_bb
3993 && then_bb->prev_bb == test_bb
3994 && else_bb != EXIT_BLOCK_PTR_FOR_FN (cfun))
3996 redirect_edge_succ (FALLTHRU_EDGE (test_bb), else_bb);
3997 new_bb = 0;
3999 else if (else_bb == EXIT_BLOCK_PTR_FOR_FN (cfun))
4000 new_bb = force_nonfallthru_and_redirect (FALLTHRU_EDGE (test_bb),
4001 else_bb, else_target);
4002 else
4003 new_bb = redirect_edge_and_branch_force (FALLTHRU_EDGE (test_bb),
4004 else_bb);
4006 df_set_bb_dirty (test_bb);
4007 df_set_bb_dirty (else_bb);
4009 then_bb_index = then_bb->index;
4010 delete_basic_block (then_bb);
4012 /* Make rest of code believe that the newly created block is the THEN_BB
4013 block we removed. */
4014 if (new_bb)
4016 df_bb_replace (then_bb_index, new_bb);
4017 /* This should have been done above via force_nonfallthru_and_redirect
4018 (possibly called from redirect_edge_and_branch_force). */
4019 gcc_checking_assert (BB_PARTITION (new_bb) == BB_PARTITION (test_bb));
4022 num_true_changes++;
4023 num_updated_if_blocks++;
4025 return TRUE;
4028 /* Test for case 2 above. */
4030 static int
4031 find_if_case_2 (basic_block test_bb, edge then_edge, edge else_edge)
4033 basic_block then_bb = then_edge->dest;
4034 basic_block else_bb = else_edge->dest;
4035 edge else_succ;
4036 int then_prob, else_prob;
4038 /* We do not want to speculate (empty) loop latches. */
4039 if (current_loops
4040 && else_bb->loop_father->latch == else_bb)
4041 return FALSE;
4043 /* If we are partitioning hot/cold basic blocks, we don't want to
4044 mess up unconditional or indirect jumps that cross between hot
4045 and cold sections.
4047 Basic block partitioning may result in some jumps that appear to
4048 be optimizable (or blocks that appear to be mergeable), but which really
4049 must be left untouched (they are required to make it safely across
4050 partition boundaries). See the comments at the top of
4051 bb-reorder.c:partition_hot_cold_basic_blocks for complete details. */
4053 if ((BB_END (then_bb)
4054 && JUMP_P (BB_END (then_bb))
4055 && CROSSING_JUMP_P (BB_END (then_bb)))
4056 || (BB_END (test_bb)
4057 && JUMP_P (BB_END (test_bb))
4058 && CROSSING_JUMP_P (BB_END (test_bb)))
4059 || (BB_END (else_bb)
4060 && JUMP_P (BB_END (else_bb))
4061 && CROSSING_JUMP_P (BB_END (else_bb))))
4062 return FALSE;
4064 /* ELSE has one successor. */
4065 if (!single_succ_p (else_bb))
4066 return FALSE;
4067 else
4068 else_succ = single_succ_edge (else_bb);
4070 /* ELSE outgoing edge is not complex. */
4071 if (else_succ->flags & EDGE_COMPLEX)
4072 return FALSE;
4074 /* ELSE has one predecessor. */
4075 if (!single_pred_p (else_bb))
4076 return FALSE;
4078 /* THEN is not EXIT. */
4079 if (then_bb->index < NUM_FIXED_BLOCKS)
4080 return FALSE;
4082 if (else_edge->probability)
4084 else_prob = else_edge->probability;
4085 then_prob = REG_BR_PROB_BASE - else_prob;
4087 else
4089 else_prob = REG_BR_PROB_BASE / 2;
4090 then_prob = REG_BR_PROB_BASE / 2;
4093 /* ELSE is predicted or SUCC(ELSE) postdominates THEN. */
4094 if (else_prob > then_prob)
4096 else if (else_succ->dest->index < NUM_FIXED_BLOCKS
4097 || dominated_by_p (CDI_POST_DOMINATORS, then_bb,
4098 else_succ->dest))
4100 else
4101 return FALSE;
4103 num_possible_if_blocks++;
4104 if (dump_file)
4105 fprintf (dump_file,
4106 "\nIF-CASE-2 found, start %d, else %d\n",
4107 test_bb->index, else_bb->index);
4109 /* We're speculating from the ELSE path, we want to make sure the cost
4110 of speculation is within reason. */
4111 if (! cheap_bb_rtx_cost_p (else_bb, else_prob,
4112 COSTS_N_INSNS (BRANCH_COST (optimize_bb_for_speed_p (else_edge->src),
4113 predictable_edge_p (else_edge)))))
4114 return FALSE;
4116 /* Registers set are dead, or are predicable. */
4117 if (! dead_or_predicable (test_bb, else_bb, then_bb, else_succ, 0))
4118 return FALSE;
4120 /* Conversion went ok, including moving the insns and fixing up the
4121 jump. Adjust the CFG to match. */
4123 df_set_bb_dirty (test_bb);
4124 df_set_bb_dirty (then_bb);
4125 delete_basic_block (else_bb);
4127 num_true_changes++;
4128 num_updated_if_blocks++;
4130 /* ??? We may now fallthru from one of THEN's successors into a join
4131 block. Rerun cleanup_cfg? Examine things manually? Wait? */
4133 return TRUE;
4136 /* Used by the code above to perform the actual rtl transformations.
4137 Return TRUE if successful.
4139 TEST_BB is the block containing the conditional branch. MERGE_BB
4140 is the block containing the code to manipulate. DEST_EDGE is an
4141 edge representing a jump to the join block; after the conversion,
4142 TEST_BB should be branching to its destination.
4143 REVERSEP is true if the sense of the branch should be reversed. */
4145 static int
4146 dead_or_predicable (basic_block test_bb, basic_block merge_bb,
4147 basic_block other_bb, edge dest_edge, int reversep)
4149 basic_block new_dest = dest_edge->dest;
4150 rtx_insn *head, *end, *jump;
4151 rtx_insn *earliest = NULL;
4152 rtx old_dest;
4153 bitmap merge_set = NULL;
4154 /* Number of pending changes. */
4155 int n_validated_changes = 0;
4156 rtx new_dest_label = NULL_RTX;
4158 jump = BB_END (test_bb);
4160 /* Find the extent of the real code in the merge block. */
4161 head = BB_HEAD (merge_bb);
4162 end = BB_END (merge_bb);
4164 while (DEBUG_INSN_P (end) && end != head)
4165 end = PREV_INSN (end);
4167 /* If merge_bb ends with a tablejump, predicating/moving insn's
4168 into test_bb and then deleting merge_bb will result in the jumptable
4169 that follows merge_bb being removed along with merge_bb and then we
4170 get an unresolved reference to the jumptable. */
4171 if (tablejump_p (end, NULL, NULL))
4172 return FALSE;
4174 if (LABEL_P (head))
4175 head = NEXT_INSN (head);
4176 while (DEBUG_INSN_P (head) && head != end)
4177 head = NEXT_INSN (head);
4178 if (NOTE_P (head))
4180 if (head == end)
4182 head = end = NULL;
4183 goto no_body;
4185 head = NEXT_INSN (head);
4186 while (DEBUG_INSN_P (head) && head != end)
4187 head = NEXT_INSN (head);
4190 if (JUMP_P (end))
4192 if (!onlyjump_p (end))
4193 return FALSE;
4194 if (head == end)
4196 head = end = NULL;
4197 goto no_body;
4199 end = PREV_INSN (end);
4200 while (DEBUG_INSN_P (end) && end != head)
4201 end = PREV_INSN (end);
4204 /* Don't move frame-related insn across the conditional branch. This
4205 can lead to one of the paths of the branch having wrong unwind info. */
4206 if (epilogue_completed)
4208 rtx_insn *insn = head;
4209 while (1)
4211 if (INSN_P (insn) && RTX_FRAME_RELATED_P (insn))
4212 return FALSE;
4213 if (insn == end)
4214 break;
4215 insn = NEXT_INSN (insn);
4219 /* Disable handling dead code by conditional execution if the machine needs
4220 to do anything funny with the tests, etc. */
4221 #ifndef IFCVT_MODIFY_TESTS
4222 if (targetm.have_conditional_execution ())
4224 /* In the conditional execution case, we have things easy. We know
4225 the condition is reversible. We don't have to check life info
4226 because we're going to conditionally execute the code anyway.
4227 All that's left is making sure the insns involved can actually
4228 be predicated. */
4230 rtx cond;
4232 cond = cond_exec_get_condition (jump);
4233 if (! cond)
4234 return FALSE;
4236 rtx note = find_reg_note (jump, REG_BR_PROB, NULL_RTX);
4237 int prob_val = (note ? XINT (note, 0) : -1);
4239 if (reversep)
4241 enum rtx_code rev = reversed_comparison_code (cond, jump);
4242 if (rev == UNKNOWN)
4243 return FALSE;
4244 cond = gen_rtx_fmt_ee (rev, GET_MODE (cond), XEXP (cond, 0),
4245 XEXP (cond, 1));
4246 if (prob_val >= 0)
4247 prob_val = REG_BR_PROB_BASE - prob_val;
4250 if (cond_exec_process_insns (NULL, head, end, cond, prob_val, 0)
4251 && verify_changes (0))
4252 n_validated_changes = num_validated_changes ();
4253 else
4254 cancel_changes (0);
4256 earliest = jump;
4258 #endif
4260 /* If we allocated new pseudos (e.g. in the conditional move
4261 expander called from noce_emit_cmove), we must resize the
4262 array first. */
4263 if (max_regno < max_reg_num ())
4264 max_regno = max_reg_num ();
4266 /* Try the NCE path if the CE path did not result in any changes. */
4267 if (n_validated_changes == 0)
4269 rtx cond;
4270 rtx_insn *insn;
4271 regset live;
4272 bool success;
4274 /* In the non-conditional execution case, we have to verify that there
4275 are no trapping operations, no calls, no references to memory, and
4276 that any registers modified are dead at the branch site. */
4278 if (!any_condjump_p (jump))
4279 return FALSE;
4281 /* Find the extent of the conditional. */
4282 cond = noce_get_condition (jump, &earliest, false);
4283 if (!cond)
4284 return FALSE;
4286 live = BITMAP_ALLOC (&reg_obstack);
4287 simulate_backwards_to_point (merge_bb, live, end);
4288 success = can_move_insns_across (head, end, earliest, jump,
4289 merge_bb, live,
4290 df_get_live_in (other_bb), NULL);
4291 BITMAP_FREE (live);
4292 if (!success)
4293 return FALSE;
4295 /* Collect the set of registers set in MERGE_BB. */
4296 merge_set = BITMAP_ALLOC (&reg_obstack);
4298 FOR_BB_INSNS (merge_bb, insn)
4299 if (NONDEBUG_INSN_P (insn))
4300 df_simulate_find_defs (insn, merge_set);
4302 /* If shrink-wrapping, disable this optimization when test_bb is
4303 the first basic block and merge_bb exits. The idea is to not
4304 move code setting up a return register as that may clobber a
4305 register used to pass function parameters, which then must be
4306 saved in caller-saved regs. A caller-saved reg requires the
4307 prologue, killing a shrink-wrap opportunity. */
4308 if ((SHRINK_WRAPPING_ENABLED && !epilogue_completed)
4309 && ENTRY_BLOCK_PTR_FOR_FN (cfun)->next_bb == test_bb
4310 && single_succ_p (new_dest)
4311 && single_succ (new_dest) == EXIT_BLOCK_PTR_FOR_FN (cfun)
4312 && bitmap_intersect_p (df_get_live_in (new_dest), merge_set))
4314 regset return_regs;
4315 unsigned int i;
4317 return_regs = BITMAP_ALLOC (&reg_obstack);
4319 /* Start off with the intersection of regs used to pass
4320 params and regs used to return values. */
4321 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
4322 if (FUNCTION_ARG_REGNO_P (i)
4323 && targetm.calls.function_value_regno_p (i))
4324 bitmap_set_bit (return_regs, INCOMING_REGNO (i));
4326 bitmap_and_into (return_regs,
4327 df_get_live_out (ENTRY_BLOCK_PTR_FOR_FN (cfun)));
4328 bitmap_and_into (return_regs,
4329 df_get_live_in (EXIT_BLOCK_PTR_FOR_FN (cfun)));
4330 if (!bitmap_empty_p (return_regs))
4332 FOR_BB_INSNS_REVERSE (new_dest, insn)
4333 if (NONDEBUG_INSN_P (insn))
4335 df_ref def;
4337 /* If this insn sets any reg in return_regs, add all
4338 reg uses to the set of regs we're interested in. */
4339 FOR_EACH_INSN_DEF (def, insn)
4340 if (bitmap_bit_p (return_regs, DF_REF_REGNO (def)))
4342 df_simulate_uses (insn, return_regs);
4343 break;
4346 if (bitmap_intersect_p (merge_set, return_regs))
4348 BITMAP_FREE (return_regs);
4349 BITMAP_FREE (merge_set);
4350 return FALSE;
4353 BITMAP_FREE (return_regs);
4357 no_body:
4358 /* We don't want to use normal invert_jump or redirect_jump because
4359 we don't want to delete_insn called. Also, we want to do our own
4360 change group management. */
4362 old_dest = JUMP_LABEL (jump);
4363 if (other_bb != new_dest)
4365 if (!any_condjump_p (jump))
4366 goto cancel;
4368 if (JUMP_P (BB_END (dest_edge->src)))
4369 new_dest_label = JUMP_LABEL (BB_END (dest_edge->src));
4370 else if (new_dest == EXIT_BLOCK_PTR_FOR_FN (cfun))
4371 new_dest_label = ret_rtx;
4372 else
4373 new_dest_label = block_label (new_dest);
4375 if (reversep
4376 ? ! invert_jump_1 (jump, new_dest_label)
4377 : ! redirect_jump_1 (jump, new_dest_label))
4378 goto cancel;
4381 if (verify_changes (n_validated_changes))
4382 confirm_change_group ();
4383 else
4384 goto cancel;
4386 if (other_bb != new_dest)
4388 redirect_jump_2 (jump, old_dest, new_dest_label, 0, reversep);
4390 redirect_edge_succ (BRANCH_EDGE (test_bb), new_dest);
4391 if (reversep)
4393 gcov_type count, probability;
4394 count = BRANCH_EDGE (test_bb)->count;
4395 BRANCH_EDGE (test_bb)->count = FALLTHRU_EDGE (test_bb)->count;
4396 FALLTHRU_EDGE (test_bb)->count = count;
4397 probability = BRANCH_EDGE (test_bb)->probability;
4398 BRANCH_EDGE (test_bb)->probability
4399 = FALLTHRU_EDGE (test_bb)->probability;
4400 FALLTHRU_EDGE (test_bb)->probability = probability;
4401 update_br_prob_note (test_bb);
4405 /* Move the insns out of MERGE_BB to before the branch. */
4406 if (head != NULL)
4408 rtx_insn *insn;
4410 if (end == BB_END (merge_bb))
4411 BB_END (merge_bb) = PREV_INSN (head);
4413 /* PR 21767: when moving insns above a conditional branch, the REG_EQUAL
4414 notes being moved might become invalid. */
4415 insn = head;
4418 rtx note;
4420 if (! INSN_P (insn))
4421 continue;
4422 note = find_reg_note (insn, REG_EQUAL, NULL_RTX);
4423 if (! note)
4424 continue;
4425 remove_note (insn, note);
4426 } while (insn != end && (insn = NEXT_INSN (insn)));
4428 /* PR46315: when moving insns above a conditional branch, the REG_EQUAL
4429 notes referring to the registers being set might become invalid. */
4430 if (merge_set)
4432 unsigned i;
4433 bitmap_iterator bi;
4435 EXECUTE_IF_SET_IN_BITMAP (merge_set, 0, i, bi)
4436 remove_reg_equal_equiv_notes_for_regno (i);
4438 BITMAP_FREE (merge_set);
4441 reorder_insns (head, end, PREV_INSN (earliest));
4444 /* Remove the jump and edge if we can. */
4445 if (other_bb == new_dest)
4447 delete_insn (jump);
4448 remove_edge (BRANCH_EDGE (test_bb));
4449 /* ??? Can't merge blocks here, as then_bb is still in use.
4450 At minimum, the merge will get done just before bb-reorder. */
4453 return TRUE;
4455 cancel:
4456 cancel_changes (0);
4458 if (merge_set)
4459 BITMAP_FREE (merge_set);
4461 return FALSE;
4464 /* Main entry point for all if-conversion. AFTER_COMBINE is true if
4465 we are after combine pass. */
4467 static void
4468 if_convert (bool after_combine)
4470 basic_block bb;
4471 int pass;
4473 if (optimize == 1)
4475 df_live_add_problem ();
4476 df_live_set_all_dirty ();
4479 /* Record whether we are after combine pass. */
4480 ifcvt_after_combine = after_combine;
4481 num_possible_if_blocks = 0;
4482 num_updated_if_blocks = 0;
4483 num_true_changes = 0;
4485 loop_optimizer_init (AVOID_CFG_MODIFICATIONS);
4486 mark_loop_exit_edges ();
4487 loop_optimizer_finalize ();
4488 free_dominance_info (CDI_DOMINATORS);
4490 /* Compute postdominators. */
4491 calculate_dominance_info (CDI_POST_DOMINATORS);
4493 df_set_flags (DF_LR_RUN_DCE);
4495 /* Go through each of the basic blocks looking for things to convert. If we
4496 have conditional execution, we make multiple passes to allow us to handle
4497 IF-THEN{-ELSE} blocks within other IF-THEN{-ELSE} blocks. */
4498 pass = 0;
4501 df_analyze ();
4502 /* Only need to do dce on the first pass. */
4503 df_clear_flags (DF_LR_RUN_DCE);
4504 cond_exec_changed_p = FALSE;
4505 pass++;
4507 #ifdef IFCVT_MULTIPLE_DUMPS
4508 if (dump_file && pass > 1)
4509 fprintf (dump_file, "\n\n========== Pass %d ==========\n", pass);
4510 #endif
4512 FOR_EACH_BB_FN (bb, cfun)
4514 basic_block new_bb;
4515 while (!df_get_bb_dirty (bb)
4516 && (new_bb = find_if_header (bb, pass)) != NULL)
4517 bb = new_bb;
4520 #ifdef IFCVT_MULTIPLE_DUMPS
4521 if (dump_file && cond_exec_changed_p)
4522 print_rtl_with_bb (dump_file, get_insns (), dump_flags);
4523 #endif
4525 while (cond_exec_changed_p);
4527 #ifdef IFCVT_MULTIPLE_DUMPS
4528 if (dump_file)
4529 fprintf (dump_file, "\n\n========== no more changes\n");
4530 #endif
4532 free_dominance_info (CDI_POST_DOMINATORS);
4534 if (dump_file)
4535 fflush (dump_file);
4537 clear_aux_for_blocks ();
4539 /* If we allocated new pseudos, we must resize the array for sched1. */
4540 if (max_regno < max_reg_num ())
4541 max_regno = max_reg_num ();
4543 /* Write the final stats. */
4544 if (dump_file && num_possible_if_blocks > 0)
4546 fprintf (dump_file,
4547 "\n%d possible IF blocks searched.\n",
4548 num_possible_if_blocks);
4549 fprintf (dump_file,
4550 "%d IF blocks converted.\n",
4551 num_updated_if_blocks);
4552 fprintf (dump_file,
4553 "%d true changes made.\n\n\n",
4554 num_true_changes);
4557 if (optimize == 1)
4558 df_remove_problem (df_live);
4560 #ifdef ENABLE_CHECKING
4561 verify_flow_info ();
4562 #endif
4565 /* If-conversion and CFG cleanup. */
4566 static unsigned int
4567 rest_of_handle_if_conversion (void)
4569 if (flag_if_conversion)
4571 if (dump_file)
4573 dump_reg_info (dump_file);
4574 dump_flow_info (dump_file, dump_flags);
4576 cleanup_cfg (CLEANUP_EXPENSIVE);
4577 if_convert (false);
4580 cleanup_cfg (0);
4581 return 0;
4584 namespace {
4586 const pass_data pass_data_rtl_ifcvt =
4588 RTL_PASS, /* type */
4589 "ce1", /* name */
4590 OPTGROUP_NONE, /* optinfo_flags */
4591 TV_IFCVT, /* tv_id */
4592 0, /* properties_required */
4593 0, /* properties_provided */
4594 0, /* properties_destroyed */
4595 0, /* todo_flags_start */
4596 TODO_df_finish, /* todo_flags_finish */
4599 class pass_rtl_ifcvt : public rtl_opt_pass
4601 public:
4602 pass_rtl_ifcvt (gcc::context *ctxt)
4603 : rtl_opt_pass (pass_data_rtl_ifcvt, ctxt)
4606 /* opt_pass methods: */
4607 virtual bool gate (function *)
4609 return (optimize > 0) && dbg_cnt (if_conversion);
4612 virtual unsigned int execute (function *)
4614 return rest_of_handle_if_conversion ();
4617 }; // class pass_rtl_ifcvt
4619 } // anon namespace
4621 rtl_opt_pass *
4622 make_pass_rtl_ifcvt (gcc::context *ctxt)
4624 return new pass_rtl_ifcvt (ctxt);
4628 /* Rerun if-conversion, as combine may have simplified things enough
4629 to now meet sequence length restrictions. */
4631 namespace {
4633 const pass_data pass_data_if_after_combine =
4635 RTL_PASS, /* type */
4636 "ce2", /* name */
4637 OPTGROUP_NONE, /* optinfo_flags */
4638 TV_IFCVT, /* tv_id */
4639 0, /* properties_required */
4640 0, /* properties_provided */
4641 0, /* properties_destroyed */
4642 0, /* todo_flags_start */
4643 TODO_df_finish, /* todo_flags_finish */
4646 class pass_if_after_combine : public rtl_opt_pass
4648 public:
4649 pass_if_after_combine (gcc::context *ctxt)
4650 : rtl_opt_pass (pass_data_if_after_combine, ctxt)
4653 /* opt_pass methods: */
4654 virtual bool gate (function *)
4656 return optimize > 0 && flag_if_conversion
4657 && dbg_cnt (if_after_combine);
4660 virtual unsigned int execute (function *)
4662 if_convert (true);
4663 return 0;
4666 }; // class pass_if_after_combine
4668 } // anon namespace
4670 rtl_opt_pass *
4671 make_pass_if_after_combine (gcc::context *ctxt)
4673 return new pass_if_after_combine (ctxt);
4677 namespace {
4679 const pass_data pass_data_if_after_reload =
4681 RTL_PASS, /* type */
4682 "ce3", /* name */
4683 OPTGROUP_NONE, /* optinfo_flags */
4684 TV_IFCVT2, /* tv_id */
4685 0, /* properties_required */
4686 0, /* properties_provided */
4687 0, /* properties_destroyed */
4688 0, /* todo_flags_start */
4689 TODO_df_finish, /* todo_flags_finish */
4692 class pass_if_after_reload : public rtl_opt_pass
4694 public:
4695 pass_if_after_reload (gcc::context *ctxt)
4696 : rtl_opt_pass (pass_data_if_after_reload, ctxt)
4699 /* opt_pass methods: */
4700 virtual bool gate (function *)
4702 return optimize > 0 && flag_if_conversion2
4703 && dbg_cnt (if_after_reload);
4706 virtual unsigned int execute (function *)
4708 if_convert (true);
4709 return 0;
4712 }; // class pass_if_after_reload
4714 } // anon namespace
4716 rtl_opt_pass *
4717 make_pass_if_after_reload (gcc::context *ctxt)
4719 return new pass_if_after_reload (ctxt);