change a function argument from rtx to rtx_insn *
[official-gcc.git] / gcc / ifcvt.c
blobd0ae494155646de9381b39b7eacd494f4a079ed7
1 /* If-conversion support.
2 Copyright (C) 2000-2015 Free Software Foundation, Inc.
4 This file is part of GCC.
6 GCC is free software; you can redistribute it and/or modify it
7 under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 3, or (at your option)
9 any later version.
11 GCC is distributed in the hope that it will be useful, but WITHOUT
12 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
13 or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
14 License for more details.
16 You should have received a copy of the GNU General Public License
17 along with GCC; see the file COPYING3. If not see
18 <http://www.gnu.org/licenses/>. */
20 #include "config.h"
21 #include "system.h"
22 #include "coretypes.h"
23 #include "backend.h"
24 #include "cfghooks.h"
25 #include "tree.h"
26 #include "rtl.h"
27 #include "df.h"
29 #include "regs.h"
30 #include "flags.h"
31 #include "insn-config.h"
32 #include "recog.h"
33 #include "except.h"
34 #include "cfgrtl.h"
35 #include "cfganal.h"
36 #include "cfgcleanup.h"
37 #include "alias.h"
38 #include "expmed.h"
39 #include "dojump.h"
40 #include "explow.h"
41 #include "calls.h"
42 #include "emit-rtl.h"
43 #include "varasm.h"
44 #include "stmt.h"
45 #include "expr.h"
46 #include "output.h"
47 #include "insn-codes.h"
48 #include "optabs.h"
49 #include "diagnostic-core.h"
50 #include "tm_p.h"
51 #include "cfgloop.h"
52 #include "target.h"
53 #include "tree-pass.h"
54 #include "dbgcnt.h"
55 #include "shrink-wrap.h"
56 #include "rtl-iter.h"
57 #include "ifcvt.h"
59 #ifndef MAX_CONDITIONAL_EXECUTE
60 #define MAX_CONDITIONAL_EXECUTE \
61 (BRANCH_COST (optimize_function_for_speed_p (cfun), false) \
62 + 1)
63 #endif
65 #define IFCVT_MULTIPLE_DUMPS 1
67 #define NULL_BLOCK ((basic_block) NULL)
69 /* True if after combine pass. */
70 static bool ifcvt_after_combine;
72 /* True if the target has the cbranchcc4 optab. */
73 static bool have_cbranchcc4;
75 /* # of IF-THEN or IF-THEN-ELSE blocks we looked at */
76 static int num_possible_if_blocks;
78 /* # of IF-THEN or IF-THEN-ELSE blocks were converted to conditional
79 execution. */
80 static int num_updated_if_blocks;
82 /* # of changes made. */
83 static int num_true_changes;
85 /* Whether conditional execution changes were made. */
86 static int cond_exec_changed_p;
88 /* Forward references. */
89 static int count_bb_insns (const_basic_block);
90 static bool cheap_bb_rtx_cost_p (const_basic_block, int, int);
91 static rtx_insn *first_active_insn (basic_block);
92 static rtx_insn *last_active_insn (basic_block, int);
93 static rtx_insn *find_active_insn_before (basic_block, rtx_insn *);
94 static rtx_insn *find_active_insn_after (basic_block, rtx_insn *);
95 static basic_block block_fallthru (basic_block);
96 static int cond_exec_process_insns (ce_if_block *, rtx_insn *, rtx, rtx, int,
97 int);
98 static rtx cond_exec_get_condition (rtx_insn *);
99 static rtx noce_get_condition (rtx_insn *, rtx_insn **, bool);
100 static int noce_operand_ok (const_rtx);
101 static void merge_if_block (ce_if_block *);
102 static int find_cond_trap (basic_block, edge, edge);
103 static basic_block find_if_header (basic_block, int);
104 static int block_jumps_and_fallthru_p (basic_block, basic_block);
105 static int noce_find_if_block (basic_block, edge, edge, int);
106 static int cond_exec_find_if_block (ce_if_block *);
107 static int find_if_case_1 (basic_block, edge, edge);
108 static int find_if_case_2 (basic_block, edge, edge);
109 static int dead_or_predicable (basic_block, basic_block, basic_block,
110 edge, int);
111 static void noce_emit_move_insn (rtx, rtx);
112 static rtx_insn *block_has_only_trap (basic_block);
114 /* Count the number of non-jump active insns in BB. */
116 static int
117 count_bb_insns (const_basic_block bb)
119 int count = 0;
120 rtx_insn *insn = BB_HEAD (bb);
122 while (1)
124 if (active_insn_p (insn) && !JUMP_P (insn))
125 count++;
127 if (insn == BB_END (bb))
128 break;
129 insn = NEXT_INSN (insn);
132 return count;
135 /* Determine whether the total insn_rtx_cost on non-jump insns in
136 basic block BB is less than MAX_COST. This function returns
137 false if the cost of any instruction could not be estimated.
139 The cost of the non-jump insns in BB is scaled by REG_BR_PROB_BASE
140 as those insns are being speculated. MAX_COST is scaled with SCALE
141 plus a small fudge factor. */
143 static bool
144 cheap_bb_rtx_cost_p (const_basic_block bb, int scale, int max_cost)
146 int count = 0;
147 rtx_insn *insn = BB_HEAD (bb);
148 bool speed = optimize_bb_for_speed_p (bb);
150 /* Set scale to REG_BR_PROB_BASE to void the identical scaling
151 applied to insn_rtx_cost when optimizing for size. Only do
152 this after combine because if-conversion might interfere with
153 passes before combine.
155 Use optimize_function_for_speed_p instead of the pre-defined
156 variable speed to make sure it is set to same value for all
157 basic blocks in one if-conversion transformation. */
158 if (!optimize_function_for_speed_p (cfun) && ifcvt_after_combine)
159 scale = REG_BR_PROB_BASE;
160 /* Our branch probability/scaling factors are just estimates and don't
161 account for cases where we can get speculation for free and other
162 secondary benefits. So we fudge the scale factor to make speculating
163 appear a little more profitable when optimizing for performance. */
164 else
165 scale += REG_BR_PROB_BASE / 8;
168 max_cost *= scale;
170 while (1)
172 if (NONJUMP_INSN_P (insn))
174 int cost = insn_rtx_cost (PATTERN (insn), speed) * REG_BR_PROB_BASE;
175 if (cost == 0)
176 return false;
178 /* If this instruction is the load or set of a "stack" register,
179 such as a floating point register on x87, then the cost of
180 speculatively executing this insn may need to include
181 the additional cost of popping its result off of the
182 register stack. Unfortunately, correctly recognizing and
183 accounting for this additional overhead is tricky, so for
184 now we simply prohibit such speculative execution. */
185 #ifdef STACK_REGS
187 rtx set = single_set (insn);
188 if (set && STACK_REG_P (SET_DEST (set)))
189 return false;
191 #endif
193 count += cost;
194 if (count >= max_cost)
195 return false;
197 else if (CALL_P (insn))
198 return false;
200 if (insn == BB_END (bb))
201 break;
202 insn = NEXT_INSN (insn);
205 return true;
208 /* Return the first non-jump active insn in the basic block. */
210 static rtx_insn *
211 first_active_insn (basic_block bb)
213 rtx_insn *insn = BB_HEAD (bb);
215 if (LABEL_P (insn))
217 if (insn == BB_END (bb))
218 return NULL;
219 insn = NEXT_INSN (insn);
222 while (NOTE_P (insn) || DEBUG_INSN_P (insn))
224 if (insn == BB_END (bb))
225 return NULL;
226 insn = NEXT_INSN (insn);
229 if (JUMP_P (insn))
230 return NULL;
232 return insn;
235 /* Return the last non-jump active (non-jump) insn in the basic block. */
237 static rtx_insn *
238 last_active_insn (basic_block bb, int skip_use_p)
240 rtx_insn *insn = BB_END (bb);
241 rtx_insn *head = BB_HEAD (bb);
243 while (NOTE_P (insn)
244 || JUMP_P (insn)
245 || DEBUG_INSN_P (insn)
246 || (skip_use_p
247 && NONJUMP_INSN_P (insn)
248 && GET_CODE (PATTERN (insn)) == USE))
250 if (insn == head)
251 return NULL;
252 insn = PREV_INSN (insn);
255 if (LABEL_P (insn))
256 return NULL;
258 return insn;
261 /* Return the active insn before INSN inside basic block CURR_BB. */
263 static rtx_insn *
264 find_active_insn_before (basic_block curr_bb, rtx_insn *insn)
266 if (!insn || insn == BB_HEAD (curr_bb))
267 return NULL;
269 while ((insn = PREV_INSN (insn)) != NULL_RTX)
271 if (NONJUMP_INSN_P (insn) || JUMP_P (insn) || CALL_P (insn))
272 break;
274 /* No other active insn all the way to the start of the basic block. */
275 if (insn == BB_HEAD (curr_bb))
276 return NULL;
279 return insn;
282 /* Return the active insn after INSN inside basic block CURR_BB. */
284 static rtx_insn *
285 find_active_insn_after (basic_block curr_bb, rtx_insn *insn)
287 if (!insn || insn == BB_END (curr_bb))
288 return NULL;
290 while ((insn = NEXT_INSN (insn)) != NULL_RTX)
292 if (NONJUMP_INSN_P (insn) || JUMP_P (insn) || CALL_P (insn))
293 break;
295 /* No other active insn all the way to the end of the basic block. */
296 if (insn == BB_END (curr_bb))
297 return NULL;
300 return insn;
303 /* Return the basic block reached by falling though the basic block BB. */
305 static basic_block
306 block_fallthru (basic_block bb)
308 edge e = find_fallthru_edge (bb->succs);
310 return (e) ? e->dest : NULL_BLOCK;
313 /* Return true if RTXs A and B can be safely interchanged. */
315 static bool
316 rtx_interchangeable_p (const_rtx a, const_rtx b)
318 if (!rtx_equal_p (a, b))
319 return false;
321 if (GET_CODE (a) != MEM)
322 return true;
324 /* A dead type-unsafe memory reference is legal, but a live type-unsafe memory
325 reference is not. Interchanging a dead type-unsafe memory reference with
326 a live type-safe one creates a live type-unsafe memory reference, in other
327 words, it makes the program illegal.
328 We check here conservatively whether the two memory references have equal
329 memory attributes. */
331 return mem_attrs_eq_p (get_mem_attrs (a), get_mem_attrs (b));
335 /* Go through a bunch of insns, converting them to conditional
336 execution format if possible. Return TRUE if all of the non-note
337 insns were processed. */
339 static int
340 cond_exec_process_insns (ce_if_block *ce_info ATTRIBUTE_UNUSED,
341 /* if block information */rtx_insn *start,
342 /* first insn to look at */rtx end,
343 /* last insn to look at */rtx test,
344 /* conditional execution test */int prob_val,
345 /* probability of branch taken. */int mod_ok)
347 int must_be_last = FALSE;
348 rtx_insn *insn;
349 rtx xtest;
350 rtx pattern;
352 if (!start || !end)
353 return FALSE;
355 for (insn = start; ; insn = NEXT_INSN (insn))
357 /* dwarf2out can't cope with conditional prologues. */
358 if (NOTE_P (insn) && NOTE_KIND (insn) == NOTE_INSN_PROLOGUE_END)
359 return FALSE;
361 if (NOTE_P (insn) || DEBUG_INSN_P (insn))
362 goto insn_done;
364 gcc_assert (NONJUMP_INSN_P (insn) || CALL_P (insn));
366 /* dwarf2out can't cope with conditional unwind info. */
367 if (RTX_FRAME_RELATED_P (insn))
368 return FALSE;
370 /* Remove USE insns that get in the way. */
371 if (reload_completed && GET_CODE (PATTERN (insn)) == USE)
373 /* ??? Ug. Actually unlinking the thing is problematic,
374 given what we'd have to coordinate with our callers. */
375 SET_INSN_DELETED (insn);
376 goto insn_done;
379 /* Last insn wasn't last? */
380 if (must_be_last)
381 return FALSE;
383 if (modified_in_p (test, insn))
385 if (!mod_ok)
386 return FALSE;
387 must_be_last = TRUE;
390 /* Now build the conditional form of the instruction. */
391 pattern = PATTERN (insn);
392 xtest = copy_rtx (test);
394 /* If this is already a COND_EXEC, rewrite the test to be an AND of the
395 two conditions. */
396 if (GET_CODE (pattern) == COND_EXEC)
398 if (GET_MODE (xtest) != GET_MODE (COND_EXEC_TEST (pattern)))
399 return FALSE;
401 xtest = gen_rtx_AND (GET_MODE (xtest), xtest,
402 COND_EXEC_TEST (pattern));
403 pattern = COND_EXEC_CODE (pattern);
406 pattern = gen_rtx_COND_EXEC (VOIDmode, xtest, pattern);
408 /* If the machine needs to modify the insn being conditionally executed,
409 say for example to force a constant integer operand into a temp
410 register, do so here. */
411 #ifdef IFCVT_MODIFY_INSN
412 IFCVT_MODIFY_INSN (ce_info, pattern, insn);
413 if (! pattern)
414 return FALSE;
415 #endif
417 validate_change (insn, &PATTERN (insn), pattern, 1);
419 if (CALL_P (insn) && prob_val >= 0)
420 validate_change (insn, &REG_NOTES (insn),
421 gen_rtx_INT_LIST ((machine_mode) REG_BR_PROB,
422 prob_val, REG_NOTES (insn)), 1);
424 insn_done:
425 if (insn == end)
426 break;
429 return TRUE;
432 /* Return the condition for a jump. Do not do any special processing. */
434 static rtx
435 cond_exec_get_condition (rtx_insn *jump)
437 rtx test_if, cond;
439 if (any_condjump_p (jump))
440 test_if = SET_SRC (pc_set (jump));
441 else
442 return NULL_RTX;
443 cond = XEXP (test_if, 0);
445 /* If this branches to JUMP_LABEL when the condition is false,
446 reverse the condition. */
447 if (GET_CODE (XEXP (test_if, 2)) == LABEL_REF
448 && LABEL_REF_LABEL (XEXP (test_if, 2)) == JUMP_LABEL (jump))
450 enum rtx_code rev = reversed_comparison_code (cond, jump);
451 if (rev == UNKNOWN)
452 return NULL_RTX;
454 cond = gen_rtx_fmt_ee (rev, GET_MODE (cond), XEXP (cond, 0),
455 XEXP (cond, 1));
458 return cond;
461 /* Given a simple IF-THEN or IF-THEN-ELSE block, attempt to convert it
462 to conditional execution. Return TRUE if we were successful at
463 converting the block. */
465 static int
466 cond_exec_process_if_block (ce_if_block * ce_info,
467 /* if block information */int do_multiple_p)
469 basic_block test_bb = ce_info->test_bb; /* last test block */
470 basic_block then_bb = ce_info->then_bb; /* THEN */
471 basic_block else_bb = ce_info->else_bb; /* ELSE or NULL */
472 rtx test_expr; /* expression in IF_THEN_ELSE that is tested */
473 rtx_insn *then_start; /* first insn in THEN block */
474 rtx_insn *then_end; /* last insn + 1 in THEN block */
475 rtx_insn *else_start = NULL; /* first insn in ELSE block or NULL */
476 rtx_insn *else_end = NULL; /* last insn + 1 in ELSE block */
477 int max; /* max # of insns to convert. */
478 int then_mod_ok; /* whether conditional mods are ok in THEN */
479 rtx true_expr; /* test for else block insns */
480 rtx false_expr; /* test for then block insns */
481 int true_prob_val; /* probability of else block */
482 int false_prob_val; /* probability of then block */
483 rtx_insn *then_last_head = NULL; /* Last match at the head of THEN */
484 rtx_insn *else_last_head = NULL; /* Last match at the head of ELSE */
485 rtx_insn *then_first_tail = NULL; /* First match at the tail of THEN */
486 rtx_insn *else_first_tail = NULL; /* First match at the tail of ELSE */
487 int then_n_insns, else_n_insns, n_insns;
488 enum rtx_code false_code;
489 rtx note;
491 /* If test is comprised of && or || elements, and we've failed at handling
492 all of them together, just use the last test if it is the special case of
493 && elements without an ELSE block. */
494 if (!do_multiple_p && ce_info->num_multiple_test_blocks)
496 if (else_bb || ! ce_info->and_and_p)
497 return FALSE;
499 ce_info->test_bb = test_bb = ce_info->last_test_bb;
500 ce_info->num_multiple_test_blocks = 0;
501 ce_info->num_and_and_blocks = 0;
502 ce_info->num_or_or_blocks = 0;
505 /* Find the conditional jump to the ELSE or JOIN part, and isolate
506 the test. */
507 test_expr = cond_exec_get_condition (BB_END (test_bb));
508 if (! test_expr)
509 return FALSE;
511 /* If the conditional jump is more than just a conditional jump,
512 then we can not do conditional execution conversion on this block. */
513 if (! onlyjump_p (BB_END (test_bb)))
514 return FALSE;
516 /* Collect the bounds of where we're to search, skipping any labels, jumps
517 and notes at the beginning and end of the block. Then count the total
518 number of insns and see if it is small enough to convert. */
519 then_start = first_active_insn (then_bb);
520 then_end = last_active_insn (then_bb, TRUE);
521 then_n_insns = ce_info->num_then_insns = count_bb_insns (then_bb);
522 n_insns = then_n_insns;
523 max = MAX_CONDITIONAL_EXECUTE;
525 if (else_bb)
527 int n_matching;
529 max *= 2;
530 else_start = first_active_insn (else_bb);
531 else_end = last_active_insn (else_bb, TRUE);
532 else_n_insns = ce_info->num_else_insns = count_bb_insns (else_bb);
533 n_insns += else_n_insns;
535 /* Look for matching sequences at the head and tail of the two blocks,
536 and limit the range of insns to be converted if possible. */
537 n_matching = flow_find_cross_jump (then_bb, else_bb,
538 &then_first_tail, &else_first_tail,
539 NULL);
540 if (then_first_tail == BB_HEAD (then_bb))
541 then_start = then_end = NULL;
542 if (else_first_tail == BB_HEAD (else_bb))
543 else_start = else_end = NULL;
545 if (n_matching > 0)
547 if (then_end)
548 then_end = find_active_insn_before (then_bb, then_first_tail);
549 if (else_end)
550 else_end = find_active_insn_before (else_bb, else_first_tail);
551 n_insns -= 2 * n_matching;
554 if (then_start
555 && else_start
556 && then_n_insns > n_matching
557 && else_n_insns > n_matching)
559 int longest_match = MIN (then_n_insns - n_matching,
560 else_n_insns - n_matching);
561 n_matching
562 = flow_find_head_matching_sequence (then_bb, else_bb,
563 &then_last_head,
564 &else_last_head,
565 longest_match);
567 if (n_matching > 0)
569 rtx_insn *insn;
571 /* We won't pass the insns in the head sequence to
572 cond_exec_process_insns, so we need to test them here
573 to make sure that they don't clobber the condition. */
574 for (insn = BB_HEAD (then_bb);
575 insn != NEXT_INSN (then_last_head);
576 insn = NEXT_INSN (insn))
577 if (!LABEL_P (insn) && !NOTE_P (insn)
578 && !DEBUG_INSN_P (insn)
579 && modified_in_p (test_expr, insn))
580 return FALSE;
583 if (then_last_head == then_end)
584 then_start = then_end = NULL;
585 if (else_last_head == else_end)
586 else_start = else_end = NULL;
588 if (n_matching > 0)
590 if (then_start)
591 then_start = find_active_insn_after (then_bb, then_last_head);
592 if (else_start)
593 else_start = find_active_insn_after (else_bb, else_last_head);
594 n_insns -= 2 * n_matching;
599 if (n_insns > max)
600 return FALSE;
602 /* Map test_expr/test_jump into the appropriate MD tests to use on
603 the conditionally executed code. */
605 true_expr = test_expr;
607 false_code = reversed_comparison_code (true_expr, BB_END (test_bb));
608 if (false_code != UNKNOWN)
609 false_expr = gen_rtx_fmt_ee (false_code, GET_MODE (true_expr),
610 XEXP (true_expr, 0), XEXP (true_expr, 1));
611 else
612 false_expr = NULL_RTX;
614 #ifdef IFCVT_MODIFY_TESTS
615 /* If the machine description needs to modify the tests, such as setting a
616 conditional execution register from a comparison, it can do so here. */
617 IFCVT_MODIFY_TESTS (ce_info, true_expr, false_expr);
619 /* See if the conversion failed. */
620 if (!true_expr || !false_expr)
621 goto fail;
622 #endif
624 note = find_reg_note (BB_END (test_bb), REG_BR_PROB, NULL_RTX);
625 if (note)
627 true_prob_val = XINT (note, 0);
628 false_prob_val = REG_BR_PROB_BASE - true_prob_val;
630 else
632 true_prob_val = -1;
633 false_prob_val = -1;
636 /* If we have && or || tests, do them here. These tests are in the adjacent
637 blocks after the first block containing the test. */
638 if (ce_info->num_multiple_test_blocks > 0)
640 basic_block bb = test_bb;
641 basic_block last_test_bb = ce_info->last_test_bb;
643 if (! false_expr)
644 goto fail;
648 rtx_insn *start, *end;
649 rtx t, f;
650 enum rtx_code f_code;
652 bb = block_fallthru (bb);
653 start = first_active_insn (bb);
654 end = last_active_insn (bb, TRUE);
655 if (start
656 && ! cond_exec_process_insns (ce_info, start, end, false_expr,
657 false_prob_val, FALSE))
658 goto fail;
660 /* If the conditional jump is more than just a conditional jump, then
661 we can not do conditional execution conversion on this block. */
662 if (! onlyjump_p (BB_END (bb)))
663 goto fail;
665 /* Find the conditional jump and isolate the test. */
666 t = cond_exec_get_condition (BB_END (bb));
667 if (! t)
668 goto fail;
670 f_code = reversed_comparison_code (t, BB_END (bb));
671 if (f_code == UNKNOWN)
672 goto fail;
674 f = gen_rtx_fmt_ee (f_code, GET_MODE (t), XEXP (t, 0), XEXP (t, 1));
675 if (ce_info->and_and_p)
677 t = gen_rtx_AND (GET_MODE (t), true_expr, t);
678 f = gen_rtx_IOR (GET_MODE (t), false_expr, f);
680 else
682 t = gen_rtx_IOR (GET_MODE (t), true_expr, t);
683 f = gen_rtx_AND (GET_MODE (t), false_expr, f);
686 /* If the machine description needs to modify the tests, such as
687 setting a conditional execution register from a comparison, it can
688 do so here. */
689 #ifdef IFCVT_MODIFY_MULTIPLE_TESTS
690 IFCVT_MODIFY_MULTIPLE_TESTS (ce_info, bb, t, f);
692 /* See if the conversion failed. */
693 if (!t || !f)
694 goto fail;
695 #endif
697 true_expr = t;
698 false_expr = f;
700 while (bb != last_test_bb);
703 /* For IF-THEN-ELSE blocks, we don't allow modifications of the test
704 on then THEN block. */
705 then_mod_ok = (else_bb == NULL_BLOCK);
707 /* Go through the THEN and ELSE blocks converting the insns if possible
708 to conditional execution. */
710 if (then_end
711 && (! false_expr
712 || ! cond_exec_process_insns (ce_info, then_start, then_end,
713 false_expr, false_prob_val,
714 then_mod_ok)))
715 goto fail;
717 if (else_bb && else_end
718 && ! cond_exec_process_insns (ce_info, else_start, else_end,
719 true_expr, true_prob_val, TRUE))
720 goto fail;
722 /* If we cannot apply the changes, fail. Do not go through the normal fail
723 processing, since apply_change_group will call cancel_changes. */
724 if (! apply_change_group ())
726 #ifdef IFCVT_MODIFY_CANCEL
727 /* Cancel any machine dependent changes. */
728 IFCVT_MODIFY_CANCEL (ce_info);
729 #endif
730 return FALSE;
733 #ifdef IFCVT_MODIFY_FINAL
734 /* Do any machine dependent final modifications. */
735 IFCVT_MODIFY_FINAL (ce_info);
736 #endif
738 /* Conversion succeeded. */
739 if (dump_file)
740 fprintf (dump_file, "%d insn%s converted to conditional execution.\n",
741 n_insns, (n_insns == 1) ? " was" : "s were");
743 /* Merge the blocks! If we had matching sequences, make sure to delete one
744 copy at the appropriate location first: delete the copy in the THEN branch
745 for a tail sequence so that the remaining one is executed last for both
746 branches, and delete the copy in the ELSE branch for a head sequence so
747 that the remaining one is executed first for both branches. */
748 if (then_first_tail)
750 rtx_insn *from = then_first_tail;
751 if (!INSN_P (from))
752 from = find_active_insn_after (then_bb, from);
753 delete_insn_chain (from, BB_END (then_bb), false);
755 if (else_last_head)
756 delete_insn_chain (first_active_insn (else_bb), else_last_head, false);
758 merge_if_block (ce_info);
759 cond_exec_changed_p = TRUE;
760 return TRUE;
762 fail:
763 #ifdef IFCVT_MODIFY_CANCEL
764 /* Cancel any machine dependent changes. */
765 IFCVT_MODIFY_CANCEL (ce_info);
766 #endif
768 cancel_changes (0);
769 return FALSE;
772 /* Used by noce_process_if_block to communicate with its subroutines.
774 The subroutines know that A and B may be evaluated freely. They
775 know that X is a register. They should insert new instructions
776 before cond_earliest. */
778 struct noce_if_info
780 /* The basic blocks that make up the IF-THEN-{ELSE-,}JOIN block. */
781 basic_block test_bb, then_bb, else_bb, join_bb;
783 /* The jump that ends TEST_BB. */
784 rtx_insn *jump;
786 /* The jump condition. */
787 rtx cond;
789 /* New insns should be inserted before this one. */
790 rtx_insn *cond_earliest;
792 /* Insns in the THEN and ELSE block. There is always just this
793 one insns in those blocks. The insns are single_set insns.
794 If there was no ELSE block, INSN_B is the last insn before
795 COND_EARLIEST, or NULL_RTX. In the former case, the insn
796 operands are still valid, as if INSN_B was moved down below
797 the jump. */
798 rtx_insn *insn_a, *insn_b;
800 /* The SET_SRC of INSN_A and INSN_B. */
801 rtx a, b;
803 /* The SET_DEST of INSN_A. */
804 rtx x;
806 /* True if this if block is not canonical. In the canonical form of
807 if blocks, the THEN_BB is the block reached via the fallthru edge
808 from TEST_BB. For the noce transformations, we allow the symmetric
809 form as well. */
810 bool then_else_reversed;
812 /* True if the contents of then_bb and else_bb are a
813 simple single set instruction. */
814 bool then_simple;
815 bool else_simple;
817 /* The total rtx cost of the instructions in then_bb and else_bb. */
818 unsigned int then_cost;
819 unsigned int else_cost;
821 /* Estimated cost of the particular branch instruction. */
822 unsigned int branch_cost;
825 static rtx noce_emit_store_flag (struct noce_if_info *, rtx, int, int);
826 static int noce_try_move (struct noce_if_info *);
827 static int noce_try_store_flag (struct noce_if_info *);
828 static int noce_try_addcc (struct noce_if_info *);
829 static int noce_try_store_flag_constants (struct noce_if_info *);
830 static int noce_try_store_flag_mask (struct noce_if_info *);
831 static rtx noce_emit_cmove (struct noce_if_info *, rtx, enum rtx_code, rtx,
832 rtx, rtx, rtx);
833 static int noce_try_cmove (struct noce_if_info *);
834 static int noce_try_cmove_arith (struct noce_if_info *);
835 static rtx noce_get_alt_condition (struct noce_if_info *, rtx, rtx_insn **);
836 static int noce_try_minmax (struct noce_if_info *);
837 static int noce_try_abs (struct noce_if_info *);
838 static int noce_try_sign_mask (struct noce_if_info *);
840 /* Helper function for noce_try_store_flag*. */
842 static rtx
843 noce_emit_store_flag (struct noce_if_info *if_info, rtx x, int reversep,
844 int normalize)
846 rtx cond = if_info->cond;
847 int cond_complex;
848 enum rtx_code code;
850 cond_complex = (! general_operand (XEXP (cond, 0), VOIDmode)
851 || ! general_operand (XEXP (cond, 1), VOIDmode));
853 /* If earliest == jump, or when the condition is complex, try to
854 build the store_flag insn directly. */
856 if (cond_complex)
858 rtx set = pc_set (if_info->jump);
859 cond = XEXP (SET_SRC (set), 0);
860 if (GET_CODE (XEXP (SET_SRC (set), 2)) == LABEL_REF
861 && LABEL_REF_LABEL (XEXP (SET_SRC (set), 2)) == JUMP_LABEL (if_info->jump))
862 reversep = !reversep;
863 if (if_info->then_else_reversed)
864 reversep = !reversep;
867 if (reversep)
868 code = reversed_comparison_code (cond, if_info->jump);
869 else
870 code = GET_CODE (cond);
872 if ((if_info->cond_earliest == if_info->jump || cond_complex)
873 && (normalize == 0 || STORE_FLAG_VALUE == normalize))
875 rtx src = gen_rtx_fmt_ee (code, GET_MODE (x), XEXP (cond, 0),
876 XEXP (cond, 1));
877 rtx set = gen_rtx_SET (x, src);
879 start_sequence ();
880 rtx_insn *insn = emit_insn (set);
882 if (recog_memoized (insn) >= 0)
884 rtx_insn *seq = get_insns ();
885 end_sequence ();
886 emit_insn (seq);
888 if_info->cond_earliest = if_info->jump;
890 return x;
893 end_sequence ();
896 /* Don't even try if the comparison operands or the mode of X are weird. */
897 if (cond_complex || !SCALAR_INT_MODE_P (GET_MODE (x)))
898 return NULL_RTX;
900 return emit_store_flag (x, code, XEXP (cond, 0),
901 XEXP (cond, 1), VOIDmode,
902 (code == LTU || code == LEU
903 || code == GEU || code == GTU), normalize);
906 /* Emit instruction to move an rtx, possibly into STRICT_LOW_PART.
907 X is the destination/target and Y is the value to copy. */
909 static void
910 noce_emit_move_insn (rtx x, rtx y)
912 machine_mode outmode;
913 rtx outer, inner;
914 int bitpos;
916 if (GET_CODE (x) != STRICT_LOW_PART)
918 rtx_insn *seq, *insn;
919 rtx target;
920 optab ot;
922 start_sequence ();
923 /* Check that the SET_SRC is reasonable before calling emit_move_insn,
924 otherwise construct a suitable SET pattern ourselves. */
925 insn = (OBJECT_P (y) || CONSTANT_P (y) || GET_CODE (y) == SUBREG)
926 ? emit_move_insn (x, y)
927 : emit_insn (gen_rtx_SET (x, y));
928 seq = get_insns ();
929 end_sequence ();
931 if (recog_memoized (insn) <= 0)
933 if (GET_CODE (x) == ZERO_EXTRACT)
935 rtx op = XEXP (x, 0);
936 unsigned HOST_WIDE_INT size = INTVAL (XEXP (x, 1));
937 unsigned HOST_WIDE_INT start = INTVAL (XEXP (x, 2));
939 /* store_bit_field expects START to be relative to
940 BYTES_BIG_ENDIAN and adjusts this value for machines with
941 BITS_BIG_ENDIAN != BYTES_BIG_ENDIAN. In order to be able to
942 invoke store_bit_field again it is necessary to have the START
943 value from the first call. */
944 if (BITS_BIG_ENDIAN != BYTES_BIG_ENDIAN)
946 if (MEM_P (op))
947 start = BITS_PER_UNIT - start - size;
948 else
950 gcc_assert (REG_P (op));
951 start = BITS_PER_WORD - start - size;
955 gcc_assert (start < (MEM_P (op) ? BITS_PER_UNIT : BITS_PER_WORD));
956 store_bit_field (op, size, start, 0, 0, GET_MODE (x), y);
957 return;
960 switch (GET_RTX_CLASS (GET_CODE (y)))
962 case RTX_UNARY:
963 ot = code_to_optab (GET_CODE (y));
964 if (ot)
966 start_sequence ();
967 target = expand_unop (GET_MODE (y), ot, XEXP (y, 0), x, 0);
968 if (target != NULL_RTX)
970 if (target != x)
971 emit_move_insn (x, target);
972 seq = get_insns ();
974 end_sequence ();
976 break;
978 case RTX_BIN_ARITH:
979 case RTX_COMM_ARITH:
980 ot = code_to_optab (GET_CODE (y));
981 if (ot)
983 start_sequence ();
984 target = expand_binop (GET_MODE (y), ot,
985 XEXP (y, 0), XEXP (y, 1),
986 x, 0, OPTAB_DIRECT);
987 if (target != NULL_RTX)
989 if (target != x)
990 emit_move_insn (x, target);
991 seq = get_insns ();
993 end_sequence ();
995 break;
997 default:
998 break;
1002 emit_insn (seq);
1003 return;
1006 outer = XEXP (x, 0);
1007 inner = XEXP (outer, 0);
1008 outmode = GET_MODE (outer);
1009 bitpos = SUBREG_BYTE (outer) * BITS_PER_UNIT;
1010 store_bit_field (inner, GET_MODE_BITSIZE (outmode), bitpos,
1011 0, 0, outmode, y);
1014 /* Return the CC reg if it is used in COND. */
1016 static rtx
1017 cc_in_cond (rtx cond)
1019 if (have_cbranchcc4 && cond
1020 && GET_MODE_CLASS (GET_MODE (XEXP (cond, 0))) == MODE_CC)
1021 return XEXP (cond, 0);
1023 return NULL_RTX;
1026 /* Return sequence of instructions generated by if conversion. This
1027 function calls end_sequence() to end the current stream, ensures
1028 that the instructions are unshared, recognizable non-jump insns.
1029 On failure, this function returns a NULL_RTX. */
1031 static rtx_insn *
1032 end_ifcvt_sequence (struct noce_if_info *if_info)
1034 rtx_insn *insn;
1035 rtx_insn *seq = get_insns ();
1036 rtx cc = cc_in_cond (if_info->cond);
1038 set_used_flags (if_info->x);
1039 set_used_flags (if_info->cond);
1040 set_used_flags (if_info->a);
1041 set_used_flags (if_info->b);
1043 for (insn = seq; insn; insn = NEXT_INSN (insn))
1044 set_used_flags (insn);
1046 unshare_all_rtl_in_chain (seq);
1047 end_sequence ();
1049 /* Make sure that all of the instructions emitted are recognizable,
1050 and that we haven't introduced a new jump instruction.
1051 As an exercise for the reader, build a general mechanism that
1052 allows proper placement of required clobbers. */
1053 for (insn = seq; insn; insn = NEXT_INSN (insn))
1054 if (JUMP_P (insn)
1055 || recog_memoized (insn) == -1
1056 /* Make sure new generated code does not clobber CC. */
1057 || (cc && set_of (cc, insn)))
1058 return NULL;
1060 return seq;
1063 /* Return true iff the then and else basic block (if it exists)
1064 consist of a single simple set instruction. */
1066 static bool
1067 noce_simple_bbs (struct noce_if_info *if_info)
1069 if (!if_info->then_simple)
1070 return false;
1072 if (if_info->else_bb)
1073 return if_info->else_simple;
1075 return true;
1078 /* Convert "if (a != b) x = a; else x = b" into "x = a" and
1079 "if (a == b) x = a; else x = b" into "x = b". */
1081 static int
1082 noce_try_move (struct noce_if_info *if_info)
1084 rtx cond = if_info->cond;
1085 enum rtx_code code = GET_CODE (cond);
1086 rtx y;
1087 rtx_insn *seq;
1089 if (code != NE && code != EQ)
1090 return FALSE;
1092 if (!noce_simple_bbs (if_info))
1093 return FALSE;
1095 /* This optimization isn't valid if either A or B could be a NaN
1096 or a signed zero. */
1097 if (HONOR_NANS (if_info->x)
1098 || HONOR_SIGNED_ZEROS (if_info->x))
1099 return FALSE;
1101 /* Check whether the operands of the comparison are A and in
1102 either order. */
1103 if ((rtx_equal_p (if_info->a, XEXP (cond, 0))
1104 && rtx_equal_p (if_info->b, XEXP (cond, 1)))
1105 || (rtx_equal_p (if_info->a, XEXP (cond, 1))
1106 && rtx_equal_p (if_info->b, XEXP (cond, 0))))
1108 if (!rtx_interchangeable_p (if_info->a, if_info->b))
1109 return FALSE;
1111 y = (code == EQ) ? if_info->a : if_info->b;
1113 /* Avoid generating the move if the source is the destination. */
1114 if (! rtx_equal_p (if_info->x, y))
1116 start_sequence ();
1117 noce_emit_move_insn (if_info->x, y);
1118 seq = end_ifcvt_sequence (if_info);
1119 if (!seq)
1120 return FALSE;
1122 emit_insn_before_setloc (seq, if_info->jump,
1123 INSN_LOCATION (if_info->insn_a));
1125 return TRUE;
1127 return FALSE;
1130 /* Convert "if (test) x = 1; else x = 0".
1132 Only try 0 and STORE_FLAG_VALUE here. Other combinations will be
1133 tried in noce_try_store_flag_constants after noce_try_cmove has had
1134 a go at the conversion. */
1136 static int
1137 noce_try_store_flag (struct noce_if_info *if_info)
1139 int reversep;
1140 rtx target;
1141 rtx_insn *seq;
1143 if (!noce_simple_bbs (if_info))
1144 return FALSE;
1146 if (CONST_INT_P (if_info->b)
1147 && INTVAL (if_info->b) == STORE_FLAG_VALUE
1148 && if_info->a == const0_rtx)
1149 reversep = 0;
1150 else if (if_info->b == const0_rtx
1151 && CONST_INT_P (if_info->a)
1152 && INTVAL (if_info->a) == STORE_FLAG_VALUE
1153 && (reversed_comparison_code (if_info->cond, if_info->jump)
1154 != UNKNOWN))
1155 reversep = 1;
1156 else
1157 return FALSE;
1159 start_sequence ();
1161 target = noce_emit_store_flag (if_info, if_info->x, reversep, 0);
1162 if (target)
1164 if (target != if_info->x)
1165 noce_emit_move_insn (if_info->x, target);
1167 seq = end_ifcvt_sequence (if_info);
1168 if (! seq)
1169 return FALSE;
1171 emit_insn_before_setloc (seq, if_info->jump,
1172 INSN_LOCATION (if_info->insn_a));
1173 return TRUE;
1175 else
1177 end_sequence ();
1178 return FALSE;
1182 /* Convert "if (test) x = a; else x = b", for A and B constant.
1183 Also allow A = y + c1, B = y + c2, with a common y between A
1184 and B. */
1186 static int
1187 noce_try_store_flag_constants (struct noce_if_info *if_info)
1189 rtx target;
1190 rtx_insn *seq;
1191 bool reversep;
1192 HOST_WIDE_INT itrue, ifalse, diff, tmp;
1193 int normalize;
1194 bool can_reverse;
1195 machine_mode mode = GET_MODE (if_info->x);;
1196 rtx common = NULL_RTX;
1198 rtx a = if_info->a;
1199 rtx b = if_info->b;
1201 /* Handle cases like x := test ? y + 3 : y + 4. */
1202 if (GET_CODE (a) == PLUS
1203 && GET_CODE (b) == PLUS
1204 && CONST_INT_P (XEXP (a, 1))
1205 && CONST_INT_P (XEXP (b, 1))
1206 && rtx_equal_p (XEXP (a, 0), XEXP (b, 0))
1207 && noce_operand_ok (XEXP (a, 0))
1208 && if_info->branch_cost >= 2)
1210 common = XEXP (a, 0);
1211 a = XEXP (a, 1);
1212 b = XEXP (b, 1);
1215 if (!noce_simple_bbs (if_info))
1216 return FALSE;
1218 if (CONST_INT_P (a)
1219 && CONST_INT_P (b))
1221 ifalse = INTVAL (a);
1222 itrue = INTVAL (b);
1223 bool subtract_flag_p = false;
1225 diff = (unsigned HOST_WIDE_INT) itrue - ifalse;
1226 /* Make sure we can represent the difference between the two values. */
1227 if ((diff > 0)
1228 != ((ifalse < 0) != (itrue < 0) ? ifalse < 0 : ifalse < itrue))
1229 return FALSE;
1231 diff = trunc_int_for_mode (diff, mode);
1233 can_reverse = (reversed_comparison_code (if_info->cond, if_info->jump)
1234 != UNKNOWN);
1236 reversep = false;
1237 if (diff == STORE_FLAG_VALUE || diff == -STORE_FLAG_VALUE)
1239 normalize = 0;
1240 /* We could collapse these cases but it is easier to follow the
1241 diff/STORE_FLAG_VALUE combinations when they are listed
1242 explicitly. */
1244 /* test ? 3 : 4
1245 => 4 + (test != 0). */
1246 if (diff < 0 && STORE_FLAG_VALUE < 0)
1247 reversep = false;
1248 /* test ? 4 : 3
1249 => can_reverse | 4 + (test == 0)
1250 !can_reverse | 3 - (test != 0). */
1251 else if (diff > 0 && STORE_FLAG_VALUE < 0)
1253 reversep = can_reverse;
1254 subtract_flag_p = !can_reverse;
1255 /* If we need to subtract the flag and we have PLUS-immediate
1256 A and B then it is unlikely to be beneficial to play tricks
1257 here. */
1258 if (subtract_flag_p && common)
1259 return FALSE;
1261 /* test ? 3 : 4
1262 => can_reverse | 3 + (test == 0)
1263 !can_reverse | 4 - (test != 0). */
1264 else if (diff < 0 && STORE_FLAG_VALUE > 0)
1266 reversep = can_reverse;
1267 subtract_flag_p = !can_reverse;
1268 /* If we need to subtract the flag and we have PLUS-immediate
1269 A and B then it is unlikely to be beneficial to play tricks
1270 here. */
1271 if (subtract_flag_p && common)
1272 return FALSE;
1274 /* test ? 4 : 3
1275 => 4 + (test != 0). */
1276 else if (diff > 0 && STORE_FLAG_VALUE > 0)
1277 reversep = false;
1278 else
1279 gcc_unreachable ();
1281 else if (ifalse == 0 && exact_log2 (itrue) >= 0
1282 && (STORE_FLAG_VALUE == 1
1283 || if_info->branch_cost >= 2))
1284 normalize = 1;
1285 else if (itrue == 0 && exact_log2 (ifalse) >= 0 && can_reverse
1286 && (STORE_FLAG_VALUE == 1 || if_info->branch_cost >= 2))
1288 normalize = 1;
1289 reversep = true;
1291 else if (itrue == -1
1292 && (STORE_FLAG_VALUE == -1
1293 || if_info->branch_cost >= 2))
1294 normalize = -1;
1295 else if (ifalse == -1 && can_reverse
1296 && (STORE_FLAG_VALUE == -1 || if_info->branch_cost >= 2))
1298 normalize = -1;
1299 reversep = true;
1301 else
1302 return FALSE;
1304 if (reversep)
1306 std::swap (itrue, ifalse);
1307 diff = trunc_int_for_mode (-(unsigned HOST_WIDE_INT) diff, mode);
1310 start_sequence ();
1312 /* If we have x := test ? x + 3 : x + 4 then move the original
1313 x out of the way while we store flags. */
1314 if (common && rtx_equal_p (common, if_info->x))
1316 common = gen_reg_rtx (mode);
1317 noce_emit_move_insn (common, if_info->x);
1320 target = noce_emit_store_flag (if_info, if_info->x, reversep, normalize);
1321 if (! target)
1323 end_sequence ();
1324 return FALSE;
1327 /* if (test) x = 3; else x = 4;
1328 => x = 3 + (test == 0); */
1329 if (diff == STORE_FLAG_VALUE || diff == -STORE_FLAG_VALUE)
1331 /* Add the common part now. This may allow combine to merge this
1332 with the store flag operation earlier into some sort of conditional
1333 increment/decrement if the target allows it. */
1334 if (common)
1335 target = expand_simple_binop (mode, PLUS,
1336 target, common,
1337 target, 0, OPTAB_WIDEN);
1339 /* Always use ifalse here. It should have been swapped with itrue
1340 when appropriate when reversep is true. */
1341 target = expand_simple_binop (mode, subtract_flag_p ? MINUS : PLUS,
1342 gen_int_mode (ifalse, mode), target,
1343 if_info->x, 0, OPTAB_WIDEN);
1345 /* Other cases are not beneficial when the original A and B are PLUS
1346 expressions. */
1347 else if (common)
1349 end_sequence ();
1350 return FALSE;
1352 /* if (test) x = 8; else x = 0;
1353 => x = (test != 0) << 3; */
1354 else if (ifalse == 0 && (tmp = exact_log2 (itrue)) >= 0)
1356 target = expand_simple_binop (mode, ASHIFT,
1357 target, GEN_INT (tmp), if_info->x, 0,
1358 OPTAB_WIDEN);
1361 /* if (test) x = -1; else x = b;
1362 => x = -(test != 0) | b; */
1363 else if (itrue == -1)
1365 target = expand_simple_binop (mode, IOR,
1366 target, gen_int_mode (ifalse, mode),
1367 if_info->x, 0, OPTAB_WIDEN);
1369 else
1371 end_sequence ();
1372 return FALSE;
1375 if (! target)
1377 end_sequence ();
1378 return FALSE;
1381 if (target != if_info->x)
1382 noce_emit_move_insn (if_info->x, target);
1384 seq = end_ifcvt_sequence (if_info);
1385 if (!seq)
1386 return FALSE;
1388 emit_insn_before_setloc (seq, if_info->jump,
1389 INSN_LOCATION (if_info->insn_a));
1390 return TRUE;
1393 return FALSE;
1396 /* Convert "if (test) foo++" into "foo += (test != 0)", and
1397 similarly for "foo--". */
1399 static int
1400 noce_try_addcc (struct noce_if_info *if_info)
1402 rtx target;
1403 rtx_insn *seq;
1404 int subtract, normalize;
1406 if (!noce_simple_bbs (if_info))
1407 return FALSE;
1409 if (GET_CODE (if_info->a) == PLUS
1410 && rtx_equal_p (XEXP (if_info->a, 0), if_info->b)
1411 && (reversed_comparison_code (if_info->cond, if_info->jump)
1412 != UNKNOWN))
1414 rtx cond = if_info->cond;
1415 enum rtx_code code = reversed_comparison_code (cond, if_info->jump);
1417 /* First try to use addcc pattern. */
1418 if (general_operand (XEXP (cond, 0), VOIDmode)
1419 && general_operand (XEXP (cond, 1), VOIDmode))
1421 start_sequence ();
1422 target = emit_conditional_add (if_info->x, code,
1423 XEXP (cond, 0),
1424 XEXP (cond, 1),
1425 VOIDmode,
1426 if_info->b,
1427 XEXP (if_info->a, 1),
1428 GET_MODE (if_info->x),
1429 (code == LTU || code == GEU
1430 || code == LEU || code == GTU));
1431 if (target)
1433 if (target != if_info->x)
1434 noce_emit_move_insn (if_info->x, target);
1436 seq = end_ifcvt_sequence (if_info);
1437 if (!seq)
1438 return FALSE;
1440 emit_insn_before_setloc (seq, if_info->jump,
1441 INSN_LOCATION (if_info->insn_a));
1442 return TRUE;
1444 end_sequence ();
1447 /* If that fails, construct conditional increment or decrement using
1448 setcc. */
1449 if (if_info->branch_cost >= 2
1450 && (XEXP (if_info->a, 1) == const1_rtx
1451 || XEXP (if_info->a, 1) == constm1_rtx))
1453 start_sequence ();
1454 if (STORE_FLAG_VALUE == INTVAL (XEXP (if_info->a, 1)))
1455 subtract = 0, normalize = 0;
1456 else if (-STORE_FLAG_VALUE == INTVAL (XEXP (if_info->a, 1)))
1457 subtract = 1, normalize = 0;
1458 else
1459 subtract = 0, normalize = INTVAL (XEXP (if_info->a, 1));
1462 target = noce_emit_store_flag (if_info,
1463 gen_reg_rtx (GET_MODE (if_info->x)),
1464 1, normalize);
1466 if (target)
1467 target = expand_simple_binop (GET_MODE (if_info->x),
1468 subtract ? MINUS : PLUS,
1469 if_info->b, target, if_info->x,
1470 0, OPTAB_WIDEN);
1471 if (target)
1473 if (target != if_info->x)
1474 noce_emit_move_insn (if_info->x, target);
1476 seq = end_ifcvt_sequence (if_info);
1477 if (!seq)
1478 return FALSE;
1480 emit_insn_before_setloc (seq, if_info->jump,
1481 INSN_LOCATION (if_info->insn_a));
1482 return TRUE;
1484 end_sequence ();
1488 return FALSE;
1491 /* Convert "if (test) x = 0;" to "x &= -(test == 0);" */
1493 static int
1494 noce_try_store_flag_mask (struct noce_if_info *if_info)
1496 rtx target;
1497 rtx_insn *seq;
1498 int reversep;
1500 if (!noce_simple_bbs (if_info))
1501 return FALSE;
1503 reversep = 0;
1504 if ((if_info->branch_cost >= 2
1505 || STORE_FLAG_VALUE == -1)
1506 && ((if_info->a == const0_rtx
1507 && rtx_equal_p (if_info->b, if_info->x))
1508 || ((reversep = (reversed_comparison_code (if_info->cond,
1509 if_info->jump)
1510 != UNKNOWN))
1511 && if_info->b == const0_rtx
1512 && rtx_equal_p (if_info->a, if_info->x))))
1514 start_sequence ();
1515 target = noce_emit_store_flag (if_info,
1516 gen_reg_rtx (GET_MODE (if_info->x)),
1517 reversep, -1);
1518 if (target)
1519 target = expand_simple_binop (GET_MODE (if_info->x), AND,
1520 if_info->x,
1521 target, if_info->x, 0,
1522 OPTAB_WIDEN);
1524 if (target)
1526 int old_cost, new_cost, insn_cost;
1527 int speed_p;
1529 if (target != if_info->x)
1530 noce_emit_move_insn (if_info->x, target);
1532 seq = end_ifcvt_sequence (if_info);
1533 if (!seq)
1534 return FALSE;
1536 speed_p = optimize_bb_for_speed_p (BLOCK_FOR_INSN (if_info->insn_a));
1537 insn_cost = insn_rtx_cost (PATTERN (if_info->insn_a), speed_p);
1538 old_cost = COSTS_N_INSNS (if_info->branch_cost) + insn_cost;
1539 new_cost = seq_cost (seq, speed_p);
1541 if (new_cost > old_cost)
1542 return FALSE;
1544 emit_insn_before_setloc (seq, if_info->jump,
1545 INSN_LOCATION (if_info->insn_a));
1546 return TRUE;
1549 end_sequence ();
1552 return FALSE;
1555 /* Helper function for noce_try_cmove and noce_try_cmove_arith. */
1557 static rtx
1558 noce_emit_cmove (struct noce_if_info *if_info, rtx x, enum rtx_code code,
1559 rtx cmp_a, rtx cmp_b, rtx vfalse, rtx vtrue)
1561 rtx target ATTRIBUTE_UNUSED;
1562 int unsignedp ATTRIBUTE_UNUSED;
1564 /* If earliest == jump, try to build the cmove insn directly.
1565 This is helpful when combine has created some complex condition
1566 (like for alpha's cmovlbs) that we can't hope to regenerate
1567 through the normal interface. */
1569 if (if_info->cond_earliest == if_info->jump)
1571 rtx cond = gen_rtx_fmt_ee (code, GET_MODE (if_info->cond), cmp_a, cmp_b);
1572 rtx if_then_else = gen_rtx_IF_THEN_ELSE (GET_MODE (x),
1573 cond, vtrue, vfalse);
1574 rtx set = gen_rtx_SET (x, if_then_else);
1576 start_sequence ();
1577 rtx_insn *insn = emit_insn (set);
1579 if (recog_memoized (insn) >= 0)
1581 rtx_insn *seq = get_insns ();
1582 end_sequence ();
1583 emit_insn (seq);
1585 return x;
1588 end_sequence ();
1591 /* Don't even try if the comparison operands are weird
1592 except that the target supports cbranchcc4. */
1593 if (! general_operand (cmp_a, GET_MODE (cmp_a))
1594 || ! general_operand (cmp_b, GET_MODE (cmp_b)))
1596 if (!have_cbranchcc4
1597 || GET_MODE_CLASS (GET_MODE (cmp_a)) != MODE_CC
1598 || cmp_b != const0_rtx)
1599 return NULL_RTX;
1602 unsignedp = (code == LTU || code == GEU
1603 || code == LEU || code == GTU);
1605 target = emit_conditional_move (x, code, cmp_a, cmp_b, VOIDmode,
1606 vtrue, vfalse, GET_MODE (x),
1607 unsignedp);
1608 if (target)
1609 return target;
1611 /* We might be faced with a situation like:
1613 x = (reg:M TARGET)
1614 vtrue = (subreg:M (reg:N VTRUE) BYTE)
1615 vfalse = (subreg:M (reg:N VFALSE) BYTE)
1617 We can't do a conditional move in mode M, but it's possible that we
1618 could do a conditional move in mode N instead and take a subreg of
1619 the result.
1621 If we can't create new pseudos, though, don't bother. */
1622 if (reload_completed)
1623 return NULL_RTX;
1625 if (GET_CODE (vtrue) == SUBREG && GET_CODE (vfalse) == SUBREG)
1627 rtx reg_vtrue = SUBREG_REG (vtrue);
1628 rtx reg_vfalse = SUBREG_REG (vfalse);
1629 unsigned int byte_vtrue = SUBREG_BYTE (vtrue);
1630 unsigned int byte_vfalse = SUBREG_BYTE (vfalse);
1631 rtx promoted_target;
1633 if (GET_MODE (reg_vtrue) != GET_MODE (reg_vfalse)
1634 || byte_vtrue != byte_vfalse
1635 || (SUBREG_PROMOTED_VAR_P (vtrue)
1636 != SUBREG_PROMOTED_VAR_P (vfalse))
1637 || (SUBREG_PROMOTED_GET (vtrue)
1638 != SUBREG_PROMOTED_GET (vfalse)))
1639 return NULL_RTX;
1641 promoted_target = gen_reg_rtx (GET_MODE (reg_vtrue));
1643 target = emit_conditional_move (promoted_target, code, cmp_a, cmp_b,
1644 VOIDmode, reg_vtrue, reg_vfalse,
1645 GET_MODE (reg_vtrue), unsignedp);
1646 /* Nope, couldn't do it in that mode either. */
1647 if (!target)
1648 return NULL_RTX;
1650 target = gen_rtx_SUBREG (GET_MODE (vtrue), promoted_target, byte_vtrue);
1651 SUBREG_PROMOTED_VAR_P (target) = SUBREG_PROMOTED_VAR_P (vtrue);
1652 SUBREG_PROMOTED_SET (target, SUBREG_PROMOTED_GET (vtrue));
1653 emit_move_insn (x, target);
1654 return x;
1656 else
1657 return NULL_RTX;
1660 /* Try only simple constants and registers here. More complex cases
1661 are handled in noce_try_cmove_arith after noce_try_store_flag_arith
1662 has had a go at it. */
1664 static int
1665 noce_try_cmove (struct noce_if_info *if_info)
1667 enum rtx_code code;
1668 rtx target;
1669 rtx_insn *seq;
1671 if (!noce_simple_bbs (if_info))
1672 return FALSE;
1674 if ((CONSTANT_P (if_info->a) || register_operand (if_info->a, VOIDmode))
1675 && (CONSTANT_P (if_info->b) || register_operand (if_info->b, VOIDmode)))
1677 start_sequence ();
1679 code = GET_CODE (if_info->cond);
1680 target = noce_emit_cmove (if_info, if_info->x, code,
1681 XEXP (if_info->cond, 0),
1682 XEXP (if_info->cond, 1),
1683 if_info->a, if_info->b);
1685 if (target)
1687 if (target != if_info->x)
1688 noce_emit_move_insn (if_info->x, target);
1690 seq = end_ifcvt_sequence (if_info);
1691 if (!seq)
1692 return FALSE;
1694 emit_insn_before_setloc (seq, if_info->jump,
1695 INSN_LOCATION (if_info->insn_a));
1696 return TRUE;
1698 /* If both a and b are constants try a last-ditch transformation:
1699 if (test) x = a; else x = b;
1700 => x = (-(test != 0) & (b - a)) + a;
1701 Try this only if the target-specific expansion above has failed.
1702 The target-specific expander may want to generate sequences that
1703 we don't know about, so give them a chance before trying this
1704 approach. */
1705 else if (!targetm.have_conditional_execution ()
1706 && CONST_INT_P (if_info->a) && CONST_INT_P (if_info->b)
1707 && ((if_info->branch_cost >= 2 && STORE_FLAG_VALUE == -1)
1708 || if_info->branch_cost >= 3))
1710 machine_mode mode = GET_MODE (if_info->x);
1711 HOST_WIDE_INT ifalse = INTVAL (if_info->a);
1712 HOST_WIDE_INT itrue = INTVAL (if_info->b);
1713 rtx target = noce_emit_store_flag (if_info, if_info->x, false, -1);
1714 if (!target)
1716 end_sequence ();
1717 return FALSE;
1720 HOST_WIDE_INT diff = (unsigned HOST_WIDE_INT) itrue - ifalse;
1721 /* Make sure we can represent the difference
1722 between the two values. */
1723 if ((diff > 0)
1724 != ((ifalse < 0) != (itrue < 0) ? ifalse < 0 : ifalse < itrue))
1726 end_sequence ();
1727 return FALSE;
1730 diff = trunc_int_for_mode (diff, mode);
1731 target = expand_simple_binop (mode, AND,
1732 target, gen_int_mode (diff, mode),
1733 if_info->x, 0, OPTAB_WIDEN);
1734 if (target)
1735 target = expand_simple_binop (mode, PLUS,
1736 target, gen_int_mode (ifalse, mode),
1737 if_info->x, 0, OPTAB_WIDEN);
1738 if (target)
1740 if (target != if_info->x)
1741 noce_emit_move_insn (if_info->x, target);
1743 seq = end_ifcvt_sequence (if_info);
1744 if (!seq)
1745 return FALSE;
1747 emit_insn_before_setloc (seq, if_info->jump,
1748 INSN_LOCATION (if_info->insn_a));
1749 return TRUE;
1751 else
1753 end_sequence ();
1754 return FALSE;
1757 else
1758 end_sequence ();
1761 return FALSE;
1764 /* Return true if X contains a conditional code mode rtx. */
1766 static bool
1767 contains_ccmode_rtx_p (rtx x)
1769 subrtx_iterator::array_type array;
1770 FOR_EACH_SUBRTX (iter, array, x, ALL)
1771 if (GET_MODE_CLASS (GET_MODE (*iter)) == MODE_CC)
1772 return true;
1774 return false;
1777 /* Helper for bb_valid_for_noce_process_p. Validate that
1778 the rtx insn INSN is a single set that does not set
1779 the conditional register CC and is in general valid for
1780 if-conversion. */
1782 static bool
1783 insn_valid_noce_process_p (rtx_insn *insn, rtx cc)
1785 if (!insn
1786 || !NONJUMP_INSN_P (insn)
1787 || (cc && set_of (cc, insn)))
1788 return false;
1790 rtx sset = single_set (insn);
1792 /* Currently support only simple single sets in test_bb. */
1793 if (!sset
1794 || !noce_operand_ok (SET_DEST (sset))
1795 || contains_ccmode_rtx_p (SET_DEST (sset))
1796 || !noce_operand_ok (SET_SRC (sset)))
1797 return false;
1799 return true;
1803 /* Return true iff the registers that the insns in BB_A set do not
1804 get used in BB_B. */
1806 static bool
1807 bbs_ok_for_cmove_arith (basic_block bb_a, basic_block bb_b)
1809 rtx_insn *a_insn;
1810 bitmap bba_sets = BITMAP_ALLOC (&reg_obstack);
1812 df_ref def;
1813 df_ref use;
1815 FOR_BB_INSNS (bb_a, a_insn)
1817 if (!active_insn_p (a_insn))
1818 continue;
1820 rtx sset_a = single_set (a_insn);
1822 if (!sset_a)
1824 BITMAP_FREE (bba_sets);
1825 return false;
1828 /* Record all registers that BB_A sets. */
1829 FOR_EACH_INSN_DEF (def, a_insn)
1830 bitmap_set_bit (bba_sets, DF_REF_REGNO (def));
1833 rtx_insn *b_insn;
1835 FOR_BB_INSNS (bb_b, b_insn)
1837 if (!active_insn_p (b_insn))
1838 continue;
1840 rtx sset_b = single_set (b_insn);
1842 if (!sset_b)
1844 BITMAP_FREE (bba_sets);
1845 return false;
1848 /* Make sure this is a REG and not some instance
1849 of ZERO_EXTRACT or SUBREG or other dangerous stuff. */
1850 if (!REG_P (SET_DEST (sset_b)))
1852 BITMAP_FREE (bba_sets);
1853 return false;
1856 /* If the insn uses a reg set in BB_A return false. */
1857 FOR_EACH_INSN_USE (use, b_insn)
1859 if (bitmap_bit_p (bba_sets, DF_REF_REGNO (use)))
1861 BITMAP_FREE (bba_sets);
1862 return false;
1868 BITMAP_FREE (bba_sets);
1869 return true;
1872 /* Emit copies of all the active instructions in BB except the last.
1873 This is a helper for noce_try_cmove_arith. */
1875 static void
1876 noce_emit_all_but_last (basic_block bb)
1878 rtx_insn *last = last_active_insn (bb, FALSE);
1879 rtx_insn *insn;
1880 FOR_BB_INSNS (bb, insn)
1882 if (insn != last && active_insn_p (insn))
1884 rtx_insn *to_emit = as_a <rtx_insn *> (copy_rtx (insn));
1886 emit_insn (PATTERN (to_emit));
1891 /* Helper for noce_try_cmove_arith. Emit the pattern TO_EMIT and return
1892 the resulting insn or NULL if it's not a valid insn. */
1894 static rtx_insn *
1895 noce_emit_insn (rtx to_emit)
1897 gcc_assert (to_emit);
1898 rtx_insn *insn = emit_insn (to_emit);
1900 if (recog_memoized (insn) < 0)
1901 return NULL;
1903 return insn;
1906 /* Helper for noce_try_cmove_arith. Emit a copy of the insns up to
1907 and including the penultimate one in BB if it is not simple
1908 (as indicated by SIMPLE). Then emit LAST_INSN as the last
1909 insn in the block. The reason for that is that LAST_INSN may
1910 have been modified by the preparation in noce_try_cmove_arith. */
1912 static bool
1913 noce_emit_bb (rtx last_insn, basic_block bb, bool simple)
1915 if (bb && !simple)
1916 noce_emit_all_but_last (bb);
1918 if (last_insn && !noce_emit_insn (last_insn))
1919 return false;
1921 return true;
1924 /* Try more complex cases involving conditional_move. */
1926 static int
1927 noce_try_cmove_arith (struct noce_if_info *if_info)
1929 rtx a = if_info->a;
1930 rtx b = if_info->b;
1931 rtx x = if_info->x;
1932 rtx orig_a, orig_b;
1933 rtx_insn *insn_a, *insn_b;
1934 bool a_simple = if_info->then_simple;
1935 bool b_simple = if_info->else_simple;
1936 basic_block then_bb = if_info->then_bb;
1937 basic_block else_bb = if_info->else_bb;
1938 rtx target;
1939 int is_mem = 0;
1940 enum rtx_code code;
1941 rtx_insn *ifcvt_seq;
1943 /* A conditional move from two memory sources is equivalent to a
1944 conditional on their addresses followed by a load. Don't do this
1945 early because it'll screw alias analysis. Note that we've
1946 already checked for no side effects. */
1947 /* ??? FIXME: Magic number 5. */
1948 if (cse_not_expected
1949 && MEM_P (a) && MEM_P (b)
1950 && MEM_ADDR_SPACE (a) == MEM_ADDR_SPACE (b)
1951 && if_info->branch_cost >= 5)
1953 machine_mode address_mode = get_address_mode (a);
1955 a = XEXP (a, 0);
1956 b = XEXP (b, 0);
1957 x = gen_reg_rtx (address_mode);
1958 is_mem = 1;
1961 /* ??? We could handle this if we knew that a load from A or B could
1962 not trap or fault. This is also true if we've already loaded
1963 from the address along the path from ENTRY. */
1964 else if (may_trap_or_fault_p (a) || may_trap_or_fault_p (b))
1965 return FALSE;
1967 /* if (test) x = a + b; else x = c - d;
1968 => y = a + b;
1969 x = c - d;
1970 if (test)
1971 x = y;
1974 code = GET_CODE (if_info->cond);
1975 insn_a = if_info->insn_a;
1976 insn_b = if_info->insn_b;
1978 machine_mode x_mode = GET_MODE (x);
1980 if (!can_conditionally_move_p (x_mode))
1981 return FALSE;
1983 unsigned int then_cost;
1984 unsigned int else_cost;
1985 if (insn_a)
1986 then_cost = if_info->then_cost;
1987 else
1988 then_cost = 0;
1990 if (insn_b)
1991 else_cost = if_info->else_cost;
1992 else
1993 else_cost = 0;
1995 /* We're going to execute one of the basic blocks anyway, so
1996 bail out if the most expensive of the two blocks is unacceptable. */
1997 if (MAX (then_cost, else_cost) > COSTS_N_INSNS (if_info->branch_cost))
1998 return FALSE;
2000 /* Possibly rearrange operands to make things come out more natural. */
2001 if (reversed_comparison_code (if_info->cond, if_info->jump) != UNKNOWN)
2003 int reversep = 0;
2004 if (rtx_equal_p (b, x))
2005 reversep = 1;
2006 else if (general_operand (b, GET_MODE (b)))
2007 reversep = 1;
2009 if (reversep)
2011 code = reversed_comparison_code (if_info->cond, if_info->jump);
2012 std::swap (a, b);
2013 std::swap (insn_a, insn_b);
2014 std::swap (a_simple, b_simple);
2015 std::swap (then_bb, else_bb);
2019 if (then_bb && else_bb && !a_simple && !b_simple
2020 && (!bbs_ok_for_cmove_arith (then_bb, else_bb)
2021 || !bbs_ok_for_cmove_arith (else_bb, then_bb)))
2022 return FALSE;
2024 start_sequence ();
2026 /* If one of the blocks is empty then the corresponding B or A value
2027 came from the test block. The non-empty complex block that we will
2028 emit might clobber the register used by B or A, so move it to a pseudo
2029 first. */
2031 if (b_simple || !else_bb)
2033 rtx tmp_b = gen_reg_rtx (x_mode);
2034 /* Perform the simplest kind of set. The register allocator
2035 should remove it if it's not actually needed. If this set is not
2036 a valid insn (can happen on the is_mem path) then end_ifcvt_sequence
2037 will cancel the whole sequence. Don't try any of the fallback paths
2038 from noce_emit_move_insn since we want this to be the simplest kind
2039 of move. */
2040 emit_insn (gen_rtx_SET (tmp_b, b));
2041 b = tmp_b;
2044 if (a_simple || !then_bb)
2046 rtx tmp_a = gen_reg_rtx (x_mode);
2047 emit_insn (gen_rtx_SET (tmp_a, a));
2048 a = tmp_a;
2051 orig_a = a;
2052 orig_b = b;
2054 rtx emit_a = NULL_RTX;
2055 rtx emit_b = NULL_RTX;
2057 /* If either operand is complex, load it into a register first.
2058 The best way to do this is to copy the original insn. In this
2059 way we preserve any clobbers etc that the insn may have had.
2060 This is of course not possible in the IS_MEM case. */
2062 if (! general_operand (a, GET_MODE (a)))
2065 if (is_mem)
2067 rtx reg = gen_reg_rtx (GET_MODE (a));
2068 emit_a = gen_rtx_SET (reg, a);
2070 else if (! insn_a)
2071 goto end_seq_and_fail;
2072 else
2074 a = gen_reg_rtx (GET_MODE (a));
2075 rtx_insn *copy_of_a = as_a <rtx_insn *> (copy_rtx (insn_a));
2076 rtx set = single_set (copy_of_a);
2077 SET_DEST (set) = a;
2079 emit_a = PATTERN (copy_of_a);
2083 if (! general_operand (b, GET_MODE (b)))
2085 if (is_mem)
2087 rtx reg = gen_reg_rtx (GET_MODE (b));
2088 emit_b = gen_rtx_SET (reg, b);
2090 else if (! insn_b)
2091 goto end_seq_and_fail;
2092 else
2094 b = gen_reg_rtx (GET_MODE (b));
2095 rtx_insn *copy_of_b = as_a <rtx_insn *> (copy_rtx (insn_b));
2096 rtx set = single_set (copy_of_b);
2098 SET_DEST (set) = b;
2099 emit_b = PATTERN (copy_of_b);
2103 /* If insn to set up A clobbers any registers B depends on, try to
2104 swap insn that sets up A with the one that sets up B. If even
2105 that doesn't help, punt. */
2107 if (emit_a && modified_in_p (orig_b, emit_a))
2109 if (modified_in_p (orig_a, emit_b))
2110 goto end_seq_and_fail;
2112 if (else_bb && !b_simple)
2114 if (!noce_emit_bb (emit_b, else_bb, b_simple))
2115 goto end_seq_and_fail;
2118 if (!noce_emit_bb (emit_a, then_bb, a_simple))
2119 goto end_seq_and_fail;
2121 else
2123 if (!noce_emit_bb (emit_a, then_bb, a_simple))
2124 goto end_seq_and_fail;
2126 if (!noce_emit_bb (emit_b, else_bb, b_simple))
2127 goto end_seq_and_fail;
2131 target = noce_emit_cmove (if_info, x, code, XEXP (if_info->cond, 0),
2132 XEXP (if_info->cond, 1), a, b);
2134 if (! target)
2135 goto end_seq_and_fail;
2137 /* If we're handling a memory for above, emit the load now. */
2138 if (is_mem)
2140 rtx mem = gen_rtx_MEM (GET_MODE (if_info->x), target);
2142 /* Copy over flags as appropriate. */
2143 if (MEM_VOLATILE_P (if_info->a) || MEM_VOLATILE_P (if_info->b))
2144 MEM_VOLATILE_P (mem) = 1;
2145 if (MEM_ALIAS_SET (if_info->a) == MEM_ALIAS_SET (if_info->b))
2146 set_mem_alias_set (mem, MEM_ALIAS_SET (if_info->a));
2147 set_mem_align (mem,
2148 MIN (MEM_ALIGN (if_info->a), MEM_ALIGN (if_info->b)));
2150 gcc_assert (MEM_ADDR_SPACE (if_info->a) == MEM_ADDR_SPACE (if_info->b));
2151 set_mem_addr_space (mem, MEM_ADDR_SPACE (if_info->a));
2153 noce_emit_move_insn (if_info->x, mem);
2155 else if (target != x)
2156 noce_emit_move_insn (x, target);
2158 ifcvt_seq = end_ifcvt_sequence (if_info);
2159 if (!ifcvt_seq)
2160 return FALSE;
2162 emit_insn_before_setloc (ifcvt_seq, if_info->jump,
2163 INSN_LOCATION (if_info->insn_a));
2164 return TRUE;
2166 end_seq_and_fail:
2167 end_sequence ();
2168 return FALSE;
2171 /* For most cases, the simplified condition we found is the best
2172 choice, but this is not the case for the min/max/abs transforms.
2173 For these we wish to know that it is A or B in the condition. */
2175 static rtx
2176 noce_get_alt_condition (struct noce_if_info *if_info, rtx target,
2177 rtx_insn **earliest)
2179 rtx cond, set;
2180 rtx_insn *insn;
2181 int reverse;
2183 /* If target is already mentioned in the known condition, return it. */
2184 if (reg_mentioned_p (target, if_info->cond))
2186 *earliest = if_info->cond_earliest;
2187 return if_info->cond;
2190 set = pc_set (if_info->jump);
2191 cond = XEXP (SET_SRC (set), 0);
2192 reverse
2193 = GET_CODE (XEXP (SET_SRC (set), 2)) == LABEL_REF
2194 && LABEL_REF_LABEL (XEXP (SET_SRC (set), 2)) == JUMP_LABEL (if_info->jump);
2195 if (if_info->then_else_reversed)
2196 reverse = !reverse;
2198 /* If we're looking for a constant, try to make the conditional
2199 have that constant in it. There are two reasons why it may
2200 not have the constant we want:
2202 1. GCC may have needed to put the constant in a register, because
2203 the target can't compare directly against that constant. For
2204 this case, we look for a SET immediately before the comparison
2205 that puts a constant in that register.
2207 2. GCC may have canonicalized the conditional, for example
2208 replacing "if x < 4" with "if x <= 3". We can undo that (or
2209 make equivalent types of changes) to get the constants we need
2210 if they're off by one in the right direction. */
2212 if (CONST_INT_P (target))
2214 enum rtx_code code = GET_CODE (if_info->cond);
2215 rtx op_a = XEXP (if_info->cond, 0);
2216 rtx op_b = XEXP (if_info->cond, 1);
2217 rtx_insn *prev_insn;
2219 /* First, look to see if we put a constant in a register. */
2220 prev_insn = prev_nonnote_insn (if_info->cond_earliest);
2221 if (prev_insn
2222 && BLOCK_FOR_INSN (prev_insn)
2223 == BLOCK_FOR_INSN (if_info->cond_earliest)
2224 && INSN_P (prev_insn)
2225 && GET_CODE (PATTERN (prev_insn)) == SET)
2227 rtx src = find_reg_equal_equiv_note (prev_insn);
2228 if (!src)
2229 src = SET_SRC (PATTERN (prev_insn));
2230 if (CONST_INT_P (src))
2232 if (rtx_equal_p (op_a, SET_DEST (PATTERN (prev_insn))))
2233 op_a = src;
2234 else if (rtx_equal_p (op_b, SET_DEST (PATTERN (prev_insn))))
2235 op_b = src;
2237 if (CONST_INT_P (op_a))
2239 std::swap (op_a, op_b);
2240 code = swap_condition (code);
2245 /* Now, look to see if we can get the right constant by
2246 adjusting the conditional. */
2247 if (CONST_INT_P (op_b))
2249 HOST_WIDE_INT desired_val = INTVAL (target);
2250 HOST_WIDE_INT actual_val = INTVAL (op_b);
2252 switch (code)
2254 case LT:
2255 if (actual_val == desired_val + 1)
2257 code = LE;
2258 op_b = GEN_INT (desired_val);
2260 break;
2261 case LE:
2262 if (actual_val == desired_val - 1)
2264 code = LT;
2265 op_b = GEN_INT (desired_val);
2267 break;
2268 case GT:
2269 if (actual_val == desired_val - 1)
2271 code = GE;
2272 op_b = GEN_INT (desired_val);
2274 break;
2275 case GE:
2276 if (actual_val == desired_val + 1)
2278 code = GT;
2279 op_b = GEN_INT (desired_val);
2281 break;
2282 default:
2283 break;
2287 /* If we made any changes, generate a new conditional that is
2288 equivalent to what we started with, but has the right
2289 constants in it. */
2290 if (code != GET_CODE (if_info->cond)
2291 || op_a != XEXP (if_info->cond, 0)
2292 || op_b != XEXP (if_info->cond, 1))
2294 cond = gen_rtx_fmt_ee (code, GET_MODE (cond), op_a, op_b);
2295 *earliest = if_info->cond_earliest;
2296 return cond;
2300 cond = canonicalize_condition (if_info->jump, cond, reverse,
2301 earliest, target, have_cbranchcc4, true);
2302 if (! cond || ! reg_mentioned_p (target, cond))
2303 return NULL;
2305 /* We almost certainly searched back to a different place.
2306 Need to re-verify correct lifetimes. */
2308 /* X may not be mentioned in the range (cond_earliest, jump]. */
2309 for (insn = if_info->jump; insn != *earliest; insn = PREV_INSN (insn))
2310 if (INSN_P (insn) && reg_overlap_mentioned_p (if_info->x, PATTERN (insn)))
2311 return NULL;
2313 /* A and B may not be modified in the range [cond_earliest, jump). */
2314 for (insn = *earliest; insn != if_info->jump; insn = NEXT_INSN (insn))
2315 if (INSN_P (insn)
2316 && (modified_in_p (if_info->a, insn)
2317 || modified_in_p (if_info->b, insn)))
2318 return NULL;
2320 return cond;
2323 /* Convert "if (a < b) x = a; else x = b;" to "x = min(a, b);", etc. */
2325 static int
2326 noce_try_minmax (struct noce_if_info *if_info)
2328 rtx cond, target;
2329 rtx_insn *earliest, *seq;
2330 enum rtx_code code, op;
2331 int unsignedp;
2333 if (!noce_simple_bbs (if_info))
2334 return FALSE;
2336 /* ??? Reject modes with NaNs or signed zeros since we don't know how
2337 they will be resolved with an SMIN/SMAX. It wouldn't be too hard
2338 to get the target to tell us... */
2339 if (HONOR_SIGNED_ZEROS (if_info->x)
2340 || HONOR_NANS (if_info->x))
2341 return FALSE;
2343 cond = noce_get_alt_condition (if_info, if_info->a, &earliest);
2344 if (!cond)
2345 return FALSE;
2347 /* Verify the condition is of the form we expect, and canonicalize
2348 the comparison code. */
2349 code = GET_CODE (cond);
2350 if (rtx_equal_p (XEXP (cond, 0), if_info->a))
2352 if (! rtx_equal_p (XEXP (cond, 1), if_info->b))
2353 return FALSE;
2355 else if (rtx_equal_p (XEXP (cond, 1), if_info->a))
2357 if (! rtx_equal_p (XEXP (cond, 0), if_info->b))
2358 return FALSE;
2359 code = swap_condition (code);
2361 else
2362 return FALSE;
2364 /* Determine what sort of operation this is. Note that the code is for
2365 a taken branch, so the code->operation mapping appears backwards. */
2366 switch (code)
2368 case LT:
2369 case LE:
2370 case UNLT:
2371 case UNLE:
2372 op = SMAX;
2373 unsignedp = 0;
2374 break;
2375 case GT:
2376 case GE:
2377 case UNGT:
2378 case UNGE:
2379 op = SMIN;
2380 unsignedp = 0;
2381 break;
2382 case LTU:
2383 case LEU:
2384 op = UMAX;
2385 unsignedp = 1;
2386 break;
2387 case GTU:
2388 case GEU:
2389 op = UMIN;
2390 unsignedp = 1;
2391 break;
2392 default:
2393 return FALSE;
2396 start_sequence ();
2398 target = expand_simple_binop (GET_MODE (if_info->x), op,
2399 if_info->a, if_info->b,
2400 if_info->x, unsignedp, OPTAB_WIDEN);
2401 if (! target)
2403 end_sequence ();
2404 return FALSE;
2406 if (target != if_info->x)
2407 noce_emit_move_insn (if_info->x, target);
2409 seq = end_ifcvt_sequence (if_info);
2410 if (!seq)
2411 return FALSE;
2413 emit_insn_before_setloc (seq, if_info->jump, INSN_LOCATION (if_info->insn_a));
2414 if_info->cond = cond;
2415 if_info->cond_earliest = earliest;
2417 return TRUE;
2420 /* Convert "if (a < 0) x = -a; else x = a;" to "x = abs(a);",
2421 "if (a < 0) x = ~a; else x = a;" to "x = one_cmpl_abs(a);",
2422 etc. */
2424 static int
2425 noce_try_abs (struct noce_if_info *if_info)
2427 rtx cond, target, a, b, c;
2428 rtx_insn *earliest, *seq;
2429 int negate;
2430 bool one_cmpl = false;
2432 if (!noce_simple_bbs (if_info))
2433 return FALSE;
2435 /* Reject modes with signed zeros. */
2436 if (HONOR_SIGNED_ZEROS (if_info->x))
2437 return FALSE;
2439 /* Recognize A and B as constituting an ABS or NABS. The canonical
2440 form is a branch around the negation, taken when the object is the
2441 first operand of a comparison against 0 that evaluates to true. */
2442 a = if_info->a;
2443 b = if_info->b;
2444 if (GET_CODE (a) == NEG && rtx_equal_p (XEXP (a, 0), b))
2445 negate = 0;
2446 else if (GET_CODE (b) == NEG && rtx_equal_p (XEXP (b, 0), a))
2448 std::swap (a, b);
2449 negate = 1;
2451 else if (GET_CODE (a) == NOT && rtx_equal_p (XEXP (a, 0), b))
2453 negate = 0;
2454 one_cmpl = true;
2456 else if (GET_CODE (b) == NOT && rtx_equal_p (XEXP (b, 0), a))
2458 std::swap (a, b);
2459 negate = 1;
2460 one_cmpl = true;
2462 else
2463 return FALSE;
2465 cond = noce_get_alt_condition (if_info, b, &earliest);
2466 if (!cond)
2467 return FALSE;
2469 /* Verify the condition is of the form we expect. */
2470 if (rtx_equal_p (XEXP (cond, 0), b))
2471 c = XEXP (cond, 1);
2472 else if (rtx_equal_p (XEXP (cond, 1), b))
2474 c = XEXP (cond, 0);
2475 negate = !negate;
2477 else
2478 return FALSE;
2480 /* Verify that C is zero. Search one step backward for a
2481 REG_EQUAL note or a simple source if necessary. */
2482 if (REG_P (c))
2484 rtx set;
2485 rtx_insn *insn = prev_nonnote_insn (earliest);
2486 if (insn
2487 && BLOCK_FOR_INSN (insn) == BLOCK_FOR_INSN (earliest)
2488 && (set = single_set (insn))
2489 && rtx_equal_p (SET_DEST (set), c))
2491 rtx note = find_reg_equal_equiv_note (insn);
2492 if (note)
2493 c = XEXP (note, 0);
2494 else
2495 c = SET_SRC (set);
2497 else
2498 return FALSE;
2500 if (MEM_P (c)
2501 && GET_CODE (XEXP (c, 0)) == SYMBOL_REF
2502 && CONSTANT_POOL_ADDRESS_P (XEXP (c, 0)))
2503 c = get_pool_constant (XEXP (c, 0));
2505 /* Work around funny ideas get_condition has wrt canonicalization.
2506 Note that these rtx constants are known to be CONST_INT, and
2507 therefore imply integer comparisons. */
2508 if (c == constm1_rtx && GET_CODE (cond) == GT)
2510 else if (c == const1_rtx && GET_CODE (cond) == LT)
2512 else if (c != CONST0_RTX (GET_MODE (b)))
2513 return FALSE;
2515 /* Determine what sort of operation this is. */
2516 switch (GET_CODE (cond))
2518 case LT:
2519 case LE:
2520 case UNLT:
2521 case UNLE:
2522 negate = !negate;
2523 break;
2524 case GT:
2525 case GE:
2526 case UNGT:
2527 case UNGE:
2528 break;
2529 default:
2530 return FALSE;
2533 start_sequence ();
2534 if (one_cmpl)
2535 target = expand_one_cmpl_abs_nojump (GET_MODE (if_info->x), b,
2536 if_info->x);
2537 else
2538 target = expand_abs_nojump (GET_MODE (if_info->x), b, if_info->x, 1);
2540 /* ??? It's a quandary whether cmove would be better here, especially
2541 for integers. Perhaps combine will clean things up. */
2542 if (target && negate)
2544 if (one_cmpl)
2545 target = expand_simple_unop (GET_MODE (target), NOT, target,
2546 if_info->x, 0);
2547 else
2548 target = expand_simple_unop (GET_MODE (target), NEG, target,
2549 if_info->x, 0);
2552 if (! target)
2554 end_sequence ();
2555 return FALSE;
2558 if (target != if_info->x)
2559 noce_emit_move_insn (if_info->x, target);
2561 seq = end_ifcvt_sequence (if_info);
2562 if (!seq)
2563 return FALSE;
2565 emit_insn_before_setloc (seq, if_info->jump, INSN_LOCATION (if_info->insn_a));
2566 if_info->cond = cond;
2567 if_info->cond_earliest = earliest;
2569 return TRUE;
2572 /* Convert "if (m < 0) x = b; else x = 0;" to "x = (m >> C) & b;". */
2574 static int
2575 noce_try_sign_mask (struct noce_if_info *if_info)
2577 rtx cond, t, m, c;
2578 rtx_insn *seq;
2579 machine_mode mode;
2580 enum rtx_code code;
2581 bool t_unconditional;
2583 if (!noce_simple_bbs (if_info))
2584 return FALSE;
2586 cond = if_info->cond;
2587 code = GET_CODE (cond);
2588 m = XEXP (cond, 0);
2589 c = XEXP (cond, 1);
2591 t = NULL_RTX;
2592 if (if_info->a == const0_rtx)
2594 if ((code == LT && c == const0_rtx)
2595 || (code == LE && c == constm1_rtx))
2596 t = if_info->b;
2598 else if (if_info->b == const0_rtx)
2600 if ((code == GE && c == const0_rtx)
2601 || (code == GT && c == constm1_rtx))
2602 t = if_info->a;
2605 if (! t || side_effects_p (t))
2606 return FALSE;
2608 /* We currently don't handle different modes. */
2609 mode = GET_MODE (t);
2610 if (GET_MODE (m) != mode)
2611 return FALSE;
2613 /* This is only profitable if T is unconditionally executed/evaluated in the
2614 original insn sequence or T is cheap. The former happens if B is the
2615 non-zero (T) value and if INSN_B was taken from TEST_BB, or there was no
2616 INSN_B which can happen for e.g. conditional stores to memory. For the
2617 cost computation use the block TEST_BB where the evaluation will end up
2618 after the transformation. */
2619 t_unconditional =
2620 (t == if_info->b
2621 && (if_info->insn_b == NULL_RTX
2622 || BLOCK_FOR_INSN (if_info->insn_b) == if_info->test_bb));
2623 if (!(t_unconditional
2624 || (set_src_cost (t, mode, optimize_bb_for_speed_p (if_info->test_bb))
2625 < COSTS_N_INSNS (2))))
2626 return FALSE;
2628 start_sequence ();
2629 /* Use emit_store_flag to generate "m < 0 ? -1 : 0" instead of expanding
2630 "(signed) m >> 31" directly. This benefits targets with specialized
2631 insns to obtain the signmask, but still uses ashr_optab otherwise. */
2632 m = emit_store_flag (gen_reg_rtx (mode), LT, m, const0_rtx, mode, 0, -1);
2633 t = m ? expand_binop (mode, and_optab, m, t, NULL_RTX, 0, OPTAB_DIRECT)
2634 : NULL_RTX;
2636 if (!t)
2638 end_sequence ();
2639 return FALSE;
2642 noce_emit_move_insn (if_info->x, t);
2644 seq = end_ifcvt_sequence (if_info);
2645 if (!seq)
2646 return FALSE;
2648 emit_insn_before_setloc (seq, if_info->jump, INSN_LOCATION (if_info->insn_a));
2649 return TRUE;
2653 /* Optimize away "if (x & C) x |= C" and similar bit manipulation
2654 transformations. */
2656 static int
2657 noce_try_bitop (struct noce_if_info *if_info)
2659 rtx cond, x, a, result;
2660 rtx_insn *seq;
2661 machine_mode mode;
2662 enum rtx_code code;
2663 int bitnum;
2665 x = if_info->x;
2666 cond = if_info->cond;
2667 code = GET_CODE (cond);
2669 if (!noce_simple_bbs (if_info))
2670 return FALSE;
2672 /* Check for no else condition. */
2673 if (! rtx_equal_p (x, if_info->b))
2674 return FALSE;
2676 /* Check for a suitable condition. */
2677 if (code != NE && code != EQ)
2678 return FALSE;
2679 if (XEXP (cond, 1) != const0_rtx)
2680 return FALSE;
2681 cond = XEXP (cond, 0);
2683 /* ??? We could also handle AND here. */
2684 if (GET_CODE (cond) == ZERO_EXTRACT)
2686 if (XEXP (cond, 1) != const1_rtx
2687 || !CONST_INT_P (XEXP (cond, 2))
2688 || ! rtx_equal_p (x, XEXP (cond, 0)))
2689 return FALSE;
2690 bitnum = INTVAL (XEXP (cond, 2));
2691 mode = GET_MODE (x);
2692 if (BITS_BIG_ENDIAN)
2693 bitnum = GET_MODE_BITSIZE (mode) - 1 - bitnum;
2694 if (bitnum < 0 || bitnum >= HOST_BITS_PER_WIDE_INT)
2695 return FALSE;
2697 else
2698 return FALSE;
2700 a = if_info->a;
2701 if (GET_CODE (a) == IOR || GET_CODE (a) == XOR)
2703 /* Check for "if (X & C) x = x op C". */
2704 if (! rtx_equal_p (x, XEXP (a, 0))
2705 || !CONST_INT_P (XEXP (a, 1))
2706 || (INTVAL (XEXP (a, 1)) & GET_MODE_MASK (mode))
2707 != (unsigned HOST_WIDE_INT) 1 << bitnum)
2708 return FALSE;
2710 /* if ((x & C) == 0) x |= C; is transformed to x |= C. */
2711 /* if ((x & C) != 0) x |= C; is transformed to nothing. */
2712 if (GET_CODE (a) == IOR)
2713 result = (code == NE) ? a : NULL_RTX;
2714 else if (code == NE)
2716 /* if ((x & C) == 0) x ^= C; is transformed to x |= C. */
2717 result = gen_int_mode ((HOST_WIDE_INT) 1 << bitnum, mode);
2718 result = simplify_gen_binary (IOR, mode, x, result);
2720 else
2722 /* if ((x & C) != 0) x ^= C; is transformed to x &= ~C. */
2723 result = gen_int_mode (~((HOST_WIDE_INT) 1 << bitnum), mode);
2724 result = simplify_gen_binary (AND, mode, x, result);
2727 else if (GET_CODE (a) == AND)
2729 /* Check for "if (X & C) x &= ~C". */
2730 if (! rtx_equal_p (x, XEXP (a, 0))
2731 || !CONST_INT_P (XEXP (a, 1))
2732 || (INTVAL (XEXP (a, 1)) & GET_MODE_MASK (mode))
2733 != (~((HOST_WIDE_INT) 1 << bitnum) & GET_MODE_MASK (mode)))
2734 return FALSE;
2736 /* if ((x & C) == 0) x &= ~C; is transformed to nothing. */
2737 /* if ((x & C) != 0) x &= ~C; is transformed to x &= ~C. */
2738 result = (code == EQ) ? a : NULL_RTX;
2740 else
2741 return FALSE;
2743 if (result)
2745 start_sequence ();
2746 noce_emit_move_insn (x, result);
2747 seq = end_ifcvt_sequence (if_info);
2748 if (!seq)
2749 return FALSE;
2751 emit_insn_before_setloc (seq, if_info->jump,
2752 INSN_LOCATION (if_info->insn_a));
2754 return TRUE;
2758 /* Similar to get_condition, only the resulting condition must be
2759 valid at JUMP, instead of at EARLIEST.
2761 If THEN_ELSE_REVERSED is true, the fallthrough does not go to the
2762 THEN block of the caller, and we have to reverse the condition. */
2764 static rtx
2765 noce_get_condition (rtx_insn *jump, rtx_insn **earliest, bool then_else_reversed)
2767 rtx cond, set, tmp;
2768 bool reverse;
2770 if (! any_condjump_p (jump))
2771 return NULL_RTX;
2773 set = pc_set (jump);
2775 /* If this branches to JUMP_LABEL when the condition is false,
2776 reverse the condition. */
2777 reverse = (GET_CODE (XEXP (SET_SRC (set), 2)) == LABEL_REF
2778 && LABEL_REF_LABEL (XEXP (SET_SRC (set), 2)) == JUMP_LABEL (jump));
2780 /* We may have to reverse because the caller's if block is not canonical,
2781 i.e. the THEN block isn't the fallthrough block for the TEST block
2782 (see find_if_header). */
2783 if (then_else_reversed)
2784 reverse = !reverse;
2786 /* If the condition variable is a register and is MODE_INT, accept it. */
2788 cond = XEXP (SET_SRC (set), 0);
2789 tmp = XEXP (cond, 0);
2790 if (REG_P (tmp) && GET_MODE_CLASS (GET_MODE (tmp)) == MODE_INT
2791 && (GET_MODE (tmp) != BImode
2792 || !targetm.small_register_classes_for_mode_p (BImode)))
2794 *earliest = jump;
2796 if (reverse)
2797 cond = gen_rtx_fmt_ee (reverse_condition (GET_CODE (cond)),
2798 GET_MODE (cond), tmp, XEXP (cond, 1));
2799 return cond;
2802 /* Otherwise, fall back on canonicalize_condition to do the dirty
2803 work of manipulating MODE_CC values and COMPARE rtx codes. */
2804 tmp = canonicalize_condition (jump, cond, reverse, earliest,
2805 NULL_RTX, have_cbranchcc4, true);
2807 /* We don't handle side-effects in the condition, like handling
2808 REG_INC notes and making sure no duplicate conditions are emitted. */
2809 if (tmp != NULL_RTX && side_effects_p (tmp))
2810 return NULL_RTX;
2812 return tmp;
2815 /* Return true if OP is ok for if-then-else processing. */
2817 static int
2818 noce_operand_ok (const_rtx op)
2820 if (side_effects_p (op))
2821 return FALSE;
2823 /* We special-case memories, so handle any of them with
2824 no address side effects. */
2825 if (MEM_P (op))
2826 return ! side_effects_p (XEXP (op, 0));
2828 return ! may_trap_p (op);
2831 /* Return true if a write into MEM may trap or fault. */
2833 static bool
2834 noce_mem_write_may_trap_or_fault_p (const_rtx mem)
2836 rtx addr;
2838 if (MEM_READONLY_P (mem))
2839 return true;
2841 if (may_trap_or_fault_p (mem))
2842 return true;
2844 addr = XEXP (mem, 0);
2846 /* Call target hook to avoid the effects of -fpic etc.... */
2847 addr = targetm.delegitimize_address (addr);
2849 while (addr)
2850 switch (GET_CODE (addr))
2852 case CONST:
2853 case PRE_DEC:
2854 case PRE_INC:
2855 case POST_DEC:
2856 case POST_INC:
2857 case POST_MODIFY:
2858 addr = XEXP (addr, 0);
2859 break;
2860 case LO_SUM:
2861 case PRE_MODIFY:
2862 addr = XEXP (addr, 1);
2863 break;
2864 case PLUS:
2865 if (CONST_INT_P (XEXP (addr, 1)))
2866 addr = XEXP (addr, 0);
2867 else
2868 return false;
2869 break;
2870 case LABEL_REF:
2871 return true;
2872 case SYMBOL_REF:
2873 if (SYMBOL_REF_DECL (addr)
2874 && decl_readonly_section (SYMBOL_REF_DECL (addr), 0))
2875 return true;
2876 return false;
2877 default:
2878 return false;
2881 return false;
2884 /* Return whether we can use store speculation for MEM. TOP_BB is the
2885 basic block above the conditional block where we are considering
2886 doing the speculative store. We look for whether MEM is set
2887 unconditionally later in the function. */
2889 static bool
2890 noce_can_store_speculate_p (basic_block top_bb, const_rtx mem)
2892 basic_block dominator;
2894 for (dominator = get_immediate_dominator (CDI_POST_DOMINATORS, top_bb);
2895 dominator != NULL;
2896 dominator = get_immediate_dominator (CDI_POST_DOMINATORS, dominator))
2898 rtx_insn *insn;
2900 FOR_BB_INSNS (dominator, insn)
2902 /* If we see something that might be a memory barrier, we
2903 have to stop looking. Even if the MEM is set later in
2904 the function, we still don't want to set it
2905 unconditionally before the barrier. */
2906 if (INSN_P (insn)
2907 && (volatile_insn_p (PATTERN (insn))
2908 || (CALL_P (insn) && (!RTL_CONST_CALL_P (insn)))))
2909 return false;
2911 if (memory_must_be_modified_in_insn_p (mem, insn))
2912 return true;
2913 if (modified_in_p (XEXP (mem, 0), insn))
2914 return false;
2919 return false;
2922 /* Return true if X contains a MEM subrtx. */
2924 static bool
2925 contains_mem_rtx_p (rtx x)
2927 subrtx_iterator::array_type array;
2928 FOR_EACH_SUBRTX (iter, array, x, ALL)
2929 if (MEM_P (*iter))
2930 return true;
2932 return false;
2935 /* Return true iff basic block TEST_BB is valid for noce if-conversion.
2936 The condition used in this if-conversion is in COND.
2937 In practice, check that TEST_BB ends with a single set
2938 x := a and all previous computations
2939 in TEST_BB don't produce any values that are live after TEST_BB.
2940 In other words, all the insns in TEST_BB are there only
2941 to compute a value for x. Put the rtx cost of the insns
2942 in TEST_BB into COST. Record whether TEST_BB is a single simple
2943 set instruction in SIMPLE_P. */
2945 static bool
2946 bb_valid_for_noce_process_p (basic_block test_bb, rtx cond,
2947 unsigned int *cost, bool *simple_p)
2949 if (!test_bb)
2950 return false;
2952 rtx_insn *last_insn = last_active_insn (test_bb, FALSE);
2953 rtx last_set = NULL_RTX;
2955 rtx cc = cc_in_cond (cond);
2957 if (!insn_valid_noce_process_p (last_insn, cc))
2958 return false;
2959 last_set = single_set (last_insn);
2961 rtx x = SET_DEST (last_set);
2962 rtx_insn *first_insn = first_active_insn (test_bb);
2963 rtx first_set = single_set (first_insn);
2965 if (!first_set)
2966 return false;
2968 /* We have a single simple set, that's okay. */
2969 bool speed_p = optimize_bb_for_speed_p (test_bb);
2971 if (first_insn == last_insn)
2973 *simple_p = noce_operand_ok (SET_DEST (first_set));
2974 *cost = insn_rtx_cost (first_set, speed_p);
2975 return *simple_p;
2978 rtx_insn *prev_last_insn = PREV_INSN (last_insn);
2979 gcc_assert (prev_last_insn);
2981 /* For now, disallow setting x multiple times in test_bb. */
2982 if (REG_P (x) && reg_set_between_p (x, first_insn, prev_last_insn))
2983 return false;
2985 bitmap test_bb_temps = BITMAP_ALLOC (&reg_obstack);
2987 /* The regs that are live out of test_bb. */
2988 bitmap test_bb_live_out = df_get_live_out (test_bb);
2990 int potential_cost = insn_rtx_cost (last_set, speed_p);
2991 rtx_insn *insn;
2992 FOR_BB_INSNS (test_bb, insn)
2994 if (insn != last_insn)
2996 if (!active_insn_p (insn))
2997 continue;
2999 if (!insn_valid_noce_process_p (insn, cc))
3000 goto free_bitmap_and_fail;
3002 rtx sset = single_set (insn);
3003 gcc_assert (sset);
3005 if (contains_mem_rtx_p (SET_SRC (sset))
3006 || !REG_P (SET_DEST (sset))
3007 || reg_overlap_mentioned_p (SET_DEST (sset), cond))
3008 goto free_bitmap_and_fail;
3010 potential_cost += insn_rtx_cost (sset, speed_p);
3011 bitmap_set_bit (test_bb_temps, REGNO (SET_DEST (sset)));
3015 /* If any of the intermediate results in test_bb are live after test_bb
3016 then fail. */
3017 if (bitmap_intersect_p (test_bb_live_out, test_bb_temps))
3018 goto free_bitmap_and_fail;
3020 BITMAP_FREE (test_bb_temps);
3021 *cost = potential_cost;
3022 *simple_p = false;
3023 return true;
3025 free_bitmap_and_fail:
3026 BITMAP_FREE (test_bb_temps);
3027 return false;
3030 /* Given a simple IF-THEN-JOIN or IF-THEN-ELSE-JOIN block, attempt to convert
3031 it without using conditional execution. Return TRUE if we were successful
3032 at converting the block. */
3034 static int
3035 noce_process_if_block (struct noce_if_info *if_info)
3037 basic_block test_bb = if_info->test_bb; /* test block */
3038 basic_block then_bb = if_info->then_bb; /* THEN */
3039 basic_block else_bb = if_info->else_bb; /* ELSE or NULL */
3040 basic_block join_bb = if_info->join_bb; /* JOIN */
3041 rtx_insn *jump = if_info->jump;
3042 rtx cond = if_info->cond;
3043 rtx_insn *insn_a, *insn_b;
3044 rtx set_a, set_b;
3045 rtx orig_x, x, a, b;
3047 /* We're looking for patterns of the form
3049 (1) if (...) x = a; else x = b;
3050 (2) x = b; if (...) x = a;
3051 (3) if (...) x = a; // as if with an initial x = x.
3053 The later patterns require jumps to be more expensive.
3054 For the if (...) x = a; else x = b; case we allow multiple insns
3055 inside the then and else blocks as long as their only effect is
3056 to calculate a value for x.
3057 ??? For future expansion, look for multiple X in such patterns. */
3059 if (! bb_valid_for_noce_process_p (then_bb, cond, &if_info->then_cost,
3060 &if_info->then_simple))
3061 return false;
3063 if (else_bb
3064 && ! bb_valid_for_noce_process_p (else_bb, cond, &if_info->else_cost,
3065 &if_info->else_simple))
3066 return false;
3068 insn_a = last_active_insn (then_bb, FALSE);
3069 set_a = single_set (insn_a);
3070 gcc_assert (set_a);
3072 x = SET_DEST (set_a);
3073 a = SET_SRC (set_a);
3075 /* Look for the other potential set. Make sure we've got equivalent
3076 destinations. */
3077 /* ??? This is overconservative. Storing to two different mems is
3078 as easy as conditionally computing the address. Storing to a
3079 single mem merely requires a scratch memory to use as one of the
3080 destination addresses; often the memory immediately below the
3081 stack pointer is available for this. */
3082 set_b = NULL_RTX;
3083 if (else_bb)
3085 insn_b = last_active_insn (else_bb, FALSE);
3086 set_b = single_set (insn_b);
3087 gcc_assert (set_b);
3089 if (!rtx_interchangeable_p (x, SET_DEST (set_b)))
3090 return FALSE;
3092 else
3094 insn_b = prev_nonnote_nondebug_insn (if_info->cond_earliest);
3095 /* We're going to be moving the evaluation of B down from above
3096 COND_EARLIEST to JUMP. Make sure the relevant data is still
3097 intact. */
3098 if (! insn_b
3099 || BLOCK_FOR_INSN (insn_b) != BLOCK_FOR_INSN (if_info->cond_earliest)
3100 || !NONJUMP_INSN_P (insn_b)
3101 || (set_b = single_set (insn_b)) == NULL_RTX
3102 || ! rtx_interchangeable_p (x, SET_DEST (set_b))
3103 || ! noce_operand_ok (SET_SRC (set_b))
3104 || reg_overlap_mentioned_p (x, SET_SRC (set_b))
3105 || modified_between_p (SET_SRC (set_b), insn_b, jump)
3106 /* Avoid extending the lifetime of hard registers on small
3107 register class machines. */
3108 || (REG_P (SET_SRC (set_b))
3109 && HARD_REGISTER_P (SET_SRC (set_b))
3110 && targetm.small_register_classes_for_mode_p
3111 (GET_MODE (SET_SRC (set_b))))
3112 /* Likewise with X. In particular this can happen when
3113 noce_get_condition looks farther back in the instruction
3114 stream than one might expect. */
3115 || reg_overlap_mentioned_p (x, cond)
3116 || reg_overlap_mentioned_p (x, a)
3117 || modified_between_p (x, insn_b, jump))
3119 insn_b = NULL;
3120 set_b = NULL_RTX;
3124 /* If x has side effects then only the if-then-else form is safe to
3125 convert. But even in that case we would need to restore any notes
3126 (such as REG_INC) at then end. That can be tricky if
3127 noce_emit_move_insn expands to more than one insn, so disable the
3128 optimization entirely for now if there are side effects. */
3129 if (side_effects_p (x))
3130 return FALSE;
3132 b = (set_b ? SET_SRC (set_b) : x);
3134 /* Only operate on register destinations, and even then avoid extending
3135 the lifetime of hard registers on small register class machines. */
3136 orig_x = x;
3137 if (!REG_P (x)
3138 || (HARD_REGISTER_P (x)
3139 && targetm.small_register_classes_for_mode_p (GET_MODE (x))))
3141 if (GET_MODE (x) == BLKmode)
3142 return FALSE;
3144 if (GET_CODE (x) == ZERO_EXTRACT
3145 && (!CONST_INT_P (XEXP (x, 1))
3146 || !CONST_INT_P (XEXP (x, 2))))
3147 return FALSE;
3149 x = gen_reg_rtx (GET_MODE (GET_CODE (x) == STRICT_LOW_PART
3150 ? XEXP (x, 0) : x));
3153 /* Don't operate on sources that may trap or are volatile. */
3154 if (! noce_operand_ok (a) || ! noce_operand_ok (b))
3155 return FALSE;
3157 retry:
3158 /* Set up the info block for our subroutines. */
3159 if_info->insn_a = insn_a;
3160 if_info->insn_b = insn_b;
3161 if_info->x = x;
3162 if_info->a = a;
3163 if_info->b = b;
3165 /* Try optimizations in some approximation of a useful order. */
3166 /* ??? Should first look to see if X is live incoming at all. If it
3167 isn't, we don't need anything but an unconditional set. */
3169 /* Look and see if A and B are really the same. Avoid creating silly
3170 cmove constructs that no one will fix up later. */
3171 if (noce_simple_bbs (if_info)
3172 && rtx_interchangeable_p (a, b))
3174 /* If we have an INSN_B, we don't have to create any new rtl. Just
3175 move the instruction that we already have. If we don't have an
3176 INSN_B, that means that A == X, and we've got a noop move. In
3177 that case don't do anything and let the code below delete INSN_A. */
3178 if (insn_b && else_bb)
3180 rtx note;
3182 if (else_bb && insn_b == BB_END (else_bb))
3183 BB_END (else_bb) = PREV_INSN (insn_b);
3184 reorder_insns (insn_b, insn_b, PREV_INSN (jump));
3186 /* If there was a REG_EQUAL note, delete it since it may have been
3187 true due to this insn being after a jump. */
3188 if ((note = find_reg_note (insn_b, REG_EQUAL, NULL_RTX)) != 0)
3189 remove_note (insn_b, note);
3191 insn_b = NULL;
3193 /* If we have "x = b; if (...) x = a;", and x has side-effects, then
3194 x must be executed twice. */
3195 else if (insn_b && side_effects_p (orig_x))
3196 return FALSE;
3198 x = orig_x;
3199 goto success;
3202 if (!set_b && MEM_P (orig_x))
3204 /* Disallow the "if (...) x = a;" form (implicit "else x = x;")
3205 for optimizations if writing to x may trap or fault,
3206 i.e. it's a memory other than a static var or a stack slot,
3207 is misaligned on strict aligned machines or is read-only. If
3208 x is a read-only memory, then the program is valid only if we
3209 avoid the store into it. If there are stores on both the
3210 THEN and ELSE arms, then we can go ahead with the conversion;
3211 either the program is broken, or the condition is always
3212 false such that the other memory is selected. */
3213 if (noce_mem_write_may_trap_or_fault_p (orig_x))
3214 return FALSE;
3216 /* Avoid store speculation: given "if (...) x = a" where x is a
3217 MEM, we only want to do the store if x is always set
3218 somewhere in the function. This avoids cases like
3219 if (pthread_mutex_trylock(mutex))
3220 ++global_variable;
3221 where we only want global_variable to be changed if the mutex
3222 is held. FIXME: This should ideally be expressed directly in
3223 RTL somehow. */
3224 if (!noce_can_store_speculate_p (test_bb, orig_x))
3225 return FALSE;
3228 if (noce_try_move (if_info))
3229 goto success;
3230 if (noce_try_store_flag (if_info))
3231 goto success;
3232 if (noce_try_bitop (if_info))
3233 goto success;
3234 if (noce_try_minmax (if_info))
3235 goto success;
3236 if (noce_try_abs (if_info))
3237 goto success;
3238 if (!targetm.have_conditional_execution ()
3239 && noce_try_store_flag_constants (if_info))
3240 goto success;
3241 if (HAVE_conditional_move
3242 && noce_try_cmove (if_info))
3243 goto success;
3244 if (! targetm.have_conditional_execution ())
3246 if (noce_try_addcc (if_info))
3247 goto success;
3248 if (noce_try_store_flag_mask (if_info))
3249 goto success;
3250 if (HAVE_conditional_move
3251 && noce_try_cmove_arith (if_info))
3252 goto success;
3253 if (noce_try_sign_mask (if_info))
3254 goto success;
3257 if (!else_bb && set_b)
3259 insn_b = NULL;
3260 set_b = NULL_RTX;
3261 b = orig_x;
3262 goto retry;
3265 return FALSE;
3267 success:
3269 /* If we used a temporary, fix it up now. */
3270 if (orig_x != x)
3272 rtx_insn *seq;
3274 start_sequence ();
3275 noce_emit_move_insn (orig_x, x);
3276 seq = get_insns ();
3277 set_used_flags (orig_x);
3278 unshare_all_rtl_in_chain (seq);
3279 end_sequence ();
3281 emit_insn_before_setloc (seq, BB_END (test_bb), INSN_LOCATION (insn_a));
3284 /* The original THEN and ELSE blocks may now be removed. The test block
3285 must now jump to the join block. If the test block and the join block
3286 can be merged, do so. */
3287 if (else_bb)
3289 delete_basic_block (else_bb);
3290 num_true_changes++;
3292 else
3293 remove_edge (find_edge (test_bb, join_bb));
3295 remove_edge (find_edge (then_bb, join_bb));
3296 redirect_edge_and_branch_force (single_succ_edge (test_bb), join_bb);
3297 delete_basic_block (then_bb);
3298 num_true_changes++;
3300 if (can_merge_blocks_p (test_bb, join_bb))
3302 merge_blocks (test_bb, join_bb);
3303 num_true_changes++;
3306 num_updated_if_blocks++;
3307 return TRUE;
3310 /* Check whether a block is suitable for conditional move conversion.
3311 Every insn must be a simple set of a register to a constant or a
3312 register. For each assignment, store the value in the pointer map
3313 VALS, keyed indexed by register pointer, then store the register
3314 pointer in REGS. COND is the condition we will test. */
3316 static int
3317 check_cond_move_block (basic_block bb,
3318 hash_map<rtx, rtx> *vals,
3319 vec<rtx> *regs,
3320 rtx cond)
3322 rtx_insn *insn;
3323 rtx cc = cc_in_cond (cond);
3325 /* We can only handle simple jumps at the end of the basic block.
3326 It is almost impossible to update the CFG otherwise. */
3327 insn = BB_END (bb);
3328 if (JUMP_P (insn) && !onlyjump_p (insn))
3329 return FALSE;
3331 FOR_BB_INSNS (bb, insn)
3333 rtx set, dest, src;
3335 if (!NONDEBUG_INSN_P (insn) || JUMP_P (insn))
3336 continue;
3337 set = single_set (insn);
3338 if (!set)
3339 return FALSE;
3341 dest = SET_DEST (set);
3342 src = SET_SRC (set);
3343 if (!REG_P (dest)
3344 || (HARD_REGISTER_P (dest)
3345 && targetm.small_register_classes_for_mode_p (GET_MODE (dest))))
3346 return FALSE;
3348 if (!CONSTANT_P (src) && !register_operand (src, VOIDmode))
3349 return FALSE;
3351 if (side_effects_p (src) || side_effects_p (dest))
3352 return FALSE;
3354 if (may_trap_p (src) || may_trap_p (dest))
3355 return FALSE;
3357 /* Don't try to handle this if the source register was
3358 modified earlier in the block. */
3359 if ((REG_P (src)
3360 && vals->get (src))
3361 || (GET_CODE (src) == SUBREG && REG_P (SUBREG_REG (src))
3362 && vals->get (SUBREG_REG (src))))
3363 return FALSE;
3365 /* Don't try to handle this if the destination register was
3366 modified earlier in the block. */
3367 if (vals->get (dest))
3368 return FALSE;
3370 /* Don't try to handle this if the condition uses the
3371 destination register. */
3372 if (reg_overlap_mentioned_p (dest, cond))
3373 return FALSE;
3375 /* Don't try to handle this if the source register is modified
3376 later in the block. */
3377 if (!CONSTANT_P (src)
3378 && modified_between_p (src, insn, NEXT_INSN (BB_END (bb))))
3379 return FALSE;
3381 /* Skip it if the instruction to be moved might clobber CC. */
3382 if (cc && set_of (cc, insn))
3383 return FALSE;
3385 vals->put (dest, src);
3387 regs->safe_push (dest);
3390 return TRUE;
3393 /* Given a basic block BB suitable for conditional move conversion,
3394 a condition COND, and pointer maps THEN_VALS and ELSE_VALS containing
3395 the register values depending on COND, emit the insns in the block as
3396 conditional moves. If ELSE_BLOCK is true, THEN_BB was already
3397 processed. The caller has started a sequence for the conversion.
3398 Return true if successful, false if something goes wrong. */
3400 static bool
3401 cond_move_convert_if_block (struct noce_if_info *if_infop,
3402 basic_block bb, rtx cond,
3403 hash_map<rtx, rtx> *then_vals,
3404 hash_map<rtx, rtx> *else_vals,
3405 bool else_block_p)
3407 enum rtx_code code;
3408 rtx_insn *insn;
3409 rtx cond_arg0, cond_arg1;
3411 code = GET_CODE (cond);
3412 cond_arg0 = XEXP (cond, 0);
3413 cond_arg1 = XEXP (cond, 1);
3415 FOR_BB_INSNS (bb, insn)
3417 rtx set, target, dest, t, e;
3419 /* ??? Maybe emit conditional debug insn? */
3420 if (!NONDEBUG_INSN_P (insn) || JUMP_P (insn))
3421 continue;
3422 set = single_set (insn);
3423 gcc_assert (set && REG_P (SET_DEST (set)));
3425 dest = SET_DEST (set);
3427 rtx *then_slot = then_vals->get (dest);
3428 rtx *else_slot = else_vals->get (dest);
3429 t = then_slot ? *then_slot : NULL_RTX;
3430 e = else_slot ? *else_slot : NULL_RTX;
3432 if (else_block_p)
3434 /* If this register was set in the then block, we already
3435 handled this case there. */
3436 if (t)
3437 continue;
3438 t = dest;
3439 gcc_assert (e);
3441 else
3443 gcc_assert (t);
3444 if (!e)
3445 e = dest;
3448 target = noce_emit_cmove (if_infop, dest, code, cond_arg0, cond_arg1,
3449 t, e);
3450 if (!target)
3451 return false;
3453 if (target != dest)
3454 noce_emit_move_insn (dest, target);
3457 return true;
3460 /* Given a simple IF-THEN-JOIN or IF-THEN-ELSE-JOIN block, attempt to convert
3461 it using only conditional moves. Return TRUE if we were successful at
3462 converting the block. */
3464 static int
3465 cond_move_process_if_block (struct noce_if_info *if_info)
3467 basic_block test_bb = if_info->test_bb;
3468 basic_block then_bb = if_info->then_bb;
3469 basic_block else_bb = if_info->else_bb;
3470 basic_block join_bb = if_info->join_bb;
3471 rtx_insn *jump = if_info->jump;
3472 rtx cond = if_info->cond;
3473 rtx_insn *seq, *loc_insn;
3474 rtx reg;
3475 int c;
3476 vec<rtx> then_regs = vNULL;
3477 vec<rtx> else_regs = vNULL;
3478 unsigned int i;
3479 int success_p = FALSE;
3481 /* Build a mapping for each block to the value used for each
3482 register. */
3483 hash_map<rtx, rtx> then_vals;
3484 hash_map<rtx, rtx> else_vals;
3486 /* Make sure the blocks are suitable. */
3487 if (!check_cond_move_block (then_bb, &then_vals, &then_regs, cond)
3488 || (else_bb
3489 && !check_cond_move_block (else_bb, &else_vals, &else_regs, cond)))
3490 goto done;
3492 /* Make sure the blocks can be used together. If the same register
3493 is set in both blocks, and is not set to a constant in both
3494 cases, then both blocks must set it to the same register. We
3495 have already verified that if it is set to a register, that the
3496 source register does not change after the assignment. Also count
3497 the number of registers set in only one of the blocks. */
3498 c = 0;
3499 FOR_EACH_VEC_ELT (then_regs, i, reg)
3501 rtx *then_slot = then_vals.get (reg);
3502 rtx *else_slot = else_vals.get (reg);
3504 gcc_checking_assert (then_slot);
3505 if (!else_slot)
3506 ++c;
3507 else
3509 rtx then_val = *then_slot;
3510 rtx else_val = *else_slot;
3511 if (!CONSTANT_P (then_val) && !CONSTANT_P (else_val)
3512 && !rtx_equal_p (then_val, else_val))
3513 goto done;
3517 /* Finish off c for MAX_CONDITIONAL_EXECUTE. */
3518 FOR_EACH_VEC_ELT (else_regs, i, reg)
3520 gcc_checking_assert (else_vals.get (reg));
3521 if (!then_vals.get (reg))
3522 ++c;
3525 /* Make sure it is reasonable to convert this block. What matters
3526 is the number of assignments currently made in only one of the
3527 branches, since if we convert we are going to always execute
3528 them. */
3529 if (c > MAX_CONDITIONAL_EXECUTE)
3530 goto done;
3532 /* Try to emit the conditional moves. First do the then block,
3533 then do anything left in the else blocks. */
3534 start_sequence ();
3535 if (!cond_move_convert_if_block (if_info, then_bb, cond,
3536 &then_vals, &else_vals, false)
3537 || (else_bb
3538 && !cond_move_convert_if_block (if_info, else_bb, cond,
3539 &then_vals, &else_vals, true)))
3541 end_sequence ();
3542 goto done;
3544 seq = end_ifcvt_sequence (if_info);
3545 if (!seq)
3546 goto done;
3548 loc_insn = first_active_insn (then_bb);
3549 if (!loc_insn)
3551 loc_insn = first_active_insn (else_bb);
3552 gcc_assert (loc_insn);
3554 emit_insn_before_setloc (seq, jump, INSN_LOCATION (loc_insn));
3556 if (else_bb)
3558 delete_basic_block (else_bb);
3559 num_true_changes++;
3561 else
3562 remove_edge (find_edge (test_bb, join_bb));
3564 remove_edge (find_edge (then_bb, join_bb));
3565 redirect_edge_and_branch_force (single_succ_edge (test_bb), join_bb);
3566 delete_basic_block (then_bb);
3567 num_true_changes++;
3569 if (can_merge_blocks_p (test_bb, join_bb))
3571 merge_blocks (test_bb, join_bb);
3572 num_true_changes++;
3575 num_updated_if_blocks++;
3577 success_p = TRUE;
3579 done:
3580 then_regs.release ();
3581 else_regs.release ();
3582 return success_p;
3586 /* Determine if a given basic block heads a simple IF-THEN-JOIN or an
3587 IF-THEN-ELSE-JOIN block.
3589 If so, we'll try to convert the insns to not require the branch,
3590 using only transformations that do not require conditional execution.
3592 Return TRUE if we were successful at converting the block. */
3594 static int
3595 noce_find_if_block (basic_block test_bb, edge then_edge, edge else_edge,
3596 int pass)
3598 basic_block then_bb, else_bb, join_bb;
3599 bool then_else_reversed = false;
3600 rtx_insn *jump;
3601 rtx cond;
3602 rtx_insn *cond_earliest;
3603 struct noce_if_info if_info;
3605 /* We only ever should get here before reload. */
3606 gcc_assert (!reload_completed);
3608 /* Recognize an IF-THEN-ELSE-JOIN block. */
3609 if (single_pred_p (then_edge->dest)
3610 && single_succ_p (then_edge->dest)
3611 && single_pred_p (else_edge->dest)
3612 && single_succ_p (else_edge->dest)
3613 && single_succ (then_edge->dest) == single_succ (else_edge->dest))
3615 then_bb = then_edge->dest;
3616 else_bb = else_edge->dest;
3617 join_bb = single_succ (then_bb);
3619 /* Recognize an IF-THEN-JOIN block. */
3620 else if (single_pred_p (then_edge->dest)
3621 && single_succ_p (then_edge->dest)
3622 && single_succ (then_edge->dest) == else_edge->dest)
3624 then_bb = then_edge->dest;
3625 else_bb = NULL_BLOCK;
3626 join_bb = else_edge->dest;
3628 /* Recognize an IF-ELSE-JOIN block. We can have those because the order
3629 of basic blocks in cfglayout mode does not matter, so the fallthrough
3630 edge can go to any basic block (and not just to bb->next_bb, like in
3631 cfgrtl mode). */
3632 else if (single_pred_p (else_edge->dest)
3633 && single_succ_p (else_edge->dest)
3634 && single_succ (else_edge->dest) == then_edge->dest)
3636 /* The noce transformations do not apply to IF-ELSE-JOIN blocks.
3637 To make this work, we have to invert the THEN and ELSE blocks
3638 and reverse the jump condition. */
3639 then_bb = else_edge->dest;
3640 else_bb = NULL_BLOCK;
3641 join_bb = single_succ (then_bb);
3642 then_else_reversed = true;
3644 else
3645 /* Not a form we can handle. */
3646 return FALSE;
3648 /* The edges of the THEN and ELSE blocks cannot have complex edges. */
3649 if (single_succ_edge (then_bb)->flags & EDGE_COMPLEX)
3650 return FALSE;
3651 if (else_bb
3652 && single_succ_edge (else_bb)->flags & EDGE_COMPLEX)
3653 return FALSE;
3655 num_possible_if_blocks++;
3657 if (dump_file)
3659 fprintf (dump_file,
3660 "\nIF-THEN%s-JOIN block found, pass %d, test %d, then %d",
3661 (else_bb) ? "-ELSE" : "",
3662 pass, test_bb->index, then_bb->index);
3664 if (else_bb)
3665 fprintf (dump_file, ", else %d", else_bb->index);
3667 fprintf (dump_file, ", join %d\n", join_bb->index);
3670 /* If the conditional jump is more than just a conditional
3671 jump, then we can not do if-conversion on this block. */
3672 jump = BB_END (test_bb);
3673 if (! onlyjump_p (jump))
3674 return FALSE;
3676 /* If this is not a standard conditional jump, we can't parse it. */
3677 cond = noce_get_condition (jump, &cond_earliest, then_else_reversed);
3678 if (!cond)
3679 return FALSE;
3681 /* We must be comparing objects whose modes imply the size. */
3682 if (GET_MODE (XEXP (cond, 0)) == BLKmode)
3683 return FALSE;
3685 /* Initialize an IF_INFO struct to pass around. */
3686 memset (&if_info, 0, sizeof if_info);
3687 if_info.test_bb = test_bb;
3688 if_info.then_bb = then_bb;
3689 if_info.else_bb = else_bb;
3690 if_info.join_bb = join_bb;
3691 if_info.cond = cond;
3692 if_info.cond_earliest = cond_earliest;
3693 if_info.jump = jump;
3694 if_info.then_else_reversed = then_else_reversed;
3695 if_info.branch_cost = BRANCH_COST (optimize_bb_for_speed_p (test_bb),
3696 predictable_edge_p (then_edge));
3698 /* Do the real work. */
3700 if (noce_process_if_block (&if_info))
3701 return TRUE;
3703 if (HAVE_conditional_move
3704 && cond_move_process_if_block (&if_info))
3705 return TRUE;
3707 return FALSE;
3711 /* Merge the blocks and mark for local life update. */
3713 static void
3714 merge_if_block (struct ce_if_block * ce_info)
3716 basic_block test_bb = ce_info->test_bb; /* last test block */
3717 basic_block then_bb = ce_info->then_bb; /* THEN */
3718 basic_block else_bb = ce_info->else_bb; /* ELSE or NULL */
3719 basic_block join_bb = ce_info->join_bb; /* join block */
3720 basic_block combo_bb;
3722 /* All block merging is done into the lower block numbers. */
3724 combo_bb = test_bb;
3725 df_set_bb_dirty (test_bb);
3727 /* Merge any basic blocks to handle && and || subtests. Each of
3728 the blocks are on the fallthru path from the predecessor block. */
3729 if (ce_info->num_multiple_test_blocks > 0)
3731 basic_block bb = test_bb;
3732 basic_block last_test_bb = ce_info->last_test_bb;
3733 basic_block fallthru = block_fallthru (bb);
3737 bb = fallthru;
3738 fallthru = block_fallthru (bb);
3739 merge_blocks (combo_bb, bb);
3740 num_true_changes++;
3742 while (bb != last_test_bb);
3745 /* Merge TEST block into THEN block. Normally the THEN block won't have a
3746 label, but it might if there were || tests. That label's count should be
3747 zero, and it normally should be removed. */
3749 if (then_bb)
3751 /* If THEN_BB has no successors, then there's a BARRIER after it.
3752 If COMBO_BB has more than one successor (THEN_BB), then that BARRIER
3753 is no longer needed, and in fact it is incorrect to leave it in
3754 the insn stream. */
3755 if (EDGE_COUNT (then_bb->succs) == 0
3756 && EDGE_COUNT (combo_bb->succs) > 1)
3758 rtx_insn *end = NEXT_INSN (BB_END (then_bb));
3759 while (end && NOTE_P (end) && !NOTE_INSN_BASIC_BLOCK_P (end))
3760 end = NEXT_INSN (end);
3762 if (end && BARRIER_P (end))
3763 delete_insn (end);
3765 merge_blocks (combo_bb, then_bb);
3766 num_true_changes++;
3769 /* The ELSE block, if it existed, had a label. That label count
3770 will almost always be zero, but odd things can happen when labels
3771 get their addresses taken. */
3772 if (else_bb)
3774 /* If ELSE_BB has no successors, then there's a BARRIER after it.
3775 If COMBO_BB has more than one successor (ELSE_BB), then that BARRIER
3776 is no longer needed, and in fact it is incorrect to leave it in
3777 the insn stream. */
3778 if (EDGE_COUNT (else_bb->succs) == 0
3779 && EDGE_COUNT (combo_bb->succs) > 1)
3781 rtx_insn *end = NEXT_INSN (BB_END (else_bb));
3782 while (end && NOTE_P (end) && !NOTE_INSN_BASIC_BLOCK_P (end))
3783 end = NEXT_INSN (end);
3785 if (end && BARRIER_P (end))
3786 delete_insn (end);
3788 merge_blocks (combo_bb, else_bb);
3789 num_true_changes++;
3792 /* If there was no join block reported, that means it was not adjacent
3793 to the others, and so we cannot merge them. */
3795 if (! join_bb)
3797 rtx_insn *last = BB_END (combo_bb);
3799 /* The outgoing edge for the current COMBO block should already
3800 be correct. Verify this. */
3801 if (EDGE_COUNT (combo_bb->succs) == 0)
3802 gcc_assert (find_reg_note (last, REG_NORETURN, NULL)
3803 || (NONJUMP_INSN_P (last)
3804 && GET_CODE (PATTERN (last)) == TRAP_IF
3805 && (TRAP_CONDITION (PATTERN (last))
3806 == const_true_rtx)));
3808 else
3809 /* There should still be something at the end of the THEN or ELSE
3810 blocks taking us to our final destination. */
3811 gcc_assert (JUMP_P (last)
3812 || (EDGE_SUCC (combo_bb, 0)->dest
3813 == EXIT_BLOCK_PTR_FOR_FN (cfun)
3814 && CALL_P (last)
3815 && SIBLING_CALL_P (last))
3816 || ((EDGE_SUCC (combo_bb, 0)->flags & EDGE_EH)
3817 && can_throw_internal (last)));
3820 /* The JOIN block may have had quite a number of other predecessors too.
3821 Since we've already merged the TEST, THEN and ELSE blocks, we should
3822 have only one remaining edge from our if-then-else diamond. If there
3823 is more than one remaining edge, it must come from elsewhere. There
3824 may be zero incoming edges if the THEN block didn't actually join
3825 back up (as with a call to a non-return function). */
3826 else if (EDGE_COUNT (join_bb->preds) < 2
3827 && join_bb != EXIT_BLOCK_PTR_FOR_FN (cfun))
3829 /* We can merge the JOIN cleanly and update the dataflow try
3830 again on this pass.*/
3831 merge_blocks (combo_bb, join_bb);
3832 num_true_changes++;
3834 else
3836 /* We cannot merge the JOIN. */
3838 /* The outgoing edge for the current COMBO block should already
3839 be correct. Verify this. */
3840 gcc_assert (single_succ_p (combo_bb)
3841 && single_succ (combo_bb) == join_bb);
3843 /* Remove the jump and cruft from the end of the COMBO block. */
3844 if (join_bb != EXIT_BLOCK_PTR_FOR_FN (cfun))
3845 tidy_fallthru_edge (single_succ_edge (combo_bb));
3848 num_updated_if_blocks++;
3851 /* Find a block ending in a simple IF condition and try to transform it
3852 in some way. When converting a multi-block condition, put the new code
3853 in the first such block and delete the rest. Return a pointer to this
3854 first block if some transformation was done. Return NULL otherwise. */
3856 static basic_block
3857 find_if_header (basic_block test_bb, int pass)
3859 ce_if_block ce_info;
3860 edge then_edge;
3861 edge else_edge;
3863 /* The kind of block we're looking for has exactly two successors. */
3864 if (EDGE_COUNT (test_bb->succs) != 2)
3865 return NULL;
3867 then_edge = EDGE_SUCC (test_bb, 0);
3868 else_edge = EDGE_SUCC (test_bb, 1);
3870 if (df_get_bb_dirty (then_edge->dest))
3871 return NULL;
3872 if (df_get_bb_dirty (else_edge->dest))
3873 return NULL;
3875 /* Neither edge should be abnormal. */
3876 if ((then_edge->flags & EDGE_COMPLEX)
3877 || (else_edge->flags & EDGE_COMPLEX))
3878 return NULL;
3880 /* Nor exit the loop. */
3881 if ((then_edge->flags & EDGE_LOOP_EXIT)
3882 || (else_edge->flags & EDGE_LOOP_EXIT))
3883 return NULL;
3885 /* The THEN edge is canonically the one that falls through. */
3886 if (then_edge->flags & EDGE_FALLTHRU)
3888 else if (else_edge->flags & EDGE_FALLTHRU)
3889 std::swap (then_edge, else_edge);
3890 else
3891 /* Otherwise this must be a multiway branch of some sort. */
3892 return NULL;
3894 memset (&ce_info, 0, sizeof (ce_info));
3895 ce_info.test_bb = test_bb;
3896 ce_info.then_bb = then_edge->dest;
3897 ce_info.else_bb = else_edge->dest;
3898 ce_info.pass = pass;
3900 #ifdef IFCVT_MACHDEP_INIT
3901 IFCVT_MACHDEP_INIT (&ce_info);
3902 #endif
3904 if (!reload_completed
3905 && noce_find_if_block (test_bb, then_edge, else_edge, pass))
3906 goto success;
3908 if (reload_completed
3909 && targetm.have_conditional_execution ()
3910 && cond_exec_find_if_block (&ce_info))
3911 goto success;
3913 if (targetm.have_trap ()
3914 && optab_handler (ctrap_optab, word_mode) != CODE_FOR_nothing
3915 && find_cond_trap (test_bb, then_edge, else_edge))
3916 goto success;
3918 if (dom_info_state (CDI_POST_DOMINATORS) >= DOM_NO_FAST_QUERY
3919 && (reload_completed || !targetm.have_conditional_execution ()))
3921 if (find_if_case_1 (test_bb, then_edge, else_edge))
3922 goto success;
3923 if (find_if_case_2 (test_bb, then_edge, else_edge))
3924 goto success;
3927 return NULL;
3929 success:
3930 if (dump_file)
3931 fprintf (dump_file, "Conversion succeeded on pass %d.\n", pass);
3932 /* Set this so we continue looking. */
3933 cond_exec_changed_p = TRUE;
3934 return ce_info.test_bb;
3937 /* Return true if a block has two edges, one of which falls through to the next
3938 block, and the other jumps to a specific block, so that we can tell if the
3939 block is part of an && test or an || test. Returns either -1 or the number
3940 of non-note, non-jump, non-USE/CLOBBER insns in the block. */
3942 static int
3943 block_jumps_and_fallthru_p (basic_block cur_bb, basic_block target_bb)
3945 edge cur_edge;
3946 int fallthru_p = FALSE;
3947 int jump_p = FALSE;
3948 rtx_insn *insn;
3949 rtx_insn *end;
3950 int n_insns = 0;
3951 edge_iterator ei;
3953 if (!cur_bb || !target_bb)
3954 return -1;
3956 /* If no edges, obviously it doesn't jump or fallthru. */
3957 if (EDGE_COUNT (cur_bb->succs) == 0)
3958 return FALSE;
3960 FOR_EACH_EDGE (cur_edge, ei, cur_bb->succs)
3962 if (cur_edge->flags & EDGE_COMPLEX)
3963 /* Anything complex isn't what we want. */
3964 return -1;
3966 else if (cur_edge->flags & EDGE_FALLTHRU)
3967 fallthru_p = TRUE;
3969 else if (cur_edge->dest == target_bb)
3970 jump_p = TRUE;
3972 else
3973 return -1;
3976 if ((jump_p & fallthru_p) == 0)
3977 return -1;
3979 /* Don't allow calls in the block, since this is used to group && and ||
3980 together for conditional execution support. ??? we should support
3981 conditional execution support across calls for IA-64 some day, but
3982 for now it makes the code simpler. */
3983 end = BB_END (cur_bb);
3984 insn = BB_HEAD (cur_bb);
3986 while (insn != NULL_RTX)
3988 if (CALL_P (insn))
3989 return -1;
3991 if (INSN_P (insn)
3992 && !JUMP_P (insn)
3993 && !DEBUG_INSN_P (insn)
3994 && GET_CODE (PATTERN (insn)) != USE
3995 && GET_CODE (PATTERN (insn)) != CLOBBER)
3996 n_insns++;
3998 if (insn == end)
3999 break;
4001 insn = NEXT_INSN (insn);
4004 return n_insns;
4007 /* Determine if a given basic block heads a simple IF-THEN or IF-THEN-ELSE
4008 block. If so, we'll try to convert the insns to not require the branch.
4009 Return TRUE if we were successful at converting the block. */
4011 static int
4012 cond_exec_find_if_block (struct ce_if_block * ce_info)
4014 basic_block test_bb = ce_info->test_bb;
4015 basic_block then_bb = ce_info->then_bb;
4016 basic_block else_bb = ce_info->else_bb;
4017 basic_block join_bb = NULL_BLOCK;
4018 edge cur_edge;
4019 basic_block next;
4020 edge_iterator ei;
4022 ce_info->last_test_bb = test_bb;
4024 /* We only ever should get here after reload,
4025 and if we have conditional execution. */
4026 gcc_assert (reload_completed && targetm.have_conditional_execution ());
4028 /* Discover if any fall through predecessors of the current test basic block
4029 were && tests (which jump to the else block) or || tests (which jump to
4030 the then block). */
4031 if (single_pred_p (test_bb)
4032 && single_pred_edge (test_bb)->flags == EDGE_FALLTHRU)
4034 basic_block bb = single_pred (test_bb);
4035 basic_block target_bb;
4036 int max_insns = MAX_CONDITIONAL_EXECUTE;
4037 int n_insns;
4039 /* Determine if the preceding block is an && or || block. */
4040 if ((n_insns = block_jumps_and_fallthru_p (bb, else_bb)) >= 0)
4042 ce_info->and_and_p = TRUE;
4043 target_bb = else_bb;
4045 else if ((n_insns = block_jumps_and_fallthru_p (bb, then_bb)) >= 0)
4047 ce_info->and_and_p = FALSE;
4048 target_bb = then_bb;
4050 else
4051 target_bb = NULL_BLOCK;
4053 if (target_bb && n_insns <= max_insns)
4055 int total_insns = 0;
4056 int blocks = 0;
4058 ce_info->last_test_bb = test_bb;
4060 /* Found at least one && or || block, look for more. */
4063 ce_info->test_bb = test_bb = bb;
4064 total_insns += n_insns;
4065 blocks++;
4067 if (!single_pred_p (bb))
4068 break;
4070 bb = single_pred (bb);
4071 n_insns = block_jumps_and_fallthru_p (bb, target_bb);
4073 while (n_insns >= 0 && (total_insns + n_insns) <= max_insns);
4075 ce_info->num_multiple_test_blocks = blocks;
4076 ce_info->num_multiple_test_insns = total_insns;
4078 if (ce_info->and_and_p)
4079 ce_info->num_and_and_blocks = blocks;
4080 else
4081 ce_info->num_or_or_blocks = blocks;
4085 /* The THEN block of an IF-THEN combo must have exactly one predecessor,
4086 other than any || blocks which jump to the THEN block. */
4087 if ((EDGE_COUNT (then_bb->preds) - ce_info->num_or_or_blocks) != 1)
4088 return FALSE;
4090 /* The edges of the THEN and ELSE blocks cannot have complex edges. */
4091 FOR_EACH_EDGE (cur_edge, ei, then_bb->preds)
4093 if (cur_edge->flags & EDGE_COMPLEX)
4094 return FALSE;
4097 FOR_EACH_EDGE (cur_edge, ei, else_bb->preds)
4099 if (cur_edge->flags & EDGE_COMPLEX)
4100 return FALSE;
4103 /* The THEN block of an IF-THEN combo must have zero or one successors. */
4104 if (EDGE_COUNT (then_bb->succs) > 0
4105 && (!single_succ_p (then_bb)
4106 || (single_succ_edge (then_bb)->flags & EDGE_COMPLEX)
4107 || (epilogue_completed
4108 && tablejump_p (BB_END (then_bb), NULL, NULL))))
4109 return FALSE;
4111 /* If the THEN block has no successors, conditional execution can still
4112 make a conditional call. Don't do this unless the ELSE block has
4113 only one incoming edge -- the CFG manipulation is too ugly otherwise.
4114 Check for the last insn of the THEN block being an indirect jump, which
4115 is listed as not having any successors, but confuses the rest of the CE
4116 code processing. ??? we should fix this in the future. */
4117 if (EDGE_COUNT (then_bb->succs) == 0)
4119 if (single_pred_p (else_bb) && else_bb != EXIT_BLOCK_PTR_FOR_FN (cfun))
4121 rtx_insn *last_insn = BB_END (then_bb);
4123 while (last_insn
4124 && NOTE_P (last_insn)
4125 && last_insn != BB_HEAD (then_bb))
4126 last_insn = PREV_INSN (last_insn);
4128 if (last_insn
4129 && JUMP_P (last_insn)
4130 && ! simplejump_p (last_insn))
4131 return FALSE;
4133 join_bb = else_bb;
4134 else_bb = NULL_BLOCK;
4136 else
4137 return FALSE;
4140 /* If the THEN block's successor is the other edge out of the TEST block,
4141 then we have an IF-THEN combo without an ELSE. */
4142 else if (single_succ (then_bb) == else_bb)
4144 join_bb = else_bb;
4145 else_bb = NULL_BLOCK;
4148 /* If the THEN and ELSE block meet in a subsequent block, and the ELSE
4149 has exactly one predecessor and one successor, and the outgoing edge
4150 is not complex, then we have an IF-THEN-ELSE combo. */
4151 else if (single_succ_p (else_bb)
4152 && single_succ (then_bb) == single_succ (else_bb)
4153 && single_pred_p (else_bb)
4154 && !(single_succ_edge (else_bb)->flags & EDGE_COMPLEX)
4155 && !(epilogue_completed
4156 && tablejump_p (BB_END (else_bb), NULL, NULL)))
4157 join_bb = single_succ (else_bb);
4159 /* Otherwise it is not an IF-THEN or IF-THEN-ELSE combination. */
4160 else
4161 return FALSE;
4163 num_possible_if_blocks++;
4165 if (dump_file)
4167 fprintf (dump_file,
4168 "\nIF-THEN%s block found, pass %d, start block %d "
4169 "[insn %d], then %d [%d]",
4170 (else_bb) ? "-ELSE" : "",
4171 ce_info->pass,
4172 test_bb->index,
4173 BB_HEAD (test_bb) ? (int)INSN_UID (BB_HEAD (test_bb)) : -1,
4174 then_bb->index,
4175 BB_HEAD (then_bb) ? (int)INSN_UID (BB_HEAD (then_bb)) : -1);
4177 if (else_bb)
4178 fprintf (dump_file, ", else %d [%d]",
4179 else_bb->index,
4180 BB_HEAD (else_bb) ? (int)INSN_UID (BB_HEAD (else_bb)) : -1);
4182 fprintf (dump_file, ", join %d [%d]",
4183 join_bb->index,
4184 BB_HEAD (join_bb) ? (int)INSN_UID (BB_HEAD (join_bb)) : -1);
4186 if (ce_info->num_multiple_test_blocks > 0)
4187 fprintf (dump_file, ", %d %s block%s last test %d [%d]",
4188 ce_info->num_multiple_test_blocks,
4189 (ce_info->and_and_p) ? "&&" : "||",
4190 (ce_info->num_multiple_test_blocks == 1) ? "" : "s",
4191 ce_info->last_test_bb->index,
4192 ((BB_HEAD (ce_info->last_test_bb))
4193 ? (int)INSN_UID (BB_HEAD (ce_info->last_test_bb))
4194 : -1));
4196 fputc ('\n', dump_file);
4199 /* Make sure IF, THEN, and ELSE, blocks are adjacent. Actually, we get the
4200 first condition for free, since we've already asserted that there's a
4201 fallthru edge from IF to THEN. Likewise for the && and || blocks, since
4202 we checked the FALLTHRU flag, those are already adjacent to the last IF
4203 block. */
4204 /* ??? As an enhancement, move the ELSE block. Have to deal with
4205 BLOCK notes, if by no other means than backing out the merge if they
4206 exist. Sticky enough I don't want to think about it now. */
4207 next = then_bb;
4208 if (else_bb && (next = next->next_bb) != else_bb)
4209 return FALSE;
4210 if ((next = next->next_bb) != join_bb
4211 && join_bb != EXIT_BLOCK_PTR_FOR_FN (cfun))
4213 if (else_bb)
4214 join_bb = NULL;
4215 else
4216 return FALSE;
4219 /* Do the real work. */
4221 ce_info->else_bb = else_bb;
4222 ce_info->join_bb = join_bb;
4224 /* If we have && and || tests, try to first handle combining the && and ||
4225 tests into the conditional code, and if that fails, go back and handle
4226 it without the && and ||, which at present handles the && case if there
4227 was no ELSE block. */
4228 if (cond_exec_process_if_block (ce_info, TRUE))
4229 return TRUE;
4231 if (ce_info->num_multiple_test_blocks)
4233 cancel_changes (0);
4235 if (cond_exec_process_if_block (ce_info, FALSE))
4236 return TRUE;
4239 return FALSE;
4242 /* Convert a branch over a trap, or a branch
4243 to a trap, into a conditional trap. */
4245 static int
4246 find_cond_trap (basic_block test_bb, edge then_edge, edge else_edge)
4248 basic_block then_bb = then_edge->dest;
4249 basic_block else_bb = else_edge->dest;
4250 basic_block other_bb, trap_bb;
4251 rtx_insn *trap, *jump;
4252 rtx cond;
4253 rtx_insn *cond_earliest;
4254 enum rtx_code code;
4256 /* Locate the block with the trap instruction. */
4257 /* ??? While we look for no successors, we really ought to allow
4258 EH successors. Need to fix merge_if_block for that to work. */
4259 if ((trap = block_has_only_trap (then_bb)) != NULL)
4260 trap_bb = then_bb, other_bb = else_bb;
4261 else if ((trap = block_has_only_trap (else_bb)) != NULL)
4262 trap_bb = else_bb, other_bb = then_bb;
4263 else
4264 return FALSE;
4266 if (dump_file)
4268 fprintf (dump_file, "\nTRAP-IF block found, start %d, trap %d\n",
4269 test_bb->index, trap_bb->index);
4272 /* If this is not a standard conditional jump, we can't parse it. */
4273 jump = BB_END (test_bb);
4274 cond = noce_get_condition (jump, &cond_earliest, false);
4275 if (! cond)
4276 return FALSE;
4278 /* If the conditional jump is more than just a conditional jump, then
4279 we can not do if-conversion on this block. */
4280 if (! onlyjump_p (jump))
4281 return FALSE;
4283 /* We must be comparing objects whose modes imply the size. */
4284 if (GET_MODE (XEXP (cond, 0)) == BLKmode)
4285 return FALSE;
4287 /* Reverse the comparison code, if necessary. */
4288 code = GET_CODE (cond);
4289 if (then_bb == trap_bb)
4291 code = reversed_comparison_code (cond, jump);
4292 if (code == UNKNOWN)
4293 return FALSE;
4296 /* Attempt to generate the conditional trap. */
4297 rtx_insn *seq = gen_cond_trap (code, copy_rtx (XEXP (cond, 0)),
4298 copy_rtx (XEXP (cond, 1)),
4299 TRAP_CODE (PATTERN (trap)));
4300 if (seq == NULL)
4301 return FALSE;
4303 /* Emit the new insns before cond_earliest. */
4304 emit_insn_before_setloc (seq, cond_earliest, INSN_LOCATION (trap));
4306 /* Delete the trap block if possible. */
4307 remove_edge (trap_bb == then_bb ? then_edge : else_edge);
4308 df_set_bb_dirty (test_bb);
4309 df_set_bb_dirty (then_bb);
4310 df_set_bb_dirty (else_bb);
4312 if (EDGE_COUNT (trap_bb->preds) == 0)
4314 delete_basic_block (trap_bb);
4315 num_true_changes++;
4318 /* Wire together the blocks again. */
4319 if (current_ir_type () == IR_RTL_CFGLAYOUT)
4320 single_succ_edge (test_bb)->flags |= EDGE_FALLTHRU;
4321 else if (trap_bb == then_bb)
4323 rtx lab = JUMP_LABEL (jump);
4324 rtx_insn *seq = targetm.gen_jump (lab);
4325 rtx_jump_insn *newjump = emit_jump_insn_after (seq, jump);
4326 LABEL_NUSES (lab) += 1;
4327 JUMP_LABEL (newjump) = lab;
4328 emit_barrier_after (newjump);
4330 delete_insn (jump);
4332 if (can_merge_blocks_p (test_bb, other_bb))
4334 merge_blocks (test_bb, other_bb);
4335 num_true_changes++;
4338 num_updated_if_blocks++;
4339 return TRUE;
4342 /* Subroutine of find_cond_trap: if BB contains only a trap insn,
4343 return it. */
4345 static rtx_insn *
4346 block_has_only_trap (basic_block bb)
4348 rtx_insn *trap;
4350 /* We're not the exit block. */
4351 if (bb == EXIT_BLOCK_PTR_FOR_FN (cfun))
4352 return NULL;
4354 /* The block must have no successors. */
4355 if (EDGE_COUNT (bb->succs) > 0)
4356 return NULL;
4358 /* The only instruction in the THEN block must be the trap. */
4359 trap = first_active_insn (bb);
4360 if (! (trap == BB_END (bb)
4361 && GET_CODE (PATTERN (trap)) == TRAP_IF
4362 && TRAP_CONDITION (PATTERN (trap)) == const_true_rtx))
4363 return NULL;
4365 return trap;
4368 /* Look for IF-THEN-ELSE cases in which one of THEN or ELSE is
4369 transformable, but not necessarily the other. There need be no
4370 JOIN block.
4372 Return TRUE if we were successful at converting the block.
4374 Cases we'd like to look at:
4377 if (test) goto over; // x not live
4378 x = a;
4379 goto label;
4380 over:
4382 becomes
4384 x = a;
4385 if (! test) goto label;
4388 if (test) goto E; // x not live
4389 x = big();
4390 goto L;
4392 x = b;
4393 goto M;
4395 becomes
4397 x = b;
4398 if (test) goto M;
4399 x = big();
4400 goto L;
4402 (3) // This one's really only interesting for targets that can do
4403 // multiway branching, e.g. IA-64 BBB bundles. For other targets
4404 // it results in multiple branches on a cache line, which often
4405 // does not sit well with predictors.
4407 if (test1) goto E; // predicted not taken
4408 x = a;
4409 if (test2) goto F;
4412 x = b;
4415 becomes
4417 x = a;
4418 if (test1) goto E;
4419 if (test2) goto F;
4421 Notes:
4423 (A) Don't do (2) if the branch is predicted against the block we're
4424 eliminating. Do it anyway if we can eliminate a branch; this requires
4425 that the sole successor of the eliminated block postdominate the other
4426 side of the if.
4428 (B) With CE, on (3) we can steal from both sides of the if, creating
4430 if (test1) x = a;
4431 if (!test1) x = b;
4432 if (test1) goto J;
4433 if (test2) goto F;
4437 Again, this is most useful if J postdominates.
4439 (C) CE substitutes for helpful life information.
4441 (D) These heuristics need a lot of work. */
4443 /* Tests for case 1 above. */
4445 static int
4446 find_if_case_1 (basic_block test_bb, edge then_edge, edge else_edge)
4448 basic_block then_bb = then_edge->dest;
4449 basic_block else_bb = else_edge->dest;
4450 basic_block new_bb;
4451 int then_bb_index, then_prob;
4452 rtx else_target = NULL_RTX;
4454 /* If we are partitioning hot/cold basic blocks, we don't want to
4455 mess up unconditional or indirect jumps that cross between hot
4456 and cold sections.
4458 Basic block partitioning may result in some jumps that appear to
4459 be optimizable (or blocks that appear to be mergeable), but which really
4460 must be left untouched (they are required to make it safely across
4461 partition boundaries). See the comments at the top of
4462 bb-reorder.c:partition_hot_cold_basic_blocks for complete details. */
4464 if ((BB_END (then_bb)
4465 && JUMP_P (BB_END (then_bb))
4466 && CROSSING_JUMP_P (BB_END (then_bb)))
4467 || (BB_END (test_bb)
4468 && JUMP_P (BB_END (test_bb))
4469 && CROSSING_JUMP_P (BB_END (test_bb)))
4470 || (BB_END (else_bb)
4471 && JUMP_P (BB_END (else_bb))
4472 && CROSSING_JUMP_P (BB_END (else_bb))))
4473 return FALSE;
4475 /* THEN has one successor. */
4476 if (!single_succ_p (then_bb))
4477 return FALSE;
4479 /* THEN does not fall through, but is not strange either. */
4480 if (single_succ_edge (then_bb)->flags & (EDGE_COMPLEX | EDGE_FALLTHRU))
4481 return FALSE;
4483 /* THEN has one predecessor. */
4484 if (!single_pred_p (then_bb))
4485 return FALSE;
4487 /* THEN must do something. */
4488 if (forwarder_block_p (then_bb))
4489 return FALSE;
4491 num_possible_if_blocks++;
4492 if (dump_file)
4493 fprintf (dump_file,
4494 "\nIF-CASE-1 found, start %d, then %d\n",
4495 test_bb->index, then_bb->index);
4497 if (then_edge->probability)
4498 then_prob = REG_BR_PROB_BASE - then_edge->probability;
4499 else
4500 then_prob = REG_BR_PROB_BASE / 2;
4502 /* We're speculating from the THEN path, we want to make sure the cost
4503 of speculation is within reason. */
4504 if (! cheap_bb_rtx_cost_p (then_bb, then_prob,
4505 COSTS_N_INSNS (BRANCH_COST (optimize_bb_for_speed_p (then_edge->src),
4506 predictable_edge_p (then_edge)))))
4507 return FALSE;
4509 if (else_bb == EXIT_BLOCK_PTR_FOR_FN (cfun))
4511 rtx_insn *jump = BB_END (else_edge->src);
4512 gcc_assert (JUMP_P (jump));
4513 else_target = JUMP_LABEL (jump);
4516 /* Registers set are dead, or are predicable. */
4517 if (! dead_or_predicable (test_bb, then_bb, else_bb,
4518 single_succ_edge (then_bb), 1))
4519 return FALSE;
4521 /* Conversion went ok, including moving the insns and fixing up the
4522 jump. Adjust the CFG to match. */
4524 /* We can avoid creating a new basic block if then_bb is immediately
4525 followed by else_bb, i.e. deleting then_bb allows test_bb to fall
4526 through to else_bb. */
4528 if (then_bb->next_bb == else_bb
4529 && then_bb->prev_bb == test_bb
4530 && else_bb != EXIT_BLOCK_PTR_FOR_FN (cfun))
4532 redirect_edge_succ (FALLTHRU_EDGE (test_bb), else_bb);
4533 new_bb = 0;
4535 else if (else_bb == EXIT_BLOCK_PTR_FOR_FN (cfun))
4536 new_bb = force_nonfallthru_and_redirect (FALLTHRU_EDGE (test_bb),
4537 else_bb, else_target);
4538 else
4539 new_bb = redirect_edge_and_branch_force (FALLTHRU_EDGE (test_bb),
4540 else_bb);
4542 df_set_bb_dirty (test_bb);
4543 df_set_bb_dirty (else_bb);
4545 then_bb_index = then_bb->index;
4546 delete_basic_block (then_bb);
4548 /* Make rest of code believe that the newly created block is the THEN_BB
4549 block we removed. */
4550 if (new_bb)
4552 df_bb_replace (then_bb_index, new_bb);
4553 /* This should have been done above via force_nonfallthru_and_redirect
4554 (possibly called from redirect_edge_and_branch_force). */
4555 gcc_checking_assert (BB_PARTITION (new_bb) == BB_PARTITION (test_bb));
4558 num_true_changes++;
4559 num_updated_if_blocks++;
4561 return TRUE;
4564 /* Test for case 2 above. */
4566 static int
4567 find_if_case_2 (basic_block test_bb, edge then_edge, edge else_edge)
4569 basic_block then_bb = then_edge->dest;
4570 basic_block else_bb = else_edge->dest;
4571 edge else_succ;
4572 int then_prob, else_prob;
4574 /* We do not want to speculate (empty) loop latches. */
4575 if (current_loops
4576 && else_bb->loop_father->latch == else_bb)
4577 return FALSE;
4579 /* If we are partitioning hot/cold basic blocks, we don't want to
4580 mess up unconditional or indirect jumps that cross between hot
4581 and cold sections.
4583 Basic block partitioning may result in some jumps that appear to
4584 be optimizable (or blocks that appear to be mergeable), but which really
4585 must be left untouched (they are required to make it safely across
4586 partition boundaries). See the comments at the top of
4587 bb-reorder.c:partition_hot_cold_basic_blocks for complete details. */
4589 if ((BB_END (then_bb)
4590 && JUMP_P (BB_END (then_bb))
4591 && CROSSING_JUMP_P (BB_END (then_bb)))
4592 || (BB_END (test_bb)
4593 && JUMP_P (BB_END (test_bb))
4594 && CROSSING_JUMP_P (BB_END (test_bb)))
4595 || (BB_END (else_bb)
4596 && JUMP_P (BB_END (else_bb))
4597 && CROSSING_JUMP_P (BB_END (else_bb))))
4598 return FALSE;
4600 /* ELSE has one successor. */
4601 if (!single_succ_p (else_bb))
4602 return FALSE;
4603 else
4604 else_succ = single_succ_edge (else_bb);
4606 /* ELSE outgoing edge is not complex. */
4607 if (else_succ->flags & EDGE_COMPLEX)
4608 return FALSE;
4610 /* ELSE has one predecessor. */
4611 if (!single_pred_p (else_bb))
4612 return FALSE;
4614 /* THEN is not EXIT. */
4615 if (then_bb->index < NUM_FIXED_BLOCKS)
4616 return FALSE;
4618 if (else_edge->probability)
4620 else_prob = else_edge->probability;
4621 then_prob = REG_BR_PROB_BASE - else_prob;
4623 else
4625 else_prob = REG_BR_PROB_BASE / 2;
4626 then_prob = REG_BR_PROB_BASE / 2;
4629 /* ELSE is predicted or SUCC(ELSE) postdominates THEN. */
4630 if (else_prob > then_prob)
4632 else if (else_succ->dest->index < NUM_FIXED_BLOCKS
4633 || dominated_by_p (CDI_POST_DOMINATORS, then_bb,
4634 else_succ->dest))
4636 else
4637 return FALSE;
4639 num_possible_if_blocks++;
4640 if (dump_file)
4641 fprintf (dump_file,
4642 "\nIF-CASE-2 found, start %d, else %d\n",
4643 test_bb->index, else_bb->index);
4645 /* We're speculating from the ELSE path, we want to make sure the cost
4646 of speculation is within reason. */
4647 if (! cheap_bb_rtx_cost_p (else_bb, else_prob,
4648 COSTS_N_INSNS (BRANCH_COST (optimize_bb_for_speed_p (else_edge->src),
4649 predictable_edge_p (else_edge)))))
4650 return FALSE;
4652 /* Registers set are dead, or are predicable. */
4653 if (! dead_or_predicable (test_bb, else_bb, then_bb, else_succ, 0))
4654 return FALSE;
4656 /* Conversion went ok, including moving the insns and fixing up the
4657 jump. Adjust the CFG to match. */
4659 df_set_bb_dirty (test_bb);
4660 df_set_bb_dirty (then_bb);
4661 delete_basic_block (else_bb);
4663 num_true_changes++;
4664 num_updated_if_blocks++;
4666 /* ??? We may now fallthru from one of THEN's successors into a join
4667 block. Rerun cleanup_cfg? Examine things manually? Wait? */
4669 return TRUE;
4672 /* Used by the code above to perform the actual rtl transformations.
4673 Return TRUE if successful.
4675 TEST_BB is the block containing the conditional branch. MERGE_BB
4676 is the block containing the code to manipulate. DEST_EDGE is an
4677 edge representing a jump to the join block; after the conversion,
4678 TEST_BB should be branching to its destination.
4679 REVERSEP is true if the sense of the branch should be reversed. */
4681 static int
4682 dead_or_predicable (basic_block test_bb, basic_block merge_bb,
4683 basic_block other_bb, edge dest_edge, int reversep)
4685 basic_block new_dest = dest_edge->dest;
4686 rtx_insn *head, *end, *jump;
4687 rtx_insn *earliest = NULL;
4688 rtx old_dest;
4689 bitmap merge_set = NULL;
4690 /* Number of pending changes. */
4691 int n_validated_changes = 0;
4692 rtx new_dest_label = NULL_RTX;
4694 jump = BB_END (test_bb);
4696 /* Find the extent of the real code in the merge block. */
4697 head = BB_HEAD (merge_bb);
4698 end = BB_END (merge_bb);
4700 while (DEBUG_INSN_P (end) && end != head)
4701 end = PREV_INSN (end);
4703 /* If merge_bb ends with a tablejump, predicating/moving insn's
4704 into test_bb and then deleting merge_bb will result in the jumptable
4705 that follows merge_bb being removed along with merge_bb and then we
4706 get an unresolved reference to the jumptable. */
4707 if (tablejump_p (end, NULL, NULL))
4708 return FALSE;
4710 if (LABEL_P (head))
4711 head = NEXT_INSN (head);
4712 while (DEBUG_INSN_P (head) && head != end)
4713 head = NEXT_INSN (head);
4714 if (NOTE_P (head))
4716 if (head == end)
4718 head = end = NULL;
4719 goto no_body;
4721 head = NEXT_INSN (head);
4722 while (DEBUG_INSN_P (head) && head != end)
4723 head = NEXT_INSN (head);
4726 if (JUMP_P (end))
4728 if (!onlyjump_p (end))
4729 return FALSE;
4730 if (head == end)
4732 head = end = NULL;
4733 goto no_body;
4735 end = PREV_INSN (end);
4736 while (DEBUG_INSN_P (end) && end != head)
4737 end = PREV_INSN (end);
4740 /* Don't move frame-related insn across the conditional branch. This
4741 can lead to one of the paths of the branch having wrong unwind info. */
4742 if (epilogue_completed)
4744 rtx_insn *insn = head;
4745 while (1)
4747 if (INSN_P (insn) && RTX_FRAME_RELATED_P (insn))
4748 return FALSE;
4749 if (insn == end)
4750 break;
4751 insn = NEXT_INSN (insn);
4755 /* Disable handling dead code by conditional execution if the machine needs
4756 to do anything funny with the tests, etc. */
4757 #ifndef IFCVT_MODIFY_TESTS
4758 if (targetm.have_conditional_execution ())
4760 /* In the conditional execution case, we have things easy. We know
4761 the condition is reversible. We don't have to check life info
4762 because we're going to conditionally execute the code anyway.
4763 All that's left is making sure the insns involved can actually
4764 be predicated. */
4766 rtx cond;
4768 cond = cond_exec_get_condition (jump);
4769 if (! cond)
4770 return FALSE;
4772 rtx note = find_reg_note (jump, REG_BR_PROB, NULL_RTX);
4773 int prob_val = (note ? XINT (note, 0) : -1);
4775 if (reversep)
4777 enum rtx_code rev = reversed_comparison_code (cond, jump);
4778 if (rev == UNKNOWN)
4779 return FALSE;
4780 cond = gen_rtx_fmt_ee (rev, GET_MODE (cond), XEXP (cond, 0),
4781 XEXP (cond, 1));
4782 if (prob_val >= 0)
4783 prob_val = REG_BR_PROB_BASE - prob_val;
4786 if (cond_exec_process_insns (NULL, head, end, cond, prob_val, 0)
4787 && verify_changes (0))
4788 n_validated_changes = num_validated_changes ();
4789 else
4790 cancel_changes (0);
4792 earliest = jump;
4794 #endif
4796 /* If we allocated new pseudos (e.g. in the conditional move
4797 expander called from noce_emit_cmove), we must resize the
4798 array first. */
4799 if (max_regno < max_reg_num ())
4800 max_regno = max_reg_num ();
4802 /* Try the NCE path if the CE path did not result in any changes. */
4803 if (n_validated_changes == 0)
4805 rtx cond;
4806 rtx_insn *insn;
4807 regset live;
4808 bool success;
4810 /* In the non-conditional execution case, we have to verify that there
4811 are no trapping operations, no calls, no references to memory, and
4812 that any registers modified are dead at the branch site. */
4814 if (!any_condjump_p (jump))
4815 return FALSE;
4817 /* Find the extent of the conditional. */
4818 cond = noce_get_condition (jump, &earliest, false);
4819 if (!cond)
4820 return FALSE;
4822 live = BITMAP_ALLOC (&reg_obstack);
4823 simulate_backwards_to_point (merge_bb, live, end);
4824 success = can_move_insns_across (head, end, earliest, jump,
4825 merge_bb, live,
4826 df_get_live_in (other_bb), NULL);
4827 BITMAP_FREE (live);
4828 if (!success)
4829 return FALSE;
4831 /* Collect the set of registers set in MERGE_BB. */
4832 merge_set = BITMAP_ALLOC (&reg_obstack);
4834 FOR_BB_INSNS (merge_bb, insn)
4835 if (NONDEBUG_INSN_P (insn))
4836 df_simulate_find_defs (insn, merge_set);
4838 /* If shrink-wrapping, disable this optimization when test_bb is
4839 the first basic block and merge_bb exits. The idea is to not
4840 move code setting up a return register as that may clobber a
4841 register used to pass function parameters, which then must be
4842 saved in caller-saved regs. A caller-saved reg requires the
4843 prologue, killing a shrink-wrap opportunity. */
4844 if ((SHRINK_WRAPPING_ENABLED && !epilogue_completed)
4845 && ENTRY_BLOCK_PTR_FOR_FN (cfun)->next_bb == test_bb
4846 && single_succ_p (new_dest)
4847 && single_succ (new_dest) == EXIT_BLOCK_PTR_FOR_FN (cfun)
4848 && bitmap_intersect_p (df_get_live_in (new_dest), merge_set))
4850 regset return_regs;
4851 unsigned int i;
4853 return_regs = BITMAP_ALLOC (&reg_obstack);
4855 /* Start off with the intersection of regs used to pass
4856 params and regs used to return values. */
4857 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
4858 if (FUNCTION_ARG_REGNO_P (i)
4859 && targetm.calls.function_value_regno_p (i))
4860 bitmap_set_bit (return_regs, INCOMING_REGNO (i));
4862 bitmap_and_into (return_regs,
4863 df_get_live_out (ENTRY_BLOCK_PTR_FOR_FN (cfun)));
4864 bitmap_and_into (return_regs,
4865 df_get_live_in (EXIT_BLOCK_PTR_FOR_FN (cfun)));
4866 if (!bitmap_empty_p (return_regs))
4868 FOR_BB_INSNS_REVERSE (new_dest, insn)
4869 if (NONDEBUG_INSN_P (insn))
4871 df_ref def;
4873 /* If this insn sets any reg in return_regs, add all
4874 reg uses to the set of regs we're interested in. */
4875 FOR_EACH_INSN_DEF (def, insn)
4876 if (bitmap_bit_p (return_regs, DF_REF_REGNO (def)))
4878 df_simulate_uses (insn, return_regs);
4879 break;
4882 if (bitmap_intersect_p (merge_set, return_regs))
4884 BITMAP_FREE (return_regs);
4885 BITMAP_FREE (merge_set);
4886 return FALSE;
4889 BITMAP_FREE (return_regs);
4893 no_body:
4894 /* We don't want to use normal invert_jump or redirect_jump because
4895 we don't want to delete_insn called. Also, we want to do our own
4896 change group management. */
4898 old_dest = JUMP_LABEL (jump);
4899 if (other_bb != new_dest)
4901 if (!any_condjump_p (jump))
4902 goto cancel;
4904 if (JUMP_P (BB_END (dest_edge->src)))
4905 new_dest_label = JUMP_LABEL (BB_END (dest_edge->src));
4906 else if (new_dest == EXIT_BLOCK_PTR_FOR_FN (cfun))
4907 new_dest_label = ret_rtx;
4908 else
4909 new_dest_label = block_label (new_dest);
4911 rtx_jump_insn *jump_insn = as_a <rtx_jump_insn *> (jump);
4912 if (reversep
4913 ? ! invert_jump_1 (jump_insn, new_dest_label)
4914 : ! redirect_jump_1 (jump_insn, new_dest_label))
4915 goto cancel;
4918 if (verify_changes (n_validated_changes))
4919 confirm_change_group ();
4920 else
4921 goto cancel;
4923 if (other_bb != new_dest)
4925 redirect_jump_2 (as_a <rtx_jump_insn *> (jump), old_dest, new_dest_label,
4926 0, reversep);
4928 redirect_edge_succ (BRANCH_EDGE (test_bb), new_dest);
4929 if (reversep)
4931 std::swap (BRANCH_EDGE (test_bb)->count,
4932 FALLTHRU_EDGE (test_bb)->count);
4933 std::swap (BRANCH_EDGE (test_bb)->probability,
4934 FALLTHRU_EDGE (test_bb)->probability);
4935 update_br_prob_note (test_bb);
4939 /* Move the insns out of MERGE_BB to before the branch. */
4940 if (head != NULL)
4942 rtx_insn *insn;
4944 if (end == BB_END (merge_bb))
4945 BB_END (merge_bb) = PREV_INSN (head);
4947 /* PR 21767: when moving insns above a conditional branch, the REG_EQUAL
4948 notes being moved might become invalid. */
4949 insn = head;
4952 rtx note;
4954 if (! INSN_P (insn))
4955 continue;
4956 note = find_reg_note (insn, REG_EQUAL, NULL_RTX);
4957 if (! note)
4958 continue;
4959 remove_note (insn, note);
4960 } while (insn != end && (insn = NEXT_INSN (insn)));
4962 /* PR46315: when moving insns above a conditional branch, the REG_EQUAL
4963 notes referring to the registers being set might become invalid. */
4964 if (merge_set)
4966 unsigned i;
4967 bitmap_iterator bi;
4969 EXECUTE_IF_SET_IN_BITMAP (merge_set, 0, i, bi)
4970 remove_reg_equal_equiv_notes_for_regno (i);
4972 BITMAP_FREE (merge_set);
4975 reorder_insns (head, end, PREV_INSN (earliest));
4978 /* Remove the jump and edge if we can. */
4979 if (other_bb == new_dest)
4981 delete_insn (jump);
4982 remove_edge (BRANCH_EDGE (test_bb));
4983 /* ??? Can't merge blocks here, as then_bb is still in use.
4984 At minimum, the merge will get done just before bb-reorder. */
4987 return TRUE;
4989 cancel:
4990 cancel_changes (0);
4992 if (merge_set)
4993 BITMAP_FREE (merge_set);
4995 return FALSE;
4998 /* Main entry point for all if-conversion. AFTER_COMBINE is true if
4999 we are after combine pass. */
5001 static void
5002 if_convert (bool after_combine)
5004 basic_block bb;
5005 int pass;
5007 if (optimize == 1)
5009 df_live_add_problem ();
5010 df_live_set_all_dirty ();
5013 /* Record whether we are after combine pass. */
5014 ifcvt_after_combine = after_combine;
5015 have_cbranchcc4 = (direct_optab_handler (cbranch_optab, CCmode)
5016 != CODE_FOR_nothing);
5017 num_possible_if_blocks = 0;
5018 num_updated_if_blocks = 0;
5019 num_true_changes = 0;
5021 loop_optimizer_init (AVOID_CFG_MODIFICATIONS);
5022 mark_loop_exit_edges ();
5023 loop_optimizer_finalize ();
5024 free_dominance_info (CDI_DOMINATORS);
5026 /* Compute postdominators. */
5027 calculate_dominance_info (CDI_POST_DOMINATORS);
5029 df_set_flags (DF_LR_RUN_DCE);
5031 /* Go through each of the basic blocks looking for things to convert. If we
5032 have conditional execution, we make multiple passes to allow us to handle
5033 IF-THEN{-ELSE} blocks within other IF-THEN{-ELSE} blocks. */
5034 pass = 0;
5037 df_analyze ();
5038 /* Only need to do dce on the first pass. */
5039 df_clear_flags (DF_LR_RUN_DCE);
5040 cond_exec_changed_p = FALSE;
5041 pass++;
5043 #ifdef IFCVT_MULTIPLE_DUMPS
5044 if (dump_file && pass > 1)
5045 fprintf (dump_file, "\n\n========== Pass %d ==========\n", pass);
5046 #endif
5048 FOR_EACH_BB_FN (bb, cfun)
5050 basic_block new_bb;
5051 while (!df_get_bb_dirty (bb)
5052 && (new_bb = find_if_header (bb, pass)) != NULL)
5053 bb = new_bb;
5056 #ifdef IFCVT_MULTIPLE_DUMPS
5057 if (dump_file && cond_exec_changed_p)
5058 print_rtl_with_bb (dump_file, get_insns (), dump_flags);
5059 #endif
5061 while (cond_exec_changed_p);
5063 #ifdef IFCVT_MULTIPLE_DUMPS
5064 if (dump_file)
5065 fprintf (dump_file, "\n\n========== no more changes\n");
5066 #endif
5068 free_dominance_info (CDI_POST_DOMINATORS);
5070 if (dump_file)
5071 fflush (dump_file);
5073 clear_aux_for_blocks ();
5075 /* If we allocated new pseudos, we must resize the array for sched1. */
5076 if (max_regno < max_reg_num ())
5077 max_regno = max_reg_num ();
5079 /* Write the final stats. */
5080 if (dump_file && num_possible_if_blocks > 0)
5082 fprintf (dump_file,
5083 "\n%d possible IF blocks searched.\n",
5084 num_possible_if_blocks);
5085 fprintf (dump_file,
5086 "%d IF blocks converted.\n",
5087 num_updated_if_blocks);
5088 fprintf (dump_file,
5089 "%d true changes made.\n\n\n",
5090 num_true_changes);
5093 if (optimize == 1)
5094 df_remove_problem (df_live);
5096 #ifdef ENABLE_CHECKING
5097 verify_flow_info ();
5098 #endif
5101 /* If-conversion and CFG cleanup. */
5102 static unsigned int
5103 rest_of_handle_if_conversion (void)
5105 if (flag_if_conversion)
5107 if (dump_file)
5109 dump_reg_info (dump_file);
5110 dump_flow_info (dump_file, dump_flags);
5112 cleanup_cfg (CLEANUP_EXPENSIVE);
5113 if_convert (false);
5116 cleanup_cfg (0);
5117 return 0;
5120 namespace {
5122 const pass_data pass_data_rtl_ifcvt =
5124 RTL_PASS, /* type */
5125 "ce1", /* name */
5126 OPTGROUP_NONE, /* optinfo_flags */
5127 TV_IFCVT, /* tv_id */
5128 0, /* properties_required */
5129 0, /* properties_provided */
5130 0, /* properties_destroyed */
5131 0, /* todo_flags_start */
5132 TODO_df_finish, /* todo_flags_finish */
5135 class pass_rtl_ifcvt : public rtl_opt_pass
5137 public:
5138 pass_rtl_ifcvt (gcc::context *ctxt)
5139 : rtl_opt_pass (pass_data_rtl_ifcvt, ctxt)
5142 /* opt_pass methods: */
5143 virtual bool gate (function *)
5145 return (optimize > 0) && dbg_cnt (if_conversion);
5148 virtual unsigned int execute (function *)
5150 return rest_of_handle_if_conversion ();
5153 }; // class pass_rtl_ifcvt
5155 } // anon namespace
5157 rtl_opt_pass *
5158 make_pass_rtl_ifcvt (gcc::context *ctxt)
5160 return new pass_rtl_ifcvt (ctxt);
5164 /* Rerun if-conversion, as combine may have simplified things enough
5165 to now meet sequence length restrictions. */
5167 namespace {
5169 const pass_data pass_data_if_after_combine =
5171 RTL_PASS, /* type */
5172 "ce2", /* name */
5173 OPTGROUP_NONE, /* optinfo_flags */
5174 TV_IFCVT, /* tv_id */
5175 0, /* properties_required */
5176 0, /* properties_provided */
5177 0, /* properties_destroyed */
5178 0, /* todo_flags_start */
5179 TODO_df_finish, /* todo_flags_finish */
5182 class pass_if_after_combine : public rtl_opt_pass
5184 public:
5185 pass_if_after_combine (gcc::context *ctxt)
5186 : rtl_opt_pass (pass_data_if_after_combine, ctxt)
5189 /* opt_pass methods: */
5190 virtual bool gate (function *)
5192 return optimize > 0 && flag_if_conversion
5193 && dbg_cnt (if_after_combine);
5196 virtual unsigned int execute (function *)
5198 if_convert (true);
5199 return 0;
5202 }; // class pass_if_after_combine
5204 } // anon namespace
5206 rtl_opt_pass *
5207 make_pass_if_after_combine (gcc::context *ctxt)
5209 return new pass_if_after_combine (ctxt);
5213 namespace {
5215 const pass_data pass_data_if_after_reload =
5217 RTL_PASS, /* type */
5218 "ce3", /* name */
5219 OPTGROUP_NONE, /* optinfo_flags */
5220 TV_IFCVT2, /* tv_id */
5221 0, /* properties_required */
5222 0, /* properties_provided */
5223 0, /* properties_destroyed */
5224 0, /* todo_flags_start */
5225 TODO_df_finish, /* todo_flags_finish */
5228 class pass_if_after_reload : public rtl_opt_pass
5230 public:
5231 pass_if_after_reload (gcc::context *ctxt)
5232 : rtl_opt_pass (pass_data_if_after_reload, ctxt)
5235 /* opt_pass methods: */
5236 virtual bool gate (function *)
5238 return optimize > 0 && flag_if_conversion2
5239 && dbg_cnt (if_after_reload);
5242 virtual unsigned int execute (function *)
5244 if_convert (true);
5245 return 0;
5248 }; // class pass_if_after_reload
5250 } // anon namespace
5252 rtl_opt_pass *
5253 make_pass_if_after_reload (gcc::context *ctxt)
5255 return new pass_if_after_reload (ctxt);