re PR target/66258 (compiling a stdarg function with arch +nofp generates an ICE)
[official-gcc.git] / gcc / ifcvt.c
blob37117b7e5ef5ef2af441acd888addf4feef3b90e
1 /* If-conversion support.
2 Copyright (C) 2000-2015 Free Software Foundation, Inc.
4 This file is part of GCC.
6 GCC is free software; you can redistribute it and/or modify it
7 under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 3, or (at your option)
9 any later version.
11 GCC is distributed in the hope that it will be useful, but WITHOUT
12 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
13 or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
14 License for more details.
16 You should have received a copy of the GNU General Public License
17 along with GCC; see the file COPYING3. If not see
18 <http://www.gnu.org/licenses/>. */
20 #include "config.h"
21 #include "system.h"
22 #include "coretypes.h"
23 #include "tm.h"
25 #include "rtl.h"
26 #include "regs.h"
27 #include "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 "predict.h"
39 #include "dominance.h"
40 #include "cfg.h"
41 #include "cfgrtl.h"
42 #include "cfganal.h"
43 #include "cfgcleanup.h"
44 #include "basic-block.h"
45 #include "symtab.h"
46 #include "statistics.h"
47 #include "double-int.h"
48 #include "real.h"
49 #include "fixed-value.h"
50 #include "alias.h"
51 #include "wide-int.h"
52 #include "inchash.h"
53 #include "tree.h"
54 #include "expmed.h"
55 #include "dojump.h"
56 #include "explow.h"
57 #include "calls.h"
58 #include "emit-rtl.h"
59 #include "varasm.h"
60 #include "stmt.h"
61 #include "expr.h"
62 #include "output.h"
63 #include "insn-codes.h"
64 #include "optabs.h"
65 #include "diagnostic-core.h"
66 #include "tm_p.h"
67 #include "cfgloop.h"
68 #include "target.h"
69 #include "tree-pass.h"
70 #include "df.h"
71 #include "dbgcnt.h"
72 #include "shrink-wrap.h"
73 #include "ifcvt.h"
75 #ifndef HAVE_incscc
76 #define HAVE_incscc 0
77 #endif
78 #ifndef HAVE_decscc
79 #define HAVE_decscc 0
80 #endif
81 #ifndef HAVE_trap
82 #define HAVE_trap 0
83 #endif
85 #ifndef MAX_CONDITIONAL_EXECUTE
86 #define MAX_CONDITIONAL_EXECUTE \
87 (BRANCH_COST (optimize_function_for_speed_p (cfun), false) \
88 + 1)
89 #endif
91 #ifndef HAVE_cbranchcc4
92 #define HAVE_cbranchcc4 0
93 #endif
95 #define IFCVT_MULTIPLE_DUMPS 1
97 #define NULL_BLOCK ((basic_block) NULL)
99 /* True if after combine pass. */
100 static bool ifcvt_after_combine;
102 /* # of IF-THEN or IF-THEN-ELSE blocks we looked at */
103 static int num_possible_if_blocks;
105 /* # of IF-THEN or IF-THEN-ELSE blocks were converted to conditional
106 execution. */
107 static int num_updated_if_blocks;
109 /* # of changes made. */
110 static int num_true_changes;
112 /* Whether conditional execution changes were made. */
113 static int cond_exec_changed_p;
115 /* Forward references. */
116 static int count_bb_insns (const_basic_block);
117 static bool cheap_bb_rtx_cost_p (const_basic_block, int, int);
118 static rtx_insn *first_active_insn (basic_block);
119 static rtx_insn *last_active_insn (basic_block, int);
120 static rtx_insn *find_active_insn_before (basic_block, rtx_insn *);
121 static rtx_insn *find_active_insn_after (basic_block, rtx_insn *);
122 static basic_block block_fallthru (basic_block);
123 static int cond_exec_process_insns (ce_if_block *, rtx_insn *, rtx, rtx, int,
124 int);
125 static rtx cond_exec_get_condition (rtx_insn *);
126 static rtx noce_get_condition (rtx_insn *, rtx_insn **, bool);
127 static int noce_operand_ok (const_rtx);
128 static void merge_if_block (ce_if_block *);
129 static int find_cond_trap (basic_block, edge, edge);
130 static basic_block find_if_header (basic_block, int);
131 static int block_jumps_and_fallthru_p (basic_block, basic_block);
132 static int noce_find_if_block (basic_block, edge, edge, int);
133 static int cond_exec_find_if_block (ce_if_block *);
134 static int find_if_case_1 (basic_block, edge, edge);
135 static int find_if_case_2 (basic_block, edge, edge);
136 static int dead_or_predicable (basic_block, basic_block, basic_block,
137 edge, int);
138 static void noce_emit_move_insn (rtx, rtx);
139 static rtx_insn *block_has_only_trap (basic_block);
141 /* Count the number of non-jump active insns in BB. */
143 static int
144 count_bb_insns (const_basic_block bb)
146 int count = 0;
147 rtx_insn *insn = BB_HEAD (bb);
149 while (1)
151 if (active_insn_p (insn) && !JUMP_P (insn))
152 count++;
154 if (insn == BB_END (bb))
155 break;
156 insn = NEXT_INSN (insn);
159 return count;
162 /* Determine whether the total insn_rtx_cost on non-jump insns in
163 basic block BB is less than MAX_COST. This function returns
164 false if the cost of any instruction could not be estimated.
166 The cost of the non-jump insns in BB is scaled by REG_BR_PROB_BASE
167 as those insns are being speculated. MAX_COST is scaled with SCALE
168 plus a small fudge factor. */
170 static bool
171 cheap_bb_rtx_cost_p (const_basic_block bb, int scale, int max_cost)
173 int count = 0;
174 rtx_insn *insn = BB_HEAD (bb);
175 bool speed = optimize_bb_for_speed_p (bb);
177 /* Set scale to REG_BR_PROB_BASE to void the identical scaling
178 applied to insn_rtx_cost when optimizing for size. Only do
179 this after combine because if-conversion might interfere with
180 passes before combine.
182 Use optimize_function_for_speed_p instead of the pre-defined
183 variable speed to make sure it is set to same value for all
184 basic blocks in one if-conversion transformation. */
185 if (!optimize_function_for_speed_p (cfun) && ifcvt_after_combine)
186 scale = REG_BR_PROB_BASE;
187 /* Our branch probability/scaling factors are just estimates and don't
188 account for cases where we can get speculation for free and other
189 secondary benefits. So we fudge the scale factor to make speculating
190 appear a little more profitable when optimizing for performance. */
191 else
192 scale += REG_BR_PROB_BASE / 8;
195 max_cost *= scale;
197 while (1)
199 if (NONJUMP_INSN_P (insn))
201 int cost = insn_rtx_cost (PATTERN (insn), speed) * REG_BR_PROB_BASE;
202 if (cost == 0)
203 return false;
205 /* If this instruction is the load or set of a "stack" register,
206 such as a floating point register on x87, then the cost of
207 speculatively executing this insn may need to include
208 the additional cost of popping its result off of the
209 register stack. Unfortunately, correctly recognizing and
210 accounting for this additional overhead is tricky, so for
211 now we simply prohibit such speculative execution. */
212 #ifdef STACK_REGS
214 rtx set = single_set (insn);
215 if (set && STACK_REG_P (SET_DEST (set)))
216 return false;
218 #endif
220 count += cost;
221 if (count >= max_cost)
222 return false;
224 else if (CALL_P (insn))
225 return false;
227 if (insn == BB_END (bb))
228 break;
229 insn = NEXT_INSN (insn);
232 return true;
235 /* Return the first non-jump active insn in the basic block. */
237 static rtx_insn *
238 first_active_insn (basic_block bb)
240 rtx_insn *insn = BB_HEAD (bb);
242 if (LABEL_P (insn))
244 if (insn == BB_END (bb))
245 return NULL;
246 insn = NEXT_INSN (insn);
249 while (NOTE_P (insn) || DEBUG_INSN_P (insn))
251 if (insn == BB_END (bb))
252 return NULL;
253 insn = NEXT_INSN (insn);
256 if (JUMP_P (insn))
257 return NULL;
259 return insn;
262 /* Return the last non-jump active (non-jump) insn in the basic block. */
264 static rtx_insn *
265 last_active_insn (basic_block bb, int skip_use_p)
267 rtx_insn *insn = BB_END (bb);
268 rtx_insn *head = BB_HEAD (bb);
270 while (NOTE_P (insn)
271 || JUMP_P (insn)
272 || DEBUG_INSN_P (insn)
273 || (skip_use_p
274 && NONJUMP_INSN_P (insn)
275 && GET_CODE (PATTERN (insn)) == USE))
277 if (insn == head)
278 return NULL;
279 insn = PREV_INSN (insn);
282 if (LABEL_P (insn))
283 return NULL;
285 return insn;
288 /* Return the active insn before INSN inside basic block CURR_BB. */
290 static rtx_insn *
291 find_active_insn_before (basic_block curr_bb, rtx_insn *insn)
293 if (!insn || insn == BB_HEAD (curr_bb))
294 return NULL;
296 while ((insn = PREV_INSN (insn)) != NULL_RTX)
298 if (NONJUMP_INSN_P (insn) || JUMP_P (insn) || CALL_P (insn))
299 break;
301 /* No other active insn all the way to the start of the basic block. */
302 if (insn == BB_HEAD (curr_bb))
303 return NULL;
306 return insn;
309 /* Return the active insn after INSN inside basic block CURR_BB. */
311 static rtx_insn *
312 find_active_insn_after (basic_block curr_bb, rtx_insn *insn)
314 if (!insn || insn == BB_END (curr_bb))
315 return NULL;
317 while ((insn = NEXT_INSN (insn)) != NULL_RTX)
319 if (NONJUMP_INSN_P (insn) || JUMP_P (insn) || CALL_P (insn))
320 break;
322 /* No other active insn all the way to the end of the basic block. */
323 if (insn == BB_END (curr_bb))
324 return NULL;
327 return insn;
330 /* Return the basic block reached by falling though the basic block BB. */
332 static basic_block
333 block_fallthru (basic_block bb)
335 edge e = find_fallthru_edge (bb->succs);
337 return (e) ? e->dest : NULL_BLOCK;
340 /* Return true if RTXs A and B can be safely interchanged. */
342 static bool
343 rtx_interchangeable_p (const_rtx a, const_rtx b)
345 if (!rtx_equal_p (a, b))
346 return false;
348 if (GET_CODE (a) != MEM)
349 return true;
351 /* A dead type-unsafe memory reference is legal, but a live type-unsafe memory
352 reference is not. Interchanging a dead type-unsafe memory reference with
353 a live type-safe one creates a live type-unsafe memory reference, in other
354 words, it makes the program illegal.
355 We check here conservatively whether the two memory references have equal
356 memory attributes. */
358 return mem_attrs_eq_p (get_mem_attrs (a), get_mem_attrs (b));
362 /* Go through a bunch of insns, converting them to conditional
363 execution format if possible. Return TRUE if all of the non-note
364 insns were processed. */
366 static int
367 cond_exec_process_insns (ce_if_block *ce_info ATTRIBUTE_UNUSED,
368 /* if block information */rtx_insn *start,
369 /* first insn to look at */rtx end,
370 /* last insn to look at */rtx test,
371 /* conditional execution test */int prob_val,
372 /* probability of branch taken. */int mod_ok)
374 int must_be_last = FALSE;
375 rtx_insn *insn;
376 rtx xtest;
377 rtx pattern;
379 if (!start || !end)
380 return FALSE;
382 for (insn = start; ; insn = NEXT_INSN (insn))
384 /* dwarf2out can't cope with conditional prologues. */
385 if (NOTE_P (insn) && NOTE_KIND (insn) == NOTE_INSN_PROLOGUE_END)
386 return FALSE;
388 if (NOTE_P (insn) || DEBUG_INSN_P (insn))
389 goto insn_done;
391 gcc_assert (NONJUMP_INSN_P (insn) || CALL_P (insn));
393 /* dwarf2out can't cope with conditional unwind info. */
394 if (RTX_FRAME_RELATED_P (insn))
395 return FALSE;
397 /* Remove USE insns that get in the way. */
398 if (reload_completed && GET_CODE (PATTERN (insn)) == USE)
400 /* ??? Ug. Actually unlinking the thing is problematic,
401 given what we'd have to coordinate with our callers. */
402 SET_INSN_DELETED (insn);
403 goto insn_done;
406 /* Last insn wasn't last? */
407 if (must_be_last)
408 return FALSE;
410 if (modified_in_p (test, insn))
412 if (!mod_ok)
413 return FALSE;
414 must_be_last = TRUE;
417 /* Now build the conditional form of the instruction. */
418 pattern = PATTERN (insn);
419 xtest = copy_rtx (test);
421 /* If this is already a COND_EXEC, rewrite the test to be an AND of the
422 two conditions. */
423 if (GET_CODE (pattern) == COND_EXEC)
425 if (GET_MODE (xtest) != GET_MODE (COND_EXEC_TEST (pattern)))
426 return FALSE;
428 xtest = gen_rtx_AND (GET_MODE (xtest), xtest,
429 COND_EXEC_TEST (pattern));
430 pattern = COND_EXEC_CODE (pattern);
433 pattern = gen_rtx_COND_EXEC (VOIDmode, xtest, pattern);
435 /* If the machine needs to modify the insn being conditionally executed,
436 say for example to force a constant integer operand into a temp
437 register, do so here. */
438 #ifdef IFCVT_MODIFY_INSN
439 IFCVT_MODIFY_INSN (ce_info, pattern, insn);
440 if (! pattern)
441 return FALSE;
442 #endif
444 validate_change (insn, &PATTERN (insn), pattern, 1);
446 if (CALL_P (insn) && prob_val >= 0)
447 validate_change (insn, &REG_NOTES (insn),
448 gen_rtx_INT_LIST ((machine_mode) REG_BR_PROB,
449 prob_val, REG_NOTES (insn)), 1);
451 insn_done:
452 if (insn == end)
453 break;
456 return TRUE;
459 /* Return the condition for a jump. Do not do any special processing. */
461 static rtx
462 cond_exec_get_condition (rtx_insn *jump)
464 rtx test_if, cond;
466 if (any_condjump_p (jump))
467 test_if = SET_SRC (pc_set (jump));
468 else
469 return NULL_RTX;
470 cond = XEXP (test_if, 0);
472 /* If this branches to JUMP_LABEL when the condition is false,
473 reverse the condition. */
474 if (GET_CODE (XEXP (test_if, 2)) == LABEL_REF
475 && LABEL_REF_LABEL (XEXP (test_if, 2)) == JUMP_LABEL (jump))
477 enum rtx_code rev = reversed_comparison_code (cond, jump);
478 if (rev == UNKNOWN)
479 return NULL_RTX;
481 cond = gen_rtx_fmt_ee (rev, GET_MODE (cond), XEXP (cond, 0),
482 XEXP (cond, 1));
485 return cond;
488 /* Given a simple IF-THEN or IF-THEN-ELSE block, attempt to convert it
489 to conditional execution. Return TRUE if we were successful at
490 converting the block. */
492 static int
493 cond_exec_process_if_block (ce_if_block * ce_info,
494 /* if block information */int do_multiple_p)
496 basic_block test_bb = ce_info->test_bb; /* last test block */
497 basic_block then_bb = ce_info->then_bb; /* THEN */
498 basic_block else_bb = ce_info->else_bb; /* ELSE or NULL */
499 rtx test_expr; /* expression in IF_THEN_ELSE that is tested */
500 rtx_insn *then_start; /* first insn in THEN block */
501 rtx_insn *then_end; /* last insn + 1 in THEN block */
502 rtx_insn *else_start = NULL; /* first insn in ELSE block or NULL */
503 rtx_insn *else_end = NULL; /* last insn + 1 in ELSE block */
504 int max; /* max # of insns to convert. */
505 int then_mod_ok; /* whether conditional mods are ok in THEN */
506 rtx true_expr; /* test for else block insns */
507 rtx false_expr; /* test for then block insns */
508 int true_prob_val; /* probability of else block */
509 int false_prob_val; /* probability of then block */
510 rtx_insn *then_last_head = NULL; /* Last match at the head of THEN */
511 rtx_insn *else_last_head = NULL; /* Last match at the head of ELSE */
512 rtx_insn *then_first_tail = NULL; /* First match at the tail of THEN */
513 rtx_insn *else_first_tail = NULL; /* First match at the tail of ELSE */
514 int then_n_insns, else_n_insns, n_insns;
515 enum rtx_code false_code;
516 rtx note;
518 /* If test is comprised of && or || elements, and we've failed at handling
519 all of them together, just use the last test if it is the special case of
520 && elements without an ELSE block. */
521 if (!do_multiple_p && ce_info->num_multiple_test_blocks)
523 if (else_bb || ! ce_info->and_and_p)
524 return FALSE;
526 ce_info->test_bb = test_bb = ce_info->last_test_bb;
527 ce_info->num_multiple_test_blocks = 0;
528 ce_info->num_and_and_blocks = 0;
529 ce_info->num_or_or_blocks = 0;
532 /* Find the conditional jump to the ELSE or JOIN part, and isolate
533 the test. */
534 test_expr = cond_exec_get_condition (BB_END (test_bb));
535 if (! test_expr)
536 return FALSE;
538 /* If the conditional jump is more than just a conditional jump,
539 then we can not do conditional execution conversion on this block. */
540 if (! onlyjump_p (BB_END (test_bb)))
541 return FALSE;
543 /* Collect the bounds of where we're to search, skipping any labels, jumps
544 and notes at the beginning and end of the block. Then count the total
545 number of insns and see if it is small enough to convert. */
546 then_start = first_active_insn (then_bb);
547 then_end = last_active_insn (then_bb, TRUE);
548 then_n_insns = ce_info->num_then_insns = count_bb_insns (then_bb);
549 n_insns = then_n_insns;
550 max = MAX_CONDITIONAL_EXECUTE;
552 if (else_bb)
554 int n_matching;
556 max *= 2;
557 else_start = first_active_insn (else_bb);
558 else_end = last_active_insn (else_bb, TRUE);
559 else_n_insns = ce_info->num_else_insns = count_bb_insns (else_bb);
560 n_insns += else_n_insns;
562 /* Look for matching sequences at the head and tail of the two blocks,
563 and limit the range of insns to be converted if possible. */
564 n_matching = flow_find_cross_jump (then_bb, else_bb,
565 &then_first_tail, &else_first_tail,
566 NULL);
567 if (then_first_tail == BB_HEAD (then_bb))
568 then_start = then_end = NULL;
569 if (else_first_tail == BB_HEAD (else_bb))
570 else_start = else_end = NULL;
572 if (n_matching > 0)
574 if (then_end)
575 then_end = find_active_insn_before (then_bb, then_first_tail);
576 if (else_end)
577 else_end = find_active_insn_before (else_bb, else_first_tail);
578 n_insns -= 2 * n_matching;
581 if (then_start
582 && else_start
583 && then_n_insns > n_matching
584 && else_n_insns > n_matching)
586 int longest_match = MIN (then_n_insns - n_matching,
587 else_n_insns - n_matching);
588 n_matching
589 = flow_find_head_matching_sequence (then_bb, else_bb,
590 &then_last_head,
591 &else_last_head,
592 longest_match);
594 if (n_matching > 0)
596 rtx_insn *insn;
598 /* We won't pass the insns in the head sequence to
599 cond_exec_process_insns, so we need to test them here
600 to make sure that they don't clobber the condition. */
601 for (insn = BB_HEAD (then_bb);
602 insn != NEXT_INSN (then_last_head);
603 insn = NEXT_INSN (insn))
604 if (!LABEL_P (insn) && !NOTE_P (insn)
605 && !DEBUG_INSN_P (insn)
606 && modified_in_p (test_expr, insn))
607 return FALSE;
610 if (then_last_head == then_end)
611 then_start = then_end = NULL;
612 if (else_last_head == else_end)
613 else_start = else_end = NULL;
615 if (n_matching > 0)
617 if (then_start)
618 then_start = find_active_insn_after (then_bb, then_last_head);
619 if (else_start)
620 else_start = find_active_insn_after (else_bb, else_last_head);
621 n_insns -= 2 * n_matching;
626 if (n_insns > max)
627 return FALSE;
629 /* Map test_expr/test_jump into the appropriate MD tests to use on
630 the conditionally executed code. */
632 true_expr = test_expr;
634 false_code = reversed_comparison_code (true_expr, BB_END (test_bb));
635 if (false_code != UNKNOWN)
636 false_expr = gen_rtx_fmt_ee (false_code, GET_MODE (true_expr),
637 XEXP (true_expr, 0), XEXP (true_expr, 1));
638 else
639 false_expr = NULL_RTX;
641 #ifdef IFCVT_MODIFY_TESTS
642 /* If the machine description needs to modify the tests, such as setting a
643 conditional execution register from a comparison, it can do so here. */
644 IFCVT_MODIFY_TESTS (ce_info, true_expr, false_expr);
646 /* See if the conversion failed. */
647 if (!true_expr || !false_expr)
648 goto fail;
649 #endif
651 note = find_reg_note (BB_END (test_bb), REG_BR_PROB, NULL_RTX);
652 if (note)
654 true_prob_val = XINT (note, 0);
655 false_prob_val = REG_BR_PROB_BASE - true_prob_val;
657 else
659 true_prob_val = -1;
660 false_prob_val = -1;
663 /* If we have && or || tests, do them here. These tests are in the adjacent
664 blocks after the first block containing the test. */
665 if (ce_info->num_multiple_test_blocks > 0)
667 basic_block bb = test_bb;
668 basic_block last_test_bb = ce_info->last_test_bb;
670 if (! false_expr)
671 goto fail;
675 rtx_insn *start, *end;
676 rtx t, f;
677 enum rtx_code f_code;
679 bb = block_fallthru (bb);
680 start = first_active_insn (bb);
681 end = last_active_insn (bb, TRUE);
682 if (start
683 && ! cond_exec_process_insns (ce_info, start, end, false_expr,
684 false_prob_val, FALSE))
685 goto fail;
687 /* If the conditional jump is more than just a conditional jump, then
688 we can not do conditional execution conversion on this block. */
689 if (! onlyjump_p (BB_END (bb)))
690 goto fail;
692 /* Find the conditional jump and isolate the test. */
693 t = cond_exec_get_condition (BB_END (bb));
694 if (! t)
695 goto fail;
697 f_code = reversed_comparison_code (t, BB_END (bb));
698 if (f_code == UNKNOWN)
699 goto fail;
701 f = gen_rtx_fmt_ee (f_code, GET_MODE (t), XEXP (t, 0), XEXP (t, 1));
702 if (ce_info->and_and_p)
704 t = gen_rtx_AND (GET_MODE (t), true_expr, t);
705 f = gen_rtx_IOR (GET_MODE (t), false_expr, f);
707 else
709 t = gen_rtx_IOR (GET_MODE (t), true_expr, t);
710 f = gen_rtx_AND (GET_MODE (t), false_expr, f);
713 /* If the machine description needs to modify the tests, such as
714 setting a conditional execution register from a comparison, it can
715 do so here. */
716 #ifdef IFCVT_MODIFY_MULTIPLE_TESTS
717 IFCVT_MODIFY_MULTIPLE_TESTS (ce_info, bb, t, f);
719 /* See if the conversion failed. */
720 if (!t || !f)
721 goto fail;
722 #endif
724 true_expr = t;
725 false_expr = f;
727 while (bb != last_test_bb);
730 /* For IF-THEN-ELSE blocks, we don't allow modifications of the test
731 on then THEN block. */
732 then_mod_ok = (else_bb == NULL_BLOCK);
734 /* Go through the THEN and ELSE blocks converting the insns if possible
735 to conditional execution. */
737 if (then_end
738 && (! false_expr
739 || ! cond_exec_process_insns (ce_info, then_start, then_end,
740 false_expr, false_prob_val,
741 then_mod_ok)))
742 goto fail;
744 if (else_bb && else_end
745 && ! cond_exec_process_insns (ce_info, else_start, else_end,
746 true_expr, true_prob_val, TRUE))
747 goto fail;
749 /* If we cannot apply the changes, fail. Do not go through the normal fail
750 processing, since apply_change_group will call cancel_changes. */
751 if (! apply_change_group ())
753 #ifdef IFCVT_MODIFY_CANCEL
754 /* Cancel any machine dependent changes. */
755 IFCVT_MODIFY_CANCEL (ce_info);
756 #endif
757 return FALSE;
760 #ifdef IFCVT_MODIFY_FINAL
761 /* Do any machine dependent final modifications. */
762 IFCVT_MODIFY_FINAL (ce_info);
763 #endif
765 /* Conversion succeeded. */
766 if (dump_file)
767 fprintf (dump_file, "%d insn%s converted to conditional execution.\n",
768 n_insns, (n_insns == 1) ? " was" : "s were");
770 /* Merge the blocks! If we had matching sequences, make sure to delete one
771 copy at the appropriate location first: delete the copy in the THEN branch
772 for a tail sequence so that the remaining one is executed last for both
773 branches, and delete the copy in the ELSE branch for a head sequence so
774 that the remaining one is executed first for both branches. */
775 if (then_first_tail)
777 rtx_insn *from = then_first_tail;
778 if (!INSN_P (from))
779 from = find_active_insn_after (then_bb, from);
780 delete_insn_chain (from, BB_END (then_bb), false);
782 if (else_last_head)
783 delete_insn_chain (first_active_insn (else_bb), else_last_head, false);
785 merge_if_block (ce_info);
786 cond_exec_changed_p = TRUE;
787 return TRUE;
789 fail:
790 #ifdef IFCVT_MODIFY_CANCEL
791 /* Cancel any machine dependent changes. */
792 IFCVT_MODIFY_CANCEL (ce_info);
793 #endif
795 cancel_changes (0);
796 return FALSE;
799 /* Used by noce_process_if_block to communicate with its subroutines.
801 The subroutines know that A and B may be evaluated freely. They
802 know that X is a register. They should insert new instructions
803 before cond_earliest. */
805 struct noce_if_info
807 /* The basic blocks that make up the IF-THEN-{ELSE-,}JOIN block. */
808 basic_block test_bb, then_bb, else_bb, join_bb;
810 /* The jump that ends TEST_BB. */
811 rtx_insn *jump;
813 /* The jump condition. */
814 rtx cond;
816 /* New insns should be inserted before this one. */
817 rtx_insn *cond_earliest;
819 /* Insns in the THEN and ELSE block. There is always just this
820 one insns in those blocks. The insns are single_set insns.
821 If there was no ELSE block, INSN_B is the last insn before
822 COND_EARLIEST, or NULL_RTX. In the former case, the insn
823 operands are still valid, as if INSN_B was moved down below
824 the jump. */
825 rtx_insn *insn_a, *insn_b;
827 /* The SET_SRC of INSN_A and INSN_B. */
828 rtx a, b;
830 /* The SET_DEST of INSN_A. */
831 rtx x;
833 /* True if this if block is not canonical. In the canonical form of
834 if blocks, the THEN_BB is the block reached via the fallthru edge
835 from TEST_BB. For the noce transformations, we allow the symmetric
836 form as well. */
837 bool then_else_reversed;
839 /* Estimated cost of the particular branch instruction. */
840 int branch_cost;
843 static rtx noce_emit_store_flag (struct noce_if_info *, rtx, int, int);
844 static int noce_try_move (struct noce_if_info *);
845 static int noce_try_store_flag (struct noce_if_info *);
846 static int noce_try_addcc (struct noce_if_info *);
847 static int noce_try_store_flag_constants (struct noce_if_info *);
848 static int noce_try_store_flag_mask (struct noce_if_info *);
849 static rtx noce_emit_cmove (struct noce_if_info *, rtx, enum rtx_code, rtx,
850 rtx, rtx, rtx);
851 static int noce_try_cmove (struct noce_if_info *);
852 static int noce_try_cmove_arith (struct noce_if_info *);
853 static rtx noce_get_alt_condition (struct noce_if_info *, rtx, rtx_insn **);
854 static int noce_try_minmax (struct noce_if_info *);
855 static int noce_try_abs (struct noce_if_info *);
856 static int noce_try_sign_mask (struct noce_if_info *);
858 /* Helper function for noce_try_store_flag*. */
860 static rtx
861 noce_emit_store_flag (struct noce_if_info *if_info, rtx x, int reversep,
862 int normalize)
864 rtx cond = if_info->cond;
865 int cond_complex;
866 enum rtx_code code;
868 cond_complex = (! general_operand (XEXP (cond, 0), VOIDmode)
869 || ! general_operand (XEXP (cond, 1), VOIDmode));
871 /* If earliest == jump, or when the condition is complex, try to
872 build the store_flag insn directly. */
874 if (cond_complex)
876 rtx set = pc_set (if_info->jump);
877 cond = XEXP (SET_SRC (set), 0);
878 if (GET_CODE (XEXP (SET_SRC (set), 2)) == LABEL_REF
879 && LABEL_REF_LABEL (XEXP (SET_SRC (set), 2)) == JUMP_LABEL (if_info->jump))
880 reversep = !reversep;
881 if (if_info->then_else_reversed)
882 reversep = !reversep;
885 if (reversep)
886 code = reversed_comparison_code (cond, if_info->jump);
887 else
888 code = GET_CODE (cond);
890 if ((if_info->cond_earliest == if_info->jump || cond_complex)
891 && (normalize == 0 || STORE_FLAG_VALUE == normalize))
893 rtx src = gen_rtx_fmt_ee (code, GET_MODE (x), XEXP (cond, 0),
894 XEXP (cond, 1));
895 rtx set = gen_rtx_SET (x, src);
897 start_sequence ();
898 rtx_insn *insn = emit_insn (set);
900 if (recog_memoized (insn) >= 0)
902 rtx_insn *seq = get_insns ();
903 end_sequence ();
904 emit_insn (seq);
906 if_info->cond_earliest = if_info->jump;
908 return x;
911 end_sequence ();
914 /* Don't even try if the comparison operands or the mode of X are weird. */
915 if (cond_complex || !SCALAR_INT_MODE_P (GET_MODE (x)))
916 return NULL_RTX;
918 return emit_store_flag (x, code, XEXP (cond, 0),
919 XEXP (cond, 1), VOIDmode,
920 (code == LTU || code == LEU
921 || code == GEU || code == GTU), normalize);
924 /* Emit instruction to move an rtx, possibly into STRICT_LOW_PART.
925 X is the destination/target and Y is the value to copy. */
927 static void
928 noce_emit_move_insn (rtx x, rtx y)
930 machine_mode outmode;
931 rtx outer, inner;
932 int bitpos;
934 if (GET_CODE (x) != STRICT_LOW_PART)
936 rtx_insn *seq, *insn;
937 rtx target;
938 optab ot;
940 start_sequence ();
941 /* Check that the SET_SRC is reasonable before calling emit_move_insn,
942 otherwise construct a suitable SET pattern ourselves. */
943 insn = (OBJECT_P (y) || CONSTANT_P (y) || GET_CODE (y) == SUBREG)
944 ? emit_move_insn (x, y)
945 : emit_insn (gen_rtx_SET (x, y));
946 seq = get_insns ();
947 end_sequence ();
949 if (recog_memoized (insn) <= 0)
951 if (GET_CODE (x) == ZERO_EXTRACT)
953 rtx op = XEXP (x, 0);
954 unsigned HOST_WIDE_INT size = INTVAL (XEXP (x, 1));
955 unsigned HOST_WIDE_INT start = INTVAL (XEXP (x, 2));
957 /* store_bit_field expects START to be relative to
958 BYTES_BIG_ENDIAN and adjusts this value for machines with
959 BITS_BIG_ENDIAN != BYTES_BIG_ENDIAN. In order to be able to
960 invoke store_bit_field again it is necessary to have the START
961 value from the first call. */
962 if (BITS_BIG_ENDIAN != BYTES_BIG_ENDIAN)
964 if (MEM_P (op))
965 start = BITS_PER_UNIT - start - size;
966 else
968 gcc_assert (REG_P (op));
969 start = BITS_PER_WORD - start - size;
973 gcc_assert (start < (MEM_P (op) ? BITS_PER_UNIT : BITS_PER_WORD));
974 store_bit_field (op, size, start, 0, 0, GET_MODE (x), y);
975 return;
978 switch (GET_RTX_CLASS (GET_CODE (y)))
980 case RTX_UNARY:
981 ot = code_to_optab (GET_CODE (y));
982 if (ot)
984 start_sequence ();
985 target = expand_unop (GET_MODE (y), ot, XEXP (y, 0), x, 0);
986 if (target != NULL_RTX)
988 if (target != x)
989 emit_move_insn (x, target);
990 seq = get_insns ();
992 end_sequence ();
994 break;
996 case RTX_BIN_ARITH:
997 case RTX_COMM_ARITH:
998 ot = code_to_optab (GET_CODE (y));
999 if (ot)
1001 start_sequence ();
1002 target = expand_binop (GET_MODE (y), ot,
1003 XEXP (y, 0), XEXP (y, 1),
1004 x, 0, OPTAB_DIRECT);
1005 if (target != NULL_RTX)
1007 if (target != x)
1008 emit_move_insn (x, target);
1009 seq = get_insns ();
1011 end_sequence ();
1013 break;
1015 default:
1016 break;
1020 emit_insn (seq);
1021 return;
1024 outer = XEXP (x, 0);
1025 inner = XEXP (outer, 0);
1026 outmode = GET_MODE (outer);
1027 bitpos = SUBREG_BYTE (outer) * BITS_PER_UNIT;
1028 store_bit_field (inner, GET_MODE_BITSIZE (outmode), bitpos,
1029 0, 0, outmode, y);
1032 /* Return the CC reg if it is used in COND. */
1034 static rtx
1035 cc_in_cond (rtx cond)
1037 if (HAVE_cbranchcc4 && cond
1038 && GET_MODE_CLASS (GET_MODE (XEXP (cond, 0))) == MODE_CC)
1039 return XEXP (cond, 0);
1041 return NULL_RTX;
1044 /* Return sequence of instructions generated by if conversion. This
1045 function calls end_sequence() to end the current stream, ensures
1046 that are instructions are unshared, recognizable non-jump insns.
1047 On failure, this function returns a NULL_RTX. */
1049 static rtx_insn *
1050 end_ifcvt_sequence (struct noce_if_info *if_info)
1052 rtx_insn *insn;
1053 rtx_insn *seq = get_insns ();
1054 rtx cc = cc_in_cond (if_info->cond);
1056 set_used_flags (if_info->x);
1057 set_used_flags (if_info->cond);
1058 set_used_flags (if_info->a);
1059 set_used_flags (if_info->b);
1060 unshare_all_rtl_in_chain (seq);
1061 end_sequence ();
1063 /* Make sure that all of the instructions emitted are recognizable,
1064 and that we haven't introduced a new jump instruction.
1065 As an exercise for the reader, build a general mechanism that
1066 allows proper placement of required clobbers. */
1067 for (insn = seq; insn; insn = NEXT_INSN (insn))
1068 if (JUMP_P (insn)
1069 || recog_memoized (insn) == -1
1070 /* Make sure new generated code does not clobber CC. */
1071 || (cc && set_of (cc, insn)))
1072 return NULL;
1074 return seq;
1077 /* Convert "if (a != b) x = a; else x = b" into "x = a" and
1078 "if (a == b) x = a; else x = b" into "x = b". */
1080 static int
1081 noce_try_move (struct noce_if_info *if_info)
1083 rtx cond = if_info->cond;
1084 enum rtx_code code = GET_CODE (cond);
1085 rtx y;
1086 rtx_insn *seq;
1088 if (code != NE && code != EQ)
1089 return FALSE;
1091 /* This optimization isn't valid if either A or B could be a NaN
1092 or a signed zero. */
1093 if (HONOR_NANS (if_info->x)
1094 || HONOR_SIGNED_ZEROS (if_info->x))
1095 return FALSE;
1097 /* Check whether the operands of the comparison are A and in
1098 either order. */
1099 if ((rtx_equal_p (if_info->a, XEXP (cond, 0))
1100 && rtx_equal_p (if_info->b, XEXP (cond, 1)))
1101 || (rtx_equal_p (if_info->a, XEXP (cond, 1))
1102 && rtx_equal_p (if_info->b, XEXP (cond, 0))))
1104 if (!rtx_interchangeable_p (if_info->a, if_info->b))
1105 return FALSE;
1107 y = (code == EQ) ? if_info->a : if_info->b;
1109 /* Avoid generating the move if the source is the destination. */
1110 if (! rtx_equal_p (if_info->x, y))
1112 start_sequence ();
1113 noce_emit_move_insn (if_info->x, y);
1114 seq = end_ifcvt_sequence (if_info);
1115 if (!seq)
1116 return FALSE;
1118 emit_insn_before_setloc (seq, if_info->jump,
1119 INSN_LOCATION (if_info->insn_a));
1121 return TRUE;
1123 return FALSE;
1126 /* Convert "if (test) x = 1; else x = 0".
1128 Only try 0 and STORE_FLAG_VALUE here. Other combinations will be
1129 tried in noce_try_store_flag_constants after noce_try_cmove has had
1130 a go at the conversion. */
1132 static int
1133 noce_try_store_flag (struct noce_if_info *if_info)
1135 int reversep;
1136 rtx target;
1137 rtx_insn *seq;
1139 if (CONST_INT_P (if_info->b)
1140 && INTVAL (if_info->b) == STORE_FLAG_VALUE
1141 && if_info->a == const0_rtx)
1142 reversep = 0;
1143 else if (if_info->b == const0_rtx
1144 && CONST_INT_P (if_info->a)
1145 && INTVAL (if_info->a) == STORE_FLAG_VALUE
1146 && (reversed_comparison_code (if_info->cond, if_info->jump)
1147 != UNKNOWN))
1148 reversep = 1;
1149 else
1150 return FALSE;
1152 start_sequence ();
1154 target = noce_emit_store_flag (if_info, if_info->x, reversep, 0);
1155 if (target)
1157 if (target != if_info->x)
1158 noce_emit_move_insn (if_info->x, target);
1160 seq = end_ifcvt_sequence (if_info);
1161 if (! seq)
1162 return FALSE;
1164 emit_insn_before_setloc (seq, if_info->jump,
1165 INSN_LOCATION (if_info->insn_a));
1166 return TRUE;
1168 else
1170 end_sequence ();
1171 return FALSE;
1175 /* Convert "if (test) x = a; else x = b", for A and B constant. */
1177 static int
1178 noce_try_store_flag_constants (struct noce_if_info *if_info)
1180 rtx target;
1181 rtx_insn *seq;
1182 int reversep;
1183 HOST_WIDE_INT itrue, ifalse, diff, tmp;
1184 int normalize, can_reverse;
1185 machine_mode mode;
1187 if (CONST_INT_P (if_info->a)
1188 && CONST_INT_P (if_info->b))
1190 mode = GET_MODE (if_info->x);
1191 ifalse = INTVAL (if_info->a);
1192 itrue = INTVAL (if_info->b);
1194 diff = (unsigned HOST_WIDE_INT) itrue - ifalse;
1195 /* Make sure we can represent the difference between the two values. */
1196 if ((diff > 0)
1197 != ((ifalse < 0) != (itrue < 0) ? ifalse < 0 : ifalse < itrue))
1198 return FALSE;
1200 diff = trunc_int_for_mode (diff, mode);
1202 can_reverse = (reversed_comparison_code (if_info->cond, if_info->jump)
1203 != UNKNOWN);
1205 reversep = 0;
1206 if (diff == STORE_FLAG_VALUE || diff == -STORE_FLAG_VALUE)
1207 normalize = 0;
1208 else if (ifalse == 0 && exact_log2 (itrue) >= 0
1209 && (STORE_FLAG_VALUE == 1
1210 || if_info->branch_cost >= 2))
1211 normalize = 1;
1212 else if (itrue == 0 && exact_log2 (ifalse) >= 0 && can_reverse
1213 && (STORE_FLAG_VALUE == 1 || if_info->branch_cost >= 2))
1214 normalize = 1, reversep = 1;
1215 else if (itrue == -1
1216 && (STORE_FLAG_VALUE == -1
1217 || if_info->branch_cost >= 2))
1218 normalize = -1;
1219 else if (ifalse == -1 && can_reverse
1220 && (STORE_FLAG_VALUE == -1 || if_info->branch_cost >= 2))
1221 normalize = -1, reversep = 1;
1222 else if ((if_info->branch_cost >= 2 && STORE_FLAG_VALUE == -1)
1223 || if_info->branch_cost >= 3)
1224 normalize = -1;
1225 else
1226 return FALSE;
1228 if (reversep)
1230 tmp = itrue; itrue = ifalse; ifalse = tmp;
1231 diff = trunc_int_for_mode (-(unsigned HOST_WIDE_INT) diff, mode);
1234 start_sequence ();
1235 target = noce_emit_store_flag (if_info, if_info->x, reversep, normalize);
1236 if (! target)
1238 end_sequence ();
1239 return FALSE;
1242 /* if (test) x = 3; else x = 4;
1243 => x = 3 + (test == 0); */
1244 if (diff == STORE_FLAG_VALUE || diff == -STORE_FLAG_VALUE)
1246 target = expand_simple_binop (mode,
1247 (diff == STORE_FLAG_VALUE
1248 ? PLUS : MINUS),
1249 gen_int_mode (ifalse, mode), target,
1250 if_info->x, 0, OPTAB_WIDEN);
1253 /* if (test) x = 8; else x = 0;
1254 => x = (test != 0) << 3; */
1255 else if (ifalse == 0 && (tmp = exact_log2 (itrue)) >= 0)
1257 target = expand_simple_binop (mode, ASHIFT,
1258 target, GEN_INT (tmp), if_info->x, 0,
1259 OPTAB_WIDEN);
1262 /* if (test) x = -1; else x = b;
1263 => x = -(test != 0) | b; */
1264 else if (itrue == -1)
1266 target = expand_simple_binop (mode, IOR,
1267 target, gen_int_mode (ifalse, mode),
1268 if_info->x, 0, OPTAB_WIDEN);
1271 /* if (test) x = a; else x = b;
1272 => x = (-(test != 0) & (b - a)) + a; */
1273 else
1275 target = expand_simple_binop (mode, AND,
1276 target, gen_int_mode (diff, mode),
1277 if_info->x, 0, OPTAB_WIDEN);
1278 if (target)
1279 target = expand_simple_binop (mode, PLUS,
1280 target, gen_int_mode (ifalse, mode),
1281 if_info->x, 0, OPTAB_WIDEN);
1284 if (! target)
1286 end_sequence ();
1287 return FALSE;
1290 if (target != if_info->x)
1291 noce_emit_move_insn (if_info->x, target);
1293 seq = end_ifcvt_sequence (if_info);
1294 if (!seq)
1295 return FALSE;
1297 emit_insn_before_setloc (seq, if_info->jump,
1298 INSN_LOCATION (if_info->insn_a));
1299 return TRUE;
1302 return FALSE;
1305 /* Convert "if (test) foo++" into "foo += (test != 0)", and
1306 similarly for "foo--". */
1308 static int
1309 noce_try_addcc (struct noce_if_info *if_info)
1311 rtx target;
1312 rtx_insn *seq;
1313 int subtract, normalize;
1315 if (GET_CODE (if_info->a) == PLUS
1316 && rtx_equal_p (XEXP (if_info->a, 0), if_info->b)
1317 && (reversed_comparison_code (if_info->cond, if_info->jump)
1318 != UNKNOWN))
1320 rtx cond = if_info->cond;
1321 enum rtx_code code = reversed_comparison_code (cond, if_info->jump);
1323 /* First try to use addcc pattern. */
1324 if (general_operand (XEXP (cond, 0), VOIDmode)
1325 && general_operand (XEXP (cond, 1), VOIDmode))
1327 start_sequence ();
1328 target = emit_conditional_add (if_info->x, code,
1329 XEXP (cond, 0),
1330 XEXP (cond, 1),
1331 VOIDmode,
1332 if_info->b,
1333 XEXP (if_info->a, 1),
1334 GET_MODE (if_info->x),
1335 (code == LTU || code == GEU
1336 || code == LEU || code == GTU));
1337 if (target)
1339 if (target != if_info->x)
1340 noce_emit_move_insn (if_info->x, target);
1342 seq = end_ifcvt_sequence (if_info);
1343 if (!seq)
1344 return FALSE;
1346 emit_insn_before_setloc (seq, if_info->jump,
1347 INSN_LOCATION (if_info->insn_a));
1348 return TRUE;
1350 end_sequence ();
1353 /* If that fails, construct conditional increment or decrement using
1354 setcc. */
1355 if (if_info->branch_cost >= 2
1356 && (XEXP (if_info->a, 1) == const1_rtx
1357 || XEXP (if_info->a, 1) == constm1_rtx))
1359 start_sequence ();
1360 if (STORE_FLAG_VALUE == INTVAL (XEXP (if_info->a, 1)))
1361 subtract = 0, normalize = 0;
1362 else if (-STORE_FLAG_VALUE == INTVAL (XEXP (if_info->a, 1)))
1363 subtract = 1, normalize = 0;
1364 else
1365 subtract = 0, normalize = INTVAL (XEXP (if_info->a, 1));
1368 target = noce_emit_store_flag (if_info,
1369 gen_reg_rtx (GET_MODE (if_info->x)),
1370 1, normalize);
1372 if (target)
1373 target = expand_simple_binop (GET_MODE (if_info->x),
1374 subtract ? MINUS : PLUS,
1375 if_info->b, target, if_info->x,
1376 0, OPTAB_WIDEN);
1377 if (target)
1379 if (target != if_info->x)
1380 noce_emit_move_insn (if_info->x, target);
1382 seq = end_ifcvt_sequence (if_info);
1383 if (!seq)
1384 return FALSE;
1386 emit_insn_before_setloc (seq, if_info->jump,
1387 INSN_LOCATION (if_info->insn_a));
1388 return TRUE;
1390 end_sequence ();
1394 return FALSE;
1397 /* Convert "if (test) x = 0;" to "x &= -(test == 0);" */
1399 static int
1400 noce_try_store_flag_mask (struct noce_if_info *if_info)
1402 rtx target;
1403 rtx_insn *seq;
1404 int reversep;
1406 reversep = 0;
1407 if ((if_info->branch_cost >= 2
1408 || STORE_FLAG_VALUE == -1)
1409 && ((if_info->a == const0_rtx
1410 && rtx_equal_p (if_info->b, if_info->x))
1411 || ((reversep = (reversed_comparison_code (if_info->cond,
1412 if_info->jump)
1413 != UNKNOWN))
1414 && if_info->b == const0_rtx
1415 && rtx_equal_p (if_info->a, if_info->x))))
1417 start_sequence ();
1418 target = noce_emit_store_flag (if_info,
1419 gen_reg_rtx (GET_MODE (if_info->x)),
1420 reversep, -1);
1421 if (target)
1422 target = expand_simple_binop (GET_MODE (if_info->x), AND,
1423 if_info->x,
1424 target, if_info->x, 0,
1425 OPTAB_WIDEN);
1427 if (target)
1429 int old_cost, new_cost, insn_cost;
1430 int speed_p;
1432 if (target != if_info->x)
1433 noce_emit_move_insn (if_info->x, target);
1435 seq = end_ifcvt_sequence (if_info);
1436 if (!seq)
1437 return FALSE;
1439 speed_p = optimize_bb_for_speed_p (BLOCK_FOR_INSN (if_info->insn_a));
1440 insn_cost = insn_rtx_cost (PATTERN (if_info->insn_a), speed_p);
1441 old_cost = COSTS_N_INSNS (if_info->branch_cost) + insn_cost;
1442 new_cost = seq_cost (seq, speed_p);
1444 if (new_cost > old_cost)
1445 return FALSE;
1447 emit_insn_before_setloc (seq, if_info->jump,
1448 INSN_LOCATION (if_info->insn_a));
1449 return TRUE;
1452 end_sequence ();
1455 return FALSE;
1458 /* Helper function for noce_try_cmove and noce_try_cmove_arith. */
1460 static rtx
1461 noce_emit_cmove (struct noce_if_info *if_info, rtx x, enum rtx_code code,
1462 rtx cmp_a, rtx cmp_b, rtx vfalse, rtx vtrue)
1464 rtx target ATTRIBUTE_UNUSED;
1465 int unsignedp ATTRIBUTE_UNUSED;
1467 /* If earliest == jump, try to build the cmove insn directly.
1468 This is helpful when combine has created some complex condition
1469 (like for alpha's cmovlbs) that we can't hope to regenerate
1470 through the normal interface. */
1472 if (if_info->cond_earliest == if_info->jump)
1474 rtx cond = gen_rtx_fmt_ee (code, GET_MODE (if_info->cond), cmp_a, cmp_b);
1475 rtx if_then_else = gen_rtx_IF_THEN_ELSE (GET_MODE (x),
1476 cond, vtrue, vfalse);
1477 rtx set = gen_rtx_SET (x, if_then_else);
1479 start_sequence ();
1480 rtx_insn *insn = emit_insn (set);
1482 if (recog_memoized (insn) >= 0)
1484 rtx_insn *seq = get_insns ();
1485 end_sequence ();
1486 emit_insn (seq);
1488 return x;
1491 end_sequence ();
1494 /* Don't even try if the comparison operands are weird
1495 except that the target supports cbranchcc4. */
1496 if (! general_operand (cmp_a, GET_MODE (cmp_a))
1497 || ! general_operand (cmp_b, GET_MODE (cmp_b)))
1499 if (!(HAVE_cbranchcc4)
1500 || GET_MODE_CLASS (GET_MODE (cmp_a)) != MODE_CC
1501 || cmp_b != const0_rtx)
1502 return NULL_RTX;
1505 unsignedp = (code == LTU || code == GEU
1506 || code == LEU || code == GTU);
1508 target = emit_conditional_move (x, code, cmp_a, cmp_b, VOIDmode,
1509 vtrue, vfalse, GET_MODE (x),
1510 unsignedp);
1511 if (target)
1512 return target;
1514 /* We might be faced with a situation like:
1516 x = (reg:M TARGET)
1517 vtrue = (subreg:M (reg:N VTRUE) BYTE)
1518 vfalse = (subreg:M (reg:N VFALSE) BYTE)
1520 We can't do a conditional move in mode M, but it's possible that we
1521 could do a conditional move in mode N instead and take a subreg of
1522 the result.
1524 If we can't create new pseudos, though, don't bother. */
1525 if (reload_completed)
1526 return NULL_RTX;
1528 if (GET_CODE (vtrue) == SUBREG && GET_CODE (vfalse) == SUBREG)
1530 rtx reg_vtrue = SUBREG_REG (vtrue);
1531 rtx reg_vfalse = SUBREG_REG (vfalse);
1532 unsigned int byte_vtrue = SUBREG_BYTE (vtrue);
1533 unsigned int byte_vfalse = SUBREG_BYTE (vfalse);
1534 rtx promoted_target;
1536 if (GET_MODE (reg_vtrue) != GET_MODE (reg_vfalse)
1537 || byte_vtrue != byte_vfalse
1538 || (SUBREG_PROMOTED_VAR_P (vtrue)
1539 != SUBREG_PROMOTED_VAR_P (vfalse))
1540 || (SUBREG_PROMOTED_GET (vtrue)
1541 != SUBREG_PROMOTED_GET (vfalse)))
1542 return NULL_RTX;
1544 promoted_target = gen_reg_rtx (GET_MODE (reg_vtrue));
1546 target = emit_conditional_move (promoted_target, code, cmp_a, cmp_b,
1547 VOIDmode, reg_vtrue, reg_vfalse,
1548 GET_MODE (reg_vtrue), unsignedp);
1549 /* Nope, couldn't do it in that mode either. */
1550 if (!target)
1551 return NULL_RTX;
1553 target = gen_rtx_SUBREG (GET_MODE (vtrue), promoted_target, byte_vtrue);
1554 SUBREG_PROMOTED_VAR_P (target) = SUBREG_PROMOTED_VAR_P (vtrue);
1555 SUBREG_PROMOTED_SET (target, SUBREG_PROMOTED_GET (vtrue));
1556 emit_move_insn (x, target);
1557 return x;
1559 else
1560 return NULL_RTX;
1563 /* Try only simple constants and registers here. More complex cases
1564 are handled in noce_try_cmove_arith after noce_try_store_flag_arith
1565 has had a go at it. */
1567 static int
1568 noce_try_cmove (struct noce_if_info *if_info)
1570 enum rtx_code code;
1571 rtx target;
1572 rtx_insn *seq;
1574 if ((CONSTANT_P (if_info->a) || register_operand (if_info->a, VOIDmode))
1575 && (CONSTANT_P (if_info->b) || register_operand (if_info->b, VOIDmode)))
1577 start_sequence ();
1579 code = GET_CODE (if_info->cond);
1580 target = noce_emit_cmove (if_info, if_info->x, code,
1581 XEXP (if_info->cond, 0),
1582 XEXP (if_info->cond, 1),
1583 if_info->a, if_info->b);
1585 if (target)
1587 if (target != if_info->x)
1588 noce_emit_move_insn (if_info->x, target);
1590 seq = end_ifcvt_sequence (if_info);
1591 if (!seq)
1592 return FALSE;
1594 emit_insn_before_setloc (seq, if_info->jump,
1595 INSN_LOCATION (if_info->insn_a));
1596 return TRUE;
1598 else
1600 end_sequence ();
1601 return FALSE;
1605 return FALSE;
1608 /* Try more complex cases involving conditional_move. */
1610 static int
1611 noce_try_cmove_arith (struct noce_if_info *if_info)
1613 rtx a = if_info->a;
1614 rtx b = if_info->b;
1615 rtx x = if_info->x;
1616 rtx orig_a, orig_b;
1617 rtx_insn *insn_a, *insn_b;
1618 rtx target;
1619 int is_mem = 0;
1620 int insn_cost;
1621 enum rtx_code code;
1622 rtx_insn *ifcvt_seq;
1624 /* A conditional move from two memory sources is equivalent to a
1625 conditional on their addresses followed by a load. Don't do this
1626 early because it'll screw alias analysis. Note that we've
1627 already checked for no side effects. */
1628 /* ??? FIXME: Magic number 5. */
1629 if (cse_not_expected
1630 && MEM_P (a) && MEM_P (b)
1631 && MEM_ADDR_SPACE (a) == MEM_ADDR_SPACE (b)
1632 && if_info->branch_cost >= 5)
1634 machine_mode address_mode = get_address_mode (a);
1636 a = XEXP (a, 0);
1637 b = XEXP (b, 0);
1638 x = gen_reg_rtx (address_mode);
1639 is_mem = 1;
1642 /* ??? We could handle this if we knew that a load from A or B could
1643 not trap or fault. This is also true if we've already loaded
1644 from the address along the path from ENTRY. */
1645 else if (may_trap_or_fault_p (a) || may_trap_or_fault_p (b))
1646 return FALSE;
1648 /* if (test) x = a + b; else x = c - d;
1649 => y = a + b;
1650 x = c - d;
1651 if (test)
1652 x = y;
1655 code = GET_CODE (if_info->cond);
1656 insn_a = if_info->insn_a;
1657 insn_b = if_info->insn_b;
1659 /* Total insn_rtx_cost should be smaller than branch cost. Exit
1660 if insn_rtx_cost can't be estimated. */
1661 if (insn_a)
1663 insn_cost
1664 = insn_rtx_cost (PATTERN (insn_a),
1665 optimize_bb_for_speed_p (BLOCK_FOR_INSN (insn_a)));
1666 if (insn_cost == 0 || insn_cost > COSTS_N_INSNS (if_info->branch_cost))
1667 return FALSE;
1669 else
1670 insn_cost = 0;
1672 if (insn_b)
1674 insn_cost
1675 += insn_rtx_cost (PATTERN (insn_b),
1676 optimize_bb_for_speed_p (BLOCK_FOR_INSN (insn_b)));
1677 if (insn_cost == 0 || insn_cost > COSTS_N_INSNS (if_info->branch_cost))
1678 return FALSE;
1681 /* Possibly rearrange operands to make things come out more natural. */
1682 if (reversed_comparison_code (if_info->cond, if_info->jump) != UNKNOWN)
1684 int reversep = 0;
1685 if (rtx_equal_p (b, x))
1686 reversep = 1;
1687 else if (general_operand (b, GET_MODE (b)))
1688 reversep = 1;
1690 if (reversep)
1692 rtx tmp;
1693 rtx_insn *tmp_insn;
1694 code = reversed_comparison_code (if_info->cond, if_info->jump);
1695 tmp = a, a = b, b = tmp;
1696 tmp_insn = insn_a, insn_a = insn_b, insn_b = tmp_insn;
1700 start_sequence ();
1702 orig_a = a;
1703 orig_b = b;
1705 /* If either operand is complex, load it into a register first.
1706 The best way to do this is to copy the original insn. In this
1707 way we preserve any clobbers etc that the insn may have had.
1708 This is of course not possible in the IS_MEM case. */
1709 if (! general_operand (a, GET_MODE (a)))
1711 rtx_insn *insn;
1713 if (is_mem)
1715 rtx reg = gen_reg_rtx (GET_MODE (a));
1716 insn = emit_insn (gen_rtx_SET (reg, a));
1718 else if (! insn_a)
1719 goto end_seq_and_fail;
1720 else
1722 a = gen_reg_rtx (GET_MODE (a));
1723 rtx_insn *copy_of_a = as_a <rtx_insn *> (copy_rtx (insn_a));
1724 rtx set = single_set (copy_of_a);
1725 SET_DEST (set) = a;
1726 insn = emit_insn (PATTERN (copy_of_a));
1728 if (recog_memoized (insn) < 0)
1729 goto end_seq_and_fail;
1731 if (! general_operand (b, GET_MODE (b)))
1733 rtx pat;
1734 rtx_insn *last;
1735 rtx_insn *new_insn;
1737 if (is_mem)
1739 rtx reg = gen_reg_rtx (GET_MODE (b));
1740 pat = gen_rtx_SET (reg, b);
1742 else if (! insn_b)
1743 goto end_seq_and_fail;
1744 else
1746 b = gen_reg_rtx (GET_MODE (b));
1747 rtx_insn *copy_of_insn_b = as_a <rtx_insn *> (copy_rtx (insn_b));
1748 rtx set = single_set (copy_of_insn_b);
1749 SET_DEST (set) = b;
1750 pat = PATTERN (copy_of_insn_b);
1753 /* If insn to set up A clobbers any registers B depends on, try to
1754 swap insn that sets up A with the one that sets up B. If even
1755 that doesn't help, punt. */
1756 last = get_last_insn ();
1757 if (last && modified_in_p (orig_b, last))
1759 new_insn = emit_insn_before (pat, get_insns ());
1760 if (modified_in_p (orig_a, new_insn))
1761 goto end_seq_and_fail;
1763 else
1764 new_insn = emit_insn (pat);
1766 if (recog_memoized (new_insn) < 0)
1767 goto end_seq_and_fail;
1770 target = noce_emit_cmove (if_info, x, code, XEXP (if_info->cond, 0),
1771 XEXP (if_info->cond, 1), a, b);
1773 if (! target)
1774 goto end_seq_and_fail;
1776 /* If we're handling a memory for above, emit the load now. */
1777 if (is_mem)
1779 rtx mem = gen_rtx_MEM (GET_MODE (if_info->x), target);
1781 /* Copy over flags as appropriate. */
1782 if (MEM_VOLATILE_P (if_info->a) || MEM_VOLATILE_P (if_info->b))
1783 MEM_VOLATILE_P (mem) = 1;
1784 if (MEM_ALIAS_SET (if_info->a) == MEM_ALIAS_SET (if_info->b))
1785 set_mem_alias_set (mem, MEM_ALIAS_SET (if_info->a));
1786 set_mem_align (mem,
1787 MIN (MEM_ALIGN (if_info->a), MEM_ALIGN (if_info->b)));
1789 gcc_assert (MEM_ADDR_SPACE (if_info->a) == MEM_ADDR_SPACE (if_info->b));
1790 set_mem_addr_space (mem, MEM_ADDR_SPACE (if_info->a));
1792 noce_emit_move_insn (if_info->x, mem);
1794 else if (target != x)
1795 noce_emit_move_insn (x, target);
1797 ifcvt_seq = end_ifcvt_sequence (if_info);
1798 if (!ifcvt_seq)
1799 return FALSE;
1801 emit_insn_before_setloc (ifcvt_seq, if_info->jump,
1802 INSN_LOCATION (if_info->insn_a));
1803 return TRUE;
1805 end_seq_and_fail:
1806 end_sequence ();
1807 return FALSE;
1810 /* For most cases, the simplified condition we found is the best
1811 choice, but this is not the case for the min/max/abs transforms.
1812 For these we wish to know that it is A or B in the condition. */
1814 static rtx
1815 noce_get_alt_condition (struct noce_if_info *if_info, rtx target,
1816 rtx_insn **earliest)
1818 rtx cond, set;
1819 rtx_insn *insn;
1820 int reverse;
1822 /* If target is already mentioned in the known condition, return it. */
1823 if (reg_mentioned_p (target, if_info->cond))
1825 *earliest = if_info->cond_earliest;
1826 return if_info->cond;
1829 set = pc_set (if_info->jump);
1830 cond = XEXP (SET_SRC (set), 0);
1831 reverse
1832 = GET_CODE (XEXP (SET_SRC (set), 2)) == LABEL_REF
1833 && LABEL_REF_LABEL (XEXP (SET_SRC (set), 2)) == JUMP_LABEL (if_info->jump);
1834 if (if_info->then_else_reversed)
1835 reverse = !reverse;
1837 /* If we're looking for a constant, try to make the conditional
1838 have that constant in it. There are two reasons why it may
1839 not have the constant we want:
1841 1. GCC may have needed to put the constant in a register, because
1842 the target can't compare directly against that constant. For
1843 this case, we look for a SET immediately before the comparison
1844 that puts a constant in that register.
1846 2. GCC may have canonicalized the conditional, for example
1847 replacing "if x < 4" with "if x <= 3". We can undo that (or
1848 make equivalent types of changes) to get the constants we need
1849 if they're off by one in the right direction. */
1851 if (CONST_INT_P (target))
1853 enum rtx_code code = GET_CODE (if_info->cond);
1854 rtx op_a = XEXP (if_info->cond, 0);
1855 rtx op_b = XEXP (if_info->cond, 1);
1856 rtx_insn *prev_insn;
1858 /* First, look to see if we put a constant in a register. */
1859 prev_insn = prev_nonnote_insn (if_info->cond_earliest);
1860 if (prev_insn
1861 && BLOCK_FOR_INSN (prev_insn)
1862 == BLOCK_FOR_INSN (if_info->cond_earliest)
1863 && INSN_P (prev_insn)
1864 && GET_CODE (PATTERN (prev_insn)) == SET)
1866 rtx src = find_reg_equal_equiv_note (prev_insn);
1867 if (!src)
1868 src = SET_SRC (PATTERN (prev_insn));
1869 if (CONST_INT_P (src))
1871 if (rtx_equal_p (op_a, SET_DEST (PATTERN (prev_insn))))
1872 op_a = src;
1873 else if (rtx_equal_p (op_b, SET_DEST (PATTERN (prev_insn))))
1874 op_b = src;
1876 if (CONST_INT_P (op_a))
1878 rtx tmp = op_a;
1879 op_a = op_b;
1880 op_b = tmp;
1881 code = swap_condition (code);
1886 /* Now, look to see if we can get the right constant by
1887 adjusting the conditional. */
1888 if (CONST_INT_P (op_b))
1890 HOST_WIDE_INT desired_val = INTVAL (target);
1891 HOST_WIDE_INT actual_val = INTVAL (op_b);
1893 switch (code)
1895 case LT:
1896 if (actual_val == desired_val + 1)
1898 code = LE;
1899 op_b = GEN_INT (desired_val);
1901 break;
1902 case LE:
1903 if (actual_val == desired_val - 1)
1905 code = LT;
1906 op_b = GEN_INT (desired_val);
1908 break;
1909 case GT:
1910 if (actual_val == desired_val - 1)
1912 code = GE;
1913 op_b = GEN_INT (desired_val);
1915 break;
1916 case GE:
1917 if (actual_val == desired_val + 1)
1919 code = GT;
1920 op_b = GEN_INT (desired_val);
1922 break;
1923 default:
1924 break;
1928 /* If we made any changes, generate a new conditional that is
1929 equivalent to what we started with, but has the right
1930 constants in it. */
1931 if (code != GET_CODE (if_info->cond)
1932 || op_a != XEXP (if_info->cond, 0)
1933 || op_b != XEXP (if_info->cond, 1))
1935 cond = gen_rtx_fmt_ee (code, GET_MODE (cond), op_a, op_b);
1936 *earliest = if_info->cond_earliest;
1937 return cond;
1941 cond = canonicalize_condition (if_info->jump, cond, reverse,
1942 earliest, target, HAVE_cbranchcc4, true);
1943 if (! cond || ! reg_mentioned_p (target, cond))
1944 return NULL;
1946 /* We almost certainly searched back to a different place.
1947 Need to re-verify correct lifetimes. */
1949 /* X may not be mentioned in the range (cond_earliest, jump]. */
1950 for (insn = if_info->jump; insn != *earliest; insn = PREV_INSN (insn))
1951 if (INSN_P (insn) && reg_overlap_mentioned_p (if_info->x, PATTERN (insn)))
1952 return NULL;
1954 /* A and B may not be modified in the range [cond_earliest, jump). */
1955 for (insn = *earliest; insn != if_info->jump; insn = NEXT_INSN (insn))
1956 if (INSN_P (insn)
1957 && (modified_in_p (if_info->a, insn)
1958 || modified_in_p (if_info->b, insn)))
1959 return NULL;
1961 return cond;
1964 /* Convert "if (a < b) x = a; else x = b;" to "x = min(a, b);", etc. */
1966 static int
1967 noce_try_minmax (struct noce_if_info *if_info)
1969 rtx cond, target;
1970 rtx_insn *earliest, *seq;
1971 enum rtx_code code, op;
1972 int unsignedp;
1974 /* ??? Reject modes with NaNs or signed zeros since we don't know how
1975 they will be resolved with an SMIN/SMAX. It wouldn't be too hard
1976 to get the target to tell us... */
1977 if (HONOR_SIGNED_ZEROS (if_info->x)
1978 || HONOR_NANS (if_info->x))
1979 return FALSE;
1981 cond = noce_get_alt_condition (if_info, if_info->a, &earliest);
1982 if (!cond)
1983 return FALSE;
1985 /* Verify the condition is of the form we expect, and canonicalize
1986 the comparison code. */
1987 code = GET_CODE (cond);
1988 if (rtx_equal_p (XEXP (cond, 0), if_info->a))
1990 if (! rtx_equal_p (XEXP (cond, 1), if_info->b))
1991 return FALSE;
1993 else if (rtx_equal_p (XEXP (cond, 1), if_info->a))
1995 if (! rtx_equal_p (XEXP (cond, 0), if_info->b))
1996 return FALSE;
1997 code = swap_condition (code);
1999 else
2000 return FALSE;
2002 /* Determine what sort of operation this is. Note that the code is for
2003 a taken branch, so the code->operation mapping appears backwards. */
2004 switch (code)
2006 case LT:
2007 case LE:
2008 case UNLT:
2009 case UNLE:
2010 op = SMAX;
2011 unsignedp = 0;
2012 break;
2013 case GT:
2014 case GE:
2015 case UNGT:
2016 case UNGE:
2017 op = SMIN;
2018 unsignedp = 0;
2019 break;
2020 case LTU:
2021 case LEU:
2022 op = UMAX;
2023 unsignedp = 1;
2024 break;
2025 case GTU:
2026 case GEU:
2027 op = UMIN;
2028 unsignedp = 1;
2029 break;
2030 default:
2031 return FALSE;
2034 start_sequence ();
2036 target = expand_simple_binop (GET_MODE (if_info->x), op,
2037 if_info->a, if_info->b,
2038 if_info->x, unsignedp, OPTAB_WIDEN);
2039 if (! target)
2041 end_sequence ();
2042 return FALSE;
2044 if (target != if_info->x)
2045 noce_emit_move_insn (if_info->x, target);
2047 seq = end_ifcvt_sequence (if_info);
2048 if (!seq)
2049 return FALSE;
2051 emit_insn_before_setloc (seq, if_info->jump, INSN_LOCATION (if_info->insn_a));
2052 if_info->cond = cond;
2053 if_info->cond_earliest = earliest;
2055 return TRUE;
2058 /* Convert "if (a < 0) x = -a; else x = a;" to "x = abs(a);",
2059 "if (a < 0) x = ~a; else x = a;" to "x = one_cmpl_abs(a);",
2060 etc. */
2062 static int
2063 noce_try_abs (struct noce_if_info *if_info)
2065 rtx cond, target, a, b, c;
2066 rtx_insn *earliest, *seq;
2067 int negate;
2068 bool one_cmpl = false;
2070 /* Reject modes with signed zeros. */
2071 if (HONOR_SIGNED_ZEROS (if_info->x))
2072 return FALSE;
2074 /* Recognize A and B as constituting an ABS or NABS. The canonical
2075 form is a branch around the negation, taken when the object is the
2076 first operand of a comparison against 0 that evaluates to true. */
2077 a = if_info->a;
2078 b = if_info->b;
2079 if (GET_CODE (a) == NEG && rtx_equal_p (XEXP (a, 0), b))
2080 negate = 0;
2081 else if (GET_CODE (b) == NEG && rtx_equal_p (XEXP (b, 0), a))
2083 c = a; a = b; b = c;
2084 negate = 1;
2086 else if (GET_CODE (a) == NOT && rtx_equal_p (XEXP (a, 0), b))
2088 negate = 0;
2089 one_cmpl = true;
2091 else if (GET_CODE (b) == NOT && rtx_equal_p (XEXP (b, 0), a))
2093 c = a; a = b; b = c;
2094 negate = 1;
2095 one_cmpl = true;
2097 else
2098 return FALSE;
2100 cond = noce_get_alt_condition (if_info, b, &earliest);
2101 if (!cond)
2102 return FALSE;
2104 /* Verify the condition is of the form we expect. */
2105 if (rtx_equal_p (XEXP (cond, 0), b))
2106 c = XEXP (cond, 1);
2107 else if (rtx_equal_p (XEXP (cond, 1), b))
2109 c = XEXP (cond, 0);
2110 negate = !negate;
2112 else
2113 return FALSE;
2115 /* Verify that C is zero. Search one step backward for a
2116 REG_EQUAL note or a simple source if necessary. */
2117 if (REG_P (c))
2119 rtx set;
2120 rtx_insn *insn = prev_nonnote_insn (earliest);
2121 if (insn
2122 && BLOCK_FOR_INSN (insn) == BLOCK_FOR_INSN (earliest)
2123 && (set = single_set (insn))
2124 && rtx_equal_p (SET_DEST (set), c))
2126 rtx note = find_reg_equal_equiv_note (insn);
2127 if (note)
2128 c = XEXP (note, 0);
2129 else
2130 c = SET_SRC (set);
2132 else
2133 return FALSE;
2135 if (MEM_P (c)
2136 && GET_CODE (XEXP (c, 0)) == SYMBOL_REF
2137 && CONSTANT_POOL_ADDRESS_P (XEXP (c, 0)))
2138 c = get_pool_constant (XEXP (c, 0));
2140 /* Work around funny ideas get_condition has wrt canonicalization.
2141 Note that these rtx constants are known to be CONST_INT, and
2142 therefore imply integer comparisons. */
2143 if (c == constm1_rtx && GET_CODE (cond) == GT)
2145 else if (c == const1_rtx && GET_CODE (cond) == LT)
2147 else if (c != CONST0_RTX (GET_MODE (b)))
2148 return FALSE;
2150 /* Determine what sort of operation this is. */
2151 switch (GET_CODE (cond))
2153 case LT:
2154 case LE:
2155 case UNLT:
2156 case UNLE:
2157 negate = !negate;
2158 break;
2159 case GT:
2160 case GE:
2161 case UNGT:
2162 case UNGE:
2163 break;
2164 default:
2165 return FALSE;
2168 start_sequence ();
2169 if (one_cmpl)
2170 target = expand_one_cmpl_abs_nojump (GET_MODE (if_info->x), b,
2171 if_info->x);
2172 else
2173 target = expand_abs_nojump (GET_MODE (if_info->x), b, if_info->x, 1);
2175 /* ??? It's a quandary whether cmove would be better here, especially
2176 for integers. Perhaps combine will clean things up. */
2177 if (target && negate)
2179 if (one_cmpl)
2180 target = expand_simple_unop (GET_MODE (target), NOT, target,
2181 if_info->x, 0);
2182 else
2183 target = expand_simple_unop (GET_MODE (target), NEG, target,
2184 if_info->x, 0);
2187 if (! target)
2189 end_sequence ();
2190 return FALSE;
2193 if (target != if_info->x)
2194 noce_emit_move_insn (if_info->x, target);
2196 seq = end_ifcvt_sequence (if_info);
2197 if (!seq)
2198 return FALSE;
2200 emit_insn_before_setloc (seq, if_info->jump, INSN_LOCATION (if_info->insn_a));
2201 if_info->cond = cond;
2202 if_info->cond_earliest = earliest;
2204 return TRUE;
2207 /* Convert "if (m < 0) x = b; else x = 0;" to "x = (m >> C) & b;". */
2209 static int
2210 noce_try_sign_mask (struct noce_if_info *if_info)
2212 rtx cond, t, m, c;
2213 rtx_insn *seq;
2214 machine_mode mode;
2215 enum rtx_code code;
2216 bool t_unconditional;
2218 cond = if_info->cond;
2219 code = GET_CODE (cond);
2220 m = XEXP (cond, 0);
2221 c = XEXP (cond, 1);
2223 t = NULL_RTX;
2224 if (if_info->a == const0_rtx)
2226 if ((code == LT && c == const0_rtx)
2227 || (code == LE && c == constm1_rtx))
2228 t = if_info->b;
2230 else if (if_info->b == const0_rtx)
2232 if ((code == GE && c == const0_rtx)
2233 || (code == GT && c == constm1_rtx))
2234 t = if_info->a;
2237 if (! t || side_effects_p (t))
2238 return FALSE;
2240 /* We currently don't handle different modes. */
2241 mode = GET_MODE (t);
2242 if (GET_MODE (m) != mode)
2243 return FALSE;
2245 /* This is only profitable if T is unconditionally executed/evaluated in the
2246 original insn sequence or T is cheap. The former happens if B is the
2247 non-zero (T) value and if INSN_B was taken from TEST_BB, or there was no
2248 INSN_B which can happen for e.g. conditional stores to memory. For the
2249 cost computation use the block TEST_BB where the evaluation will end up
2250 after the transformation. */
2251 t_unconditional =
2252 (t == if_info->b
2253 && (if_info->insn_b == NULL_RTX
2254 || BLOCK_FOR_INSN (if_info->insn_b) == if_info->test_bb));
2255 if (!(t_unconditional
2256 || (set_src_cost (t, optimize_bb_for_speed_p (if_info->test_bb))
2257 < COSTS_N_INSNS (2))))
2258 return FALSE;
2260 start_sequence ();
2261 /* Use emit_store_flag to generate "m < 0 ? -1 : 0" instead of expanding
2262 "(signed) m >> 31" directly. This benefits targets with specialized
2263 insns to obtain the signmask, but still uses ashr_optab otherwise. */
2264 m = emit_store_flag (gen_reg_rtx (mode), LT, m, const0_rtx, mode, 0, -1);
2265 t = m ? expand_binop (mode, and_optab, m, t, NULL_RTX, 0, OPTAB_DIRECT)
2266 : NULL_RTX;
2268 if (!t)
2270 end_sequence ();
2271 return FALSE;
2274 noce_emit_move_insn (if_info->x, t);
2276 seq = end_ifcvt_sequence (if_info);
2277 if (!seq)
2278 return FALSE;
2280 emit_insn_before_setloc (seq, if_info->jump, INSN_LOCATION (if_info->insn_a));
2281 return TRUE;
2285 /* Optimize away "if (x & C) x |= C" and similar bit manipulation
2286 transformations. */
2288 static int
2289 noce_try_bitop (struct noce_if_info *if_info)
2291 rtx cond, x, a, result;
2292 rtx_insn *seq;
2293 machine_mode mode;
2294 enum rtx_code code;
2295 int bitnum;
2297 x = if_info->x;
2298 cond = if_info->cond;
2299 code = GET_CODE (cond);
2301 /* Check for no else condition. */
2302 if (! rtx_equal_p (x, if_info->b))
2303 return FALSE;
2305 /* Check for a suitable condition. */
2306 if (code != NE && code != EQ)
2307 return FALSE;
2308 if (XEXP (cond, 1) != const0_rtx)
2309 return FALSE;
2310 cond = XEXP (cond, 0);
2312 /* ??? We could also handle AND here. */
2313 if (GET_CODE (cond) == ZERO_EXTRACT)
2315 if (XEXP (cond, 1) != const1_rtx
2316 || !CONST_INT_P (XEXP (cond, 2))
2317 || ! rtx_equal_p (x, XEXP (cond, 0)))
2318 return FALSE;
2319 bitnum = INTVAL (XEXP (cond, 2));
2320 mode = GET_MODE (x);
2321 if (BITS_BIG_ENDIAN)
2322 bitnum = GET_MODE_BITSIZE (mode) - 1 - bitnum;
2323 if (bitnum < 0 || bitnum >= HOST_BITS_PER_WIDE_INT)
2324 return FALSE;
2326 else
2327 return FALSE;
2329 a = if_info->a;
2330 if (GET_CODE (a) == IOR || GET_CODE (a) == XOR)
2332 /* Check for "if (X & C) x = x op C". */
2333 if (! rtx_equal_p (x, XEXP (a, 0))
2334 || !CONST_INT_P (XEXP (a, 1))
2335 || (INTVAL (XEXP (a, 1)) & GET_MODE_MASK (mode))
2336 != (unsigned HOST_WIDE_INT) 1 << bitnum)
2337 return FALSE;
2339 /* if ((x & C) == 0) x |= C; is transformed to x |= C. */
2340 /* if ((x & C) != 0) x |= C; is transformed to nothing. */
2341 if (GET_CODE (a) == IOR)
2342 result = (code == NE) ? a : NULL_RTX;
2343 else if (code == NE)
2345 /* if ((x & C) == 0) x ^= C; is transformed to x |= C. */
2346 result = gen_int_mode ((HOST_WIDE_INT) 1 << bitnum, mode);
2347 result = simplify_gen_binary (IOR, mode, x, result);
2349 else
2351 /* if ((x & C) != 0) x ^= C; is transformed to x &= ~C. */
2352 result = gen_int_mode (~((HOST_WIDE_INT) 1 << bitnum), mode);
2353 result = simplify_gen_binary (AND, mode, x, result);
2356 else if (GET_CODE (a) == AND)
2358 /* Check for "if (X & C) x &= ~C". */
2359 if (! rtx_equal_p (x, XEXP (a, 0))
2360 || !CONST_INT_P (XEXP (a, 1))
2361 || (INTVAL (XEXP (a, 1)) & GET_MODE_MASK (mode))
2362 != (~((HOST_WIDE_INT) 1 << bitnum) & GET_MODE_MASK (mode)))
2363 return FALSE;
2365 /* if ((x & C) == 0) x &= ~C; is transformed to nothing. */
2366 /* if ((x & C) != 0) x &= ~C; is transformed to x &= ~C. */
2367 result = (code == EQ) ? a : NULL_RTX;
2369 else
2370 return FALSE;
2372 if (result)
2374 start_sequence ();
2375 noce_emit_move_insn (x, result);
2376 seq = end_ifcvt_sequence (if_info);
2377 if (!seq)
2378 return FALSE;
2380 emit_insn_before_setloc (seq, if_info->jump,
2381 INSN_LOCATION (if_info->insn_a));
2383 return TRUE;
2387 /* Similar to get_condition, only the resulting condition must be
2388 valid at JUMP, instead of at EARLIEST.
2390 If THEN_ELSE_REVERSED is true, the fallthrough does not go to the
2391 THEN block of the caller, and we have to reverse the condition. */
2393 static rtx
2394 noce_get_condition (rtx_insn *jump, rtx_insn **earliest, bool then_else_reversed)
2396 rtx cond, set, tmp;
2397 bool reverse;
2399 if (! any_condjump_p (jump))
2400 return NULL_RTX;
2402 set = pc_set (jump);
2404 /* If this branches to JUMP_LABEL when the condition is false,
2405 reverse the condition. */
2406 reverse = (GET_CODE (XEXP (SET_SRC (set), 2)) == LABEL_REF
2407 && LABEL_REF_LABEL (XEXP (SET_SRC (set), 2)) == JUMP_LABEL (jump));
2409 /* We may have to reverse because the caller's if block is not canonical,
2410 i.e. the THEN block isn't the fallthrough block for the TEST block
2411 (see find_if_header). */
2412 if (then_else_reversed)
2413 reverse = !reverse;
2415 /* If the condition variable is a register and is MODE_INT, accept it. */
2417 cond = XEXP (SET_SRC (set), 0);
2418 tmp = XEXP (cond, 0);
2419 if (REG_P (tmp) && GET_MODE_CLASS (GET_MODE (tmp)) == MODE_INT
2420 && (GET_MODE (tmp) != BImode
2421 || !targetm.small_register_classes_for_mode_p (BImode)))
2423 *earliest = jump;
2425 if (reverse)
2426 cond = gen_rtx_fmt_ee (reverse_condition (GET_CODE (cond)),
2427 GET_MODE (cond), tmp, XEXP (cond, 1));
2428 return cond;
2431 /* Otherwise, fall back on canonicalize_condition to do the dirty
2432 work of manipulating MODE_CC values and COMPARE rtx codes. */
2433 tmp = canonicalize_condition (jump, cond, reverse, earliest,
2434 NULL_RTX, HAVE_cbranchcc4, true);
2436 /* We don't handle side-effects in the condition, like handling
2437 REG_INC notes and making sure no duplicate conditions are emitted. */
2438 if (tmp != NULL_RTX && side_effects_p (tmp))
2439 return NULL_RTX;
2441 return tmp;
2444 /* Return true if OP is ok for if-then-else processing. */
2446 static int
2447 noce_operand_ok (const_rtx op)
2449 if (side_effects_p (op))
2450 return FALSE;
2452 /* We special-case memories, so handle any of them with
2453 no address side effects. */
2454 if (MEM_P (op))
2455 return ! side_effects_p (XEXP (op, 0));
2457 return ! may_trap_p (op);
2460 /* Return true if a write into MEM may trap or fault. */
2462 static bool
2463 noce_mem_write_may_trap_or_fault_p (const_rtx mem)
2465 rtx addr;
2467 if (MEM_READONLY_P (mem))
2468 return true;
2470 if (may_trap_or_fault_p (mem))
2471 return true;
2473 addr = XEXP (mem, 0);
2475 /* Call target hook to avoid the effects of -fpic etc.... */
2476 addr = targetm.delegitimize_address (addr);
2478 while (addr)
2479 switch (GET_CODE (addr))
2481 case CONST:
2482 case PRE_DEC:
2483 case PRE_INC:
2484 case POST_DEC:
2485 case POST_INC:
2486 case POST_MODIFY:
2487 addr = XEXP (addr, 0);
2488 break;
2489 case LO_SUM:
2490 case PRE_MODIFY:
2491 addr = XEXP (addr, 1);
2492 break;
2493 case PLUS:
2494 if (CONST_INT_P (XEXP (addr, 1)))
2495 addr = XEXP (addr, 0);
2496 else
2497 return false;
2498 break;
2499 case LABEL_REF:
2500 return true;
2501 case SYMBOL_REF:
2502 if (SYMBOL_REF_DECL (addr)
2503 && decl_readonly_section (SYMBOL_REF_DECL (addr), 0))
2504 return true;
2505 return false;
2506 default:
2507 return false;
2510 return false;
2513 /* Return whether we can use store speculation for MEM. TOP_BB is the
2514 basic block above the conditional block where we are considering
2515 doing the speculative store. We look for whether MEM is set
2516 unconditionally later in the function. */
2518 static bool
2519 noce_can_store_speculate_p (basic_block top_bb, const_rtx mem)
2521 basic_block dominator;
2523 for (dominator = get_immediate_dominator (CDI_POST_DOMINATORS, top_bb);
2524 dominator != NULL;
2525 dominator = get_immediate_dominator (CDI_POST_DOMINATORS, dominator))
2527 rtx_insn *insn;
2529 FOR_BB_INSNS (dominator, insn)
2531 /* If we see something that might be a memory barrier, we
2532 have to stop looking. Even if the MEM is set later in
2533 the function, we still don't want to set it
2534 unconditionally before the barrier. */
2535 if (INSN_P (insn)
2536 && (volatile_insn_p (PATTERN (insn))
2537 || (CALL_P (insn) && (!RTL_CONST_CALL_P (insn)))))
2538 return false;
2540 if (memory_must_be_modified_in_insn_p (mem, insn))
2541 return true;
2542 if (modified_in_p (XEXP (mem, 0), insn))
2543 return false;
2548 return false;
2551 /* Given a simple IF-THEN-JOIN or IF-THEN-ELSE-JOIN block, attempt to convert
2552 it without using conditional execution. Return TRUE if we were successful
2553 at converting the block. */
2555 static int
2556 noce_process_if_block (struct noce_if_info *if_info)
2558 basic_block test_bb = if_info->test_bb; /* test block */
2559 basic_block then_bb = if_info->then_bb; /* THEN */
2560 basic_block else_bb = if_info->else_bb; /* ELSE or NULL */
2561 basic_block join_bb = if_info->join_bb; /* JOIN */
2562 rtx_insn *jump = if_info->jump;
2563 rtx cond = if_info->cond;
2564 rtx_insn *insn_a, *insn_b;
2565 rtx set_a, set_b;
2566 rtx orig_x, x, a, b;
2567 rtx cc;
2569 /* We're looking for patterns of the form
2571 (1) if (...) x = a; else x = b;
2572 (2) x = b; if (...) x = a;
2573 (3) if (...) x = a; // as if with an initial x = x.
2575 The later patterns require jumps to be more expensive.
2577 ??? For future expansion, look for multiple X in such patterns. */
2579 /* Look for one of the potential sets. */
2580 insn_a = first_active_insn (then_bb);
2581 if (! insn_a
2582 || insn_a != last_active_insn (then_bb, FALSE)
2583 || (set_a = single_set (insn_a)) == NULL_RTX)
2584 return FALSE;
2586 x = SET_DEST (set_a);
2587 a = SET_SRC (set_a);
2589 /* Look for the other potential set. Make sure we've got equivalent
2590 destinations. */
2591 /* ??? This is overconservative. Storing to two different mems is
2592 as easy as conditionally computing the address. Storing to a
2593 single mem merely requires a scratch memory to use as one of the
2594 destination addresses; often the memory immediately below the
2595 stack pointer is available for this. */
2596 set_b = NULL_RTX;
2597 if (else_bb)
2599 insn_b = first_active_insn (else_bb);
2600 if (! insn_b
2601 || insn_b != last_active_insn (else_bb, FALSE)
2602 || (set_b = single_set (insn_b)) == NULL_RTX
2603 || ! rtx_interchangeable_p (x, SET_DEST (set_b)))
2604 return FALSE;
2606 else
2608 insn_b = prev_nonnote_nondebug_insn (if_info->cond_earliest);
2609 /* We're going to be moving the evaluation of B down from above
2610 COND_EARLIEST to JUMP. Make sure the relevant data is still
2611 intact. */
2612 if (! insn_b
2613 || BLOCK_FOR_INSN (insn_b) != BLOCK_FOR_INSN (if_info->cond_earliest)
2614 || !NONJUMP_INSN_P (insn_b)
2615 || (set_b = single_set (insn_b)) == NULL_RTX
2616 || ! rtx_interchangeable_p (x, SET_DEST (set_b))
2617 || ! noce_operand_ok (SET_SRC (set_b))
2618 || reg_overlap_mentioned_p (x, SET_SRC (set_b))
2619 || modified_between_p (SET_SRC (set_b), insn_b, jump)
2620 /* Avoid extending the lifetime of hard registers on small
2621 register class machines. */
2622 || (REG_P (SET_SRC (set_b))
2623 && HARD_REGISTER_P (SET_SRC (set_b))
2624 && targetm.small_register_classes_for_mode_p
2625 (GET_MODE (SET_SRC (set_b))))
2626 /* Likewise with X. In particular this can happen when
2627 noce_get_condition looks farther back in the instruction
2628 stream than one might expect. */
2629 || reg_overlap_mentioned_p (x, cond)
2630 || reg_overlap_mentioned_p (x, a)
2631 || modified_between_p (x, insn_b, jump))
2633 insn_b = NULL;
2634 set_b = NULL_RTX;
2638 /* If x has side effects then only the if-then-else form is safe to
2639 convert. But even in that case we would need to restore any notes
2640 (such as REG_INC) at then end. That can be tricky if
2641 noce_emit_move_insn expands to more than one insn, so disable the
2642 optimization entirely for now if there are side effects. */
2643 if (side_effects_p (x))
2644 return FALSE;
2646 b = (set_b ? SET_SRC (set_b) : x);
2648 /* Only operate on register destinations, and even then avoid extending
2649 the lifetime of hard registers on small register class machines. */
2650 orig_x = x;
2651 if (!REG_P (x)
2652 || (HARD_REGISTER_P (x)
2653 && targetm.small_register_classes_for_mode_p (GET_MODE (x))))
2655 if (GET_MODE (x) == BLKmode)
2656 return FALSE;
2658 if (GET_CODE (x) == ZERO_EXTRACT
2659 && (!CONST_INT_P (XEXP (x, 1))
2660 || !CONST_INT_P (XEXP (x, 2))))
2661 return FALSE;
2663 x = gen_reg_rtx (GET_MODE (GET_CODE (x) == STRICT_LOW_PART
2664 ? XEXP (x, 0) : x));
2667 /* Don't operate on sources that may trap or are volatile. */
2668 if (! noce_operand_ok (a) || ! noce_operand_ok (b))
2669 return FALSE;
2671 retry:
2672 /* Set up the info block for our subroutines. */
2673 if_info->insn_a = insn_a;
2674 if_info->insn_b = insn_b;
2675 if_info->x = x;
2676 if_info->a = a;
2677 if_info->b = b;
2679 /* Skip it if the instruction to be moved might clobber CC. */
2680 cc = cc_in_cond (cond);
2681 if (cc
2682 && (set_of (cc, insn_a)
2683 || (insn_b && set_of (cc, insn_b))))
2684 return FALSE;
2686 /* Try optimizations in some approximation of a useful order. */
2687 /* ??? Should first look to see if X is live incoming at all. If it
2688 isn't, we don't need anything but an unconditional set. */
2690 /* Look and see if A and B are really the same. Avoid creating silly
2691 cmove constructs that no one will fix up later. */
2692 if (rtx_interchangeable_p (a, b))
2694 /* If we have an INSN_B, we don't have to create any new rtl. Just
2695 move the instruction that we already have. If we don't have an
2696 INSN_B, that means that A == X, and we've got a noop move. In
2697 that case don't do anything and let the code below delete INSN_A. */
2698 if (insn_b && else_bb)
2700 rtx note;
2702 if (else_bb && insn_b == BB_END (else_bb))
2703 BB_END (else_bb) = PREV_INSN (insn_b);
2704 reorder_insns (insn_b, insn_b, PREV_INSN (jump));
2706 /* If there was a REG_EQUAL note, delete it since it may have been
2707 true due to this insn being after a jump. */
2708 if ((note = find_reg_note (insn_b, REG_EQUAL, NULL_RTX)) != 0)
2709 remove_note (insn_b, note);
2711 insn_b = NULL;
2713 /* If we have "x = b; if (...) x = a;", and x has side-effects, then
2714 x must be executed twice. */
2715 else if (insn_b && side_effects_p (orig_x))
2716 return FALSE;
2718 x = orig_x;
2719 goto success;
2722 if (!set_b && MEM_P (orig_x))
2724 /* Disallow the "if (...) x = a;" form (implicit "else x = x;")
2725 for optimizations if writing to x may trap or fault,
2726 i.e. it's a memory other than a static var or a stack slot,
2727 is misaligned on strict aligned machines or is read-only. If
2728 x is a read-only memory, then the program is valid only if we
2729 avoid the store into it. If there are stores on both the
2730 THEN and ELSE arms, then we can go ahead with the conversion;
2731 either the program is broken, or the condition is always
2732 false such that the other memory is selected. */
2733 if (noce_mem_write_may_trap_or_fault_p (orig_x))
2734 return FALSE;
2736 /* Avoid store speculation: given "if (...) x = a" where x is a
2737 MEM, we only want to do the store if x is always set
2738 somewhere in the function. This avoids cases like
2739 if (pthread_mutex_trylock(mutex))
2740 ++global_variable;
2741 where we only want global_variable to be changed if the mutex
2742 is held. FIXME: This should ideally be expressed directly in
2743 RTL somehow. */
2744 if (!noce_can_store_speculate_p (test_bb, orig_x))
2745 return FALSE;
2748 if (noce_try_move (if_info))
2749 goto success;
2750 if (noce_try_store_flag (if_info))
2751 goto success;
2752 if (noce_try_bitop (if_info))
2753 goto success;
2754 if (noce_try_minmax (if_info))
2755 goto success;
2756 if (noce_try_abs (if_info))
2757 goto success;
2758 if (HAVE_conditional_move
2759 && noce_try_cmove (if_info))
2760 goto success;
2761 if (! targetm.have_conditional_execution ())
2763 if (noce_try_store_flag_constants (if_info))
2764 goto success;
2765 if (noce_try_addcc (if_info))
2766 goto success;
2767 if (noce_try_store_flag_mask (if_info))
2768 goto success;
2769 if (HAVE_conditional_move
2770 && noce_try_cmove_arith (if_info))
2771 goto success;
2772 if (noce_try_sign_mask (if_info))
2773 goto success;
2776 if (!else_bb && set_b)
2778 insn_b = NULL;
2779 set_b = NULL_RTX;
2780 b = orig_x;
2781 goto retry;
2784 return FALSE;
2786 success:
2788 /* If we used a temporary, fix it up now. */
2789 if (orig_x != x)
2791 rtx_insn *seq;
2793 start_sequence ();
2794 noce_emit_move_insn (orig_x, x);
2795 seq = get_insns ();
2796 set_used_flags (orig_x);
2797 unshare_all_rtl_in_chain (seq);
2798 end_sequence ();
2800 emit_insn_before_setloc (seq, BB_END (test_bb), INSN_LOCATION (insn_a));
2803 /* The original THEN and ELSE blocks may now be removed. The test block
2804 must now jump to the join block. If the test block and the join block
2805 can be merged, do so. */
2806 if (else_bb)
2808 delete_basic_block (else_bb);
2809 num_true_changes++;
2811 else
2812 remove_edge (find_edge (test_bb, join_bb));
2814 remove_edge (find_edge (then_bb, join_bb));
2815 redirect_edge_and_branch_force (single_succ_edge (test_bb), join_bb);
2816 delete_basic_block (then_bb);
2817 num_true_changes++;
2819 if (can_merge_blocks_p (test_bb, join_bb))
2821 merge_blocks (test_bb, join_bb);
2822 num_true_changes++;
2825 num_updated_if_blocks++;
2826 return TRUE;
2829 /* Check whether a block is suitable for conditional move conversion.
2830 Every insn must be a simple set of a register to a constant or a
2831 register. For each assignment, store the value in the pointer map
2832 VALS, keyed indexed by register pointer, then store the register
2833 pointer in REGS. COND is the condition we will test. */
2835 static int
2836 check_cond_move_block (basic_block bb,
2837 hash_map<rtx, rtx> *vals,
2838 vec<rtx> *regs,
2839 rtx cond)
2841 rtx_insn *insn;
2842 rtx cc = cc_in_cond (cond);
2844 /* We can only handle simple jumps at the end of the basic block.
2845 It is almost impossible to update the CFG otherwise. */
2846 insn = BB_END (bb);
2847 if (JUMP_P (insn) && !onlyjump_p (insn))
2848 return FALSE;
2850 FOR_BB_INSNS (bb, insn)
2852 rtx set, dest, src;
2854 if (!NONDEBUG_INSN_P (insn) || JUMP_P (insn))
2855 continue;
2856 set = single_set (insn);
2857 if (!set)
2858 return FALSE;
2860 dest = SET_DEST (set);
2861 src = SET_SRC (set);
2862 if (!REG_P (dest)
2863 || (HARD_REGISTER_P (dest)
2864 && targetm.small_register_classes_for_mode_p (GET_MODE (dest))))
2865 return FALSE;
2867 if (!CONSTANT_P (src) && !register_operand (src, VOIDmode))
2868 return FALSE;
2870 if (side_effects_p (src) || side_effects_p (dest))
2871 return FALSE;
2873 if (may_trap_p (src) || may_trap_p (dest))
2874 return FALSE;
2876 /* Don't try to handle this if the source register was
2877 modified earlier in the block. */
2878 if ((REG_P (src)
2879 && vals->get (src))
2880 || (GET_CODE (src) == SUBREG && REG_P (SUBREG_REG (src))
2881 && vals->get (SUBREG_REG (src))))
2882 return FALSE;
2884 /* Don't try to handle this if the destination register was
2885 modified earlier in the block. */
2886 if (vals->get (dest))
2887 return FALSE;
2889 /* Don't try to handle this if the condition uses the
2890 destination register. */
2891 if (reg_overlap_mentioned_p (dest, cond))
2892 return FALSE;
2894 /* Don't try to handle this if the source register is modified
2895 later in the block. */
2896 if (!CONSTANT_P (src)
2897 && modified_between_p (src, insn, NEXT_INSN (BB_END (bb))))
2898 return FALSE;
2900 /* Skip it if the instruction to be moved might clobber CC. */
2901 if (cc && set_of (cc, insn))
2902 return FALSE;
2904 vals->put (dest, src);
2906 regs->safe_push (dest);
2909 return TRUE;
2912 /* Given a basic block BB suitable for conditional move conversion,
2913 a condition COND, and pointer maps THEN_VALS and ELSE_VALS containing
2914 the register values depending on COND, emit the insns in the block as
2915 conditional moves. If ELSE_BLOCK is true, THEN_BB was already
2916 processed. The caller has started a sequence for the conversion.
2917 Return true if successful, false if something goes wrong. */
2919 static bool
2920 cond_move_convert_if_block (struct noce_if_info *if_infop,
2921 basic_block bb, rtx cond,
2922 hash_map<rtx, rtx> *then_vals,
2923 hash_map<rtx, rtx> *else_vals,
2924 bool else_block_p)
2926 enum rtx_code code;
2927 rtx_insn *insn;
2928 rtx cond_arg0, cond_arg1;
2930 code = GET_CODE (cond);
2931 cond_arg0 = XEXP (cond, 0);
2932 cond_arg1 = XEXP (cond, 1);
2934 FOR_BB_INSNS (bb, insn)
2936 rtx set, target, dest, t, e;
2938 /* ??? Maybe emit conditional debug insn? */
2939 if (!NONDEBUG_INSN_P (insn) || JUMP_P (insn))
2940 continue;
2941 set = single_set (insn);
2942 gcc_assert (set && REG_P (SET_DEST (set)));
2944 dest = SET_DEST (set);
2946 rtx *then_slot = then_vals->get (dest);
2947 rtx *else_slot = else_vals->get (dest);
2948 t = then_slot ? *then_slot : NULL_RTX;
2949 e = else_slot ? *else_slot : NULL_RTX;
2951 if (else_block_p)
2953 /* If this register was set in the then block, we already
2954 handled this case there. */
2955 if (t)
2956 continue;
2957 t = dest;
2958 gcc_assert (e);
2960 else
2962 gcc_assert (t);
2963 if (!e)
2964 e = dest;
2967 target = noce_emit_cmove (if_infop, dest, code, cond_arg0, cond_arg1,
2968 t, e);
2969 if (!target)
2970 return false;
2972 if (target != dest)
2973 noce_emit_move_insn (dest, target);
2976 return true;
2979 /* Given a simple IF-THEN-JOIN or IF-THEN-ELSE-JOIN block, attempt to convert
2980 it using only conditional moves. Return TRUE if we were successful at
2981 converting the block. */
2983 static int
2984 cond_move_process_if_block (struct noce_if_info *if_info)
2986 basic_block test_bb = if_info->test_bb;
2987 basic_block then_bb = if_info->then_bb;
2988 basic_block else_bb = if_info->else_bb;
2989 basic_block join_bb = if_info->join_bb;
2990 rtx_insn *jump = if_info->jump;
2991 rtx cond = if_info->cond;
2992 rtx_insn *seq, *loc_insn;
2993 rtx reg;
2994 int c;
2995 vec<rtx> then_regs = vNULL;
2996 vec<rtx> else_regs = vNULL;
2997 unsigned int i;
2998 int success_p = FALSE;
3000 /* Build a mapping for each block to the value used for each
3001 register. */
3002 hash_map<rtx, rtx> then_vals;
3003 hash_map<rtx, rtx> else_vals;
3005 /* Make sure the blocks are suitable. */
3006 if (!check_cond_move_block (then_bb, &then_vals, &then_regs, cond)
3007 || (else_bb
3008 && !check_cond_move_block (else_bb, &else_vals, &else_regs, cond)))
3009 goto done;
3011 /* Make sure the blocks can be used together. If the same register
3012 is set in both blocks, and is not set to a constant in both
3013 cases, then both blocks must set it to the same register. We
3014 have already verified that if it is set to a register, that the
3015 source register does not change after the assignment. Also count
3016 the number of registers set in only one of the blocks. */
3017 c = 0;
3018 FOR_EACH_VEC_ELT (then_regs, i, reg)
3020 rtx *then_slot = then_vals.get (reg);
3021 rtx *else_slot = else_vals.get (reg);
3023 gcc_checking_assert (then_slot);
3024 if (!else_slot)
3025 ++c;
3026 else
3028 rtx then_val = *then_slot;
3029 rtx else_val = *else_slot;
3030 if (!CONSTANT_P (then_val) && !CONSTANT_P (else_val)
3031 && !rtx_equal_p (then_val, else_val))
3032 goto done;
3036 /* Finish off c for MAX_CONDITIONAL_EXECUTE. */
3037 FOR_EACH_VEC_ELT (else_regs, i, reg)
3039 gcc_checking_assert (else_vals.get (reg));
3040 if (!then_vals.get (reg))
3041 ++c;
3044 /* Make sure it is reasonable to convert this block. What matters
3045 is the number of assignments currently made in only one of the
3046 branches, since if we convert we are going to always execute
3047 them. */
3048 if (c > MAX_CONDITIONAL_EXECUTE)
3049 goto done;
3051 /* Try to emit the conditional moves. First do the then block,
3052 then do anything left in the else blocks. */
3053 start_sequence ();
3054 if (!cond_move_convert_if_block (if_info, then_bb, cond,
3055 &then_vals, &else_vals, false)
3056 || (else_bb
3057 && !cond_move_convert_if_block (if_info, else_bb, cond,
3058 &then_vals, &else_vals, true)))
3060 end_sequence ();
3061 goto done;
3063 seq = end_ifcvt_sequence (if_info);
3064 if (!seq)
3065 goto done;
3067 loc_insn = first_active_insn (then_bb);
3068 if (!loc_insn)
3070 loc_insn = first_active_insn (else_bb);
3071 gcc_assert (loc_insn);
3073 emit_insn_before_setloc (seq, jump, INSN_LOCATION (loc_insn));
3075 if (else_bb)
3077 delete_basic_block (else_bb);
3078 num_true_changes++;
3080 else
3081 remove_edge (find_edge (test_bb, join_bb));
3083 remove_edge (find_edge (then_bb, join_bb));
3084 redirect_edge_and_branch_force (single_succ_edge (test_bb), join_bb);
3085 delete_basic_block (then_bb);
3086 num_true_changes++;
3088 if (can_merge_blocks_p (test_bb, join_bb))
3090 merge_blocks (test_bb, join_bb);
3091 num_true_changes++;
3094 num_updated_if_blocks++;
3096 success_p = TRUE;
3098 done:
3099 then_regs.release ();
3100 else_regs.release ();
3101 return success_p;
3105 /* Determine if a given basic block heads a simple IF-THEN-JOIN or an
3106 IF-THEN-ELSE-JOIN block.
3108 If so, we'll try to convert the insns to not require the branch,
3109 using only transformations that do not require conditional execution.
3111 Return TRUE if we were successful at converting the block. */
3113 static int
3114 noce_find_if_block (basic_block test_bb, edge then_edge, edge else_edge,
3115 int pass)
3117 basic_block then_bb, else_bb, join_bb;
3118 bool then_else_reversed = false;
3119 rtx_insn *jump;
3120 rtx cond;
3121 rtx_insn *cond_earliest;
3122 struct noce_if_info if_info;
3124 /* We only ever should get here before reload. */
3125 gcc_assert (!reload_completed);
3127 /* Recognize an IF-THEN-ELSE-JOIN block. */
3128 if (single_pred_p (then_edge->dest)
3129 && single_succ_p (then_edge->dest)
3130 && single_pred_p (else_edge->dest)
3131 && single_succ_p (else_edge->dest)
3132 && single_succ (then_edge->dest) == single_succ (else_edge->dest))
3134 then_bb = then_edge->dest;
3135 else_bb = else_edge->dest;
3136 join_bb = single_succ (then_bb);
3138 /* Recognize an IF-THEN-JOIN block. */
3139 else if (single_pred_p (then_edge->dest)
3140 && single_succ_p (then_edge->dest)
3141 && single_succ (then_edge->dest) == else_edge->dest)
3143 then_bb = then_edge->dest;
3144 else_bb = NULL_BLOCK;
3145 join_bb = else_edge->dest;
3147 /* Recognize an IF-ELSE-JOIN block. We can have those because the order
3148 of basic blocks in cfglayout mode does not matter, so the fallthrough
3149 edge can go to any basic block (and not just to bb->next_bb, like in
3150 cfgrtl mode). */
3151 else if (single_pred_p (else_edge->dest)
3152 && single_succ_p (else_edge->dest)
3153 && single_succ (else_edge->dest) == then_edge->dest)
3155 /* The noce transformations do not apply to IF-ELSE-JOIN blocks.
3156 To make this work, we have to invert the THEN and ELSE blocks
3157 and reverse the jump condition. */
3158 then_bb = else_edge->dest;
3159 else_bb = NULL_BLOCK;
3160 join_bb = single_succ (then_bb);
3161 then_else_reversed = true;
3163 else
3164 /* Not a form we can handle. */
3165 return FALSE;
3167 /* The edges of the THEN and ELSE blocks cannot have complex edges. */
3168 if (single_succ_edge (then_bb)->flags & EDGE_COMPLEX)
3169 return FALSE;
3170 if (else_bb
3171 && single_succ_edge (else_bb)->flags & EDGE_COMPLEX)
3172 return FALSE;
3174 num_possible_if_blocks++;
3176 if (dump_file)
3178 fprintf (dump_file,
3179 "\nIF-THEN%s-JOIN block found, pass %d, test %d, then %d",
3180 (else_bb) ? "-ELSE" : "",
3181 pass, test_bb->index, then_bb->index);
3183 if (else_bb)
3184 fprintf (dump_file, ", else %d", else_bb->index);
3186 fprintf (dump_file, ", join %d\n", join_bb->index);
3189 /* If the conditional jump is more than just a conditional
3190 jump, then we can not do if-conversion on this block. */
3191 jump = BB_END (test_bb);
3192 if (! onlyjump_p (jump))
3193 return FALSE;
3195 /* If this is not a standard conditional jump, we can't parse it. */
3196 cond = noce_get_condition (jump, &cond_earliest, then_else_reversed);
3197 if (!cond)
3198 return FALSE;
3200 /* We must be comparing objects whose modes imply the size. */
3201 if (GET_MODE (XEXP (cond, 0)) == BLKmode)
3202 return FALSE;
3204 /* Initialize an IF_INFO struct to pass around. */
3205 memset (&if_info, 0, sizeof if_info);
3206 if_info.test_bb = test_bb;
3207 if_info.then_bb = then_bb;
3208 if_info.else_bb = else_bb;
3209 if_info.join_bb = join_bb;
3210 if_info.cond = cond;
3211 if_info.cond_earliest = cond_earliest;
3212 if_info.jump = jump;
3213 if_info.then_else_reversed = then_else_reversed;
3214 if_info.branch_cost = BRANCH_COST (optimize_bb_for_speed_p (test_bb),
3215 predictable_edge_p (then_edge));
3217 /* Do the real work. */
3219 if (noce_process_if_block (&if_info))
3220 return TRUE;
3222 if (HAVE_conditional_move
3223 && cond_move_process_if_block (&if_info))
3224 return TRUE;
3226 return FALSE;
3230 /* Merge the blocks and mark for local life update. */
3232 static void
3233 merge_if_block (struct ce_if_block * ce_info)
3235 basic_block test_bb = ce_info->test_bb; /* last test block */
3236 basic_block then_bb = ce_info->then_bb; /* THEN */
3237 basic_block else_bb = ce_info->else_bb; /* ELSE or NULL */
3238 basic_block join_bb = ce_info->join_bb; /* join block */
3239 basic_block combo_bb;
3241 /* All block merging is done into the lower block numbers. */
3243 combo_bb = test_bb;
3244 df_set_bb_dirty (test_bb);
3246 /* Merge any basic blocks to handle && and || subtests. Each of
3247 the blocks are on the fallthru path from the predecessor block. */
3248 if (ce_info->num_multiple_test_blocks > 0)
3250 basic_block bb = test_bb;
3251 basic_block last_test_bb = ce_info->last_test_bb;
3252 basic_block fallthru = block_fallthru (bb);
3256 bb = fallthru;
3257 fallthru = block_fallthru (bb);
3258 merge_blocks (combo_bb, bb);
3259 num_true_changes++;
3261 while (bb != last_test_bb);
3264 /* Merge TEST block into THEN block. Normally the THEN block won't have a
3265 label, but it might if there were || tests. That label's count should be
3266 zero, and it normally should be removed. */
3268 if (then_bb)
3270 /* If THEN_BB has no successors, then there's a BARRIER after it.
3271 If COMBO_BB has more than one successor (THEN_BB), then that BARRIER
3272 is no longer needed, and in fact it is incorrect to leave it in
3273 the insn stream. */
3274 if (EDGE_COUNT (then_bb->succs) == 0
3275 && EDGE_COUNT (combo_bb->succs) > 1)
3277 rtx_insn *end = NEXT_INSN (BB_END (then_bb));
3278 while (end && NOTE_P (end) && !NOTE_INSN_BASIC_BLOCK_P (end))
3279 end = NEXT_INSN (end);
3281 if (end && BARRIER_P (end))
3282 delete_insn (end);
3284 merge_blocks (combo_bb, then_bb);
3285 num_true_changes++;
3288 /* The ELSE block, if it existed, had a label. That label count
3289 will almost always be zero, but odd things can happen when labels
3290 get their addresses taken. */
3291 if (else_bb)
3293 /* If ELSE_BB has no successors, then there's a BARRIER after it.
3294 If COMBO_BB has more than one successor (ELSE_BB), then that BARRIER
3295 is no longer needed, and in fact it is incorrect to leave it in
3296 the insn stream. */
3297 if (EDGE_COUNT (else_bb->succs) == 0
3298 && EDGE_COUNT (combo_bb->succs) > 1)
3300 rtx_insn *end = NEXT_INSN (BB_END (else_bb));
3301 while (end && NOTE_P (end) && !NOTE_INSN_BASIC_BLOCK_P (end))
3302 end = NEXT_INSN (end);
3304 if (end && BARRIER_P (end))
3305 delete_insn (end);
3307 merge_blocks (combo_bb, else_bb);
3308 num_true_changes++;
3311 /* If there was no join block reported, that means it was not adjacent
3312 to the others, and so we cannot merge them. */
3314 if (! join_bb)
3316 rtx_insn *last = BB_END (combo_bb);
3318 /* The outgoing edge for the current COMBO block should already
3319 be correct. Verify this. */
3320 if (EDGE_COUNT (combo_bb->succs) == 0)
3321 gcc_assert (find_reg_note (last, REG_NORETURN, NULL)
3322 || (NONJUMP_INSN_P (last)
3323 && GET_CODE (PATTERN (last)) == TRAP_IF
3324 && (TRAP_CONDITION (PATTERN (last))
3325 == const_true_rtx)));
3327 else
3328 /* There should still be something at the end of the THEN or ELSE
3329 blocks taking us to our final destination. */
3330 gcc_assert (JUMP_P (last)
3331 || (EDGE_SUCC (combo_bb, 0)->dest
3332 == EXIT_BLOCK_PTR_FOR_FN (cfun)
3333 && CALL_P (last)
3334 && SIBLING_CALL_P (last))
3335 || ((EDGE_SUCC (combo_bb, 0)->flags & EDGE_EH)
3336 && can_throw_internal (last)));
3339 /* The JOIN block may have had quite a number of other predecessors too.
3340 Since we've already merged the TEST, THEN and ELSE blocks, we should
3341 have only one remaining edge from our if-then-else diamond. If there
3342 is more than one remaining edge, it must come from elsewhere. There
3343 may be zero incoming edges if the THEN block didn't actually join
3344 back up (as with a call to a non-return function). */
3345 else if (EDGE_COUNT (join_bb->preds) < 2
3346 && join_bb != EXIT_BLOCK_PTR_FOR_FN (cfun))
3348 /* We can merge the JOIN cleanly and update the dataflow try
3349 again on this pass.*/
3350 merge_blocks (combo_bb, join_bb);
3351 num_true_changes++;
3353 else
3355 /* We cannot merge the JOIN. */
3357 /* The outgoing edge for the current COMBO block should already
3358 be correct. Verify this. */
3359 gcc_assert (single_succ_p (combo_bb)
3360 && single_succ (combo_bb) == join_bb);
3362 /* Remove the jump and cruft from the end of the COMBO block. */
3363 if (join_bb != EXIT_BLOCK_PTR_FOR_FN (cfun))
3364 tidy_fallthru_edge (single_succ_edge (combo_bb));
3367 num_updated_if_blocks++;
3370 /* Find a block ending in a simple IF condition and try to transform it
3371 in some way. When converting a multi-block condition, put the new code
3372 in the first such block and delete the rest. Return a pointer to this
3373 first block if some transformation was done. Return NULL otherwise. */
3375 static basic_block
3376 find_if_header (basic_block test_bb, int pass)
3378 ce_if_block ce_info;
3379 edge then_edge;
3380 edge else_edge;
3382 /* The kind of block we're looking for has exactly two successors. */
3383 if (EDGE_COUNT (test_bb->succs) != 2)
3384 return NULL;
3386 then_edge = EDGE_SUCC (test_bb, 0);
3387 else_edge = EDGE_SUCC (test_bb, 1);
3389 if (df_get_bb_dirty (then_edge->dest))
3390 return NULL;
3391 if (df_get_bb_dirty (else_edge->dest))
3392 return NULL;
3394 /* Neither edge should be abnormal. */
3395 if ((then_edge->flags & EDGE_COMPLEX)
3396 || (else_edge->flags & EDGE_COMPLEX))
3397 return NULL;
3399 /* Nor exit the loop. */
3400 if ((then_edge->flags & EDGE_LOOP_EXIT)
3401 || (else_edge->flags & EDGE_LOOP_EXIT))
3402 return NULL;
3404 /* The THEN edge is canonically the one that falls through. */
3405 if (then_edge->flags & EDGE_FALLTHRU)
3407 else if (else_edge->flags & EDGE_FALLTHRU)
3409 edge e = else_edge;
3410 else_edge = then_edge;
3411 then_edge = e;
3413 else
3414 /* Otherwise this must be a multiway branch of some sort. */
3415 return NULL;
3417 memset (&ce_info, 0, sizeof (ce_info));
3418 ce_info.test_bb = test_bb;
3419 ce_info.then_bb = then_edge->dest;
3420 ce_info.else_bb = else_edge->dest;
3421 ce_info.pass = pass;
3423 #ifdef IFCVT_MACHDEP_INIT
3424 IFCVT_MACHDEP_INIT (&ce_info);
3425 #endif
3427 if (!reload_completed
3428 && noce_find_if_block (test_bb, then_edge, else_edge, pass))
3429 goto success;
3431 if (reload_completed
3432 && targetm.have_conditional_execution ()
3433 && cond_exec_find_if_block (&ce_info))
3434 goto success;
3436 if (HAVE_trap
3437 && optab_handler (ctrap_optab, word_mode) != CODE_FOR_nothing
3438 && find_cond_trap (test_bb, then_edge, else_edge))
3439 goto success;
3441 if (dom_info_state (CDI_POST_DOMINATORS) >= DOM_NO_FAST_QUERY
3442 && (reload_completed || !targetm.have_conditional_execution ()))
3444 if (find_if_case_1 (test_bb, then_edge, else_edge))
3445 goto success;
3446 if (find_if_case_2 (test_bb, then_edge, else_edge))
3447 goto success;
3450 return NULL;
3452 success:
3453 if (dump_file)
3454 fprintf (dump_file, "Conversion succeeded on pass %d.\n", pass);
3455 /* Set this so we continue looking. */
3456 cond_exec_changed_p = TRUE;
3457 return ce_info.test_bb;
3460 /* Return true if a block has two edges, one of which falls through to the next
3461 block, and the other jumps to a specific block, so that we can tell if the
3462 block is part of an && test or an || test. Returns either -1 or the number
3463 of non-note, non-jump, non-USE/CLOBBER insns in the block. */
3465 static int
3466 block_jumps_and_fallthru_p (basic_block cur_bb, basic_block target_bb)
3468 edge cur_edge;
3469 int fallthru_p = FALSE;
3470 int jump_p = FALSE;
3471 rtx_insn *insn;
3472 rtx_insn *end;
3473 int n_insns = 0;
3474 edge_iterator ei;
3476 if (!cur_bb || !target_bb)
3477 return -1;
3479 /* If no edges, obviously it doesn't jump or fallthru. */
3480 if (EDGE_COUNT (cur_bb->succs) == 0)
3481 return FALSE;
3483 FOR_EACH_EDGE (cur_edge, ei, cur_bb->succs)
3485 if (cur_edge->flags & EDGE_COMPLEX)
3486 /* Anything complex isn't what we want. */
3487 return -1;
3489 else if (cur_edge->flags & EDGE_FALLTHRU)
3490 fallthru_p = TRUE;
3492 else if (cur_edge->dest == target_bb)
3493 jump_p = TRUE;
3495 else
3496 return -1;
3499 if ((jump_p & fallthru_p) == 0)
3500 return -1;
3502 /* Don't allow calls in the block, since this is used to group && and ||
3503 together for conditional execution support. ??? we should support
3504 conditional execution support across calls for IA-64 some day, but
3505 for now it makes the code simpler. */
3506 end = BB_END (cur_bb);
3507 insn = BB_HEAD (cur_bb);
3509 while (insn != NULL_RTX)
3511 if (CALL_P (insn))
3512 return -1;
3514 if (INSN_P (insn)
3515 && !JUMP_P (insn)
3516 && !DEBUG_INSN_P (insn)
3517 && GET_CODE (PATTERN (insn)) != USE
3518 && GET_CODE (PATTERN (insn)) != CLOBBER)
3519 n_insns++;
3521 if (insn == end)
3522 break;
3524 insn = NEXT_INSN (insn);
3527 return n_insns;
3530 /* Determine if a given basic block heads a simple IF-THEN or IF-THEN-ELSE
3531 block. If so, we'll try to convert the insns to not require the branch.
3532 Return TRUE if we were successful at converting the block. */
3534 static int
3535 cond_exec_find_if_block (struct ce_if_block * ce_info)
3537 basic_block test_bb = ce_info->test_bb;
3538 basic_block then_bb = ce_info->then_bb;
3539 basic_block else_bb = ce_info->else_bb;
3540 basic_block join_bb = NULL_BLOCK;
3541 edge cur_edge;
3542 basic_block next;
3543 edge_iterator ei;
3545 ce_info->last_test_bb = test_bb;
3547 /* We only ever should get here after reload,
3548 and if we have conditional execution. */
3549 gcc_assert (reload_completed && targetm.have_conditional_execution ());
3551 /* Discover if any fall through predecessors of the current test basic block
3552 were && tests (which jump to the else block) or || tests (which jump to
3553 the then block). */
3554 if (single_pred_p (test_bb)
3555 && single_pred_edge (test_bb)->flags == EDGE_FALLTHRU)
3557 basic_block bb = single_pred (test_bb);
3558 basic_block target_bb;
3559 int max_insns = MAX_CONDITIONAL_EXECUTE;
3560 int n_insns;
3562 /* Determine if the preceding block is an && or || block. */
3563 if ((n_insns = block_jumps_and_fallthru_p (bb, else_bb)) >= 0)
3565 ce_info->and_and_p = TRUE;
3566 target_bb = else_bb;
3568 else if ((n_insns = block_jumps_and_fallthru_p (bb, then_bb)) >= 0)
3570 ce_info->and_and_p = FALSE;
3571 target_bb = then_bb;
3573 else
3574 target_bb = NULL_BLOCK;
3576 if (target_bb && n_insns <= max_insns)
3578 int total_insns = 0;
3579 int blocks = 0;
3581 ce_info->last_test_bb = test_bb;
3583 /* Found at least one && or || block, look for more. */
3586 ce_info->test_bb = test_bb = bb;
3587 total_insns += n_insns;
3588 blocks++;
3590 if (!single_pred_p (bb))
3591 break;
3593 bb = single_pred (bb);
3594 n_insns = block_jumps_and_fallthru_p (bb, target_bb);
3596 while (n_insns >= 0 && (total_insns + n_insns) <= max_insns);
3598 ce_info->num_multiple_test_blocks = blocks;
3599 ce_info->num_multiple_test_insns = total_insns;
3601 if (ce_info->and_and_p)
3602 ce_info->num_and_and_blocks = blocks;
3603 else
3604 ce_info->num_or_or_blocks = blocks;
3608 /* The THEN block of an IF-THEN combo must have exactly one predecessor,
3609 other than any || blocks which jump to the THEN block. */
3610 if ((EDGE_COUNT (then_bb->preds) - ce_info->num_or_or_blocks) != 1)
3611 return FALSE;
3613 /* The edges of the THEN and ELSE blocks cannot have complex edges. */
3614 FOR_EACH_EDGE (cur_edge, ei, then_bb->preds)
3616 if (cur_edge->flags & EDGE_COMPLEX)
3617 return FALSE;
3620 FOR_EACH_EDGE (cur_edge, ei, else_bb->preds)
3622 if (cur_edge->flags & EDGE_COMPLEX)
3623 return FALSE;
3626 /* The THEN block of an IF-THEN combo must have zero or one successors. */
3627 if (EDGE_COUNT (then_bb->succs) > 0
3628 && (!single_succ_p (then_bb)
3629 || (single_succ_edge (then_bb)->flags & EDGE_COMPLEX)
3630 || (epilogue_completed
3631 && tablejump_p (BB_END (then_bb), NULL, NULL))))
3632 return FALSE;
3634 /* If the THEN block has no successors, conditional execution can still
3635 make a conditional call. Don't do this unless the ELSE block has
3636 only one incoming edge -- the CFG manipulation is too ugly otherwise.
3637 Check for the last insn of the THEN block being an indirect jump, which
3638 is listed as not having any successors, but confuses the rest of the CE
3639 code processing. ??? we should fix this in the future. */
3640 if (EDGE_COUNT (then_bb->succs) == 0)
3642 if (single_pred_p (else_bb) && else_bb != EXIT_BLOCK_PTR_FOR_FN (cfun))
3644 rtx_insn *last_insn = BB_END (then_bb);
3646 while (last_insn
3647 && NOTE_P (last_insn)
3648 && last_insn != BB_HEAD (then_bb))
3649 last_insn = PREV_INSN (last_insn);
3651 if (last_insn
3652 && JUMP_P (last_insn)
3653 && ! simplejump_p (last_insn))
3654 return FALSE;
3656 join_bb = else_bb;
3657 else_bb = NULL_BLOCK;
3659 else
3660 return FALSE;
3663 /* If the THEN block's successor is the other edge out of the TEST block,
3664 then we have an IF-THEN combo without an ELSE. */
3665 else if (single_succ (then_bb) == else_bb)
3667 join_bb = else_bb;
3668 else_bb = NULL_BLOCK;
3671 /* If the THEN and ELSE block meet in a subsequent block, and the ELSE
3672 has exactly one predecessor and one successor, and the outgoing edge
3673 is not complex, then we have an IF-THEN-ELSE combo. */
3674 else if (single_succ_p (else_bb)
3675 && single_succ (then_bb) == single_succ (else_bb)
3676 && single_pred_p (else_bb)
3677 && !(single_succ_edge (else_bb)->flags & EDGE_COMPLEX)
3678 && !(epilogue_completed
3679 && tablejump_p (BB_END (else_bb), NULL, NULL)))
3680 join_bb = single_succ (else_bb);
3682 /* Otherwise it is not an IF-THEN or IF-THEN-ELSE combination. */
3683 else
3684 return FALSE;
3686 num_possible_if_blocks++;
3688 if (dump_file)
3690 fprintf (dump_file,
3691 "\nIF-THEN%s block found, pass %d, start block %d "
3692 "[insn %d], then %d [%d]",
3693 (else_bb) ? "-ELSE" : "",
3694 ce_info->pass,
3695 test_bb->index,
3696 BB_HEAD (test_bb) ? (int)INSN_UID (BB_HEAD (test_bb)) : -1,
3697 then_bb->index,
3698 BB_HEAD (then_bb) ? (int)INSN_UID (BB_HEAD (then_bb)) : -1);
3700 if (else_bb)
3701 fprintf (dump_file, ", else %d [%d]",
3702 else_bb->index,
3703 BB_HEAD (else_bb) ? (int)INSN_UID (BB_HEAD (else_bb)) : -1);
3705 fprintf (dump_file, ", join %d [%d]",
3706 join_bb->index,
3707 BB_HEAD (join_bb) ? (int)INSN_UID (BB_HEAD (join_bb)) : -1);
3709 if (ce_info->num_multiple_test_blocks > 0)
3710 fprintf (dump_file, ", %d %s block%s last test %d [%d]",
3711 ce_info->num_multiple_test_blocks,
3712 (ce_info->and_and_p) ? "&&" : "||",
3713 (ce_info->num_multiple_test_blocks == 1) ? "" : "s",
3714 ce_info->last_test_bb->index,
3715 ((BB_HEAD (ce_info->last_test_bb))
3716 ? (int)INSN_UID (BB_HEAD (ce_info->last_test_bb))
3717 : -1));
3719 fputc ('\n', dump_file);
3722 /* Make sure IF, THEN, and ELSE, blocks are adjacent. Actually, we get the
3723 first condition for free, since we've already asserted that there's a
3724 fallthru edge from IF to THEN. Likewise for the && and || blocks, since
3725 we checked the FALLTHRU flag, those are already adjacent to the last IF
3726 block. */
3727 /* ??? As an enhancement, move the ELSE block. Have to deal with
3728 BLOCK notes, if by no other means than backing out the merge if they
3729 exist. Sticky enough I don't want to think about it now. */
3730 next = then_bb;
3731 if (else_bb && (next = next->next_bb) != else_bb)
3732 return FALSE;
3733 if ((next = next->next_bb) != join_bb
3734 && join_bb != EXIT_BLOCK_PTR_FOR_FN (cfun))
3736 if (else_bb)
3737 join_bb = NULL;
3738 else
3739 return FALSE;
3742 /* Do the real work. */
3744 ce_info->else_bb = else_bb;
3745 ce_info->join_bb = join_bb;
3747 /* If we have && and || tests, try to first handle combining the && and ||
3748 tests into the conditional code, and if that fails, go back and handle
3749 it without the && and ||, which at present handles the && case if there
3750 was no ELSE block. */
3751 if (cond_exec_process_if_block (ce_info, TRUE))
3752 return TRUE;
3754 if (ce_info->num_multiple_test_blocks)
3756 cancel_changes (0);
3758 if (cond_exec_process_if_block (ce_info, FALSE))
3759 return TRUE;
3762 return FALSE;
3765 /* Convert a branch over a trap, or a branch
3766 to a trap, into a conditional trap. */
3768 static int
3769 find_cond_trap (basic_block test_bb, edge then_edge, edge else_edge)
3771 basic_block then_bb = then_edge->dest;
3772 basic_block else_bb = else_edge->dest;
3773 basic_block other_bb, trap_bb;
3774 rtx_insn *trap, *jump;
3775 rtx cond, seq;
3776 rtx_insn *cond_earliest;
3777 enum rtx_code code;
3779 /* Locate the block with the trap instruction. */
3780 /* ??? While we look for no successors, we really ought to allow
3781 EH successors. Need to fix merge_if_block for that to work. */
3782 if ((trap = block_has_only_trap (then_bb)) != NULL)
3783 trap_bb = then_bb, other_bb = else_bb;
3784 else if ((trap = block_has_only_trap (else_bb)) != NULL)
3785 trap_bb = else_bb, other_bb = then_bb;
3786 else
3787 return FALSE;
3789 if (dump_file)
3791 fprintf (dump_file, "\nTRAP-IF block found, start %d, trap %d\n",
3792 test_bb->index, trap_bb->index);
3795 /* If this is not a standard conditional jump, we can't parse it. */
3796 jump = BB_END (test_bb);
3797 cond = noce_get_condition (jump, &cond_earliest, false);
3798 if (! cond)
3799 return FALSE;
3801 /* If the conditional jump is more than just a conditional jump, then
3802 we can not do if-conversion on this block. */
3803 if (! onlyjump_p (jump))
3804 return FALSE;
3806 /* We must be comparing objects whose modes imply the size. */
3807 if (GET_MODE (XEXP (cond, 0)) == BLKmode)
3808 return FALSE;
3810 /* Reverse the comparison code, if necessary. */
3811 code = GET_CODE (cond);
3812 if (then_bb == trap_bb)
3814 code = reversed_comparison_code (cond, jump);
3815 if (code == UNKNOWN)
3816 return FALSE;
3819 /* Attempt to generate the conditional trap. */
3820 seq = gen_cond_trap (code, copy_rtx (XEXP (cond, 0)),
3821 copy_rtx (XEXP (cond, 1)),
3822 TRAP_CODE (PATTERN (trap)));
3823 if (seq == NULL)
3824 return FALSE;
3826 /* Emit the new insns before cond_earliest. */
3827 emit_insn_before_setloc (seq, cond_earliest, INSN_LOCATION (trap));
3829 /* Delete the trap block if possible. */
3830 remove_edge (trap_bb == then_bb ? then_edge : else_edge);
3831 df_set_bb_dirty (test_bb);
3832 df_set_bb_dirty (then_bb);
3833 df_set_bb_dirty (else_bb);
3835 if (EDGE_COUNT (trap_bb->preds) == 0)
3837 delete_basic_block (trap_bb);
3838 num_true_changes++;
3841 /* Wire together the blocks again. */
3842 if (current_ir_type () == IR_RTL_CFGLAYOUT)
3843 single_succ_edge (test_bb)->flags |= EDGE_FALLTHRU;
3844 else if (trap_bb == then_bb)
3846 rtx lab;
3847 rtx_insn *newjump;
3849 lab = JUMP_LABEL (jump);
3850 newjump = emit_jump_insn_after (gen_jump (lab), jump);
3851 LABEL_NUSES (lab) += 1;
3852 JUMP_LABEL (newjump) = lab;
3853 emit_barrier_after (newjump);
3855 delete_insn (jump);
3857 if (can_merge_blocks_p (test_bb, other_bb))
3859 merge_blocks (test_bb, other_bb);
3860 num_true_changes++;
3863 num_updated_if_blocks++;
3864 return TRUE;
3867 /* Subroutine of find_cond_trap: if BB contains only a trap insn,
3868 return it. */
3870 static rtx_insn *
3871 block_has_only_trap (basic_block bb)
3873 rtx_insn *trap;
3875 /* We're not the exit block. */
3876 if (bb == EXIT_BLOCK_PTR_FOR_FN (cfun))
3877 return NULL;
3879 /* The block must have no successors. */
3880 if (EDGE_COUNT (bb->succs) > 0)
3881 return NULL;
3883 /* The only instruction in the THEN block must be the trap. */
3884 trap = first_active_insn (bb);
3885 if (! (trap == BB_END (bb)
3886 && GET_CODE (PATTERN (trap)) == TRAP_IF
3887 && TRAP_CONDITION (PATTERN (trap)) == const_true_rtx))
3888 return NULL;
3890 return trap;
3893 /* Look for IF-THEN-ELSE cases in which one of THEN or ELSE is
3894 transformable, but not necessarily the other. There need be no
3895 JOIN block.
3897 Return TRUE if we were successful at converting the block.
3899 Cases we'd like to look at:
3902 if (test) goto over; // x not live
3903 x = a;
3904 goto label;
3905 over:
3907 becomes
3909 x = a;
3910 if (! test) goto label;
3913 if (test) goto E; // x not live
3914 x = big();
3915 goto L;
3917 x = b;
3918 goto M;
3920 becomes
3922 x = b;
3923 if (test) goto M;
3924 x = big();
3925 goto L;
3927 (3) // This one's really only interesting for targets that can do
3928 // multiway branching, e.g. IA-64 BBB bundles. For other targets
3929 // it results in multiple branches on a cache line, which often
3930 // does not sit well with predictors.
3932 if (test1) goto E; // predicted not taken
3933 x = a;
3934 if (test2) goto F;
3937 x = b;
3940 becomes
3942 x = a;
3943 if (test1) goto E;
3944 if (test2) goto F;
3946 Notes:
3948 (A) Don't do (2) if the branch is predicted against the block we're
3949 eliminating. Do it anyway if we can eliminate a branch; this requires
3950 that the sole successor of the eliminated block postdominate the other
3951 side of the if.
3953 (B) With CE, on (3) we can steal from both sides of the if, creating
3955 if (test1) x = a;
3956 if (!test1) x = b;
3957 if (test1) goto J;
3958 if (test2) goto F;
3962 Again, this is most useful if J postdominates.
3964 (C) CE substitutes for helpful life information.
3966 (D) These heuristics need a lot of work. */
3968 /* Tests for case 1 above. */
3970 static int
3971 find_if_case_1 (basic_block test_bb, edge then_edge, edge else_edge)
3973 basic_block then_bb = then_edge->dest;
3974 basic_block else_bb = else_edge->dest;
3975 basic_block new_bb;
3976 int then_bb_index, then_prob;
3977 rtx else_target = NULL_RTX;
3979 /* If we are partitioning hot/cold basic blocks, we don't want to
3980 mess up unconditional or indirect jumps that cross between hot
3981 and cold sections.
3983 Basic block partitioning may result in some jumps that appear to
3984 be optimizable (or blocks that appear to be mergeable), but which really
3985 must be left untouched (they are required to make it safely across
3986 partition boundaries). See the comments at the top of
3987 bb-reorder.c:partition_hot_cold_basic_blocks for complete details. */
3989 if ((BB_END (then_bb)
3990 && JUMP_P (BB_END (then_bb))
3991 && CROSSING_JUMP_P (BB_END (then_bb)))
3992 || (BB_END (test_bb)
3993 && JUMP_P (BB_END (test_bb))
3994 && CROSSING_JUMP_P (BB_END (test_bb)))
3995 || (BB_END (else_bb)
3996 && JUMP_P (BB_END (else_bb))
3997 && CROSSING_JUMP_P (BB_END (else_bb))))
3998 return FALSE;
4000 /* THEN has one successor. */
4001 if (!single_succ_p (then_bb))
4002 return FALSE;
4004 /* THEN does not fall through, but is not strange either. */
4005 if (single_succ_edge (then_bb)->flags & (EDGE_COMPLEX | EDGE_FALLTHRU))
4006 return FALSE;
4008 /* THEN has one predecessor. */
4009 if (!single_pred_p (then_bb))
4010 return FALSE;
4012 /* THEN must do something. */
4013 if (forwarder_block_p (then_bb))
4014 return FALSE;
4016 num_possible_if_blocks++;
4017 if (dump_file)
4018 fprintf (dump_file,
4019 "\nIF-CASE-1 found, start %d, then %d\n",
4020 test_bb->index, then_bb->index);
4022 if (then_edge->probability)
4023 then_prob = REG_BR_PROB_BASE - then_edge->probability;
4024 else
4025 then_prob = REG_BR_PROB_BASE / 2;
4027 /* We're speculating from the THEN path, we want to make sure the cost
4028 of speculation is within reason. */
4029 if (! cheap_bb_rtx_cost_p (then_bb, then_prob,
4030 COSTS_N_INSNS (BRANCH_COST (optimize_bb_for_speed_p (then_edge->src),
4031 predictable_edge_p (then_edge)))))
4032 return FALSE;
4034 if (else_bb == EXIT_BLOCK_PTR_FOR_FN (cfun))
4036 rtx_insn *jump = BB_END (else_edge->src);
4037 gcc_assert (JUMP_P (jump));
4038 else_target = JUMP_LABEL (jump);
4041 /* Registers set are dead, or are predicable. */
4042 if (! dead_or_predicable (test_bb, then_bb, else_bb,
4043 single_succ_edge (then_bb), 1))
4044 return FALSE;
4046 /* Conversion went ok, including moving the insns and fixing up the
4047 jump. Adjust the CFG to match. */
4049 /* We can avoid creating a new basic block if then_bb is immediately
4050 followed by else_bb, i.e. deleting then_bb allows test_bb to fall
4051 through to else_bb. */
4053 if (then_bb->next_bb == else_bb
4054 && then_bb->prev_bb == test_bb
4055 && else_bb != EXIT_BLOCK_PTR_FOR_FN (cfun))
4057 redirect_edge_succ (FALLTHRU_EDGE (test_bb), else_bb);
4058 new_bb = 0;
4060 else if (else_bb == EXIT_BLOCK_PTR_FOR_FN (cfun))
4061 new_bb = force_nonfallthru_and_redirect (FALLTHRU_EDGE (test_bb),
4062 else_bb, else_target);
4063 else
4064 new_bb = redirect_edge_and_branch_force (FALLTHRU_EDGE (test_bb),
4065 else_bb);
4067 df_set_bb_dirty (test_bb);
4068 df_set_bb_dirty (else_bb);
4070 then_bb_index = then_bb->index;
4071 delete_basic_block (then_bb);
4073 /* Make rest of code believe that the newly created block is the THEN_BB
4074 block we removed. */
4075 if (new_bb)
4077 df_bb_replace (then_bb_index, new_bb);
4078 /* This should have been done above via force_nonfallthru_and_redirect
4079 (possibly called from redirect_edge_and_branch_force). */
4080 gcc_checking_assert (BB_PARTITION (new_bb) == BB_PARTITION (test_bb));
4083 num_true_changes++;
4084 num_updated_if_blocks++;
4086 return TRUE;
4089 /* Test for case 2 above. */
4091 static int
4092 find_if_case_2 (basic_block test_bb, edge then_edge, edge else_edge)
4094 basic_block then_bb = then_edge->dest;
4095 basic_block else_bb = else_edge->dest;
4096 edge else_succ;
4097 int then_prob, else_prob;
4099 /* We do not want to speculate (empty) loop latches. */
4100 if (current_loops
4101 && else_bb->loop_father->latch == else_bb)
4102 return FALSE;
4104 /* If we are partitioning hot/cold basic blocks, we don't want to
4105 mess up unconditional or indirect jumps that cross between hot
4106 and cold sections.
4108 Basic block partitioning may result in some jumps that appear to
4109 be optimizable (or blocks that appear to be mergeable), but which really
4110 must be left untouched (they are required to make it safely across
4111 partition boundaries). See the comments at the top of
4112 bb-reorder.c:partition_hot_cold_basic_blocks for complete details. */
4114 if ((BB_END (then_bb)
4115 && JUMP_P (BB_END (then_bb))
4116 && CROSSING_JUMP_P (BB_END (then_bb)))
4117 || (BB_END (test_bb)
4118 && JUMP_P (BB_END (test_bb))
4119 && CROSSING_JUMP_P (BB_END (test_bb)))
4120 || (BB_END (else_bb)
4121 && JUMP_P (BB_END (else_bb))
4122 && CROSSING_JUMP_P (BB_END (else_bb))))
4123 return FALSE;
4125 /* ELSE has one successor. */
4126 if (!single_succ_p (else_bb))
4127 return FALSE;
4128 else
4129 else_succ = single_succ_edge (else_bb);
4131 /* ELSE outgoing edge is not complex. */
4132 if (else_succ->flags & EDGE_COMPLEX)
4133 return FALSE;
4135 /* ELSE has one predecessor. */
4136 if (!single_pred_p (else_bb))
4137 return FALSE;
4139 /* THEN is not EXIT. */
4140 if (then_bb->index < NUM_FIXED_BLOCKS)
4141 return FALSE;
4143 if (else_edge->probability)
4145 else_prob = else_edge->probability;
4146 then_prob = REG_BR_PROB_BASE - else_prob;
4148 else
4150 else_prob = REG_BR_PROB_BASE / 2;
4151 then_prob = REG_BR_PROB_BASE / 2;
4154 /* ELSE is predicted or SUCC(ELSE) postdominates THEN. */
4155 if (else_prob > then_prob)
4157 else if (else_succ->dest->index < NUM_FIXED_BLOCKS
4158 || dominated_by_p (CDI_POST_DOMINATORS, then_bb,
4159 else_succ->dest))
4161 else
4162 return FALSE;
4164 num_possible_if_blocks++;
4165 if (dump_file)
4166 fprintf (dump_file,
4167 "\nIF-CASE-2 found, start %d, else %d\n",
4168 test_bb->index, else_bb->index);
4170 /* We're speculating from the ELSE path, we want to make sure the cost
4171 of speculation is within reason. */
4172 if (! cheap_bb_rtx_cost_p (else_bb, else_prob,
4173 COSTS_N_INSNS (BRANCH_COST (optimize_bb_for_speed_p (else_edge->src),
4174 predictable_edge_p (else_edge)))))
4175 return FALSE;
4177 /* Registers set are dead, or are predicable. */
4178 if (! dead_or_predicable (test_bb, else_bb, then_bb, else_succ, 0))
4179 return FALSE;
4181 /* Conversion went ok, including moving the insns and fixing up the
4182 jump. Adjust the CFG to match. */
4184 df_set_bb_dirty (test_bb);
4185 df_set_bb_dirty (then_bb);
4186 delete_basic_block (else_bb);
4188 num_true_changes++;
4189 num_updated_if_blocks++;
4191 /* ??? We may now fallthru from one of THEN's successors into a join
4192 block. Rerun cleanup_cfg? Examine things manually? Wait? */
4194 return TRUE;
4197 /* Used by the code above to perform the actual rtl transformations.
4198 Return TRUE if successful.
4200 TEST_BB is the block containing the conditional branch. MERGE_BB
4201 is the block containing the code to manipulate. DEST_EDGE is an
4202 edge representing a jump to the join block; after the conversion,
4203 TEST_BB should be branching to its destination.
4204 REVERSEP is true if the sense of the branch should be reversed. */
4206 static int
4207 dead_or_predicable (basic_block test_bb, basic_block merge_bb,
4208 basic_block other_bb, edge dest_edge, int reversep)
4210 basic_block new_dest = dest_edge->dest;
4211 rtx_insn *head, *end, *jump;
4212 rtx_insn *earliest = NULL;
4213 rtx old_dest;
4214 bitmap merge_set = NULL;
4215 /* Number of pending changes. */
4216 int n_validated_changes = 0;
4217 rtx new_dest_label = NULL_RTX;
4219 jump = BB_END (test_bb);
4221 /* Find the extent of the real code in the merge block. */
4222 head = BB_HEAD (merge_bb);
4223 end = BB_END (merge_bb);
4225 while (DEBUG_INSN_P (end) && end != head)
4226 end = PREV_INSN (end);
4228 /* If merge_bb ends with a tablejump, predicating/moving insn's
4229 into test_bb and then deleting merge_bb will result in the jumptable
4230 that follows merge_bb being removed along with merge_bb and then we
4231 get an unresolved reference to the jumptable. */
4232 if (tablejump_p (end, NULL, NULL))
4233 return FALSE;
4235 if (LABEL_P (head))
4236 head = NEXT_INSN (head);
4237 while (DEBUG_INSN_P (head) && head != end)
4238 head = NEXT_INSN (head);
4239 if (NOTE_P (head))
4241 if (head == end)
4243 head = end = NULL;
4244 goto no_body;
4246 head = NEXT_INSN (head);
4247 while (DEBUG_INSN_P (head) && head != end)
4248 head = NEXT_INSN (head);
4251 if (JUMP_P (end))
4253 if (!onlyjump_p (end))
4254 return FALSE;
4255 if (head == end)
4257 head = end = NULL;
4258 goto no_body;
4260 end = PREV_INSN (end);
4261 while (DEBUG_INSN_P (end) && end != head)
4262 end = PREV_INSN (end);
4265 /* Don't move frame-related insn across the conditional branch. This
4266 can lead to one of the paths of the branch having wrong unwind info. */
4267 if (epilogue_completed)
4269 rtx_insn *insn = head;
4270 while (1)
4272 if (INSN_P (insn) && RTX_FRAME_RELATED_P (insn))
4273 return FALSE;
4274 if (insn == end)
4275 break;
4276 insn = NEXT_INSN (insn);
4280 /* Disable handling dead code by conditional execution if the machine needs
4281 to do anything funny with the tests, etc. */
4282 #ifndef IFCVT_MODIFY_TESTS
4283 if (targetm.have_conditional_execution ())
4285 /* In the conditional execution case, we have things easy. We know
4286 the condition is reversible. We don't have to check life info
4287 because we're going to conditionally execute the code anyway.
4288 All that's left is making sure the insns involved can actually
4289 be predicated. */
4291 rtx cond;
4293 cond = cond_exec_get_condition (jump);
4294 if (! cond)
4295 return FALSE;
4297 rtx note = find_reg_note (jump, REG_BR_PROB, NULL_RTX);
4298 int prob_val = (note ? XINT (note, 0) : -1);
4300 if (reversep)
4302 enum rtx_code rev = reversed_comparison_code (cond, jump);
4303 if (rev == UNKNOWN)
4304 return FALSE;
4305 cond = gen_rtx_fmt_ee (rev, GET_MODE (cond), XEXP (cond, 0),
4306 XEXP (cond, 1));
4307 if (prob_val >= 0)
4308 prob_val = REG_BR_PROB_BASE - prob_val;
4311 if (cond_exec_process_insns (NULL, head, end, cond, prob_val, 0)
4312 && verify_changes (0))
4313 n_validated_changes = num_validated_changes ();
4314 else
4315 cancel_changes (0);
4317 earliest = jump;
4319 #endif
4321 /* If we allocated new pseudos (e.g. in the conditional move
4322 expander called from noce_emit_cmove), we must resize the
4323 array first. */
4324 if (max_regno < max_reg_num ())
4325 max_regno = max_reg_num ();
4327 /* Try the NCE path if the CE path did not result in any changes. */
4328 if (n_validated_changes == 0)
4330 rtx cond;
4331 rtx_insn *insn;
4332 regset live;
4333 bool success;
4335 /* In the non-conditional execution case, we have to verify that there
4336 are no trapping operations, no calls, no references to memory, and
4337 that any registers modified are dead at the branch site. */
4339 if (!any_condjump_p (jump))
4340 return FALSE;
4342 /* Find the extent of the conditional. */
4343 cond = noce_get_condition (jump, &earliest, false);
4344 if (!cond)
4345 return FALSE;
4347 live = BITMAP_ALLOC (&reg_obstack);
4348 simulate_backwards_to_point (merge_bb, live, end);
4349 success = can_move_insns_across (head, end, earliest, jump,
4350 merge_bb, live,
4351 df_get_live_in (other_bb), NULL);
4352 BITMAP_FREE (live);
4353 if (!success)
4354 return FALSE;
4356 /* Collect the set of registers set in MERGE_BB. */
4357 merge_set = BITMAP_ALLOC (&reg_obstack);
4359 FOR_BB_INSNS (merge_bb, insn)
4360 if (NONDEBUG_INSN_P (insn))
4361 df_simulate_find_defs (insn, merge_set);
4363 /* If shrink-wrapping, disable this optimization when test_bb is
4364 the first basic block and merge_bb exits. The idea is to not
4365 move code setting up a return register as that may clobber a
4366 register used to pass function parameters, which then must be
4367 saved in caller-saved regs. A caller-saved reg requires the
4368 prologue, killing a shrink-wrap opportunity. */
4369 if ((SHRINK_WRAPPING_ENABLED && !epilogue_completed)
4370 && ENTRY_BLOCK_PTR_FOR_FN (cfun)->next_bb == test_bb
4371 && single_succ_p (new_dest)
4372 && single_succ (new_dest) == EXIT_BLOCK_PTR_FOR_FN (cfun)
4373 && bitmap_intersect_p (df_get_live_in (new_dest), merge_set))
4375 regset return_regs;
4376 unsigned int i;
4378 return_regs = BITMAP_ALLOC (&reg_obstack);
4380 /* Start off with the intersection of regs used to pass
4381 params and regs used to return values. */
4382 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
4383 if (FUNCTION_ARG_REGNO_P (i)
4384 && targetm.calls.function_value_regno_p (i))
4385 bitmap_set_bit (return_regs, INCOMING_REGNO (i));
4387 bitmap_and_into (return_regs,
4388 df_get_live_out (ENTRY_BLOCK_PTR_FOR_FN (cfun)));
4389 bitmap_and_into (return_regs,
4390 df_get_live_in (EXIT_BLOCK_PTR_FOR_FN (cfun)));
4391 if (!bitmap_empty_p (return_regs))
4393 FOR_BB_INSNS_REVERSE (new_dest, insn)
4394 if (NONDEBUG_INSN_P (insn))
4396 df_ref def;
4398 /* If this insn sets any reg in return_regs, add all
4399 reg uses to the set of regs we're interested in. */
4400 FOR_EACH_INSN_DEF (def, insn)
4401 if (bitmap_bit_p (return_regs, DF_REF_REGNO (def)))
4403 df_simulate_uses (insn, return_regs);
4404 break;
4407 if (bitmap_intersect_p (merge_set, return_regs))
4409 BITMAP_FREE (return_regs);
4410 BITMAP_FREE (merge_set);
4411 return FALSE;
4414 BITMAP_FREE (return_regs);
4418 no_body:
4419 /* We don't want to use normal invert_jump or redirect_jump because
4420 we don't want to delete_insn called. Also, we want to do our own
4421 change group management. */
4423 old_dest = JUMP_LABEL (jump);
4424 if (other_bb != new_dest)
4426 if (!any_condjump_p (jump))
4427 goto cancel;
4429 if (JUMP_P (BB_END (dest_edge->src)))
4430 new_dest_label = JUMP_LABEL (BB_END (dest_edge->src));
4431 else if (new_dest == EXIT_BLOCK_PTR_FOR_FN (cfun))
4432 new_dest_label = ret_rtx;
4433 else
4434 new_dest_label = block_label (new_dest);
4436 rtx_jump_insn *jump_insn = as_a <rtx_jump_insn *> (jump);
4437 if (reversep
4438 ? ! invert_jump_1 (jump_insn, new_dest_label)
4439 : ! redirect_jump_1 (jump_insn, new_dest_label))
4440 goto cancel;
4443 if (verify_changes (n_validated_changes))
4444 confirm_change_group ();
4445 else
4446 goto cancel;
4448 if (other_bb != new_dest)
4450 redirect_jump_2 (as_a <rtx_jump_insn *> (jump), old_dest, new_dest_label,
4451 0, reversep);
4453 redirect_edge_succ (BRANCH_EDGE (test_bb), new_dest);
4454 if (reversep)
4456 std::swap (BRANCH_EDGE (test_bb)->count,
4457 FALLTHRU_EDGE (test_bb)->count);
4458 std::swap (BRANCH_EDGE (test_bb)->probability,
4459 FALLTHRU_EDGE (test_bb)->probability);
4460 update_br_prob_note (test_bb);
4464 /* Move the insns out of MERGE_BB to before the branch. */
4465 if (head != NULL)
4467 rtx_insn *insn;
4469 if (end == BB_END (merge_bb))
4470 BB_END (merge_bb) = PREV_INSN (head);
4472 /* PR 21767: when moving insns above a conditional branch, the REG_EQUAL
4473 notes being moved might become invalid. */
4474 insn = head;
4477 rtx note;
4479 if (! INSN_P (insn))
4480 continue;
4481 note = find_reg_note (insn, REG_EQUAL, NULL_RTX);
4482 if (! note)
4483 continue;
4484 remove_note (insn, note);
4485 } while (insn != end && (insn = NEXT_INSN (insn)));
4487 /* PR46315: when moving insns above a conditional branch, the REG_EQUAL
4488 notes referring to the registers being set might become invalid. */
4489 if (merge_set)
4491 unsigned i;
4492 bitmap_iterator bi;
4494 EXECUTE_IF_SET_IN_BITMAP (merge_set, 0, i, bi)
4495 remove_reg_equal_equiv_notes_for_regno (i);
4497 BITMAP_FREE (merge_set);
4500 reorder_insns (head, end, PREV_INSN (earliest));
4503 /* Remove the jump and edge if we can. */
4504 if (other_bb == new_dest)
4506 delete_insn (jump);
4507 remove_edge (BRANCH_EDGE (test_bb));
4508 /* ??? Can't merge blocks here, as then_bb is still in use.
4509 At minimum, the merge will get done just before bb-reorder. */
4512 return TRUE;
4514 cancel:
4515 cancel_changes (0);
4517 if (merge_set)
4518 BITMAP_FREE (merge_set);
4520 return FALSE;
4523 /* Main entry point for all if-conversion. AFTER_COMBINE is true if
4524 we are after combine pass. */
4526 static void
4527 if_convert (bool after_combine)
4529 basic_block bb;
4530 int pass;
4532 if (optimize == 1)
4534 df_live_add_problem ();
4535 df_live_set_all_dirty ();
4538 /* Record whether we are after combine pass. */
4539 ifcvt_after_combine = after_combine;
4540 num_possible_if_blocks = 0;
4541 num_updated_if_blocks = 0;
4542 num_true_changes = 0;
4544 loop_optimizer_init (AVOID_CFG_MODIFICATIONS);
4545 mark_loop_exit_edges ();
4546 loop_optimizer_finalize ();
4547 free_dominance_info (CDI_DOMINATORS);
4549 /* Compute postdominators. */
4550 calculate_dominance_info (CDI_POST_DOMINATORS);
4552 df_set_flags (DF_LR_RUN_DCE);
4554 /* Go through each of the basic blocks looking for things to convert. If we
4555 have conditional execution, we make multiple passes to allow us to handle
4556 IF-THEN{-ELSE} blocks within other IF-THEN{-ELSE} blocks. */
4557 pass = 0;
4560 df_analyze ();
4561 /* Only need to do dce on the first pass. */
4562 df_clear_flags (DF_LR_RUN_DCE);
4563 cond_exec_changed_p = FALSE;
4564 pass++;
4566 #ifdef IFCVT_MULTIPLE_DUMPS
4567 if (dump_file && pass > 1)
4568 fprintf (dump_file, "\n\n========== Pass %d ==========\n", pass);
4569 #endif
4571 FOR_EACH_BB_FN (bb, cfun)
4573 basic_block new_bb;
4574 while (!df_get_bb_dirty (bb)
4575 && (new_bb = find_if_header (bb, pass)) != NULL)
4576 bb = new_bb;
4579 #ifdef IFCVT_MULTIPLE_DUMPS
4580 if (dump_file && cond_exec_changed_p)
4581 print_rtl_with_bb (dump_file, get_insns (), dump_flags);
4582 #endif
4584 while (cond_exec_changed_p);
4586 #ifdef IFCVT_MULTIPLE_DUMPS
4587 if (dump_file)
4588 fprintf (dump_file, "\n\n========== no more changes\n");
4589 #endif
4591 free_dominance_info (CDI_POST_DOMINATORS);
4593 if (dump_file)
4594 fflush (dump_file);
4596 clear_aux_for_blocks ();
4598 /* If we allocated new pseudos, we must resize the array for sched1. */
4599 if (max_regno < max_reg_num ())
4600 max_regno = max_reg_num ();
4602 /* Write the final stats. */
4603 if (dump_file && num_possible_if_blocks > 0)
4605 fprintf (dump_file,
4606 "\n%d possible IF blocks searched.\n",
4607 num_possible_if_blocks);
4608 fprintf (dump_file,
4609 "%d IF blocks converted.\n",
4610 num_updated_if_blocks);
4611 fprintf (dump_file,
4612 "%d true changes made.\n\n\n",
4613 num_true_changes);
4616 if (optimize == 1)
4617 df_remove_problem (df_live);
4619 #ifdef ENABLE_CHECKING
4620 verify_flow_info ();
4621 #endif
4624 /* If-conversion and CFG cleanup. */
4625 static unsigned int
4626 rest_of_handle_if_conversion (void)
4628 if (flag_if_conversion)
4630 if (dump_file)
4632 dump_reg_info (dump_file);
4633 dump_flow_info (dump_file, dump_flags);
4635 cleanup_cfg (CLEANUP_EXPENSIVE);
4636 if_convert (false);
4639 cleanup_cfg (0);
4640 return 0;
4643 namespace {
4645 const pass_data pass_data_rtl_ifcvt =
4647 RTL_PASS, /* type */
4648 "ce1", /* name */
4649 OPTGROUP_NONE, /* optinfo_flags */
4650 TV_IFCVT, /* tv_id */
4651 0, /* properties_required */
4652 0, /* properties_provided */
4653 0, /* properties_destroyed */
4654 0, /* todo_flags_start */
4655 TODO_df_finish, /* todo_flags_finish */
4658 class pass_rtl_ifcvt : public rtl_opt_pass
4660 public:
4661 pass_rtl_ifcvt (gcc::context *ctxt)
4662 : rtl_opt_pass (pass_data_rtl_ifcvt, ctxt)
4665 /* opt_pass methods: */
4666 virtual bool gate (function *)
4668 return (optimize > 0) && dbg_cnt (if_conversion);
4671 virtual unsigned int execute (function *)
4673 return rest_of_handle_if_conversion ();
4676 }; // class pass_rtl_ifcvt
4678 } // anon namespace
4680 rtl_opt_pass *
4681 make_pass_rtl_ifcvt (gcc::context *ctxt)
4683 return new pass_rtl_ifcvt (ctxt);
4687 /* Rerun if-conversion, as combine may have simplified things enough
4688 to now meet sequence length restrictions. */
4690 namespace {
4692 const pass_data pass_data_if_after_combine =
4694 RTL_PASS, /* type */
4695 "ce2", /* name */
4696 OPTGROUP_NONE, /* optinfo_flags */
4697 TV_IFCVT, /* tv_id */
4698 0, /* properties_required */
4699 0, /* properties_provided */
4700 0, /* properties_destroyed */
4701 0, /* todo_flags_start */
4702 TODO_df_finish, /* todo_flags_finish */
4705 class pass_if_after_combine : public rtl_opt_pass
4707 public:
4708 pass_if_after_combine (gcc::context *ctxt)
4709 : rtl_opt_pass (pass_data_if_after_combine, ctxt)
4712 /* opt_pass methods: */
4713 virtual bool gate (function *)
4715 return optimize > 0 && flag_if_conversion
4716 && dbg_cnt (if_after_combine);
4719 virtual unsigned int execute (function *)
4721 if_convert (true);
4722 return 0;
4725 }; // class pass_if_after_combine
4727 } // anon namespace
4729 rtl_opt_pass *
4730 make_pass_if_after_combine (gcc::context *ctxt)
4732 return new pass_if_after_combine (ctxt);
4736 namespace {
4738 const pass_data pass_data_if_after_reload =
4740 RTL_PASS, /* type */
4741 "ce3", /* name */
4742 OPTGROUP_NONE, /* optinfo_flags */
4743 TV_IFCVT2, /* tv_id */
4744 0, /* properties_required */
4745 0, /* properties_provided */
4746 0, /* properties_destroyed */
4747 0, /* todo_flags_start */
4748 TODO_df_finish, /* todo_flags_finish */
4751 class pass_if_after_reload : public rtl_opt_pass
4753 public:
4754 pass_if_after_reload (gcc::context *ctxt)
4755 : rtl_opt_pass (pass_data_if_after_reload, ctxt)
4758 /* opt_pass methods: */
4759 virtual bool gate (function *)
4761 return optimize > 0 && flag_if_conversion2
4762 && dbg_cnt (if_after_reload);
4765 virtual unsigned int execute (function *)
4767 if_convert (true);
4768 return 0;
4771 }; // class pass_if_after_reload
4773 } // anon namespace
4775 rtl_opt_pass *
4776 make_pass_if_after_reload (gcc::context *ctxt)
4778 return new pass_if_after_reload (ctxt);