configure.in (GLIBCPP_ENABLE_CXX_FLAGS): Do not pass arguments, let the defaults...
[official-gcc.git] / gcc / ifcvt.c
blob0652afd99ee060a48fee72a64f393e0b090f9a2c
1 /* If-conversion support.
2 Copyright (C) 2000, 2001, 2002 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 2, 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 COPYING. If not, write to the Free
18 Software Foundation, 59 Temple Place - Suite 330, Boston, MA
19 02111-1307, USA. */
21 #include "config.h"
22 #include "system.h"
23 #include "coretypes.h"
24 #include "tm.h"
26 #include "rtl.h"
27 #include "regs.h"
28 #include "function.h"
29 #include "flags.h"
30 #include "insn-config.h"
31 #include "recog.h"
32 #include "except.h"
33 #include "hard-reg-set.h"
34 #include "basic-block.h"
35 #include "expr.h"
36 #include "real.h"
37 #include "output.h"
38 #include "toplev.h"
39 #include "tm_p.h"
42 #ifndef HAVE_conditional_execution
43 #define HAVE_conditional_execution 0
44 #endif
45 #ifndef HAVE_conditional_move
46 #define HAVE_conditional_move 0
47 #endif
48 #ifndef HAVE_incscc
49 #define HAVE_incscc 0
50 #endif
51 #ifndef HAVE_decscc
52 #define HAVE_decscc 0
53 #endif
54 #ifndef HAVE_trap
55 #define HAVE_trap 0
56 #endif
57 #ifndef HAVE_conditional_trap
58 #define HAVE_conditional_trap 0
59 #endif
61 #ifndef MAX_CONDITIONAL_EXECUTE
62 #define MAX_CONDITIONAL_EXECUTE (BRANCH_COST + 1)
63 #endif
65 #define NULL_EDGE ((struct edge_def *)NULL)
66 #define NULL_BLOCK ((struct basic_block_def *)NULL)
68 /* # of IF-THEN or IF-THEN-ELSE blocks we looked at */
69 static int num_possible_if_blocks;
71 /* # of IF-THEN or IF-THEN-ELSE blocks were converted to conditional
72 execution. */
73 static int num_updated_if_blocks;
75 /* # of basic blocks that were removed. */
76 static int num_removed_blocks;
78 /* Whether conditional execution changes were made. */
79 static int cond_exec_changed_p;
81 /* True if life data ok at present. */
82 static bool life_data_ok;
84 /* The post-dominator relation on the original block numbers. */
85 static dominance_info post_dominators;
87 /* Forward references. */
88 static int count_bb_insns PARAMS ((basic_block));
89 static rtx first_active_insn PARAMS ((basic_block));
90 static rtx last_active_insn PARAMS ((basic_block, int));
91 static int seq_contains_jump PARAMS ((rtx));
92 static basic_block block_fallthru PARAMS ((basic_block));
93 static int cond_exec_process_insns PARAMS ((ce_if_block_t *,
94 rtx, rtx, rtx, rtx, int));
95 static rtx cond_exec_get_condition PARAMS ((rtx));
96 static int cond_exec_process_if_block PARAMS ((ce_if_block_t *, int));
97 static rtx noce_get_condition PARAMS ((rtx, rtx *));
98 static int noce_operand_ok PARAMS ((rtx));
99 static int noce_process_if_block PARAMS ((ce_if_block_t *));
100 static int process_if_block PARAMS ((ce_if_block_t *));
101 static void merge_if_block PARAMS ((ce_if_block_t *));
102 static int find_cond_trap PARAMS ((basic_block, edge, edge));
103 static basic_block find_if_header PARAMS ((basic_block, int));
104 static int block_jumps_and_fallthru_p PARAMS ((basic_block, basic_block));
105 static int find_if_block PARAMS ((ce_if_block_t *));
106 static int find_if_case_1 PARAMS ((basic_block, edge, edge));
107 static int find_if_case_2 PARAMS ((basic_block, edge, edge));
108 static int find_memory PARAMS ((rtx *, void *));
109 static int dead_or_predicable PARAMS ((basic_block, basic_block,
110 basic_block, basic_block, int));
111 static void noce_emit_move_insn PARAMS ((rtx, rtx));
112 static rtx block_has_only_trap PARAMS ((basic_block));
114 /* Count the number of non-jump active insns in BB. */
116 static int
117 count_bb_insns (bb)
118 basic_block bb;
120 int count = 0;
121 rtx insn = bb->head;
123 while (1)
125 if (GET_CODE (insn) == CALL_INSN || GET_CODE (insn) == INSN)
126 count++;
128 if (insn == bb->end)
129 break;
130 insn = NEXT_INSN (insn);
133 return count;
136 /* Return the first non-jump active insn in the basic block. */
138 static rtx
139 first_active_insn (bb)
140 basic_block bb;
142 rtx insn = bb->head;
144 if (GET_CODE (insn) == CODE_LABEL)
146 if (insn == bb->end)
147 return NULL_RTX;
148 insn = NEXT_INSN (insn);
151 while (GET_CODE (insn) == NOTE)
153 if (insn == bb->end)
154 return NULL_RTX;
155 insn = NEXT_INSN (insn);
158 if (GET_CODE (insn) == JUMP_INSN)
159 return NULL_RTX;
161 return insn;
164 /* Return the last non-jump active (non-jump) insn in the basic block. */
166 static rtx
167 last_active_insn (bb, skip_use_p)
168 basic_block bb;
169 int skip_use_p;
171 rtx insn = bb->end;
172 rtx head = bb->head;
174 while (GET_CODE (insn) == NOTE
175 || GET_CODE (insn) == JUMP_INSN
176 || (skip_use_p
177 && GET_CODE (insn) == INSN
178 && GET_CODE (PATTERN (insn)) == USE))
180 if (insn == head)
181 return NULL_RTX;
182 insn = PREV_INSN (insn);
185 if (GET_CODE (insn) == CODE_LABEL)
186 return NULL_RTX;
188 return insn;
191 /* It is possible, especially when having dealt with multi-word
192 arithmetic, for the expanders to have emitted jumps. Search
193 through the sequence and return TRUE if a jump exists so that
194 we can abort the conversion. */
196 static int
197 seq_contains_jump (insn)
198 rtx insn;
200 while (insn)
202 if (GET_CODE (insn) == JUMP_INSN)
203 return 1;
204 insn = NEXT_INSN (insn);
206 return 0;
209 static basic_block
210 block_fallthru (bb)
211 basic_block bb;
213 edge e;
215 for (e = bb->succ;
216 e != NULL_EDGE && (e->flags & EDGE_FALLTHRU) == 0;
217 e = e->succ_next)
220 return (e) ? e->dest : NULL_BLOCK;
223 /* Go through a bunch of insns, converting them to conditional
224 execution format if possible. Return TRUE if all of the non-note
225 insns were processed. */
227 static int
228 cond_exec_process_insns (ce_info, start, end, test, prob_val, mod_ok)
229 ce_if_block_t *ce_info ATTRIBUTE_UNUSED; /* if block information */
230 rtx start; /* first insn to look at */
231 rtx end; /* last insn to look at */
232 rtx test; /* conditional execution test */
233 rtx prob_val; /* probability of branch taken. */
234 int mod_ok; /* true if modifications ok last insn. */
236 int must_be_last = FALSE;
237 rtx insn;
238 rtx xtest;
239 rtx pattern;
241 if (!start || !end)
242 return FALSE;
244 for (insn = start; ; insn = NEXT_INSN (insn))
246 if (GET_CODE (insn) == NOTE)
247 goto insn_done;
249 if (GET_CODE (insn) != INSN && GET_CODE (insn) != CALL_INSN)
250 abort ();
252 /* Remove USE insns that get in the way. */
253 if (reload_completed && GET_CODE (PATTERN (insn)) == USE)
255 /* ??? Ug. Actually unlinking the thing is problematic,
256 given what we'd have to coordinate with our callers. */
257 PUT_CODE (insn, NOTE);
258 NOTE_LINE_NUMBER (insn) = NOTE_INSN_DELETED;
259 NOTE_SOURCE_FILE (insn) = 0;
260 goto insn_done;
263 /* Last insn wasn't last? */
264 if (must_be_last)
265 return FALSE;
267 if (modified_in_p (test, insn))
269 if (!mod_ok)
270 return FALSE;
271 must_be_last = TRUE;
274 /* Now build the conditional form of the instruction. */
275 pattern = PATTERN (insn);
276 xtest = copy_rtx (test);
278 /* If this is already a COND_EXEC, rewrite the test to be an AND of the
279 two conditions. */
280 if (GET_CODE (pattern) == COND_EXEC)
282 if (GET_MODE (xtest) != GET_MODE (COND_EXEC_TEST (pattern)))
283 return FALSE;
285 xtest = gen_rtx_AND (GET_MODE (xtest), xtest,
286 COND_EXEC_TEST (pattern));
287 pattern = COND_EXEC_CODE (pattern);
290 pattern = gen_rtx_COND_EXEC (VOIDmode, xtest, pattern);
292 /* If the machine needs to modify the insn being conditionally executed,
293 say for example to force a constant integer operand into a temp
294 register, do so here. */
295 #ifdef IFCVT_MODIFY_INSN
296 IFCVT_MODIFY_INSN (ce_info, pattern, insn);
297 if (! pattern)
298 return FALSE;
299 #endif
301 validate_change (insn, &PATTERN (insn), pattern, 1);
303 if (GET_CODE (insn) == CALL_INSN && prob_val)
304 validate_change (insn, &REG_NOTES (insn),
305 alloc_EXPR_LIST (REG_BR_PROB, prob_val,
306 REG_NOTES (insn)), 1);
308 insn_done:
309 if (insn == end)
310 break;
313 return TRUE;
316 /* Return the condition for a jump. Do not do any special processing. */
318 static rtx
319 cond_exec_get_condition (jump)
320 rtx jump;
322 rtx test_if, cond;
324 if (any_condjump_p (jump))
325 test_if = SET_SRC (pc_set (jump));
326 else
327 return NULL_RTX;
328 cond = XEXP (test_if, 0);
330 /* If this branches to JUMP_LABEL when the condition is false,
331 reverse the condition. */
332 if (GET_CODE (XEXP (test_if, 2)) == LABEL_REF
333 && XEXP (XEXP (test_if, 2), 0) == JUMP_LABEL (jump))
335 enum rtx_code rev = reversed_comparison_code (cond, jump);
336 if (rev == UNKNOWN)
337 return NULL_RTX;
339 cond = gen_rtx_fmt_ee (rev, GET_MODE (cond), XEXP (cond, 0),
340 XEXP (cond, 1));
343 return cond;
346 /* Given a simple IF-THEN or IF-THEN-ELSE block, attempt to convert it
347 to conditional execution. Return TRUE if we were successful at
348 converting the block. */
350 static int
351 cond_exec_process_if_block (ce_info, do_multiple_p)
352 ce_if_block_t * ce_info; /* if block information */
353 int do_multiple_p; /* != 0 if we should handle && and || blocks */
355 basic_block test_bb = ce_info->test_bb; /* last test block */
356 basic_block then_bb = ce_info->then_bb; /* THEN */
357 basic_block else_bb = ce_info->else_bb; /* ELSE or NULL */
358 rtx test_expr; /* expression in IF_THEN_ELSE that is tested */
359 rtx then_start; /* first insn in THEN block */
360 rtx then_end; /* last insn + 1 in THEN block */
361 rtx else_start = NULL_RTX; /* first insn in ELSE block or NULL */
362 rtx else_end = NULL_RTX; /* last insn + 1 in ELSE block */
363 int max; /* max # of insns to convert. */
364 int then_mod_ok; /* whether conditional mods are ok in THEN */
365 rtx true_expr; /* test for else block insns */
366 rtx false_expr; /* test for then block insns */
367 rtx true_prob_val; /* probability of else block */
368 rtx false_prob_val; /* probability of then block */
369 int n_insns;
370 enum rtx_code false_code;
372 /* If test is comprised of && or || elements, and we've failed at handling
373 all of them together, just use the last test if it is the special case of
374 && elements without an ELSE block. */
375 if (!do_multiple_p && ce_info->num_multiple_test_blocks)
377 if (else_bb || ! ce_info->and_and_p)
378 return FALSE;
380 ce_info->test_bb = test_bb = ce_info->last_test_bb;
381 ce_info->num_multiple_test_blocks = 0;
382 ce_info->num_and_and_blocks = 0;
383 ce_info->num_or_or_blocks = 0;
386 /* Find the conditional jump to the ELSE or JOIN part, and isolate
387 the test. */
388 test_expr = cond_exec_get_condition (test_bb->end);
389 if (! test_expr)
390 return FALSE;
392 /* If the conditional jump is more than just a conditional jump,
393 then we can not do conditional execution conversion on this block. */
394 if (! onlyjump_p (test_bb->end))
395 return FALSE;
397 /* Collect the bounds of where we're to search, skipping any labels, jumps
398 and notes at the beginning and end of the block. Then count the total
399 number of insns and see if it is small enough to convert. */
400 then_start = first_active_insn (then_bb);
401 then_end = last_active_insn (then_bb, TRUE);
402 n_insns = ce_info->num_then_insns = count_bb_insns (then_bb);
403 max = MAX_CONDITIONAL_EXECUTE;
405 if (else_bb)
407 max *= 2;
408 else_start = first_active_insn (else_bb);
409 else_end = last_active_insn (else_bb, TRUE);
410 n_insns += ce_info->num_else_insns = count_bb_insns (else_bb);
413 if (n_insns > max)
414 return FALSE;
416 /* Map test_expr/test_jump into the appropriate MD tests to use on
417 the conditionally executed code. */
419 true_expr = test_expr;
421 false_code = reversed_comparison_code (true_expr, test_bb->end);
422 if (false_code != UNKNOWN)
423 false_expr = gen_rtx_fmt_ee (false_code, GET_MODE (true_expr),
424 XEXP (true_expr, 0), XEXP (true_expr, 1));
425 else
426 false_expr = NULL_RTX;
428 #ifdef IFCVT_MODIFY_TESTS
429 /* If the machine description needs to modify the tests, such as setting a
430 conditional execution register from a comparison, it can do so here. */
431 IFCVT_MODIFY_TESTS (ce_info, true_expr, false_expr);
433 /* See if the conversion failed */
434 if (!true_expr || !false_expr)
435 goto fail;
436 #endif
438 true_prob_val = find_reg_note (test_bb->end, REG_BR_PROB, NULL_RTX);
439 if (true_prob_val)
441 true_prob_val = XEXP (true_prob_val, 0);
442 false_prob_val = GEN_INT (REG_BR_PROB_BASE - INTVAL (true_prob_val));
444 else
445 false_prob_val = NULL_RTX;
447 /* If we have && or || tests, do them here. These tests are in the adjacent
448 blocks after the first block containing the test. */
449 if (ce_info->num_multiple_test_blocks > 0)
451 basic_block bb = test_bb;
452 basic_block last_test_bb = ce_info->last_test_bb;
454 if (! false_expr)
455 goto fail;
459 rtx start, end;
460 rtx t, f;
462 bb = block_fallthru (bb);
463 start = first_active_insn (bb);
464 end = last_active_insn (bb, TRUE);
465 if (start
466 && ! cond_exec_process_insns (ce_info, start, end, false_expr,
467 false_prob_val, FALSE))
468 goto fail;
470 /* If the conditional jump is more than just a conditional jump, then
471 we can not do conditional execution conversion on this block. */
472 if (! onlyjump_p (bb->end))
473 goto fail;
475 /* Find the conditional jump and isolate the test. */
476 t = cond_exec_get_condition (bb->end);
477 if (! t)
478 goto fail;
480 f = gen_rtx_fmt_ee (reverse_condition (GET_CODE (t)),
481 GET_MODE (t),
482 XEXP (t, 0),
483 XEXP (t, 1));
485 if (ce_info->and_and_p)
487 t = gen_rtx_AND (GET_MODE (t), true_expr, t);
488 f = gen_rtx_IOR (GET_MODE (t), false_expr, f);
490 else
492 t = gen_rtx_IOR (GET_MODE (t), true_expr, t);
493 f = gen_rtx_AND (GET_MODE (t), false_expr, f);
496 /* If the machine description needs to modify the tests, such as
497 setting a conditional execution register from a comparison, it can
498 do so here. */
499 #ifdef IFCVT_MODIFY_MULTIPLE_TESTS
500 IFCVT_MODIFY_MULTIPLE_TESTS (ce_info, bb, t, f);
502 /* See if the conversion failed */
503 if (!t || !f)
504 goto fail;
505 #endif
507 true_expr = t;
508 false_expr = f;
510 while (bb != last_test_bb);
513 /* For IF-THEN-ELSE blocks, we don't allow modifications of the test
514 on then THEN block. */
515 then_mod_ok = (else_bb == NULL_BLOCK);
517 /* Go through the THEN and ELSE blocks converting the insns if possible
518 to conditional execution. */
520 if (then_end
521 && (! false_expr
522 || ! cond_exec_process_insns (ce_info, then_start, then_end,
523 false_expr, false_prob_val,
524 then_mod_ok)))
525 goto fail;
527 if (else_bb && else_end
528 && ! cond_exec_process_insns (ce_info, else_start, else_end,
529 true_expr, true_prob_val, TRUE))
530 goto fail;
532 /* If we cannot apply the changes, fail. Do not go through the normal fail
533 processing, since apply_change_group will call cancel_changes. */
534 if (! apply_change_group ())
536 #ifdef IFCVT_MODIFY_CANCEL
537 /* Cancel any machine dependent changes. */
538 IFCVT_MODIFY_CANCEL (ce_info);
539 #endif
540 return FALSE;
543 #ifdef IFCVT_MODIFY_FINAL
544 /* Do any machine dependent final modifications */
545 IFCVT_MODIFY_FINAL (ce_info);
546 #endif
548 /* Conversion succeeded. */
549 if (rtl_dump_file)
550 fprintf (rtl_dump_file, "%d insn%s converted to conditional execution.\n",
551 n_insns, (n_insns == 1) ? " was" : "s were");
553 /* Merge the blocks! */
554 merge_if_block (ce_info);
555 cond_exec_changed_p = TRUE;
556 return TRUE;
558 fail:
559 #ifdef IFCVT_MODIFY_CANCEL
560 /* Cancel any machine dependent changes. */
561 IFCVT_MODIFY_CANCEL (ce_info);
562 #endif
564 cancel_changes (0);
565 return FALSE;
568 /* Used by noce_process_if_block to communicate with its subroutines.
570 The subroutines know that A and B may be evaluated freely. They
571 know that X is a register. They should insert new instructions
572 before cond_earliest. */
574 struct noce_if_info
576 basic_block test_bb;
577 rtx insn_a, insn_b;
578 rtx x, a, b;
579 rtx jump, cond, cond_earliest;
582 static rtx noce_emit_store_flag PARAMS ((struct noce_if_info *,
583 rtx, int, int));
584 static int noce_try_store_flag PARAMS ((struct noce_if_info *));
585 static int noce_try_store_flag_inc PARAMS ((struct noce_if_info *));
586 static int noce_try_store_flag_constants PARAMS ((struct noce_if_info *));
587 static int noce_try_store_flag_mask PARAMS ((struct noce_if_info *));
588 static rtx noce_emit_cmove PARAMS ((struct noce_if_info *,
589 rtx, enum rtx_code, rtx,
590 rtx, rtx, rtx));
591 static int noce_try_cmove PARAMS ((struct noce_if_info *));
592 static int noce_try_cmove_arith PARAMS ((struct noce_if_info *));
593 static rtx noce_get_alt_condition PARAMS ((struct noce_if_info *,
594 rtx, rtx *));
595 static int noce_try_minmax PARAMS ((struct noce_if_info *));
596 static int noce_try_abs PARAMS ((struct noce_if_info *));
598 /* Helper function for noce_try_store_flag*. */
600 static rtx
601 noce_emit_store_flag (if_info, x, reversep, normalize)
602 struct noce_if_info *if_info;
603 rtx x;
604 int reversep, normalize;
606 rtx cond = if_info->cond;
607 int cond_complex;
608 enum rtx_code code;
610 cond_complex = (! general_operand (XEXP (cond, 0), VOIDmode)
611 || ! general_operand (XEXP (cond, 1), VOIDmode));
613 /* If earliest == jump, or when the condition is complex, try to
614 build the store_flag insn directly. */
616 if (cond_complex)
617 cond = XEXP (SET_SRC (pc_set (if_info->jump)), 0);
619 if (reversep)
620 code = reversed_comparison_code (cond, if_info->jump);
621 else
622 code = GET_CODE (cond);
624 if ((if_info->cond_earliest == if_info->jump || cond_complex)
625 && (normalize == 0 || STORE_FLAG_VALUE == normalize))
627 rtx tmp;
629 tmp = gen_rtx_fmt_ee (code, GET_MODE (x), XEXP (cond, 0),
630 XEXP (cond, 1));
631 tmp = gen_rtx_SET (VOIDmode, x, tmp);
633 start_sequence ();
634 tmp = emit_insn (tmp);
636 if (recog_memoized (tmp) >= 0)
638 tmp = get_insns ();
639 end_sequence ();
640 emit_insn (tmp);
642 if_info->cond_earliest = if_info->jump;
644 return x;
647 end_sequence ();
650 /* Don't even try if the comparison operands are weird. */
651 if (cond_complex)
652 return NULL_RTX;
654 return emit_store_flag (x, code, XEXP (cond, 0),
655 XEXP (cond, 1), VOIDmode,
656 (code == LTU || code == LEU
657 || code == GEU || code == GTU), normalize);
660 /* Emit instruction to move an rtx into STRICT_LOW_PART. */
661 static void
662 noce_emit_move_insn (x, y)
663 rtx x, y;
665 enum machine_mode outmode, inmode;
666 rtx outer, inner;
667 int bitpos;
669 if (GET_CODE (x) != STRICT_LOW_PART)
671 emit_move_insn (x, y);
672 return;
675 outer = XEXP (x, 0);
676 inner = XEXP (outer, 0);
677 outmode = GET_MODE (outer);
678 inmode = GET_MODE (inner);
679 bitpos = SUBREG_BYTE (outer) * BITS_PER_UNIT;
680 store_bit_field (inner, GET_MODE_BITSIZE (outmode), bitpos, outmode, y,
681 GET_MODE_BITSIZE (inmode));
684 /* Convert "if (test) x = 1; else x = 0".
686 Only try 0 and STORE_FLAG_VALUE here. Other combinations will be
687 tried in noce_try_store_flag_constants after noce_try_cmove has had
688 a go at the conversion. */
690 static int
691 noce_try_store_flag (if_info)
692 struct noce_if_info *if_info;
694 int reversep;
695 rtx target, seq;
697 if (GET_CODE (if_info->b) == CONST_INT
698 && INTVAL (if_info->b) == STORE_FLAG_VALUE
699 && if_info->a == const0_rtx)
700 reversep = 0;
701 else if (if_info->b == const0_rtx
702 && GET_CODE (if_info->a) == CONST_INT
703 && INTVAL (if_info->a) == STORE_FLAG_VALUE
704 && (reversed_comparison_code (if_info->cond, if_info->jump)
705 != UNKNOWN))
706 reversep = 1;
707 else
708 return FALSE;
710 start_sequence ();
712 target = noce_emit_store_flag (if_info, if_info->x, reversep, 0);
713 if (target)
715 if (target != if_info->x)
716 noce_emit_move_insn (if_info->x, target);
718 seq = get_insns ();
719 end_sequence ();
720 emit_insn_before_scope (seq, if_info->jump, INSN_SCOPE (if_info->insn_a));
722 return TRUE;
724 else
726 end_sequence ();
727 return FALSE;
731 /* Convert "if (test) x = a; else x = b", for A and B constant. */
733 static int
734 noce_try_store_flag_constants (if_info)
735 struct noce_if_info *if_info;
737 rtx target, seq;
738 int reversep;
739 HOST_WIDE_INT itrue, ifalse, diff, tmp;
740 int normalize, can_reverse;
741 enum machine_mode mode;
743 if (! no_new_pseudos
744 && GET_CODE (if_info->a) == CONST_INT
745 && GET_CODE (if_info->b) == CONST_INT)
747 mode = GET_MODE (if_info->x);
748 ifalse = INTVAL (if_info->a);
749 itrue = INTVAL (if_info->b);
751 /* Make sure we can represent the difference between the two values. */
752 if ((itrue - ifalse > 0)
753 != ((ifalse < 0) != (itrue < 0) ? ifalse < 0 : ifalse < itrue))
754 return FALSE;
756 diff = trunc_int_for_mode (itrue - ifalse, mode);
758 can_reverse = (reversed_comparison_code (if_info->cond, if_info->jump)
759 != UNKNOWN);
761 reversep = 0;
762 if (diff == STORE_FLAG_VALUE || diff == -STORE_FLAG_VALUE)
763 normalize = 0;
764 else if (ifalse == 0 && exact_log2 (itrue) >= 0
765 && (STORE_FLAG_VALUE == 1
766 || BRANCH_COST >= 2))
767 normalize = 1;
768 else if (itrue == 0 && exact_log2 (ifalse) >= 0 && can_reverse
769 && (STORE_FLAG_VALUE == 1 || BRANCH_COST >= 2))
770 normalize = 1, reversep = 1;
771 else if (itrue == -1
772 && (STORE_FLAG_VALUE == -1
773 || BRANCH_COST >= 2))
774 normalize = -1;
775 else if (ifalse == -1 && can_reverse
776 && (STORE_FLAG_VALUE == -1 || BRANCH_COST >= 2))
777 normalize = -1, reversep = 1;
778 else if ((BRANCH_COST >= 2 && STORE_FLAG_VALUE == -1)
779 || BRANCH_COST >= 3)
780 normalize = -1;
781 else
782 return FALSE;
784 if (reversep)
786 tmp = itrue; itrue = ifalse; ifalse = tmp;
787 diff = trunc_int_for_mode (-diff, mode);
790 start_sequence ();
791 target = noce_emit_store_flag (if_info, if_info->x, reversep, normalize);
792 if (! target)
794 end_sequence ();
795 return FALSE;
798 /* if (test) x = 3; else x = 4;
799 => x = 3 + (test == 0); */
800 if (diff == STORE_FLAG_VALUE || diff == -STORE_FLAG_VALUE)
802 target = expand_simple_binop (mode,
803 (diff == STORE_FLAG_VALUE
804 ? PLUS : MINUS),
805 GEN_INT (ifalse), target, if_info->x, 0,
806 OPTAB_WIDEN);
809 /* if (test) x = 8; else x = 0;
810 => x = (test != 0) << 3; */
811 else if (ifalse == 0 && (tmp = exact_log2 (itrue)) >= 0)
813 target = expand_simple_binop (mode, ASHIFT,
814 target, GEN_INT (tmp), if_info->x, 0,
815 OPTAB_WIDEN);
818 /* if (test) x = -1; else x = b;
819 => x = -(test != 0) | b; */
820 else if (itrue == -1)
822 target = expand_simple_binop (mode, IOR,
823 target, GEN_INT (ifalse), if_info->x, 0,
824 OPTAB_WIDEN);
827 /* if (test) x = a; else x = b;
828 => x = (-(test != 0) & (b - a)) + a; */
829 else
831 target = expand_simple_binop (mode, AND,
832 target, GEN_INT (diff), if_info->x, 0,
833 OPTAB_WIDEN);
834 if (target)
835 target = expand_simple_binop (mode, PLUS,
836 target, GEN_INT (ifalse),
837 if_info->x, 0, OPTAB_WIDEN);
840 if (! target)
842 end_sequence ();
843 return FALSE;
846 if (target != if_info->x)
847 noce_emit_move_insn (if_info->x, target);
849 seq = get_insns ();
850 end_sequence ();
852 if (seq_contains_jump (seq))
853 return FALSE;
855 emit_insn_before_scope (seq, if_info->jump, INSN_SCOPE (if_info->insn_a));
857 return TRUE;
860 return FALSE;
863 /* Convert "if (test) foo++" into "foo += (test != 0)", and
864 similarly for "foo--". */
866 static int
867 noce_try_store_flag_inc (if_info)
868 struct noce_if_info *if_info;
870 rtx target, seq;
871 int subtract, normalize;
873 if (! no_new_pseudos
874 && (BRANCH_COST >= 2
875 || HAVE_incscc
876 || HAVE_decscc)
877 /* Should be no `else' case to worry about. */
878 && if_info->b == if_info->x
879 && GET_CODE (if_info->a) == PLUS
880 && (XEXP (if_info->a, 1) == const1_rtx
881 || XEXP (if_info->a, 1) == constm1_rtx)
882 && rtx_equal_p (XEXP (if_info->a, 0), if_info->x)
883 && (reversed_comparison_code (if_info->cond, if_info->jump)
884 != UNKNOWN))
886 if (STORE_FLAG_VALUE == INTVAL (XEXP (if_info->a, 1)))
887 subtract = 0, normalize = 0;
888 else if (-STORE_FLAG_VALUE == INTVAL (XEXP (if_info->a, 1)))
889 subtract = 1, normalize = 0;
890 else
891 subtract = 0, normalize = INTVAL (XEXP (if_info->a, 1));
893 start_sequence ();
895 target = noce_emit_store_flag (if_info,
896 gen_reg_rtx (GET_MODE (if_info->x)),
897 1, normalize);
899 if (target)
900 target = expand_simple_binop (GET_MODE (if_info->x),
901 subtract ? MINUS : PLUS,
902 if_info->x, target, if_info->x,
903 0, OPTAB_WIDEN);
904 if (target)
906 if (target != if_info->x)
907 noce_emit_move_insn (if_info->x, target);
909 seq = get_insns ();
910 end_sequence ();
912 if (seq_contains_jump (seq))
913 return FALSE;
915 emit_insn_before_scope (seq, if_info->jump,
916 INSN_SCOPE (if_info->insn_a));
918 return TRUE;
921 end_sequence ();
924 return FALSE;
927 /* Convert "if (test) x = 0;" to "x &= -(test == 0);" */
929 static int
930 noce_try_store_flag_mask (if_info)
931 struct noce_if_info *if_info;
933 rtx target, seq;
934 int reversep;
936 reversep = 0;
937 if (! no_new_pseudos
938 && (BRANCH_COST >= 2
939 || STORE_FLAG_VALUE == -1)
940 && ((if_info->a == const0_rtx
941 && rtx_equal_p (if_info->b, if_info->x))
942 || ((reversep = (reversed_comparison_code (if_info->cond,
943 if_info->jump)
944 != UNKNOWN))
945 && if_info->b == const0_rtx
946 && rtx_equal_p (if_info->a, if_info->x))))
948 start_sequence ();
949 target = noce_emit_store_flag (if_info,
950 gen_reg_rtx (GET_MODE (if_info->x)),
951 reversep, -1);
952 if (target)
953 target = expand_simple_binop (GET_MODE (if_info->x), AND,
954 if_info->x, target, if_info->x, 0,
955 OPTAB_WIDEN);
957 if (target)
959 if (target != if_info->x)
960 noce_emit_move_insn (if_info->x, target);
962 seq = get_insns ();
963 end_sequence ();
965 if (seq_contains_jump (seq))
966 return FALSE;
968 emit_insn_before_scope (seq, if_info->jump,
969 INSN_SCOPE (if_info->insn_a));
971 return TRUE;
974 end_sequence ();
977 return FALSE;
980 /* Helper function for noce_try_cmove and noce_try_cmove_arith. */
982 static rtx
983 noce_emit_cmove (if_info, x, code, cmp_a, cmp_b, vfalse, vtrue)
984 struct noce_if_info *if_info;
985 rtx x, cmp_a, cmp_b, vfalse, vtrue;
986 enum rtx_code code;
988 /* If earliest == jump, try to build the cmove insn directly.
989 This is helpful when combine has created some complex condition
990 (like for alpha's cmovlbs) that we can't hope to regenerate
991 through the normal interface. */
993 if (if_info->cond_earliest == if_info->jump)
995 rtx tmp;
997 tmp = gen_rtx_fmt_ee (code, GET_MODE (if_info->cond), cmp_a, cmp_b);
998 tmp = gen_rtx_IF_THEN_ELSE (GET_MODE (x), tmp, vtrue, vfalse);
999 tmp = gen_rtx_SET (VOIDmode, x, tmp);
1001 start_sequence ();
1002 tmp = emit_insn (tmp);
1004 if (recog_memoized (tmp) >= 0)
1006 tmp = get_insns ();
1007 end_sequence ();
1008 emit_insn (tmp);
1010 return x;
1013 end_sequence ();
1016 /* Don't even try if the comparison operands are weird. */
1017 if (! general_operand (cmp_a, GET_MODE (cmp_a))
1018 || ! general_operand (cmp_b, GET_MODE (cmp_b)))
1019 return NULL_RTX;
1021 #if HAVE_conditional_move
1022 return emit_conditional_move (x, code, cmp_a, cmp_b, VOIDmode,
1023 vtrue, vfalse, GET_MODE (x),
1024 (code == LTU || code == GEU
1025 || code == LEU || code == GTU));
1026 #else
1027 /* We'll never get here, as noce_process_if_block doesn't call the
1028 functions involved. Ifdef code, however, should be discouraged
1029 because it leads to typos in the code not selected. However,
1030 emit_conditional_move won't exist either. */
1031 return NULL_RTX;
1032 #endif
1035 /* Try only simple constants and registers here. More complex cases
1036 are handled in noce_try_cmove_arith after noce_try_store_flag_arith
1037 has had a go at it. */
1039 static int
1040 noce_try_cmove (if_info)
1041 struct noce_if_info *if_info;
1043 enum rtx_code code;
1044 rtx target, seq;
1046 if ((CONSTANT_P (if_info->a) || register_operand (if_info->a, VOIDmode))
1047 && (CONSTANT_P (if_info->b) || register_operand (if_info->b, VOIDmode)))
1049 start_sequence ();
1051 code = GET_CODE (if_info->cond);
1052 target = noce_emit_cmove (if_info, if_info->x, code,
1053 XEXP (if_info->cond, 0),
1054 XEXP (if_info->cond, 1),
1055 if_info->a, if_info->b);
1057 if (target)
1059 if (target != if_info->x)
1060 noce_emit_move_insn (if_info->x, target);
1062 seq = get_insns ();
1063 end_sequence ();
1064 emit_insn_before_scope (seq, if_info->jump,
1065 INSN_SCOPE (if_info->insn_a));
1066 return TRUE;
1068 else
1070 end_sequence ();
1071 return FALSE;
1075 return FALSE;
1078 /* Try more complex cases involving conditional_move. */
1080 static int
1081 noce_try_cmove_arith (if_info)
1082 struct noce_if_info *if_info;
1084 rtx a = if_info->a;
1085 rtx b = if_info->b;
1086 rtx x = if_info->x;
1087 rtx insn_a, insn_b;
1088 rtx tmp, target;
1089 int is_mem = 0;
1090 enum rtx_code code;
1092 /* A conditional move from two memory sources is equivalent to a
1093 conditional on their addresses followed by a load. Don't do this
1094 early because it'll screw alias analysis. Note that we've
1095 already checked for no side effects. */
1096 if (! no_new_pseudos && cse_not_expected
1097 && GET_CODE (a) == MEM && GET_CODE (b) == MEM
1098 && BRANCH_COST >= 5)
1100 a = XEXP (a, 0);
1101 b = XEXP (b, 0);
1102 x = gen_reg_rtx (Pmode);
1103 is_mem = 1;
1106 /* ??? We could handle this if we knew that a load from A or B could
1107 not fault. This is also true if we've already loaded
1108 from the address along the path from ENTRY. */
1109 else if (may_trap_p (a) || may_trap_p (b))
1110 return FALSE;
1112 /* if (test) x = a + b; else x = c - d;
1113 => y = a + b;
1114 x = c - d;
1115 if (test)
1116 x = y;
1119 code = GET_CODE (if_info->cond);
1120 insn_a = if_info->insn_a;
1121 insn_b = if_info->insn_b;
1123 /* Possibly rearrange operands to make things come out more natural. */
1124 if (reversed_comparison_code (if_info->cond, if_info->jump) != UNKNOWN)
1126 int reversep = 0;
1127 if (rtx_equal_p (b, x))
1128 reversep = 1;
1129 else if (general_operand (b, GET_MODE (b)))
1130 reversep = 1;
1132 if (reversep)
1134 code = reversed_comparison_code (if_info->cond, if_info->jump);
1135 tmp = a, a = b, b = tmp;
1136 tmp = insn_a, insn_a = insn_b, insn_b = tmp;
1140 start_sequence ();
1142 /* If either operand is complex, load it into a register first.
1143 The best way to do this is to copy the original insn. In this
1144 way we preserve any clobbers etc that the insn may have had.
1145 This is of course not possible in the IS_MEM case. */
1146 if (! general_operand (a, GET_MODE (a)))
1148 rtx set;
1150 if (no_new_pseudos)
1151 goto end_seq_and_fail;
1153 if (is_mem)
1155 tmp = gen_reg_rtx (GET_MODE (a));
1156 tmp = emit_insn (gen_rtx_SET (VOIDmode, tmp, a));
1158 else if (! insn_a)
1159 goto end_seq_and_fail;
1160 else
1162 a = gen_reg_rtx (GET_MODE (a));
1163 tmp = copy_rtx (insn_a);
1164 set = single_set (tmp);
1165 SET_DEST (set) = a;
1166 tmp = emit_insn (PATTERN (tmp));
1168 if (recog_memoized (tmp) < 0)
1169 goto end_seq_and_fail;
1171 if (! general_operand (b, GET_MODE (b)))
1173 rtx set;
1175 if (no_new_pseudos)
1176 goto end_seq_and_fail;
1178 if (is_mem)
1180 tmp = gen_reg_rtx (GET_MODE (b));
1181 tmp = emit_insn (gen_rtx_SET (VOIDmode, tmp, b));
1183 else if (! insn_b)
1184 goto end_seq_and_fail;
1185 else
1187 b = gen_reg_rtx (GET_MODE (b));
1188 tmp = copy_rtx (insn_b);
1189 set = single_set (tmp);
1190 SET_DEST (set) = b;
1191 tmp = emit_insn (PATTERN (tmp));
1193 if (recog_memoized (tmp) < 0)
1194 goto end_seq_and_fail;
1197 target = noce_emit_cmove (if_info, x, code, XEXP (if_info->cond, 0),
1198 XEXP (if_info->cond, 1), a, b);
1200 if (! target)
1201 goto end_seq_and_fail;
1203 /* If we're handling a memory for above, emit the load now. */
1204 if (is_mem)
1206 tmp = gen_rtx_MEM (GET_MODE (if_info->x), target);
1208 /* Copy over flags as appropriate. */
1209 if (MEM_VOLATILE_P (if_info->a) || MEM_VOLATILE_P (if_info->b))
1210 MEM_VOLATILE_P (tmp) = 1;
1211 if (MEM_IN_STRUCT_P (if_info->a) && MEM_IN_STRUCT_P (if_info->b))
1212 MEM_IN_STRUCT_P (tmp) = 1;
1213 if (MEM_SCALAR_P (if_info->a) && MEM_SCALAR_P (if_info->b))
1214 MEM_SCALAR_P (tmp) = 1;
1215 if (MEM_ALIAS_SET (if_info->a) == MEM_ALIAS_SET (if_info->b))
1216 set_mem_alias_set (tmp, MEM_ALIAS_SET (if_info->a));
1217 set_mem_align (tmp,
1218 MIN (MEM_ALIGN (if_info->a), MEM_ALIGN (if_info->b)));
1220 noce_emit_move_insn (if_info->x, tmp);
1222 else if (target != x)
1223 noce_emit_move_insn (x, target);
1225 tmp = get_insns ();
1226 end_sequence ();
1227 emit_insn_before_scope (tmp, if_info->jump, INSN_SCOPE (if_info->insn_a));
1228 return TRUE;
1230 end_seq_and_fail:
1231 end_sequence ();
1232 return FALSE;
1235 /* For most cases, the simplified condition we found is the best
1236 choice, but this is not the case for the min/max/abs transforms.
1237 For these we wish to know that it is A or B in the condition. */
1239 static rtx
1240 noce_get_alt_condition (if_info, target, earliest)
1241 struct noce_if_info *if_info;
1242 rtx target;
1243 rtx *earliest;
1245 rtx cond, set, insn;
1246 int reverse;
1248 /* If target is already mentioned in the known condition, return it. */
1249 if (reg_mentioned_p (target, if_info->cond))
1251 *earliest = if_info->cond_earliest;
1252 return if_info->cond;
1255 set = pc_set (if_info->jump);
1256 cond = XEXP (SET_SRC (set), 0);
1257 reverse
1258 = GET_CODE (XEXP (SET_SRC (set), 2)) == LABEL_REF
1259 && XEXP (XEXP (SET_SRC (set), 2), 0) == JUMP_LABEL (if_info->jump);
1261 /* If we're looking for a constant, try to make the conditional
1262 have that constant in it. There are two reasons why it may
1263 not have the constant we want:
1265 1. GCC may have needed to put the constant in a register, because
1266 the target can't compare directly against that constant. For
1267 this case, we look for a SET immediately before the comparison
1268 that puts a constant in that register.
1270 2. GCC may have canonicalized the conditional, for example
1271 replacing "if x < 4" with "if x <= 3". We can undo that (or
1272 make equivalent types of changes) to get the constants we need
1273 if they're off by one in the right direction. */
1275 if (GET_CODE (target) == CONST_INT)
1277 enum rtx_code code = GET_CODE (if_info->cond);
1278 rtx op_a = XEXP (if_info->cond, 0);
1279 rtx op_b = XEXP (if_info->cond, 1);
1280 rtx prev_insn;
1282 /* First, look to see if we put a constant in a register. */
1283 prev_insn = PREV_INSN (if_info->cond_earliest);
1284 if (prev_insn
1285 && INSN_P (prev_insn)
1286 && GET_CODE (PATTERN (prev_insn)) == SET)
1288 rtx src = find_reg_equal_equiv_note (prev_insn);
1289 if (!src)
1290 src = SET_SRC (PATTERN (prev_insn));
1291 if (GET_CODE (src) == CONST_INT)
1293 if (rtx_equal_p (op_a, SET_DEST (PATTERN (prev_insn))))
1294 op_a = src;
1295 else if (rtx_equal_p (op_b, SET_DEST (PATTERN (prev_insn))))
1296 op_b = src;
1298 if (GET_CODE (op_a) == CONST_INT)
1300 rtx tmp = op_a;
1301 op_a = op_b;
1302 op_b = tmp;
1303 code = swap_condition (code);
1308 /* Now, look to see if we can get the right constant by
1309 adjusting the conditional. */
1310 if (GET_CODE (op_b) == CONST_INT)
1312 HOST_WIDE_INT desired_val = INTVAL (target);
1313 HOST_WIDE_INT actual_val = INTVAL (op_b);
1315 switch (code)
1317 case LT:
1318 if (actual_val == desired_val + 1)
1320 code = LE;
1321 op_b = GEN_INT (desired_val);
1323 break;
1324 case LE:
1325 if (actual_val == desired_val - 1)
1327 code = LT;
1328 op_b = GEN_INT (desired_val);
1330 break;
1331 case GT:
1332 if (actual_val == desired_val - 1)
1334 code = GE;
1335 op_b = GEN_INT (desired_val);
1337 break;
1338 case GE:
1339 if (actual_val == desired_val + 1)
1341 code = GT;
1342 op_b = GEN_INT (desired_val);
1344 break;
1345 default:
1346 break;
1350 /* If we made any changes, generate a new conditional that is
1351 equivalent to what we started with, but has the right
1352 constants in it. */
1353 if (code != GET_CODE (if_info->cond)
1354 || op_a != XEXP (if_info->cond, 0)
1355 || op_b != XEXP (if_info->cond, 1))
1357 cond = gen_rtx_fmt_ee (code, GET_MODE (cond), op_a, op_b);
1358 *earliest = if_info->cond_earliest;
1359 return cond;
1363 cond = canonicalize_condition (if_info->jump, cond, reverse,
1364 earliest, target);
1365 if (! cond || ! reg_mentioned_p (target, cond))
1366 return NULL;
1368 /* We almost certainly searched back to a different place.
1369 Need to re-verify correct lifetimes. */
1371 /* X may not be mentioned in the range (cond_earliest, jump]. */
1372 for (insn = if_info->jump; insn != *earliest; insn = PREV_INSN (insn))
1373 if (INSN_P (insn) && reg_overlap_mentioned_p (if_info->x, PATTERN (insn)))
1374 return NULL;
1376 /* A and B may not be modified in the range [cond_earliest, jump). */
1377 for (insn = *earliest; insn != if_info->jump; insn = NEXT_INSN (insn))
1378 if (INSN_P (insn)
1379 && (modified_in_p (if_info->a, insn)
1380 || modified_in_p (if_info->b, insn)))
1381 return NULL;
1383 return cond;
1386 /* Convert "if (a < b) x = a; else x = b;" to "x = min(a, b);", etc. */
1388 static int
1389 noce_try_minmax (if_info)
1390 struct noce_if_info *if_info;
1392 rtx cond, earliest, target, seq;
1393 enum rtx_code code, op;
1394 int unsignedp;
1396 /* ??? Can't guarantee that expand_binop won't create pseudos. */
1397 if (no_new_pseudos)
1398 return FALSE;
1400 /* ??? Reject modes with NaNs or signed zeros since we don't know how
1401 they will be resolved with an SMIN/SMAX. It wouldn't be too hard
1402 to get the target to tell us... */
1403 if (HONOR_SIGNED_ZEROS (GET_MODE (if_info->x))
1404 || HONOR_NANS (GET_MODE (if_info->x)))
1405 return FALSE;
1407 cond = noce_get_alt_condition (if_info, if_info->a, &earliest);
1408 if (!cond)
1409 return FALSE;
1411 /* Verify the condition is of the form we expect, and canonicalize
1412 the comparison code. */
1413 code = GET_CODE (cond);
1414 if (rtx_equal_p (XEXP (cond, 0), if_info->a))
1416 if (! rtx_equal_p (XEXP (cond, 1), if_info->b))
1417 return FALSE;
1419 else if (rtx_equal_p (XEXP (cond, 1), if_info->a))
1421 if (! rtx_equal_p (XEXP (cond, 0), if_info->b))
1422 return FALSE;
1423 code = swap_condition (code);
1425 else
1426 return FALSE;
1428 /* Determine what sort of operation this is. Note that the code is for
1429 a taken branch, so the code->operation mapping appears backwards. */
1430 switch (code)
1432 case LT:
1433 case LE:
1434 case UNLT:
1435 case UNLE:
1436 op = SMAX;
1437 unsignedp = 0;
1438 break;
1439 case GT:
1440 case GE:
1441 case UNGT:
1442 case UNGE:
1443 op = SMIN;
1444 unsignedp = 0;
1445 break;
1446 case LTU:
1447 case LEU:
1448 op = UMAX;
1449 unsignedp = 1;
1450 break;
1451 case GTU:
1452 case GEU:
1453 op = UMIN;
1454 unsignedp = 1;
1455 break;
1456 default:
1457 return FALSE;
1460 start_sequence ();
1462 target = expand_simple_binop (GET_MODE (if_info->x), op,
1463 if_info->a, if_info->b,
1464 if_info->x, unsignedp, OPTAB_WIDEN);
1465 if (! target)
1467 end_sequence ();
1468 return FALSE;
1470 if (target != if_info->x)
1471 noce_emit_move_insn (if_info->x, target);
1473 seq = get_insns ();
1474 end_sequence ();
1476 if (seq_contains_jump (seq))
1477 return FALSE;
1479 emit_insn_before_scope (seq, if_info->jump, INSN_SCOPE (if_info->insn_a));
1480 if_info->cond = cond;
1481 if_info->cond_earliest = earliest;
1483 return TRUE;
1486 /* Convert "if (a < 0) x = -a; else x = a;" to "x = abs(a);", etc. */
1488 static int
1489 noce_try_abs (if_info)
1490 struct noce_if_info *if_info;
1492 rtx cond, earliest, target, seq, a, b, c;
1493 int negate;
1495 /* ??? Can't guarantee that expand_binop won't create pseudos. */
1496 if (no_new_pseudos)
1497 return FALSE;
1499 /* Recognize A and B as constituting an ABS or NABS. */
1500 a = if_info->a;
1501 b = if_info->b;
1502 if (GET_CODE (a) == NEG && rtx_equal_p (XEXP (a, 0), b))
1503 negate = 0;
1504 else if (GET_CODE (b) == NEG && rtx_equal_p (XEXP (b, 0), a))
1506 c = a; a = b; b = c;
1507 negate = 1;
1509 else
1510 return FALSE;
1512 cond = noce_get_alt_condition (if_info, b, &earliest);
1513 if (!cond)
1514 return FALSE;
1516 /* Verify the condition is of the form we expect. */
1517 if (rtx_equal_p (XEXP (cond, 0), b))
1518 c = XEXP (cond, 1);
1519 else if (rtx_equal_p (XEXP (cond, 1), b))
1520 c = XEXP (cond, 0);
1521 else
1522 return FALSE;
1524 /* Verify that C is zero. Search backward through the block for
1525 a REG_EQUAL note if necessary. */
1526 if (REG_P (c))
1528 rtx insn, note = NULL;
1529 for (insn = earliest;
1530 insn != if_info->test_bb->head;
1531 insn = PREV_INSN (insn))
1532 if (INSN_P (insn)
1533 && ((note = find_reg_note (insn, REG_EQUAL, c))
1534 || (note = find_reg_note (insn, REG_EQUIV, c))))
1535 break;
1536 if (! note)
1537 return FALSE;
1538 c = XEXP (note, 0);
1540 if (GET_CODE (c) == MEM
1541 && GET_CODE (XEXP (c, 0)) == SYMBOL_REF
1542 && CONSTANT_POOL_ADDRESS_P (XEXP (c, 0)))
1543 c = get_pool_constant (XEXP (c, 0));
1545 /* Work around funny ideas get_condition has wrt canonicalization.
1546 Note that these rtx constants are known to be CONST_INT, and
1547 therefore imply integer comparisons. */
1548 if (c == constm1_rtx && GET_CODE (cond) == GT)
1550 else if (c == const1_rtx && GET_CODE (cond) == LT)
1552 else if (c != CONST0_RTX (GET_MODE (b)))
1553 return FALSE;
1555 /* Determine what sort of operation this is. */
1556 switch (GET_CODE (cond))
1558 case LT:
1559 case LE:
1560 case UNLT:
1561 case UNLE:
1562 negate = !negate;
1563 break;
1564 case GT:
1565 case GE:
1566 case UNGT:
1567 case UNGE:
1568 break;
1569 default:
1570 return FALSE;
1573 start_sequence ();
1575 target = expand_simple_unop (GET_MODE (if_info->x), ABS, b, if_info->x, 0);
1577 /* ??? It's a quandry whether cmove would be better here, especially
1578 for integers. Perhaps combine will clean things up. */
1579 if (target && negate)
1580 target = expand_simple_unop (GET_MODE (target), NEG, target, if_info->x, 0);
1582 if (! target)
1584 end_sequence ();
1585 return FALSE;
1588 if (target != if_info->x)
1589 noce_emit_move_insn (if_info->x, target);
1591 seq = get_insns ();
1592 end_sequence ();
1594 if (seq_contains_jump (seq))
1595 return FALSE;
1597 emit_insn_before_scope (seq, if_info->jump, INSN_SCOPE (if_info->insn_a));
1598 if_info->cond = cond;
1599 if_info->cond_earliest = earliest;
1601 return TRUE;
1604 /* Similar to get_condition, only the resulting condition must be
1605 valid at JUMP, instead of at EARLIEST. */
1607 static rtx
1608 noce_get_condition (jump, earliest)
1609 rtx jump;
1610 rtx *earliest;
1612 rtx cond, set, tmp, insn;
1613 bool reverse;
1615 if (! any_condjump_p (jump))
1616 return NULL_RTX;
1618 set = pc_set (jump);
1620 /* If this branches to JUMP_LABEL when the condition is false,
1621 reverse the condition. */
1622 reverse = (GET_CODE (XEXP (SET_SRC (set), 2)) == LABEL_REF
1623 && XEXP (XEXP (SET_SRC (set), 2), 0) == JUMP_LABEL (jump));
1625 /* If the condition variable is a register and is MODE_INT, accept it. */
1627 cond = XEXP (SET_SRC (set), 0);
1628 tmp = XEXP (cond, 0);
1629 if (REG_P (tmp) && GET_MODE_CLASS (GET_MODE (tmp)) == MODE_INT)
1631 *earliest = jump;
1633 if (reverse)
1634 cond = gen_rtx_fmt_ee (reverse_condition (GET_CODE (cond)),
1635 GET_MODE (cond), tmp, XEXP (cond, 1));
1636 return cond;
1639 /* Otherwise, fall back on canonicalize_condition to do the dirty
1640 work of manipulating MODE_CC values and COMPARE rtx codes. */
1642 tmp = canonicalize_condition (jump, cond, reverse, earliest, NULL_RTX);
1643 if (!tmp)
1644 return NULL_RTX;
1646 /* We are going to insert code before JUMP, not before EARLIEST.
1647 We must therefore be certain that the given condition is valid
1648 at JUMP by virtue of not having been modified since. */
1649 for (insn = *earliest; insn != jump; insn = NEXT_INSN (insn))
1650 if (INSN_P (insn) && modified_in_p (tmp, insn))
1651 break;
1652 if (insn == jump)
1653 return tmp;
1655 /* The condition was modified. See if we can get a partial result
1656 that doesn't follow all the reversals. Perhaps combine can fold
1657 them together later. */
1658 tmp = XEXP (tmp, 0);
1659 if (!REG_P (tmp) || GET_MODE_CLASS (GET_MODE (tmp)) != MODE_INT)
1660 return NULL_RTX;
1661 tmp = canonicalize_condition (jump, cond, reverse, earliest, tmp);
1662 if (!tmp)
1663 return NULL_RTX;
1665 /* For sanity's sake, re-validate the new result. */
1666 for (insn = *earliest; insn != jump; insn = NEXT_INSN (insn))
1667 if (INSN_P (insn) && modified_in_p (tmp, insn))
1668 return NULL_RTX;
1670 return tmp;
1673 /* Return true if OP is ok for if-then-else processing. */
1675 static int
1676 noce_operand_ok (op)
1677 rtx op;
1679 /* We special-case memories, so handle any of them with
1680 no address side effects. */
1681 if (GET_CODE (op) == MEM)
1682 return ! side_effects_p (XEXP (op, 0));
1684 if (side_effects_p (op))
1685 return FALSE;
1687 return ! may_trap_p (op);
1690 /* Given a simple IF-THEN or IF-THEN-ELSE block, attempt to convert it
1691 without using conditional execution. Return TRUE if we were
1692 successful at converting the block. */
1694 static int
1695 noce_process_if_block (ce_info)
1696 struct ce_if_block * ce_info;
1698 basic_block test_bb = ce_info->test_bb; /* test block */
1699 basic_block then_bb = ce_info->then_bb; /* THEN */
1700 basic_block else_bb = ce_info->else_bb; /* ELSE or NULL */
1701 struct noce_if_info if_info;
1702 rtx insn_a, insn_b;
1703 rtx set_a, set_b;
1704 rtx orig_x, x, a, b;
1705 rtx jump, cond;
1707 /* We're looking for patterns of the form
1709 (1) if (...) x = a; else x = b;
1710 (2) x = b; if (...) x = a;
1711 (3) if (...) x = a; // as if with an initial x = x.
1713 The later patterns require jumps to be more expensive.
1715 ??? For future expansion, look for multiple X in such patterns. */
1717 /* If test is comprised of && or || elements, don't handle it unless it is
1718 the special case of && elements without an ELSE block. */
1719 if (ce_info->num_multiple_test_blocks)
1721 if (else_bb || ! ce_info->and_and_p)
1722 return FALSE;
1724 ce_info->test_bb = test_bb = ce_info->last_test_bb;
1725 ce_info->num_multiple_test_blocks = 0;
1726 ce_info->num_and_and_blocks = 0;
1727 ce_info->num_or_or_blocks = 0;
1730 /* If this is not a standard conditional jump, we can't parse it. */
1731 jump = test_bb->end;
1732 cond = noce_get_condition (jump, &if_info.cond_earliest);
1733 if (! cond)
1734 return FALSE;
1736 /* If the conditional jump is more than just a conditional
1737 jump, then we can not do if-conversion on this block. */
1738 if (! onlyjump_p (jump))
1739 return FALSE;
1741 /* We must be comparing objects whose modes imply the size. */
1742 if (GET_MODE (XEXP (cond, 0)) == BLKmode)
1743 return FALSE;
1745 /* Look for one of the potential sets. */
1746 insn_a = first_active_insn (then_bb);
1747 if (! insn_a
1748 || insn_a != last_active_insn (then_bb, FALSE)
1749 || (set_a = single_set (insn_a)) == NULL_RTX)
1750 return FALSE;
1752 x = SET_DEST (set_a);
1753 a = SET_SRC (set_a);
1755 /* Look for the other potential set. Make sure we've got equivalent
1756 destinations. */
1757 /* ??? This is overconservative. Storing to two different mems is
1758 as easy as conditionally computing the address. Storing to a
1759 single mem merely requires a scratch memory to use as one of the
1760 destination addresses; often the memory immediately below the
1761 stack pointer is available for this. */
1762 set_b = NULL_RTX;
1763 if (else_bb)
1765 insn_b = first_active_insn (else_bb);
1766 if (! insn_b
1767 || insn_b != last_active_insn (else_bb, FALSE)
1768 || (set_b = single_set (insn_b)) == NULL_RTX
1769 || ! rtx_equal_p (x, SET_DEST (set_b)))
1770 return FALSE;
1772 else
1774 insn_b = prev_nonnote_insn (if_info.cond_earliest);
1775 if (! insn_b
1776 || GET_CODE (insn_b) != INSN
1777 || (set_b = single_set (insn_b)) == NULL_RTX
1778 || ! rtx_equal_p (x, SET_DEST (set_b))
1779 || reg_overlap_mentioned_p (x, cond)
1780 || reg_overlap_mentioned_p (x, a)
1781 || reg_overlap_mentioned_p (x, SET_SRC (set_b))
1782 || modified_between_p (x, if_info.cond_earliest, NEXT_INSN (jump)))
1783 insn_b = set_b = NULL_RTX;
1785 b = (set_b ? SET_SRC (set_b) : x);
1787 /* Only operate on register destinations, and even then avoid extending
1788 the lifetime of hard registers on small register class machines. */
1789 orig_x = x;
1790 if (GET_CODE (x) != REG
1791 || (SMALL_REGISTER_CLASSES
1792 && REGNO (x) < FIRST_PSEUDO_REGISTER))
1794 if (no_new_pseudos)
1795 return FALSE;
1796 x = gen_reg_rtx (GET_MODE (GET_CODE (x) == STRICT_LOW_PART
1797 ? XEXP (x, 0) : x));
1800 /* Don't operate on sources that may trap or are volatile. */
1801 if (! noce_operand_ok (a) || ! noce_operand_ok (b))
1802 return FALSE;
1804 /* Set up the info block for our subroutines. */
1805 if_info.test_bb = test_bb;
1806 if_info.cond = cond;
1807 if_info.jump = jump;
1808 if_info.insn_a = insn_a;
1809 if_info.insn_b = insn_b;
1810 if_info.x = x;
1811 if_info.a = a;
1812 if_info.b = b;
1814 /* Try optimizations in some approximation of a useful order. */
1815 /* ??? Should first look to see if X is live incoming at all. If it
1816 isn't, we don't need anything but an unconditional set. */
1818 /* Look and see if A and B are really the same. Avoid creating silly
1819 cmove constructs that no one will fix up later. */
1820 if (rtx_equal_p (a, b))
1822 /* If we have an INSN_B, we don't have to create any new rtl. Just
1823 move the instruction that we already have. If we don't have an
1824 INSN_B, that means that A == X, and we've got a noop move. In
1825 that case don't do anything and let the code below delete INSN_A. */
1826 if (insn_b && else_bb)
1828 rtx note;
1830 if (else_bb && insn_b == else_bb->end)
1831 else_bb->end = PREV_INSN (insn_b);
1832 reorder_insns (insn_b, insn_b, PREV_INSN (jump));
1834 /* If there was a REG_EQUAL note, delete it since it may have been
1835 true due to this insn being after a jump. */
1836 if ((note = find_reg_note (insn_b, REG_EQUAL, NULL_RTX)) != 0)
1837 remove_note (insn_b, note);
1839 insn_b = NULL_RTX;
1841 /* If we have "x = b; if (...) x = a;", and x has side-effects, then
1842 x must be executed twice. */
1843 else if (insn_b && side_effects_p (orig_x))
1844 return FALSE;
1846 x = orig_x;
1847 goto success;
1850 if (noce_try_store_flag (&if_info))
1851 goto success;
1852 if (noce_try_minmax (&if_info))
1853 goto success;
1854 if (noce_try_abs (&if_info))
1855 goto success;
1856 if (HAVE_conditional_move
1857 && noce_try_cmove (&if_info))
1858 goto success;
1859 if (! HAVE_conditional_execution)
1861 if (noce_try_store_flag_constants (&if_info))
1862 goto success;
1863 if (noce_try_store_flag_inc (&if_info))
1864 goto success;
1865 if (noce_try_store_flag_mask (&if_info))
1866 goto success;
1867 if (HAVE_conditional_move
1868 && noce_try_cmove_arith (&if_info))
1869 goto success;
1872 return FALSE;
1874 success:
1875 /* The original sets may now be killed. */
1876 delete_insn (insn_a);
1878 /* Several special cases here: First, we may have reused insn_b above,
1879 in which case insn_b is now NULL. Second, we want to delete insn_b
1880 if it came from the ELSE block, because follows the now correct
1881 write that appears in the TEST block. However, if we got insn_b from
1882 the TEST block, it may in fact be loading data needed for the comparison.
1883 We'll let life_analysis remove the insn if it's really dead. */
1884 if (insn_b && else_bb)
1885 delete_insn (insn_b);
1887 /* The new insns will have been inserted immediately before the jump. We
1888 should be able to remove the jump with impunity, but the condition itself
1889 may have been modified by gcse to be shared across basic blocks. */
1890 delete_insn (jump);
1892 /* If we used a temporary, fix it up now. */
1893 if (orig_x != x)
1895 start_sequence ();
1896 noce_emit_move_insn (copy_rtx (orig_x), x);
1897 insn_b = get_insns ();
1898 end_sequence ();
1900 emit_insn_after_scope (insn_b, test_bb->end, INSN_SCOPE (insn_a));
1903 /* Merge the blocks! */
1904 merge_if_block (ce_info);
1906 return TRUE;
1909 /* Attempt to convert an IF-THEN or IF-THEN-ELSE block into
1910 straight line code. Return true if successful. */
1912 static int
1913 process_if_block (ce_info)
1914 struct ce_if_block * ce_info;
1916 if (! reload_completed
1917 && noce_process_if_block (ce_info))
1918 return TRUE;
1920 if (HAVE_conditional_execution && reload_completed)
1922 /* If we have && and || tests, try to first handle combining the && and
1923 || tests into the conditional code, and if that fails, go back and
1924 handle it without the && and ||, which at present handles the && case
1925 if there was no ELSE block. */
1926 if (cond_exec_process_if_block (ce_info, TRUE))
1927 return TRUE;
1929 if (ce_info->num_multiple_test_blocks)
1931 cancel_changes (0);
1933 if (cond_exec_process_if_block (ce_info, FALSE))
1934 return TRUE;
1938 return FALSE;
1941 /* Merge the blocks and mark for local life update. */
1943 static void
1944 merge_if_block (ce_info)
1945 struct ce_if_block * ce_info;
1947 basic_block test_bb = ce_info->test_bb; /* last test block */
1948 basic_block then_bb = ce_info->then_bb; /* THEN */
1949 basic_block else_bb = ce_info->else_bb; /* ELSE or NULL */
1950 basic_block join_bb = ce_info->join_bb; /* join block */
1951 basic_block combo_bb;
1953 /* All block merging is done into the lower block numbers. */
1955 combo_bb = test_bb;
1957 /* Merge any basic blocks to handle && and || subtests. Each of
1958 the blocks are on the fallthru path from the predecessor block. */
1959 if (ce_info->num_multiple_test_blocks > 0)
1961 basic_block bb = test_bb;
1962 basic_block last_test_bb = ce_info->last_test_bb;
1963 basic_block fallthru = block_fallthru (bb);
1967 bb = fallthru;
1968 fallthru = block_fallthru (bb);
1969 if (post_dominators)
1970 delete_from_dominance_info (post_dominators, bb);
1971 merge_blocks_nomove (combo_bb, bb);
1972 num_removed_blocks++;
1974 while (bb != last_test_bb);
1977 /* Merge TEST block into THEN block. Normally the THEN block won't have a
1978 label, but it might if there were || tests. That label's count should be
1979 zero, and it normally should be removed. */
1981 if (then_bb)
1983 if (combo_bb->global_live_at_end)
1984 COPY_REG_SET (combo_bb->global_live_at_end,
1985 then_bb->global_live_at_end);
1986 if (post_dominators)
1987 delete_from_dominance_info (post_dominators, then_bb);
1988 merge_blocks_nomove (combo_bb, then_bb);
1989 num_removed_blocks++;
1992 /* The ELSE block, if it existed, had a label. That label count
1993 will almost always be zero, but odd things can happen when labels
1994 get their addresses taken. */
1995 if (else_bb)
1997 if (post_dominators)
1998 delete_from_dominance_info (post_dominators, else_bb);
1999 merge_blocks_nomove (combo_bb, else_bb);
2000 num_removed_blocks++;
2003 /* If there was no join block reported, that means it was not adjacent
2004 to the others, and so we cannot merge them. */
2006 if (! join_bb)
2008 rtx last = combo_bb->end;
2010 /* The outgoing edge for the current COMBO block should already
2011 be correct. Verify this. */
2012 if (combo_bb->succ == NULL_EDGE)
2014 if (find_reg_note (last, REG_NORETURN, NULL))
2016 else if (GET_CODE (last) == INSN
2017 && GET_CODE (PATTERN (last)) == TRAP_IF
2018 && TRAP_CONDITION (PATTERN (last)) == const_true_rtx)
2020 else
2021 abort ();
2024 /* There should still be something at the end of the THEN or ELSE
2025 blocks taking us to our final destination. */
2026 else if (GET_CODE (last) == JUMP_INSN)
2028 else if (combo_bb->succ->dest == EXIT_BLOCK_PTR
2029 && GET_CODE (last) == CALL_INSN
2030 && SIBLING_CALL_P (last))
2032 else if ((combo_bb->succ->flags & EDGE_EH)
2033 && can_throw_internal (last))
2035 else
2036 abort ();
2039 /* The JOIN block may have had quite a number of other predecessors too.
2040 Since we've already merged the TEST, THEN and ELSE blocks, we should
2041 have only one remaining edge from our if-then-else diamond. If there
2042 is more than one remaining edge, it must come from elsewhere. There
2043 may be zero incoming edges if the THEN block didn't actually join
2044 back up (as with a call to abort). */
2045 else if ((join_bb->pred == NULL
2046 || join_bb->pred->pred_next == NULL)
2047 && join_bb != EXIT_BLOCK_PTR)
2049 /* We can merge the JOIN. */
2050 if (combo_bb->global_live_at_end)
2051 COPY_REG_SET (combo_bb->global_live_at_end,
2052 join_bb->global_live_at_end);
2054 if (post_dominators)
2055 delete_from_dominance_info (post_dominators, join_bb);
2056 merge_blocks_nomove (combo_bb, join_bb);
2057 num_removed_blocks++;
2059 else
2061 /* We cannot merge the JOIN. */
2063 /* The outgoing edge for the current COMBO block should already
2064 be correct. Verify this. */
2065 if (combo_bb->succ->succ_next != NULL_EDGE
2066 || combo_bb->succ->dest != join_bb)
2067 abort ();
2069 /* Remove the jump and cruft from the end of the COMBO block. */
2070 if (join_bb != EXIT_BLOCK_PTR)
2071 tidy_fallthru_edge (combo_bb->succ, combo_bb, join_bb);
2074 num_updated_if_blocks++;
2077 /* Find a block ending in a simple IF condition and try to transform it
2078 in some way. When converting a multi-block condition, put the new code
2079 in the first such block and delete the rest. Return a pointer to this
2080 first block if some transformation was done. Return NULL otherwise. */
2082 static basic_block
2083 find_if_header (test_bb, pass)
2084 basic_block test_bb;
2085 int pass;
2087 ce_if_block_t ce_info;
2088 edge then_edge;
2089 edge else_edge;
2091 /* The kind of block we're looking for has exactly two successors. */
2092 if ((then_edge = test_bb->succ) == NULL_EDGE
2093 || (else_edge = then_edge->succ_next) == NULL_EDGE
2094 || else_edge->succ_next != NULL_EDGE)
2095 return NULL;
2097 /* Neither edge should be abnormal. */
2098 if ((then_edge->flags & EDGE_COMPLEX)
2099 || (else_edge->flags & EDGE_COMPLEX))
2100 return NULL;
2102 /* The THEN edge is canonically the one that falls through. */
2103 if (then_edge->flags & EDGE_FALLTHRU)
2105 else if (else_edge->flags & EDGE_FALLTHRU)
2107 edge e = else_edge;
2108 else_edge = then_edge;
2109 then_edge = e;
2111 else
2112 /* Otherwise this must be a multiway branch of some sort. */
2113 return NULL;
2115 memset ((PTR) &ce_info, '\0', sizeof (ce_info));
2116 ce_info.test_bb = test_bb;
2117 ce_info.then_bb = then_edge->dest;
2118 ce_info.else_bb = else_edge->dest;
2119 ce_info.pass = pass;
2121 #ifdef IFCVT_INIT_EXTRA_FIELDS
2122 IFCVT_INIT_EXTRA_FIELDS (&ce_info);
2123 #endif
2125 if (find_if_block (&ce_info))
2126 goto success;
2128 if (HAVE_trap && HAVE_conditional_trap
2129 && find_cond_trap (test_bb, then_edge, else_edge))
2130 goto success;
2132 if (post_dominators
2133 && (! HAVE_conditional_execution || reload_completed))
2135 if (find_if_case_1 (test_bb, then_edge, else_edge))
2136 goto success;
2137 if (find_if_case_2 (test_bb, then_edge, else_edge))
2138 goto success;
2141 return NULL;
2143 success:
2144 if (rtl_dump_file)
2145 fprintf (rtl_dump_file, "Conversion succeeded on pass %d.\n", pass);
2146 return ce_info.test_bb;
2149 /* Return true if a block has two edges, one of which falls through to the next
2150 block, and the other jumps to a specific block, so that we can tell if the
2151 block is part of an && test or an || test. Returns either -1 or the number
2152 of non-note, non-jump, non-USE/CLOBBER insns in the block. */
2154 static int
2155 block_jumps_and_fallthru_p (cur_bb, target_bb)
2156 basic_block cur_bb;
2157 basic_block target_bb;
2159 edge cur_edge;
2160 int fallthru_p = FALSE;
2161 int jump_p = FALSE;
2162 rtx insn;
2163 rtx end;
2164 int n_insns = 0;
2166 if (!cur_bb || !target_bb)
2167 return -1;
2169 /* If no edges, obviously it doesn't jump or fallthru. */
2170 if (cur_bb->succ == NULL_EDGE)
2171 return FALSE;
2173 for (cur_edge = cur_bb->succ;
2174 cur_edge != NULL_EDGE;
2175 cur_edge = cur_edge->succ_next)
2177 if (cur_edge->flags & EDGE_COMPLEX)
2178 /* Anything complex isn't what we want. */
2179 return -1;
2181 else if (cur_edge->flags & EDGE_FALLTHRU)
2182 fallthru_p = TRUE;
2184 else if (cur_edge->dest == target_bb)
2185 jump_p = TRUE;
2187 else
2188 return -1;
2191 if ((jump_p & fallthru_p) == 0)
2192 return -1;
2194 /* Don't allow calls in the block, since this is used to group && and ||
2195 together for conditional execution support. ??? we should support
2196 conditional execution support across calls for IA-64 some day, but
2197 for now it makes the code simpler. */
2198 end = cur_bb->end;
2199 insn = cur_bb->head;
2201 while (insn != NULL_RTX)
2203 if (GET_CODE (insn) == CALL_INSN)
2204 return -1;
2206 if (INSN_P (insn)
2207 && GET_CODE (insn) != JUMP_INSN
2208 && GET_CODE (PATTERN (insn)) != USE
2209 && GET_CODE (PATTERN (insn)) != CLOBBER)
2210 n_insns++;
2212 if (insn == end)
2213 break;
2215 insn = NEXT_INSN (insn);
2218 return n_insns;
2221 /* Determine if a given basic block heads a simple IF-THEN or IF-THEN-ELSE
2222 block. If so, we'll try to convert the insns to not require the branch.
2223 Return TRUE if we were successful at converting the block. */
2225 static int
2226 find_if_block (ce_info)
2227 struct ce_if_block * ce_info;
2229 basic_block test_bb = ce_info->test_bb;
2230 basic_block then_bb = ce_info->then_bb;
2231 basic_block else_bb = ce_info->else_bb;
2232 basic_block join_bb = NULL_BLOCK;
2233 edge then_succ = then_bb->succ;
2234 edge else_succ = else_bb->succ;
2235 int then_predecessors;
2236 int else_predecessors;
2237 edge cur_edge;
2238 basic_block next;
2240 ce_info->last_test_bb = test_bb;
2242 /* Discover if any fall through predecessors of the current test basic block
2243 were && tests (which jump to the else block) or || tests (which jump to
2244 the then block). */
2245 if (HAVE_conditional_execution && reload_completed
2246 && test_bb->pred != NULL_EDGE
2247 && test_bb->pred->pred_next == NULL_EDGE
2248 && test_bb->pred->flags == EDGE_FALLTHRU)
2250 basic_block bb = test_bb->pred->src;
2251 basic_block target_bb;
2252 int max_insns = MAX_CONDITIONAL_EXECUTE;
2253 int n_insns;
2255 /* Determine if the preceeding block is an && or || block. */
2256 if ((n_insns = block_jumps_and_fallthru_p (bb, else_bb)) >= 0)
2258 ce_info->and_and_p = TRUE;
2259 target_bb = else_bb;
2261 else if ((n_insns = block_jumps_and_fallthru_p (bb, then_bb)) >= 0)
2263 ce_info->and_and_p = FALSE;
2264 target_bb = then_bb;
2266 else
2267 target_bb = NULL_BLOCK;
2269 if (target_bb && n_insns <= max_insns)
2271 int total_insns = 0;
2272 int blocks = 0;
2274 ce_info->last_test_bb = test_bb;
2276 /* Found at least one && or || block, look for more. */
2279 ce_info->test_bb = test_bb = bb;
2280 total_insns += n_insns;
2281 blocks++;
2283 if (bb->pred == NULL_EDGE || bb->pred->pred_next != NULL_EDGE)
2284 break;
2286 bb = bb->pred->src;
2287 n_insns = block_jumps_and_fallthru_p (bb, target_bb);
2289 while (n_insns >= 0 && (total_insns + n_insns) <= max_insns);
2291 ce_info->num_multiple_test_blocks = blocks;
2292 ce_info->num_multiple_test_insns = total_insns;
2294 if (ce_info->and_and_p)
2295 ce_info->num_and_and_blocks = blocks;
2296 else
2297 ce_info->num_or_or_blocks = blocks;
2301 /* Count the number of edges the THEN and ELSE blocks have. */
2302 then_predecessors = 0;
2303 for (cur_edge = then_bb->pred;
2304 cur_edge != NULL_EDGE;
2305 cur_edge = cur_edge->pred_next)
2307 then_predecessors++;
2308 if (cur_edge->flags & EDGE_COMPLEX)
2309 return FALSE;
2312 else_predecessors = 0;
2313 for (cur_edge = else_bb->pred;
2314 cur_edge != NULL_EDGE;
2315 cur_edge = cur_edge->pred_next)
2317 else_predecessors++;
2318 if (cur_edge->flags & EDGE_COMPLEX)
2319 return FALSE;
2322 /* The THEN block of an IF-THEN combo must have exactly one predecessor,
2323 other than any || blocks which jump to the THEN block. */
2324 if ((then_predecessors - ce_info->num_or_or_blocks) != 1)
2325 return FALSE;
2327 /* The THEN block of an IF-THEN combo must have zero or one successors. */
2328 if (then_succ != NULL_EDGE
2329 && (then_succ->succ_next != NULL_EDGE
2330 || (then_succ->flags & EDGE_COMPLEX)))
2331 return FALSE;
2333 /* If the THEN block has no successors, conditional execution can still
2334 make a conditional call. Don't do this unless the ELSE block has
2335 only one incoming edge -- the CFG manipulation is too ugly otherwise.
2336 Check for the last insn of the THEN block being an indirect jump, which
2337 is listed as not having any successors, but confuses the rest of the CE
2338 code processing. ??? we should fix this in the future. */
2339 if (then_succ == NULL)
2341 if (else_bb->pred->pred_next == NULL_EDGE)
2343 rtx last_insn = then_bb->end;
2345 while (last_insn
2346 && GET_CODE (last_insn) == NOTE
2347 && last_insn != then_bb->head)
2348 last_insn = PREV_INSN (last_insn);
2350 if (last_insn
2351 && GET_CODE (last_insn) == JUMP_INSN
2352 && ! simplejump_p (last_insn))
2353 return FALSE;
2355 join_bb = else_bb;
2356 else_bb = NULL_BLOCK;
2358 else
2359 return FALSE;
2362 /* If the THEN block's successor is the other edge out of the TEST block,
2363 then we have an IF-THEN combo without an ELSE. */
2364 else if (then_succ->dest == else_bb)
2366 join_bb = else_bb;
2367 else_bb = NULL_BLOCK;
2370 /* If the THEN and ELSE block meet in a subsequent block, and the ELSE
2371 has exactly one predecessor and one successor, and the outgoing edge
2372 is not complex, then we have an IF-THEN-ELSE combo. */
2373 else if (else_succ != NULL_EDGE
2374 && then_succ->dest == else_succ->dest
2375 && else_bb->pred->pred_next == NULL_EDGE
2376 && else_succ->succ_next == NULL_EDGE
2377 && ! (else_succ->flags & EDGE_COMPLEX))
2378 join_bb = else_succ->dest;
2380 /* Otherwise it is not an IF-THEN or IF-THEN-ELSE combination. */
2381 else
2382 return FALSE;
2384 num_possible_if_blocks++;
2386 if (rtl_dump_file)
2388 fprintf (rtl_dump_file, "\nIF-THEN%s block found, pass %d, start block %d [insn %d], then %d [%d]",
2389 (else_bb) ? "-ELSE" : "",
2390 ce_info->pass,
2391 test_bb->index, (test_bb->head) ? (int)INSN_UID (test_bb->head) : -1,
2392 then_bb->index, (then_bb->head) ? (int)INSN_UID (then_bb->head) : -1);
2394 if (else_bb)
2395 fprintf (rtl_dump_file, ", else %d [%d]",
2396 else_bb->index, (else_bb->head) ? (int)INSN_UID (else_bb->head) : -1);
2398 fprintf (rtl_dump_file, ", join %d [%d]",
2399 join_bb->index, (join_bb->head) ? (int)INSN_UID (join_bb->head) : -1);
2401 if (ce_info->num_multiple_test_blocks > 0)
2402 fprintf (rtl_dump_file, ", %d %s block%s last test %d [%d]",
2403 ce_info->num_multiple_test_blocks,
2404 (ce_info->and_and_p) ? "&&" : "||",
2405 (ce_info->num_multiple_test_blocks == 1) ? "" : "s",
2406 ce_info->last_test_bb->index,
2407 ((ce_info->last_test_bb->head)
2408 ? (int)INSN_UID (ce_info->last_test_bb->head)
2409 : -1));
2411 fputc ('\n', rtl_dump_file);
2414 /* Make sure IF, THEN, and ELSE, blocks are adjacent. Actually, we get the
2415 first condition for free, since we've already asserted that there's a
2416 fallthru edge from IF to THEN. Likewise for the && and || blocks, since
2417 we checked the FALLTHRU flag, those are already adjacent to the last IF
2418 block. */
2419 /* ??? As an enhancement, move the ELSE block. Have to deal with
2420 BLOCK notes, if by no other means than aborting the merge if they
2421 exist. Sticky enough I don't want to think about it now. */
2422 next = then_bb;
2423 if (else_bb && (next = next->next_bb) != else_bb)
2424 return FALSE;
2425 if ((next = next->next_bb) != join_bb && join_bb != EXIT_BLOCK_PTR)
2427 if (else_bb)
2428 join_bb = NULL;
2429 else
2430 return FALSE;
2433 /* Do the real work. */
2434 ce_info->else_bb = else_bb;
2435 ce_info->join_bb = join_bb;
2437 return process_if_block (ce_info);
2440 /* Convert a branch over a trap, or a branch
2441 to a trap, into a conditional trap. */
2443 static int
2444 find_cond_trap (test_bb, then_edge, else_edge)
2445 basic_block test_bb;
2446 edge then_edge, else_edge;
2448 basic_block then_bb = then_edge->dest;
2449 basic_block else_bb = else_edge->dest;
2450 basic_block other_bb, trap_bb;
2451 rtx trap, jump, cond, cond_earliest, seq;
2452 enum rtx_code code;
2454 /* Locate the block with the trap instruction. */
2455 /* ??? While we look for no successors, we really ought to allow
2456 EH successors. Need to fix merge_if_block for that to work. */
2457 if ((trap = block_has_only_trap (then_bb)) != NULL)
2458 trap_bb = then_bb, other_bb = else_bb;
2459 else if ((trap = block_has_only_trap (else_bb)) != NULL)
2460 trap_bb = else_bb, other_bb = then_bb;
2461 else
2462 return FALSE;
2464 if (rtl_dump_file)
2466 fprintf (rtl_dump_file, "\nTRAP-IF block found, start %d, trap %d\n",
2467 test_bb->index, trap_bb->index);
2470 /* If this is not a standard conditional jump, we can't parse it. */
2471 jump = test_bb->end;
2472 cond = noce_get_condition (jump, &cond_earliest);
2473 if (! cond)
2474 return FALSE;
2476 /* If the conditional jump is more than just a conditional jump, then
2477 we can not do if-conversion on this block. */
2478 if (! onlyjump_p (jump))
2479 return FALSE;
2481 /* We must be comparing objects whose modes imply the size. */
2482 if (GET_MODE (XEXP (cond, 0)) == BLKmode)
2483 return FALSE;
2485 /* Reverse the comparison code, if necessary. */
2486 code = GET_CODE (cond);
2487 if (then_bb == trap_bb)
2489 code = reversed_comparison_code (cond, jump);
2490 if (code == UNKNOWN)
2491 return FALSE;
2494 /* Attempt to generate the conditional trap. */
2495 seq = gen_cond_trap (code, XEXP (cond, 0), XEXP (cond, 1),
2496 TRAP_CODE (PATTERN (trap)));
2497 if (seq == NULL)
2498 return FALSE;
2500 /* Emit the new insns before cond_earliest. */
2501 emit_insn_before_scope (seq, cond_earliest, INSN_SCOPE (trap));
2503 /* Delete the trap block if possible. */
2504 remove_edge (trap_bb == then_bb ? then_edge : else_edge);
2505 if (trap_bb->pred == NULL)
2507 if (post_dominators)
2508 delete_from_dominance_info (post_dominators, trap_bb);
2509 flow_delete_block (trap_bb);
2510 num_removed_blocks++;
2513 /* If the non-trap block and the test are now adjacent, merge them.
2514 Otherwise we must insert a direct branch. */
2515 if (test_bb->next_bb == other_bb)
2517 struct ce_if_block new_ce_info;
2518 delete_insn (jump);
2519 memset ((PTR) &new_ce_info, '\0', sizeof (new_ce_info));
2520 new_ce_info.test_bb = test_bb;
2521 new_ce_info.then_bb = NULL;
2522 new_ce_info.else_bb = NULL;
2523 new_ce_info.join_bb = other_bb;
2524 merge_if_block (&new_ce_info);
2526 else
2528 rtx lab, newjump;
2530 lab = JUMP_LABEL (jump);
2531 newjump = emit_jump_insn_after (gen_jump (lab), jump);
2532 LABEL_NUSES (lab) += 1;
2533 JUMP_LABEL (newjump) = lab;
2534 emit_barrier_after (newjump);
2536 delete_insn (jump);
2539 return TRUE;
2542 /* Subroutine of find_cond_trap: if BB contains only a trap insn,
2543 return it. */
2545 static rtx
2546 block_has_only_trap (bb)
2547 basic_block bb;
2549 rtx trap;
2551 /* We're not the exit block. */
2552 if (bb == EXIT_BLOCK_PTR)
2553 return NULL_RTX;
2555 /* The block must have no successors. */
2556 if (bb->succ)
2557 return NULL_RTX;
2559 /* The only instruction in the THEN block must be the trap. */
2560 trap = first_active_insn (bb);
2561 if (! (trap == bb->end
2562 && GET_CODE (PATTERN (trap)) == TRAP_IF
2563 && TRAP_CONDITION (PATTERN (trap)) == const_true_rtx))
2564 return NULL_RTX;
2566 return trap;
2569 /* Look for IF-THEN-ELSE cases in which one of THEN or ELSE is
2570 transformable, but not necessarily the other. There need be no
2571 JOIN block.
2573 Return TRUE if we were successful at converting the block.
2575 Cases we'd like to look at:
2578 if (test) goto over; // x not live
2579 x = a;
2580 goto label;
2581 over:
2583 becomes
2585 x = a;
2586 if (! test) goto label;
2589 if (test) goto E; // x not live
2590 x = big();
2591 goto L;
2593 x = b;
2594 goto M;
2596 becomes
2598 x = b;
2599 if (test) goto M;
2600 x = big();
2601 goto L;
2603 (3) // This one's really only interesting for targets that can do
2604 // multiway branching, e.g. IA-64 BBB bundles. For other targets
2605 // it results in multiple branches on a cache line, which often
2606 // does not sit well with predictors.
2608 if (test1) goto E; // predicted not taken
2609 x = a;
2610 if (test2) goto F;
2613 x = b;
2616 becomes
2618 x = a;
2619 if (test1) goto E;
2620 if (test2) goto F;
2622 Notes:
2624 (A) Don't do (2) if the branch is predicted against the block we're
2625 eliminating. Do it anyway if we can eliminate a branch; this requires
2626 that the sole successor of the eliminated block postdominate the other
2627 side of the if.
2629 (B) With CE, on (3) we can steal from both sides of the if, creating
2631 if (test1) x = a;
2632 if (!test1) x = b;
2633 if (test1) goto J;
2634 if (test2) goto F;
2638 Again, this is most useful if J postdominates.
2640 (C) CE substitutes for helpful life information.
2642 (D) These heuristics need a lot of work. */
2644 /* Tests for case 1 above. */
2646 static int
2647 find_if_case_1 (test_bb, then_edge, else_edge)
2648 basic_block test_bb;
2649 edge then_edge, else_edge;
2651 basic_block then_bb = then_edge->dest;
2652 basic_block else_bb = else_edge->dest, new_bb;
2653 edge then_succ = then_bb->succ;
2654 int then_bb_index;
2656 /* THEN has one successor. */
2657 if (!then_succ || then_succ->succ_next != NULL)
2658 return FALSE;
2660 /* THEN does not fall through, but is not strange either. */
2661 if (then_succ->flags & (EDGE_COMPLEX | EDGE_FALLTHRU))
2662 return FALSE;
2664 /* THEN has one predecessor. */
2665 if (then_bb->pred->pred_next != NULL)
2666 return FALSE;
2668 /* THEN must do something. */
2669 if (forwarder_block_p (then_bb))
2670 return FALSE;
2672 num_possible_if_blocks++;
2673 if (rtl_dump_file)
2674 fprintf (rtl_dump_file,
2675 "\nIF-CASE-1 found, start %d, then %d\n",
2676 test_bb->index, then_bb->index);
2678 /* THEN is small. */
2679 if (count_bb_insns (then_bb) > BRANCH_COST)
2680 return FALSE;
2682 /* Registers set are dead, or are predicable. */
2683 if (! dead_or_predicable (test_bb, then_bb, else_bb,
2684 then_bb->succ->dest, 1))
2685 return FALSE;
2687 /* Conversion went ok, including moving the insns and fixing up the
2688 jump. Adjust the CFG to match. */
2690 bitmap_operation (test_bb->global_live_at_end,
2691 else_bb->global_live_at_start,
2692 then_bb->global_live_at_end, BITMAP_IOR);
2694 new_bb = redirect_edge_and_branch_force (FALLTHRU_EDGE (test_bb), else_bb);
2695 then_bb_index = then_bb->index;
2696 if (post_dominators)
2697 delete_from_dominance_info (post_dominators, then_bb);
2698 flow_delete_block (then_bb);
2700 /* Make rest of code believe that the newly created block is the THEN_BB
2701 block we removed. */
2702 if (new_bb)
2704 new_bb->index = then_bb_index;
2705 BASIC_BLOCK (then_bb_index) = new_bb;
2707 /* We've possibly created jump to next insn, cleanup_cfg will solve that
2708 later. */
2710 num_removed_blocks++;
2711 num_updated_if_blocks++;
2713 return TRUE;
2716 /* Test for case 2 above. */
2718 static int
2719 find_if_case_2 (test_bb, then_edge, else_edge)
2720 basic_block test_bb;
2721 edge then_edge, else_edge;
2723 basic_block then_bb = then_edge->dest;
2724 basic_block else_bb = else_edge->dest;
2725 edge else_succ = else_bb->succ;
2726 rtx note;
2728 /* ELSE has one successor. */
2729 if (!else_succ || else_succ->succ_next != NULL)
2730 return FALSE;
2732 /* ELSE outgoing edge is not complex. */
2733 if (else_succ->flags & EDGE_COMPLEX)
2734 return FALSE;
2736 /* ELSE has one predecessor. */
2737 if (else_bb->pred->pred_next != NULL)
2738 return FALSE;
2740 /* THEN is not EXIT. */
2741 if (then_bb->index < 0)
2742 return FALSE;
2744 /* ELSE is predicted or SUCC(ELSE) postdominates THEN. */
2745 note = find_reg_note (test_bb->end, REG_BR_PROB, NULL_RTX);
2746 if (note && INTVAL (XEXP (note, 0)) >= REG_BR_PROB_BASE / 2)
2748 else if (else_succ->dest->index < 0
2749 || dominated_by_p (post_dominators, then_bb,
2750 else_succ->dest))
2752 else
2753 return FALSE;
2755 num_possible_if_blocks++;
2756 if (rtl_dump_file)
2757 fprintf (rtl_dump_file,
2758 "\nIF-CASE-2 found, start %d, else %d\n",
2759 test_bb->index, else_bb->index);
2761 /* ELSE is small. */
2762 if (count_bb_insns (else_bb) > BRANCH_COST)
2763 return FALSE;
2765 /* Registers set are dead, or are predicable. */
2766 if (! dead_or_predicable (test_bb, else_bb, then_bb, else_succ->dest, 0))
2767 return FALSE;
2769 /* Conversion went ok, including moving the insns and fixing up the
2770 jump. Adjust the CFG to match. */
2772 bitmap_operation (test_bb->global_live_at_end,
2773 then_bb->global_live_at_start,
2774 else_bb->global_live_at_end, BITMAP_IOR);
2776 if (post_dominators)
2777 delete_from_dominance_info (post_dominators, else_bb);
2778 flow_delete_block (else_bb);
2780 num_removed_blocks++;
2781 num_updated_if_blocks++;
2783 /* ??? We may now fallthru from one of THEN's successors into a join
2784 block. Rerun cleanup_cfg? Examine things manually? Wait? */
2786 return TRUE;
2789 /* A subroutine of dead_or_predicable called through for_each_rtx.
2790 Return 1 if a memory is found. */
2792 static int
2793 find_memory (px, data)
2794 rtx *px;
2795 void *data ATTRIBUTE_UNUSED;
2797 return GET_CODE (*px) == MEM;
2800 /* Used by the code above to perform the actual rtl transformations.
2801 Return TRUE if successful.
2803 TEST_BB is the block containing the conditional branch. MERGE_BB
2804 is the block containing the code to manipulate. NEW_DEST is the
2805 label TEST_BB should be branching to after the conversion.
2806 REVERSEP is true if the sense of the branch should be reversed. */
2808 static int
2809 dead_or_predicable (test_bb, merge_bb, other_bb, new_dest, reversep)
2810 basic_block test_bb, merge_bb, other_bb;
2811 basic_block new_dest;
2812 int reversep;
2814 rtx head, end, jump, earliest, old_dest, new_label = NULL_RTX;
2816 jump = test_bb->end;
2818 /* Find the extent of the real code in the merge block. */
2819 head = merge_bb->head;
2820 end = merge_bb->end;
2822 if (GET_CODE (head) == CODE_LABEL)
2823 head = NEXT_INSN (head);
2824 if (GET_CODE (head) == NOTE)
2826 if (head == end)
2828 head = end = NULL_RTX;
2829 goto no_body;
2831 head = NEXT_INSN (head);
2834 if (GET_CODE (end) == JUMP_INSN)
2836 if (head == end)
2838 head = end = NULL_RTX;
2839 goto no_body;
2841 end = PREV_INSN (end);
2844 /* Disable handling dead code by conditional execution if the machine needs
2845 to do anything funny with the tests, etc. */
2846 #ifndef IFCVT_MODIFY_TESTS
2847 if (HAVE_conditional_execution)
2849 /* In the conditional execution case, we have things easy. We know
2850 the condition is reversable. We don't have to check life info,
2851 becase we're going to conditionally execute the code anyway.
2852 All that's left is making sure the insns involved can actually
2853 be predicated. */
2855 rtx cond, prob_val;
2857 cond = cond_exec_get_condition (jump);
2858 if (! cond)
2859 return FALSE;
2861 prob_val = find_reg_note (jump, REG_BR_PROB, NULL_RTX);
2862 if (prob_val)
2863 prob_val = XEXP (prob_val, 0);
2865 if (reversep)
2867 enum rtx_code rev = reversed_comparison_code (cond, jump);
2868 if (rev == UNKNOWN)
2869 return FALSE;
2870 cond = gen_rtx_fmt_ee (rev, GET_MODE (cond), XEXP (cond, 0),
2871 XEXP (cond, 1));
2872 if (prob_val)
2873 prob_val = GEN_INT (REG_BR_PROB_BASE - INTVAL (prob_val));
2876 if (! cond_exec_process_insns ((ce_if_block_t *)0, head, end, cond,
2877 prob_val, 0))
2878 goto cancel;
2880 earliest = jump;
2882 else
2883 #endif
2885 /* In the non-conditional execution case, we have to verify that there
2886 are no trapping operations, no calls, no references to memory, and
2887 that any registers modified are dead at the branch site. */
2889 rtx insn, cond, prev;
2890 regset_head merge_set_head, tmp_head, test_live_head, test_set_head;
2891 regset merge_set, tmp, test_live, test_set;
2892 struct propagate_block_info *pbi;
2893 int i, fail = 0;
2895 /* Check for no calls or trapping operations. */
2896 for (insn = head; ; insn = NEXT_INSN (insn))
2898 if (GET_CODE (insn) == CALL_INSN)
2899 return FALSE;
2900 if (INSN_P (insn))
2902 if (may_trap_p (PATTERN (insn)))
2903 return FALSE;
2905 /* ??? Even non-trapping memories such as stack frame
2906 references must be avoided. For stores, we collect
2907 no lifetime info; for reads, we'd have to assert
2908 true_dependence false against every store in the
2909 TEST range. */
2910 if (for_each_rtx (&PATTERN (insn), find_memory, NULL))
2911 return FALSE;
2913 if (insn == end)
2914 break;
2917 if (! any_condjump_p (jump))
2918 return FALSE;
2920 /* Find the extent of the conditional. */
2921 cond = noce_get_condition (jump, &earliest);
2922 if (! cond)
2923 return FALSE;
2925 /* Collect:
2926 MERGE_SET = set of registers set in MERGE_BB
2927 TEST_LIVE = set of registers live at EARLIEST
2928 TEST_SET = set of registers set between EARLIEST and the
2929 end of the block. */
2931 tmp = INITIALIZE_REG_SET (tmp_head);
2932 merge_set = INITIALIZE_REG_SET (merge_set_head);
2933 test_live = INITIALIZE_REG_SET (test_live_head);
2934 test_set = INITIALIZE_REG_SET (test_set_head);
2936 /* ??? bb->local_set is only valid during calculate_global_regs_live,
2937 so we must recompute usage for MERGE_BB. Not so bad, I suppose,
2938 since we've already asserted that MERGE_BB is small. */
2939 propagate_block (merge_bb, tmp, merge_set, merge_set, 0);
2941 /* For small register class machines, don't lengthen lifetimes of
2942 hard registers before reload. */
2943 if (SMALL_REGISTER_CLASSES && ! reload_completed)
2945 EXECUTE_IF_SET_IN_BITMAP
2946 (merge_set, 0, i,
2948 if (i < FIRST_PSEUDO_REGISTER
2949 && ! fixed_regs[i]
2950 && ! global_regs[i])
2951 fail = 1;
2955 /* For TEST, we're interested in a range of insns, not a whole block.
2956 Moreover, we're interested in the insns live from OTHER_BB. */
2958 COPY_REG_SET (test_live, other_bb->global_live_at_start);
2959 pbi = init_propagate_block_info (test_bb, test_live, test_set, test_set,
2962 for (insn = jump; ; insn = prev)
2964 prev = propagate_one_insn (pbi, insn);
2965 if (insn == earliest)
2966 break;
2969 free_propagate_block_info (pbi);
2971 /* We can perform the transformation if
2972 MERGE_SET & (TEST_SET | TEST_LIVE)
2974 TEST_SET & merge_bb->global_live_at_start
2975 are empty. */
2977 bitmap_operation (tmp, test_set, test_live, BITMAP_IOR);
2978 bitmap_operation (tmp, tmp, merge_set, BITMAP_AND);
2979 EXECUTE_IF_SET_IN_BITMAP(tmp, 0, i, fail = 1);
2981 bitmap_operation (tmp, test_set, merge_bb->global_live_at_start,
2982 BITMAP_AND);
2983 EXECUTE_IF_SET_IN_BITMAP(tmp, 0, i, fail = 1);
2985 FREE_REG_SET (tmp);
2986 FREE_REG_SET (merge_set);
2987 FREE_REG_SET (test_live);
2988 FREE_REG_SET (test_set);
2990 if (fail)
2991 return FALSE;
2994 no_body:
2995 /* We don't want to use normal invert_jump or redirect_jump because
2996 we don't want to delete_insn called. Also, we want to do our own
2997 change group management. */
2999 old_dest = JUMP_LABEL (jump);
3000 if (other_bb != new_dest)
3002 new_label = block_label (new_dest);
3003 if (reversep
3004 ? ! invert_jump_1 (jump, new_label)
3005 : ! redirect_jump_1 (jump, new_label))
3006 goto cancel;
3009 if (! apply_change_group ())
3010 return FALSE;
3012 if (other_bb != new_dest)
3014 if (old_dest)
3015 LABEL_NUSES (old_dest) -= 1;
3016 if (new_label)
3017 LABEL_NUSES (new_label) += 1;
3018 JUMP_LABEL (jump) = new_label;
3019 if (reversep)
3020 invert_br_probabilities (jump);
3022 redirect_edge_succ (BRANCH_EDGE (test_bb), new_dest);
3023 if (reversep)
3025 gcov_type count, probability;
3026 count = BRANCH_EDGE (test_bb)->count;
3027 BRANCH_EDGE (test_bb)->count = FALLTHRU_EDGE (test_bb)->count;
3028 FALLTHRU_EDGE (test_bb)->count = count;
3029 probability = BRANCH_EDGE (test_bb)->probability;
3030 BRANCH_EDGE (test_bb)->probability
3031 = FALLTHRU_EDGE (test_bb)->probability;
3032 FALLTHRU_EDGE (test_bb)->probability = probability;
3033 update_br_prob_note (test_bb);
3037 /* Move the insns out of MERGE_BB to before the branch. */
3038 if (head != NULL)
3040 if (end == merge_bb->end)
3041 merge_bb->end = PREV_INSN (head);
3043 if (squeeze_notes (&head, &end))
3044 return TRUE;
3046 reorder_insns (head, end, PREV_INSN (earliest));
3049 /* Remove the jump and edge if we can. */
3050 if (other_bb == new_dest)
3052 delete_insn (jump);
3053 remove_edge (BRANCH_EDGE (test_bb));
3054 /* ??? Can't merge blocks here, as then_bb is still in use.
3055 At minimum, the merge will get done just before bb-reorder. */
3058 return TRUE;
3060 cancel:
3061 cancel_changes (0);
3062 return FALSE;
3065 /* Main entry point for all if-conversion. */
3067 void
3068 if_convert (x_life_data_ok)
3069 int x_life_data_ok;
3071 basic_block bb;
3072 int pass;
3074 num_possible_if_blocks = 0;
3075 num_updated_if_blocks = 0;
3076 num_removed_blocks = 0;
3077 life_data_ok = (x_life_data_ok != 0);
3079 /* Free up basic_block_for_insn so that we don't have to keep it
3080 up to date, either here or in merge_blocks_nomove. */
3081 free_basic_block_vars (1);
3083 /* Compute postdominators if we think we'll use them. */
3084 post_dominators = NULL;
3085 if (HAVE_conditional_execution || life_data_ok)
3087 post_dominators = calculate_dominance_info (CDI_POST_DOMINATORS);
3089 if (life_data_ok)
3090 clear_bb_flags ();
3092 /* Go through each of the basic blocks looking for things to convert. If we
3093 have conditional execution, we make multiple passes to allow us to handle
3094 IF-THEN{-ELSE} blocks within other IF-THEN{-ELSE} blocks. */
3095 pass = 0;
3098 cond_exec_changed_p = FALSE;
3099 pass++;
3101 #ifdef IFCVT_MULTIPLE_DUMPS
3102 if (rtl_dump_file && pass > 1)
3103 fprintf (rtl_dump_file, "\n\n========== Pass %d ==========\n", pass);
3104 #endif
3106 FOR_EACH_BB (bb)
3108 basic_block new_bb;
3109 while ((new_bb = find_if_header (bb, pass)))
3110 bb = new_bb;
3113 #ifdef IFCVT_MULTIPLE_DUMPS
3114 if (rtl_dump_file && cond_exec_changed_p)
3115 print_rtl_with_bb (rtl_dump_file, get_insns ());
3116 #endif
3118 while (cond_exec_changed_p);
3120 #ifdef IFCVT_MULTIPLE_DUMPS
3121 if (rtl_dump_file)
3122 fprintf (rtl_dump_file, "\n\n========== no more changes\n");
3123 #endif
3125 if (post_dominators)
3126 free_dominance_info (post_dominators);
3128 if (rtl_dump_file)
3129 fflush (rtl_dump_file);
3131 clear_aux_for_blocks ();
3133 /* Rebuild life info for basic blocks that require it. */
3134 if (num_removed_blocks && life_data_ok)
3136 /* If we allocated new pseudos, we must resize the array for sched1. */
3137 if (max_regno < max_reg_num ())
3139 max_regno = max_reg_num ();
3140 allocate_reg_info (max_regno, FALSE, FALSE);
3142 update_life_info_in_dirty_blocks (UPDATE_LIFE_GLOBAL_RM_NOTES,
3143 PROP_DEATH_NOTES | PROP_SCAN_DEAD_CODE
3144 | PROP_KILL_DEAD_CODE);
3147 /* Write the final stats. */
3148 if (rtl_dump_file && num_possible_if_blocks > 0)
3150 fprintf (rtl_dump_file,
3151 "\n%d possible IF blocks searched.\n",
3152 num_possible_if_blocks);
3153 fprintf (rtl_dump_file,
3154 "%d IF blocks converted.\n",
3155 num_updated_if_blocks);
3156 fprintf (rtl_dump_file,
3157 "%d basic blocks deleted.\n\n\n",
3158 num_removed_blocks);
3161 #ifdef ENABLE_CHECKING
3162 verify_flow_info ();
3163 #endif