Mark ChangeLog
[official-gcc.git] / gcc / ifcvt.c
blob70a84d25775583719293c260758b91f6aea11341
1 /* If-conversion support.
2 Copyright (C) 2000-2013 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 "function.h"
28 #include "flags.h"
29 #include "insn-config.h"
30 #include "recog.h"
31 #include "except.h"
32 #include "hard-reg-set.h"
33 #include "basic-block.h"
34 #include "expr.h"
35 #include "output.h"
36 #include "optabs.h"
37 #include "diagnostic-core.h"
38 #include "tm_p.h"
39 #include "cfgloop.h"
40 #include "target.h"
41 #include "tree-pass.h"
42 #include "df.h"
43 #include "vec.h"
44 #include "pointer-set.h"
45 #include "dbgcnt.h"
47 #ifndef HAVE_conditional_move
48 #define HAVE_conditional_move 0
49 #endif
50 #ifndef HAVE_incscc
51 #define HAVE_incscc 0
52 #endif
53 #ifndef HAVE_decscc
54 #define HAVE_decscc 0
55 #endif
56 #ifndef HAVE_trap
57 #define HAVE_trap 0
58 #endif
60 #ifndef MAX_CONDITIONAL_EXECUTE
61 #define MAX_CONDITIONAL_EXECUTE \
62 (BRANCH_COST (optimize_function_for_speed_p (cfun), false) \
63 + 1)
64 #endif
66 #define IFCVT_MULTIPLE_DUMPS 1
68 #define NULL_BLOCK ((basic_block) NULL)
70 /* # of IF-THEN or IF-THEN-ELSE blocks we looked at */
71 static int num_possible_if_blocks;
73 /* # of IF-THEN or IF-THEN-ELSE blocks were converted to conditional
74 execution. */
75 static int num_updated_if_blocks;
77 /* # of changes made. */
78 static int num_true_changes;
80 /* Whether conditional execution changes were made. */
81 static int cond_exec_changed_p;
83 /* Forward references. */
84 static int count_bb_insns (const_basic_block);
85 static bool cheap_bb_rtx_cost_p (const_basic_block, int, int);
86 static rtx first_active_insn (basic_block);
87 static rtx last_active_insn (basic_block, int);
88 static rtx find_active_insn_before (basic_block, rtx);
89 static rtx find_active_insn_after (basic_block, rtx);
90 static basic_block block_fallthru (basic_block);
91 static int cond_exec_process_insns (ce_if_block_t *, rtx, rtx, rtx, rtx, int);
92 static rtx cond_exec_get_condition (rtx);
93 static rtx noce_get_condition (rtx, rtx *, bool);
94 static int noce_operand_ok (const_rtx);
95 static void merge_if_block (ce_if_block_t *);
96 static int find_cond_trap (basic_block, edge, edge);
97 static basic_block find_if_header (basic_block, int);
98 static int block_jumps_and_fallthru_p (basic_block, basic_block);
99 static int noce_find_if_block (basic_block, edge, edge, int);
100 static int cond_exec_find_if_block (ce_if_block_t *);
101 static int find_if_case_1 (basic_block, edge, edge);
102 static int find_if_case_2 (basic_block, edge, edge);
103 static int dead_or_predicable (basic_block, basic_block, basic_block,
104 edge, int);
105 static void noce_emit_move_insn (rtx, rtx);
106 static rtx block_has_only_trap (basic_block);
108 /* Count the number of non-jump active insns in BB. */
110 static int
111 count_bb_insns (const_basic_block bb)
113 int count = 0;
114 rtx insn = BB_HEAD (bb);
116 while (1)
118 if ((CALL_P (insn) || NONJUMP_INSN_P (insn))
119 /* Don't count USE/CLOBBER insns, flow_find_cross_jump etc.
120 don't count them either and we need consistency. */
121 && GET_CODE (PATTERN (insn)) != USE
122 && GET_CODE (PATTERN (insn)) != CLOBBER)
123 count++;
125 if (insn == BB_END (bb))
126 break;
127 insn = NEXT_INSN (insn);
130 return count;
133 /* Determine whether the total insn_rtx_cost on non-jump insns in
134 basic block BB is less than MAX_COST. This function returns
135 false if the cost of any instruction could not be estimated.
137 The cost of the non-jump insns in BB is scaled by REG_BR_PROB_BASE
138 as those insns are being speculated. MAX_COST is scaled with SCALE
139 plus a small fudge factor. */
141 static bool
142 cheap_bb_rtx_cost_p (const_basic_block bb, int scale, int max_cost)
144 int count = 0;
145 rtx insn = BB_HEAD (bb);
146 bool speed = optimize_bb_for_speed_p (bb);
148 /* Our branch probability/scaling factors are just estimates and don't
149 account for cases where we can get speculation for free and other
150 secondary benefits. So we fudge the scale factor to make speculating
151 appear a little more profitable. */
152 scale += REG_BR_PROB_BASE / 8;
153 max_cost *= scale;
155 while (1)
157 if (NONJUMP_INSN_P (insn))
159 int cost = insn_rtx_cost (PATTERN (insn), speed) * REG_BR_PROB_BASE;
160 if (cost == 0)
161 return false;
163 /* If this instruction is the load or set of a "stack" register,
164 such as a floating point register on x87, then the cost of
165 speculatively executing this insn may need to include
166 the additional cost of popping its result off of the
167 register stack. Unfortunately, correctly recognizing and
168 accounting for this additional overhead is tricky, so for
169 now we simply prohibit such speculative execution. */
170 #ifdef STACK_REGS
172 rtx set = single_set (insn);
173 if (set && STACK_REG_P (SET_DEST (set)))
174 return false;
176 #endif
178 count += cost;
179 if (count >= max_cost)
180 return false;
182 else if (CALL_P (insn))
183 return false;
185 if (insn == BB_END (bb))
186 break;
187 insn = NEXT_INSN (insn);
190 return true;
193 /* Return the first non-jump active insn in the basic block. */
195 static rtx
196 first_active_insn (basic_block bb)
198 rtx insn = BB_HEAD (bb);
200 if (LABEL_P (insn))
202 if (insn == BB_END (bb))
203 return NULL_RTX;
204 insn = NEXT_INSN (insn);
207 while (NOTE_P (insn) || DEBUG_INSN_P (insn))
209 if (insn == BB_END (bb))
210 return NULL_RTX;
211 insn = NEXT_INSN (insn);
214 if (JUMP_P (insn))
215 return NULL_RTX;
217 return insn;
220 /* Return the last non-jump active (non-jump) insn in the basic block. */
222 static rtx
223 last_active_insn (basic_block bb, int skip_use_p)
225 rtx insn = BB_END (bb);
226 rtx head = BB_HEAD (bb);
228 while (NOTE_P (insn)
229 || JUMP_P (insn)
230 || DEBUG_INSN_P (insn)
231 || (skip_use_p
232 && NONJUMP_INSN_P (insn)
233 && GET_CODE (PATTERN (insn)) == USE))
235 if (insn == head)
236 return NULL_RTX;
237 insn = PREV_INSN (insn);
240 if (LABEL_P (insn))
241 return NULL_RTX;
243 return insn;
246 /* Return the active insn before INSN inside basic block CURR_BB. */
248 static rtx
249 find_active_insn_before (basic_block curr_bb, rtx insn)
251 if (!insn || insn == BB_HEAD (curr_bb))
252 return NULL_RTX;
254 while ((insn = PREV_INSN (insn)) != NULL_RTX)
256 if (NONJUMP_INSN_P (insn) || JUMP_P (insn) || CALL_P (insn))
257 break;
259 /* No other active insn all the way to the start of the basic block. */
260 if (insn == BB_HEAD (curr_bb))
261 return NULL_RTX;
264 return insn;
267 /* Return the active insn after INSN inside basic block CURR_BB. */
269 static rtx
270 find_active_insn_after (basic_block curr_bb, rtx insn)
272 if (!insn || insn == BB_END (curr_bb))
273 return NULL_RTX;
275 while ((insn = NEXT_INSN (insn)) != NULL_RTX)
277 if (NONJUMP_INSN_P (insn) || JUMP_P (insn) || CALL_P (insn))
278 break;
280 /* No other active insn all the way to the end of the basic block. */
281 if (insn == BB_END (curr_bb))
282 return NULL_RTX;
285 return insn;
288 /* Return the basic block reached by falling though the basic block BB. */
290 static basic_block
291 block_fallthru (basic_block bb)
293 edge e = find_fallthru_edge (bb->succs);
295 return (e) ? e->dest : NULL_BLOCK;
298 /* Return true if RTXs A and B can be safely interchanged. */
300 static bool
301 rtx_interchangeable_p (const_rtx a, const_rtx b)
303 if (!rtx_equal_p (a, b))
304 return false;
306 if (GET_CODE (a) != MEM)
307 return true;
309 /* A dead type-unsafe memory reference is legal, but a live type-unsafe memory
310 reference is not. Interchanging a dead type-unsafe memory reference with
311 a live type-safe one creates a live type-unsafe memory reference, in other
312 words, it makes the program illegal.
313 We check here conservatively whether the two memory references have equal
314 memory attributes. */
316 return mem_attrs_eq_p (get_mem_attrs (a), get_mem_attrs (b));
320 /* Go through a bunch of insns, converting them to conditional
321 execution format if possible. Return TRUE if all of the non-note
322 insns were processed. */
324 static int
325 cond_exec_process_insns (ce_if_block_t *ce_info ATTRIBUTE_UNUSED,
326 /* if block information */rtx start,
327 /* first insn to look at */rtx end,
328 /* last insn to look at */rtx test,
329 /* conditional execution test */rtx prob_val,
330 /* probability of branch taken. */int mod_ok)
332 int must_be_last = FALSE;
333 rtx insn;
334 rtx xtest;
335 rtx pattern;
337 if (!start || !end)
338 return FALSE;
340 for (insn = start; ; insn = NEXT_INSN (insn))
342 /* dwarf2out can't cope with conditional prologues. */
343 if (NOTE_P (insn) && NOTE_KIND (insn) == NOTE_INSN_PROLOGUE_END)
344 return FALSE;
346 if (NOTE_P (insn) || DEBUG_INSN_P (insn))
347 goto insn_done;
349 gcc_assert(NONJUMP_INSN_P (insn) || CALL_P (insn));
351 /* Remove USE insns that get in the way. */
352 if (reload_completed && GET_CODE (PATTERN (insn)) == USE)
354 /* ??? Ug. Actually unlinking the thing is problematic,
355 given what we'd have to coordinate with our callers. */
356 SET_INSN_DELETED (insn);
357 goto insn_done;
360 /* Last insn wasn't last? */
361 if (must_be_last)
362 return FALSE;
364 if (modified_in_p (test, insn))
366 if (!mod_ok)
367 return FALSE;
368 must_be_last = TRUE;
371 /* Now build the conditional form of the instruction. */
372 pattern = PATTERN (insn);
373 xtest = copy_rtx (test);
375 /* If this is already a COND_EXEC, rewrite the test to be an AND of the
376 two conditions. */
377 if (GET_CODE (pattern) == COND_EXEC)
379 if (GET_MODE (xtest) != GET_MODE (COND_EXEC_TEST (pattern)))
380 return FALSE;
382 xtest = gen_rtx_AND (GET_MODE (xtest), xtest,
383 COND_EXEC_TEST (pattern));
384 pattern = COND_EXEC_CODE (pattern);
387 pattern = gen_rtx_COND_EXEC (VOIDmode, xtest, pattern);
389 /* If the machine needs to modify the insn being conditionally executed,
390 say for example to force a constant integer operand into a temp
391 register, do so here. */
392 #ifdef IFCVT_MODIFY_INSN
393 IFCVT_MODIFY_INSN (ce_info, pattern, insn);
394 if (! pattern)
395 return FALSE;
396 #endif
398 validate_change (insn, &PATTERN (insn), pattern, 1);
400 if (CALL_P (insn) && prob_val)
401 validate_change (insn, &REG_NOTES (insn),
402 alloc_EXPR_LIST (REG_BR_PROB, prob_val,
403 REG_NOTES (insn)), 1);
405 insn_done:
406 if (insn == end)
407 break;
410 return TRUE;
413 /* Return the condition for a jump. Do not do any special processing. */
415 static rtx
416 cond_exec_get_condition (rtx jump)
418 rtx test_if, cond;
420 if (any_condjump_p (jump))
421 test_if = SET_SRC (pc_set (jump));
422 else
423 return NULL_RTX;
424 cond = XEXP (test_if, 0);
426 /* If this branches to JUMP_LABEL when the condition is false,
427 reverse the condition. */
428 if (GET_CODE (XEXP (test_if, 2)) == LABEL_REF
429 && XEXP (XEXP (test_if, 2), 0) == JUMP_LABEL (jump))
431 enum rtx_code rev = reversed_comparison_code (cond, jump);
432 if (rev == UNKNOWN)
433 return NULL_RTX;
435 cond = gen_rtx_fmt_ee (rev, GET_MODE (cond), XEXP (cond, 0),
436 XEXP (cond, 1));
439 return cond;
442 /* Given a simple IF-THEN or IF-THEN-ELSE block, attempt to convert it
443 to conditional execution. Return TRUE if we were successful at
444 converting the block. */
446 static int
447 cond_exec_process_if_block (ce_if_block_t * ce_info,
448 /* if block information */int do_multiple_p)
450 basic_block test_bb = ce_info->test_bb; /* last test block */
451 basic_block then_bb = ce_info->then_bb; /* THEN */
452 basic_block else_bb = ce_info->else_bb; /* ELSE or NULL */
453 rtx test_expr; /* expression in IF_THEN_ELSE that is tested */
454 rtx then_start; /* first insn in THEN block */
455 rtx then_end; /* last insn + 1 in THEN block */
456 rtx else_start = NULL_RTX; /* first insn in ELSE block or NULL */
457 rtx else_end = NULL_RTX; /* last insn + 1 in ELSE block */
458 int max; /* max # of insns to convert. */
459 int then_mod_ok; /* whether conditional mods are ok in THEN */
460 rtx true_expr; /* test for else block insns */
461 rtx false_expr; /* test for then block insns */
462 rtx true_prob_val; /* probability of else block */
463 rtx false_prob_val; /* probability of then block */
464 rtx then_last_head = NULL_RTX; /* Last match at the head of THEN */
465 rtx else_last_head = NULL_RTX; /* Last match at the head of ELSE */
466 rtx then_first_tail = NULL_RTX; /* First match at the tail of THEN */
467 rtx else_first_tail = NULL_RTX; /* First match at the tail of ELSE */
468 int then_n_insns, else_n_insns, n_insns;
469 enum rtx_code false_code;
471 /* If test is comprised of && or || elements, and we've failed at handling
472 all of them together, just use the last test if it is the special case of
473 && elements without an ELSE block. */
474 if (!do_multiple_p && ce_info->num_multiple_test_blocks)
476 if (else_bb || ! ce_info->and_and_p)
477 return FALSE;
479 ce_info->test_bb = test_bb = ce_info->last_test_bb;
480 ce_info->num_multiple_test_blocks = 0;
481 ce_info->num_and_and_blocks = 0;
482 ce_info->num_or_or_blocks = 0;
485 /* Find the conditional jump to the ELSE or JOIN part, and isolate
486 the test. */
487 test_expr = cond_exec_get_condition (BB_END (test_bb));
488 if (! test_expr)
489 return FALSE;
491 /* If the conditional jump is more than just a conditional jump,
492 then we can not do conditional execution conversion on this block. */
493 if (! onlyjump_p (BB_END (test_bb)))
494 return FALSE;
496 /* Collect the bounds of where we're to search, skipping any labels, jumps
497 and notes at the beginning and end of the block. Then count the total
498 number of insns and see if it is small enough to convert. */
499 then_start = first_active_insn (then_bb);
500 then_end = last_active_insn (then_bb, TRUE);
501 then_n_insns = ce_info->num_then_insns = count_bb_insns (then_bb);
502 n_insns = then_n_insns;
503 max = MAX_CONDITIONAL_EXECUTE;
505 if (else_bb)
507 int n_matching;
509 max *= 2;
510 else_start = first_active_insn (else_bb);
511 else_end = last_active_insn (else_bb, TRUE);
512 else_n_insns = ce_info->num_else_insns = count_bb_insns (else_bb);
513 n_insns += else_n_insns;
515 /* Look for matching sequences at the head and tail of the two blocks,
516 and limit the range of insns to be converted if possible. */
517 n_matching = flow_find_cross_jump (then_bb, else_bb,
518 &then_first_tail, &else_first_tail,
519 NULL);
520 if (then_first_tail == BB_HEAD (then_bb))
521 then_start = then_end = NULL_RTX;
522 if (else_first_tail == BB_HEAD (else_bb))
523 else_start = else_end = NULL_RTX;
525 if (n_matching > 0)
527 if (then_end)
528 then_end = find_active_insn_before (then_bb, then_first_tail);
529 if (else_end)
530 else_end = find_active_insn_before (else_bb, else_first_tail);
531 n_insns -= 2 * n_matching;
534 if (then_start
535 && else_start
536 && then_n_insns > n_matching
537 && else_n_insns > n_matching)
539 int longest_match = MIN (then_n_insns - n_matching,
540 else_n_insns - n_matching);
541 n_matching
542 = flow_find_head_matching_sequence (then_bb, else_bb,
543 &then_last_head,
544 &else_last_head,
545 longest_match);
547 if (n_matching > 0)
549 rtx insn;
551 /* We won't pass the insns in the head sequence to
552 cond_exec_process_insns, so we need to test them here
553 to make sure that they don't clobber the condition. */
554 for (insn = BB_HEAD (then_bb);
555 insn != NEXT_INSN (then_last_head);
556 insn = NEXT_INSN (insn))
557 if (!LABEL_P (insn) && !NOTE_P (insn)
558 && !DEBUG_INSN_P (insn)
559 && modified_in_p (test_expr, insn))
560 return FALSE;
563 if (then_last_head == then_end)
564 then_start = then_end = NULL_RTX;
565 if (else_last_head == else_end)
566 else_start = else_end = NULL_RTX;
568 if (n_matching > 0)
570 if (then_start)
571 then_start = find_active_insn_after (then_bb, then_last_head);
572 if (else_start)
573 else_start = find_active_insn_after (else_bb, else_last_head);
574 n_insns -= 2 * n_matching;
579 if (n_insns > max)
580 return FALSE;
582 /* Map test_expr/test_jump into the appropriate MD tests to use on
583 the conditionally executed code. */
585 true_expr = test_expr;
587 false_code = reversed_comparison_code (true_expr, BB_END (test_bb));
588 if (false_code != UNKNOWN)
589 false_expr = gen_rtx_fmt_ee (false_code, GET_MODE (true_expr),
590 XEXP (true_expr, 0), XEXP (true_expr, 1));
591 else
592 false_expr = NULL_RTX;
594 #ifdef IFCVT_MODIFY_TESTS
595 /* If the machine description needs to modify the tests, such as setting a
596 conditional execution register from a comparison, it can do so here. */
597 IFCVT_MODIFY_TESTS (ce_info, true_expr, false_expr);
599 /* See if the conversion failed. */
600 if (!true_expr || !false_expr)
601 goto fail;
602 #endif
604 true_prob_val = find_reg_note (BB_END (test_bb), REG_BR_PROB, NULL_RTX);
605 if (true_prob_val)
607 true_prob_val = XEXP (true_prob_val, 0);
608 false_prob_val = GEN_INT (REG_BR_PROB_BASE - INTVAL (true_prob_val));
610 else
611 false_prob_val = NULL_RTX;
613 /* If we have && or || tests, do them here. These tests are in the adjacent
614 blocks after the first block containing the test. */
615 if (ce_info->num_multiple_test_blocks > 0)
617 basic_block bb = test_bb;
618 basic_block last_test_bb = ce_info->last_test_bb;
620 if (! false_expr)
621 goto fail;
625 rtx start, end;
626 rtx t, f;
627 enum rtx_code f_code;
629 bb = block_fallthru (bb);
630 start = first_active_insn (bb);
631 end = last_active_insn (bb, TRUE);
632 if (start
633 && ! cond_exec_process_insns (ce_info, start, end, false_expr,
634 false_prob_val, FALSE))
635 goto fail;
637 /* If the conditional jump is more than just a conditional jump, then
638 we can not do conditional execution conversion on this block. */
639 if (! onlyjump_p (BB_END (bb)))
640 goto fail;
642 /* Find the conditional jump and isolate the test. */
643 t = cond_exec_get_condition (BB_END (bb));
644 if (! t)
645 goto fail;
647 f_code = reversed_comparison_code (t, BB_END (bb));
648 if (f_code == UNKNOWN)
649 goto fail;
651 f = gen_rtx_fmt_ee (f_code, GET_MODE (t), XEXP (t, 0), XEXP (t, 1));
652 if (ce_info->and_and_p)
654 t = gen_rtx_AND (GET_MODE (t), true_expr, t);
655 f = gen_rtx_IOR (GET_MODE (t), false_expr, f);
657 else
659 t = gen_rtx_IOR (GET_MODE (t), true_expr, t);
660 f = gen_rtx_AND (GET_MODE (t), false_expr, f);
663 /* If the machine description needs to modify the tests, such as
664 setting a conditional execution register from a comparison, it can
665 do so here. */
666 #ifdef IFCVT_MODIFY_MULTIPLE_TESTS
667 IFCVT_MODIFY_MULTIPLE_TESTS (ce_info, bb, t, f);
669 /* See if the conversion failed. */
670 if (!t || !f)
671 goto fail;
672 #endif
674 true_expr = t;
675 false_expr = f;
677 while (bb != last_test_bb);
680 /* For IF-THEN-ELSE blocks, we don't allow modifications of the test
681 on then THEN block. */
682 then_mod_ok = (else_bb == NULL_BLOCK);
684 /* Go through the THEN and ELSE blocks converting the insns if possible
685 to conditional execution. */
687 if (then_end
688 && (! false_expr
689 || ! cond_exec_process_insns (ce_info, then_start, then_end,
690 false_expr, false_prob_val,
691 then_mod_ok)))
692 goto fail;
694 if (else_bb && else_end
695 && ! cond_exec_process_insns (ce_info, else_start, else_end,
696 true_expr, true_prob_val, TRUE))
697 goto fail;
699 /* If we cannot apply the changes, fail. Do not go through the normal fail
700 processing, since apply_change_group will call cancel_changes. */
701 if (! apply_change_group ())
703 #ifdef IFCVT_MODIFY_CANCEL
704 /* Cancel any machine dependent changes. */
705 IFCVT_MODIFY_CANCEL (ce_info);
706 #endif
707 return FALSE;
710 #ifdef IFCVT_MODIFY_FINAL
711 /* Do any machine dependent final modifications. */
712 IFCVT_MODIFY_FINAL (ce_info);
713 #endif
715 /* Conversion succeeded. */
716 if (dump_file)
717 fprintf (dump_file, "%d insn%s converted to conditional execution.\n",
718 n_insns, (n_insns == 1) ? " was" : "s were");
720 /* Merge the blocks! If we had matching sequences, make sure to delete one
721 copy at the appropriate location first: delete the copy in the THEN branch
722 for a tail sequence so that the remaining one is executed last for both
723 branches, and delete the copy in the ELSE branch for a head sequence so
724 that the remaining one is executed first for both branches. */
725 if (then_first_tail)
727 rtx from = then_first_tail;
728 if (!INSN_P (from))
729 from = find_active_insn_after (then_bb, from);
730 delete_insn_chain (from, BB_END (then_bb), false);
732 if (else_last_head)
733 delete_insn_chain (first_active_insn (else_bb), else_last_head, false);
735 merge_if_block (ce_info);
736 cond_exec_changed_p = TRUE;
737 return TRUE;
739 fail:
740 #ifdef IFCVT_MODIFY_CANCEL
741 /* Cancel any machine dependent changes. */
742 IFCVT_MODIFY_CANCEL (ce_info);
743 #endif
745 cancel_changes (0);
746 return FALSE;
749 /* Used by noce_process_if_block to communicate with its subroutines.
751 The subroutines know that A and B may be evaluated freely. They
752 know that X is a register. They should insert new instructions
753 before cond_earliest. */
755 struct noce_if_info
757 /* The basic blocks that make up the IF-THEN-{ELSE-,}JOIN block. */
758 basic_block test_bb, then_bb, else_bb, join_bb;
760 /* The jump that ends TEST_BB. */
761 rtx jump;
763 /* The jump condition. */
764 rtx cond;
766 /* New insns should be inserted before this one. */
767 rtx cond_earliest;
769 /* Insns in the THEN and ELSE block. There is always just this
770 one insns in those blocks. The insns are single_set insns.
771 If there was no ELSE block, INSN_B is the last insn before
772 COND_EARLIEST, or NULL_RTX. In the former case, the insn
773 operands are still valid, as if INSN_B was moved down below
774 the jump. */
775 rtx insn_a, insn_b;
777 /* The SET_SRC of INSN_A and INSN_B. */
778 rtx a, b;
780 /* The SET_DEST of INSN_A. */
781 rtx x;
783 /* True if this if block is not canonical. In the canonical form of
784 if blocks, the THEN_BB is the block reached via the fallthru edge
785 from TEST_BB. For the noce transformations, we allow the symmetric
786 form as well. */
787 bool then_else_reversed;
789 /* Estimated cost of the particular branch instruction. */
790 int branch_cost;
793 static rtx noce_emit_store_flag (struct noce_if_info *, rtx, int, int);
794 static int noce_try_move (struct noce_if_info *);
795 static int noce_try_store_flag (struct noce_if_info *);
796 static int noce_try_addcc (struct noce_if_info *);
797 static int noce_try_store_flag_constants (struct noce_if_info *);
798 static int noce_try_store_flag_mask (struct noce_if_info *);
799 static rtx noce_emit_cmove (struct noce_if_info *, rtx, enum rtx_code, rtx,
800 rtx, rtx, rtx);
801 static int noce_try_cmove (struct noce_if_info *);
802 static int noce_try_cmove_arith (struct noce_if_info *);
803 static rtx noce_get_alt_condition (struct noce_if_info *, rtx, rtx *);
804 static int noce_try_minmax (struct noce_if_info *);
805 static int noce_try_abs (struct noce_if_info *);
806 static int noce_try_sign_mask (struct noce_if_info *);
808 /* Helper function for noce_try_store_flag*. */
810 static rtx
811 noce_emit_store_flag (struct noce_if_info *if_info, rtx x, int reversep,
812 int normalize)
814 rtx cond = if_info->cond;
815 int cond_complex;
816 enum rtx_code code;
818 cond_complex = (! general_operand (XEXP (cond, 0), VOIDmode)
819 || ! general_operand (XEXP (cond, 1), VOIDmode));
821 /* If earliest == jump, or when the condition is complex, try to
822 build the store_flag insn directly. */
824 if (cond_complex)
826 rtx set = pc_set (if_info->jump);
827 cond = XEXP (SET_SRC (set), 0);
828 if (GET_CODE (XEXP (SET_SRC (set), 2)) == LABEL_REF
829 && XEXP (XEXP (SET_SRC (set), 2), 0) == JUMP_LABEL (if_info->jump))
830 reversep = !reversep;
831 if (if_info->then_else_reversed)
832 reversep = !reversep;
835 if (reversep)
836 code = reversed_comparison_code (cond, if_info->jump);
837 else
838 code = GET_CODE (cond);
840 if ((if_info->cond_earliest == if_info->jump || cond_complex)
841 && (normalize == 0 || STORE_FLAG_VALUE == normalize))
843 rtx tmp;
845 tmp = gen_rtx_fmt_ee (code, GET_MODE (x), XEXP (cond, 0),
846 XEXP (cond, 1));
847 tmp = gen_rtx_SET (VOIDmode, x, tmp);
849 start_sequence ();
850 tmp = emit_insn (tmp);
852 if (recog_memoized (tmp) >= 0)
854 tmp = get_insns ();
855 end_sequence ();
856 emit_insn (tmp);
858 if_info->cond_earliest = if_info->jump;
860 return x;
863 end_sequence ();
866 /* Don't even try if the comparison operands or the mode of X are weird. */
867 if (cond_complex || !SCALAR_INT_MODE_P (GET_MODE (x)))
868 return NULL_RTX;
870 return emit_store_flag (x, code, XEXP (cond, 0),
871 XEXP (cond, 1), VOIDmode,
872 (code == LTU || code == LEU
873 || code == GEU || code == GTU), normalize);
876 /* Emit instruction to move an rtx, possibly into STRICT_LOW_PART.
877 X is the destination/target and Y is the value to copy. */
879 static void
880 noce_emit_move_insn (rtx x, rtx y)
882 enum machine_mode outmode;
883 rtx outer, inner;
884 int bitpos;
886 if (GET_CODE (x) != STRICT_LOW_PART)
888 rtx seq, insn, target;
889 optab ot;
891 start_sequence ();
892 /* Check that the SET_SRC is reasonable before calling emit_move_insn,
893 otherwise construct a suitable SET pattern ourselves. */
894 insn = (OBJECT_P (y) || CONSTANT_P (y) || GET_CODE (y) == SUBREG)
895 ? emit_move_insn (x, y)
896 : emit_insn (gen_rtx_SET (VOIDmode, x, y));
897 seq = get_insns ();
898 end_sequence ();
900 if (recog_memoized (insn) <= 0)
902 if (GET_CODE (x) == ZERO_EXTRACT)
904 rtx op = XEXP (x, 0);
905 unsigned HOST_WIDE_INT size = INTVAL (XEXP (x, 1));
906 unsigned HOST_WIDE_INT start = INTVAL (XEXP (x, 2));
908 /* store_bit_field expects START to be relative to
909 BYTES_BIG_ENDIAN and adjusts this value for machines with
910 BITS_BIG_ENDIAN != BYTES_BIG_ENDIAN. In order to be able to
911 invoke store_bit_field again it is necessary to have the START
912 value from the first call. */
913 if (BITS_BIG_ENDIAN != BYTES_BIG_ENDIAN)
915 if (MEM_P (op))
916 start = BITS_PER_UNIT - start - size;
917 else
919 gcc_assert (REG_P (op));
920 start = BITS_PER_WORD - start - size;
924 gcc_assert (start < (MEM_P (op) ? BITS_PER_UNIT : BITS_PER_WORD));
925 store_bit_field (op, size, start, 0, 0, GET_MODE (x), y);
926 return;
929 switch (GET_RTX_CLASS (GET_CODE (y)))
931 case RTX_UNARY:
932 ot = code_to_optab (GET_CODE (y));
933 if (ot)
935 start_sequence ();
936 target = expand_unop (GET_MODE (y), ot, XEXP (y, 0), x, 0);
937 if (target != NULL_RTX)
939 if (target != x)
940 emit_move_insn (x, target);
941 seq = get_insns ();
943 end_sequence ();
945 break;
947 case RTX_BIN_ARITH:
948 case RTX_COMM_ARITH:
949 ot = code_to_optab (GET_CODE (y));
950 if (ot)
952 start_sequence ();
953 target = expand_binop (GET_MODE (y), ot,
954 XEXP (y, 0), XEXP (y, 1),
955 x, 0, OPTAB_DIRECT);
956 if (target != NULL_RTX)
958 if (target != x)
959 emit_move_insn (x, target);
960 seq = get_insns ();
962 end_sequence ();
964 break;
966 default:
967 break;
971 emit_insn (seq);
972 return;
975 outer = XEXP (x, 0);
976 inner = XEXP (outer, 0);
977 outmode = GET_MODE (outer);
978 bitpos = SUBREG_BYTE (outer) * BITS_PER_UNIT;
979 store_bit_field (inner, GET_MODE_BITSIZE (outmode), bitpos,
980 0, 0, outmode, y);
983 /* Return sequence of instructions generated by if conversion. This
984 function calls end_sequence() to end the current stream, ensures
985 that are instructions are unshared, recognizable non-jump insns.
986 On failure, this function returns a NULL_RTX. */
988 static rtx
989 end_ifcvt_sequence (struct noce_if_info *if_info)
991 rtx insn;
992 rtx seq = get_insns ();
994 set_used_flags (if_info->x);
995 set_used_flags (if_info->cond);
996 set_used_flags (if_info->a);
997 set_used_flags (if_info->b);
998 unshare_all_rtl_in_chain (seq);
999 end_sequence ();
1001 /* Make sure that all of the instructions emitted are recognizable,
1002 and that we haven't introduced a new jump instruction.
1003 As an exercise for the reader, build a general mechanism that
1004 allows proper placement of required clobbers. */
1005 for (insn = seq; insn; insn = NEXT_INSN (insn))
1006 if (JUMP_P (insn)
1007 || recog_memoized (insn) == -1)
1008 return NULL_RTX;
1010 return seq;
1013 /* Convert "if (a != b) x = a; else x = b" into "x = a" and
1014 "if (a == b) x = a; else x = b" into "x = b". */
1016 static int
1017 noce_try_move (struct noce_if_info *if_info)
1019 rtx cond = if_info->cond;
1020 enum rtx_code code = GET_CODE (cond);
1021 rtx y, seq;
1023 if (code != NE && code != EQ)
1024 return FALSE;
1026 /* This optimization isn't valid if either A or B could be a NaN
1027 or a signed zero. */
1028 if (HONOR_NANS (GET_MODE (if_info->x))
1029 || HONOR_SIGNED_ZEROS (GET_MODE (if_info->x)))
1030 return FALSE;
1032 /* Check whether the operands of the comparison are A and in
1033 either order. */
1034 if ((rtx_equal_p (if_info->a, XEXP (cond, 0))
1035 && rtx_equal_p (if_info->b, XEXP (cond, 1)))
1036 || (rtx_equal_p (if_info->a, XEXP (cond, 1))
1037 && rtx_equal_p (if_info->b, XEXP (cond, 0))))
1039 if (!rtx_interchangeable_p (if_info->a, if_info->b))
1040 return FALSE;
1042 y = (code == EQ) ? if_info->a : if_info->b;
1044 /* Avoid generating the move if the source is the destination. */
1045 if (! rtx_equal_p (if_info->x, y))
1047 start_sequence ();
1048 noce_emit_move_insn (if_info->x, y);
1049 seq = end_ifcvt_sequence (if_info);
1050 if (!seq)
1051 return FALSE;
1053 emit_insn_before_setloc (seq, if_info->jump,
1054 INSN_LOCATION (if_info->insn_a));
1056 return TRUE;
1058 return FALSE;
1061 /* Convert "if (test) x = 1; else x = 0".
1063 Only try 0 and STORE_FLAG_VALUE here. Other combinations will be
1064 tried in noce_try_store_flag_constants after noce_try_cmove has had
1065 a go at the conversion. */
1067 static int
1068 noce_try_store_flag (struct noce_if_info *if_info)
1070 int reversep;
1071 rtx target, seq;
1073 if (CONST_INT_P (if_info->b)
1074 && INTVAL (if_info->b) == STORE_FLAG_VALUE
1075 && if_info->a == const0_rtx)
1076 reversep = 0;
1077 else if (if_info->b == const0_rtx
1078 && CONST_INT_P (if_info->a)
1079 && INTVAL (if_info->a) == STORE_FLAG_VALUE
1080 && (reversed_comparison_code (if_info->cond, if_info->jump)
1081 != UNKNOWN))
1082 reversep = 1;
1083 else
1084 return FALSE;
1086 start_sequence ();
1088 target = noce_emit_store_flag (if_info, if_info->x, reversep, 0);
1089 if (target)
1091 if (target != if_info->x)
1092 noce_emit_move_insn (if_info->x, target);
1094 seq = end_ifcvt_sequence (if_info);
1095 if (! seq)
1096 return FALSE;
1098 emit_insn_before_setloc (seq, if_info->jump,
1099 INSN_LOCATION (if_info->insn_a));
1100 return TRUE;
1102 else
1104 end_sequence ();
1105 return FALSE;
1109 /* Convert "if (test) x = a; else x = b", for A and B constant. */
1111 static int
1112 noce_try_store_flag_constants (struct noce_if_info *if_info)
1114 rtx target, seq;
1115 int reversep;
1116 HOST_WIDE_INT itrue, ifalse, diff, tmp;
1117 int normalize, can_reverse;
1118 enum machine_mode mode;
1120 if (CONST_INT_P (if_info->a)
1121 && CONST_INT_P (if_info->b))
1123 mode = GET_MODE (if_info->x);
1124 ifalse = INTVAL (if_info->a);
1125 itrue = INTVAL (if_info->b);
1127 /* Make sure we can represent the difference between the two values. */
1128 if ((itrue - ifalse > 0)
1129 != ((ifalse < 0) != (itrue < 0) ? ifalse < 0 : ifalse < itrue))
1130 return FALSE;
1132 diff = trunc_int_for_mode (itrue - ifalse, mode);
1134 can_reverse = (reversed_comparison_code (if_info->cond, if_info->jump)
1135 != UNKNOWN);
1137 reversep = 0;
1138 if (diff == STORE_FLAG_VALUE || diff == -STORE_FLAG_VALUE)
1139 normalize = 0;
1140 else if (ifalse == 0 && exact_log2 (itrue) >= 0
1141 && (STORE_FLAG_VALUE == 1
1142 || if_info->branch_cost >= 2))
1143 normalize = 1;
1144 else if (itrue == 0 && exact_log2 (ifalse) >= 0 && can_reverse
1145 && (STORE_FLAG_VALUE == 1 || if_info->branch_cost >= 2))
1146 normalize = 1, reversep = 1;
1147 else if (itrue == -1
1148 && (STORE_FLAG_VALUE == -1
1149 || if_info->branch_cost >= 2))
1150 normalize = -1;
1151 else if (ifalse == -1 && can_reverse
1152 && (STORE_FLAG_VALUE == -1 || if_info->branch_cost >= 2))
1153 normalize = -1, reversep = 1;
1154 else if ((if_info->branch_cost >= 2 && STORE_FLAG_VALUE == -1)
1155 || if_info->branch_cost >= 3)
1156 normalize = -1;
1157 else
1158 return FALSE;
1160 if (reversep)
1162 tmp = itrue; itrue = ifalse; ifalse = tmp;
1163 diff = trunc_int_for_mode (-diff, mode);
1166 start_sequence ();
1167 target = noce_emit_store_flag (if_info, if_info->x, reversep, normalize);
1168 if (! target)
1170 end_sequence ();
1171 return FALSE;
1174 /* if (test) x = 3; else x = 4;
1175 => x = 3 + (test == 0); */
1176 if (diff == STORE_FLAG_VALUE || diff == -STORE_FLAG_VALUE)
1178 target = expand_simple_binop (mode,
1179 (diff == STORE_FLAG_VALUE
1180 ? PLUS : MINUS),
1181 GEN_INT (ifalse), target, if_info->x, 0,
1182 OPTAB_WIDEN);
1185 /* if (test) x = 8; else x = 0;
1186 => x = (test != 0) << 3; */
1187 else if (ifalse == 0 && (tmp = exact_log2 (itrue)) >= 0)
1189 target = expand_simple_binop (mode, ASHIFT,
1190 target, GEN_INT (tmp), if_info->x, 0,
1191 OPTAB_WIDEN);
1194 /* if (test) x = -1; else x = b;
1195 => x = -(test != 0) | b; */
1196 else if (itrue == -1)
1198 target = expand_simple_binop (mode, IOR,
1199 target, GEN_INT (ifalse), if_info->x, 0,
1200 OPTAB_WIDEN);
1203 /* if (test) x = a; else x = b;
1204 => x = (-(test != 0) & (b - a)) + a; */
1205 else
1207 target = expand_simple_binop (mode, AND,
1208 target, GEN_INT (diff), if_info->x, 0,
1209 OPTAB_WIDEN);
1210 if (target)
1211 target = expand_simple_binop (mode, PLUS,
1212 target, GEN_INT (ifalse),
1213 if_info->x, 0, OPTAB_WIDEN);
1216 if (! target)
1218 end_sequence ();
1219 return FALSE;
1222 if (target != if_info->x)
1223 noce_emit_move_insn (if_info->x, target);
1225 seq = end_ifcvt_sequence (if_info);
1226 if (!seq)
1227 return FALSE;
1229 emit_insn_before_setloc (seq, if_info->jump,
1230 INSN_LOCATION (if_info->insn_a));
1231 return TRUE;
1234 return FALSE;
1237 /* Convert "if (test) foo++" into "foo += (test != 0)", and
1238 similarly for "foo--". */
1240 static int
1241 noce_try_addcc (struct noce_if_info *if_info)
1243 rtx target, seq;
1244 int subtract, normalize;
1246 if (GET_CODE (if_info->a) == PLUS
1247 && rtx_equal_p (XEXP (if_info->a, 0), if_info->b)
1248 && (reversed_comparison_code (if_info->cond, if_info->jump)
1249 != UNKNOWN))
1251 rtx cond = if_info->cond;
1252 enum rtx_code code = reversed_comparison_code (cond, if_info->jump);
1254 /* First try to use addcc pattern. */
1255 if (general_operand (XEXP (cond, 0), VOIDmode)
1256 && general_operand (XEXP (cond, 1), VOIDmode))
1258 start_sequence ();
1259 target = emit_conditional_add (if_info->x, code,
1260 XEXP (cond, 0),
1261 XEXP (cond, 1),
1262 VOIDmode,
1263 if_info->b,
1264 XEXP (if_info->a, 1),
1265 GET_MODE (if_info->x),
1266 (code == LTU || code == GEU
1267 || code == LEU || code == GTU));
1268 if (target)
1270 if (target != if_info->x)
1271 noce_emit_move_insn (if_info->x, target);
1273 seq = end_ifcvt_sequence (if_info);
1274 if (!seq)
1275 return FALSE;
1277 emit_insn_before_setloc (seq, if_info->jump,
1278 INSN_LOCATION (if_info->insn_a));
1279 return TRUE;
1281 end_sequence ();
1284 /* If that fails, construct conditional increment or decrement using
1285 setcc. */
1286 if (if_info->branch_cost >= 2
1287 && (XEXP (if_info->a, 1) == const1_rtx
1288 || XEXP (if_info->a, 1) == constm1_rtx))
1290 start_sequence ();
1291 if (STORE_FLAG_VALUE == INTVAL (XEXP (if_info->a, 1)))
1292 subtract = 0, normalize = 0;
1293 else if (-STORE_FLAG_VALUE == INTVAL (XEXP (if_info->a, 1)))
1294 subtract = 1, normalize = 0;
1295 else
1296 subtract = 0, normalize = INTVAL (XEXP (if_info->a, 1));
1299 target = noce_emit_store_flag (if_info,
1300 gen_reg_rtx (GET_MODE (if_info->x)),
1301 1, normalize);
1303 if (target)
1304 target = expand_simple_binop (GET_MODE (if_info->x),
1305 subtract ? MINUS : PLUS,
1306 if_info->b, target, if_info->x,
1307 0, OPTAB_WIDEN);
1308 if (target)
1310 if (target != if_info->x)
1311 noce_emit_move_insn (if_info->x, target);
1313 seq = end_ifcvt_sequence (if_info);
1314 if (!seq)
1315 return FALSE;
1317 emit_insn_before_setloc (seq, if_info->jump,
1318 INSN_LOCATION (if_info->insn_a));
1319 return TRUE;
1321 end_sequence ();
1325 return FALSE;
1328 /* Convert "if (test) x = 0;" to "x &= -(test == 0);" */
1330 static int
1331 noce_try_store_flag_mask (struct noce_if_info *if_info)
1333 rtx target, seq;
1334 int reversep;
1336 reversep = 0;
1337 if ((if_info->branch_cost >= 2
1338 || STORE_FLAG_VALUE == -1)
1339 && ((if_info->a == const0_rtx
1340 && rtx_equal_p (if_info->b, if_info->x))
1341 || ((reversep = (reversed_comparison_code (if_info->cond,
1342 if_info->jump)
1343 != UNKNOWN))
1344 && if_info->b == const0_rtx
1345 && rtx_equal_p (if_info->a, if_info->x))))
1347 start_sequence ();
1348 target = noce_emit_store_flag (if_info,
1349 gen_reg_rtx (GET_MODE (if_info->x)),
1350 reversep, -1);
1351 if (target)
1352 target = expand_simple_binop (GET_MODE (if_info->x), AND,
1353 if_info->x,
1354 target, if_info->x, 0,
1355 OPTAB_WIDEN);
1357 if (target)
1359 if (target != if_info->x)
1360 noce_emit_move_insn (if_info->x, target);
1362 seq = end_ifcvt_sequence (if_info);
1363 if (!seq)
1364 return FALSE;
1366 emit_insn_before_setloc (seq, if_info->jump,
1367 INSN_LOCATION (if_info->insn_a));
1368 return TRUE;
1371 end_sequence ();
1374 return FALSE;
1377 /* Helper function for noce_try_cmove and noce_try_cmove_arith. */
1379 static rtx
1380 noce_emit_cmove (struct noce_if_info *if_info, rtx x, enum rtx_code code,
1381 rtx cmp_a, rtx cmp_b, rtx vfalse, rtx vtrue)
1383 rtx target ATTRIBUTE_UNUSED;
1384 int unsignedp ATTRIBUTE_UNUSED;
1386 /* If earliest == jump, try to build the cmove insn directly.
1387 This is helpful when combine has created some complex condition
1388 (like for alpha's cmovlbs) that we can't hope to regenerate
1389 through the normal interface. */
1391 if (if_info->cond_earliest == if_info->jump)
1393 rtx tmp;
1395 tmp = gen_rtx_fmt_ee (code, GET_MODE (if_info->cond), cmp_a, cmp_b);
1396 tmp = gen_rtx_IF_THEN_ELSE (GET_MODE (x), tmp, vtrue, vfalse);
1397 tmp = gen_rtx_SET (VOIDmode, x, tmp);
1399 start_sequence ();
1400 tmp = emit_insn (tmp);
1402 if (recog_memoized (tmp) >= 0)
1404 tmp = get_insns ();
1405 end_sequence ();
1406 emit_insn (tmp);
1408 return x;
1411 end_sequence ();
1414 /* Don't even try if the comparison operands are weird. */
1415 if (! general_operand (cmp_a, GET_MODE (cmp_a))
1416 || ! general_operand (cmp_b, GET_MODE (cmp_b)))
1417 return NULL_RTX;
1419 #if HAVE_conditional_move
1420 unsignedp = (code == LTU || code == GEU
1421 || code == LEU || code == GTU);
1423 target = emit_conditional_move (x, code, cmp_a, cmp_b, VOIDmode,
1424 vtrue, vfalse, GET_MODE (x),
1425 unsignedp);
1426 if (target)
1427 return target;
1429 /* We might be faced with a situation like:
1431 x = (reg:M TARGET)
1432 vtrue = (subreg:M (reg:N VTRUE) BYTE)
1433 vfalse = (subreg:M (reg:N VFALSE) BYTE)
1435 We can't do a conditional move in mode M, but it's possible that we
1436 could do a conditional move in mode N instead and take a subreg of
1437 the result.
1439 If we can't create new pseudos, though, don't bother. */
1440 if (reload_completed)
1441 return NULL_RTX;
1443 if (GET_CODE (vtrue) == SUBREG && GET_CODE (vfalse) == SUBREG)
1445 rtx reg_vtrue = SUBREG_REG (vtrue);
1446 rtx reg_vfalse = SUBREG_REG (vfalse);
1447 unsigned int byte_vtrue = SUBREG_BYTE (vtrue);
1448 unsigned int byte_vfalse = SUBREG_BYTE (vfalse);
1449 rtx promoted_target;
1451 if (GET_MODE (reg_vtrue) != GET_MODE (reg_vfalse)
1452 || byte_vtrue != byte_vfalse
1453 || (SUBREG_PROMOTED_VAR_P (vtrue)
1454 != SUBREG_PROMOTED_VAR_P (vfalse))
1455 || (SUBREG_PROMOTED_UNSIGNED_P (vtrue)
1456 != SUBREG_PROMOTED_UNSIGNED_P (vfalse)))
1457 return NULL_RTX;
1459 promoted_target = gen_reg_rtx (GET_MODE (reg_vtrue));
1461 target = emit_conditional_move (promoted_target, code, cmp_a, cmp_b,
1462 VOIDmode, reg_vtrue, reg_vfalse,
1463 GET_MODE (reg_vtrue), unsignedp);
1464 /* Nope, couldn't do it in that mode either. */
1465 if (!target)
1466 return NULL_RTX;
1468 target = gen_rtx_SUBREG (GET_MODE (vtrue), promoted_target, byte_vtrue);
1469 SUBREG_PROMOTED_VAR_P (target) = SUBREG_PROMOTED_VAR_P (vtrue);
1470 SUBREG_PROMOTED_UNSIGNED_SET (target, SUBREG_PROMOTED_UNSIGNED_P (vtrue));
1471 emit_move_insn (x, target);
1472 return x;
1474 else
1475 return NULL_RTX;
1476 #else
1477 /* We'll never get here, as noce_process_if_block doesn't call the
1478 functions involved. Ifdef code, however, should be discouraged
1479 because it leads to typos in the code not selected. However,
1480 emit_conditional_move won't exist either. */
1481 return NULL_RTX;
1482 #endif
1485 /* Try only simple constants and registers here. More complex cases
1486 are handled in noce_try_cmove_arith after noce_try_store_flag_arith
1487 has had a go at it. */
1489 static int
1490 noce_try_cmove (struct noce_if_info *if_info)
1492 enum rtx_code code;
1493 rtx target, seq;
1495 if ((CONSTANT_P (if_info->a) || register_operand (if_info->a, VOIDmode))
1496 && (CONSTANT_P (if_info->b) || register_operand (if_info->b, VOIDmode)))
1498 start_sequence ();
1500 code = GET_CODE (if_info->cond);
1501 target = noce_emit_cmove (if_info, if_info->x, code,
1502 XEXP (if_info->cond, 0),
1503 XEXP (if_info->cond, 1),
1504 if_info->a, if_info->b);
1506 if (target)
1508 if (target != if_info->x)
1509 noce_emit_move_insn (if_info->x, target);
1511 seq = end_ifcvt_sequence (if_info);
1512 if (!seq)
1513 return FALSE;
1515 emit_insn_before_setloc (seq, if_info->jump,
1516 INSN_LOCATION (if_info->insn_a));
1517 return TRUE;
1519 else
1521 end_sequence ();
1522 return FALSE;
1526 return FALSE;
1529 /* Try more complex cases involving conditional_move. */
1531 static int
1532 noce_try_cmove_arith (struct noce_if_info *if_info)
1534 rtx a = if_info->a;
1535 rtx b = if_info->b;
1536 rtx x = if_info->x;
1537 rtx orig_a, orig_b;
1538 rtx insn_a, insn_b;
1539 rtx tmp, target;
1540 int is_mem = 0;
1541 int insn_cost;
1542 enum rtx_code code;
1544 /* A conditional move from two memory sources is equivalent to a
1545 conditional on their addresses followed by a load. Don't do this
1546 early because it'll screw alias analysis. Note that we've
1547 already checked for no side effects. */
1548 /* ??? FIXME: Magic number 5. */
1549 if (cse_not_expected
1550 && MEM_P (a) && MEM_P (b)
1551 && MEM_ADDR_SPACE (a) == MEM_ADDR_SPACE (b)
1552 && if_info->branch_cost >= 5)
1554 enum machine_mode address_mode = get_address_mode (a);
1556 a = XEXP (a, 0);
1557 b = XEXP (b, 0);
1558 x = gen_reg_rtx (address_mode);
1559 is_mem = 1;
1562 /* ??? We could handle this if we knew that a load from A or B could
1563 not trap or fault. This is also true if we've already loaded
1564 from the address along the path from ENTRY. */
1565 else if (may_trap_or_fault_p (a) || may_trap_or_fault_p (b))
1566 return FALSE;
1568 /* if (test) x = a + b; else x = c - d;
1569 => y = a + b;
1570 x = c - d;
1571 if (test)
1572 x = y;
1575 code = GET_CODE (if_info->cond);
1576 insn_a = if_info->insn_a;
1577 insn_b = if_info->insn_b;
1579 /* Total insn_rtx_cost should be smaller than branch cost. Exit
1580 if insn_rtx_cost can't be estimated. */
1581 if (insn_a)
1583 insn_cost
1584 = insn_rtx_cost (PATTERN (insn_a),
1585 optimize_bb_for_speed_p (BLOCK_FOR_INSN (insn_a)));
1586 if (insn_cost == 0 || insn_cost > COSTS_N_INSNS (if_info->branch_cost))
1587 return FALSE;
1589 else
1590 insn_cost = 0;
1592 if (insn_b)
1594 insn_cost
1595 += insn_rtx_cost (PATTERN (insn_b),
1596 optimize_bb_for_speed_p (BLOCK_FOR_INSN (insn_b)));
1597 if (insn_cost == 0 || insn_cost > COSTS_N_INSNS (if_info->branch_cost))
1598 return FALSE;
1601 /* Possibly rearrange operands to make things come out more natural. */
1602 if (reversed_comparison_code (if_info->cond, if_info->jump) != UNKNOWN)
1604 int reversep = 0;
1605 if (rtx_equal_p (b, x))
1606 reversep = 1;
1607 else if (general_operand (b, GET_MODE (b)))
1608 reversep = 1;
1610 if (reversep)
1612 code = reversed_comparison_code (if_info->cond, if_info->jump);
1613 tmp = a, a = b, b = tmp;
1614 tmp = insn_a, insn_a = insn_b, insn_b = tmp;
1618 start_sequence ();
1620 orig_a = a;
1621 orig_b = b;
1623 /* If either operand is complex, load it into a register first.
1624 The best way to do this is to copy the original insn. In this
1625 way we preserve any clobbers etc that the insn may have had.
1626 This is of course not possible in the IS_MEM case. */
1627 if (! general_operand (a, GET_MODE (a)))
1629 rtx set;
1631 if (is_mem)
1633 tmp = gen_reg_rtx (GET_MODE (a));
1634 tmp = emit_insn (gen_rtx_SET (VOIDmode, tmp, a));
1636 else if (! insn_a)
1637 goto end_seq_and_fail;
1638 else
1640 a = gen_reg_rtx (GET_MODE (a));
1641 tmp = copy_rtx (insn_a);
1642 set = single_set (tmp);
1643 SET_DEST (set) = a;
1644 tmp = emit_insn (PATTERN (tmp));
1646 if (recog_memoized (tmp) < 0)
1647 goto end_seq_and_fail;
1649 if (! general_operand (b, GET_MODE (b)))
1651 rtx set, last;
1653 if (is_mem)
1655 tmp = gen_reg_rtx (GET_MODE (b));
1656 tmp = gen_rtx_SET (VOIDmode, tmp, b);
1658 else if (! insn_b)
1659 goto end_seq_and_fail;
1660 else
1662 b = gen_reg_rtx (GET_MODE (b));
1663 tmp = copy_rtx (insn_b);
1664 set = single_set (tmp);
1665 SET_DEST (set) = b;
1666 tmp = PATTERN (tmp);
1669 /* If insn to set up A clobbers any registers B depends on, try to
1670 swap insn that sets up A with the one that sets up B. If even
1671 that doesn't help, punt. */
1672 last = get_last_insn ();
1673 if (last && modified_in_p (orig_b, last))
1675 tmp = emit_insn_before (tmp, get_insns ());
1676 if (modified_in_p (orig_a, tmp))
1677 goto end_seq_and_fail;
1679 else
1680 tmp = emit_insn (tmp);
1682 if (recog_memoized (tmp) < 0)
1683 goto end_seq_and_fail;
1686 target = noce_emit_cmove (if_info, x, code, XEXP (if_info->cond, 0),
1687 XEXP (if_info->cond, 1), a, b);
1689 if (! target)
1690 goto end_seq_and_fail;
1692 /* If we're handling a memory for above, emit the load now. */
1693 if (is_mem)
1695 tmp = gen_rtx_MEM (GET_MODE (if_info->x), target);
1697 /* Copy over flags as appropriate. */
1698 if (MEM_VOLATILE_P (if_info->a) || MEM_VOLATILE_P (if_info->b))
1699 MEM_VOLATILE_P (tmp) = 1;
1700 if (MEM_ALIAS_SET (if_info->a) == MEM_ALIAS_SET (if_info->b))
1701 set_mem_alias_set (tmp, MEM_ALIAS_SET (if_info->a));
1702 set_mem_align (tmp,
1703 MIN (MEM_ALIGN (if_info->a), MEM_ALIGN (if_info->b)));
1705 gcc_assert (MEM_ADDR_SPACE (if_info->a) == MEM_ADDR_SPACE (if_info->b));
1706 set_mem_addr_space (tmp, MEM_ADDR_SPACE (if_info->a));
1708 noce_emit_move_insn (if_info->x, tmp);
1710 else if (target != x)
1711 noce_emit_move_insn (x, target);
1713 tmp = end_ifcvt_sequence (if_info);
1714 if (!tmp)
1715 return FALSE;
1717 emit_insn_before_setloc (tmp, if_info->jump, INSN_LOCATION (if_info->insn_a));
1718 return TRUE;
1720 end_seq_and_fail:
1721 end_sequence ();
1722 return FALSE;
1725 /* For most cases, the simplified condition we found is the best
1726 choice, but this is not the case for the min/max/abs transforms.
1727 For these we wish to know that it is A or B in the condition. */
1729 static rtx
1730 noce_get_alt_condition (struct noce_if_info *if_info, rtx target,
1731 rtx *earliest)
1733 rtx cond, set, insn;
1734 int reverse;
1736 /* If target is already mentioned in the known condition, return it. */
1737 if (reg_mentioned_p (target, if_info->cond))
1739 *earliest = if_info->cond_earliest;
1740 return if_info->cond;
1743 set = pc_set (if_info->jump);
1744 cond = XEXP (SET_SRC (set), 0);
1745 reverse
1746 = GET_CODE (XEXP (SET_SRC (set), 2)) == LABEL_REF
1747 && XEXP (XEXP (SET_SRC (set), 2), 0) == JUMP_LABEL (if_info->jump);
1748 if (if_info->then_else_reversed)
1749 reverse = !reverse;
1751 /* If we're looking for a constant, try to make the conditional
1752 have that constant in it. There are two reasons why it may
1753 not have the constant we want:
1755 1. GCC may have needed to put the constant in a register, because
1756 the target can't compare directly against that constant. For
1757 this case, we look for a SET immediately before the comparison
1758 that puts a constant in that register.
1760 2. GCC may have canonicalized the conditional, for example
1761 replacing "if x < 4" with "if x <= 3". We can undo that (or
1762 make equivalent types of changes) to get the constants we need
1763 if they're off by one in the right direction. */
1765 if (CONST_INT_P (target))
1767 enum rtx_code code = GET_CODE (if_info->cond);
1768 rtx op_a = XEXP (if_info->cond, 0);
1769 rtx op_b = XEXP (if_info->cond, 1);
1770 rtx prev_insn;
1772 /* First, look to see if we put a constant in a register. */
1773 prev_insn = prev_nonnote_insn (if_info->cond_earliest);
1774 if (prev_insn
1775 && BLOCK_FOR_INSN (prev_insn)
1776 == BLOCK_FOR_INSN (if_info->cond_earliest)
1777 && INSN_P (prev_insn)
1778 && GET_CODE (PATTERN (prev_insn)) == SET)
1780 rtx src = find_reg_equal_equiv_note (prev_insn);
1781 if (!src)
1782 src = SET_SRC (PATTERN (prev_insn));
1783 if (CONST_INT_P (src))
1785 if (rtx_equal_p (op_a, SET_DEST (PATTERN (prev_insn))))
1786 op_a = src;
1787 else if (rtx_equal_p (op_b, SET_DEST (PATTERN (prev_insn))))
1788 op_b = src;
1790 if (CONST_INT_P (op_a))
1792 rtx tmp = op_a;
1793 op_a = op_b;
1794 op_b = tmp;
1795 code = swap_condition (code);
1800 /* Now, look to see if we can get the right constant by
1801 adjusting the conditional. */
1802 if (CONST_INT_P (op_b))
1804 HOST_WIDE_INT desired_val = INTVAL (target);
1805 HOST_WIDE_INT actual_val = INTVAL (op_b);
1807 switch (code)
1809 case LT:
1810 if (actual_val == desired_val + 1)
1812 code = LE;
1813 op_b = GEN_INT (desired_val);
1815 break;
1816 case LE:
1817 if (actual_val == desired_val - 1)
1819 code = LT;
1820 op_b = GEN_INT (desired_val);
1822 break;
1823 case GT:
1824 if (actual_val == desired_val - 1)
1826 code = GE;
1827 op_b = GEN_INT (desired_val);
1829 break;
1830 case GE:
1831 if (actual_val == desired_val + 1)
1833 code = GT;
1834 op_b = GEN_INT (desired_val);
1836 break;
1837 default:
1838 break;
1842 /* If we made any changes, generate a new conditional that is
1843 equivalent to what we started with, but has the right
1844 constants in it. */
1845 if (code != GET_CODE (if_info->cond)
1846 || op_a != XEXP (if_info->cond, 0)
1847 || op_b != XEXP (if_info->cond, 1))
1849 cond = gen_rtx_fmt_ee (code, GET_MODE (cond), op_a, op_b);
1850 *earliest = if_info->cond_earliest;
1851 return cond;
1855 cond = canonicalize_condition (if_info->jump, cond, reverse,
1856 earliest, target, false, true);
1857 if (! cond || ! reg_mentioned_p (target, cond))
1858 return NULL;
1860 /* We almost certainly searched back to a different place.
1861 Need to re-verify correct lifetimes. */
1863 /* X may not be mentioned in the range (cond_earliest, jump]. */
1864 for (insn = if_info->jump; insn != *earliest; insn = PREV_INSN (insn))
1865 if (INSN_P (insn) && reg_overlap_mentioned_p (if_info->x, PATTERN (insn)))
1866 return NULL;
1868 /* A and B may not be modified in the range [cond_earliest, jump). */
1869 for (insn = *earliest; insn != if_info->jump; insn = NEXT_INSN (insn))
1870 if (INSN_P (insn)
1871 && (modified_in_p (if_info->a, insn)
1872 || modified_in_p (if_info->b, insn)))
1873 return NULL;
1875 return cond;
1878 /* Convert "if (a < b) x = a; else x = b;" to "x = min(a, b);", etc. */
1880 static int
1881 noce_try_minmax (struct noce_if_info *if_info)
1883 rtx cond, earliest, target, seq;
1884 enum rtx_code code, op;
1885 int unsignedp;
1887 /* ??? Reject modes with NaNs or signed zeros since we don't know how
1888 they will be resolved with an SMIN/SMAX. It wouldn't be too hard
1889 to get the target to tell us... */
1890 if (HONOR_SIGNED_ZEROS (GET_MODE (if_info->x))
1891 || HONOR_NANS (GET_MODE (if_info->x)))
1892 return FALSE;
1894 cond = noce_get_alt_condition (if_info, if_info->a, &earliest);
1895 if (!cond)
1896 return FALSE;
1898 /* Verify the condition is of the form we expect, and canonicalize
1899 the comparison code. */
1900 code = GET_CODE (cond);
1901 if (rtx_equal_p (XEXP (cond, 0), if_info->a))
1903 if (! rtx_equal_p (XEXP (cond, 1), if_info->b))
1904 return FALSE;
1906 else if (rtx_equal_p (XEXP (cond, 1), if_info->a))
1908 if (! rtx_equal_p (XEXP (cond, 0), if_info->b))
1909 return FALSE;
1910 code = swap_condition (code);
1912 else
1913 return FALSE;
1915 /* Determine what sort of operation this is. Note that the code is for
1916 a taken branch, so the code->operation mapping appears backwards. */
1917 switch (code)
1919 case LT:
1920 case LE:
1921 case UNLT:
1922 case UNLE:
1923 op = SMAX;
1924 unsignedp = 0;
1925 break;
1926 case GT:
1927 case GE:
1928 case UNGT:
1929 case UNGE:
1930 op = SMIN;
1931 unsignedp = 0;
1932 break;
1933 case LTU:
1934 case LEU:
1935 op = UMAX;
1936 unsignedp = 1;
1937 break;
1938 case GTU:
1939 case GEU:
1940 op = UMIN;
1941 unsignedp = 1;
1942 break;
1943 default:
1944 return FALSE;
1947 start_sequence ();
1949 target = expand_simple_binop (GET_MODE (if_info->x), op,
1950 if_info->a, if_info->b,
1951 if_info->x, unsignedp, OPTAB_WIDEN);
1952 if (! target)
1954 end_sequence ();
1955 return FALSE;
1957 if (target != if_info->x)
1958 noce_emit_move_insn (if_info->x, target);
1960 seq = end_ifcvt_sequence (if_info);
1961 if (!seq)
1962 return FALSE;
1964 emit_insn_before_setloc (seq, if_info->jump, INSN_LOCATION (if_info->insn_a));
1965 if_info->cond = cond;
1966 if_info->cond_earliest = earliest;
1968 return TRUE;
1971 /* Convert "if (a < 0) x = -a; else x = a;" to "x = abs(a);",
1972 "if (a < 0) x = ~a; else x = a;" to "x = one_cmpl_abs(a);",
1973 etc. */
1975 static int
1976 noce_try_abs (struct noce_if_info *if_info)
1978 rtx cond, earliest, target, seq, a, b, c;
1979 int negate;
1980 bool one_cmpl = false;
1982 /* Reject modes with signed zeros. */
1983 if (HONOR_SIGNED_ZEROS (GET_MODE (if_info->x)))
1984 return FALSE;
1986 /* Recognize A and B as constituting an ABS or NABS. The canonical
1987 form is a branch around the negation, taken when the object is the
1988 first operand of a comparison against 0 that evaluates to true. */
1989 a = if_info->a;
1990 b = if_info->b;
1991 if (GET_CODE (a) == NEG && rtx_equal_p (XEXP (a, 0), b))
1992 negate = 0;
1993 else if (GET_CODE (b) == NEG && rtx_equal_p (XEXP (b, 0), a))
1995 c = a; a = b; b = c;
1996 negate = 1;
1998 else if (GET_CODE (a) == NOT && rtx_equal_p (XEXP (a, 0), b))
2000 negate = 0;
2001 one_cmpl = true;
2003 else if (GET_CODE (b) == NOT && rtx_equal_p (XEXP (b, 0), a))
2005 c = a; a = b; b = c;
2006 negate = 1;
2007 one_cmpl = true;
2009 else
2010 return FALSE;
2012 cond = noce_get_alt_condition (if_info, b, &earliest);
2013 if (!cond)
2014 return FALSE;
2016 /* Verify the condition is of the form we expect. */
2017 if (rtx_equal_p (XEXP (cond, 0), b))
2018 c = XEXP (cond, 1);
2019 else if (rtx_equal_p (XEXP (cond, 1), b))
2021 c = XEXP (cond, 0);
2022 negate = !negate;
2024 else
2025 return FALSE;
2027 /* Verify that C is zero. Search one step backward for a
2028 REG_EQUAL note or a simple source if necessary. */
2029 if (REG_P (c))
2031 rtx set, insn = prev_nonnote_insn (earliest);
2032 if (insn
2033 && BLOCK_FOR_INSN (insn) == BLOCK_FOR_INSN (earliest)
2034 && (set = single_set (insn))
2035 && rtx_equal_p (SET_DEST (set), c))
2037 rtx note = find_reg_equal_equiv_note (insn);
2038 if (note)
2039 c = XEXP (note, 0);
2040 else
2041 c = SET_SRC (set);
2043 else
2044 return FALSE;
2046 if (MEM_P (c)
2047 && GET_CODE (XEXP (c, 0)) == SYMBOL_REF
2048 && CONSTANT_POOL_ADDRESS_P (XEXP (c, 0)))
2049 c = get_pool_constant (XEXP (c, 0));
2051 /* Work around funny ideas get_condition has wrt canonicalization.
2052 Note that these rtx constants are known to be CONST_INT, and
2053 therefore imply integer comparisons. */
2054 if (c == constm1_rtx && GET_CODE (cond) == GT)
2056 else if (c == const1_rtx && GET_CODE (cond) == LT)
2058 else if (c != CONST0_RTX (GET_MODE (b)))
2059 return FALSE;
2061 /* Determine what sort of operation this is. */
2062 switch (GET_CODE (cond))
2064 case LT:
2065 case LE:
2066 case UNLT:
2067 case UNLE:
2068 negate = !negate;
2069 break;
2070 case GT:
2071 case GE:
2072 case UNGT:
2073 case UNGE:
2074 break;
2075 default:
2076 return FALSE;
2079 start_sequence ();
2080 if (one_cmpl)
2081 target = expand_one_cmpl_abs_nojump (GET_MODE (if_info->x), b,
2082 if_info->x);
2083 else
2084 target = expand_abs_nojump (GET_MODE (if_info->x), b, if_info->x, 1);
2086 /* ??? It's a quandary whether cmove would be better here, especially
2087 for integers. Perhaps combine will clean things up. */
2088 if (target && negate)
2090 if (one_cmpl)
2091 target = expand_simple_unop (GET_MODE (target), NOT, target,
2092 if_info->x, 0);
2093 else
2094 target = expand_simple_unop (GET_MODE (target), NEG, target,
2095 if_info->x, 0);
2098 if (! target)
2100 end_sequence ();
2101 return FALSE;
2104 if (target != if_info->x)
2105 noce_emit_move_insn (if_info->x, target);
2107 seq = end_ifcvt_sequence (if_info);
2108 if (!seq)
2109 return FALSE;
2111 emit_insn_before_setloc (seq, if_info->jump, INSN_LOCATION (if_info->insn_a));
2112 if_info->cond = cond;
2113 if_info->cond_earliest = earliest;
2115 return TRUE;
2118 /* Convert "if (m < 0) x = b; else x = 0;" to "x = (m >> C) & b;". */
2120 static int
2121 noce_try_sign_mask (struct noce_if_info *if_info)
2123 rtx cond, t, m, c, seq;
2124 enum machine_mode mode;
2125 enum rtx_code code;
2126 bool t_unconditional;
2128 cond = if_info->cond;
2129 code = GET_CODE (cond);
2130 m = XEXP (cond, 0);
2131 c = XEXP (cond, 1);
2133 t = NULL_RTX;
2134 if (if_info->a == const0_rtx)
2136 if ((code == LT && c == const0_rtx)
2137 || (code == LE && c == constm1_rtx))
2138 t = if_info->b;
2140 else if (if_info->b == const0_rtx)
2142 if ((code == GE && c == const0_rtx)
2143 || (code == GT && c == constm1_rtx))
2144 t = if_info->a;
2147 if (! t || side_effects_p (t))
2148 return FALSE;
2150 /* We currently don't handle different modes. */
2151 mode = GET_MODE (t);
2152 if (GET_MODE (m) != mode)
2153 return FALSE;
2155 /* This is only profitable if T is unconditionally executed/evaluated in the
2156 original insn sequence or T is cheap. The former happens if B is the
2157 non-zero (T) value and if INSN_B was taken from TEST_BB, or there was no
2158 INSN_B which can happen for e.g. conditional stores to memory. For the
2159 cost computation use the block TEST_BB where the evaluation will end up
2160 after the transformation. */
2161 t_unconditional =
2162 (t == if_info->b
2163 && (if_info->insn_b == NULL_RTX
2164 || BLOCK_FOR_INSN (if_info->insn_b) == if_info->test_bb));
2165 if (!(t_unconditional
2166 || (set_src_cost (t, optimize_bb_for_speed_p (if_info->test_bb))
2167 < COSTS_N_INSNS (2))))
2168 return FALSE;
2170 start_sequence ();
2171 /* Use emit_store_flag to generate "m < 0 ? -1 : 0" instead of expanding
2172 "(signed) m >> 31" directly. This benefits targets with specialized
2173 insns to obtain the signmask, but still uses ashr_optab otherwise. */
2174 m = emit_store_flag (gen_reg_rtx (mode), LT, m, const0_rtx, mode, 0, -1);
2175 t = m ? expand_binop (mode, and_optab, m, t, NULL_RTX, 0, OPTAB_DIRECT)
2176 : NULL_RTX;
2178 if (!t)
2180 end_sequence ();
2181 return FALSE;
2184 noce_emit_move_insn (if_info->x, t);
2186 seq = end_ifcvt_sequence (if_info);
2187 if (!seq)
2188 return FALSE;
2190 emit_insn_before_setloc (seq, if_info->jump, INSN_LOCATION (if_info->insn_a));
2191 return TRUE;
2195 /* Optimize away "if (x & C) x |= C" and similar bit manipulation
2196 transformations. */
2198 static int
2199 noce_try_bitop (struct noce_if_info *if_info)
2201 rtx cond, x, a, result, seq;
2202 enum machine_mode mode;
2203 enum rtx_code code;
2204 int bitnum;
2206 x = if_info->x;
2207 cond = if_info->cond;
2208 code = GET_CODE (cond);
2210 /* Check for no else condition. */
2211 if (! rtx_equal_p (x, if_info->b))
2212 return FALSE;
2214 /* Check for a suitable condition. */
2215 if (code != NE && code != EQ)
2216 return FALSE;
2217 if (XEXP (cond, 1) != const0_rtx)
2218 return FALSE;
2219 cond = XEXP (cond, 0);
2221 /* ??? We could also handle AND here. */
2222 if (GET_CODE (cond) == ZERO_EXTRACT)
2224 if (XEXP (cond, 1) != const1_rtx
2225 || !CONST_INT_P (XEXP (cond, 2))
2226 || ! rtx_equal_p (x, XEXP (cond, 0)))
2227 return FALSE;
2228 bitnum = INTVAL (XEXP (cond, 2));
2229 mode = GET_MODE (x);
2230 if (BITS_BIG_ENDIAN)
2231 bitnum = GET_MODE_BITSIZE (mode) - 1 - bitnum;
2232 if (bitnum < 0 || bitnum >= HOST_BITS_PER_WIDE_INT)
2233 return FALSE;
2235 else
2236 return FALSE;
2238 a = if_info->a;
2239 if (GET_CODE (a) == IOR || GET_CODE (a) == XOR)
2241 /* Check for "if (X & C) x = x op C". */
2242 if (! rtx_equal_p (x, XEXP (a, 0))
2243 || !CONST_INT_P (XEXP (a, 1))
2244 || (INTVAL (XEXP (a, 1)) & GET_MODE_MASK (mode))
2245 != (unsigned HOST_WIDE_INT) 1 << bitnum)
2246 return FALSE;
2248 /* if ((x & C) == 0) x |= C; is transformed to x |= C. */
2249 /* if ((x & C) != 0) x |= C; is transformed to nothing. */
2250 if (GET_CODE (a) == IOR)
2251 result = (code == NE) ? a : NULL_RTX;
2252 else if (code == NE)
2254 /* if ((x & C) == 0) x ^= C; is transformed to x |= C. */
2255 result = gen_int_mode ((HOST_WIDE_INT) 1 << bitnum, mode);
2256 result = simplify_gen_binary (IOR, mode, x, result);
2258 else
2260 /* if ((x & C) != 0) x ^= C; is transformed to x &= ~C. */
2261 result = gen_int_mode (~((HOST_WIDE_INT) 1 << bitnum), mode);
2262 result = simplify_gen_binary (AND, mode, x, result);
2265 else if (GET_CODE (a) == AND)
2267 /* Check for "if (X & C) x &= ~C". */
2268 if (! rtx_equal_p (x, XEXP (a, 0))
2269 || !CONST_INT_P (XEXP (a, 1))
2270 || (INTVAL (XEXP (a, 1)) & GET_MODE_MASK (mode))
2271 != (~((HOST_WIDE_INT) 1 << bitnum) & GET_MODE_MASK (mode)))
2272 return FALSE;
2274 /* if ((x & C) == 0) x &= ~C; is transformed to nothing. */
2275 /* if ((x & C) != 0) x &= ~C; is transformed to x &= ~C. */
2276 result = (code == EQ) ? a : NULL_RTX;
2278 else
2279 return FALSE;
2281 if (result)
2283 start_sequence ();
2284 noce_emit_move_insn (x, result);
2285 seq = end_ifcvt_sequence (if_info);
2286 if (!seq)
2287 return FALSE;
2289 emit_insn_before_setloc (seq, if_info->jump,
2290 INSN_LOCATION (if_info->insn_a));
2292 return TRUE;
2296 /* Similar to get_condition, only the resulting condition must be
2297 valid at JUMP, instead of at EARLIEST.
2299 If THEN_ELSE_REVERSED is true, the fallthrough does not go to the
2300 THEN block of the caller, and we have to reverse the condition. */
2302 static rtx
2303 noce_get_condition (rtx jump, rtx *earliest, bool then_else_reversed)
2305 rtx cond, set, tmp;
2306 bool reverse;
2308 if (! any_condjump_p (jump))
2309 return NULL_RTX;
2311 set = pc_set (jump);
2313 /* If this branches to JUMP_LABEL when the condition is false,
2314 reverse the condition. */
2315 reverse = (GET_CODE (XEXP (SET_SRC (set), 2)) == LABEL_REF
2316 && XEXP (XEXP (SET_SRC (set), 2), 0) == JUMP_LABEL (jump));
2318 /* We may have to reverse because the caller's if block is not canonical,
2319 i.e. the THEN block isn't the fallthrough block for the TEST block
2320 (see find_if_header). */
2321 if (then_else_reversed)
2322 reverse = !reverse;
2324 /* If the condition variable is a register and is MODE_INT, accept it. */
2326 cond = XEXP (SET_SRC (set), 0);
2327 tmp = XEXP (cond, 0);
2328 if (REG_P (tmp) && GET_MODE_CLASS (GET_MODE (tmp)) == MODE_INT
2329 && (GET_MODE (tmp) != BImode
2330 || !targetm.small_register_classes_for_mode_p (BImode)))
2332 *earliest = jump;
2334 if (reverse)
2335 cond = gen_rtx_fmt_ee (reverse_condition (GET_CODE (cond)),
2336 GET_MODE (cond), tmp, XEXP (cond, 1));
2337 return cond;
2340 /* Otherwise, fall back on canonicalize_condition to do the dirty
2341 work of manipulating MODE_CC values and COMPARE rtx codes. */
2342 tmp = canonicalize_condition (jump, cond, reverse, earliest,
2343 NULL_RTX, false, true);
2345 /* We don't handle side-effects in the condition, like handling
2346 REG_INC notes and making sure no duplicate conditions are emitted. */
2347 if (tmp != NULL_RTX && side_effects_p (tmp))
2348 return NULL_RTX;
2350 return tmp;
2353 /* Return true if OP is ok for if-then-else processing. */
2355 static int
2356 noce_operand_ok (const_rtx op)
2358 if (side_effects_p (op))
2359 return FALSE;
2361 /* We special-case memories, so handle any of them with
2362 no address side effects. */
2363 if (MEM_P (op))
2364 return ! side_effects_p (XEXP (op, 0));
2366 return ! may_trap_p (op);
2369 /* Return true if a write into MEM may trap or fault. */
2371 static bool
2372 noce_mem_write_may_trap_or_fault_p (const_rtx mem)
2374 rtx addr;
2376 if (MEM_READONLY_P (mem))
2377 return true;
2379 if (may_trap_or_fault_p (mem))
2380 return true;
2382 addr = XEXP (mem, 0);
2384 /* Call target hook to avoid the effects of -fpic etc.... */
2385 addr = targetm.delegitimize_address (addr);
2387 while (addr)
2388 switch (GET_CODE (addr))
2390 case CONST:
2391 case PRE_DEC:
2392 case PRE_INC:
2393 case POST_DEC:
2394 case POST_INC:
2395 case POST_MODIFY:
2396 addr = XEXP (addr, 0);
2397 break;
2398 case LO_SUM:
2399 case PRE_MODIFY:
2400 addr = XEXP (addr, 1);
2401 break;
2402 case PLUS:
2403 if (CONST_INT_P (XEXP (addr, 1)))
2404 addr = XEXP (addr, 0);
2405 else
2406 return false;
2407 break;
2408 case LABEL_REF:
2409 return true;
2410 case SYMBOL_REF:
2411 if (SYMBOL_REF_DECL (addr)
2412 && decl_readonly_section (SYMBOL_REF_DECL (addr), 0))
2413 return true;
2414 return false;
2415 default:
2416 return false;
2419 return false;
2422 /* Return whether we can use store speculation for MEM. TOP_BB is the
2423 basic block above the conditional block where we are considering
2424 doing the speculative store. We look for whether MEM is set
2425 unconditionally later in the function. */
2427 static bool
2428 noce_can_store_speculate_p (basic_block top_bb, const_rtx mem)
2430 basic_block dominator;
2432 for (dominator = get_immediate_dominator (CDI_POST_DOMINATORS, top_bb);
2433 dominator != NULL;
2434 dominator = get_immediate_dominator (CDI_POST_DOMINATORS, dominator))
2436 rtx insn;
2438 FOR_BB_INSNS (dominator, insn)
2440 /* If we see something that might be a memory barrier, we
2441 have to stop looking. Even if the MEM is set later in
2442 the function, we still don't want to set it
2443 unconditionally before the barrier. */
2444 if (INSN_P (insn)
2445 && (volatile_insn_p (PATTERN (insn))
2446 || (CALL_P (insn) && (!RTL_CONST_CALL_P (insn)))))
2447 return false;
2449 if (memory_must_be_modified_in_insn_p (mem, insn))
2450 return true;
2451 if (modified_in_p (XEXP (mem, 0), insn))
2452 return false;
2457 return false;
2460 /* Given a simple IF-THEN-JOIN or IF-THEN-ELSE-JOIN block, attempt to convert
2461 it without using conditional execution. Return TRUE if we were successful
2462 at converting the block. */
2464 static int
2465 noce_process_if_block (struct noce_if_info *if_info)
2467 basic_block test_bb = if_info->test_bb; /* test block */
2468 basic_block then_bb = if_info->then_bb; /* THEN */
2469 basic_block else_bb = if_info->else_bb; /* ELSE or NULL */
2470 basic_block join_bb = if_info->join_bb; /* JOIN */
2471 rtx jump = if_info->jump;
2472 rtx cond = if_info->cond;
2473 rtx insn_a, insn_b;
2474 rtx set_a, set_b;
2475 rtx orig_x, x, a, b;
2477 /* We're looking for patterns of the form
2479 (1) if (...) x = a; else x = b;
2480 (2) x = b; if (...) x = a;
2481 (3) if (...) x = a; // as if with an initial x = x.
2483 The later patterns require jumps to be more expensive.
2485 ??? For future expansion, look for multiple X in such patterns. */
2487 /* Look for one of the potential sets. */
2488 insn_a = first_active_insn (then_bb);
2489 if (! insn_a
2490 || insn_a != last_active_insn (then_bb, FALSE)
2491 || (set_a = single_set (insn_a)) == NULL_RTX)
2492 return FALSE;
2494 x = SET_DEST (set_a);
2495 a = SET_SRC (set_a);
2497 /* Look for the other potential set. Make sure we've got equivalent
2498 destinations. */
2499 /* ??? This is overconservative. Storing to two different mems is
2500 as easy as conditionally computing the address. Storing to a
2501 single mem merely requires a scratch memory to use as one of the
2502 destination addresses; often the memory immediately below the
2503 stack pointer is available for this. */
2504 set_b = NULL_RTX;
2505 if (else_bb)
2507 insn_b = first_active_insn (else_bb);
2508 if (! insn_b
2509 || insn_b != last_active_insn (else_bb, FALSE)
2510 || (set_b = single_set (insn_b)) == NULL_RTX
2511 || ! rtx_interchangeable_p (x, SET_DEST (set_b)))
2512 return FALSE;
2514 else
2516 insn_b = prev_nonnote_nondebug_insn (if_info->cond_earliest);
2517 /* We're going to be moving the evaluation of B down from above
2518 COND_EARLIEST to JUMP. Make sure the relevant data is still
2519 intact. */
2520 if (! insn_b
2521 || BLOCK_FOR_INSN (insn_b) != BLOCK_FOR_INSN (if_info->cond_earliest)
2522 || !NONJUMP_INSN_P (insn_b)
2523 || (set_b = single_set (insn_b)) == NULL_RTX
2524 || ! rtx_interchangeable_p (x, SET_DEST (set_b))
2525 || ! noce_operand_ok (SET_SRC (set_b))
2526 || reg_overlap_mentioned_p (x, SET_SRC (set_b))
2527 || modified_between_p (SET_SRC (set_b), insn_b, jump)
2528 /* Avoid extending the lifetime of hard registers on small
2529 register class machines. */
2530 || (REG_P (SET_SRC (set_b))
2531 && HARD_REGISTER_P (SET_SRC (set_b))
2532 && targetm.small_register_classes_for_mode_p
2533 (GET_MODE (SET_SRC (set_b))))
2534 /* Likewise with X. In particular this can happen when
2535 noce_get_condition looks farther back in the instruction
2536 stream than one might expect. */
2537 || reg_overlap_mentioned_p (x, cond)
2538 || reg_overlap_mentioned_p (x, a)
2539 || modified_between_p (x, insn_b, jump))
2540 insn_b = set_b = NULL_RTX;
2543 /* If x has side effects then only the if-then-else form is safe to
2544 convert. But even in that case we would need to restore any notes
2545 (such as REG_INC) at then end. That can be tricky if
2546 noce_emit_move_insn expands to more than one insn, so disable the
2547 optimization entirely for now if there are side effects. */
2548 if (side_effects_p (x))
2549 return FALSE;
2551 b = (set_b ? SET_SRC (set_b) : x);
2553 /* Only operate on register destinations, and even then avoid extending
2554 the lifetime of hard registers on small register class machines. */
2555 orig_x = x;
2556 if (!REG_P (x)
2557 || (HARD_REGISTER_P (x)
2558 && targetm.small_register_classes_for_mode_p (GET_MODE (x))))
2560 if (GET_MODE (x) == BLKmode)
2561 return FALSE;
2563 if (GET_CODE (x) == ZERO_EXTRACT
2564 && (!CONST_INT_P (XEXP (x, 1))
2565 || !CONST_INT_P (XEXP (x, 2))))
2566 return FALSE;
2568 x = gen_reg_rtx (GET_MODE (GET_CODE (x) == STRICT_LOW_PART
2569 ? XEXP (x, 0) : x));
2572 /* Don't operate on sources that may trap or are volatile. */
2573 if (! noce_operand_ok (a) || ! noce_operand_ok (b))
2574 return FALSE;
2576 retry:
2577 /* Set up the info block for our subroutines. */
2578 if_info->insn_a = insn_a;
2579 if_info->insn_b = insn_b;
2580 if_info->x = x;
2581 if_info->a = a;
2582 if_info->b = b;
2584 /* Try optimizations in some approximation of a useful order. */
2585 /* ??? Should first look to see if X is live incoming at all. If it
2586 isn't, we don't need anything but an unconditional set. */
2588 /* Look and see if A and B are really the same. Avoid creating silly
2589 cmove constructs that no one will fix up later. */
2590 if (rtx_interchangeable_p (a, b))
2592 /* If we have an INSN_B, we don't have to create any new rtl. Just
2593 move the instruction that we already have. If we don't have an
2594 INSN_B, that means that A == X, and we've got a noop move. In
2595 that case don't do anything and let the code below delete INSN_A. */
2596 if (insn_b && else_bb)
2598 rtx note;
2600 if (else_bb && insn_b == BB_END (else_bb))
2601 BB_END (else_bb) = PREV_INSN (insn_b);
2602 reorder_insns (insn_b, insn_b, PREV_INSN (jump));
2604 /* If there was a REG_EQUAL note, delete it since it may have been
2605 true due to this insn being after a jump. */
2606 if ((note = find_reg_note (insn_b, REG_EQUAL, NULL_RTX)) != 0)
2607 remove_note (insn_b, note);
2609 insn_b = NULL_RTX;
2611 /* If we have "x = b; if (...) x = a;", and x has side-effects, then
2612 x must be executed twice. */
2613 else if (insn_b && side_effects_p (orig_x))
2614 return FALSE;
2616 x = orig_x;
2617 goto success;
2620 if (!set_b && MEM_P (orig_x))
2622 /* Disallow the "if (...) x = a;" form (implicit "else x = x;")
2623 for optimizations if writing to x may trap or fault,
2624 i.e. it's a memory other than a static var or a stack slot,
2625 is misaligned on strict aligned machines or is read-only. If
2626 x is a read-only memory, then the program is valid only if we
2627 avoid the store into it. If there are stores on both the
2628 THEN and ELSE arms, then we can go ahead with the conversion;
2629 either the program is broken, or the condition is always
2630 false such that the other memory is selected. */
2631 if (noce_mem_write_may_trap_or_fault_p (orig_x))
2632 return FALSE;
2634 /* Avoid store speculation: given "if (...) x = a" where x is a
2635 MEM, we only want to do the store if x is always set
2636 somewhere in the function. This avoids cases like
2637 if (pthread_mutex_trylock(mutex))
2638 ++global_variable;
2639 where we only want global_variable to be changed if the mutex
2640 is held. FIXME: This should ideally be expressed directly in
2641 RTL somehow. */
2642 if (!noce_can_store_speculate_p (test_bb, orig_x))
2643 return FALSE;
2646 if (noce_try_move (if_info))
2647 goto success;
2648 if (noce_try_store_flag (if_info))
2649 goto success;
2650 if (noce_try_bitop (if_info))
2651 goto success;
2652 if (noce_try_minmax (if_info))
2653 goto success;
2654 if (noce_try_abs (if_info))
2655 goto success;
2656 if (HAVE_conditional_move
2657 && noce_try_cmove (if_info))
2658 goto success;
2659 if (! targetm.have_conditional_execution ())
2661 if (noce_try_store_flag_constants (if_info))
2662 goto success;
2663 if (noce_try_addcc (if_info))
2664 goto success;
2665 if (noce_try_store_flag_mask (if_info))
2666 goto success;
2667 if (HAVE_conditional_move
2668 && noce_try_cmove_arith (if_info))
2669 goto success;
2670 if (noce_try_sign_mask (if_info))
2671 goto success;
2674 if (!else_bb && set_b)
2676 insn_b = set_b = NULL_RTX;
2677 b = orig_x;
2678 goto retry;
2681 return FALSE;
2683 success:
2685 /* If we used a temporary, fix it up now. */
2686 if (orig_x != x)
2688 rtx seq;
2690 start_sequence ();
2691 noce_emit_move_insn (orig_x, x);
2692 seq = get_insns ();
2693 set_used_flags (orig_x);
2694 unshare_all_rtl_in_chain (seq);
2695 end_sequence ();
2697 emit_insn_before_setloc (seq, BB_END (test_bb), INSN_LOCATION (insn_a));
2700 /* The original THEN and ELSE blocks may now be removed. The test block
2701 must now jump to the join block. If the test block and the join block
2702 can be merged, do so. */
2703 if (else_bb)
2705 delete_basic_block (else_bb);
2706 num_true_changes++;
2708 else
2709 remove_edge (find_edge (test_bb, join_bb));
2711 remove_edge (find_edge (then_bb, join_bb));
2712 redirect_edge_and_branch_force (single_succ_edge (test_bb), join_bb);
2713 delete_basic_block (then_bb);
2714 num_true_changes++;
2716 if (can_merge_blocks_p (test_bb, join_bb))
2718 merge_blocks (test_bb, join_bb);
2719 num_true_changes++;
2722 num_updated_if_blocks++;
2723 return TRUE;
2726 /* Check whether a block is suitable for conditional move conversion.
2727 Every insn must be a simple set of a register to a constant or a
2728 register. For each assignment, store the value in the pointer map
2729 VALS, keyed indexed by register pointer, then store the register
2730 pointer in REGS. COND is the condition we will test. */
2732 static int
2733 check_cond_move_block (basic_block bb,
2734 struct pointer_map_t *vals,
2735 vec<rtx> *regs,
2736 rtx cond)
2738 rtx insn;
2740 /* We can only handle simple jumps at the end of the basic block.
2741 It is almost impossible to update the CFG otherwise. */
2742 insn = BB_END (bb);
2743 if (JUMP_P (insn) && !onlyjump_p (insn))
2744 return FALSE;
2746 FOR_BB_INSNS (bb, insn)
2748 rtx set, dest, src;
2749 void **slot;
2751 if (!NONDEBUG_INSN_P (insn) || JUMP_P (insn))
2752 continue;
2753 set = single_set (insn);
2754 if (!set)
2755 return FALSE;
2757 dest = SET_DEST (set);
2758 src = SET_SRC (set);
2759 if (!REG_P (dest)
2760 || (HARD_REGISTER_P (dest)
2761 && targetm.small_register_classes_for_mode_p (GET_MODE (dest))))
2762 return FALSE;
2764 if (!CONSTANT_P (src) && !register_operand (src, VOIDmode))
2765 return FALSE;
2767 if (side_effects_p (src) || side_effects_p (dest))
2768 return FALSE;
2770 if (may_trap_p (src) || may_trap_p (dest))
2771 return FALSE;
2773 /* Don't try to handle this if the source register was
2774 modified earlier in the block. */
2775 if ((REG_P (src)
2776 && pointer_map_contains (vals, src))
2777 || (GET_CODE (src) == SUBREG && REG_P (SUBREG_REG (src))
2778 && pointer_map_contains (vals, SUBREG_REG (src))))
2779 return FALSE;
2781 /* Don't try to handle this if the destination register was
2782 modified earlier in the block. */
2783 if (pointer_map_contains (vals, dest))
2784 return FALSE;
2786 /* Don't try to handle this if the condition uses the
2787 destination register. */
2788 if (reg_overlap_mentioned_p (dest, cond))
2789 return FALSE;
2791 /* Don't try to handle this if the source register is modified
2792 later in the block. */
2793 if (!CONSTANT_P (src)
2794 && modified_between_p (src, insn, NEXT_INSN (BB_END (bb))))
2795 return FALSE;
2797 slot = pointer_map_insert (vals, (void *) dest);
2798 *slot = (void *) src;
2800 regs->safe_push (dest);
2803 return TRUE;
2806 /* Given a basic block BB suitable for conditional move conversion,
2807 a condition COND, and pointer maps THEN_VALS and ELSE_VALS containing
2808 the register values depending on COND, emit the insns in the block as
2809 conditional moves. If ELSE_BLOCK is true, THEN_BB was already
2810 processed. The caller has started a sequence for the conversion.
2811 Return true if successful, false if something goes wrong. */
2813 static bool
2814 cond_move_convert_if_block (struct noce_if_info *if_infop,
2815 basic_block bb, rtx cond,
2816 struct pointer_map_t *then_vals,
2817 struct pointer_map_t *else_vals,
2818 bool else_block_p)
2820 enum rtx_code code;
2821 rtx insn, cond_arg0, cond_arg1;
2823 code = GET_CODE (cond);
2824 cond_arg0 = XEXP (cond, 0);
2825 cond_arg1 = XEXP (cond, 1);
2827 FOR_BB_INSNS (bb, insn)
2829 rtx set, target, dest, t, e;
2830 void **then_slot, **else_slot;
2832 /* ??? Maybe emit conditional debug insn? */
2833 if (!NONDEBUG_INSN_P (insn) || JUMP_P (insn))
2834 continue;
2835 set = single_set (insn);
2836 gcc_assert (set && REG_P (SET_DEST (set)));
2838 dest = SET_DEST (set);
2840 then_slot = pointer_map_contains (then_vals, dest);
2841 else_slot = pointer_map_contains (else_vals, dest);
2842 t = then_slot ? (rtx) *then_slot : NULL_RTX;
2843 e = else_slot ? (rtx) *else_slot : NULL_RTX;
2845 if (else_block_p)
2847 /* If this register was set in the then block, we already
2848 handled this case there. */
2849 if (t)
2850 continue;
2851 t = dest;
2852 gcc_assert (e);
2854 else
2856 gcc_assert (t);
2857 if (!e)
2858 e = dest;
2861 target = noce_emit_cmove (if_infop, dest, code, cond_arg0, cond_arg1,
2862 t, e);
2863 if (!target)
2864 return false;
2866 if (target != dest)
2867 noce_emit_move_insn (dest, target);
2870 return true;
2873 /* Given a simple IF-THEN-JOIN or IF-THEN-ELSE-JOIN block, attempt to convert
2874 it using only conditional moves. Return TRUE if we were successful at
2875 converting the block. */
2877 static int
2878 cond_move_process_if_block (struct noce_if_info *if_info)
2880 basic_block test_bb = if_info->test_bb;
2881 basic_block then_bb = if_info->then_bb;
2882 basic_block else_bb = if_info->else_bb;
2883 basic_block join_bb = if_info->join_bb;
2884 rtx jump = if_info->jump;
2885 rtx cond = if_info->cond;
2886 rtx seq, loc_insn;
2887 rtx reg;
2888 int c;
2889 struct pointer_map_t *then_vals;
2890 struct pointer_map_t *else_vals;
2891 vec<rtx> then_regs = vNULL;
2892 vec<rtx> else_regs = vNULL;
2893 unsigned int i;
2894 int success_p = FALSE;
2896 /* Build a mapping for each block to the value used for each
2897 register. */
2898 then_vals = pointer_map_create ();
2899 else_vals = pointer_map_create ();
2901 /* Make sure the blocks are suitable. */
2902 if (!check_cond_move_block (then_bb, then_vals, &then_regs, cond)
2903 || (else_bb
2904 && !check_cond_move_block (else_bb, else_vals, &else_regs, cond)))
2905 goto done;
2907 /* Make sure the blocks can be used together. If the same register
2908 is set in both blocks, and is not set to a constant in both
2909 cases, then both blocks must set it to the same register. We
2910 have already verified that if it is set to a register, that the
2911 source register does not change after the assignment. Also count
2912 the number of registers set in only one of the blocks. */
2913 c = 0;
2914 FOR_EACH_VEC_ELT (then_regs, i, reg)
2916 void **then_slot = pointer_map_contains (then_vals, reg);
2917 void **else_slot = pointer_map_contains (else_vals, reg);
2919 gcc_checking_assert (then_slot);
2920 if (!else_slot)
2921 ++c;
2922 else
2924 rtx then_val = (rtx) *then_slot;
2925 rtx else_val = (rtx) *else_slot;
2926 if (!CONSTANT_P (then_val) && !CONSTANT_P (else_val)
2927 && !rtx_equal_p (then_val, else_val))
2928 goto done;
2932 /* Finish off c for MAX_CONDITIONAL_EXECUTE. */
2933 FOR_EACH_VEC_ELT (else_regs, i, reg)
2935 gcc_checking_assert (pointer_map_contains (else_vals, reg));
2936 if (!pointer_map_contains (then_vals, reg))
2937 ++c;
2940 /* Make sure it is reasonable to convert this block. What matters
2941 is the number of assignments currently made in only one of the
2942 branches, since if we convert we are going to always execute
2943 them. */
2944 if (c > MAX_CONDITIONAL_EXECUTE)
2945 goto done;
2947 /* Try to emit the conditional moves. First do the then block,
2948 then do anything left in the else blocks. */
2949 start_sequence ();
2950 if (!cond_move_convert_if_block (if_info, then_bb, cond,
2951 then_vals, else_vals, false)
2952 || (else_bb
2953 && !cond_move_convert_if_block (if_info, else_bb, cond,
2954 then_vals, else_vals, true)))
2956 end_sequence ();
2957 goto done;
2959 seq = end_ifcvt_sequence (if_info);
2960 if (!seq)
2961 goto done;
2963 loc_insn = first_active_insn (then_bb);
2964 if (!loc_insn)
2966 loc_insn = first_active_insn (else_bb);
2967 gcc_assert (loc_insn);
2969 emit_insn_before_setloc (seq, jump, INSN_LOCATION (loc_insn));
2971 if (else_bb)
2973 delete_basic_block (else_bb);
2974 num_true_changes++;
2976 else
2977 remove_edge (find_edge (test_bb, join_bb));
2979 remove_edge (find_edge (then_bb, join_bb));
2980 redirect_edge_and_branch_force (single_succ_edge (test_bb), join_bb);
2981 delete_basic_block (then_bb);
2982 num_true_changes++;
2984 if (can_merge_blocks_p (test_bb, join_bb))
2986 merge_blocks (test_bb, join_bb);
2987 num_true_changes++;
2990 num_updated_if_blocks++;
2992 success_p = TRUE;
2994 done:
2995 pointer_map_destroy (then_vals);
2996 pointer_map_destroy (else_vals);
2997 then_regs.release ();
2998 else_regs.release ();
2999 return success_p;
3003 /* Determine if a given basic block heads a simple IF-THEN-JOIN or an
3004 IF-THEN-ELSE-JOIN block.
3006 If so, we'll try to convert the insns to not require the branch,
3007 using only transformations that do not require conditional execution.
3009 Return TRUE if we were successful at converting the block. */
3011 static int
3012 noce_find_if_block (basic_block test_bb, edge then_edge, edge else_edge,
3013 int pass)
3015 basic_block then_bb, else_bb, join_bb;
3016 bool then_else_reversed = false;
3017 rtx jump, cond;
3018 rtx cond_earliest;
3019 struct noce_if_info if_info;
3021 /* We only ever should get here before reload. */
3022 gcc_assert (!reload_completed);
3024 /* Recognize an IF-THEN-ELSE-JOIN block. */
3025 if (single_pred_p (then_edge->dest)
3026 && single_succ_p (then_edge->dest)
3027 && single_pred_p (else_edge->dest)
3028 && single_succ_p (else_edge->dest)
3029 && single_succ (then_edge->dest) == single_succ (else_edge->dest))
3031 then_bb = then_edge->dest;
3032 else_bb = else_edge->dest;
3033 join_bb = single_succ (then_bb);
3035 /* Recognize an IF-THEN-JOIN block. */
3036 else if (single_pred_p (then_edge->dest)
3037 && single_succ_p (then_edge->dest)
3038 && single_succ (then_edge->dest) == else_edge->dest)
3040 then_bb = then_edge->dest;
3041 else_bb = NULL_BLOCK;
3042 join_bb = else_edge->dest;
3044 /* Recognize an IF-ELSE-JOIN block. We can have those because the order
3045 of basic blocks in cfglayout mode does not matter, so the fallthrough
3046 edge can go to any basic block (and not just to bb->next_bb, like in
3047 cfgrtl mode). */
3048 else if (single_pred_p (else_edge->dest)
3049 && single_succ_p (else_edge->dest)
3050 && single_succ (else_edge->dest) == then_edge->dest)
3052 /* The noce transformations do not apply to IF-ELSE-JOIN blocks.
3053 To make this work, we have to invert the THEN and ELSE blocks
3054 and reverse the jump condition. */
3055 then_bb = else_edge->dest;
3056 else_bb = NULL_BLOCK;
3057 join_bb = single_succ (then_bb);
3058 then_else_reversed = true;
3060 else
3061 /* Not a form we can handle. */
3062 return FALSE;
3064 /* The edges of the THEN and ELSE blocks cannot have complex edges. */
3065 if (single_succ_edge (then_bb)->flags & EDGE_COMPLEX)
3066 return FALSE;
3067 if (else_bb
3068 && single_succ_edge (else_bb)->flags & EDGE_COMPLEX)
3069 return FALSE;
3071 num_possible_if_blocks++;
3073 if (dump_file)
3075 fprintf (dump_file,
3076 "\nIF-THEN%s-JOIN block found, pass %d, test %d, then %d",
3077 (else_bb) ? "-ELSE" : "",
3078 pass, test_bb->index, then_bb->index);
3080 if (else_bb)
3081 fprintf (dump_file, ", else %d", else_bb->index);
3083 fprintf (dump_file, ", join %d\n", join_bb->index);
3086 /* If the conditional jump is more than just a conditional
3087 jump, then we can not do if-conversion on this block. */
3088 jump = BB_END (test_bb);
3089 if (! onlyjump_p (jump))
3090 return FALSE;
3092 /* If this is not a standard conditional jump, we can't parse it. */
3093 cond = noce_get_condition (jump, &cond_earliest, then_else_reversed);
3094 if (!cond)
3095 return FALSE;
3097 /* We must be comparing objects whose modes imply the size. */
3098 if (GET_MODE (XEXP (cond, 0)) == BLKmode)
3099 return FALSE;
3101 /* Initialize an IF_INFO struct to pass around. */
3102 memset (&if_info, 0, sizeof if_info);
3103 if_info.test_bb = test_bb;
3104 if_info.then_bb = then_bb;
3105 if_info.else_bb = else_bb;
3106 if_info.join_bb = join_bb;
3107 if_info.cond = cond;
3108 if_info.cond_earliest = cond_earliest;
3109 if_info.jump = jump;
3110 if_info.then_else_reversed = then_else_reversed;
3111 if_info.branch_cost = BRANCH_COST (optimize_bb_for_speed_p (test_bb),
3112 predictable_edge_p (then_edge));
3114 /* Do the real work. */
3116 if (noce_process_if_block (&if_info))
3117 return TRUE;
3119 if (HAVE_conditional_move
3120 && cond_move_process_if_block (&if_info))
3121 return TRUE;
3123 return FALSE;
3127 /* Merge the blocks and mark for local life update. */
3129 static void
3130 merge_if_block (struct ce_if_block * ce_info)
3132 basic_block test_bb = ce_info->test_bb; /* last test block */
3133 basic_block then_bb = ce_info->then_bb; /* THEN */
3134 basic_block else_bb = ce_info->else_bb; /* ELSE or NULL */
3135 basic_block join_bb = ce_info->join_bb; /* join block */
3136 basic_block combo_bb;
3138 /* All block merging is done into the lower block numbers. */
3140 combo_bb = test_bb;
3141 df_set_bb_dirty (test_bb);
3143 /* Merge any basic blocks to handle && and || subtests. Each of
3144 the blocks are on the fallthru path from the predecessor block. */
3145 if (ce_info->num_multiple_test_blocks > 0)
3147 basic_block bb = test_bb;
3148 basic_block last_test_bb = ce_info->last_test_bb;
3149 basic_block fallthru = block_fallthru (bb);
3153 bb = fallthru;
3154 fallthru = block_fallthru (bb);
3155 merge_blocks (combo_bb, bb);
3156 num_true_changes++;
3158 while (bb != last_test_bb);
3161 /* Merge TEST block into THEN block. Normally the THEN block won't have a
3162 label, but it might if there were || tests. That label's count should be
3163 zero, and it normally should be removed. */
3165 if (then_bb)
3167 merge_blocks (combo_bb, then_bb);
3168 num_true_changes++;
3171 /* The ELSE block, if it existed, had a label. That label count
3172 will almost always be zero, but odd things can happen when labels
3173 get their addresses taken. */
3174 if (else_bb)
3176 merge_blocks (combo_bb, else_bb);
3177 num_true_changes++;
3180 /* If there was no join block reported, that means it was not adjacent
3181 to the others, and so we cannot merge them. */
3183 if (! join_bb)
3185 rtx last = BB_END (combo_bb);
3187 /* The outgoing edge for the current COMBO block should already
3188 be correct. Verify this. */
3189 if (EDGE_COUNT (combo_bb->succs) == 0)
3190 gcc_assert (find_reg_note (last, REG_NORETURN, NULL)
3191 || (NONJUMP_INSN_P (last)
3192 && GET_CODE (PATTERN (last)) == TRAP_IF
3193 && (TRAP_CONDITION (PATTERN (last))
3194 == const_true_rtx)));
3196 else
3197 /* There should still be something at the end of the THEN or ELSE
3198 blocks taking us to our final destination. */
3199 gcc_assert (JUMP_P (last)
3200 || (EDGE_SUCC (combo_bb, 0)->dest == EXIT_BLOCK_PTR
3201 && CALL_P (last)
3202 && SIBLING_CALL_P (last))
3203 || ((EDGE_SUCC (combo_bb, 0)->flags & EDGE_EH)
3204 && can_throw_internal (last)));
3207 /* The JOIN block may have had quite a number of other predecessors too.
3208 Since we've already merged the TEST, THEN and ELSE blocks, we should
3209 have only one remaining edge from our if-then-else diamond. If there
3210 is more than one remaining edge, it must come from elsewhere. There
3211 may be zero incoming edges if the THEN block didn't actually join
3212 back up (as with a call to a non-return function). */
3213 else if (EDGE_COUNT (join_bb->preds) < 2
3214 && join_bb != EXIT_BLOCK_PTR)
3216 /* We can merge the JOIN cleanly and update the dataflow try
3217 again on this pass.*/
3218 merge_blocks (combo_bb, join_bb);
3219 num_true_changes++;
3221 else
3223 /* We cannot merge the JOIN. */
3225 /* The outgoing edge for the current COMBO block should already
3226 be correct. Verify this. */
3227 gcc_assert (single_succ_p (combo_bb)
3228 && single_succ (combo_bb) == join_bb);
3230 /* Remove the jump and cruft from the end of the COMBO block. */
3231 if (join_bb != EXIT_BLOCK_PTR)
3232 tidy_fallthru_edge (single_succ_edge (combo_bb));
3235 num_updated_if_blocks++;
3238 /* Find a block ending in a simple IF condition and try to transform it
3239 in some way. When converting a multi-block condition, put the new code
3240 in the first such block and delete the rest. Return a pointer to this
3241 first block if some transformation was done. Return NULL otherwise. */
3243 static basic_block
3244 find_if_header (basic_block test_bb, int pass)
3246 ce_if_block_t ce_info;
3247 edge then_edge;
3248 edge else_edge;
3250 /* The kind of block we're looking for has exactly two successors. */
3251 if (EDGE_COUNT (test_bb->succs) != 2)
3252 return NULL;
3254 then_edge = EDGE_SUCC (test_bb, 0);
3255 else_edge = EDGE_SUCC (test_bb, 1);
3257 if (df_get_bb_dirty (then_edge->dest))
3258 return NULL;
3259 if (df_get_bb_dirty (else_edge->dest))
3260 return NULL;
3262 /* Neither edge should be abnormal. */
3263 if ((then_edge->flags & EDGE_COMPLEX)
3264 || (else_edge->flags & EDGE_COMPLEX))
3265 return NULL;
3267 /* Nor exit the loop. */
3268 if ((then_edge->flags & EDGE_LOOP_EXIT)
3269 || (else_edge->flags & EDGE_LOOP_EXIT))
3270 return NULL;
3272 /* The THEN edge is canonically the one that falls through. */
3273 if (then_edge->flags & EDGE_FALLTHRU)
3275 else if (else_edge->flags & EDGE_FALLTHRU)
3277 edge e = else_edge;
3278 else_edge = then_edge;
3279 then_edge = e;
3281 else
3282 /* Otherwise this must be a multiway branch of some sort. */
3283 return NULL;
3285 memset (&ce_info, 0, sizeof (ce_info));
3286 ce_info.test_bb = test_bb;
3287 ce_info.then_bb = then_edge->dest;
3288 ce_info.else_bb = else_edge->dest;
3289 ce_info.pass = pass;
3291 #ifdef IFCVT_MACHDEP_INIT
3292 IFCVT_MACHDEP_INIT (&ce_info);
3293 #endif
3295 if (!reload_completed
3296 && noce_find_if_block (test_bb, then_edge, else_edge, pass))
3297 goto success;
3299 if (reload_completed
3300 && targetm.have_conditional_execution ()
3301 && cond_exec_find_if_block (&ce_info))
3302 goto success;
3304 if (HAVE_trap
3305 && optab_handler (ctrap_optab, word_mode) != CODE_FOR_nothing
3306 && find_cond_trap (test_bb, then_edge, else_edge))
3307 goto success;
3309 if (dom_info_state (CDI_POST_DOMINATORS) >= DOM_NO_FAST_QUERY
3310 && (reload_completed || !targetm.have_conditional_execution ()))
3312 if (find_if_case_1 (test_bb, then_edge, else_edge))
3313 goto success;
3314 if (find_if_case_2 (test_bb, then_edge, else_edge))
3315 goto success;
3318 return NULL;
3320 success:
3321 if (dump_file)
3322 fprintf (dump_file, "Conversion succeeded on pass %d.\n", pass);
3323 /* Set this so we continue looking. */
3324 cond_exec_changed_p = TRUE;
3325 return ce_info.test_bb;
3328 /* Return true if a block has two edges, one of which falls through to the next
3329 block, and the other jumps to a specific block, so that we can tell if the
3330 block is part of an && test or an || test. Returns either -1 or the number
3331 of non-note, non-jump, non-USE/CLOBBER insns in the block. */
3333 static int
3334 block_jumps_and_fallthru_p (basic_block cur_bb, basic_block target_bb)
3336 edge cur_edge;
3337 int fallthru_p = FALSE;
3338 int jump_p = FALSE;
3339 rtx insn;
3340 rtx end;
3341 int n_insns = 0;
3342 edge_iterator ei;
3344 if (!cur_bb || !target_bb)
3345 return -1;
3347 /* If no edges, obviously it doesn't jump or fallthru. */
3348 if (EDGE_COUNT (cur_bb->succs) == 0)
3349 return FALSE;
3351 FOR_EACH_EDGE (cur_edge, ei, cur_bb->succs)
3353 if (cur_edge->flags & EDGE_COMPLEX)
3354 /* Anything complex isn't what we want. */
3355 return -1;
3357 else if (cur_edge->flags & EDGE_FALLTHRU)
3358 fallthru_p = TRUE;
3360 else if (cur_edge->dest == target_bb)
3361 jump_p = TRUE;
3363 else
3364 return -1;
3367 if ((jump_p & fallthru_p) == 0)
3368 return -1;
3370 /* Don't allow calls in the block, since this is used to group && and ||
3371 together for conditional execution support. ??? we should support
3372 conditional execution support across calls for IA-64 some day, but
3373 for now it makes the code simpler. */
3374 end = BB_END (cur_bb);
3375 insn = BB_HEAD (cur_bb);
3377 while (insn != NULL_RTX)
3379 if (CALL_P (insn))
3380 return -1;
3382 if (INSN_P (insn)
3383 && !JUMP_P (insn)
3384 && !DEBUG_INSN_P (insn)
3385 && GET_CODE (PATTERN (insn)) != USE
3386 && GET_CODE (PATTERN (insn)) != CLOBBER)
3387 n_insns++;
3389 if (insn == end)
3390 break;
3392 insn = NEXT_INSN (insn);
3395 return n_insns;
3398 /* Determine if a given basic block heads a simple IF-THEN or IF-THEN-ELSE
3399 block. If so, we'll try to convert the insns to not require the branch.
3400 Return TRUE if we were successful at converting the block. */
3402 static int
3403 cond_exec_find_if_block (struct ce_if_block * ce_info)
3405 basic_block test_bb = ce_info->test_bb;
3406 basic_block then_bb = ce_info->then_bb;
3407 basic_block else_bb = ce_info->else_bb;
3408 basic_block join_bb = NULL_BLOCK;
3409 edge cur_edge;
3410 basic_block next;
3411 edge_iterator ei;
3413 ce_info->last_test_bb = test_bb;
3415 /* We only ever should get here after reload,
3416 and if we have conditional execution. */
3417 gcc_assert (reload_completed && targetm.have_conditional_execution ());
3419 /* Discover if any fall through predecessors of the current test basic block
3420 were && tests (which jump to the else block) or || tests (which jump to
3421 the then block). */
3422 if (single_pred_p (test_bb)
3423 && single_pred_edge (test_bb)->flags == EDGE_FALLTHRU)
3425 basic_block bb = single_pred (test_bb);
3426 basic_block target_bb;
3427 int max_insns = MAX_CONDITIONAL_EXECUTE;
3428 int n_insns;
3430 /* Determine if the preceding block is an && or || block. */
3431 if ((n_insns = block_jumps_and_fallthru_p (bb, else_bb)) >= 0)
3433 ce_info->and_and_p = TRUE;
3434 target_bb = else_bb;
3436 else if ((n_insns = block_jumps_and_fallthru_p (bb, then_bb)) >= 0)
3438 ce_info->and_and_p = FALSE;
3439 target_bb = then_bb;
3441 else
3442 target_bb = NULL_BLOCK;
3444 if (target_bb && n_insns <= max_insns)
3446 int total_insns = 0;
3447 int blocks = 0;
3449 ce_info->last_test_bb = test_bb;
3451 /* Found at least one && or || block, look for more. */
3454 ce_info->test_bb = test_bb = bb;
3455 total_insns += n_insns;
3456 blocks++;
3458 if (!single_pred_p (bb))
3459 break;
3461 bb = single_pred (bb);
3462 n_insns = block_jumps_and_fallthru_p (bb, target_bb);
3464 while (n_insns >= 0 && (total_insns + n_insns) <= max_insns);
3466 ce_info->num_multiple_test_blocks = blocks;
3467 ce_info->num_multiple_test_insns = total_insns;
3469 if (ce_info->and_and_p)
3470 ce_info->num_and_and_blocks = blocks;
3471 else
3472 ce_info->num_or_or_blocks = blocks;
3476 /* The THEN block of an IF-THEN combo must have exactly one predecessor,
3477 other than any || blocks which jump to the THEN block. */
3478 if ((EDGE_COUNT (then_bb->preds) - ce_info->num_or_or_blocks) != 1)
3479 return FALSE;
3481 /* The edges of the THEN and ELSE blocks cannot have complex edges. */
3482 FOR_EACH_EDGE (cur_edge, ei, then_bb->preds)
3484 if (cur_edge->flags & EDGE_COMPLEX)
3485 return FALSE;
3488 FOR_EACH_EDGE (cur_edge, ei, else_bb->preds)
3490 if (cur_edge->flags & EDGE_COMPLEX)
3491 return FALSE;
3494 /* The THEN block of an IF-THEN combo must have zero or one successors. */
3495 if (EDGE_COUNT (then_bb->succs) > 0
3496 && (!single_succ_p (then_bb)
3497 || (single_succ_edge (then_bb)->flags & EDGE_COMPLEX)
3498 || (epilogue_completed
3499 && tablejump_p (BB_END (then_bb), NULL, NULL))))
3500 return FALSE;
3502 /* If the THEN block has no successors, conditional execution can still
3503 make a conditional call. Don't do this unless the ELSE block has
3504 only one incoming edge -- the CFG manipulation is too ugly otherwise.
3505 Check for the last insn of the THEN block being an indirect jump, which
3506 is listed as not having any successors, but confuses the rest of the CE
3507 code processing. ??? we should fix this in the future. */
3508 if (EDGE_COUNT (then_bb->succs) == 0)
3510 if (single_pred_p (else_bb) && else_bb != EXIT_BLOCK_PTR)
3512 rtx last_insn = BB_END (then_bb);
3514 while (last_insn
3515 && NOTE_P (last_insn)
3516 && last_insn != BB_HEAD (then_bb))
3517 last_insn = PREV_INSN (last_insn);
3519 if (last_insn
3520 && JUMP_P (last_insn)
3521 && ! simplejump_p (last_insn))
3522 return FALSE;
3524 join_bb = else_bb;
3525 else_bb = NULL_BLOCK;
3527 else
3528 return FALSE;
3531 /* If the THEN block's successor is the other edge out of the TEST block,
3532 then we have an IF-THEN combo without an ELSE. */
3533 else if (single_succ (then_bb) == else_bb)
3535 join_bb = else_bb;
3536 else_bb = NULL_BLOCK;
3539 /* If the THEN and ELSE block meet in a subsequent block, and the ELSE
3540 has exactly one predecessor and one successor, and the outgoing edge
3541 is not complex, then we have an IF-THEN-ELSE combo. */
3542 else if (single_succ_p (else_bb)
3543 && single_succ (then_bb) == single_succ (else_bb)
3544 && single_pred_p (else_bb)
3545 && !(single_succ_edge (else_bb)->flags & EDGE_COMPLEX)
3546 && !(epilogue_completed
3547 && tablejump_p (BB_END (else_bb), NULL, NULL)))
3548 join_bb = single_succ (else_bb);
3550 /* Otherwise it is not an IF-THEN or IF-THEN-ELSE combination. */
3551 else
3552 return FALSE;
3554 num_possible_if_blocks++;
3556 if (dump_file)
3558 fprintf (dump_file,
3559 "\nIF-THEN%s block found, pass %d, start block %d "
3560 "[insn %d], then %d [%d]",
3561 (else_bb) ? "-ELSE" : "",
3562 ce_info->pass,
3563 test_bb->index,
3564 BB_HEAD (test_bb) ? (int)INSN_UID (BB_HEAD (test_bb)) : -1,
3565 then_bb->index,
3566 BB_HEAD (then_bb) ? (int)INSN_UID (BB_HEAD (then_bb)) : -1);
3568 if (else_bb)
3569 fprintf (dump_file, ", else %d [%d]",
3570 else_bb->index,
3571 BB_HEAD (else_bb) ? (int)INSN_UID (BB_HEAD (else_bb)) : -1);
3573 fprintf (dump_file, ", join %d [%d]",
3574 join_bb->index,
3575 BB_HEAD (join_bb) ? (int)INSN_UID (BB_HEAD (join_bb)) : -1);
3577 if (ce_info->num_multiple_test_blocks > 0)
3578 fprintf (dump_file, ", %d %s block%s last test %d [%d]",
3579 ce_info->num_multiple_test_blocks,
3580 (ce_info->and_and_p) ? "&&" : "||",
3581 (ce_info->num_multiple_test_blocks == 1) ? "" : "s",
3582 ce_info->last_test_bb->index,
3583 ((BB_HEAD (ce_info->last_test_bb))
3584 ? (int)INSN_UID (BB_HEAD (ce_info->last_test_bb))
3585 : -1));
3587 fputc ('\n', dump_file);
3590 /* Make sure IF, THEN, and ELSE, blocks are adjacent. Actually, we get the
3591 first condition for free, since we've already asserted that there's a
3592 fallthru edge from IF to THEN. Likewise for the && and || blocks, since
3593 we checked the FALLTHRU flag, those are already adjacent to the last IF
3594 block. */
3595 /* ??? As an enhancement, move the ELSE block. Have to deal with
3596 BLOCK notes, if by no other means than backing out the merge if they
3597 exist. Sticky enough I don't want to think about it now. */
3598 next = then_bb;
3599 if (else_bb && (next = next->next_bb) != else_bb)
3600 return FALSE;
3601 if ((next = next->next_bb) != join_bb && join_bb != EXIT_BLOCK_PTR)
3603 if (else_bb)
3604 join_bb = NULL;
3605 else
3606 return FALSE;
3609 /* Do the real work. */
3611 ce_info->else_bb = else_bb;
3612 ce_info->join_bb = join_bb;
3614 /* If we have && and || tests, try to first handle combining the && and ||
3615 tests into the conditional code, and if that fails, go back and handle
3616 it without the && and ||, which at present handles the && case if there
3617 was no ELSE block. */
3618 if (cond_exec_process_if_block (ce_info, TRUE))
3619 return TRUE;
3621 if (ce_info->num_multiple_test_blocks)
3623 cancel_changes (0);
3625 if (cond_exec_process_if_block (ce_info, FALSE))
3626 return TRUE;
3629 return FALSE;
3632 /* Convert a branch over a trap, or a branch
3633 to a trap, into a conditional trap. */
3635 static int
3636 find_cond_trap (basic_block test_bb, edge then_edge, edge else_edge)
3638 basic_block then_bb = then_edge->dest;
3639 basic_block else_bb = else_edge->dest;
3640 basic_block other_bb, trap_bb;
3641 rtx trap, jump, cond, cond_earliest, seq;
3642 enum rtx_code code;
3644 /* Locate the block with the trap instruction. */
3645 /* ??? While we look for no successors, we really ought to allow
3646 EH successors. Need to fix merge_if_block for that to work. */
3647 if ((trap = block_has_only_trap (then_bb)) != NULL)
3648 trap_bb = then_bb, other_bb = else_bb;
3649 else if ((trap = block_has_only_trap (else_bb)) != NULL)
3650 trap_bb = else_bb, other_bb = then_bb;
3651 else
3652 return FALSE;
3654 if (dump_file)
3656 fprintf (dump_file, "\nTRAP-IF block found, start %d, trap %d\n",
3657 test_bb->index, trap_bb->index);
3660 /* If this is not a standard conditional jump, we can't parse it. */
3661 jump = BB_END (test_bb);
3662 cond = noce_get_condition (jump, &cond_earliest, false);
3663 if (! cond)
3664 return FALSE;
3666 /* If the conditional jump is more than just a conditional jump, then
3667 we can not do if-conversion on this block. */
3668 if (! onlyjump_p (jump))
3669 return FALSE;
3671 /* We must be comparing objects whose modes imply the size. */
3672 if (GET_MODE (XEXP (cond, 0)) == BLKmode)
3673 return FALSE;
3675 /* Reverse the comparison code, if necessary. */
3676 code = GET_CODE (cond);
3677 if (then_bb == trap_bb)
3679 code = reversed_comparison_code (cond, jump);
3680 if (code == UNKNOWN)
3681 return FALSE;
3684 /* Attempt to generate the conditional trap. */
3685 seq = gen_cond_trap (code, copy_rtx (XEXP (cond, 0)),
3686 copy_rtx (XEXP (cond, 1)),
3687 TRAP_CODE (PATTERN (trap)));
3688 if (seq == NULL)
3689 return FALSE;
3691 /* Emit the new insns before cond_earliest. */
3692 emit_insn_before_setloc (seq, cond_earliest, INSN_LOCATION (trap));
3694 /* Delete the trap block if possible. */
3695 remove_edge (trap_bb == then_bb ? then_edge : else_edge);
3696 df_set_bb_dirty (test_bb);
3697 df_set_bb_dirty (then_bb);
3698 df_set_bb_dirty (else_bb);
3700 if (EDGE_COUNT (trap_bb->preds) == 0)
3702 delete_basic_block (trap_bb);
3703 num_true_changes++;
3706 /* Wire together the blocks again. */
3707 if (current_ir_type () == IR_RTL_CFGLAYOUT)
3708 single_succ_edge (test_bb)->flags |= EDGE_FALLTHRU;
3709 else
3711 rtx lab, newjump;
3713 lab = JUMP_LABEL (jump);
3714 newjump = emit_jump_insn_after (gen_jump (lab), jump);
3715 LABEL_NUSES (lab) += 1;
3716 JUMP_LABEL (newjump) = lab;
3717 emit_barrier_after (newjump);
3719 delete_insn (jump);
3721 if (can_merge_blocks_p (test_bb, other_bb))
3723 merge_blocks (test_bb, other_bb);
3724 num_true_changes++;
3727 num_updated_if_blocks++;
3728 return TRUE;
3731 /* Subroutine of find_cond_trap: if BB contains only a trap insn,
3732 return it. */
3734 static rtx
3735 block_has_only_trap (basic_block bb)
3737 rtx trap;
3739 /* We're not the exit block. */
3740 if (bb == EXIT_BLOCK_PTR)
3741 return NULL_RTX;
3743 /* The block must have no successors. */
3744 if (EDGE_COUNT (bb->succs) > 0)
3745 return NULL_RTX;
3747 /* The only instruction in the THEN block must be the trap. */
3748 trap = first_active_insn (bb);
3749 if (! (trap == BB_END (bb)
3750 && GET_CODE (PATTERN (trap)) == TRAP_IF
3751 && TRAP_CONDITION (PATTERN (trap)) == const_true_rtx))
3752 return NULL_RTX;
3754 return trap;
3757 /* Look for IF-THEN-ELSE cases in which one of THEN or ELSE is
3758 transformable, but not necessarily the other. There need be no
3759 JOIN block.
3761 Return TRUE if we were successful at converting the block.
3763 Cases we'd like to look at:
3766 if (test) goto over; // x not live
3767 x = a;
3768 goto label;
3769 over:
3771 becomes
3773 x = a;
3774 if (! test) goto label;
3777 if (test) goto E; // x not live
3778 x = big();
3779 goto L;
3781 x = b;
3782 goto M;
3784 becomes
3786 x = b;
3787 if (test) goto M;
3788 x = big();
3789 goto L;
3791 (3) // This one's really only interesting for targets that can do
3792 // multiway branching, e.g. IA-64 BBB bundles. For other targets
3793 // it results in multiple branches on a cache line, which often
3794 // does not sit well with predictors.
3796 if (test1) goto E; // predicted not taken
3797 x = a;
3798 if (test2) goto F;
3801 x = b;
3804 becomes
3806 x = a;
3807 if (test1) goto E;
3808 if (test2) goto F;
3810 Notes:
3812 (A) Don't do (2) if the branch is predicted against the block we're
3813 eliminating. Do it anyway if we can eliminate a branch; this requires
3814 that the sole successor of the eliminated block postdominate the other
3815 side of the if.
3817 (B) With CE, on (3) we can steal from both sides of the if, creating
3819 if (test1) x = a;
3820 if (!test1) x = b;
3821 if (test1) goto J;
3822 if (test2) goto F;
3826 Again, this is most useful if J postdominates.
3828 (C) CE substitutes for helpful life information.
3830 (D) These heuristics need a lot of work. */
3832 /* Tests for case 1 above. */
3834 static int
3835 find_if_case_1 (basic_block test_bb, edge then_edge, edge else_edge)
3837 basic_block then_bb = then_edge->dest;
3838 basic_block else_bb = else_edge->dest;
3839 basic_block new_bb;
3840 int then_bb_index, then_prob;
3841 rtx else_target = NULL_RTX;
3843 /* If we are partitioning hot/cold basic blocks, we don't want to
3844 mess up unconditional or indirect jumps that cross between hot
3845 and cold sections.
3847 Basic block partitioning may result in some jumps that appear to
3848 be optimizable (or blocks that appear to be mergeable), but which really
3849 must be left untouched (they are required to make it safely across
3850 partition boundaries). See the comments at the top of
3851 bb-reorder.c:partition_hot_cold_basic_blocks for complete details. */
3853 if ((BB_END (then_bb)
3854 && find_reg_note (BB_END (then_bb), REG_CROSSING_JUMP, NULL_RTX))
3855 || (BB_END (test_bb)
3856 && find_reg_note (BB_END (test_bb), REG_CROSSING_JUMP, NULL_RTX))
3857 || (BB_END (else_bb)
3858 && find_reg_note (BB_END (else_bb), REG_CROSSING_JUMP,
3859 NULL_RTX)))
3860 return FALSE;
3862 /* THEN has one successor. */
3863 if (!single_succ_p (then_bb))
3864 return FALSE;
3866 /* THEN does not fall through, but is not strange either. */
3867 if (single_succ_edge (then_bb)->flags & (EDGE_COMPLEX | EDGE_FALLTHRU))
3868 return FALSE;
3870 /* THEN has one predecessor. */
3871 if (!single_pred_p (then_bb))
3872 return FALSE;
3874 /* THEN must do something. */
3875 if (forwarder_block_p (then_bb))
3876 return FALSE;
3878 num_possible_if_blocks++;
3879 if (dump_file)
3880 fprintf (dump_file,
3881 "\nIF-CASE-1 found, start %d, then %d\n",
3882 test_bb->index, then_bb->index);
3884 if (then_edge->probability)
3885 then_prob = REG_BR_PROB_BASE - then_edge->probability;
3886 else
3887 then_prob = REG_BR_PROB_BASE / 2;
3889 /* We're speculating from the THEN path, we want to make sure the cost
3890 of speculation is within reason. */
3891 if (! cheap_bb_rtx_cost_p (then_bb, then_prob,
3892 COSTS_N_INSNS (BRANCH_COST (optimize_bb_for_speed_p (then_edge->src),
3893 predictable_edge_p (then_edge)))))
3894 return FALSE;
3896 if (else_bb == EXIT_BLOCK_PTR)
3898 rtx jump = BB_END (else_edge->src);
3899 gcc_assert (JUMP_P (jump));
3900 else_target = JUMP_LABEL (jump);
3903 /* Registers set are dead, or are predicable. */
3904 if (! dead_or_predicable (test_bb, then_bb, else_bb,
3905 single_succ_edge (then_bb), 1))
3906 return FALSE;
3908 /* Conversion went ok, including moving the insns and fixing up the
3909 jump. Adjust the CFG to match. */
3911 /* We can avoid creating a new basic block if then_bb is immediately
3912 followed by else_bb, i.e. deleting then_bb allows test_bb to fall
3913 through to else_bb. */
3915 if (then_bb->next_bb == else_bb
3916 && then_bb->prev_bb == test_bb
3917 && else_bb != EXIT_BLOCK_PTR)
3919 redirect_edge_succ (FALLTHRU_EDGE (test_bb), else_bb);
3920 new_bb = 0;
3922 else if (else_bb == EXIT_BLOCK_PTR)
3923 new_bb = force_nonfallthru_and_redirect (FALLTHRU_EDGE (test_bb),
3924 else_bb, else_target);
3925 else
3926 new_bb = redirect_edge_and_branch_force (FALLTHRU_EDGE (test_bb),
3927 else_bb);
3929 df_set_bb_dirty (test_bb);
3930 df_set_bb_dirty (else_bb);
3932 then_bb_index = then_bb->index;
3933 delete_basic_block (then_bb);
3935 /* Make rest of code believe that the newly created block is the THEN_BB
3936 block we removed. */
3937 if (new_bb)
3939 df_bb_replace (then_bb_index, new_bb);
3940 /* Since the fallthru edge was redirected from test_bb to new_bb,
3941 we need to ensure that new_bb is in the same partition as
3942 test bb (you can not fall through across section boundaries). */
3943 BB_COPY_PARTITION (new_bb, test_bb);
3946 num_true_changes++;
3947 num_updated_if_blocks++;
3949 return TRUE;
3952 /* Test for case 2 above. */
3954 static int
3955 find_if_case_2 (basic_block test_bb, edge then_edge, edge else_edge)
3957 basic_block then_bb = then_edge->dest;
3958 basic_block else_bb = else_edge->dest;
3959 edge else_succ;
3960 int then_prob, else_prob;
3962 /* We do not want to speculate (empty) loop latches. */
3963 if (current_loops
3964 && else_bb->loop_father->latch == else_bb)
3965 return FALSE;
3967 /* If we are partitioning hot/cold basic blocks, we don't want to
3968 mess up unconditional or indirect jumps that cross between hot
3969 and cold sections.
3971 Basic block partitioning may result in some jumps that appear to
3972 be optimizable (or blocks that appear to be mergeable), but which really
3973 must be left untouched (they are required to make it safely across
3974 partition boundaries). See the comments at the top of
3975 bb-reorder.c:partition_hot_cold_basic_blocks for complete details. */
3977 if ((BB_END (then_bb)
3978 && find_reg_note (BB_END (then_bb), REG_CROSSING_JUMP, NULL_RTX))
3979 || (BB_END (test_bb)
3980 && find_reg_note (BB_END (test_bb), REG_CROSSING_JUMP, NULL_RTX))
3981 || (BB_END (else_bb)
3982 && find_reg_note (BB_END (else_bb), REG_CROSSING_JUMP,
3983 NULL_RTX)))
3984 return FALSE;
3986 /* ELSE has one successor. */
3987 if (!single_succ_p (else_bb))
3988 return FALSE;
3989 else
3990 else_succ = single_succ_edge (else_bb);
3992 /* ELSE outgoing edge is not complex. */
3993 if (else_succ->flags & EDGE_COMPLEX)
3994 return FALSE;
3996 /* ELSE has one predecessor. */
3997 if (!single_pred_p (else_bb))
3998 return FALSE;
4000 /* THEN is not EXIT. */
4001 if (then_bb->index < NUM_FIXED_BLOCKS)
4002 return FALSE;
4004 if (else_edge->probability)
4006 else_prob = else_edge->probability;
4007 then_prob = REG_BR_PROB_BASE - else_prob;
4009 else
4011 else_prob = REG_BR_PROB_BASE / 2;
4012 then_prob = REG_BR_PROB_BASE / 2;
4015 /* ELSE is predicted or SUCC(ELSE) postdominates THEN. */
4016 if (else_prob > then_prob)
4018 else if (else_succ->dest->index < NUM_FIXED_BLOCKS
4019 || dominated_by_p (CDI_POST_DOMINATORS, then_bb,
4020 else_succ->dest))
4022 else
4023 return FALSE;
4025 num_possible_if_blocks++;
4026 if (dump_file)
4027 fprintf (dump_file,
4028 "\nIF-CASE-2 found, start %d, else %d\n",
4029 test_bb->index, else_bb->index);
4031 /* We're speculating from the ELSE path, we want to make sure the cost
4032 of speculation is within reason. */
4033 if (! cheap_bb_rtx_cost_p (else_bb, else_prob,
4034 COSTS_N_INSNS (BRANCH_COST (optimize_bb_for_speed_p (else_edge->src),
4035 predictable_edge_p (else_edge)))))
4036 return FALSE;
4038 /* Registers set are dead, or are predicable. */
4039 if (! dead_or_predicable (test_bb, else_bb, then_bb, else_succ, 0))
4040 return FALSE;
4042 /* Conversion went ok, including moving the insns and fixing up the
4043 jump. Adjust the CFG to match. */
4045 df_set_bb_dirty (test_bb);
4046 df_set_bb_dirty (then_bb);
4047 delete_basic_block (else_bb);
4049 num_true_changes++;
4050 num_updated_if_blocks++;
4052 /* ??? We may now fallthru from one of THEN's successors into a join
4053 block. Rerun cleanup_cfg? Examine things manually? Wait? */
4055 return TRUE;
4058 /* Used by the code above to perform the actual rtl transformations.
4059 Return TRUE if successful.
4061 TEST_BB is the block containing the conditional branch. MERGE_BB
4062 is the block containing the code to manipulate. DEST_EDGE is an
4063 edge representing a jump to the join block; after the conversion,
4064 TEST_BB should be branching to its destination.
4065 REVERSEP is true if the sense of the branch should be reversed. */
4067 static int
4068 dead_or_predicable (basic_block test_bb, basic_block merge_bb,
4069 basic_block other_bb, edge dest_edge, int reversep)
4071 basic_block new_dest = dest_edge->dest;
4072 rtx head, end, jump, earliest = NULL_RTX, old_dest;
4073 bitmap merge_set = NULL;
4074 /* Number of pending changes. */
4075 int n_validated_changes = 0;
4076 rtx new_dest_label = NULL_RTX;
4078 jump = BB_END (test_bb);
4080 /* Find the extent of the real code in the merge block. */
4081 head = BB_HEAD (merge_bb);
4082 end = BB_END (merge_bb);
4084 while (DEBUG_INSN_P (end) && end != head)
4085 end = PREV_INSN (end);
4087 /* If merge_bb ends with a tablejump, predicating/moving insn's
4088 into test_bb and then deleting merge_bb will result in the jumptable
4089 that follows merge_bb being removed along with merge_bb and then we
4090 get an unresolved reference to the jumptable. */
4091 if (tablejump_p (end, NULL, NULL))
4092 return FALSE;
4094 if (LABEL_P (head))
4095 head = NEXT_INSN (head);
4096 while (DEBUG_INSN_P (head) && head != end)
4097 head = NEXT_INSN (head);
4098 if (NOTE_P (head))
4100 if (head == end)
4102 head = end = NULL_RTX;
4103 goto no_body;
4105 head = NEXT_INSN (head);
4106 while (DEBUG_INSN_P (head) && head != end)
4107 head = NEXT_INSN (head);
4110 if (JUMP_P (end))
4112 if (head == end)
4114 head = end = NULL_RTX;
4115 goto no_body;
4117 end = PREV_INSN (end);
4118 while (DEBUG_INSN_P (end) && end != head)
4119 end = PREV_INSN (end);
4122 /* Disable handling dead code by conditional execution if the machine needs
4123 to do anything funny with the tests, etc. */
4124 #ifndef IFCVT_MODIFY_TESTS
4125 if (targetm.have_conditional_execution ())
4127 /* In the conditional execution case, we have things easy. We know
4128 the condition is reversible. We don't have to check life info
4129 because we're going to conditionally execute the code anyway.
4130 All that's left is making sure the insns involved can actually
4131 be predicated. */
4133 rtx cond, prob_val;
4135 cond = cond_exec_get_condition (jump);
4136 if (! cond)
4137 return FALSE;
4139 prob_val = find_reg_note (jump, REG_BR_PROB, NULL_RTX);
4140 if (prob_val)
4141 prob_val = XEXP (prob_val, 0);
4143 if (reversep)
4145 enum rtx_code rev = reversed_comparison_code (cond, jump);
4146 if (rev == UNKNOWN)
4147 return FALSE;
4148 cond = gen_rtx_fmt_ee (rev, GET_MODE (cond), XEXP (cond, 0),
4149 XEXP (cond, 1));
4150 if (prob_val)
4151 prob_val = GEN_INT (REG_BR_PROB_BASE - INTVAL (prob_val));
4154 if (cond_exec_process_insns (NULL, head, end, cond, prob_val, 0)
4155 && verify_changes (0))
4156 n_validated_changes = num_validated_changes ();
4157 else
4158 cancel_changes (0);
4160 earliest = jump;
4162 #endif
4164 /* If we allocated new pseudos (e.g. in the conditional move
4165 expander called from noce_emit_cmove), we must resize the
4166 array first. */
4167 if (max_regno < max_reg_num ())
4168 max_regno = max_reg_num ();
4170 /* Try the NCE path if the CE path did not result in any changes. */
4171 if (n_validated_changes == 0)
4173 rtx cond, insn;
4174 regset live;
4175 bool success;
4177 /* In the non-conditional execution case, we have to verify that there
4178 are no trapping operations, no calls, no references to memory, and
4179 that any registers modified are dead at the branch site. */
4181 if (!any_condjump_p (jump))
4182 return FALSE;
4184 /* Find the extent of the conditional. */
4185 cond = noce_get_condition (jump, &earliest, false);
4186 if (!cond)
4187 return FALSE;
4189 live = BITMAP_ALLOC (&reg_obstack);
4190 simulate_backwards_to_point (merge_bb, live, end);
4191 success = can_move_insns_across (head, end, earliest, jump,
4192 merge_bb, live,
4193 df_get_live_in (other_bb), NULL);
4194 BITMAP_FREE (live);
4195 if (!success)
4196 return FALSE;
4198 /* Collect the set of registers set in MERGE_BB. */
4199 merge_set = BITMAP_ALLOC (&reg_obstack);
4201 FOR_BB_INSNS (merge_bb, insn)
4202 if (NONDEBUG_INSN_P (insn))
4203 df_simulate_find_defs (insn, merge_set);
4205 #ifdef HAVE_simple_return
4206 /* If shrink-wrapping, disable this optimization when test_bb is
4207 the first basic block and merge_bb exits. The idea is to not
4208 move code setting up a return register as that may clobber a
4209 register used to pass function parameters, which then must be
4210 saved in caller-saved regs. A caller-saved reg requires the
4211 prologue, killing a shrink-wrap opportunity. */
4212 if ((flag_shrink_wrap && HAVE_simple_return && !epilogue_completed)
4213 && ENTRY_BLOCK_PTR->next_bb == test_bb
4214 && single_succ_p (new_dest)
4215 && single_succ (new_dest) == EXIT_BLOCK_PTR
4216 && bitmap_intersect_p (df_get_live_in (new_dest), merge_set))
4218 regset return_regs;
4219 unsigned int i;
4221 return_regs = BITMAP_ALLOC (&reg_obstack);
4223 /* Start off with the intersection of regs used to pass
4224 params and regs used to return values. */
4225 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
4226 if (FUNCTION_ARG_REGNO_P (i)
4227 && targetm.calls.function_value_regno_p (i))
4228 bitmap_set_bit (return_regs, INCOMING_REGNO (i));
4230 bitmap_and_into (return_regs, df_get_live_out (ENTRY_BLOCK_PTR));
4231 bitmap_and_into (return_regs, df_get_live_in (EXIT_BLOCK_PTR));
4232 if (!bitmap_empty_p (return_regs))
4234 FOR_BB_INSNS_REVERSE (new_dest, insn)
4235 if (NONDEBUG_INSN_P (insn))
4237 df_ref *def_rec;
4238 unsigned int uid = INSN_UID (insn);
4240 /* If this insn sets any reg in return_regs.. */
4241 for (def_rec = DF_INSN_UID_DEFS (uid); *def_rec; def_rec++)
4243 df_ref def = *def_rec;
4244 unsigned r = DF_REF_REGNO (def);
4246 if (bitmap_bit_p (return_regs, r))
4247 break;
4249 /* ..then add all reg uses to the set of regs
4250 we're interested in. */
4251 if (*def_rec)
4252 df_simulate_uses (insn, return_regs);
4254 if (bitmap_intersect_p (merge_set, return_regs))
4256 BITMAP_FREE (return_regs);
4257 BITMAP_FREE (merge_set);
4258 return FALSE;
4261 BITMAP_FREE (return_regs);
4263 #endif
4266 no_body:
4267 /* We don't want to use normal invert_jump or redirect_jump because
4268 we don't want to delete_insn called. Also, we want to do our own
4269 change group management. */
4271 old_dest = JUMP_LABEL (jump);
4272 if (other_bb != new_dest)
4274 if (!any_condjump_p (jump))
4275 goto cancel;
4277 if (JUMP_P (BB_END (dest_edge->src)))
4278 new_dest_label = JUMP_LABEL (BB_END (dest_edge->src));
4279 else if (new_dest == EXIT_BLOCK_PTR)
4280 new_dest_label = ret_rtx;
4281 else
4282 new_dest_label = block_label (new_dest);
4284 if (reversep
4285 ? ! invert_jump_1 (jump, new_dest_label)
4286 : ! redirect_jump_1 (jump, new_dest_label))
4287 goto cancel;
4290 if (verify_changes (n_validated_changes))
4291 confirm_change_group ();
4292 else
4293 goto cancel;
4295 if (other_bb != new_dest)
4297 redirect_jump_2 (jump, old_dest, new_dest_label, 0, reversep);
4299 redirect_edge_succ (BRANCH_EDGE (test_bb), new_dest);
4300 if (reversep)
4302 gcov_type count, probability;
4303 count = BRANCH_EDGE (test_bb)->count;
4304 BRANCH_EDGE (test_bb)->count = FALLTHRU_EDGE (test_bb)->count;
4305 FALLTHRU_EDGE (test_bb)->count = count;
4306 probability = BRANCH_EDGE (test_bb)->probability;
4307 BRANCH_EDGE (test_bb)->probability
4308 = FALLTHRU_EDGE (test_bb)->probability;
4309 FALLTHRU_EDGE (test_bb)->probability = probability;
4310 update_br_prob_note (test_bb);
4314 /* Move the insns out of MERGE_BB to before the branch. */
4315 if (head != NULL)
4317 rtx insn;
4319 if (end == BB_END (merge_bb))
4320 BB_END (merge_bb) = PREV_INSN (head);
4322 /* PR 21767: when moving insns above a conditional branch, the REG_EQUAL
4323 notes being moved might become invalid. */
4324 insn = head;
4327 rtx note, set;
4329 if (! INSN_P (insn))
4330 continue;
4331 note = find_reg_note (insn, REG_EQUAL, NULL_RTX);
4332 if (! note)
4333 continue;
4334 set = single_set (insn);
4335 if (!set || !function_invariant_p (SET_SRC (set))
4336 || !function_invariant_p (XEXP (note, 0)))
4337 remove_note (insn, note);
4338 } while (insn != end && (insn = NEXT_INSN (insn)));
4340 /* PR46315: when moving insns above a conditional branch, the REG_EQUAL
4341 notes referring to the registers being set might become invalid. */
4342 if (merge_set)
4344 unsigned i;
4345 bitmap_iterator bi;
4347 EXECUTE_IF_SET_IN_BITMAP (merge_set, 0, i, bi)
4348 remove_reg_equal_equiv_notes_for_regno (i);
4350 BITMAP_FREE (merge_set);
4353 reorder_insns (head, end, PREV_INSN (earliest));
4356 /* Remove the jump and edge if we can. */
4357 if (other_bb == new_dest)
4359 delete_insn (jump);
4360 remove_edge (BRANCH_EDGE (test_bb));
4361 /* ??? Can't merge blocks here, as then_bb is still in use.
4362 At minimum, the merge will get done just before bb-reorder. */
4365 return TRUE;
4367 cancel:
4368 cancel_changes (0);
4370 if (merge_set)
4371 BITMAP_FREE (merge_set);
4373 return FALSE;
4376 /* Main entry point for all if-conversion. */
4378 static void
4379 if_convert (void)
4381 basic_block bb;
4382 int pass;
4384 if (optimize == 1)
4386 df_live_add_problem ();
4387 df_live_set_all_dirty ();
4390 num_possible_if_blocks = 0;
4391 num_updated_if_blocks = 0;
4392 num_true_changes = 0;
4394 loop_optimizer_init (AVOID_CFG_MODIFICATIONS);
4395 mark_loop_exit_edges ();
4396 loop_optimizer_finalize ();
4397 free_dominance_info (CDI_DOMINATORS);
4399 /* Compute postdominators. */
4400 calculate_dominance_info (CDI_POST_DOMINATORS);
4402 df_set_flags (DF_LR_RUN_DCE);
4404 /* Go through each of the basic blocks looking for things to convert. If we
4405 have conditional execution, we make multiple passes to allow us to handle
4406 IF-THEN{-ELSE} blocks within other IF-THEN{-ELSE} blocks. */
4407 pass = 0;
4410 df_analyze ();
4411 /* Only need to do dce on the first pass. */
4412 df_clear_flags (DF_LR_RUN_DCE);
4413 cond_exec_changed_p = FALSE;
4414 pass++;
4416 #ifdef IFCVT_MULTIPLE_DUMPS
4417 if (dump_file && pass > 1)
4418 fprintf (dump_file, "\n\n========== Pass %d ==========\n", pass);
4419 #endif
4421 FOR_EACH_BB (bb)
4423 basic_block new_bb;
4424 while (!df_get_bb_dirty (bb)
4425 && (new_bb = find_if_header (bb, pass)) != NULL)
4426 bb = new_bb;
4429 #ifdef IFCVT_MULTIPLE_DUMPS
4430 if (dump_file && cond_exec_changed_p)
4431 print_rtl_with_bb (dump_file, get_insns (), dump_flags);
4432 #endif
4434 while (cond_exec_changed_p);
4436 #ifdef IFCVT_MULTIPLE_DUMPS
4437 if (dump_file)
4438 fprintf (dump_file, "\n\n========== no more changes\n");
4439 #endif
4441 free_dominance_info (CDI_POST_DOMINATORS);
4443 if (dump_file)
4444 fflush (dump_file);
4446 clear_aux_for_blocks ();
4448 /* If we allocated new pseudos, we must resize the array for sched1. */
4449 if (max_regno < max_reg_num ())
4450 max_regno = max_reg_num ();
4452 /* Write the final stats. */
4453 if (dump_file && num_possible_if_blocks > 0)
4455 fprintf (dump_file,
4456 "\n%d possible IF blocks searched.\n",
4457 num_possible_if_blocks);
4458 fprintf (dump_file,
4459 "%d IF blocks converted.\n",
4460 num_updated_if_blocks);
4461 fprintf (dump_file,
4462 "%d true changes made.\n\n\n",
4463 num_true_changes);
4466 if (optimize == 1)
4467 df_remove_problem (df_live);
4469 #ifdef ENABLE_CHECKING
4470 verify_flow_info ();
4471 #endif
4474 static bool
4475 gate_handle_if_conversion (void)
4477 return (optimize > 0)
4478 && dbg_cnt (if_conversion);
4481 /* If-conversion and CFG cleanup. */
4482 static unsigned int
4483 rest_of_handle_if_conversion (void)
4485 if (flag_if_conversion)
4487 if (dump_file)
4489 dump_reg_info (dump_file);
4490 dump_flow_info (dump_file, dump_flags);
4492 cleanup_cfg (CLEANUP_EXPENSIVE);
4493 if_convert ();
4496 cleanup_cfg (0);
4497 return 0;
4500 struct rtl_opt_pass pass_rtl_ifcvt =
4503 RTL_PASS,
4504 "ce1", /* name */
4505 OPTGROUP_NONE, /* optinfo_flags */
4506 gate_handle_if_conversion, /* gate */
4507 rest_of_handle_if_conversion, /* execute */
4508 NULL, /* sub */
4509 NULL, /* next */
4510 0, /* static_pass_number */
4511 TV_IFCVT, /* tv_id */
4512 0, /* properties_required */
4513 0, /* properties_provided */
4514 0, /* properties_destroyed */
4515 0, /* todo_flags_start */
4516 TODO_df_finish | TODO_verify_rtl_sharing |
4517 0 /* todo_flags_finish */
4521 static bool
4522 gate_handle_if_after_combine (void)
4524 return optimize > 0 && flag_if_conversion
4525 && dbg_cnt (if_after_combine);
4529 /* Rerun if-conversion, as combine may have simplified things enough
4530 to now meet sequence length restrictions. */
4531 static unsigned int
4532 rest_of_handle_if_after_combine (void)
4534 if_convert ();
4535 return 0;
4538 struct rtl_opt_pass pass_if_after_combine =
4541 RTL_PASS,
4542 "ce2", /* name */
4543 OPTGROUP_NONE, /* optinfo_flags */
4544 gate_handle_if_after_combine, /* gate */
4545 rest_of_handle_if_after_combine, /* execute */
4546 NULL, /* sub */
4547 NULL, /* next */
4548 0, /* static_pass_number */
4549 TV_IFCVT, /* tv_id */
4550 0, /* properties_required */
4551 0, /* properties_provided */
4552 0, /* properties_destroyed */
4553 0, /* todo_flags_start */
4554 TODO_df_finish | TODO_verify_rtl_sharing |
4555 TODO_ggc_collect /* todo_flags_finish */
4560 static bool
4561 gate_handle_if_after_reload (void)
4563 return optimize > 0 && flag_if_conversion2
4564 && dbg_cnt (if_after_reload);
4567 static unsigned int
4568 rest_of_handle_if_after_reload (void)
4570 if_convert ();
4571 return 0;
4575 struct rtl_opt_pass pass_if_after_reload =
4578 RTL_PASS,
4579 "ce3", /* name */
4580 OPTGROUP_NONE, /* optinfo_flags */
4581 gate_handle_if_after_reload, /* gate */
4582 rest_of_handle_if_after_reload, /* execute */
4583 NULL, /* sub */
4584 NULL, /* next */
4585 0, /* static_pass_number */
4586 TV_IFCVT2, /* tv_id */
4587 0, /* properties_required */
4588 0, /* properties_provided */
4589 0, /* properties_destroyed */
4590 0, /* todo_flags_start */
4591 TODO_df_finish | TODO_verify_rtl_sharing |
4592 TODO_ggc_collect /* todo_flags_finish */