compiler: Check for initialization cycles in bound method expressions.
[official-gcc.git] / gcc / ifcvt.c
blob949d2b40ba51f1e07882b77b18f2f93e8cdce734
1 /* If-conversion support.
2 Copyright (C) 2000-2014 Free Software Foundation, Inc.
4 This file is part of GCC.
6 GCC is free software; you can redistribute it and/or modify it
7 under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 3, or (at your option)
9 any later version.
11 GCC is distributed in the hope that it will be useful, but WITHOUT
12 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
13 or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
14 License for more details.
16 You should have received a copy of the GNU General Public License
17 along with GCC; see the file COPYING3. If not see
18 <http://www.gnu.org/licenses/>. */
20 #include "config.h"
21 #include "system.h"
22 #include "coretypes.h"
23 #include "tm.h"
25 #include "rtl.h"
26 #include "regs.h"
27 #include "hashtab.h"
28 #include "hash-set.h"
29 #include "vec.h"
30 #include "machmode.h"
31 #include "hard-reg-set.h"
32 #include "input.h"
33 #include "function.h"
34 #include "flags.h"
35 #include "insn-config.h"
36 #include "recog.h"
37 #include "except.h"
38 #include "basic-block.h"
39 #include "expr.h"
40 #include "output.h"
41 #include "optabs.h"
42 #include "diagnostic-core.h"
43 #include "tm_p.h"
44 #include "cfgloop.h"
45 #include "target.h"
46 #include "tree-pass.h"
47 #include "df.h"
48 #include "dbgcnt.h"
49 #include "shrink-wrap.h"
51 #ifndef HAVE_conditional_move
52 #define HAVE_conditional_move 0
53 #endif
54 #ifndef HAVE_incscc
55 #define HAVE_incscc 0
56 #endif
57 #ifndef HAVE_decscc
58 #define HAVE_decscc 0
59 #endif
60 #ifndef HAVE_trap
61 #define HAVE_trap 0
62 #endif
64 #ifndef MAX_CONDITIONAL_EXECUTE
65 #define MAX_CONDITIONAL_EXECUTE \
66 (BRANCH_COST (optimize_function_for_speed_p (cfun), false) \
67 + 1)
68 #endif
70 #define IFCVT_MULTIPLE_DUMPS 1
72 #define NULL_BLOCK ((basic_block) NULL)
74 /* True if after combine pass. */
75 static bool ifcvt_after_combine;
77 /* # of IF-THEN or IF-THEN-ELSE blocks we looked at */
78 static int num_possible_if_blocks;
80 /* # of IF-THEN or IF-THEN-ELSE blocks were converted to conditional
81 execution. */
82 static int num_updated_if_blocks;
84 /* # of changes made. */
85 static int num_true_changes;
87 /* Whether conditional execution changes were made. */
88 static int cond_exec_changed_p;
90 /* Forward references. */
91 static int count_bb_insns (const_basic_block);
92 static bool cheap_bb_rtx_cost_p (const_basic_block, int, int);
93 static rtx_insn *first_active_insn (basic_block);
94 static rtx_insn *last_active_insn (basic_block, int);
95 static rtx_insn *find_active_insn_before (basic_block, rtx_insn *);
96 static rtx_insn *find_active_insn_after (basic_block, rtx_insn *);
97 static basic_block block_fallthru (basic_block);
98 static int cond_exec_process_insns (ce_if_block *, rtx_insn *, rtx, rtx, int,
99 int);
100 static rtx cond_exec_get_condition (rtx_insn *);
101 static rtx noce_get_condition (rtx_insn *, rtx_insn **, bool);
102 static int noce_operand_ok (const_rtx);
103 static void merge_if_block (ce_if_block *);
104 static int find_cond_trap (basic_block, edge, edge);
105 static basic_block find_if_header (basic_block, int);
106 static int block_jumps_and_fallthru_p (basic_block, basic_block);
107 static int noce_find_if_block (basic_block, edge, edge, int);
108 static int cond_exec_find_if_block (ce_if_block *);
109 static int find_if_case_1 (basic_block, edge, edge);
110 static int find_if_case_2 (basic_block, edge, edge);
111 static int dead_or_predicable (basic_block, basic_block, basic_block,
112 edge, int);
113 static void noce_emit_move_insn (rtx, rtx);
114 static rtx_insn *block_has_only_trap (basic_block);
116 /* Count the number of non-jump active insns in BB. */
118 static int
119 count_bb_insns (const_basic_block bb)
121 int count = 0;
122 rtx_insn *insn = BB_HEAD (bb);
124 while (1)
126 if (active_insn_p (insn) && !JUMP_P (insn))
127 count++;
129 if (insn == BB_END (bb))
130 break;
131 insn = NEXT_INSN (insn);
134 return count;
137 /* Determine whether the total insn_rtx_cost on non-jump insns in
138 basic block BB is less than MAX_COST. This function returns
139 false if the cost of any instruction could not be estimated.
141 The cost of the non-jump insns in BB is scaled by REG_BR_PROB_BASE
142 as those insns are being speculated. MAX_COST is scaled with SCALE
143 plus a small fudge factor. */
145 static bool
146 cheap_bb_rtx_cost_p (const_basic_block bb, int scale, int max_cost)
148 int count = 0;
149 rtx_insn *insn = BB_HEAD (bb);
150 bool speed = optimize_bb_for_speed_p (bb);
152 /* Set scale to REG_BR_PROB_BASE to void the identical scaling
153 applied to insn_rtx_cost when optimizing for size. Only do
154 this after combine because if-conversion might interfere with
155 passes before combine.
157 Use optimize_function_for_speed_p instead of the pre-defined
158 variable speed to make sure it is set to same value for all
159 basic blocks in one if-conversion transformation. */
160 if (!optimize_function_for_speed_p (cfun) && ifcvt_after_combine)
161 scale = REG_BR_PROB_BASE;
162 /* Our branch probability/scaling factors are just estimates and don't
163 account for cases where we can get speculation for free and other
164 secondary benefits. So we fudge the scale factor to make speculating
165 appear a little more profitable when optimizing for performance. */
166 else
167 scale += REG_BR_PROB_BASE / 8;
170 max_cost *= scale;
172 while (1)
174 if (NONJUMP_INSN_P (insn))
176 int cost = insn_rtx_cost (PATTERN (insn), speed) * REG_BR_PROB_BASE;
177 if (cost == 0)
178 return false;
180 /* If this instruction is the load or set of a "stack" register,
181 such as a floating point register on x87, then the cost of
182 speculatively executing this insn may need to include
183 the additional cost of popping its result off of the
184 register stack. Unfortunately, correctly recognizing and
185 accounting for this additional overhead is tricky, so for
186 now we simply prohibit such speculative execution. */
187 #ifdef STACK_REGS
189 rtx set = single_set (insn);
190 if (set && STACK_REG_P (SET_DEST (set)))
191 return false;
193 #endif
195 count += cost;
196 if (count >= max_cost)
197 return false;
199 else if (CALL_P (insn))
200 return false;
202 if (insn == BB_END (bb))
203 break;
204 insn = NEXT_INSN (insn);
207 return true;
210 /* Return the first non-jump active insn in the basic block. */
212 static rtx_insn *
213 first_active_insn (basic_block bb)
215 rtx_insn *insn = BB_HEAD (bb);
217 if (LABEL_P (insn))
219 if (insn == BB_END (bb))
220 return NULL;
221 insn = NEXT_INSN (insn);
224 while (NOTE_P (insn) || DEBUG_INSN_P (insn))
226 if (insn == BB_END (bb))
227 return NULL;
228 insn = NEXT_INSN (insn);
231 if (JUMP_P (insn))
232 return NULL;
234 return insn;
237 /* Return the last non-jump active (non-jump) insn in the basic block. */
239 static rtx_insn *
240 last_active_insn (basic_block bb, int skip_use_p)
242 rtx_insn *insn = BB_END (bb);
243 rtx_insn *head = BB_HEAD (bb);
245 while (NOTE_P (insn)
246 || JUMP_P (insn)
247 || DEBUG_INSN_P (insn)
248 || (skip_use_p
249 && NONJUMP_INSN_P (insn)
250 && GET_CODE (PATTERN (insn)) == USE))
252 if (insn == head)
253 return NULL;
254 insn = PREV_INSN (insn);
257 if (LABEL_P (insn))
258 return NULL;
260 return insn;
263 /* Return the active insn before INSN inside basic block CURR_BB. */
265 static rtx_insn *
266 find_active_insn_before (basic_block curr_bb, rtx_insn *insn)
268 if (!insn || insn == BB_HEAD (curr_bb))
269 return NULL;
271 while ((insn = PREV_INSN (insn)) != NULL_RTX)
273 if (NONJUMP_INSN_P (insn) || JUMP_P (insn) || CALL_P (insn))
274 break;
276 /* No other active insn all the way to the start of the basic block. */
277 if (insn == BB_HEAD (curr_bb))
278 return NULL;
281 return insn;
284 /* Return the active insn after INSN inside basic block CURR_BB. */
286 static rtx_insn *
287 find_active_insn_after (basic_block curr_bb, rtx_insn *insn)
289 if (!insn || insn == BB_END (curr_bb))
290 return NULL;
292 while ((insn = NEXT_INSN (insn)) != NULL_RTX)
294 if (NONJUMP_INSN_P (insn) || JUMP_P (insn) || CALL_P (insn))
295 break;
297 /* No other active insn all the way to the end of the basic block. */
298 if (insn == BB_END (curr_bb))
299 return NULL;
302 return insn;
305 /* Return the basic block reached by falling though the basic block BB. */
307 static basic_block
308 block_fallthru (basic_block bb)
310 edge e = find_fallthru_edge (bb->succs);
312 return (e) ? e->dest : NULL_BLOCK;
315 /* Return true if RTXs A and B can be safely interchanged. */
317 static bool
318 rtx_interchangeable_p (const_rtx a, const_rtx b)
320 if (!rtx_equal_p (a, b))
321 return false;
323 if (GET_CODE (a) != MEM)
324 return true;
326 /* A dead type-unsafe memory reference is legal, but a live type-unsafe memory
327 reference is not. Interchanging a dead type-unsafe memory reference with
328 a live type-safe one creates a live type-unsafe memory reference, in other
329 words, it makes the program illegal.
330 We check here conservatively whether the two memory references have equal
331 memory attributes. */
333 return mem_attrs_eq_p (get_mem_attrs (a), get_mem_attrs (b));
337 /* Go through a bunch of insns, converting them to conditional
338 execution format if possible. Return TRUE if all of the non-note
339 insns were processed. */
341 static int
342 cond_exec_process_insns (ce_if_block *ce_info ATTRIBUTE_UNUSED,
343 /* if block information */rtx_insn *start,
344 /* first insn to look at */rtx end,
345 /* last insn to look at */rtx test,
346 /* conditional execution test */int prob_val,
347 /* probability of branch taken. */int mod_ok)
349 int must_be_last = FALSE;
350 rtx_insn *insn;
351 rtx xtest;
352 rtx pattern;
354 if (!start || !end)
355 return FALSE;
357 for (insn = start; ; insn = NEXT_INSN (insn))
359 /* dwarf2out can't cope with conditional prologues. */
360 if (NOTE_P (insn) && NOTE_KIND (insn) == NOTE_INSN_PROLOGUE_END)
361 return FALSE;
363 if (NOTE_P (insn) || DEBUG_INSN_P (insn))
364 goto insn_done;
366 gcc_assert (NONJUMP_INSN_P (insn) || CALL_P (insn));
368 /* dwarf2out can't cope with conditional unwind info. */
369 if (RTX_FRAME_RELATED_P (insn))
370 return FALSE;
372 /* Remove USE insns that get in the way. */
373 if (reload_completed && GET_CODE (PATTERN (insn)) == USE)
375 /* ??? Ug. Actually unlinking the thing is problematic,
376 given what we'd have to coordinate with our callers. */
377 SET_INSN_DELETED (insn);
378 goto insn_done;
381 /* Last insn wasn't last? */
382 if (must_be_last)
383 return FALSE;
385 if (modified_in_p (test, insn))
387 if (!mod_ok)
388 return FALSE;
389 must_be_last = TRUE;
392 /* Now build the conditional form of the instruction. */
393 pattern = PATTERN (insn);
394 xtest = copy_rtx (test);
396 /* If this is already a COND_EXEC, rewrite the test to be an AND of the
397 two conditions. */
398 if (GET_CODE (pattern) == COND_EXEC)
400 if (GET_MODE (xtest) != GET_MODE (COND_EXEC_TEST (pattern)))
401 return FALSE;
403 xtest = gen_rtx_AND (GET_MODE (xtest), xtest,
404 COND_EXEC_TEST (pattern));
405 pattern = COND_EXEC_CODE (pattern);
408 pattern = gen_rtx_COND_EXEC (VOIDmode, xtest, pattern);
410 /* If the machine needs to modify the insn being conditionally executed,
411 say for example to force a constant integer operand into a temp
412 register, do so here. */
413 #ifdef IFCVT_MODIFY_INSN
414 IFCVT_MODIFY_INSN (ce_info, pattern, insn);
415 if (! pattern)
416 return FALSE;
417 #endif
419 validate_change (insn, &PATTERN (insn), pattern, 1);
421 if (CALL_P (insn) && prob_val >= 0)
422 validate_change (insn, &REG_NOTES (insn),
423 gen_rtx_INT_LIST ((enum machine_mode) REG_BR_PROB,
424 prob_val, REG_NOTES (insn)), 1);
426 insn_done:
427 if (insn == end)
428 break;
431 return TRUE;
434 /* Return the condition for a jump. Do not do any special processing. */
436 static rtx
437 cond_exec_get_condition (rtx_insn *jump)
439 rtx test_if, cond;
441 if (any_condjump_p (jump))
442 test_if = SET_SRC (pc_set (jump));
443 else
444 return NULL_RTX;
445 cond = XEXP (test_if, 0);
447 /* If this branches to JUMP_LABEL when the condition is false,
448 reverse the condition. */
449 if (GET_CODE (XEXP (test_if, 2)) == LABEL_REF
450 && LABEL_REF_LABEL (XEXP (test_if, 2)) == JUMP_LABEL (jump))
452 enum rtx_code rev = reversed_comparison_code (cond, jump);
453 if (rev == UNKNOWN)
454 return NULL_RTX;
456 cond = gen_rtx_fmt_ee (rev, GET_MODE (cond), XEXP (cond, 0),
457 XEXP (cond, 1));
460 return cond;
463 /* Given a simple IF-THEN or IF-THEN-ELSE block, attempt to convert it
464 to conditional execution. Return TRUE if we were successful at
465 converting the block. */
467 static int
468 cond_exec_process_if_block (ce_if_block * ce_info,
469 /* if block information */int do_multiple_p)
471 basic_block test_bb = ce_info->test_bb; /* last test block */
472 basic_block then_bb = ce_info->then_bb; /* THEN */
473 basic_block else_bb = ce_info->else_bb; /* ELSE or NULL */
474 rtx test_expr; /* expression in IF_THEN_ELSE that is tested */
475 rtx_insn *then_start; /* first insn in THEN block */
476 rtx_insn *then_end; /* last insn + 1 in THEN block */
477 rtx_insn *else_start = NULL; /* first insn in ELSE block or NULL */
478 rtx_insn *else_end = NULL; /* last insn + 1 in ELSE block */
479 int max; /* max # of insns to convert. */
480 int then_mod_ok; /* whether conditional mods are ok in THEN */
481 rtx true_expr; /* test for else block insns */
482 rtx false_expr; /* test for then block insns */
483 int true_prob_val; /* probability of else block */
484 int false_prob_val; /* probability of then block */
485 rtx_insn *then_last_head = NULL; /* Last match at the head of THEN */
486 rtx_insn *else_last_head = NULL; /* Last match at the head of ELSE */
487 rtx_insn *then_first_tail = NULL; /* First match at the tail of THEN */
488 rtx_insn *else_first_tail = NULL; /* First match at the tail of ELSE */
489 int then_n_insns, else_n_insns, n_insns;
490 enum rtx_code false_code;
491 rtx note;
493 /* If test is comprised of && or || elements, and we've failed at handling
494 all of them together, just use the last test if it is the special case of
495 && elements without an ELSE block. */
496 if (!do_multiple_p && ce_info->num_multiple_test_blocks)
498 if (else_bb || ! ce_info->and_and_p)
499 return FALSE;
501 ce_info->test_bb = test_bb = ce_info->last_test_bb;
502 ce_info->num_multiple_test_blocks = 0;
503 ce_info->num_and_and_blocks = 0;
504 ce_info->num_or_or_blocks = 0;
507 /* Find the conditional jump to the ELSE or JOIN part, and isolate
508 the test. */
509 test_expr = cond_exec_get_condition (BB_END (test_bb));
510 if (! test_expr)
511 return FALSE;
513 /* If the conditional jump is more than just a conditional jump,
514 then we can not do conditional execution conversion on this block. */
515 if (! onlyjump_p (BB_END (test_bb)))
516 return FALSE;
518 /* Collect the bounds of where we're to search, skipping any labels, jumps
519 and notes at the beginning and end of the block. Then count the total
520 number of insns and see if it is small enough to convert. */
521 then_start = first_active_insn (then_bb);
522 then_end = last_active_insn (then_bb, TRUE);
523 then_n_insns = ce_info->num_then_insns = count_bb_insns (then_bb);
524 n_insns = then_n_insns;
525 max = MAX_CONDITIONAL_EXECUTE;
527 if (else_bb)
529 int n_matching;
531 max *= 2;
532 else_start = first_active_insn (else_bb);
533 else_end = last_active_insn (else_bb, TRUE);
534 else_n_insns = ce_info->num_else_insns = count_bb_insns (else_bb);
535 n_insns += else_n_insns;
537 /* Look for matching sequences at the head and tail of the two blocks,
538 and limit the range of insns to be converted if possible. */
539 n_matching = flow_find_cross_jump (then_bb, else_bb,
540 &then_first_tail, &else_first_tail,
541 NULL);
542 if (then_first_tail == BB_HEAD (then_bb))
543 then_start = then_end = NULL;
544 if (else_first_tail == BB_HEAD (else_bb))
545 else_start = else_end = NULL;
547 if (n_matching > 0)
549 if (then_end)
550 then_end = find_active_insn_before (then_bb, then_first_tail);
551 if (else_end)
552 else_end = find_active_insn_before (else_bb, else_first_tail);
553 n_insns -= 2 * n_matching;
556 if (then_start
557 && else_start
558 && then_n_insns > n_matching
559 && else_n_insns > n_matching)
561 int longest_match = MIN (then_n_insns - n_matching,
562 else_n_insns - n_matching);
563 n_matching
564 = flow_find_head_matching_sequence (then_bb, else_bb,
565 &then_last_head,
566 &else_last_head,
567 longest_match);
569 if (n_matching > 0)
571 rtx_insn *insn;
573 /* We won't pass the insns in the head sequence to
574 cond_exec_process_insns, so we need to test them here
575 to make sure that they don't clobber the condition. */
576 for (insn = BB_HEAD (then_bb);
577 insn != NEXT_INSN (then_last_head);
578 insn = NEXT_INSN (insn))
579 if (!LABEL_P (insn) && !NOTE_P (insn)
580 && !DEBUG_INSN_P (insn)
581 && modified_in_p (test_expr, insn))
582 return FALSE;
585 if (then_last_head == then_end)
586 then_start = then_end = NULL;
587 if (else_last_head == else_end)
588 else_start = else_end = NULL;
590 if (n_matching > 0)
592 if (then_start)
593 then_start = find_active_insn_after (then_bb, then_last_head);
594 if (else_start)
595 else_start = find_active_insn_after (else_bb, else_last_head);
596 n_insns -= 2 * n_matching;
601 if (n_insns > max)
602 return FALSE;
604 /* Map test_expr/test_jump into the appropriate MD tests to use on
605 the conditionally executed code. */
607 true_expr = test_expr;
609 false_code = reversed_comparison_code (true_expr, BB_END (test_bb));
610 if (false_code != UNKNOWN)
611 false_expr = gen_rtx_fmt_ee (false_code, GET_MODE (true_expr),
612 XEXP (true_expr, 0), XEXP (true_expr, 1));
613 else
614 false_expr = NULL_RTX;
616 #ifdef IFCVT_MODIFY_TESTS
617 /* If the machine description needs to modify the tests, such as setting a
618 conditional execution register from a comparison, it can do so here. */
619 IFCVT_MODIFY_TESTS (ce_info, true_expr, false_expr);
621 /* See if the conversion failed. */
622 if (!true_expr || !false_expr)
623 goto fail;
624 #endif
626 note = find_reg_note (BB_END (test_bb), REG_BR_PROB, NULL_RTX);
627 if (note)
629 true_prob_val = XINT (note, 0);
630 false_prob_val = REG_BR_PROB_BASE - true_prob_val;
632 else
634 true_prob_val = -1;
635 false_prob_val = -1;
638 /* If we have && or || tests, do them here. These tests are in the adjacent
639 blocks after the first block containing the test. */
640 if (ce_info->num_multiple_test_blocks > 0)
642 basic_block bb = test_bb;
643 basic_block last_test_bb = ce_info->last_test_bb;
645 if (! false_expr)
646 goto fail;
650 rtx_insn *start, *end;
651 rtx t, f;
652 enum rtx_code f_code;
654 bb = block_fallthru (bb);
655 start = first_active_insn (bb);
656 end = last_active_insn (bb, TRUE);
657 if (start
658 && ! cond_exec_process_insns (ce_info, start, end, false_expr,
659 false_prob_val, FALSE))
660 goto fail;
662 /* If the conditional jump is more than just a conditional jump, then
663 we can not do conditional execution conversion on this block. */
664 if (! onlyjump_p (BB_END (bb)))
665 goto fail;
667 /* Find the conditional jump and isolate the test. */
668 t = cond_exec_get_condition (BB_END (bb));
669 if (! t)
670 goto fail;
672 f_code = reversed_comparison_code (t, BB_END (bb));
673 if (f_code == UNKNOWN)
674 goto fail;
676 f = gen_rtx_fmt_ee (f_code, GET_MODE (t), XEXP (t, 0), XEXP (t, 1));
677 if (ce_info->and_and_p)
679 t = gen_rtx_AND (GET_MODE (t), true_expr, t);
680 f = gen_rtx_IOR (GET_MODE (t), false_expr, f);
682 else
684 t = gen_rtx_IOR (GET_MODE (t), true_expr, t);
685 f = gen_rtx_AND (GET_MODE (t), false_expr, f);
688 /* If the machine description needs to modify the tests, such as
689 setting a conditional execution register from a comparison, it can
690 do so here. */
691 #ifdef IFCVT_MODIFY_MULTIPLE_TESTS
692 IFCVT_MODIFY_MULTIPLE_TESTS (ce_info, bb, t, f);
694 /* See if the conversion failed. */
695 if (!t || !f)
696 goto fail;
697 #endif
699 true_expr = t;
700 false_expr = f;
702 while (bb != last_test_bb);
705 /* For IF-THEN-ELSE blocks, we don't allow modifications of the test
706 on then THEN block. */
707 then_mod_ok = (else_bb == NULL_BLOCK);
709 /* Go through the THEN and ELSE blocks converting the insns if possible
710 to conditional execution. */
712 if (then_end
713 && (! false_expr
714 || ! cond_exec_process_insns (ce_info, then_start, then_end,
715 false_expr, false_prob_val,
716 then_mod_ok)))
717 goto fail;
719 if (else_bb && else_end
720 && ! cond_exec_process_insns (ce_info, else_start, else_end,
721 true_expr, true_prob_val, TRUE))
722 goto fail;
724 /* If we cannot apply the changes, fail. Do not go through the normal fail
725 processing, since apply_change_group will call cancel_changes. */
726 if (! apply_change_group ())
728 #ifdef IFCVT_MODIFY_CANCEL
729 /* Cancel any machine dependent changes. */
730 IFCVT_MODIFY_CANCEL (ce_info);
731 #endif
732 return FALSE;
735 #ifdef IFCVT_MODIFY_FINAL
736 /* Do any machine dependent final modifications. */
737 IFCVT_MODIFY_FINAL (ce_info);
738 #endif
740 /* Conversion succeeded. */
741 if (dump_file)
742 fprintf (dump_file, "%d insn%s converted to conditional execution.\n",
743 n_insns, (n_insns == 1) ? " was" : "s were");
745 /* Merge the blocks! If we had matching sequences, make sure to delete one
746 copy at the appropriate location first: delete the copy in the THEN branch
747 for a tail sequence so that the remaining one is executed last for both
748 branches, and delete the copy in the ELSE branch for a head sequence so
749 that the remaining one is executed first for both branches. */
750 if (then_first_tail)
752 rtx_insn *from = then_first_tail;
753 if (!INSN_P (from))
754 from = find_active_insn_after (then_bb, from);
755 delete_insn_chain (from, BB_END (then_bb), false);
757 if (else_last_head)
758 delete_insn_chain (first_active_insn (else_bb), else_last_head, false);
760 merge_if_block (ce_info);
761 cond_exec_changed_p = TRUE;
762 return TRUE;
764 fail:
765 #ifdef IFCVT_MODIFY_CANCEL
766 /* Cancel any machine dependent changes. */
767 IFCVT_MODIFY_CANCEL (ce_info);
768 #endif
770 cancel_changes (0);
771 return FALSE;
774 /* Used by noce_process_if_block to communicate with its subroutines.
776 The subroutines know that A and B may be evaluated freely. They
777 know that X is a register. They should insert new instructions
778 before cond_earliest. */
780 struct noce_if_info
782 /* The basic blocks that make up the IF-THEN-{ELSE-,}JOIN block. */
783 basic_block test_bb, then_bb, else_bb, join_bb;
785 /* The jump that ends TEST_BB. */
786 rtx_insn *jump;
788 /* The jump condition. */
789 rtx cond;
791 /* New insns should be inserted before this one. */
792 rtx_insn *cond_earliest;
794 /* Insns in the THEN and ELSE block. There is always just this
795 one insns in those blocks. The insns are single_set insns.
796 If there was no ELSE block, INSN_B is the last insn before
797 COND_EARLIEST, or NULL_RTX. In the former case, the insn
798 operands are still valid, as if INSN_B was moved down below
799 the jump. */
800 rtx_insn *insn_a, *insn_b;
802 /* The SET_SRC of INSN_A and INSN_B. */
803 rtx a, b;
805 /* The SET_DEST of INSN_A. */
806 rtx x;
808 /* True if this if block is not canonical. In the canonical form of
809 if blocks, the THEN_BB is the block reached via the fallthru edge
810 from TEST_BB. For the noce transformations, we allow the symmetric
811 form as well. */
812 bool then_else_reversed;
814 /* Estimated cost of the particular branch instruction. */
815 int branch_cost;
818 static rtx noce_emit_store_flag (struct noce_if_info *, rtx, int, int);
819 static int noce_try_move (struct noce_if_info *);
820 static int noce_try_store_flag (struct noce_if_info *);
821 static int noce_try_addcc (struct noce_if_info *);
822 static int noce_try_store_flag_constants (struct noce_if_info *);
823 static int noce_try_store_flag_mask (struct noce_if_info *);
824 static rtx noce_emit_cmove (struct noce_if_info *, rtx, enum rtx_code, rtx,
825 rtx, rtx, rtx);
826 static int noce_try_cmove (struct noce_if_info *);
827 static int noce_try_cmove_arith (struct noce_if_info *);
828 static rtx noce_get_alt_condition (struct noce_if_info *, rtx, rtx_insn **);
829 static int noce_try_minmax (struct noce_if_info *);
830 static int noce_try_abs (struct noce_if_info *);
831 static int noce_try_sign_mask (struct noce_if_info *);
833 /* Helper function for noce_try_store_flag*. */
835 static rtx
836 noce_emit_store_flag (struct noce_if_info *if_info, rtx x, int reversep,
837 int normalize)
839 rtx cond = if_info->cond;
840 int cond_complex;
841 enum rtx_code code;
843 cond_complex = (! general_operand (XEXP (cond, 0), VOIDmode)
844 || ! general_operand (XEXP (cond, 1), VOIDmode));
846 /* If earliest == jump, or when the condition is complex, try to
847 build the store_flag insn directly. */
849 if (cond_complex)
851 rtx set = pc_set (if_info->jump);
852 cond = XEXP (SET_SRC (set), 0);
853 if (GET_CODE (XEXP (SET_SRC (set), 2)) == LABEL_REF
854 && LABEL_REF_LABEL (XEXP (SET_SRC (set), 2)) == JUMP_LABEL (if_info->jump))
855 reversep = !reversep;
856 if (if_info->then_else_reversed)
857 reversep = !reversep;
860 if (reversep)
861 code = reversed_comparison_code (cond, if_info->jump);
862 else
863 code = GET_CODE (cond);
865 if ((if_info->cond_earliest == if_info->jump || cond_complex)
866 && (normalize == 0 || STORE_FLAG_VALUE == normalize))
868 rtx src = gen_rtx_fmt_ee (code, GET_MODE (x), XEXP (cond, 0),
869 XEXP (cond, 1));
870 rtx set = gen_rtx_SET (VOIDmode, x, src);
872 start_sequence ();
873 rtx_insn *insn = emit_insn (set);
875 if (recog_memoized (insn) >= 0)
877 rtx_insn *seq = get_insns ();
878 end_sequence ();
879 emit_insn (seq);
881 if_info->cond_earliest = if_info->jump;
883 return x;
886 end_sequence ();
889 /* Don't even try if the comparison operands or the mode of X are weird. */
890 if (cond_complex || !SCALAR_INT_MODE_P (GET_MODE (x)))
891 return NULL_RTX;
893 return emit_store_flag (x, code, XEXP (cond, 0),
894 XEXP (cond, 1), VOIDmode,
895 (code == LTU || code == LEU
896 || code == GEU || code == GTU), normalize);
899 /* Emit instruction to move an rtx, possibly into STRICT_LOW_PART.
900 X is the destination/target and Y is the value to copy. */
902 static void
903 noce_emit_move_insn (rtx x, rtx y)
905 enum machine_mode outmode;
906 rtx outer, inner;
907 int bitpos;
909 if (GET_CODE (x) != STRICT_LOW_PART)
911 rtx_insn *seq, *insn;
912 rtx target;
913 optab ot;
915 start_sequence ();
916 /* Check that the SET_SRC is reasonable before calling emit_move_insn,
917 otherwise construct a suitable SET pattern ourselves. */
918 insn = (OBJECT_P (y) || CONSTANT_P (y) || GET_CODE (y) == SUBREG)
919 ? emit_move_insn (x, y)
920 : emit_insn (gen_rtx_SET (VOIDmode, x, y));
921 seq = get_insns ();
922 end_sequence ();
924 if (recog_memoized (insn) <= 0)
926 if (GET_CODE (x) == ZERO_EXTRACT)
928 rtx op = XEXP (x, 0);
929 unsigned HOST_WIDE_INT size = INTVAL (XEXP (x, 1));
930 unsigned HOST_WIDE_INT start = INTVAL (XEXP (x, 2));
932 /* store_bit_field expects START to be relative to
933 BYTES_BIG_ENDIAN and adjusts this value for machines with
934 BITS_BIG_ENDIAN != BYTES_BIG_ENDIAN. In order to be able to
935 invoke store_bit_field again it is necessary to have the START
936 value from the first call. */
937 if (BITS_BIG_ENDIAN != BYTES_BIG_ENDIAN)
939 if (MEM_P (op))
940 start = BITS_PER_UNIT - start - size;
941 else
943 gcc_assert (REG_P (op));
944 start = BITS_PER_WORD - start - size;
948 gcc_assert (start < (MEM_P (op) ? BITS_PER_UNIT : BITS_PER_WORD));
949 store_bit_field (op, size, start, 0, 0, GET_MODE (x), y);
950 return;
953 switch (GET_RTX_CLASS (GET_CODE (y)))
955 case RTX_UNARY:
956 ot = code_to_optab (GET_CODE (y));
957 if (ot)
959 start_sequence ();
960 target = expand_unop (GET_MODE (y), ot, XEXP (y, 0), x, 0);
961 if (target != NULL_RTX)
963 if (target != x)
964 emit_move_insn (x, target);
965 seq = get_insns ();
967 end_sequence ();
969 break;
971 case RTX_BIN_ARITH:
972 case RTX_COMM_ARITH:
973 ot = code_to_optab (GET_CODE (y));
974 if (ot)
976 start_sequence ();
977 target = expand_binop (GET_MODE (y), ot,
978 XEXP (y, 0), XEXP (y, 1),
979 x, 0, OPTAB_DIRECT);
980 if (target != NULL_RTX)
982 if (target != x)
983 emit_move_insn (x, target);
984 seq = get_insns ();
986 end_sequence ();
988 break;
990 default:
991 break;
995 emit_insn (seq);
996 return;
999 outer = XEXP (x, 0);
1000 inner = XEXP (outer, 0);
1001 outmode = GET_MODE (outer);
1002 bitpos = SUBREG_BYTE (outer) * BITS_PER_UNIT;
1003 store_bit_field (inner, GET_MODE_BITSIZE (outmode), bitpos,
1004 0, 0, outmode, y);
1007 /* Return sequence of instructions generated by if conversion. This
1008 function calls end_sequence() to end the current stream, ensures
1009 that are instructions are unshared, recognizable non-jump insns.
1010 On failure, this function returns a NULL_RTX. */
1012 static rtx_insn *
1013 end_ifcvt_sequence (struct noce_if_info *if_info)
1015 rtx_insn *insn;
1016 rtx_insn *seq = get_insns ();
1018 set_used_flags (if_info->x);
1019 set_used_flags (if_info->cond);
1020 set_used_flags (if_info->a);
1021 set_used_flags (if_info->b);
1022 unshare_all_rtl_in_chain (seq);
1023 end_sequence ();
1025 /* Make sure that all of the instructions emitted are recognizable,
1026 and that we haven't introduced a new jump instruction.
1027 As an exercise for the reader, build a general mechanism that
1028 allows proper placement of required clobbers. */
1029 for (insn = seq; insn; insn = NEXT_INSN (insn))
1030 if (JUMP_P (insn)
1031 || recog_memoized (insn) == -1)
1032 return NULL;
1034 return seq;
1037 /* Convert "if (a != b) x = a; else x = b" into "x = a" and
1038 "if (a == b) x = a; else x = b" into "x = b". */
1040 static int
1041 noce_try_move (struct noce_if_info *if_info)
1043 rtx cond = if_info->cond;
1044 enum rtx_code code = GET_CODE (cond);
1045 rtx y;
1046 rtx_insn *seq;
1048 if (code != NE && code != EQ)
1049 return FALSE;
1051 /* This optimization isn't valid if either A or B could be a NaN
1052 or a signed zero. */
1053 if (HONOR_NANS (GET_MODE (if_info->x))
1054 || HONOR_SIGNED_ZEROS (GET_MODE (if_info->x)))
1055 return FALSE;
1057 /* Check whether the operands of the comparison are A and in
1058 either order. */
1059 if ((rtx_equal_p (if_info->a, XEXP (cond, 0))
1060 && rtx_equal_p (if_info->b, XEXP (cond, 1)))
1061 || (rtx_equal_p (if_info->a, XEXP (cond, 1))
1062 && rtx_equal_p (if_info->b, XEXP (cond, 0))))
1064 if (!rtx_interchangeable_p (if_info->a, if_info->b))
1065 return FALSE;
1067 y = (code == EQ) ? if_info->a : if_info->b;
1069 /* Avoid generating the move if the source is the destination. */
1070 if (! rtx_equal_p (if_info->x, y))
1072 start_sequence ();
1073 noce_emit_move_insn (if_info->x, y);
1074 seq = end_ifcvt_sequence (if_info);
1075 if (!seq)
1076 return FALSE;
1078 emit_insn_before_setloc (seq, if_info->jump,
1079 INSN_LOCATION (if_info->insn_a));
1081 return TRUE;
1083 return FALSE;
1086 /* Convert "if (test) x = 1; else x = 0".
1088 Only try 0 and STORE_FLAG_VALUE here. Other combinations will be
1089 tried in noce_try_store_flag_constants after noce_try_cmove has had
1090 a go at the conversion. */
1092 static int
1093 noce_try_store_flag (struct noce_if_info *if_info)
1095 int reversep;
1096 rtx target;
1097 rtx_insn *seq;
1099 if (CONST_INT_P (if_info->b)
1100 && INTVAL (if_info->b) == STORE_FLAG_VALUE
1101 && if_info->a == const0_rtx)
1102 reversep = 0;
1103 else if (if_info->b == const0_rtx
1104 && CONST_INT_P (if_info->a)
1105 && INTVAL (if_info->a) == STORE_FLAG_VALUE
1106 && (reversed_comparison_code (if_info->cond, if_info->jump)
1107 != UNKNOWN))
1108 reversep = 1;
1109 else
1110 return FALSE;
1112 start_sequence ();
1114 target = noce_emit_store_flag (if_info, if_info->x, reversep, 0);
1115 if (target)
1117 if (target != if_info->x)
1118 noce_emit_move_insn (if_info->x, target);
1120 seq = end_ifcvt_sequence (if_info);
1121 if (! seq)
1122 return FALSE;
1124 emit_insn_before_setloc (seq, if_info->jump,
1125 INSN_LOCATION (if_info->insn_a));
1126 return TRUE;
1128 else
1130 end_sequence ();
1131 return FALSE;
1135 /* Convert "if (test) x = a; else x = b", for A and B constant. */
1137 static int
1138 noce_try_store_flag_constants (struct noce_if_info *if_info)
1140 rtx target;
1141 rtx_insn *seq;
1142 int reversep;
1143 HOST_WIDE_INT itrue, ifalse, diff, tmp;
1144 int normalize, can_reverse;
1145 enum machine_mode mode;
1147 if (CONST_INT_P (if_info->a)
1148 && CONST_INT_P (if_info->b))
1150 mode = GET_MODE (if_info->x);
1151 ifalse = INTVAL (if_info->a);
1152 itrue = INTVAL (if_info->b);
1154 diff = (unsigned HOST_WIDE_INT) itrue - ifalse;
1155 /* Make sure we can represent the difference between the two values. */
1156 if ((diff > 0)
1157 != ((ifalse < 0) != (itrue < 0) ? ifalse < 0 : ifalse < itrue))
1158 return FALSE;
1160 diff = trunc_int_for_mode (diff, mode);
1162 can_reverse = (reversed_comparison_code (if_info->cond, if_info->jump)
1163 != UNKNOWN);
1165 reversep = 0;
1166 if (diff == STORE_FLAG_VALUE || diff == -STORE_FLAG_VALUE)
1167 normalize = 0;
1168 else if (ifalse == 0 && exact_log2 (itrue) >= 0
1169 && (STORE_FLAG_VALUE == 1
1170 || if_info->branch_cost >= 2))
1171 normalize = 1;
1172 else if (itrue == 0 && exact_log2 (ifalse) >= 0 && can_reverse
1173 && (STORE_FLAG_VALUE == 1 || if_info->branch_cost >= 2))
1174 normalize = 1, reversep = 1;
1175 else if (itrue == -1
1176 && (STORE_FLAG_VALUE == -1
1177 || if_info->branch_cost >= 2))
1178 normalize = -1;
1179 else if (ifalse == -1 && can_reverse
1180 && (STORE_FLAG_VALUE == -1 || if_info->branch_cost >= 2))
1181 normalize = -1, reversep = 1;
1182 else if ((if_info->branch_cost >= 2 && STORE_FLAG_VALUE == -1)
1183 || if_info->branch_cost >= 3)
1184 normalize = -1;
1185 else
1186 return FALSE;
1188 if (reversep)
1190 tmp = itrue; itrue = ifalse; ifalse = tmp;
1191 diff = trunc_int_for_mode (-(unsigned HOST_WIDE_INT) diff, mode);
1194 start_sequence ();
1195 target = noce_emit_store_flag (if_info, if_info->x, reversep, normalize);
1196 if (! target)
1198 end_sequence ();
1199 return FALSE;
1202 /* if (test) x = 3; else x = 4;
1203 => x = 3 + (test == 0); */
1204 if (diff == STORE_FLAG_VALUE || diff == -STORE_FLAG_VALUE)
1206 target = expand_simple_binop (mode,
1207 (diff == STORE_FLAG_VALUE
1208 ? PLUS : MINUS),
1209 gen_int_mode (ifalse, mode), target,
1210 if_info->x, 0, OPTAB_WIDEN);
1213 /* if (test) x = 8; else x = 0;
1214 => x = (test != 0) << 3; */
1215 else if (ifalse == 0 && (tmp = exact_log2 (itrue)) >= 0)
1217 target = expand_simple_binop (mode, ASHIFT,
1218 target, GEN_INT (tmp), if_info->x, 0,
1219 OPTAB_WIDEN);
1222 /* if (test) x = -1; else x = b;
1223 => x = -(test != 0) | b; */
1224 else if (itrue == -1)
1226 target = expand_simple_binop (mode, IOR,
1227 target, gen_int_mode (ifalse, mode),
1228 if_info->x, 0, OPTAB_WIDEN);
1231 /* if (test) x = a; else x = b;
1232 => x = (-(test != 0) & (b - a)) + a; */
1233 else
1235 target = expand_simple_binop (mode, AND,
1236 target, gen_int_mode (diff, mode),
1237 if_info->x, 0, OPTAB_WIDEN);
1238 if (target)
1239 target = expand_simple_binop (mode, PLUS,
1240 target, gen_int_mode (ifalse, mode),
1241 if_info->x, 0, OPTAB_WIDEN);
1244 if (! target)
1246 end_sequence ();
1247 return FALSE;
1250 if (target != if_info->x)
1251 noce_emit_move_insn (if_info->x, target);
1253 seq = end_ifcvt_sequence (if_info);
1254 if (!seq)
1255 return FALSE;
1257 emit_insn_before_setloc (seq, if_info->jump,
1258 INSN_LOCATION (if_info->insn_a));
1259 return TRUE;
1262 return FALSE;
1265 /* Convert "if (test) foo++" into "foo += (test != 0)", and
1266 similarly for "foo--". */
1268 static int
1269 noce_try_addcc (struct noce_if_info *if_info)
1271 rtx target;
1272 rtx_insn *seq;
1273 int subtract, normalize;
1275 if (GET_CODE (if_info->a) == PLUS
1276 && rtx_equal_p (XEXP (if_info->a, 0), if_info->b)
1277 && (reversed_comparison_code (if_info->cond, if_info->jump)
1278 != UNKNOWN))
1280 rtx cond = if_info->cond;
1281 enum rtx_code code = reversed_comparison_code (cond, if_info->jump);
1283 /* First try to use addcc pattern. */
1284 if (general_operand (XEXP (cond, 0), VOIDmode)
1285 && general_operand (XEXP (cond, 1), VOIDmode))
1287 start_sequence ();
1288 target = emit_conditional_add (if_info->x, code,
1289 XEXP (cond, 0),
1290 XEXP (cond, 1),
1291 VOIDmode,
1292 if_info->b,
1293 XEXP (if_info->a, 1),
1294 GET_MODE (if_info->x),
1295 (code == LTU || code == GEU
1296 || code == LEU || code == GTU));
1297 if (target)
1299 if (target != if_info->x)
1300 noce_emit_move_insn (if_info->x, target);
1302 seq = end_ifcvt_sequence (if_info);
1303 if (!seq)
1304 return FALSE;
1306 emit_insn_before_setloc (seq, if_info->jump,
1307 INSN_LOCATION (if_info->insn_a));
1308 return TRUE;
1310 end_sequence ();
1313 /* If that fails, construct conditional increment or decrement using
1314 setcc. */
1315 if (if_info->branch_cost >= 2
1316 && (XEXP (if_info->a, 1) == const1_rtx
1317 || XEXP (if_info->a, 1) == constm1_rtx))
1319 start_sequence ();
1320 if (STORE_FLAG_VALUE == INTVAL (XEXP (if_info->a, 1)))
1321 subtract = 0, normalize = 0;
1322 else if (-STORE_FLAG_VALUE == INTVAL (XEXP (if_info->a, 1)))
1323 subtract = 1, normalize = 0;
1324 else
1325 subtract = 0, normalize = INTVAL (XEXP (if_info->a, 1));
1328 target = noce_emit_store_flag (if_info,
1329 gen_reg_rtx (GET_MODE (if_info->x)),
1330 1, normalize);
1332 if (target)
1333 target = expand_simple_binop (GET_MODE (if_info->x),
1334 subtract ? MINUS : PLUS,
1335 if_info->b, target, if_info->x,
1336 0, OPTAB_WIDEN);
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 ();
1354 return FALSE;
1357 /* Convert "if (test) x = 0;" to "x &= -(test == 0);" */
1359 static int
1360 noce_try_store_flag_mask (struct noce_if_info *if_info)
1362 rtx target;
1363 rtx_insn *seq;
1364 int reversep;
1366 reversep = 0;
1367 if ((if_info->branch_cost >= 2
1368 || STORE_FLAG_VALUE == -1)
1369 && ((if_info->a == const0_rtx
1370 && rtx_equal_p (if_info->b, if_info->x))
1371 || ((reversep = (reversed_comparison_code (if_info->cond,
1372 if_info->jump)
1373 != UNKNOWN))
1374 && if_info->b == const0_rtx
1375 && rtx_equal_p (if_info->a, if_info->x))))
1377 start_sequence ();
1378 target = noce_emit_store_flag (if_info,
1379 gen_reg_rtx (GET_MODE (if_info->x)),
1380 reversep, -1);
1381 if (target)
1382 target = expand_simple_binop (GET_MODE (if_info->x), AND,
1383 if_info->x,
1384 target, if_info->x, 0,
1385 OPTAB_WIDEN);
1387 if (target)
1389 if (target != if_info->x)
1390 noce_emit_move_insn (if_info->x, target);
1392 seq = end_ifcvt_sequence (if_info);
1393 if (!seq)
1394 return FALSE;
1396 emit_insn_before_setloc (seq, if_info->jump,
1397 INSN_LOCATION (if_info->insn_a));
1398 return TRUE;
1401 end_sequence ();
1404 return FALSE;
1407 /* Helper function for noce_try_cmove and noce_try_cmove_arith. */
1409 static rtx
1410 noce_emit_cmove (struct noce_if_info *if_info, rtx x, enum rtx_code code,
1411 rtx cmp_a, rtx cmp_b, rtx vfalse, rtx vtrue)
1413 rtx target ATTRIBUTE_UNUSED;
1414 int unsignedp ATTRIBUTE_UNUSED;
1416 /* If earliest == jump, try to build the cmove insn directly.
1417 This is helpful when combine has created some complex condition
1418 (like for alpha's cmovlbs) that we can't hope to regenerate
1419 through the normal interface. */
1421 if (if_info->cond_earliest == if_info->jump)
1423 rtx cond = gen_rtx_fmt_ee (code, GET_MODE (if_info->cond), cmp_a, cmp_b);
1424 rtx if_then_else = gen_rtx_IF_THEN_ELSE (GET_MODE (x),
1425 cond, vtrue, vfalse);
1426 rtx set = gen_rtx_SET (VOIDmode, x, if_then_else);
1428 start_sequence ();
1429 rtx_insn *insn = emit_insn (set);
1431 if (recog_memoized (insn) >= 0)
1433 rtx_insn *seq = get_insns ();
1434 end_sequence ();
1435 emit_insn (seq);
1437 return x;
1440 end_sequence ();
1443 /* Don't even try if the comparison operands are weird. */
1444 if (! general_operand (cmp_a, GET_MODE (cmp_a))
1445 || ! general_operand (cmp_b, GET_MODE (cmp_b)))
1446 return NULL_RTX;
1448 #if HAVE_conditional_move
1449 unsignedp = (code == LTU || code == GEU
1450 || code == LEU || code == GTU);
1452 target = emit_conditional_move (x, code, cmp_a, cmp_b, VOIDmode,
1453 vtrue, vfalse, GET_MODE (x),
1454 unsignedp);
1455 if (target)
1456 return target;
1458 /* We might be faced with a situation like:
1460 x = (reg:M TARGET)
1461 vtrue = (subreg:M (reg:N VTRUE) BYTE)
1462 vfalse = (subreg:M (reg:N VFALSE) BYTE)
1464 We can't do a conditional move in mode M, but it's possible that we
1465 could do a conditional move in mode N instead and take a subreg of
1466 the result.
1468 If we can't create new pseudos, though, don't bother. */
1469 if (reload_completed)
1470 return NULL_RTX;
1472 if (GET_CODE (vtrue) == SUBREG && GET_CODE (vfalse) == SUBREG)
1474 rtx reg_vtrue = SUBREG_REG (vtrue);
1475 rtx reg_vfalse = SUBREG_REG (vfalse);
1476 unsigned int byte_vtrue = SUBREG_BYTE (vtrue);
1477 unsigned int byte_vfalse = SUBREG_BYTE (vfalse);
1478 rtx promoted_target;
1480 if (GET_MODE (reg_vtrue) != GET_MODE (reg_vfalse)
1481 || byte_vtrue != byte_vfalse
1482 || (SUBREG_PROMOTED_VAR_P (vtrue)
1483 != SUBREG_PROMOTED_VAR_P (vfalse))
1484 || (SUBREG_PROMOTED_GET (vtrue)
1485 != SUBREG_PROMOTED_GET (vfalse)))
1486 return NULL_RTX;
1488 promoted_target = gen_reg_rtx (GET_MODE (reg_vtrue));
1490 target = emit_conditional_move (promoted_target, code, cmp_a, cmp_b,
1491 VOIDmode, reg_vtrue, reg_vfalse,
1492 GET_MODE (reg_vtrue), unsignedp);
1493 /* Nope, couldn't do it in that mode either. */
1494 if (!target)
1495 return NULL_RTX;
1497 target = gen_rtx_SUBREG (GET_MODE (vtrue), promoted_target, byte_vtrue);
1498 SUBREG_PROMOTED_VAR_P (target) = SUBREG_PROMOTED_VAR_P (vtrue);
1499 SUBREG_PROMOTED_SET (target, SUBREG_PROMOTED_GET (vtrue));
1500 emit_move_insn (x, target);
1501 return x;
1503 else
1504 return NULL_RTX;
1505 #else
1506 /* We'll never get here, as noce_process_if_block doesn't call the
1507 functions involved. Ifdef code, however, should be discouraged
1508 because it leads to typos in the code not selected. However,
1509 emit_conditional_move won't exist either. */
1510 return NULL_RTX;
1511 #endif
1514 /* Try only simple constants and registers here. More complex cases
1515 are handled in noce_try_cmove_arith after noce_try_store_flag_arith
1516 has had a go at it. */
1518 static int
1519 noce_try_cmove (struct noce_if_info *if_info)
1521 enum rtx_code code;
1522 rtx target;
1523 rtx_insn *seq;
1525 if ((CONSTANT_P (if_info->a) || register_operand (if_info->a, VOIDmode))
1526 && (CONSTANT_P (if_info->b) || register_operand (if_info->b, VOIDmode)))
1528 start_sequence ();
1530 code = GET_CODE (if_info->cond);
1531 target = noce_emit_cmove (if_info, if_info->x, code,
1532 XEXP (if_info->cond, 0),
1533 XEXP (if_info->cond, 1),
1534 if_info->a, if_info->b);
1536 if (target)
1538 if (target != if_info->x)
1539 noce_emit_move_insn (if_info->x, target);
1541 seq = end_ifcvt_sequence (if_info);
1542 if (!seq)
1543 return FALSE;
1545 emit_insn_before_setloc (seq, if_info->jump,
1546 INSN_LOCATION (if_info->insn_a));
1547 return TRUE;
1549 else
1551 end_sequence ();
1552 return FALSE;
1556 return FALSE;
1559 /* Try more complex cases involving conditional_move. */
1561 static int
1562 noce_try_cmove_arith (struct noce_if_info *if_info)
1564 rtx a = if_info->a;
1565 rtx b = if_info->b;
1566 rtx x = if_info->x;
1567 rtx orig_a, orig_b;
1568 rtx_insn *insn_a, *insn_b;
1569 rtx target;
1570 int is_mem = 0;
1571 int insn_cost;
1572 enum rtx_code code;
1573 rtx_insn *ifcvt_seq;
1575 /* A conditional move from two memory sources is equivalent to a
1576 conditional on their addresses followed by a load. Don't do this
1577 early because it'll screw alias analysis. Note that we've
1578 already checked for no side effects. */
1579 /* ??? FIXME: Magic number 5. */
1580 if (cse_not_expected
1581 && MEM_P (a) && MEM_P (b)
1582 && MEM_ADDR_SPACE (a) == MEM_ADDR_SPACE (b)
1583 && if_info->branch_cost >= 5)
1585 enum machine_mode address_mode = get_address_mode (a);
1587 a = XEXP (a, 0);
1588 b = XEXP (b, 0);
1589 x = gen_reg_rtx (address_mode);
1590 is_mem = 1;
1593 /* ??? We could handle this if we knew that a load from A or B could
1594 not trap or fault. This is also true if we've already loaded
1595 from the address along the path from ENTRY. */
1596 else if (may_trap_or_fault_p (a) || may_trap_or_fault_p (b))
1597 return FALSE;
1599 /* if (test) x = a + b; else x = c - d;
1600 => y = a + b;
1601 x = c - d;
1602 if (test)
1603 x = y;
1606 code = GET_CODE (if_info->cond);
1607 insn_a = if_info->insn_a;
1608 insn_b = if_info->insn_b;
1610 /* Total insn_rtx_cost should be smaller than branch cost. Exit
1611 if insn_rtx_cost can't be estimated. */
1612 if (insn_a)
1614 insn_cost
1615 = insn_rtx_cost (PATTERN (insn_a),
1616 optimize_bb_for_speed_p (BLOCK_FOR_INSN (insn_a)));
1617 if (insn_cost == 0 || insn_cost > COSTS_N_INSNS (if_info->branch_cost))
1618 return FALSE;
1620 else
1621 insn_cost = 0;
1623 if (insn_b)
1625 insn_cost
1626 += insn_rtx_cost (PATTERN (insn_b),
1627 optimize_bb_for_speed_p (BLOCK_FOR_INSN (insn_b)));
1628 if (insn_cost == 0 || insn_cost > COSTS_N_INSNS (if_info->branch_cost))
1629 return FALSE;
1632 /* Possibly rearrange operands to make things come out more natural. */
1633 if (reversed_comparison_code (if_info->cond, if_info->jump) != UNKNOWN)
1635 int reversep = 0;
1636 if (rtx_equal_p (b, x))
1637 reversep = 1;
1638 else if (general_operand (b, GET_MODE (b)))
1639 reversep = 1;
1641 if (reversep)
1643 rtx tmp;
1644 rtx_insn *tmp_insn;
1645 code = reversed_comparison_code (if_info->cond, if_info->jump);
1646 tmp = a, a = b, b = tmp;
1647 tmp_insn = insn_a, insn_a = insn_b, insn_b = tmp_insn;
1651 start_sequence ();
1653 orig_a = a;
1654 orig_b = b;
1656 /* If either operand is complex, load it into a register first.
1657 The best way to do this is to copy the original insn. In this
1658 way we preserve any clobbers etc that the insn may have had.
1659 This is of course not possible in the IS_MEM case. */
1660 if (! general_operand (a, GET_MODE (a)))
1662 rtx_insn *insn;
1664 if (is_mem)
1666 rtx reg = gen_reg_rtx (GET_MODE (a));
1667 insn = emit_insn (gen_rtx_SET (VOIDmode, reg, a));
1669 else if (! insn_a)
1670 goto end_seq_and_fail;
1671 else
1673 a = gen_reg_rtx (GET_MODE (a));
1674 rtx_insn *copy_of_a = as_a <rtx_insn *> (copy_rtx (insn_a));
1675 rtx set = single_set (copy_of_a);
1676 SET_DEST (set) = a;
1677 insn = emit_insn (PATTERN (copy_of_a));
1679 if (recog_memoized (insn) < 0)
1680 goto end_seq_and_fail;
1682 if (! general_operand (b, GET_MODE (b)))
1684 rtx pat;
1685 rtx_insn *last;
1686 rtx_insn *new_insn;
1688 if (is_mem)
1690 rtx reg = gen_reg_rtx (GET_MODE (b));
1691 pat = gen_rtx_SET (VOIDmode, reg, b);
1693 else if (! insn_b)
1694 goto end_seq_and_fail;
1695 else
1697 b = gen_reg_rtx (GET_MODE (b));
1698 rtx_insn *copy_of_insn_b = as_a <rtx_insn *> (copy_rtx (insn_b));
1699 rtx set = single_set (copy_of_insn_b);
1700 SET_DEST (set) = b;
1701 pat = PATTERN (copy_of_insn_b);
1704 /* If insn to set up A clobbers any registers B depends on, try to
1705 swap insn that sets up A with the one that sets up B. If even
1706 that doesn't help, punt. */
1707 last = get_last_insn ();
1708 if (last && modified_in_p (orig_b, last))
1710 new_insn = emit_insn_before (pat, get_insns ());
1711 if (modified_in_p (orig_a, new_insn))
1712 goto end_seq_and_fail;
1714 else
1715 new_insn = emit_insn (pat);
1717 if (recog_memoized (new_insn) < 0)
1718 goto end_seq_and_fail;
1721 target = noce_emit_cmove (if_info, x, code, XEXP (if_info->cond, 0),
1722 XEXP (if_info->cond, 1), a, b);
1724 if (! target)
1725 goto end_seq_and_fail;
1727 /* If we're handling a memory for above, emit the load now. */
1728 if (is_mem)
1730 rtx mem = gen_rtx_MEM (GET_MODE (if_info->x), target);
1732 /* Copy over flags as appropriate. */
1733 if (MEM_VOLATILE_P (if_info->a) || MEM_VOLATILE_P (if_info->b))
1734 MEM_VOLATILE_P (mem) = 1;
1735 if (MEM_ALIAS_SET (if_info->a) == MEM_ALIAS_SET (if_info->b))
1736 set_mem_alias_set (mem, MEM_ALIAS_SET (if_info->a));
1737 set_mem_align (mem,
1738 MIN (MEM_ALIGN (if_info->a), MEM_ALIGN (if_info->b)));
1740 gcc_assert (MEM_ADDR_SPACE (if_info->a) == MEM_ADDR_SPACE (if_info->b));
1741 set_mem_addr_space (mem, MEM_ADDR_SPACE (if_info->a));
1743 noce_emit_move_insn (if_info->x, mem);
1745 else if (target != x)
1746 noce_emit_move_insn (x, target);
1748 ifcvt_seq = end_ifcvt_sequence (if_info);
1749 if (!ifcvt_seq)
1750 return FALSE;
1752 emit_insn_before_setloc (ifcvt_seq, if_info->jump,
1753 INSN_LOCATION (if_info->insn_a));
1754 return TRUE;
1756 end_seq_and_fail:
1757 end_sequence ();
1758 return FALSE;
1761 /* For most cases, the simplified condition we found is the best
1762 choice, but this is not the case for the min/max/abs transforms.
1763 For these we wish to know that it is A or B in the condition. */
1765 static rtx
1766 noce_get_alt_condition (struct noce_if_info *if_info, rtx target,
1767 rtx_insn **earliest)
1769 rtx cond, set;
1770 rtx_insn *insn;
1771 int reverse;
1773 /* If target is already mentioned in the known condition, return it. */
1774 if (reg_mentioned_p (target, if_info->cond))
1776 *earliest = if_info->cond_earliest;
1777 return if_info->cond;
1780 set = pc_set (if_info->jump);
1781 cond = XEXP (SET_SRC (set), 0);
1782 reverse
1783 = GET_CODE (XEXP (SET_SRC (set), 2)) == LABEL_REF
1784 && LABEL_REF_LABEL (XEXP (SET_SRC (set), 2)) == JUMP_LABEL (if_info->jump);
1785 if (if_info->then_else_reversed)
1786 reverse = !reverse;
1788 /* If we're looking for a constant, try to make the conditional
1789 have that constant in it. There are two reasons why it may
1790 not have the constant we want:
1792 1. GCC may have needed to put the constant in a register, because
1793 the target can't compare directly against that constant. For
1794 this case, we look for a SET immediately before the comparison
1795 that puts a constant in that register.
1797 2. GCC may have canonicalized the conditional, for example
1798 replacing "if x < 4" with "if x <= 3". We can undo that (or
1799 make equivalent types of changes) to get the constants we need
1800 if they're off by one in the right direction. */
1802 if (CONST_INT_P (target))
1804 enum rtx_code code = GET_CODE (if_info->cond);
1805 rtx op_a = XEXP (if_info->cond, 0);
1806 rtx op_b = XEXP (if_info->cond, 1);
1807 rtx prev_insn;
1809 /* First, look to see if we put a constant in a register. */
1810 prev_insn = prev_nonnote_insn (if_info->cond_earliest);
1811 if (prev_insn
1812 && BLOCK_FOR_INSN (prev_insn)
1813 == BLOCK_FOR_INSN (if_info->cond_earliest)
1814 && INSN_P (prev_insn)
1815 && GET_CODE (PATTERN (prev_insn)) == SET)
1817 rtx src = find_reg_equal_equiv_note (prev_insn);
1818 if (!src)
1819 src = SET_SRC (PATTERN (prev_insn));
1820 if (CONST_INT_P (src))
1822 if (rtx_equal_p (op_a, SET_DEST (PATTERN (prev_insn))))
1823 op_a = src;
1824 else if (rtx_equal_p (op_b, SET_DEST (PATTERN (prev_insn))))
1825 op_b = src;
1827 if (CONST_INT_P (op_a))
1829 rtx tmp = op_a;
1830 op_a = op_b;
1831 op_b = tmp;
1832 code = swap_condition (code);
1837 /* Now, look to see if we can get the right constant by
1838 adjusting the conditional. */
1839 if (CONST_INT_P (op_b))
1841 HOST_WIDE_INT desired_val = INTVAL (target);
1842 HOST_WIDE_INT actual_val = INTVAL (op_b);
1844 switch (code)
1846 case LT:
1847 if (actual_val == desired_val + 1)
1849 code = LE;
1850 op_b = GEN_INT (desired_val);
1852 break;
1853 case LE:
1854 if (actual_val == desired_val - 1)
1856 code = LT;
1857 op_b = GEN_INT (desired_val);
1859 break;
1860 case GT:
1861 if (actual_val == desired_val - 1)
1863 code = GE;
1864 op_b = GEN_INT (desired_val);
1866 break;
1867 case GE:
1868 if (actual_val == desired_val + 1)
1870 code = GT;
1871 op_b = GEN_INT (desired_val);
1873 break;
1874 default:
1875 break;
1879 /* If we made any changes, generate a new conditional that is
1880 equivalent to what we started with, but has the right
1881 constants in it. */
1882 if (code != GET_CODE (if_info->cond)
1883 || op_a != XEXP (if_info->cond, 0)
1884 || op_b != XEXP (if_info->cond, 1))
1886 cond = gen_rtx_fmt_ee (code, GET_MODE (cond), op_a, op_b);
1887 *earliest = if_info->cond_earliest;
1888 return cond;
1892 cond = canonicalize_condition (if_info->jump, cond, reverse,
1893 earliest, target, false, true);
1894 if (! cond || ! reg_mentioned_p (target, cond))
1895 return NULL;
1897 /* We almost certainly searched back to a different place.
1898 Need to re-verify correct lifetimes. */
1900 /* X may not be mentioned in the range (cond_earliest, jump]. */
1901 for (insn = if_info->jump; insn != *earliest; insn = PREV_INSN (insn))
1902 if (INSN_P (insn) && reg_overlap_mentioned_p (if_info->x, PATTERN (insn)))
1903 return NULL;
1905 /* A and B may not be modified in the range [cond_earliest, jump). */
1906 for (insn = *earliest; insn != if_info->jump; insn = NEXT_INSN (insn))
1907 if (INSN_P (insn)
1908 && (modified_in_p (if_info->a, insn)
1909 || modified_in_p (if_info->b, insn)))
1910 return NULL;
1912 return cond;
1915 /* Convert "if (a < b) x = a; else x = b;" to "x = min(a, b);", etc. */
1917 static int
1918 noce_try_minmax (struct noce_if_info *if_info)
1920 rtx cond, target;
1921 rtx_insn *earliest, *seq;
1922 enum rtx_code code, op;
1923 int unsignedp;
1925 /* ??? Reject modes with NaNs or signed zeros since we don't know how
1926 they will be resolved with an SMIN/SMAX. It wouldn't be too hard
1927 to get the target to tell us... */
1928 if (HONOR_SIGNED_ZEROS (GET_MODE (if_info->x))
1929 || HONOR_NANS (GET_MODE (if_info->x)))
1930 return FALSE;
1932 cond = noce_get_alt_condition (if_info, if_info->a, &earliest);
1933 if (!cond)
1934 return FALSE;
1936 /* Verify the condition is of the form we expect, and canonicalize
1937 the comparison code. */
1938 code = GET_CODE (cond);
1939 if (rtx_equal_p (XEXP (cond, 0), if_info->a))
1941 if (! rtx_equal_p (XEXP (cond, 1), if_info->b))
1942 return FALSE;
1944 else if (rtx_equal_p (XEXP (cond, 1), if_info->a))
1946 if (! rtx_equal_p (XEXP (cond, 0), if_info->b))
1947 return FALSE;
1948 code = swap_condition (code);
1950 else
1951 return FALSE;
1953 /* Determine what sort of operation this is. Note that the code is for
1954 a taken branch, so the code->operation mapping appears backwards. */
1955 switch (code)
1957 case LT:
1958 case LE:
1959 case UNLT:
1960 case UNLE:
1961 op = SMAX;
1962 unsignedp = 0;
1963 break;
1964 case GT:
1965 case GE:
1966 case UNGT:
1967 case UNGE:
1968 op = SMIN;
1969 unsignedp = 0;
1970 break;
1971 case LTU:
1972 case LEU:
1973 op = UMAX;
1974 unsignedp = 1;
1975 break;
1976 case GTU:
1977 case GEU:
1978 op = UMIN;
1979 unsignedp = 1;
1980 break;
1981 default:
1982 return FALSE;
1985 start_sequence ();
1987 target = expand_simple_binop (GET_MODE (if_info->x), op,
1988 if_info->a, if_info->b,
1989 if_info->x, unsignedp, OPTAB_WIDEN);
1990 if (! target)
1992 end_sequence ();
1993 return FALSE;
1995 if (target != if_info->x)
1996 noce_emit_move_insn (if_info->x, target);
1998 seq = end_ifcvt_sequence (if_info);
1999 if (!seq)
2000 return FALSE;
2002 emit_insn_before_setloc (seq, if_info->jump, INSN_LOCATION (if_info->insn_a));
2003 if_info->cond = cond;
2004 if_info->cond_earliest = earliest;
2006 return TRUE;
2009 /* Convert "if (a < 0) x = -a; else x = a;" to "x = abs(a);",
2010 "if (a < 0) x = ~a; else x = a;" to "x = one_cmpl_abs(a);",
2011 etc. */
2013 static int
2014 noce_try_abs (struct noce_if_info *if_info)
2016 rtx cond, target, a, b, c;
2017 rtx_insn *earliest, *seq;
2018 int negate;
2019 bool one_cmpl = false;
2021 /* Reject modes with signed zeros. */
2022 if (HONOR_SIGNED_ZEROS (GET_MODE (if_info->x)))
2023 return FALSE;
2025 /* Recognize A and B as constituting an ABS or NABS. The canonical
2026 form is a branch around the negation, taken when the object is the
2027 first operand of a comparison against 0 that evaluates to true. */
2028 a = if_info->a;
2029 b = if_info->b;
2030 if (GET_CODE (a) == NEG && rtx_equal_p (XEXP (a, 0), b))
2031 negate = 0;
2032 else if (GET_CODE (b) == NEG && rtx_equal_p (XEXP (b, 0), a))
2034 c = a; a = b; b = c;
2035 negate = 1;
2037 else if (GET_CODE (a) == NOT && rtx_equal_p (XEXP (a, 0), b))
2039 negate = 0;
2040 one_cmpl = true;
2042 else if (GET_CODE (b) == NOT && rtx_equal_p (XEXP (b, 0), a))
2044 c = a; a = b; b = c;
2045 negate = 1;
2046 one_cmpl = true;
2048 else
2049 return FALSE;
2051 cond = noce_get_alt_condition (if_info, b, &earliest);
2052 if (!cond)
2053 return FALSE;
2055 /* Verify the condition is of the form we expect. */
2056 if (rtx_equal_p (XEXP (cond, 0), b))
2057 c = XEXP (cond, 1);
2058 else if (rtx_equal_p (XEXP (cond, 1), b))
2060 c = XEXP (cond, 0);
2061 negate = !negate;
2063 else
2064 return FALSE;
2066 /* Verify that C is zero. Search one step backward for a
2067 REG_EQUAL note or a simple source if necessary. */
2068 if (REG_P (c))
2070 rtx set;
2071 rtx_insn *insn = prev_nonnote_insn (earliest);
2072 if (insn
2073 && BLOCK_FOR_INSN (insn) == BLOCK_FOR_INSN (earliest)
2074 && (set = single_set (insn))
2075 && rtx_equal_p (SET_DEST (set), c))
2077 rtx note = find_reg_equal_equiv_note (insn);
2078 if (note)
2079 c = XEXP (note, 0);
2080 else
2081 c = SET_SRC (set);
2083 else
2084 return FALSE;
2086 if (MEM_P (c)
2087 && GET_CODE (XEXP (c, 0)) == SYMBOL_REF
2088 && CONSTANT_POOL_ADDRESS_P (XEXP (c, 0)))
2089 c = get_pool_constant (XEXP (c, 0));
2091 /* Work around funny ideas get_condition has wrt canonicalization.
2092 Note that these rtx constants are known to be CONST_INT, and
2093 therefore imply integer comparisons. */
2094 if (c == constm1_rtx && GET_CODE (cond) == GT)
2096 else if (c == const1_rtx && GET_CODE (cond) == LT)
2098 else if (c != CONST0_RTX (GET_MODE (b)))
2099 return FALSE;
2101 /* Determine what sort of operation this is. */
2102 switch (GET_CODE (cond))
2104 case LT:
2105 case LE:
2106 case UNLT:
2107 case UNLE:
2108 negate = !negate;
2109 break;
2110 case GT:
2111 case GE:
2112 case UNGT:
2113 case UNGE:
2114 break;
2115 default:
2116 return FALSE;
2119 start_sequence ();
2120 if (one_cmpl)
2121 target = expand_one_cmpl_abs_nojump (GET_MODE (if_info->x), b,
2122 if_info->x);
2123 else
2124 target = expand_abs_nojump (GET_MODE (if_info->x), b, if_info->x, 1);
2126 /* ??? It's a quandary whether cmove would be better here, especially
2127 for integers. Perhaps combine will clean things up. */
2128 if (target && negate)
2130 if (one_cmpl)
2131 target = expand_simple_unop (GET_MODE (target), NOT, target,
2132 if_info->x, 0);
2133 else
2134 target = expand_simple_unop (GET_MODE (target), NEG, target,
2135 if_info->x, 0);
2138 if (! target)
2140 end_sequence ();
2141 return FALSE;
2144 if (target != if_info->x)
2145 noce_emit_move_insn (if_info->x, target);
2147 seq = end_ifcvt_sequence (if_info);
2148 if (!seq)
2149 return FALSE;
2151 emit_insn_before_setloc (seq, if_info->jump, INSN_LOCATION (if_info->insn_a));
2152 if_info->cond = cond;
2153 if_info->cond_earliest = earliest;
2155 return TRUE;
2158 /* Convert "if (m < 0) x = b; else x = 0;" to "x = (m >> C) & b;". */
2160 static int
2161 noce_try_sign_mask (struct noce_if_info *if_info)
2163 rtx cond, t, m, c;
2164 rtx_insn *seq;
2165 enum machine_mode mode;
2166 enum rtx_code code;
2167 bool t_unconditional;
2169 cond = if_info->cond;
2170 code = GET_CODE (cond);
2171 m = XEXP (cond, 0);
2172 c = XEXP (cond, 1);
2174 t = NULL_RTX;
2175 if (if_info->a == const0_rtx)
2177 if ((code == LT && c == const0_rtx)
2178 || (code == LE && c == constm1_rtx))
2179 t = if_info->b;
2181 else if (if_info->b == const0_rtx)
2183 if ((code == GE && c == const0_rtx)
2184 || (code == GT && c == constm1_rtx))
2185 t = if_info->a;
2188 if (! t || side_effects_p (t))
2189 return FALSE;
2191 /* We currently don't handle different modes. */
2192 mode = GET_MODE (t);
2193 if (GET_MODE (m) != mode)
2194 return FALSE;
2196 /* This is only profitable if T is unconditionally executed/evaluated in the
2197 original insn sequence or T is cheap. The former happens if B is the
2198 non-zero (T) value and if INSN_B was taken from TEST_BB, or there was no
2199 INSN_B which can happen for e.g. conditional stores to memory. For the
2200 cost computation use the block TEST_BB where the evaluation will end up
2201 after the transformation. */
2202 t_unconditional =
2203 (t == if_info->b
2204 && (if_info->insn_b == NULL_RTX
2205 || BLOCK_FOR_INSN (if_info->insn_b) == if_info->test_bb));
2206 if (!(t_unconditional
2207 || (set_src_cost (t, optimize_bb_for_speed_p (if_info->test_bb))
2208 < COSTS_N_INSNS (2))))
2209 return FALSE;
2211 start_sequence ();
2212 /* Use emit_store_flag to generate "m < 0 ? -1 : 0" instead of expanding
2213 "(signed) m >> 31" directly. This benefits targets with specialized
2214 insns to obtain the signmask, but still uses ashr_optab otherwise. */
2215 m = emit_store_flag (gen_reg_rtx (mode), LT, m, const0_rtx, mode, 0, -1);
2216 t = m ? expand_binop (mode, and_optab, m, t, NULL_RTX, 0, OPTAB_DIRECT)
2217 : NULL_RTX;
2219 if (!t)
2221 end_sequence ();
2222 return FALSE;
2225 noce_emit_move_insn (if_info->x, t);
2227 seq = end_ifcvt_sequence (if_info);
2228 if (!seq)
2229 return FALSE;
2231 emit_insn_before_setloc (seq, if_info->jump, INSN_LOCATION (if_info->insn_a));
2232 return TRUE;
2236 /* Optimize away "if (x & C) x |= C" and similar bit manipulation
2237 transformations. */
2239 static int
2240 noce_try_bitop (struct noce_if_info *if_info)
2242 rtx cond, x, a, result;
2243 rtx_insn *seq;
2244 enum machine_mode mode;
2245 enum rtx_code code;
2246 int bitnum;
2248 x = if_info->x;
2249 cond = if_info->cond;
2250 code = GET_CODE (cond);
2252 /* Check for no else condition. */
2253 if (! rtx_equal_p (x, if_info->b))
2254 return FALSE;
2256 /* Check for a suitable condition. */
2257 if (code != NE && code != EQ)
2258 return FALSE;
2259 if (XEXP (cond, 1) != const0_rtx)
2260 return FALSE;
2261 cond = XEXP (cond, 0);
2263 /* ??? We could also handle AND here. */
2264 if (GET_CODE (cond) == ZERO_EXTRACT)
2266 if (XEXP (cond, 1) != const1_rtx
2267 || !CONST_INT_P (XEXP (cond, 2))
2268 || ! rtx_equal_p (x, XEXP (cond, 0)))
2269 return FALSE;
2270 bitnum = INTVAL (XEXP (cond, 2));
2271 mode = GET_MODE (x);
2272 if (BITS_BIG_ENDIAN)
2273 bitnum = GET_MODE_BITSIZE (mode) - 1 - bitnum;
2274 if (bitnum < 0 || bitnum >= HOST_BITS_PER_WIDE_INT)
2275 return FALSE;
2277 else
2278 return FALSE;
2280 a = if_info->a;
2281 if (GET_CODE (a) == IOR || GET_CODE (a) == XOR)
2283 /* Check for "if (X & C) x = x op C". */
2284 if (! rtx_equal_p (x, XEXP (a, 0))
2285 || !CONST_INT_P (XEXP (a, 1))
2286 || (INTVAL (XEXP (a, 1)) & GET_MODE_MASK (mode))
2287 != (unsigned HOST_WIDE_INT) 1 << bitnum)
2288 return FALSE;
2290 /* if ((x & C) == 0) x |= C; is transformed to x |= C. */
2291 /* if ((x & C) != 0) x |= C; is transformed to nothing. */
2292 if (GET_CODE (a) == IOR)
2293 result = (code == NE) ? a : NULL_RTX;
2294 else if (code == NE)
2296 /* if ((x & C) == 0) x ^= C; is transformed to x |= C. */
2297 result = gen_int_mode ((HOST_WIDE_INT) 1 << bitnum, mode);
2298 result = simplify_gen_binary (IOR, mode, x, result);
2300 else
2302 /* if ((x & C) != 0) x ^= C; is transformed to x &= ~C. */
2303 result = gen_int_mode (~((HOST_WIDE_INT) 1 << bitnum), mode);
2304 result = simplify_gen_binary (AND, mode, x, result);
2307 else if (GET_CODE (a) == AND)
2309 /* Check for "if (X & C) x &= ~C". */
2310 if (! rtx_equal_p (x, XEXP (a, 0))
2311 || !CONST_INT_P (XEXP (a, 1))
2312 || (INTVAL (XEXP (a, 1)) & GET_MODE_MASK (mode))
2313 != (~((HOST_WIDE_INT) 1 << bitnum) & GET_MODE_MASK (mode)))
2314 return FALSE;
2316 /* if ((x & C) == 0) x &= ~C; is transformed to nothing. */
2317 /* if ((x & C) != 0) x &= ~C; is transformed to x &= ~C. */
2318 result = (code == EQ) ? a : NULL_RTX;
2320 else
2321 return FALSE;
2323 if (result)
2325 start_sequence ();
2326 noce_emit_move_insn (x, result);
2327 seq = end_ifcvt_sequence (if_info);
2328 if (!seq)
2329 return FALSE;
2331 emit_insn_before_setloc (seq, if_info->jump,
2332 INSN_LOCATION (if_info->insn_a));
2334 return TRUE;
2338 /* Similar to get_condition, only the resulting condition must be
2339 valid at JUMP, instead of at EARLIEST.
2341 If THEN_ELSE_REVERSED is true, the fallthrough does not go to the
2342 THEN block of the caller, and we have to reverse the condition. */
2344 static rtx
2345 noce_get_condition (rtx_insn *jump, rtx_insn **earliest, bool then_else_reversed)
2347 rtx cond, set, tmp;
2348 bool reverse;
2350 if (! any_condjump_p (jump))
2351 return NULL_RTX;
2353 set = pc_set (jump);
2355 /* If this branches to JUMP_LABEL when the condition is false,
2356 reverse the condition. */
2357 reverse = (GET_CODE (XEXP (SET_SRC (set), 2)) == LABEL_REF
2358 && LABEL_REF_LABEL (XEXP (SET_SRC (set), 2)) == JUMP_LABEL (jump));
2360 /* We may have to reverse because the caller's if block is not canonical,
2361 i.e. the THEN block isn't the fallthrough block for the TEST block
2362 (see find_if_header). */
2363 if (then_else_reversed)
2364 reverse = !reverse;
2366 /* If the condition variable is a register and is MODE_INT, accept it. */
2368 cond = XEXP (SET_SRC (set), 0);
2369 tmp = XEXP (cond, 0);
2370 if (REG_P (tmp) && GET_MODE_CLASS (GET_MODE (tmp)) == MODE_INT
2371 && (GET_MODE (tmp) != BImode
2372 || !targetm.small_register_classes_for_mode_p (BImode)))
2374 *earliest = jump;
2376 if (reverse)
2377 cond = gen_rtx_fmt_ee (reverse_condition (GET_CODE (cond)),
2378 GET_MODE (cond), tmp, XEXP (cond, 1));
2379 return cond;
2382 /* Otherwise, fall back on canonicalize_condition to do the dirty
2383 work of manipulating MODE_CC values and COMPARE rtx codes. */
2384 tmp = canonicalize_condition (jump, cond, reverse, earliest,
2385 NULL_RTX, false, true);
2387 /* We don't handle side-effects in the condition, like handling
2388 REG_INC notes and making sure no duplicate conditions are emitted. */
2389 if (tmp != NULL_RTX && side_effects_p (tmp))
2390 return NULL_RTX;
2392 return tmp;
2395 /* Return true if OP is ok for if-then-else processing. */
2397 static int
2398 noce_operand_ok (const_rtx op)
2400 if (side_effects_p (op))
2401 return FALSE;
2403 /* We special-case memories, so handle any of them with
2404 no address side effects. */
2405 if (MEM_P (op))
2406 return ! side_effects_p (XEXP (op, 0));
2408 return ! may_trap_p (op);
2411 /* Return true if a write into MEM may trap or fault. */
2413 static bool
2414 noce_mem_write_may_trap_or_fault_p (const_rtx mem)
2416 rtx addr;
2418 if (MEM_READONLY_P (mem))
2419 return true;
2421 if (may_trap_or_fault_p (mem))
2422 return true;
2424 addr = XEXP (mem, 0);
2426 /* Call target hook to avoid the effects of -fpic etc.... */
2427 addr = targetm.delegitimize_address (addr);
2429 while (addr)
2430 switch (GET_CODE (addr))
2432 case CONST:
2433 case PRE_DEC:
2434 case PRE_INC:
2435 case POST_DEC:
2436 case POST_INC:
2437 case POST_MODIFY:
2438 addr = XEXP (addr, 0);
2439 break;
2440 case LO_SUM:
2441 case PRE_MODIFY:
2442 addr = XEXP (addr, 1);
2443 break;
2444 case PLUS:
2445 if (CONST_INT_P (XEXP (addr, 1)))
2446 addr = XEXP (addr, 0);
2447 else
2448 return false;
2449 break;
2450 case LABEL_REF:
2451 return true;
2452 case SYMBOL_REF:
2453 if (SYMBOL_REF_DECL (addr)
2454 && decl_readonly_section (SYMBOL_REF_DECL (addr), 0))
2455 return true;
2456 return false;
2457 default:
2458 return false;
2461 return false;
2464 /* Return whether we can use store speculation for MEM. TOP_BB is the
2465 basic block above the conditional block where we are considering
2466 doing the speculative store. We look for whether MEM is set
2467 unconditionally later in the function. */
2469 static bool
2470 noce_can_store_speculate_p (basic_block top_bb, const_rtx mem)
2472 basic_block dominator;
2474 for (dominator = get_immediate_dominator (CDI_POST_DOMINATORS, top_bb);
2475 dominator != NULL;
2476 dominator = get_immediate_dominator (CDI_POST_DOMINATORS, dominator))
2478 rtx_insn *insn;
2480 FOR_BB_INSNS (dominator, insn)
2482 /* If we see something that might be a memory barrier, we
2483 have to stop looking. Even if the MEM is set later in
2484 the function, we still don't want to set it
2485 unconditionally before the barrier. */
2486 if (INSN_P (insn)
2487 && (volatile_insn_p (PATTERN (insn))
2488 || (CALL_P (insn) && (!RTL_CONST_CALL_P (insn)))))
2489 return false;
2491 if (memory_must_be_modified_in_insn_p (mem, insn))
2492 return true;
2493 if (modified_in_p (XEXP (mem, 0), insn))
2494 return false;
2499 return false;
2502 /* Given a simple IF-THEN-JOIN or IF-THEN-ELSE-JOIN block, attempt to convert
2503 it without using conditional execution. Return TRUE if we were successful
2504 at converting the block. */
2506 static int
2507 noce_process_if_block (struct noce_if_info *if_info)
2509 basic_block test_bb = if_info->test_bb; /* test block */
2510 basic_block then_bb = if_info->then_bb; /* THEN */
2511 basic_block else_bb = if_info->else_bb; /* ELSE or NULL */
2512 basic_block join_bb = if_info->join_bb; /* JOIN */
2513 rtx_insn *jump = if_info->jump;
2514 rtx cond = if_info->cond;
2515 rtx_insn *insn_a, *insn_b;
2516 rtx set_a, set_b;
2517 rtx orig_x, x, a, b;
2519 /* We're looking for patterns of the form
2521 (1) if (...) x = a; else x = b;
2522 (2) x = b; if (...) x = a;
2523 (3) if (...) x = a; // as if with an initial x = x.
2525 The later patterns require jumps to be more expensive.
2527 ??? For future expansion, look for multiple X in such patterns. */
2529 /* Look for one of the potential sets. */
2530 insn_a = first_active_insn (then_bb);
2531 if (! insn_a
2532 || insn_a != last_active_insn (then_bb, FALSE)
2533 || (set_a = single_set (insn_a)) == NULL_RTX)
2534 return FALSE;
2536 x = SET_DEST (set_a);
2537 a = SET_SRC (set_a);
2539 /* Look for the other potential set. Make sure we've got equivalent
2540 destinations. */
2541 /* ??? This is overconservative. Storing to two different mems is
2542 as easy as conditionally computing the address. Storing to a
2543 single mem merely requires a scratch memory to use as one of the
2544 destination addresses; often the memory immediately below the
2545 stack pointer is available for this. */
2546 set_b = NULL_RTX;
2547 if (else_bb)
2549 insn_b = first_active_insn (else_bb);
2550 if (! insn_b
2551 || insn_b != last_active_insn (else_bb, FALSE)
2552 || (set_b = single_set (insn_b)) == NULL_RTX
2553 || ! rtx_interchangeable_p (x, SET_DEST (set_b)))
2554 return FALSE;
2556 else
2558 insn_b = prev_nonnote_nondebug_insn (if_info->cond_earliest);
2559 /* We're going to be moving the evaluation of B down from above
2560 COND_EARLIEST to JUMP. Make sure the relevant data is still
2561 intact. */
2562 if (! insn_b
2563 || BLOCK_FOR_INSN (insn_b) != BLOCK_FOR_INSN (if_info->cond_earliest)
2564 || !NONJUMP_INSN_P (insn_b)
2565 || (set_b = single_set (insn_b)) == NULL_RTX
2566 || ! rtx_interchangeable_p (x, SET_DEST (set_b))
2567 || ! noce_operand_ok (SET_SRC (set_b))
2568 || reg_overlap_mentioned_p (x, SET_SRC (set_b))
2569 || modified_between_p (SET_SRC (set_b), insn_b, jump)
2570 /* Avoid extending the lifetime of hard registers on small
2571 register class machines. */
2572 || (REG_P (SET_SRC (set_b))
2573 && HARD_REGISTER_P (SET_SRC (set_b))
2574 && targetm.small_register_classes_for_mode_p
2575 (GET_MODE (SET_SRC (set_b))))
2576 /* Likewise with X. In particular this can happen when
2577 noce_get_condition looks farther back in the instruction
2578 stream than one might expect. */
2579 || reg_overlap_mentioned_p (x, cond)
2580 || reg_overlap_mentioned_p (x, a)
2581 || modified_between_p (x, insn_b, jump))
2583 insn_b = NULL;
2584 set_b = NULL_RTX;
2588 /* If x has side effects then only the if-then-else form is safe to
2589 convert. But even in that case we would need to restore any notes
2590 (such as REG_INC) at then end. That can be tricky if
2591 noce_emit_move_insn expands to more than one insn, so disable the
2592 optimization entirely for now if there are side effects. */
2593 if (side_effects_p (x))
2594 return FALSE;
2596 b = (set_b ? SET_SRC (set_b) : x);
2598 /* Only operate on register destinations, and even then avoid extending
2599 the lifetime of hard registers on small register class machines. */
2600 orig_x = x;
2601 if (!REG_P (x)
2602 || (HARD_REGISTER_P (x)
2603 && targetm.small_register_classes_for_mode_p (GET_MODE (x))))
2605 if (GET_MODE (x) == BLKmode)
2606 return FALSE;
2608 if (GET_CODE (x) == ZERO_EXTRACT
2609 && (!CONST_INT_P (XEXP (x, 1))
2610 || !CONST_INT_P (XEXP (x, 2))))
2611 return FALSE;
2613 x = gen_reg_rtx (GET_MODE (GET_CODE (x) == STRICT_LOW_PART
2614 ? XEXP (x, 0) : x));
2617 /* Don't operate on sources that may trap or are volatile. */
2618 if (! noce_operand_ok (a) || ! noce_operand_ok (b))
2619 return FALSE;
2621 retry:
2622 /* Set up the info block for our subroutines. */
2623 if_info->insn_a = insn_a;
2624 if_info->insn_b = insn_b;
2625 if_info->x = x;
2626 if_info->a = a;
2627 if_info->b = b;
2629 /* Try optimizations in some approximation of a useful order. */
2630 /* ??? Should first look to see if X is live incoming at all. If it
2631 isn't, we don't need anything but an unconditional set. */
2633 /* Look and see if A and B are really the same. Avoid creating silly
2634 cmove constructs that no one will fix up later. */
2635 if (rtx_interchangeable_p (a, b))
2637 /* If we have an INSN_B, we don't have to create any new rtl. Just
2638 move the instruction that we already have. If we don't have an
2639 INSN_B, that means that A == X, and we've got a noop move. In
2640 that case don't do anything and let the code below delete INSN_A. */
2641 if (insn_b && else_bb)
2643 rtx note;
2645 if (else_bb && insn_b == BB_END (else_bb))
2646 BB_END (else_bb) = PREV_INSN (insn_b);
2647 reorder_insns (insn_b, insn_b, PREV_INSN (jump));
2649 /* If there was a REG_EQUAL note, delete it since it may have been
2650 true due to this insn being after a jump. */
2651 if ((note = find_reg_note (insn_b, REG_EQUAL, NULL_RTX)) != 0)
2652 remove_note (insn_b, note);
2654 insn_b = NULL;
2656 /* If we have "x = b; if (...) x = a;", and x has side-effects, then
2657 x must be executed twice. */
2658 else if (insn_b && side_effects_p (orig_x))
2659 return FALSE;
2661 x = orig_x;
2662 goto success;
2665 if (!set_b && MEM_P (orig_x))
2667 /* Disallow the "if (...) x = a;" form (implicit "else x = x;")
2668 for optimizations if writing to x may trap or fault,
2669 i.e. it's a memory other than a static var or a stack slot,
2670 is misaligned on strict aligned machines or is read-only. If
2671 x is a read-only memory, then the program is valid only if we
2672 avoid the store into it. If there are stores on both the
2673 THEN and ELSE arms, then we can go ahead with the conversion;
2674 either the program is broken, or the condition is always
2675 false such that the other memory is selected. */
2676 if (noce_mem_write_may_trap_or_fault_p (orig_x))
2677 return FALSE;
2679 /* Avoid store speculation: given "if (...) x = a" where x is a
2680 MEM, we only want to do the store if x is always set
2681 somewhere in the function. This avoids cases like
2682 if (pthread_mutex_trylock(mutex))
2683 ++global_variable;
2684 where we only want global_variable to be changed if the mutex
2685 is held. FIXME: This should ideally be expressed directly in
2686 RTL somehow. */
2687 if (!noce_can_store_speculate_p (test_bb, orig_x))
2688 return FALSE;
2691 if (noce_try_move (if_info))
2692 goto success;
2693 if (noce_try_store_flag (if_info))
2694 goto success;
2695 if (noce_try_bitop (if_info))
2696 goto success;
2697 if (noce_try_minmax (if_info))
2698 goto success;
2699 if (noce_try_abs (if_info))
2700 goto success;
2701 if (HAVE_conditional_move
2702 && noce_try_cmove (if_info))
2703 goto success;
2704 if (! targetm.have_conditional_execution ())
2706 if (noce_try_store_flag_constants (if_info))
2707 goto success;
2708 if (noce_try_addcc (if_info))
2709 goto success;
2710 if (noce_try_store_flag_mask (if_info))
2711 goto success;
2712 if (HAVE_conditional_move
2713 && noce_try_cmove_arith (if_info))
2714 goto success;
2715 if (noce_try_sign_mask (if_info))
2716 goto success;
2719 if (!else_bb && set_b)
2721 insn_b = NULL;
2722 set_b = NULL_RTX;
2723 b = orig_x;
2724 goto retry;
2727 return FALSE;
2729 success:
2731 /* If we used a temporary, fix it up now. */
2732 if (orig_x != x)
2734 rtx_insn *seq;
2736 start_sequence ();
2737 noce_emit_move_insn (orig_x, x);
2738 seq = get_insns ();
2739 set_used_flags (orig_x);
2740 unshare_all_rtl_in_chain (seq);
2741 end_sequence ();
2743 emit_insn_before_setloc (seq, BB_END (test_bb), INSN_LOCATION (insn_a));
2746 /* The original THEN and ELSE blocks may now be removed. The test block
2747 must now jump to the join block. If the test block and the join block
2748 can be merged, do so. */
2749 if (else_bb)
2751 delete_basic_block (else_bb);
2752 num_true_changes++;
2754 else
2755 remove_edge (find_edge (test_bb, join_bb));
2757 remove_edge (find_edge (then_bb, join_bb));
2758 redirect_edge_and_branch_force (single_succ_edge (test_bb), join_bb);
2759 delete_basic_block (then_bb);
2760 num_true_changes++;
2762 if (can_merge_blocks_p (test_bb, join_bb))
2764 merge_blocks (test_bb, join_bb);
2765 num_true_changes++;
2768 num_updated_if_blocks++;
2769 return TRUE;
2772 /* Check whether a block is suitable for conditional move conversion.
2773 Every insn must be a simple set of a register to a constant or a
2774 register. For each assignment, store the value in the pointer map
2775 VALS, keyed indexed by register pointer, then store the register
2776 pointer in REGS. COND is the condition we will test. */
2778 static int
2779 check_cond_move_block (basic_block bb,
2780 hash_map<rtx, rtx> *vals,
2781 vec<rtx> *regs,
2782 rtx cond)
2784 rtx_insn *insn;
2786 /* We can only handle simple jumps at the end of the basic block.
2787 It is almost impossible to update the CFG otherwise. */
2788 insn = BB_END (bb);
2789 if (JUMP_P (insn) && !onlyjump_p (insn))
2790 return FALSE;
2792 FOR_BB_INSNS (bb, insn)
2794 rtx set, dest, src;
2796 if (!NONDEBUG_INSN_P (insn) || JUMP_P (insn))
2797 continue;
2798 set = single_set (insn);
2799 if (!set)
2800 return FALSE;
2802 dest = SET_DEST (set);
2803 src = SET_SRC (set);
2804 if (!REG_P (dest)
2805 || (HARD_REGISTER_P (dest)
2806 && targetm.small_register_classes_for_mode_p (GET_MODE (dest))))
2807 return FALSE;
2809 if (!CONSTANT_P (src) && !register_operand (src, VOIDmode))
2810 return FALSE;
2812 if (side_effects_p (src) || side_effects_p (dest))
2813 return FALSE;
2815 if (may_trap_p (src) || may_trap_p (dest))
2816 return FALSE;
2818 /* Don't try to handle this if the source register was
2819 modified earlier in the block. */
2820 if ((REG_P (src)
2821 && vals->get (src))
2822 || (GET_CODE (src) == SUBREG && REG_P (SUBREG_REG (src))
2823 && vals->get (SUBREG_REG (src))))
2824 return FALSE;
2826 /* Don't try to handle this if the destination register was
2827 modified earlier in the block. */
2828 if (vals->get (dest))
2829 return FALSE;
2831 /* Don't try to handle this if the condition uses the
2832 destination register. */
2833 if (reg_overlap_mentioned_p (dest, cond))
2834 return FALSE;
2836 /* Don't try to handle this if the source register is modified
2837 later in the block. */
2838 if (!CONSTANT_P (src)
2839 && modified_between_p (src, insn, NEXT_INSN (BB_END (bb))))
2840 return FALSE;
2842 vals->put (dest, src);
2844 regs->safe_push (dest);
2847 return TRUE;
2850 /* Given a basic block BB suitable for conditional move conversion,
2851 a condition COND, and pointer maps THEN_VALS and ELSE_VALS containing
2852 the register values depending on COND, emit the insns in the block as
2853 conditional moves. If ELSE_BLOCK is true, THEN_BB was already
2854 processed. The caller has started a sequence for the conversion.
2855 Return true if successful, false if something goes wrong. */
2857 static bool
2858 cond_move_convert_if_block (struct noce_if_info *if_infop,
2859 basic_block bb, rtx cond,
2860 hash_map<rtx, rtx> *then_vals,
2861 hash_map<rtx, rtx> *else_vals,
2862 bool else_block_p)
2864 enum rtx_code code;
2865 rtx_insn *insn;
2866 rtx cond_arg0, cond_arg1;
2868 code = GET_CODE (cond);
2869 cond_arg0 = XEXP (cond, 0);
2870 cond_arg1 = XEXP (cond, 1);
2872 FOR_BB_INSNS (bb, insn)
2874 rtx set, target, dest, t, e;
2876 /* ??? Maybe emit conditional debug insn? */
2877 if (!NONDEBUG_INSN_P (insn) || JUMP_P (insn))
2878 continue;
2879 set = single_set (insn);
2880 gcc_assert (set && REG_P (SET_DEST (set)));
2882 dest = SET_DEST (set);
2884 rtx *then_slot = then_vals->get (dest);
2885 rtx *else_slot = else_vals->get (dest);
2886 t = then_slot ? *then_slot : NULL_RTX;
2887 e = else_slot ? *else_slot : NULL_RTX;
2889 if (else_block_p)
2891 /* If this register was set in the then block, we already
2892 handled this case there. */
2893 if (t)
2894 continue;
2895 t = dest;
2896 gcc_assert (e);
2898 else
2900 gcc_assert (t);
2901 if (!e)
2902 e = dest;
2905 target = noce_emit_cmove (if_infop, dest, code, cond_arg0, cond_arg1,
2906 t, e);
2907 if (!target)
2908 return false;
2910 if (target != dest)
2911 noce_emit_move_insn (dest, target);
2914 return true;
2917 /* Given a simple IF-THEN-JOIN or IF-THEN-ELSE-JOIN block, attempt to convert
2918 it using only conditional moves. Return TRUE if we were successful at
2919 converting the block. */
2921 static int
2922 cond_move_process_if_block (struct noce_if_info *if_info)
2924 basic_block test_bb = if_info->test_bb;
2925 basic_block then_bb = if_info->then_bb;
2926 basic_block else_bb = if_info->else_bb;
2927 basic_block join_bb = if_info->join_bb;
2928 rtx_insn *jump = if_info->jump;
2929 rtx cond = if_info->cond;
2930 rtx_insn *seq, *loc_insn;
2931 rtx reg;
2932 int c;
2933 vec<rtx> then_regs = vNULL;
2934 vec<rtx> else_regs = vNULL;
2935 unsigned int i;
2936 int success_p = FALSE;
2938 /* Build a mapping for each block to the value used for each
2939 register. */
2940 hash_map<rtx, rtx> then_vals;
2941 hash_map<rtx, rtx> else_vals;
2943 /* Make sure the blocks are suitable. */
2944 if (!check_cond_move_block (then_bb, &then_vals, &then_regs, cond)
2945 || (else_bb
2946 && !check_cond_move_block (else_bb, &else_vals, &else_regs, cond)))
2947 goto done;
2949 /* Make sure the blocks can be used together. If the same register
2950 is set in both blocks, and is not set to a constant in both
2951 cases, then both blocks must set it to the same register. We
2952 have already verified that if it is set to a register, that the
2953 source register does not change after the assignment. Also count
2954 the number of registers set in only one of the blocks. */
2955 c = 0;
2956 FOR_EACH_VEC_ELT (then_regs, i, reg)
2958 rtx *then_slot = then_vals.get (reg);
2959 rtx *else_slot = else_vals.get (reg);
2961 gcc_checking_assert (then_slot);
2962 if (!else_slot)
2963 ++c;
2964 else
2966 rtx then_val = *then_slot;
2967 rtx else_val = *else_slot;
2968 if (!CONSTANT_P (then_val) && !CONSTANT_P (else_val)
2969 && !rtx_equal_p (then_val, else_val))
2970 goto done;
2974 /* Finish off c for MAX_CONDITIONAL_EXECUTE. */
2975 FOR_EACH_VEC_ELT (else_regs, i, reg)
2977 gcc_checking_assert (else_vals.get (reg));
2978 if (!then_vals.get (reg))
2979 ++c;
2982 /* Make sure it is reasonable to convert this block. What matters
2983 is the number of assignments currently made in only one of the
2984 branches, since if we convert we are going to always execute
2985 them. */
2986 if (c > MAX_CONDITIONAL_EXECUTE)
2987 goto done;
2989 /* Try to emit the conditional moves. First do the then block,
2990 then do anything left in the else blocks. */
2991 start_sequence ();
2992 if (!cond_move_convert_if_block (if_info, then_bb, cond,
2993 &then_vals, &else_vals, false)
2994 || (else_bb
2995 && !cond_move_convert_if_block (if_info, else_bb, cond,
2996 &then_vals, &else_vals, true)))
2998 end_sequence ();
2999 goto done;
3001 seq = end_ifcvt_sequence (if_info);
3002 if (!seq)
3003 goto done;
3005 loc_insn = first_active_insn (then_bb);
3006 if (!loc_insn)
3008 loc_insn = first_active_insn (else_bb);
3009 gcc_assert (loc_insn);
3011 emit_insn_before_setloc (seq, jump, INSN_LOCATION (loc_insn));
3013 if (else_bb)
3015 delete_basic_block (else_bb);
3016 num_true_changes++;
3018 else
3019 remove_edge (find_edge (test_bb, join_bb));
3021 remove_edge (find_edge (then_bb, join_bb));
3022 redirect_edge_and_branch_force (single_succ_edge (test_bb), join_bb);
3023 delete_basic_block (then_bb);
3024 num_true_changes++;
3026 if (can_merge_blocks_p (test_bb, join_bb))
3028 merge_blocks (test_bb, join_bb);
3029 num_true_changes++;
3032 num_updated_if_blocks++;
3034 success_p = TRUE;
3036 done:
3037 then_regs.release ();
3038 else_regs.release ();
3039 return success_p;
3043 /* Determine if a given basic block heads a simple IF-THEN-JOIN or an
3044 IF-THEN-ELSE-JOIN block.
3046 If so, we'll try to convert the insns to not require the branch,
3047 using only transformations that do not require conditional execution.
3049 Return TRUE if we were successful at converting the block. */
3051 static int
3052 noce_find_if_block (basic_block test_bb, edge then_edge, edge else_edge,
3053 int pass)
3055 basic_block then_bb, else_bb, join_bb;
3056 bool then_else_reversed = false;
3057 rtx_insn *jump;
3058 rtx cond;
3059 rtx_insn *cond_earliest;
3060 struct noce_if_info if_info;
3062 /* We only ever should get here before reload. */
3063 gcc_assert (!reload_completed);
3065 /* Recognize an IF-THEN-ELSE-JOIN block. */
3066 if (single_pred_p (then_edge->dest)
3067 && single_succ_p (then_edge->dest)
3068 && single_pred_p (else_edge->dest)
3069 && single_succ_p (else_edge->dest)
3070 && single_succ (then_edge->dest) == single_succ (else_edge->dest))
3072 then_bb = then_edge->dest;
3073 else_bb = else_edge->dest;
3074 join_bb = single_succ (then_bb);
3076 /* Recognize an IF-THEN-JOIN block. */
3077 else if (single_pred_p (then_edge->dest)
3078 && single_succ_p (then_edge->dest)
3079 && single_succ (then_edge->dest) == else_edge->dest)
3081 then_bb = then_edge->dest;
3082 else_bb = NULL_BLOCK;
3083 join_bb = else_edge->dest;
3085 /* Recognize an IF-ELSE-JOIN block. We can have those because the order
3086 of basic blocks in cfglayout mode does not matter, so the fallthrough
3087 edge can go to any basic block (and not just to bb->next_bb, like in
3088 cfgrtl mode). */
3089 else if (single_pred_p (else_edge->dest)
3090 && single_succ_p (else_edge->dest)
3091 && single_succ (else_edge->dest) == then_edge->dest)
3093 /* The noce transformations do not apply to IF-ELSE-JOIN blocks.
3094 To make this work, we have to invert the THEN and ELSE blocks
3095 and reverse the jump condition. */
3096 then_bb = else_edge->dest;
3097 else_bb = NULL_BLOCK;
3098 join_bb = single_succ (then_bb);
3099 then_else_reversed = true;
3101 else
3102 /* Not a form we can handle. */
3103 return FALSE;
3105 /* The edges of the THEN and ELSE blocks cannot have complex edges. */
3106 if (single_succ_edge (then_bb)->flags & EDGE_COMPLEX)
3107 return FALSE;
3108 if (else_bb
3109 && single_succ_edge (else_bb)->flags & EDGE_COMPLEX)
3110 return FALSE;
3112 num_possible_if_blocks++;
3114 if (dump_file)
3116 fprintf (dump_file,
3117 "\nIF-THEN%s-JOIN block found, pass %d, test %d, then %d",
3118 (else_bb) ? "-ELSE" : "",
3119 pass, test_bb->index, then_bb->index);
3121 if (else_bb)
3122 fprintf (dump_file, ", else %d", else_bb->index);
3124 fprintf (dump_file, ", join %d\n", join_bb->index);
3127 /* If the conditional jump is more than just a conditional
3128 jump, then we can not do if-conversion on this block. */
3129 jump = BB_END (test_bb);
3130 if (! onlyjump_p (jump))
3131 return FALSE;
3133 /* If this is not a standard conditional jump, we can't parse it. */
3134 cond = noce_get_condition (jump, &cond_earliest, then_else_reversed);
3135 if (!cond)
3136 return FALSE;
3138 /* We must be comparing objects whose modes imply the size. */
3139 if (GET_MODE (XEXP (cond, 0)) == BLKmode)
3140 return FALSE;
3142 /* Initialize an IF_INFO struct to pass around. */
3143 memset (&if_info, 0, sizeof if_info);
3144 if_info.test_bb = test_bb;
3145 if_info.then_bb = then_bb;
3146 if_info.else_bb = else_bb;
3147 if_info.join_bb = join_bb;
3148 if_info.cond = cond;
3149 if_info.cond_earliest = cond_earliest;
3150 if_info.jump = jump;
3151 if_info.then_else_reversed = then_else_reversed;
3152 if_info.branch_cost = BRANCH_COST (optimize_bb_for_speed_p (test_bb),
3153 predictable_edge_p (then_edge));
3155 /* Do the real work. */
3157 if (noce_process_if_block (&if_info))
3158 return TRUE;
3160 if (HAVE_conditional_move
3161 && cond_move_process_if_block (&if_info))
3162 return TRUE;
3164 return FALSE;
3168 /* Merge the blocks and mark for local life update. */
3170 static void
3171 merge_if_block (struct ce_if_block * ce_info)
3173 basic_block test_bb = ce_info->test_bb; /* last test block */
3174 basic_block then_bb = ce_info->then_bb; /* THEN */
3175 basic_block else_bb = ce_info->else_bb; /* ELSE or NULL */
3176 basic_block join_bb = ce_info->join_bb; /* join block */
3177 basic_block combo_bb;
3179 /* All block merging is done into the lower block numbers. */
3181 combo_bb = test_bb;
3182 df_set_bb_dirty (test_bb);
3184 /* Merge any basic blocks to handle && and || subtests. Each of
3185 the blocks are on the fallthru path from the predecessor block. */
3186 if (ce_info->num_multiple_test_blocks > 0)
3188 basic_block bb = test_bb;
3189 basic_block last_test_bb = ce_info->last_test_bb;
3190 basic_block fallthru = block_fallthru (bb);
3194 bb = fallthru;
3195 fallthru = block_fallthru (bb);
3196 merge_blocks (combo_bb, bb);
3197 num_true_changes++;
3199 while (bb != last_test_bb);
3202 /* Merge TEST block into THEN block. Normally the THEN block won't have a
3203 label, but it might if there were || tests. That label's count should be
3204 zero, and it normally should be removed. */
3206 if (then_bb)
3208 /* If THEN_BB has no successors, then there's a BARRIER after it.
3209 If COMBO_BB has more than one successor (THEN_BB), then that BARRIER
3210 is no longer needed, and in fact it is incorrect to leave it in
3211 the insn stream. */
3212 if (EDGE_COUNT (then_bb->succs) == 0
3213 && EDGE_COUNT (combo_bb->succs) > 1)
3215 rtx_insn *end = NEXT_INSN (BB_END (then_bb));
3216 while (end && NOTE_P (end) && !NOTE_INSN_BASIC_BLOCK_P (end))
3217 end = NEXT_INSN (end);
3219 if (end && BARRIER_P (end))
3220 delete_insn (end);
3222 merge_blocks (combo_bb, then_bb);
3223 num_true_changes++;
3226 /* The ELSE block, if it existed, had a label. That label count
3227 will almost always be zero, but odd things can happen when labels
3228 get their addresses taken. */
3229 if (else_bb)
3231 /* If ELSE_BB has no successors, then there's a BARRIER after it.
3232 If COMBO_BB has more than one successor (ELSE_BB), then that BARRIER
3233 is no longer needed, and in fact it is incorrect to leave it in
3234 the insn stream. */
3235 if (EDGE_COUNT (else_bb->succs) == 0
3236 && EDGE_COUNT (combo_bb->succs) > 1)
3238 rtx_insn *end = NEXT_INSN (BB_END (else_bb));
3239 while (end && NOTE_P (end) && !NOTE_INSN_BASIC_BLOCK_P (end))
3240 end = NEXT_INSN (end);
3242 if (end && BARRIER_P (end))
3243 delete_insn (end);
3245 merge_blocks (combo_bb, else_bb);
3246 num_true_changes++;
3249 /* If there was no join block reported, that means it was not adjacent
3250 to the others, and so we cannot merge them. */
3252 if (! join_bb)
3254 rtx_insn *last = BB_END (combo_bb);
3256 /* The outgoing edge for the current COMBO block should already
3257 be correct. Verify this. */
3258 if (EDGE_COUNT (combo_bb->succs) == 0)
3259 gcc_assert (find_reg_note (last, REG_NORETURN, NULL)
3260 || (NONJUMP_INSN_P (last)
3261 && GET_CODE (PATTERN (last)) == TRAP_IF
3262 && (TRAP_CONDITION (PATTERN (last))
3263 == const_true_rtx)));
3265 else
3266 /* There should still be something at the end of the THEN or ELSE
3267 blocks taking us to our final destination. */
3268 gcc_assert (JUMP_P (last)
3269 || (EDGE_SUCC (combo_bb, 0)->dest
3270 == EXIT_BLOCK_PTR_FOR_FN (cfun)
3271 && CALL_P (last)
3272 && SIBLING_CALL_P (last))
3273 || ((EDGE_SUCC (combo_bb, 0)->flags & EDGE_EH)
3274 && can_throw_internal (last)));
3277 /* The JOIN block may have had quite a number of other predecessors too.
3278 Since we've already merged the TEST, THEN and ELSE blocks, we should
3279 have only one remaining edge from our if-then-else diamond. If there
3280 is more than one remaining edge, it must come from elsewhere. There
3281 may be zero incoming edges if the THEN block didn't actually join
3282 back up (as with a call to a non-return function). */
3283 else if (EDGE_COUNT (join_bb->preds) < 2
3284 && join_bb != EXIT_BLOCK_PTR_FOR_FN (cfun))
3286 /* We can merge the JOIN cleanly and update the dataflow try
3287 again on this pass.*/
3288 merge_blocks (combo_bb, join_bb);
3289 num_true_changes++;
3291 else
3293 /* We cannot merge the JOIN. */
3295 /* The outgoing edge for the current COMBO block should already
3296 be correct. Verify this. */
3297 gcc_assert (single_succ_p (combo_bb)
3298 && single_succ (combo_bb) == join_bb);
3300 /* Remove the jump and cruft from the end of the COMBO block. */
3301 if (join_bb != EXIT_BLOCK_PTR_FOR_FN (cfun))
3302 tidy_fallthru_edge (single_succ_edge (combo_bb));
3305 num_updated_if_blocks++;
3308 /* Find a block ending in a simple IF condition and try to transform it
3309 in some way. When converting a multi-block condition, put the new code
3310 in the first such block and delete the rest. Return a pointer to this
3311 first block if some transformation was done. Return NULL otherwise. */
3313 static basic_block
3314 find_if_header (basic_block test_bb, int pass)
3316 ce_if_block ce_info;
3317 edge then_edge;
3318 edge else_edge;
3320 /* The kind of block we're looking for has exactly two successors. */
3321 if (EDGE_COUNT (test_bb->succs) != 2)
3322 return NULL;
3324 then_edge = EDGE_SUCC (test_bb, 0);
3325 else_edge = EDGE_SUCC (test_bb, 1);
3327 if (df_get_bb_dirty (then_edge->dest))
3328 return NULL;
3329 if (df_get_bb_dirty (else_edge->dest))
3330 return NULL;
3332 /* Neither edge should be abnormal. */
3333 if ((then_edge->flags & EDGE_COMPLEX)
3334 || (else_edge->flags & EDGE_COMPLEX))
3335 return NULL;
3337 /* Nor exit the loop. */
3338 if ((then_edge->flags & EDGE_LOOP_EXIT)
3339 || (else_edge->flags & EDGE_LOOP_EXIT))
3340 return NULL;
3342 /* The THEN edge is canonically the one that falls through. */
3343 if (then_edge->flags & EDGE_FALLTHRU)
3345 else if (else_edge->flags & EDGE_FALLTHRU)
3347 edge e = else_edge;
3348 else_edge = then_edge;
3349 then_edge = e;
3351 else
3352 /* Otherwise this must be a multiway branch of some sort. */
3353 return NULL;
3355 memset (&ce_info, 0, sizeof (ce_info));
3356 ce_info.test_bb = test_bb;
3357 ce_info.then_bb = then_edge->dest;
3358 ce_info.else_bb = else_edge->dest;
3359 ce_info.pass = pass;
3361 #ifdef IFCVT_MACHDEP_INIT
3362 IFCVT_MACHDEP_INIT (&ce_info);
3363 #endif
3365 if (!reload_completed
3366 && noce_find_if_block (test_bb, then_edge, else_edge, pass))
3367 goto success;
3369 if (reload_completed
3370 && targetm.have_conditional_execution ()
3371 && cond_exec_find_if_block (&ce_info))
3372 goto success;
3374 if (HAVE_trap
3375 && optab_handler (ctrap_optab, word_mode) != CODE_FOR_nothing
3376 && find_cond_trap (test_bb, then_edge, else_edge))
3377 goto success;
3379 if (dom_info_state (CDI_POST_DOMINATORS) >= DOM_NO_FAST_QUERY
3380 && (reload_completed || !targetm.have_conditional_execution ()))
3382 if (find_if_case_1 (test_bb, then_edge, else_edge))
3383 goto success;
3384 if (find_if_case_2 (test_bb, then_edge, else_edge))
3385 goto success;
3388 return NULL;
3390 success:
3391 if (dump_file)
3392 fprintf (dump_file, "Conversion succeeded on pass %d.\n", pass);
3393 /* Set this so we continue looking. */
3394 cond_exec_changed_p = TRUE;
3395 return ce_info.test_bb;
3398 /* Return true if a block has two edges, one of which falls through to the next
3399 block, and the other jumps to a specific block, so that we can tell if the
3400 block is part of an && test or an || test. Returns either -1 or the number
3401 of non-note, non-jump, non-USE/CLOBBER insns in the block. */
3403 static int
3404 block_jumps_and_fallthru_p (basic_block cur_bb, basic_block target_bb)
3406 edge cur_edge;
3407 int fallthru_p = FALSE;
3408 int jump_p = FALSE;
3409 rtx_insn *insn;
3410 rtx_insn *end;
3411 int n_insns = 0;
3412 edge_iterator ei;
3414 if (!cur_bb || !target_bb)
3415 return -1;
3417 /* If no edges, obviously it doesn't jump or fallthru. */
3418 if (EDGE_COUNT (cur_bb->succs) == 0)
3419 return FALSE;
3421 FOR_EACH_EDGE (cur_edge, ei, cur_bb->succs)
3423 if (cur_edge->flags & EDGE_COMPLEX)
3424 /* Anything complex isn't what we want. */
3425 return -1;
3427 else if (cur_edge->flags & EDGE_FALLTHRU)
3428 fallthru_p = TRUE;
3430 else if (cur_edge->dest == target_bb)
3431 jump_p = TRUE;
3433 else
3434 return -1;
3437 if ((jump_p & fallthru_p) == 0)
3438 return -1;
3440 /* Don't allow calls in the block, since this is used to group && and ||
3441 together for conditional execution support. ??? we should support
3442 conditional execution support across calls for IA-64 some day, but
3443 for now it makes the code simpler. */
3444 end = BB_END (cur_bb);
3445 insn = BB_HEAD (cur_bb);
3447 while (insn != NULL_RTX)
3449 if (CALL_P (insn))
3450 return -1;
3452 if (INSN_P (insn)
3453 && !JUMP_P (insn)
3454 && !DEBUG_INSN_P (insn)
3455 && GET_CODE (PATTERN (insn)) != USE
3456 && GET_CODE (PATTERN (insn)) != CLOBBER)
3457 n_insns++;
3459 if (insn == end)
3460 break;
3462 insn = NEXT_INSN (insn);
3465 return n_insns;
3468 /* Determine if a given basic block heads a simple IF-THEN or IF-THEN-ELSE
3469 block. If so, we'll try to convert the insns to not require the branch.
3470 Return TRUE if we were successful at converting the block. */
3472 static int
3473 cond_exec_find_if_block (struct ce_if_block * ce_info)
3475 basic_block test_bb = ce_info->test_bb;
3476 basic_block then_bb = ce_info->then_bb;
3477 basic_block else_bb = ce_info->else_bb;
3478 basic_block join_bb = NULL_BLOCK;
3479 edge cur_edge;
3480 basic_block next;
3481 edge_iterator ei;
3483 ce_info->last_test_bb = test_bb;
3485 /* We only ever should get here after reload,
3486 and if we have conditional execution. */
3487 gcc_assert (reload_completed && targetm.have_conditional_execution ());
3489 /* Discover if any fall through predecessors of the current test basic block
3490 were && tests (which jump to the else block) or || tests (which jump to
3491 the then block). */
3492 if (single_pred_p (test_bb)
3493 && single_pred_edge (test_bb)->flags == EDGE_FALLTHRU)
3495 basic_block bb = single_pred (test_bb);
3496 basic_block target_bb;
3497 int max_insns = MAX_CONDITIONAL_EXECUTE;
3498 int n_insns;
3500 /* Determine if the preceding block is an && or || block. */
3501 if ((n_insns = block_jumps_and_fallthru_p (bb, else_bb)) >= 0)
3503 ce_info->and_and_p = TRUE;
3504 target_bb = else_bb;
3506 else if ((n_insns = block_jumps_and_fallthru_p (bb, then_bb)) >= 0)
3508 ce_info->and_and_p = FALSE;
3509 target_bb = then_bb;
3511 else
3512 target_bb = NULL_BLOCK;
3514 if (target_bb && n_insns <= max_insns)
3516 int total_insns = 0;
3517 int blocks = 0;
3519 ce_info->last_test_bb = test_bb;
3521 /* Found at least one && or || block, look for more. */
3524 ce_info->test_bb = test_bb = bb;
3525 total_insns += n_insns;
3526 blocks++;
3528 if (!single_pred_p (bb))
3529 break;
3531 bb = single_pred (bb);
3532 n_insns = block_jumps_and_fallthru_p (bb, target_bb);
3534 while (n_insns >= 0 && (total_insns + n_insns) <= max_insns);
3536 ce_info->num_multiple_test_blocks = blocks;
3537 ce_info->num_multiple_test_insns = total_insns;
3539 if (ce_info->and_and_p)
3540 ce_info->num_and_and_blocks = blocks;
3541 else
3542 ce_info->num_or_or_blocks = blocks;
3546 /* The THEN block of an IF-THEN combo must have exactly one predecessor,
3547 other than any || blocks which jump to the THEN block. */
3548 if ((EDGE_COUNT (then_bb->preds) - ce_info->num_or_or_blocks) != 1)
3549 return FALSE;
3551 /* The edges of the THEN and ELSE blocks cannot have complex edges. */
3552 FOR_EACH_EDGE (cur_edge, ei, then_bb->preds)
3554 if (cur_edge->flags & EDGE_COMPLEX)
3555 return FALSE;
3558 FOR_EACH_EDGE (cur_edge, ei, else_bb->preds)
3560 if (cur_edge->flags & EDGE_COMPLEX)
3561 return FALSE;
3564 /* The THEN block of an IF-THEN combo must have zero or one successors. */
3565 if (EDGE_COUNT (then_bb->succs) > 0
3566 && (!single_succ_p (then_bb)
3567 || (single_succ_edge (then_bb)->flags & EDGE_COMPLEX)
3568 || (epilogue_completed
3569 && tablejump_p (BB_END (then_bb), NULL, NULL))))
3570 return FALSE;
3572 /* If the THEN block has no successors, conditional execution can still
3573 make a conditional call. Don't do this unless the ELSE block has
3574 only one incoming edge -- the CFG manipulation is too ugly otherwise.
3575 Check for the last insn of the THEN block being an indirect jump, which
3576 is listed as not having any successors, but confuses the rest of the CE
3577 code processing. ??? we should fix this in the future. */
3578 if (EDGE_COUNT (then_bb->succs) == 0)
3580 if (single_pred_p (else_bb) && else_bb != EXIT_BLOCK_PTR_FOR_FN (cfun))
3582 rtx_insn *last_insn = BB_END (then_bb);
3584 while (last_insn
3585 && NOTE_P (last_insn)
3586 && last_insn != BB_HEAD (then_bb))
3587 last_insn = PREV_INSN (last_insn);
3589 if (last_insn
3590 && JUMP_P (last_insn)
3591 && ! simplejump_p (last_insn))
3592 return FALSE;
3594 join_bb = else_bb;
3595 else_bb = NULL_BLOCK;
3597 else
3598 return FALSE;
3601 /* If the THEN block's successor is the other edge out of the TEST block,
3602 then we have an IF-THEN combo without an ELSE. */
3603 else if (single_succ (then_bb) == else_bb)
3605 join_bb = else_bb;
3606 else_bb = NULL_BLOCK;
3609 /* If the THEN and ELSE block meet in a subsequent block, and the ELSE
3610 has exactly one predecessor and one successor, and the outgoing edge
3611 is not complex, then we have an IF-THEN-ELSE combo. */
3612 else if (single_succ_p (else_bb)
3613 && single_succ (then_bb) == single_succ (else_bb)
3614 && single_pred_p (else_bb)
3615 && !(single_succ_edge (else_bb)->flags & EDGE_COMPLEX)
3616 && !(epilogue_completed
3617 && tablejump_p (BB_END (else_bb), NULL, NULL)))
3618 join_bb = single_succ (else_bb);
3620 /* Otherwise it is not an IF-THEN or IF-THEN-ELSE combination. */
3621 else
3622 return FALSE;
3624 num_possible_if_blocks++;
3626 if (dump_file)
3628 fprintf (dump_file,
3629 "\nIF-THEN%s block found, pass %d, start block %d "
3630 "[insn %d], then %d [%d]",
3631 (else_bb) ? "-ELSE" : "",
3632 ce_info->pass,
3633 test_bb->index,
3634 BB_HEAD (test_bb) ? (int)INSN_UID (BB_HEAD (test_bb)) : -1,
3635 then_bb->index,
3636 BB_HEAD (then_bb) ? (int)INSN_UID (BB_HEAD (then_bb)) : -1);
3638 if (else_bb)
3639 fprintf (dump_file, ", else %d [%d]",
3640 else_bb->index,
3641 BB_HEAD (else_bb) ? (int)INSN_UID (BB_HEAD (else_bb)) : -1);
3643 fprintf (dump_file, ", join %d [%d]",
3644 join_bb->index,
3645 BB_HEAD (join_bb) ? (int)INSN_UID (BB_HEAD (join_bb)) : -1);
3647 if (ce_info->num_multiple_test_blocks > 0)
3648 fprintf (dump_file, ", %d %s block%s last test %d [%d]",
3649 ce_info->num_multiple_test_blocks,
3650 (ce_info->and_and_p) ? "&&" : "||",
3651 (ce_info->num_multiple_test_blocks == 1) ? "" : "s",
3652 ce_info->last_test_bb->index,
3653 ((BB_HEAD (ce_info->last_test_bb))
3654 ? (int)INSN_UID (BB_HEAD (ce_info->last_test_bb))
3655 : -1));
3657 fputc ('\n', dump_file);
3660 /* Make sure IF, THEN, and ELSE, blocks are adjacent. Actually, we get the
3661 first condition for free, since we've already asserted that there's a
3662 fallthru edge from IF to THEN. Likewise for the && and || blocks, since
3663 we checked the FALLTHRU flag, those are already adjacent to the last IF
3664 block. */
3665 /* ??? As an enhancement, move the ELSE block. Have to deal with
3666 BLOCK notes, if by no other means than backing out the merge if they
3667 exist. Sticky enough I don't want to think about it now. */
3668 next = then_bb;
3669 if (else_bb && (next = next->next_bb) != else_bb)
3670 return FALSE;
3671 if ((next = next->next_bb) != join_bb
3672 && join_bb != EXIT_BLOCK_PTR_FOR_FN (cfun))
3674 if (else_bb)
3675 join_bb = NULL;
3676 else
3677 return FALSE;
3680 /* Do the real work. */
3682 ce_info->else_bb = else_bb;
3683 ce_info->join_bb = join_bb;
3685 /* If we have && and || tests, try to first handle combining the && and ||
3686 tests into the conditional code, and if that fails, go back and handle
3687 it without the && and ||, which at present handles the && case if there
3688 was no ELSE block. */
3689 if (cond_exec_process_if_block (ce_info, TRUE))
3690 return TRUE;
3692 if (ce_info->num_multiple_test_blocks)
3694 cancel_changes (0);
3696 if (cond_exec_process_if_block (ce_info, FALSE))
3697 return TRUE;
3700 return FALSE;
3703 /* Convert a branch over a trap, or a branch
3704 to a trap, into a conditional trap. */
3706 static int
3707 find_cond_trap (basic_block test_bb, edge then_edge, edge else_edge)
3709 basic_block then_bb = then_edge->dest;
3710 basic_block else_bb = else_edge->dest;
3711 basic_block other_bb, trap_bb;
3712 rtx_insn *trap, *jump;
3713 rtx cond, seq;
3714 rtx_insn *cond_earliest;
3715 enum rtx_code code;
3717 /* Locate the block with the trap instruction. */
3718 /* ??? While we look for no successors, we really ought to allow
3719 EH successors. Need to fix merge_if_block for that to work. */
3720 if ((trap = block_has_only_trap (then_bb)) != NULL)
3721 trap_bb = then_bb, other_bb = else_bb;
3722 else if ((trap = block_has_only_trap (else_bb)) != NULL)
3723 trap_bb = else_bb, other_bb = then_bb;
3724 else
3725 return FALSE;
3727 if (dump_file)
3729 fprintf (dump_file, "\nTRAP-IF block found, start %d, trap %d\n",
3730 test_bb->index, trap_bb->index);
3733 /* If this is not a standard conditional jump, we can't parse it. */
3734 jump = BB_END (test_bb);
3735 cond = noce_get_condition (jump, &cond_earliest, false);
3736 if (! cond)
3737 return FALSE;
3739 /* If the conditional jump is more than just a conditional jump, then
3740 we can not do if-conversion on this block. */
3741 if (! onlyjump_p (jump))
3742 return FALSE;
3744 /* We must be comparing objects whose modes imply the size. */
3745 if (GET_MODE (XEXP (cond, 0)) == BLKmode)
3746 return FALSE;
3748 /* Reverse the comparison code, if necessary. */
3749 code = GET_CODE (cond);
3750 if (then_bb == trap_bb)
3752 code = reversed_comparison_code (cond, jump);
3753 if (code == UNKNOWN)
3754 return FALSE;
3757 /* Attempt to generate the conditional trap. */
3758 seq = gen_cond_trap (code, copy_rtx (XEXP (cond, 0)),
3759 copy_rtx (XEXP (cond, 1)),
3760 TRAP_CODE (PATTERN (trap)));
3761 if (seq == NULL)
3762 return FALSE;
3764 /* Emit the new insns before cond_earliest. */
3765 emit_insn_before_setloc (seq, cond_earliest, INSN_LOCATION (trap));
3767 /* Delete the trap block if possible. */
3768 remove_edge (trap_bb == then_bb ? then_edge : else_edge);
3769 df_set_bb_dirty (test_bb);
3770 df_set_bb_dirty (then_bb);
3771 df_set_bb_dirty (else_bb);
3773 if (EDGE_COUNT (trap_bb->preds) == 0)
3775 delete_basic_block (trap_bb);
3776 num_true_changes++;
3779 /* Wire together the blocks again. */
3780 if (current_ir_type () == IR_RTL_CFGLAYOUT)
3781 single_succ_edge (test_bb)->flags |= EDGE_FALLTHRU;
3782 else if (trap_bb == then_bb)
3784 rtx lab;
3785 rtx_insn *newjump;
3787 lab = JUMP_LABEL (jump);
3788 newjump = emit_jump_insn_after (gen_jump (lab), jump);
3789 LABEL_NUSES (lab) += 1;
3790 JUMP_LABEL (newjump) = lab;
3791 emit_barrier_after (newjump);
3793 delete_insn (jump);
3795 if (can_merge_blocks_p (test_bb, other_bb))
3797 merge_blocks (test_bb, other_bb);
3798 num_true_changes++;
3801 num_updated_if_blocks++;
3802 return TRUE;
3805 /* Subroutine of find_cond_trap: if BB contains only a trap insn,
3806 return it. */
3808 static rtx_insn *
3809 block_has_only_trap (basic_block bb)
3811 rtx_insn *trap;
3813 /* We're not the exit block. */
3814 if (bb == EXIT_BLOCK_PTR_FOR_FN (cfun))
3815 return NULL;
3817 /* The block must have no successors. */
3818 if (EDGE_COUNT (bb->succs) > 0)
3819 return NULL;
3821 /* The only instruction in the THEN block must be the trap. */
3822 trap = first_active_insn (bb);
3823 if (! (trap == BB_END (bb)
3824 && GET_CODE (PATTERN (trap)) == TRAP_IF
3825 && TRAP_CONDITION (PATTERN (trap)) == const_true_rtx))
3826 return NULL;
3828 return trap;
3831 /* Look for IF-THEN-ELSE cases in which one of THEN or ELSE is
3832 transformable, but not necessarily the other. There need be no
3833 JOIN block.
3835 Return TRUE if we were successful at converting the block.
3837 Cases we'd like to look at:
3840 if (test) goto over; // x not live
3841 x = a;
3842 goto label;
3843 over:
3845 becomes
3847 x = a;
3848 if (! test) goto label;
3851 if (test) goto E; // x not live
3852 x = big();
3853 goto L;
3855 x = b;
3856 goto M;
3858 becomes
3860 x = b;
3861 if (test) goto M;
3862 x = big();
3863 goto L;
3865 (3) // This one's really only interesting for targets that can do
3866 // multiway branching, e.g. IA-64 BBB bundles. For other targets
3867 // it results in multiple branches on a cache line, which often
3868 // does not sit well with predictors.
3870 if (test1) goto E; // predicted not taken
3871 x = a;
3872 if (test2) goto F;
3875 x = b;
3878 becomes
3880 x = a;
3881 if (test1) goto E;
3882 if (test2) goto F;
3884 Notes:
3886 (A) Don't do (2) if the branch is predicted against the block we're
3887 eliminating. Do it anyway if we can eliminate a branch; this requires
3888 that the sole successor of the eliminated block postdominate the other
3889 side of the if.
3891 (B) With CE, on (3) we can steal from both sides of the if, creating
3893 if (test1) x = a;
3894 if (!test1) x = b;
3895 if (test1) goto J;
3896 if (test2) goto F;
3900 Again, this is most useful if J postdominates.
3902 (C) CE substitutes for helpful life information.
3904 (D) These heuristics need a lot of work. */
3906 /* Tests for case 1 above. */
3908 static int
3909 find_if_case_1 (basic_block test_bb, edge then_edge, edge else_edge)
3911 basic_block then_bb = then_edge->dest;
3912 basic_block else_bb = else_edge->dest;
3913 basic_block new_bb;
3914 int then_bb_index, then_prob;
3915 rtx else_target = NULL_RTX;
3917 /* If we are partitioning hot/cold basic blocks, we don't want to
3918 mess up unconditional or indirect jumps that cross between hot
3919 and cold sections.
3921 Basic block partitioning may result in some jumps that appear to
3922 be optimizable (or blocks that appear to be mergeable), but which really
3923 must be left untouched (they are required to make it safely across
3924 partition boundaries). See the comments at the top of
3925 bb-reorder.c:partition_hot_cold_basic_blocks for complete details. */
3927 if ((BB_END (then_bb)
3928 && JUMP_P (BB_END (then_bb))
3929 && CROSSING_JUMP_P (BB_END (then_bb)))
3930 || (BB_END (test_bb)
3931 && JUMP_P (BB_END (test_bb))
3932 && CROSSING_JUMP_P (BB_END (test_bb)))
3933 || (BB_END (else_bb)
3934 && JUMP_P (BB_END (else_bb))
3935 && CROSSING_JUMP_P (BB_END (else_bb))))
3936 return FALSE;
3938 /* THEN has one successor. */
3939 if (!single_succ_p (then_bb))
3940 return FALSE;
3942 /* THEN does not fall through, but is not strange either. */
3943 if (single_succ_edge (then_bb)->flags & (EDGE_COMPLEX | EDGE_FALLTHRU))
3944 return FALSE;
3946 /* THEN has one predecessor. */
3947 if (!single_pred_p (then_bb))
3948 return FALSE;
3950 /* THEN must do something. */
3951 if (forwarder_block_p (then_bb))
3952 return FALSE;
3954 num_possible_if_blocks++;
3955 if (dump_file)
3956 fprintf (dump_file,
3957 "\nIF-CASE-1 found, start %d, then %d\n",
3958 test_bb->index, then_bb->index);
3960 if (then_edge->probability)
3961 then_prob = REG_BR_PROB_BASE - then_edge->probability;
3962 else
3963 then_prob = REG_BR_PROB_BASE / 2;
3965 /* We're speculating from the THEN path, we want to make sure the cost
3966 of speculation is within reason. */
3967 if (! cheap_bb_rtx_cost_p (then_bb, then_prob,
3968 COSTS_N_INSNS (BRANCH_COST (optimize_bb_for_speed_p (then_edge->src),
3969 predictable_edge_p (then_edge)))))
3970 return FALSE;
3972 if (else_bb == EXIT_BLOCK_PTR_FOR_FN (cfun))
3974 rtx_insn *jump = BB_END (else_edge->src);
3975 gcc_assert (JUMP_P (jump));
3976 else_target = JUMP_LABEL (jump);
3979 /* Registers set are dead, or are predicable. */
3980 if (! dead_or_predicable (test_bb, then_bb, else_bb,
3981 single_succ_edge (then_bb), 1))
3982 return FALSE;
3984 /* Conversion went ok, including moving the insns and fixing up the
3985 jump. Adjust the CFG to match. */
3987 /* We can avoid creating a new basic block if then_bb is immediately
3988 followed by else_bb, i.e. deleting then_bb allows test_bb to fall
3989 through to else_bb. */
3991 if (then_bb->next_bb == else_bb
3992 && then_bb->prev_bb == test_bb
3993 && else_bb != EXIT_BLOCK_PTR_FOR_FN (cfun))
3995 redirect_edge_succ (FALLTHRU_EDGE (test_bb), else_bb);
3996 new_bb = 0;
3998 else if (else_bb == EXIT_BLOCK_PTR_FOR_FN (cfun))
3999 new_bb = force_nonfallthru_and_redirect (FALLTHRU_EDGE (test_bb),
4000 else_bb, else_target);
4001 else
4002 new_bb = redirect_edge_and_branch_force (FALLTHRU_EDGE (test_bb),
4003 else_bb);
4005 df_set_bb_dirty (test_bb);
4006 df_set_bb_dirty (else_bb);
4008 then_bb_index = then_bb->index;
4009 delete_basic_block (then_bb);
4011 /* Make rest of code believe that the newly created block is the THEN_BB
4012 block we removed. */
4013 if (new_bb)
4015 df_bb_replace (then_bb_index, new_bb);
4016 /* This should have been done above via force_nonfallthru_and_redirect
4017 (possibly called from redirect_edge_and_branch_force). */
4018 gcc_checking_assert (BB_PARTITION (new_bb) == BB_PARTITION (test_bb));
4021 num_true_changes++;
4022 num_updated_if_blocks++;
4024 return TRUE;
4027 /* Test for case 2 above. */
4029 static int
4030 find_if_case_2 (basic_block test_bb, edge then_edge, edge else_edge)
4032 basic_block then_bb = then_edge->dest;
4033 basic_block else_bb = else_edge->dest;
4034 edge else_succ;
4035 int then_prob, else_prob;
4037 /* We do not want to speculate (empty) loop latches. */
4038 if (current_loops
4039 && else_bb->loop_father->latch == else_bb)
4040 return FALSE;
4042 /* If we are partitioning hot/cold basic blocks, we don't want to
4043 mess up unconditional or indirect jumps that cross between hot
4044 and cold sections.
4046 Basic block partitioning may result in some jumps that appear to
4047 be optimizable (or blocks that appear to be mergeable), but which really
4048 must be left untouched (they are required to make it safely across
4049 partition boundaries). See the comments at the top of
4050 bb-reorder.c:partition_hot_cold_basic_blocks for complete details. */
4052 if ((BB_END (then_bb)
4053 && JUMP_P (BB_END (then_bb))
4054 && CROSSING_JUMP_P (BB_END (then_bb)))
4055 || (BB_END (test_bb)
4056 && JUMP_P (BB_END (test_bb))
4057 && CROSSING_JUMP_P (BB_END (test_bb)))
4058 || (BB_END (else_bb)
4059 && JUMP_P (BB_END (else_bb))
4060 && CROSSING_JUMP_P (BB_END (else_bb))))
4061 return FALSE;
4063 /* ELSE has one successor. */
4064 if (!single_succ_p (else_bb))
4065 return FALSE;
4066 else
4067 else_succ = single_succ_edge (else_bb);
4069 /* ELSE outgoing edge is not complex. */
4070 if (else_succ->flags & EDGE_COMPLEX)
4071 return FALSE;
4073 /* ELSE has one predecessor. */
4074 if (!single_pred_p (else_bb))
4075 return FALSE;
4077 /* THEN is not EXIT. */
4078 if (then_bb->index < NUM_FIXED_BLOCKS)
4079 return FALSE;
4081 if (else_edge->probability)
4083 else_prob = else_edge->probability;
4084 then_prob = REG_BR_PROB_BASE - else_prob;
4086 else
4088 else_prob = REG_BR_PROB_BASE / 2;
4089 then_prob = REG_BR_PROB_BASE / 2;
4092 /* ELSE is predicted or SUCC(ELSE) postdominates THEN. */
4093 if (else_prob > then_prob)
4095 else if (else_succ->dest->index < NUM_FIXED_BLOCKS
4096 || dominated_by_p (CDI_POST_DOMINATORS, then_bb,
4097 else_succ->dest))
4099 else
4100 return FALSE;
4102 num_possible_if_blocks++;
4103 if (dump_file)
4104 fprintf (dump_file,
4105 "\nIF-CASE-2 found, start %d, else %d\n",
4106 test_bb->index, else_bb->index);
4108 /* We're speculating from the ELSE path, we want to make sure the cost
4109 of speculation is within reason. */
4110 if (! cheap_bb_rtx_cost_p (else_bb, else_prob,
4111 COSTS_N_INSNS (BRANCH_COST (optimize_bb_for_speed_p (else_edge->src),
4112 predictable_edge_p (else_edge)))))
4113 return FALSE;
4115 /* Registers set are dead, or are predicable. */
4116 if (! dead_or_predicable (test_bb, else_bb, then_bb, else_succ, 0))
4117 return FALSE;
4119 /* Conversion went ok, including moving the insns and fixing up the
4120 jump. Adjust the CFG to match. */
4122 df_set_bb_dirty (test_bb);
4123 df_set_bb_dirty (then_bb);
4124 delete_basic_block (else_bb);
4126 num_true_changes++;
4127 num_updated_if_blocks++;
4129 /* ??? We may now fallthru from one of THEN's successors into a join
4130 block. Rerun cleanup_cfg? Examine things manually? Wait? */
4132 return TRUE;
4135 /* Used by the code above to perform the actual rtl transformations.
4136 Return TRUE if successful.
4138 TEST_BB is the block containing the conditional branch. MERGE_BB
4139 is the block containing the code to manipulate. DEST_EDGE is an
4140 edge representing a jump to the join block; after the conversion,
4141 TEST_BB should be branching to its destination.
4142 REVERSEP is true if the sense of the branch should be reversed. */
4144 static int
4145 dead_or_predicable (basic_block test_bb, basic_block merge_bb,
4146 basic_block other_bb, edge dest_edge, int reversep)
4148 basic_block new_dest = dest_edge->dest;
4149 rtx_insn *head, *end, *jump;
4150 rtx_insn *earliest = NULL;
4151 rtx old_dest;
4152 bitmap merge_set = NULL;
4153 /* Number of pending changes. */
4154 int n_validated_changes = 0;
4155 rtx new_dest_label = NULL_RTX;
4157 jump = BB_END (test_bb);
4159 /* Find the extent of the real code in the merge block. */
4160 head = BB_HEAD (merge_bb);
4161 end = BB_END (merge_bb);
4163 while (DEBUG_INSN_P (end) && end != head)
4164 end = PREV_INSN (end);
4166 /* If merge_bb ends with a tablejump, predicating/moving insn's
4167 into test_bb and then deleting merge_bb will result in the jumptable
4168 that follows merge_bb being removed along with merge_bb and then we
4169 get an unresolved reference to the jumptable. */
4170 if (tablejump_p (end, NULL, NULL))
4171 return FALSE;
4173 if (LABEL_P (head))
4174 head = NEXT_INSN (head);
4175 while (DEBUG_INSN_P (head) && head != end)
4176 head = NEXT_INSN (head);
4177 if (NOTE_P (head))
4179 if (head == end)
4181 head = end = NULL;
4182 goto no_body;
4184 head = NEXT_INSN (head);
4185 while (DEBUG_INSN_P (head) && head != end)
4186 head = NEXT_INSN (head);
4189 if (JUMP_P (end))
4191 if (!onlyjump_p (end))
4192 return FALSE;
4193 if (head == end)
4195 head = end = NULL;
4196 goto no_body;
4198 end = PREV_INSN (end);
4199 while (DEBUG_INSN_P (end) && end != head)
4200 end = PREV_INSN (end);
4203 /* Don't move frame-related insn across the conditional branch. This
4204 can lead to one of the paths of the branch having wrong unwind info. */
4205 if (epilogue_completed)
4207 rtx_insn *insn = head;
4208 while (1)
4210 if (INSN_P (insn) && RTX_FRAME_RELATED_P (insn))
4211 return FALSE;
4212 if (insn == end)
4213 break;
4214 insn = NEXT_INSN (insn);
4218 /* Disable handling dead code by conditional execution if the machine needs
4219 to do anything funny with the tests, etc. */
4220 #ifndef IFCVT_MODIFY_TESTS
4221 if (targetm.have_conditional_execution ())
4223 /* In the conditional execution case, we have things easy. We know
4224 the condition is reversible. We don't have to check life info
4225 because we're going to conditionally execute the code anyway.
4226 All that's left is making sure the insns involved can actually
4227 be predicated. */
4229 rtx cond;
4231 cond = cond_exec_get_condition (jump);
4232 if (! cond)
4233 return FALSE;
4235 rtx note = find_reg_note (jump, REG_BR_PROB, NULL_RTX);
4236 int prob_val = (note ? XINT (note, 0) : -1);
4238 if (reversep)
4240 enum rtx_code rev = reversed_comparison_code (cond, jump);
4241 if (rev == UNKNOWN)
4242 return FALSE;
4243 cond = gen_rtx_fmt_ee (rev, GET_MODE (cond), XEXP (cond, 0),
4244 XEXP (cond, 1));
4245 if (prob_val >= 0)
4246 prob_val = REG_BR_PROB_BASE - prob_val;
4249 if (cond_exec_process_insns (NULL, head, end, cond, prob_val, 0)
4250 && verify_changes (0))
4251 n_validated_changes = num_validated_changes ();
4252 else
4253 cancel_changes (0);
4255 earliest = jump;
4257 #endif
4259 /* If we allocated new pseudos (e.g. in the conditional move
4260 expander called from noce_emit_cmove), we must resize the
4261 array first. */
4262 if (max_regno < max_reg_num ())
4263 max_regno = max_reg_num ();
4265 /* Try the NCE path if the CE path did not result in any changes. */
4266 if (n_validated_changes == 0)
4268 rtx cond;
4269 rtx_insn *insn;
4270 regset live;
4271 bool success;
4273 /* In the non-conditional execution case, we have to verify that there
4274 are no trapping operations, no calls, no references to memory, and
4275 that any registers modified are dead at the branch site. */
4277 if (!any_condjump_p (jump))
4278 return FALSE;
4280 /* Find the extent of the conditional. */
4281 cond = noce_get_condition (jump, &earliest, false);
4282 if (!cond)
4283 return FALSE;
4285 live = BITMAP_ALLOC (&reg_obstack);
4286 simulate_backwards_to_point (merge_bb, live, end);
4287 success = can_move_insns_across (head, end, earliest, jump,
4288 merge_bb, live,
4289 df_get_live_in (other_bb), NULL);
4290 BITMAP_FREE (live);
4291 if (!success)
4292 return FALSE;
4294 /* Collect the set of registers set in MERGE_BB. */
4295 merge_set = BITMAP_ALLOC (&reg_obstack);
4297 FOR_BB_INSNS (merge_bb, insn)
4298 if (NONDEBUG_INSN_P (insn))
4299 df_simulate_find_defs (insn, merge_set);
4301 /* If shrink-wrapping, disable this optimization when test_bb is
4302 the first basic block and merge_bb exits. The idea is to not
4303 move code setting up a return register as that may clobber a
4304 register used to pass function parameters, which then must be
4305 saved in caller-saved regs. A caller-saved reg requires the
4306 prologue, killing a shrink-wrap opportunity. */
4307 if ((SHRINK_WRAPPING_ENABLED && !epilogue_completed)
4308 && ENTRY_BLOCK_PTR_FOR_FN (cfun)->next_bb == test_bb
4309 && single_succ_p (new_dest)
4310 && single_succ (new_dest) == EXIT_BLOCK_PTR_FOR_FN (cfun)
4311 && bitmap_intersect_p (df_get_live_in (new_dest), merge_set))
4313 regset return_regs;
4314 unsigned int i;
4316 return_regs = BITMAP_ALLOC (&reg_obstack);
4318 /* Start off with the intersection of regs used to pass
4319 params and regs used to return values. */
4320 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
4321 if (FUNCTION_ARG_REGNO_P (i)
4322 && targetm.calls.function_value_regno_p (i))
4323 bitmap_set_bit (return_regs, INCOMING_REGNO (i));
4325 bitmap_and_into (return_regs,
4326 df_get_live_out (ENTRY_BLOCK_PTR_FOR_FN (cfun)));
4327 bitmap_and_into (return_regs,
4328 df_get_live_in (EXIT_BLOCK_PTR_FOR_FN (cfun)));
4329 if (!bitmap_empty_p (return_regs))
4331 FOR_BB_INSNS_REVERSE (new_dest, insn)
4332 if (NONDEBUG_INSN_P (insn))
4334 df_ref def;
4336 /* If this insn sets any reg in return_regs, add all
4337 reg uses to the set of regs we're interested in. */
4338 FOR_EACH_INSN_DEF (def, insn)
4339 if (bitmap_bit_p (return_regs, DF_REF_REGNO (def)))
4341 df_simulate_uses (insn, return_regs);
4342 break;
4345 if (bitmap_intersect_p (merge_set, return_regs))
4347 BITMAP_FREE (return_regs);
4348 BITMAP_FREE (merge_set);
4349 return FALSE;
4352 BITMAP_FREE (return_regs);
4356 no_body:
4357 /* We don't want to use normal invert_jump or redirect_jump because
4358 we don't want to delete_insn called. Also, we want to do our own
4359 change group management. */
4361 old_dest = JUMP_LABEL (jump);
4362 if (other_bb != new_dest)
4364 if (!any_condjump_p (jump))
4365 goto cancel;
4367 if (JUMP_P (BB_END (dest_edge->src)))
4368 new_dest_label = JUMP_LABEL (BB_END (dest_edge->src));
4369 else if (new_dest == EXIT_BLOCK_PTR_FOR_FN (cfun))
4370 new_dest_label = ret_rtx;
4371 else
4372 new_dest_label = block_label (new_dest);
4374 if (reversep
4375 ? ! invert_jump_1 (jump, new_dest_label)
4376 : ! redirect_jump_1 (jump, new_dest_label))
4377 goto cancel;
4380 if (verify_changes (n_validated_changes))
4381 confirm_change_group ();
4382 else
4383 goto cancel;
4385 if (other_bb != new_dest)
4387 redirect_jump_2 (jump, old_dest, new_dest_label, 0, reversep);
4389 redirect_edge_succ (BRANCH_EDGE (test_bb), new_dest);
4390 if (reversep)
4392 gcov_type count, probability;
4393 count = BRANCH_EDGE (test_bb)->count;
4394 BRANCH_EDGE (test_bb)->count = FALLTHRU_EDGE (test_bb)->count;
4395 FALLTHRU_EDGE (test_bb)->count = count;
4396 probability = BRANCH_EDGE (test_bb)->probability;
4397 BRANCH_EDGE (test_bb)->probability
4398 = FALLTHRU_EDGE (test_bb)->probability;
4399 FALLTHRU_EDGE (test_bb)->probability = probability;
4400 update_br_prob_note (test_bb);
4404 /* Move the insns out of MERGE_BB to before the branch. */
4405 if (head != NULL)
4407 rtx_insn *insn;
4409 if (end == BB_END (merge_bb))
4410 BB_END (merge_bb) = PREV_INSN (head);
4412 /* PR 21767: when moving insns above a conditional branch, the REG_EQUAL
4413 notes being moved might become invalid. */
4414 insn = head;
4417 rtx note;
4419 if (! INSN_P (insn))
4420 continue;
4421 note = find_reg_note (insn, REG_EQUAL, NULL_RTX);
4422 if (! note)
4423 continue;
4424 remove_note (insn, note);
4425 } while (insn != end && (insn = NEXT_INSN (insn)));
4427 /* PR46315: when moving insns above a conditional branch, the REG_EQUAL
4428 notes referring to the registers being set might become invalid. */
4429 if (merge_set)
4431 unsigned i;
4432 bitmap_iterator bi;
4434 EXECUTE_IF_SET_IN_BITMAP (merge_set, 0, i, bi)
4435 remove_reg_equal_equiv_notes_for_regno (i);
4437 BITMAP_FREE (merge_set);
4440 reorder_insns (head, end, PREV_INSN (earliest));
4443 /* Remove the jump and edge if we can. */
4444 if (other_bb == new_dest)
4446 delete_insn (jump);
4447 remove_edge (BRANCH_EDGE (test_bb));
4448 /* ??? Can't merge blocks here, as then_bb is still in use.
4449 At minimum, the merge will get done just before bb-reorder. */
4452 return TRUE;
4454 cancel:
4455 cancel_changes (0);
4457 if (merge_set)
4458 BITMAP_FREE (merge_set);
4460 return FALSE;
4463 /* Main entry point for all if-conversion. AFTER_COMBINE is true if
4464 we are after combine pass. */
4466 static void
4467 if_convert (bool after_combine)
4469 basic_block bb;
4470 int pass;
4472 if (optimize == 1)
4474 df_live_add_problem ();
4475 df_live_set_all_dirty ();
4478 /* Record whether we are after combine pass. */
4479 ifcvt_after_combine = after_combine;
4480 num_possible_if_blocks = 0;
4481 num_updated_if_blocks = 0;
4482 num_true_changes = 0;
4484 loop_optimizer_init (AVOID_CFG_MODIFICATIONS);
4485 mark_loop_exit_edges ();
4486 loop_optimizer_finalize ();
4487 free_dominance_info (CDI_DOMINATORS);
4489 /* Compute postdominators. */
4490 calculate_dominance_info (CDI_POST_DOMINATORS);
4492 df_set_flags (DF_LR_RUN_DCE);
4494 /* Go through each of the basic blocks looking for things to convert. If we
4495 have conditional execution, we make multiple passes to allow us to handle
4496 IF-THEN{-ELSE} blocks within other IF-THEN{-ELSE} blocks. */
4497 pass = 0;
4500 df_analyze ();
4501 /* Only need to do dce on the first pass. */
4502 df_clear_flags (DF_LR_RUN_DCE);
4503 cond_exec_changed_p = FALSE;
4504 pass++;
4506 #ifdef IFCVT_MULTIPLE_DUMPS
4507 if (dump_file && pass > 1)
4508 fprintf (dump_file, "\n\n========== Pass %d ==========\n", pass);
4509 #endif
4511 FOR_EACH_BB_FN (bb, cfun)
4513 basic_block new_bb;
4514 while (!df_get_bb_dirty (bb)
4515 && (new_bb = find_if_header (bb, pass)) != NULL)
4516 bb = new_bb;
4519 #ifdef IFCVT_MULTIPLE_DUMPS
4520 if (dump_file && cond_exec_changed_p)
4521 print_rtl_with_bb (dump_file, get_insns (), dump_flags);
4522 #endif
4524 while (cond_exec_changed_p);
4526 #ifdef IFCVT_MULTIPLE_DUMPS
4527 if (dump_file)
4528 fprintf (dump_file, "\n\n========== no more changes\n");
4529 #endif
4531 free_dominance_info (CDI_POST_DOMINATORS);
4533 if (dump_file)
4534 fflush (dump_file);
4536 clear_aux_for_blocks ();
4538 /* If we allocated new pseudos, we must resize the array for sched1. */
4539 if (max_regno < max_reg_num ())
4540 max_regno = max_reg_num ();
4542 /* Write the final stats. */
4543 if (dump_file && num_possible_if_blocks > 0)
4545 fprintf (dump_file,
4546 "\n%d possible IF blocks searched.\n",
4547 num_possible_if_blocks);
4548 fprintf (dump_file,
4549 "%d IF blocks converted.\n",
4550 num_updated_if_blocks);
4551 fprintf (dump_file,
4552 "%d true changes made.\n\n\n",
4553 num_true_changes);
4556 if (optimize == 1)
4557 df_remove_problem (df_live);
4559 #ifdef ENABLE_CHECKING
4560 verify_flow_info ();
4561 #endif
4564 /* If-conversion and CFG cleanup. */
4565 static unsigned int
4566 rest_of_handle_if_conversion (void)
4568 if (flag_if_conversion)
4570 if (dump_file)
4572 dump_reg_info (dump_file);
4573 dump_flow_info (dump_file, dump_flags);
4575 cleanup_cfg (CLEANUP_EXPENSIVE);
4576 if_convert (false);
4579 cleanup_cfg (0);
4580 return 0;
4583 namespace {
4585 const pass_data pass_data_rtl_ifcvt =
4587 RTL_PASS, /* type */
4588 "ce1", /* name */
4589 OPTGROUP_NONE, /* optinfo_flags */
4590 TV_IFCVT, /* tv_id */
4591 0, /* properties_required */
4592 0, /* properties_provided */
4593 0, /* properties_destroyed */
4594 0, /* todo_flags_start */
4595 TODO_df_finish, /* todo_flags_finish */
4598 class pass_rtl_ifcvt : public rtl_opt_pass
4600 public:
4601 pass_rtl_ifcvt (gcc::context *ctxt)
4602 : rtl_opt_pass (pass_data_rtl_ifcvt, ctxt)
4605 /* opt_pass methods: */
4606 virtual bool gate (function *)
4608 return (optimize > 0) && dbg_cnt (if_conversion);
4611 virtual unsigned int execute (function *)
4613 return rest_of_handle_if_conversion ();
4616 }; // class pass_rtl_ifcvt
4618 } // anon namespace
4620 rtl_opt_pass *
4621 make_pass_rtl_ifcvt (gcc::context *ctxt)
4623 return new pass_rtl_ifcvt (ctxt);
4627 /* Rerun if-conversion, as combine may have simplified things enough
4628 to now meet sequence length restrictions. */
4630 namespace {
4632 const pass_data pass_data_if_after_combine =
4634 RTL_PASS, /* type */
4635 "ce2", /* name */
4636 OPTGROUP_NONE, /* optinfo_flags */
4637 TV_IFCVT, /* tv_id */
4638 0, /* properties_required */
4639 0, /* properties_provided */
4640 0, /* properties_destroyed */
4641 0, /* todo_flags_start */
4642 TODO_df_finish, /* todo_flags_finish */
4645 class pass_if_after_combine : public rtl_opt_pass
4647 public:
4648 pass_if_after_combine (gcc::context *ctxt)
4649 : rtl_opt_pass (pass_data_if_after_combine, ctxt)
4652 /* opt_pass methods: */
4653 virtual bool gate (function *)
4655 return optimize > 0 && flag_if_conversion
4656 && dbg_cnt (if_after_combine);
4659 virtual unsigned int execute (function *)
4661 if_convert (true);
4662 return 0;
4665 }; // class pass_if_after_combine
4667 } // anon namespace
4669 rtl_opt_pass *
4670 make_pass_if_after_combine (gcc::context *ctxt)
4672 return new pass_if_after_combine (ctxt);
4676 namespace {
4678 const pass_data pass_data_if_after_reload =
4680 RTL_PASS, /* type */
4681 "ce3", /* name */
4682 OPTGROUP_NONE, /* optinfo_flags */
4683 TV_IFCVT2, /* tv_id */
4684 0, /* properties_required */
4685 0, /* properties_provided */
4686 0, /* properties_destroyed */
4687 0, /* todo_flags_start */
4688 TODO_df_finish, /* todo_flags_finish */
4691 class pass_if_after_reload : public rtl_opt_pass
4693 public:
4694 pass_if_after_reload (gcc::context *ctxt)
4695 : rtl_opt_pass (pass_data_if_after_reload, ctxt)
4698 /* opt_pass methods: */
4699 virtual bool gate (function *)
4701 return optimize > 0 && flag_if_conversion2
4702 && dbg_cnt (if_after_reload);
4705 virtual unsigned int execute (function *)
4707 if_convert (true);
4708 return 0;
4711 }; // class pass_if_after_reload
4713 } // anon namespace
4715 rtl_opt_pass *
4716 make_pass_if_after_reload (gcc::context *ctxt)
4718 return new pass_if_after_reload (ctxt);