* hooks.h (hook_uint_uintp_false): Rename to...
[official-gcc.git] / gcc / ifcvt.c
blob24542f008485e6c28e068030fa301f2ce040efc1
1 /* If-conversion support.
2 Copyright (C) 2000-2016 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 "target.h"
25 #include "rtl.h"
26 #include "tree.h"
27 #include "cfghooks.h"
28 #include "df.h"
29 #include "tm_p.h"
30 #include "expmed.h"
31 #include "optabs.h"
32 #include "regs.h"
33 #include "emit-rtl.h"
34 #include "recog.h"
36 #include "cfgrtl.h"
37 #include "cfganal.h"
38 #include "cfgcleanup.h"
39 #include "expr.h"
40 #include "output.h"
41 #include "cfgloop.h"
42 #include "tree-pass.h"
43 #include "dbgcnt.h"
44 #include "shrink-wrap.h"
45 #include "rtl-iter.h"
46 #include "ifcvt.h"
47 #include "params.h"
49 #ifndef MAX_CONDITIONAL_EXECUTE
50 #define MAX_CONDITIONAL_EXECUTE \
51 (BRANCH_COST (optimize_function_for_speed_p (cfun), false) \
52 + 1)
53 #endif
55 #define IFCVT_MULTIPLE_DUMPS 1
57 #define NULL_BLOCK ((basic_block) NULL)
59 /* True if after combine pass. */
60 static bool ifcvt_after_combine;
62 /* True if the target has the cbranchcc4 optab. */
63 static bool have_cbranchcc4;
65 /* # of IF-THEN or IF-THEN-ELSE blocks we looked at */
66 static int num_possible_if_blocks;
68 /* # of IF-THEN or IF-THEN-ELSE blocks were converted to conditional
69 execution. */
70 static int num_updated_if_blocks;
72 /* # of changes made. */
73 static int num_true_changes;
75 /* Whether conditional execution changes were made. */
76 static int cond_exec_changed_p;
78 /* Forward references. */
79 static int count_bb_insns (const_basic_block);
80 static bool cheap_bb_rtx_cost_p (const_basic_block, int, int);
81 static rtx_insn *first_active_insn (basic_block);
82 static rtx_insn *last_active_insn (basic_block, int);
83 static rtx_insn *find_active_insn_before (basic_block, rtx_insn *);
84 static rtx_insn *find_active_insn_after (basic_block, rtx_insn *);
85 static basic_block block_fallthru (basic_block);
86 static int cond_exec_process_insns (ce_if_block *, rtx_insn *, rtx, rtx, int,
87 int);
88 static rtx cond_exec_get_condition (rtx_insn *);
89 static rtx noce_get_condition (rtx_insn *, rtx_insn **, bool);
90 static int noce_operand_ok (const_rtx);
91 static void merge_if_block (ce_if_block *);
92 static int find_cond_trap (basic_block, edge, edge);
93 static basic_block find_if_header (basic_block, int);
94 static int block_jumps_and_fallthru_p (basic_block, basic_block);
95 static int noce_find_if_block (basic_block, edge, edge, int);
96 static int cond_exec_find_if_block (ce_if_block *);
97 static int find_if_case_1 (basic_block, edge, edge);
98 static int find_if_case_2 (basic_block, edge, edge);
99 static int dead_or_predicable (basic_block, basic_block, basic_block,
100 edge, int);
101 static void noce_emit_move_insn (rtx, rtx);
102 static rtx_insn *block_has_only_trap (basic_block);
104 /* Count the number of non-jump active insns in BB. */
106 static int
107 count_bb_insns (const_basic_block bb)
109 int count = 0;
110 rtx_insn *insn = BB_HEAD (bb);
112 while (1)
114 if (active_insn_p (insn) && !JUMP_P (insn))
115 count++;
117 if (insn == BB_END (bb))
118 break;
119 insn = NEXT_INSN (insn);
122 return count;
125 /* Determine whether the total insn_rtx_cost on non-jump insns in
126 basic block BB is less than MAX_COST. This function returns
127 false if the cost of any instruction could not be estimated.
129 The cost of the non-jump insns in BB is scaled by REG_BR_PROB_BASE
130 as those insns are being speculated. MAX_COST is scaled with SCALE
131 plus a small fudge factor. */
133 static bool
134 cheap_bb_rtx_cost_p (const_basic_block bb, int scale, int max_cost)
136 int count = 0;
137 rtx_insn *insn = BB_HEAD (bb);
138 bool speed = optimize_bb_for_speed_p (bb);
140 /* Set scale to REG_BR_PROB_BASE to void the identical scaling
141 applied to insn_rtx_cost when optimizing for size. Only do
142 this after combine because if-conversion might interfere with
143 passes before combine.
145 Use optimize_function_for_speed_p instead of the pre-defined
146 variable speed to make sure it is set to same value for all
147 basic blocks in one if-conversion transformation. */
148 if (!optimize_function_for_speed_p (cfun) && ifcvt_after_combine)
149 scale = REG_BR_PROB_BASE;
150 /* Our branch probability/scaling factors are just estimates and don't
151 account for cases where we can get speculation for free and other
152 secondary benefits. So we fudge the scale factor to make speculating
153 appear a little more profitable when optimizing for performance. */
154 else
155 scale += REG_BR_PROB_BASE / 8;
158 max_cost *= scale;
160 while (1)
162 if (NONJUMP_INSN_P (insn))
164 int cost = insn_rtx_cost (PATTERN (insn), speed) * REG_BR_PROB_BASE;
165 if (cost == 0)
166 return false;
168 /* If this instruction is the load or set of a "stack" register,
169 such as a floating point register on x87, then the cost of
170 speculatively executing this insn may need to include
171 the additional cost of popping its result off of the
172 register stack. Unfortunately, correctly recognizing and
173 accounting for this additional overhead is tricky, so for
174 now we simply prohibit such speculative execution. */
175 #ifdef STACK_REGS
177 rtx set = single_set (insn);
178 if (set && STACK_REG_P (SET_DEST (set)))
179 return false;
181 #endif
183 count += cost;
184 if (count >= max_cost)
185 return false;
187 else if (CALL_P (insn))
188 return false;
190 if (insn == BB_END (bb))
191 break;
192 insn = NEXT_INSN (insn);
195 return true;
198 /* Return the first non-jump active insn in the basic block. */
200 static rtx_insn *
201 first_active_insn (basic_block bb)
203 rtx_insn *insn = BB_HEAD (bb);
205 if (LABEL_P (insn))
207 if (insn == BB_END (bb))
208 return NULL;
209 insn = NEXT_INSN (insn);
212 while (NOTE_P (insn) || DEBUG_INSN_P (insn))
214 if (insn == BB_END (bb))
215 return NULL;
216 insn = NEXT_INSN (insn);
219 if (JUMP_P (insn))
220 return NULL;
222 return insn;
225 /* Return the last non-jump active (non-jump) insn in the basic block. */
227 static rtx_insn *
228 last_active_insn (basic_block bb, int skip_use_p)
230 rtx_insn *insn = BB_END (bb);
231 rtx_insn *head = BB_HEAD (bb);
233 while (NOTE_P (insn)
234 || JUMP_P (insn)
235 || DEBUG_INSN_P (insn)
236 || (skip_use_p
237 && NONJUMP_INSN_P (insn)
238 && GET_CODE (PATTERN (insn)) == USE))
240 if (insn == head)
241 return NULL;
242 insn = PREV_INSN (insn);
245 if (LABEL_P (insn))
246 return NULL;
248 return insn;
251 /* Return the active insn before INSN inside basic block CURR_BB. */
253 static rtx_insn *
254 find_active_insn_before (basic_block curr_bb, rtx_insn *insn)
256 if (!insn || insn == BB_HEAD (curr_bb))
257 return NULL;
259 while ((insn = PREV_INSN (insn)) != NULL_RTX)
261 if (NONJUMP_INSN_P (insn) || JUMP_P (insn) || CALL_P (insn))
262 break;
264 /* No other active insn all the way to the start of the basic block. */
265 if (insn == BB_HEAD (curr_bb))
266 return NULL;
269 return insn;
272 /* Return the active insn after INSN inside basic block CURR_BB. */
274 static rtx_insn *
275 find_active_insn_after (basic_block curr_bb, rtx_insn *insn)
277 if (!insn || insn == BB_END (curr_bb))
278 return NULL;
280 while ((insn = NEXT_INSN (insn)) != NULL_RTX)
282 if (NONJUMP_INSN_P (insn) || JUMP_P (insn) || CALL_P (insn))
283 break;
285 /* No other active insn all the way to the end of the basic block. */
286 if (insn == BB_END (curr_bb))
287 return NULL;
290 return insn;
293 /* Return the basic block reached by falling though the basic block BB. */
295 static basic_block
296 block_fallthru (basic_block bb)
298 edge e = find_fallthru_edge (bb->succs);
300 return (e) ? e->dest : NULL_BLOCK;
303 /* Return true if RTXs A and B can be safely interchanged. */
305 static bool
306 rtx_interchangeable_p (const_rtx a, const_rtx b)
308 if (!rtx_equal_p (a, b))
309 return false;
311 if (GET_CODE (a) != MEM)
312 return true;
314 /* A dead type-unsafe memory reference is legal, but a live type-unsafe memory
315 reference is not. Interchanging a dead type-unsafe memory reference with
316 a live type-safe one creates a live type-unsafe memory reference, in other
317 words, it makes the program illegal.
318 We check here conservatively whether the two memory references have equal
319 memory attributes. */
321 return mem_attrs_eq_p (get_mem_attrs (a), get_mem_attrs (b));
325 /* Go through a bunch of insns, converting them to conditional
326 execution format if possible. Return TRUE if all of the non-note
327 insns were processed. */
329 static int
330 cond_exec_process_insns (ce_if_block *ce_info ATTRIBUTE_UNUSED,
331 /* if block information */rtx_insn *start,
332 /* first insn to look at */rtx end,
333 /* last insn to look at */rtx test,
334 /* conditional execution test */int prob_val,
335 /* probability of branch taken. */int mod_ok)
337 int must_be_last = FALSE;
338 rtx_insn *insn;
339 rtx xtest;
340 rtx pattern;
342 if (!start || !end)
343 return FALSE;
345 for (insn = start; ; insn = NEXT_INSN (insn))
347 /* dwarf2out can't cope with conditional prologues. */
348 if (NOTE_P (insn) && NOTE_KIND (insn) == NOTE_INSN_PROLOGUE_END)
349 return FALSE;
351 if (NOTE_P (insn) || DEBUG_INSN_P (insn))
352 goto insn_done;
354 gcc_assert (NONJUMP_INSN_P (insn) || CALL_P (insn));
356 /* dwarf2out can't cope with conditional unwind info. */
357 if (RTX_FRAME_RELATED_P (insn))
358 return FALSE;
360 /* Remove USE insns that get in the way. */
361 if (reload_completed && GET_CODE (PATTERN (insn)) == USE)
363 /* ??? Ug. Actually unlinking the thing is problematic,
364 given what we'd have to coordinate with our callers. */
365 SET_INSN_DELETED (insn);
366 goto insn_done;
369 /* Last insn wasn't last? */
370 if (must_be_last)
371 return FALSE;
373 if (modified_in_p (test, insn))
375 if (!mod_ok)
376 return FALSE;
377 must_be_last = TRUE;
380 /* Now build the conditional form of the instruction. */
381 pattern = PATTERN (insn);
382 xtest = copy_rtx (test);
384 /* If this is already a COND_EXEC, rewrite the test to be an AND of the
385 two conditions. */
386 if (GET_CODE (pattern) == COND_EXEC)
388 if (GET_MODE (xtest) != GET_MODE (COND_EXEC_TEST (pattern)))
389 return FALSE;
391 xtest = gen_rtx_AND (GET_MODE (xtest), xtest,
392 COND_EXEC_TEST (pattern));
393 pattern = COND_EXEC_CODE (pattern);
396 pattern = gen_rtx_COND_EXEC (VOIDmode, xtest, pattern);
398 /* If the machine needs to modify the insn being conditionally executed,
399 say for example to force a constant integer operand into a temp
400 register, do so here. */
401 #ifdef IFCVT_MODIFY_INSN
402 IFCVT_MODIFY_INSN (ce_info, pattern, insn);
403 if (! pattern)
404 return FALSE;
405 #endif
407 validate_change (insn, &PATTERN (insn), pattern, 1);
409 if (CALL_P (insn) && prob_val >= 0)
410 validate_change (insn, &REG_NOTES (insn),
411 gen_rtx_INT_LIST ((machine_mode) REG_BR_PROB,
412 prob_val, REG_NOTES (insn)), 1);
414 insn_done:
415 if (insn == end)
416 break;
419 return TRUE;
422 /* Return the condition for a jump. Do not do any special processing. */
424 static rtx
425 cond_exec_get_condition (rtx_insn *jump)
427 rtx test_if, cond;
429 if (any_condjump_p (jump))
430 test_if = SET_SRC (pc_set (jump));
431 else
432 return NULL_RTX;
433 cond = XEXP (test_if, 0);
435 /* If this branches to JUMP_LABEL when the condition is false,
436 reverse the condition. */
437 if (GET_CODE (XEXP (test_if, 2)) == LABEL_REF
438 && LABEL_REF_LABEL (XEXP (test_if, 2)) == JUMP_LABEL (jump))
440 enum rtx_code rev = reversed_comparison_code (cond, jump);
441 if (rev == UNKNOWN)
442 return NULL_RTX;
444 cond = gen_rtx_fmt_ee (rev, GET_MODE (cond), XEXP (cond, 0),
445 XEXP (cond, 1));
448 return cond;
451 /* Given a simple IF-THEN or IF-THEN-ELSE block, attempt to convert it
452 to conditional execution. Return TRUE if we were successful at
453 converting the block. */
455 static int
456 cond_exec_process_if_block (ce_if_block * ce_info,
457 /* if block information */int do_multiple_p)
459 basic_block test_bb = ce_info->test_bb; /* last test block */
460 basic_block then_bb = ce_info->then_bb; /* THEN */
461 basic_block else_bb = ce_info->else_bb; /* ELSE or NULL */
462 rtx test_expr; /* expression in IF_THEN_ELSE that is tested */
463 rtx_insn *then_start; /* first insn in THEN block */
464 rtx_insn *then_end; /* last insn + 1 in THEN block */
465 rtx_insn *else_start = NULL; /* first insn in ELSE block or NULL */
466 rtx_insn *else_end = NULL; /* last insn + 1 in ELSE block */
467 int max; /* max # of insns to convert. */
468 int then_mod_ok; /* whether conditional mods are ok in THEN */
469 rtx true_expr; /* test for else block insns */
470 rtx false_expr; /* test for then block insns */
471 int true_prob_val; /* probability of else block */
472 int false_prob_val; /* probability of then block */
473 rtx_insn *then_last_head = NULL; /* Last match at the head of THEN */
474 rtx_insn *else_last_head = NULL; /* Last match at the head of ELSE */
475 rtx_insn *then_first_tail = NULL; /* First match at the tail of THEN */
476 rtx_insn *else_first_tail = NULL; /* First match at the tail of ELSE */
477 int then_n_insns, else_n_insns, n_insns;
478 enum rtx_code false_code;
479 rtx note;
481 /* If test is comprised of && or || elements, and we've failed at handling
482 all of them together, just use the last test if it is the special case of
483 && elements without an ELSE block. */
484 if (!do_multiple_p && ce_info->num_multiple_test_blocks)
486 if (else_bb || ! ce_info->and_and_p)
487 return FALSE;
489 ce_info->test_bb = test_bb = ce_info->last_test_bb;
490 ce_info->num_multiple_test_blocks = 0;
491 ce_info->num_and_and_blocks = 0;
492 ce_info->num_or_or_blocks = 0;
495 /* Find the conditional jump to the ELSE or JOIN part, and isolate
496 the test. */
497 test_expr = cond_exec_get_condition (BB_END (test_bb));
498 if (! test_expr)
499 return FALSE;
501 /* If the conditional jump is more than just a conditional jump,
502 then we can not do conditional execution conversion on this block. */
503 if (! onlyjump_p (BB_END (test_bb)))
504 return FALSE;
506 /* Collect the bounds of where we're to search, skipping any labels, jumps
507 and notes at the beginning and end of the block. Then count the total
508 number of insns and see if it is small enough to convert. */
509 then_start = first_active_insn (then_bb);
510 then_end = last_active_insn (then_bb, TRUE);
511 then_n_insns = ce_info->num_then_insns = count_bb_insns (then_bb);
512 n_insns = then_n_insns;
513 max = MAX_CONDITIONAL_EXECUTE;
515 if (else_bb)
517 int n_matching;
519 max *= 2;
520 else_start = first_active_insn (else_bb);
521 else_end = last_active_insn (else_bb, TRUE);
522 else_n_insns = ce_info->num_else_insns = count_bb_insns (else_bb);
523 n_insns += else_n_insns;
525 /* Look for matching sequences at the head and tail of the two blocks,
526 and limit the range of insns to be converted if possible. */
527 n_matching = flow_find_cross_jump (then_bb, else_bb,
528 &then_first_tail, &else_first_tail,
529 NULL);
530 if (then_first_tail == BB_HEAD (then_bb))
531 then_start = then_end = NULL;
532 if (else_first_tail == BB_HEAD (else_bb))
533 else_start = else_end = NULL;
535 if (n_matching > 0)
537 if (then_end)
538 then_end = find_active_insn_before (then_bb, then_first_tail);
539 if (else_end)
540 else_end = find_active_insn_before (else_bb, else_first_tail);
541 n_insns -= 2 * n_matching;
544 if (then_start
545 && else_start
546 && then_n_insns > n_matching
547 && else_n_insns > n_matching)
549 int longest_match = MIN (then_n_insns - n_matching,
550 else_n_insns - n_matching);
551 n_matching
552 = flow_find_head_matching_sequence (then_bb, else_bb,
553 &then_last_head,
554 &else_last_head,
555 longest_match);
557 if (n_matching > 0)
559 rtx_insn *insn;
561 /* We won't pass the insns in the head sequence to
562 cond_exec_process_insns, so we need to test them here
563 to make sure that they don't clobber the condition. */
564 for (insn = BB_HEAD (then_bb);
565 insn != NEXT_INSN (then_last_head);
566 insn = NEXT_INSN (insn))
567 if (!LABEL_P (insn) && !NOTE_P (insn)
568 && !DEBUG_INSN_P (insn)
569 && modified_in_p (test_expr, insn))
570 return FALSE;
573 if (then_last_head == then_end)
574 then_start = then_end = NULL;
575 if (else_last_head == else_end)
576 else_start = else_end = NULL;
578 if (n_matching > 0)
580 if (then_start)
581 then_start = find_active_insn_after (then_bb, then_last_head);
582 if (else_start)
583 else_start = find_active_insn_after (else_bb, else_last_head);
584 n_insns -= 2 * n_matching;
589 if (n_insns > max)
590 return FALSE;
592 /* Map test_expr/test_jump into the appropriate MD tests to use on
593 the conditionally executed code. */
595 true_expr = test_expr;
597 false_code = reversed_comparison_code (true_expr, BB_END (test_bb));
598 if (false_code != UNKNOWN)
599 false_expr = gen_rtx_fmt_ee (false_code, GET_MODE (true_expr),
600 XEXP (true_expr, 0), XEXP (true_expr, 1));
601 else
602 false_expr = NULL_RTX;
604 #ifdef IFCVT_MODIFY_TESTS
605 /* If the machine description needs to modify the tests, such as setting a
606 conditional execution register from a comparison, it can do so here. */
607 IFCVT_MODIFY_TESTS (ce_info, true_expr, false_expr);
609 /* See if the conversion failed. */
610 if (!true_expr || !false_expr)
611 goto fail;
612 #endif
614 note = find_reg_note (BB_END (test_bb), REG_BR_PROB, NULL_RTX);
615 if (note)
617 true_prob_val = XINT (note, 0);
618 false_prob_val = REG_BR_PROB_BASE - true_prob_val;
620 else
622 true_prob_val = -1;
623 false_prob_val = -1;
626 /* If we have && or || tests, do them here. These tests are in the adjacent
627 blocks after the first block containing the test. */
628 if (ce_info->num_multiple_test_blocks > 0)
630 basic_block bb = test_bb;
631 basic_block last_test_bb = ce_info->last_test_bb;
633 if (! false_expr)
634 goto fail;
638 rtx_insn *start, *end;
639 rtx t, f;
640 enum rtx_code f_code;
642 bb = block_fallthru (bb);
643 start = first_active_insn (bb);
644 end = last_active_insn (bb, TRUE);
645 if (start
646 && ! cond_exec_process_insns (ce_info, start, end, false_expr,
647 false_prob_val, FALSE))
648 goto fail;
650 /* If the conditional jump is more than just a conditional jump, then
651 we can not do conditional execution conversion on this block. */
652 if (! onlyjump_p (BB_END (bb)))
653 goto fail;
655 /* Find the conditional jump and isolate the test. */
656 t = cond_exec_get_condition (BB_END (bb));
657 if (! t)
658 goto fail;
660 f_code = reversed_comparison_code (t, BB_END (bb));
661 if (f_code == UNKNOWN)
662 goto fail;
664 f = gen_rtx_fmt_ee (f_code, GET_MODE (t), XEXP (t, 0), XEXP (t, 1));
665 if (ce_info->and_and_p)
667 t = gen_rtx_AND (GET_MODE (t), true_expr, t);
668 f = gen_rtx_IOR (GET_MODE (t), false_expr, f);
670 else
672 t = gen_rtx_IOR (GET_MODE (t), true_expr, t);
673 f = gen_rtx_AND (GET_MODE (t), false_expr, f);
676 /* If the machine description needs to modify the tests, such as
677 setting a conditional execution register from a comparison, it can
678 do so here. */
679 #ifdef IFCVT_MODIFY_MULTIPLE_TESTS
680 IFCVT_MODIFY_MULTIPLE_TESTS (ce_info, bb, t, f);
682 /* See if the conversion failed. */
683 if (!t || !f)
684 goto fail;
685 #endif
687 true_expr = t;
688 false_expr = f;
690 while (bb != last_test_bb);
693 /* For IF-THEN-ELSE blocks, we don't allow modifications of the test
694 on then THEN block. */
695 then_mod_ok = (else_bb == NULL_BLOCK);
697 /* Go through the THEN and ELSE blocks converting the insns if possible
698 to conditional execution. */
700 if (then_end
701 && (! false_expr
702 || ! cond_exec_process_insns (ce_info, then_start, then_end,
703 false_expr, false_prob_val,
704 then_mod_ok)))
705 goto fail;
707 if (else_bb && else_end
708 && ! cond_exec_process_insns (ce_info, else_start, else_end,
709 true_expr, true_prob_val, TRUE))
710 goto fail;
712 /* If we cannot apply the changes, fail. Do not go through the normal fail
713 processing, since apply_change_group will call cancel_changes. */
714 if (! apply_change_group ())
716 #ifdef IFCVT_MODIFY_CANCEL
717 /* Cancel any machine dependent changes. */
718 IFCVT_MODIFY_CANCEL (ce_info);
719 #endif
720 return FALSE;
723 #ifdef IFCVT_MODIFY_FINAL
724 /* Do any machine dependent final modifications. */
725 IFCVT_MODIFY_FINAL (ce_info);
726 #endif
728 /* Conversion succeeded. */
729 if (dump_file)
730 fprintf (dump_file, "%d insn%s converted to conditional execution.\n",
731 n_insns, (n_insns == 1) ? " was" : "s were");
733 /* Merge the blocks! If we had matching sequences, make sure to delete one
734 copy at the appropriate location first: delete the copy in the THEN branch
735 for a tail sequence so that the remaining one is executed last for both
736 branches, and delete the copy in the ELSE branch for a head sequence so
737 that the remaining one is executed first for both branches. */
738 if (then_first_tail)
740 rtx_insn *from = then_first_tail;
741 if (!INSN_P (from))
742 from = find_active_insn_after (then_bb, from);
743 delete_insn_chain (from, get_last_bb_insn (then_bb), false);
745 if (else_last_head)
746 delete_insn_chain (first_active_insn (else_bb), else_last_head, false);
748 merge_if_block (ce_info);
749 cond_exec_changed_p = TRUE;
750 return TRUE;
752 fail:
753 #ifdef IFCVT_MODIFY_CANCEL
754 /* Cancel any machine dependent changes. */
755 IFCVT_MODIFY_CANCEL (ce_info);
756 #endif
758 cancel_changes (0);
759 return FALSE;
762 /* Used by noce_process_if_block to communicate with its subroutines.
764 The subroutines know that A and B may be evaluated freely. They
765 know that X is a register. They should insert new instructions
766 before cond_earliest. */
768 struct noce_if_info
770 /* The basic blocks that make up the IF-THEN-{ELSE-,}JOIN block. */
771 basic_block test_bb, then_bb, else_bb, join_bb;
773 /* The jump that ends TEST_BB. */
774 rtx_insn *jump;
776 /* The jump condition. */
777 rtx cond;
779 /* New insns should be inserted before this one. */
780 rtx_insn *cond_earliest;
782 /* Insns in the THEN and ELSE block. There is always just this
783 one insns in those blocks. The insns are single_set insns.
784 If there was no ELSE block, INSN_B is the last insn before
785 COND_EARLIEST, or NULL_RTX. In the former case, the insn
786 operands are still valid, as if INSN_B was moved down below
787 the jump. */
788 rtx_insn *insn_a, *insn_b;
790 /* The SET_SRC of INSN_A and INSN_B. */
791 rtx a, b;
793 /* The SET_DEST of INSN_A. */
794 rtx x;
796 /* The original set destination that the THEN and ELSE basic blocks finally
797 write their result to. */
798 rtx orig_x;
799 /* True if this if block is not canonical. In the canonical form of
800 if blocks, the THEN_BB is the block reached via the fallthru edge
801 from TEST_BB. For the noce transformations, we allow the symmetric
802 form as well. */
803 bool then_else_reversed;
805 /* True if the contents of then_bb and else_bb are a
806 simple single set instruction. */
807 bool then_simple;
808 bool else_simple;
810 /* True if we're optimisizing the control block for speed, false if
811 we're optimizing for size. */
812 bool speed_p;
814 /* The combined cost of COND, JUMP and the costs for THEN_BB and
815 ELSE_BB. */
816 unsigned int original_cost;
818 /* Maximum permissible cost for the unconditional sequence we should
819 generate to replace this branch. */
820 unsigned int max_seq_cost;
822 /* The name of the noce transform that succeeded in if-converting
823 this structure. Used for debugging. */
824 const char *transform_name;
827 static rtx noce_emit_store_flag (struct noce_if_info *, rtx, int, int);
828 static int noce_try_move (struct noce_if_info *);
829 static int noce_try_ifelse_collapse (struct noce_if_info *);
830 static int noce_try_store_flag (struct noce_if_info *);
831 static int noce_try_addcc (struct noce_if_info *);
832 static int noce_try_store_flag_constants (struct noce_if_info *);
833 static int noce_try_store_flag_mask (struct noce_if_info *);
834 static rtx noce_emit_cmove (struct noce_if_info *, rtx, enum rtx_code, rtx,
835 rtx, rtx, rtx);
836 static int noce_try_cmove (struct noce_if_info *);
837 static int noce_try_cmove_arith (struct noce_if_info *);
838 static rtx noce_get_alt_condition (struct noce_if_info *, rtx, rtx_insn **);
839 static int noce_try_minmax (struct noce_if_info *);
840 static int noce_try_abs (struct noce_if_info *);
841 static int noce_try_sign_mask (struct noce_if_info *);
843 /* Return TRUE if SEQ is a good candidate as a replacement for the
844 if-convertible sequence described in IF_INFO. */
846 inline static bool
847 noce_conversion_profitable_p (rtx_insn *seq, struct noce_if_info *if_info)
849 bool speed_p = if_info->speed_p;
851 /* Cost up the new sequence. */
852 unsigned int cost = seq_cost (seq, speed_p);
854 /* When compiling for size, we can make a reasonably accurately guess
855 at the size growth. */
856 if (!speed_p)
857 return cost <= if_info->original_cost;
858 else
859 return cost <= if_info->max_seq_cost;
862 /* Helper function for noce_try_store_flag*. */
864 static rtx
865 noce_emit_store_flag (struct noce_if_info *if_info, rtx x, int reversep,
866 int normalize)
868 rtx cond = if_info->cond;
869 int cond_complex;
870 enum rtx_code code;
872 cond_complex = (! general_operand (XEXP (cond, 0), VOIDmode)
873 || ! general_operand (XEXP (cond, 1), VOIDmode));
875 /* If earliest == jump, or when the condition is complex, try to
876 build the store_flag insn directly. */
878 if (cond_complex)
880 rtx set = pc_set (if_info->jump);
881 cond = XEXP (SET_SRC (set), 0);
882 if (GET_CODE (XEXP (SET_SRC (set), 2)) == LABEL_REF
883 && LABEL_REF_LABEL (XEXP (SET_SRC (set), 2)) == JUMP_LABEL (if_info->jump))
884 reversep = !reversep;
885 if (if_info->then_else_reversed)
886 reversep = !reversep;
889 if (reversep)
890 code = reversed_comparison_code (cond, if_info->jump);
891 else
892 code = GET_CODE (cond);
894 if ((if_info->cond_earliest == if_info->jump || cond_complex)
895 && (normalize == 0 || STORE_FLAG_VALUE == normalize))
897 rtx src = gen_rtx_fmt_ee (code, GET_MODE (x), XEXP (cond, 0),
898 XEXP (cond, 1));
899 rtx set = gen_rtx_SET (x, src);
901 start_sequence ();
902 rtx_insn *insn = emit_insn (set);
904 if (recog_memoized (insn) >= 0)
906 rtx_insn *seq = get_insns ();
907 end_sequence ();
908 emit_insn (seq);
910 if_info->cond_earliest = if_info->jump;
912 return x;
915 end_sequence ();
918 /* Don't even try if the comparison operands or the mode of X are weird. */
919 if (cond_complex || !SCALAR_INT_MODE_P (GET_MODE (x)))
920 return NULL_RTX;
922 return emit_store_flag (x, code, XEXP (cond, 0),
923 XEXP (cond, 1), VOIDmode,
924 (code == LTU || code == LEU
925 || code == GEU || code == GTU), normalize);
928 /* Emit instruction to move an rtx, possibly into STRICT_LOW_PART.
929 X is the destination/target and Y is the value to copy. */
931 static void
932 noce_emit_move_insn (rtx x, rtx y)
934 machine_mode outmode;
935 rtx outer, inner;
936 int bitpos;
938 if (GET_CODE (x) != STRICT_LOW_PART)
940 rtx_insn *seq, *insn;
941 rtx target;
942 optab ot;
944 start_sequence ();
945 /* Check that the SET_SRC is reasonable before calling emit_move_insn,
946 otherwise construct a suitable SET pattern ourselves. */
947 insn = (OBJECT_P (y) || CONSTANT_P (y) || GET_CODE (y) == SUBREG)
948 ? emit_move_insn (x, y)
949 : emit_insn (gen_rtx_SET (x, y));
950 seq = get_insns ();
951 end_sequence ();
953 if (recog_memoized (insn) <= 0)
955 if (GET_CODE (x) == ZERO_EXTRACT)
957 rtx op = XEXP (x, 0);
958 unsigned HOST_WIDE_INT size = INTVAL (XEXP (x, 1));
959 unsigned HOST_WIDE_INT start = INTVAL (XEXP (x, 2));
961 /* store_bit_field expects START to be relative to
962 BYTES_BIG_ENDIAN and adjusts this value for machines with
963 BITS_BIG_ENDIAN != BYTES_BIG_ENDIAN. In order to be able to
964 invoke store_bit_field again it is necessary to have the START
965 value from the first call. */
966 if (BITS_BIG_ENDIAN != BYTES_BIG_ENDIAN)
968 if (MEM_P (op))
969 start = BITS_PER_UNIT - start - size;
970 else
972 gcc_assert (REG_P (op));
973 start = BITS_PER_WORD - start - size;
977 gcc_assert (start < (MEM_P (op) ? BITS_PER_UNIT : BITS_PER_WORD));
978 store_bit_field (op, size, start, 0, 0, GET_MODE (x), y, false);
979 return;
982 switch (GET_RTX_CLASS (GET_CODE (y)))
984 case RTX_UNARY:
985 ot = code_to_optab (GET_CODE (y));
986 if (ot)
988 start_sequence ();
989 target = expand_unop (GET_MODE (y), ot, XEXP (y, 0), x, 0);
990 if (target != NULL_RTX)
992 if (target != x)
993 emit_move_insn (x, target);
994 seq = get_insns ();
996 end_sequence ();
998 break;
1000 case RTX_BIN_ARITH:
1001 case RTX_COMM_ARITH:
1002 ot = code_to_optab (GET_CODE (y));
1003 if (ot)
1005 start_sequence ();
1006 target = expand_binop (GET_MODE (y), ot,
1007 XEXP (y, 0), XEXP (y, 1),
1008 x, 0, OPTAB_DIRECT);
1009 if (target != NULL_RTX)
1011 if (target != x)
1012 emit_move_insn (x, target);
1013 seq = get_insns ();
1015 end_sequence ();
1017 break;
1019 default:
1020 break;
1024 emit_insn (seq);
1025 return;
1028 outer = XEXP (x, 0);
1029 inner = XEXP (outer, 0);
1030 outmode = GET_MODE (outer);
1031 bitpos = SUBREG_BYTE (outer) * BITS_PER_UNIT;
1032 store_bit_field (inner, GET_MODE_BITSIZE (outmode), bitpos,
1033 0, 0, outmode, y, false);
1036 /* Return the CC reg if it is used in COND. */
1038 static rtx
1039 cc_in_cond (rtx cond)
1041 if (have_cbranchcc4 && cond
1042 && GET_MODE_CLASS (GET_MODE (XEXP (cond, 0))) == MODE_CC)
1043 return XEXP (cond, 0);
1045 return NULL_RTX;
1048 /* Return sequence of instructions generated by if conversion. This
1049 function calls end_sequence() to end the current stream, ensures
1050 that the instructions are unshared, recognizable non-jump insns.
1051 On failure, this function returns a NULL_RTX. */
1053 static rtx_insn *
1054 end_ifcvt_sequence (struct noce_if_info *if_info)
1056 rtx_insn *insn;
1057 rtx_insn *seq = get_insns ();
1058 rtx cc = cc_in_cond (if_info->cond);
1060 set_used_flags (if_info->x);
1061 set_used_flags (if_info->cond);
1062 set_used_flags (if_info->a);
1063 set_used_flags (if_info->b);
1065 for (insn = seq; insn; insn = NEXT_INSN (insn))
1066 set_used_flags (insn);
1068 unshare_all_rtl_in_chain (seq);
1069 end_sequence ();
1071 /* Make sure that all of the instructions emitted are recognizable,
1072 and that we haven't introduced a new jump instruction.
1073 As an exercise for the reader, build a general mechanism that
1074 allows proper placement of required clobbers. */
1075 for (insn = seq; insn; insn = NEXT_INSN (insn))
1076 if (JUMP_P (insn)
1077 || recog_memoized (insn) == -1
1078 /* Make sure new generated code does not clobber CC. */
1079 || (cc && set_of (cc, insn)))
1080 return NULL;
1082 return seq;
1085 /* Return true iff the then and else basic block (if it exists)
1086 consist of a single simple set instruction. */
1088 static bool
1089 noce_simple_bbs (struct noce_if_info *if_info)
1091 if (!if_info->then_simple)
1092 return false;
1094 if (if_info->else_bb)
1095 return if_info->else_simple;
1097 return true;
1100 /* Convert "if (a != b) x = a; else x = b" into "x = a" and
1101 "if (a == b) x = a; else x = b" into "x = b". */
1103 static int
1104 noce_try_move (struct noce_if_info *if_info)
1106 rtx cond = if_info->cond;
1107 enum rtx_code code = GET_CODE (cond);
1108 rtx y;
1109 rtx_insn *seq;
1111 if (code != NE && code != EQ)
1112 return FALSE;
1114 if (!noce_simple_bbs (if_info))
1115 return FALSE;
1117 /* This optimization isn't valid if either A or B could be a NaN
1118 or a signed zero. */
1119 if (HONOR_NANS (if_info->x)
1120 || HONOR_SIGNED_ZEROS (if_info->x))
1121 return FALSE;
1123 /* Check whether the operands of the comparison are A and in
1124 either order. */
1125 if ((rtx_equal_p (if_info->a, XEXP (cond, 0))
1126 && rtx_equal_p (if_info->b, XEXP (cond, 1)))
1127 || (rtx_equal_p (if_info->a, XEXP (cond, 1))
1128 && rtx_equal_p (if_info->b, XEXP (cond, 0))))
1130 if (!rtx_interchangeable_p (if_info->a, if_info->b))
1131 return FALSE;
1133 y = (code == EQ) ? if_info->a : if_info->b;
1135 /* Avoid generating the move if the source is the destination. */
1136 if (! rtx_equal_p (if_info->x, y))
1138 start_sequence ();
1139 noce_emit_move_insn (if_info->x, y);
1140 seq = end_ifcvt_sequence (if_info);
1141 if (!seq)
1142 return FALSE;
1144 emit_insn_before_setloc (seq, if_info->jump,
1145 INSN_LOCATION (if_info->insn_a));
1147 if_info->transform_name = "noce_try_move";
1148 return TRUE;
1150 return FALSE;
1153 /* Try forming an IF_THEN_ELSE (cond, b, a) and collapsing that
1154 through simplify_rtx. Sometimes that can eliminate the IF_THEN_ELSE.
1155 If that is the case, emit the result into x. */
1157 static int
1158 noce_try_ifelse_collapse (struct noce_if_info * if_info)
1160 if (!noce_simple_bbs (if_info))
1161 return FALSE;
1163 machine_mode mode = GET_MODE (if_info->x);
1164 rtx if_then_else = simplify_gen_ternary (IF_THEN_ELSE, mode, mode,
1165 if_info->cond, if_info->b,
1166 if_info->a);
1168 if (GET_CODE (if_then_else) == IF_THEN_ELSE)
1169 return FALSE;
1171 rtx_insn *seq;
1172 start_sequence ();
1173 noce_emit_move_insn (if_info->x, if_then_else);
1174 seq = end_ifcvt_sequence (if_info);
1175 if (!seq)
1176 return FALSE;
1178 emit_insn_before_setloc (seq, if_info->jump,
1179 INSN_LOCATION (if_info->insn_a));
1181 if_info->transform_name = "noce_try_ifelse_collapse";
1182 return TRUE;
1186 /* Convert "if (test) x = 1; else x = 0".
1188 Only try 0 and STORE_FLAG_VALUE here. Other combinations will be
1189 tried in noce_try_store_flag_constants after noce_try_cmove has had
1190 a go at the conversion. */
1192 static int
1193 noce_try_store_flag (struct noce_if_info *if_info)
1195 int reversep;
1196 rtx target;
1197 rtx_insn *seq;
1199 if (!noce_simple_bbs (if_info))
1200 return FALSE;
1202 if (CONST_INT_P (if_info->b)
1203 && INTVAL (if_info->b) == STORE_FLAG_VALUE
1204 && if_info->a == const0_rtx)
1205 reversep = 0;
1206 else if (if_info->b == const0_rtx
1207 && CONST_INT_P (if_info->a)
1208 && INTVAL (if_info->a) == STORE_FLAG_VALUE
1209 && (reversed_comparison_code (if_info->cond, if_info->jump)
1210 != UNKNOWN))
1211 reversep = 1;
1212 else
1213 return FALSE;
1215 start_sequence ();
1217 target = noce_emit_store_flag (if_info, if_info->x, reversep, 0);
1218 if (target)
1220 if (target != if_info->x)
1221 noce_emit_move_insn (if_info->x, target);
1223 seq = end_ifcvt_sequence (if_info);
1224 if (! seq)
1225 return FALSE;
1227 emit_insn_before_setloc (seq, if_info->jump,
1228 INSN_LOCATION (if_info->insn_a));
1229 if_info->transform_name = "noce_try_store_flag";
1230 return TRUE;
1232 else
1234 end_sequence ();
1235 return FALSE;
1240 /* Convert "if (test) x = -A; else x = A" into
1241 x = A; if (test) x = -x if the machine can do the
1242 conditional negate form of this cheaply.
1243 Try this before noce_try_cmove that will just load the
1244 immediates into two registers and do a conditional select
1245 between them. If the target has a conditional negate or
1246 conditional invert operation we can save a potentially
1247 expensive constant synthesis. */
1249 static bool
1250 noce_try_inverse_constants (struct noce_if_info *if_info)
1252 if (!noce_simple_bbs (if_info))
1253 return false;
1255 if (!CONST_INT_P (if_info->a)
1256 || !CONST_INT_P (if_info->b)
1257 || !REG_P (if_info->x))
1258 return false;
1260 machine_mode mode = GET_MODE (if_info->x);
1262 HOST_WIDE_INT val_a = INTVAL (if_info->a);
1263 HOST_WIDE_INT val_b = INTVAL (if_info->b);
1265 rtx cond = if_info->cond;
1267 rtx x = if_info->x;
1268 rtx target;
1270 start_sequence ();
1272 rtx_code code;
1273 if (val_b != HOST_WIDE_INT_MIN && val_a == -val_b)
1274 code = NEG;
1275 else if (val_a == ~val_b)
1276 code = NOT;
1277 else
1279 end_sequence ();
1280 return false;
1283 rtx tmp = gen_reg_rtx (mode);
1284 noce_emit_move_insn (tmp, if_info->a);
1286 target = emit_conditional_neg_or_complement (x, code, mode, cond, tmp, tmp);
1288 if (target)
1290 rtx_insn *seq = get_insns ();
1292 if (!seq)
1294 end_sequence ();
1295 return false;
1298 if (target != if_info->x)
1299 noce_emit_move_insn (if_info->x, target);
1301 seq = end_ifcvt_sequence (if_info);
1303 if (!seq)
1304 return false;
1306 emit_insn_before_setloc (seq, if_info->jump,
1307 INSN_LOCATION (if_info->insn_a));
1308 if_info->transform_name = "noce_try_inverse_constants";
1309 return true;
1312 end_sequence ();
1313 return false;
1317 /* Convert "if (test) x = a; else x = b", for A and B constant.
1318 Also allow A = y + c1, B = y + c2, with a common y between A
1319 and B. */
1321 static int
1322 noce_try_store_flag_constants (struct noce_if_info *if_info)
1324 rtx target;
1325 rtx_insn *seq;
1326 bool reversep;
1327 HOST_WIDE_INT itrue, ifalse, diff, tmp;
1328 int normalize;
1329 bool can_reverse;
1330 machine_mode mode = GET_MODE (if_info->x);;
1331 rtx common = NULL_RTX;
1333 rtx a = if_info->a;
1334 rtx b = if_info->b;
1336 /* Handle cases like x := test ? y + 3 : y + 4. */
1337 if (GET_CODE (a) == PLUS
1338 && GET_CODE (b) == PLUS
1339 && CONST_INT_P (XEXP (a, 1))
1340 && CONST_INT_P (XEXP (b, 1))
1341 && rtx_equal_p (XEXP (a, 0), XEXP (b, 0))
1342 /* Allow expressions that are not using the result or plain
1343 registers where we handle overlap below. */
1344 && (REG_P (XEXP (a, 0))
1345 || (noce_operand_ok (XEXP (a, 0))
1346 && ! reg_overlap_mentioned_p (if_info->x, XEXP (a, 0)))))
1348 common = XEXP (a, 0);
1349 a = XEXP (a, 1);
1350 b = XEXP (b, 1);
1353 if (!noce_simple_bbs (if_info))
1354 return FALSE;
1356 if (CONST_INT_P (a)
1357 && CONST_INT_P (b))
1359 ifalse = INTVAL (a);
1360 itrue = INTVAL (b);
1361 bool subtract_flag_p = false;
1363 diff = (unsigned HOST_WIDE_INT) itrue - ifalse;
1364 /* Make sure we can represent the difference between the two values. */
1365 if ((diff > 0)
1366 != ((ifalse < 0) != (itrue < 0) ? ifalse < 0 : ifalse < itrue))
1367 return FALSE;
1369 diff = trunc_int_for_mode (diff, mode);
1371 can_reverse = (reversed_comparison_code (if_info->cond, if_info->jump)
1372 != UNKNOWN);
1374 reversep = false;
1375 if (diff == STORE_FLAG_VALUE || diff == -STORE_FLAG_VALUE)
1377 normalize = 0;
1378 /* We could collapse these cases but it is easier to follow the
1379 diff/STORE_FLAG_VALUE combinations when they are listed
1380 explicitly. */
1382 /* test ? 3 : 4
1383 => 4 + (test != 0). */
1384 if (diff < 0 && STORE_FLAG_VALUE < 0)
1385 reversep = false;
1386 /* test ? 4 : 3
1387 => can_reverse | 4 + (test == 0)
1388 !can_reverse | 3 - (test != 0). */
1389 else if (diff > 0 && STORE_FLAG_VALUE < 0)
1391 reversep = can_reverse;
1392 subtract_flag_p = !can_reverse;
1393 /* If we need to subtract the flag and we have PLUS-immediate
1394 A and B then it is unlikely to be beneficial to play tricks
1395 here. */
1396 if (subtract_flag_p && common)
1397 return FALSE;
1399 /* test ? 3 : 4
1400 => can_reverse | 3 + (test == 0)
1401 !can_reverse | 4 - (test != 0). */
1402 else if (diff < 0 && STORE_FLAG_VALUE > 0)
1404 reversep = can_reverse;
1405 subtract_flag_p = !can_reverse;
1406 /* If we need to subtract the flag and we have PLUS-immediate
1407 A and B then it is unlikely to be beneficial to play tricks
1408 here. */
1409 if (subtract_flag_p && common)
1410 return FALSE;
1412 /* test ? 4 : 3
1413 => 4 + (test != 0). */
1414 else if (diff > 0 && STORE_FLAG_VALUE > 0)
1415 reversep = false;
1416 else
1417 gcc_unreachable ();
1419 /* Is this (cond) ? 2^n : 0? */
1420 else if (ifalse == 0 && pow2p_hwi (itrue)
1421 && STORE_FLAG_VALUE == 1)
1422 normalize = 1;
1423 /* Is this (cond) ? 0 : 2^n? */
1424 else if (itrue == 0 && pow2p_hwi (ifalse) && can_reverse
1425 && STORE_FLAG_VALUE == 1)
1427 normalize = 1;
1428 reversep = true;
1430 /* Is this (cond) ? -1 : x? */
1431 else if (itrue == -1
1432 && STORE_FLAG_VALUE == -1)
1433 normalize = -1;
1434 /* Is this (cond) ? x : -1? */
1435 else if (ifalse == -1 && can_reverse
1436 && STORE_FLAG_VALUE == -1)
1438 normalize = -1;
1439 reversep = true;
1441 else
1442 return FALSE;
1444 if (reversep)
1446 std::swap (itrue, ifalse);
1447 diff = trunc_int_for_mode (-(unsigned HOST_WIDE_INT) diff, mode);
1450 start_sequence ();
1452 /* If we have x := test ? x + 3 : x + 4 then move the original
1453 x out of the way while we store flags. */
1454 if (common && rtx_equal_p (common, if_info->x))
1456 common = gen_reg_rtx (mode);
1457 noce_emit_move_insn (common, if_info->x);
1460 target = noce_emit_store_flag (if_info, if_info->x, reversep, normalize);
1461 if (! target)
1463 end_sequence ();
1464 return FALSE;
1467 /* if (test) x = 3; else x = 4;
1468 => x = 3 + (test == 0); */
1469 if (diff == STORE_FLAG_VALUE || diff == -STORE_FLAG_VALUE)
1471 /* Add the common part now. This may allow combine to merge this
1472 with the store flag operation earlier into some sort of conditional
1473 increment/decrement if the target allows it. */
1474 if (common)
1475 target = expand_simple_binop (mode, PLUS,
1476 target, common,
1477 target, 0, OPTAB_WIDEN);
1479 /* Always use ifalse here. It should have been swapped with itrue
1480 when appropriate when reversep is true. */
1481 target = expand_simple_binop (mode, subtract_flag_p ? MINUS : PLUS,
1482 gen_int_mode (ifalse, mode), target,
1483 if_info->x, 0, OPTAB_WIDEN);
1485 /* Other cases are not beneficial when the original A and B are PLUS
1486 expressions. */
1487 else if (common)
1489 end_sequence ();
1490 return FALSE;
1492 /* if (test) x = 8; else x = 0;
1493 => x = (test != 0) << 3; */
1494 else if (ifalse == 0 && (tmp = exact_log2 (itrue)) >= 0)
1496 target = expand_simple_binop (mode, ASHIFT,
1497 target, GEN_INT (tmp), if_info->x, 0,
1498 OPTAB_WIDEN);
1501 /* if (test) x = -1; else x = b;
1502 => x = -(test != 0) | b; */
1503 else if (itrue == -1)
1505 target = expand_simple_binop (mode, IOR,
1506 target, gen_int_mode (ifalse, mode),
1507 if_info->x, 0, OPTAB_WIDEN);
1509 else
1511 end_sequence ();
1512 return FALSE;
1515 if (! target)
1517 end_sequence ();
1518 return FALSE;
1521 if (target != if_info->x)
1522 noce_emit_move_insn (if_info->x, target);
1524 seq = end_ifcvt_sequence (if_info);
1525 if (!seq || !noce_conversion_profitable_p (seq, if_info))
1526 return FALSE;
1528 emit_insn_before_setloc (seq, if_info->jump,
1529 INSN_LOCATION (if_info->insn_a));
1530 if_info->transform_name = "noce_try_store_flag_constants";
1532 return TRUE;
1535 return FALSE;
1538 /* Convert "if (test) foo++" into "foo += (test != 0)", and
1539 similarly for "foo--". */
1541 static int
1542 noce_try_addcc (struct noce_if_info *if_info)
1544 rtx target;
1545 rtx_insn *seq;
1546 int subtract, normalize;
1548 if (!noce_simple_bbs (if_info))
1549 return FALSE;
1551 if (GET_CODE (if_info->a) == PLUS
1552 && rtx_equal_p (XEXP (if_info->a, 0), if_info->b)
1553 && (reversed_comparison_code (if_info->cond, if_info->jump)
1554 != UNKNOWN))
1556 rtx cond = if_info->cond;
1557 enum rtx_code code = reversed_comparison_code (cond, if_info->jump);
1559 /* First try to use addcc pattern. */
1560 if (general_operand (XEXP (cond, 0), VOIDmode)
1561 && general_operand (XEXP (cond, 1), VOIDmode))
1563 start_sequence ();
1564 target = emit_conditional_add (if_info->x, code,
1565 XEXP (cond, 0),
1566 XEXP (cond, 1),
1567 VOIDmode,
1568 if_info->b,
1569 XEXP (if_info->a, 1),
1570 GET_MODE (if_info->x),
1571 (code == LTU || code == GEU
1572 || code == LEU || code == GTU));
1573 if (target)
1575 if (target != if_info->x)
1576 noce_emit_move_insn (if_info->x, target);
1578 seq = end_ifcvt_sequence (if_info);
1579 if (!seq || !noce_conversion_profitable_p (seq, if_info))
1580 return FALSE;
1582 emit_insn_before_setloc (seq, if_info->jump,
1583 INSN_LOCATION (if_info->insn_a));
1584 if_info->transform_name = "noce_try_addcc";
1586 return TRUE;
1588 end_sequence ();
1591 /* If that fails, construct conditional increment or decrement using
1592 setcc. We're changing a branch and an increment to a comparison and
1593 an ADD/SUB. */
1594 if (XEXP (if_info->a, 1) == const1_rtx
1595 || XEXP (if_info->a, 1) == constm1_rtx)
1597 start_sequence ();
1598 if (STORE_FLAG_VALUE == INTVAL (XEXP (if_info->a, 1)))
1599 subtract = 0, normalize = 0;
1600 else if (-STORE_FLAG_VALUE == INTVAL (XEXP (if_info->a, 1)))
1601 subtract = 1, normalize = 0;
1602 else
1603 subtract = 0, normalize = INTVAL (XEXP (if_info->a, 1));
1606 target = noce_emit_store_flag (if_info,
1607 gen_reg_rtx (GET_MODE (if_info->x)),
1608 1, normalize);
1610 if (target)
1611 target = expand_simple_binop (GET_MODE (if_info->x),
1612 subtract ? MINUS : PLUS,
1613 if_info->b, target, if_info->x,
1614 0, OPTAB_WIDEN);
1615 if (target)
1617 if (target != if_info->x)
1618 noce_emit_move_insn (if_info->x, target);
1620 seq = end_ifcvt_sequence (if_info);
1621 if (!seq || !noce_conversion_profitable_p (seq, if_info))
1622 return FALSE;
1624 emit_insn_before_setloc (seq, if_info->jump,
1625 INSN_LOCATION (if_info->insn_a));
1626 if_info->transform_name = "noce_try_addcc";
1627 return TRUE;
1629 end_sequence ();
1633 return FALSE;
1636 /* Convert "if (test) x = 0;" to "x &= -(test == 0);" */
1638 static int
1639 noce_try_store_flag_mask (struct noce_if_info *if_info)
1641 rtx target;
1642 rtx_insn *seq;
1643 int reversep;
1645 if (!noce_simple_bbs (if_info))
1646 return FALSE;
1648 reversep = 0;
1650 if ((if_info->a == const0_rtx
1651 && rtx_equal_p (if_info->b, if_info->x))
1652 || ((reversep = (reversed_comparison_code (if_info->cond,
1653 if_info->jump)
1654 != UNKNOWN))
1655 && if_info->b == const0_rtx
1656 && rtx_equal_p (if_info->a, if_info->x)))
1658 start_sequence ();
1659 target = noce_emit_store_flag (if_info,
1660 gen_reg_rtx (GET_MODE (if_info->x)),
1661 reversep, -1);
1662 if (target)
1663 target = expand_simple_binop (GET_MODE (if_info->x), AND,
1664 if_info->x,
1665 target, if_info->x, 0,
1666 OPTAB_WIDEN);
1668 if (target)
1670 if (target != if_info->x)
1671 noce_emit_move_insn (if_info->x, target);
1673 seq = end_ifcvt_sequence (if_info);
1674 if (!seq || !noce_conversion_profitable_p (seq, if_info))
1675 return FALSE;
1677 emit_insn_before_setloc (seq, if_info->jump,
1678 INSN_LOCATION (if_info->insn_a));
1679 if_info->transform_name = "noce_try_store_flag_mask";
1681 return TRUE;
1684 end_sequence ();
1687 return FALSE;
1690 /* Helper function for noce_try_cmove and noce_try_cmove_arith. */
1692 static rtx
1693 noce_emit_cmove (struct noce_if_info *if_info, rtx x, enum rtx_code code,
1694 rtx cmp_a, rtx cmp_b, rtx vfalse, rtx vtrue)
1696 rtx target ATTRIBUTE_UNUSED;
1697 int unsignedp ATTRIBUTE_UNUSED;
1699 /* If earliest == jump, try to build the cmove insn directly.
1700 This is helpful when combine has created some complex condition
1701 (like for alpha's cmovlbs) that we can't hope to regenerate
1702 through the normal interface. */
1704 if (if_info->cond_earliest == if_info->jump)
1706 rtx cond = gen_rtx_fmt_ee (code, GET_MODE (if_info->cond), cmp_a, cmp_b);
1707 rtx if_then_else = gen_rtx_IF_THEN_ELSE (GET_MODE (x),
1708 cond, vtrue, vfalse);
1709 rtx set = gen_rtx_SET (x, if_then_else);
1711 start_sequence ();
1712 rtx_insn *insn = emit_insn (set);
1714 if (recog_memoized (insn) >= 0)
1716 rtx_insn *seq = get_insns ();
1717 end_sequence ();
1718 emit_insn (seq);
1720 return x;
1723 end_sequence ();
1726 /* Don't even try if the comparison operands are weird
1727 except that the target supports cbranchcc4. */
1728 if (! general_operand (cmp_a, GET_MODE (cmp_a))
1729 || ! general_operand (cmp_b, GET_MODE (cmp_b)))
1731 if (!have_cbranchcc4
1732 || GET_MODE_CLASS (GET_MODE (cmp_a)) != MODE_CC
1733 || cmp_b != const0_rtx)
1734 return NULL_RTX;
1737 unsignedp = (code == LTU || code == GEU
1738 || code == LEU || code == GTU);
1740 target = emit_conditional_move (x, code, cmp_a, cmp_b, VOIDmode,
1741 vtrue, vfalse, GET_MODE (x),
1742 unsignedp);
1743 if (target)
1744 return target;
1746 /* We might be faced with a situation like:
1748 x = (reg:M TARGET)
1749 vtrue = (subreg:M (reg:N VTRUE) BYTE)
1750 vfalse = (subreg:M (reg:N VFALSE) BYTE)
1752 We can't do a conditional move in mode M, but it's possible that we
1753 could do a conditional move in mode N instead and take a subreg of
1754 the result.
1756 If we can't create new pseudos, though, don't bother. */
1757 if (reload_completed)
1758 return NULL_RTX;
1760 if (GET_CODE (vtrue) == SUBREG && GET_CODE (vfalse) == SUBREG)
1762 rtx reg_vtrue = SUBREG_REG (vtrue);
1763 rtx reg_vfalse = SUBREG_REG (vfalse);
1764 unsigned int byte_vtrue = SUBREG_BYTE (vtrue);
1765 unsigned int byte_vfalse = SUBREG_BYTE (vfalse);
1766 rtx promoted_target;
1768 if (GET_MODE (reg_vtrue) != GET_MODE (reg_vfalse)
1769 || byte_vtrue != byte_vfalse
1770 || (SUBREG_PROMOTED_VAR_P (vtrue)
1771 != SUBREG_PROMOTED_VAR_P (vfalse))
1772 || (SUBREG_PROMOTED_GET (vtrue)
1773 != SUBREG_PROMOTED_GET (vfalse)))
1774 return NULL_RTX;
1776 promoted_target = gen_reg_rtx (GET_MODE (reg_vtrue));
1778 target = emit_conditional_move (promoted_target, code, cmp_a, cmp_b,
1779 VOIDmode, reg_vtrue, reg_vfalse,
1780 GET_MODE (reg_vtrue), unsignedp);
1781 /* Nope, couldn't do it in that mode either. */
1782 if (!target)
1783 return NULL_RTX;
1785 target = gen_rtx_SUBREG (GET_MODE (vtrue), promoted_target, byte_vtrue);
1786 SUBREG_PROMOTED_VAR_P (target) = SUBREG_PROMOTED_VAR_P (vtrue);
1787 SUBREG_PROMOTED_SET (target, SUBREG_PROMOTED_GET (vtrue));
1788 emit_move_insn (x, target);
1789 return x;
1791 else
1792 return NULL_RTX;
1795 /* Try only simple constants and registers here. More complex cases
1796 are handled in noce_try_cmove_arith after noce_try_store_flag_arith
1797 has had a go at it. */
1799 static int
1800 noce_try_cmove (struct noce_if_info *if_info)
1802 enum rtx_code code;
1803 rtx target;
1804 rtx_insn *seq;
1806 if (!noce_simple_bbs (if_info))
1807 return FALSE;
1809 if ((CONSTANT_P (if_info->a) || register_operand (if_info->a, VOIDmode))
1810 && (CONSTANT_P (if_info->b) || register_operand (if_info->b, VOIDmode)))
1812 start_sequence ();
1814 code = GET_CODE (if_info->cond);
1815 target = noce_emit_cmove (if_info, if_info->x, code,
1816 XEXP (if_info->cond, 0),
1817 XEXP (if_info->cond, 1),
1818 if_info->a, if_info->b);
1820 if (target)
1822 if (target != if_info->x)
1823 noce_emit_move_insn (if_info->x, target);
1825 seq = end_ifcvt_sequence (if_info);
1826 if (!seq)
1827 return FALSE;
1829 emit_insn_before_setloc (seq, if_info->jump,
1830 INSN_LOCATION (if_info->insn_a));
1831 if_info->transform_name = "noce_try_cmove";
1833 return TRUE;
1835 /* If both a and b are constants try a last-ditch transformation:
1836 if (test) x = a; else x = b;
1837 => x = (-(test != 0) & (b - a)) + a;
1838 Try this only if the target-specific expansion above has failed.
1839 The target-specific expander may want to generate sequences that
1840 we don't know about, so give them a chance before trying this
1841 approach. */
1842 else if (!targetm.have_conditional_execution ()
1843 && CONST_INT_P (if_info->a) && CONST_INT_P (if_info->b))
1845 machine_mode mode = GET_MODE (if_info->x);
1846 HOST_WIDE_INT ifalse = INTVAL (if_info->a);
1847 HOST_WIDE_INT itrue = INTVAL (if_info->b);
1848 rtx target = noce_emit_store_flag (if_info, if_info->x, false, -1);
1849 if (!target)
1851 end_sequence ();
1852 return FALSE;
1855 HOST_WIDE_INT diff = (unsigned HOST_WIDE_INT) itrue - ifalse;
1856 /* Make sure we can represent the difference
1857 between the two values. */
1858 if ((diff > 0)
1859 != ((ifalse < 0) != (itrue < 0) ? ifalse < 0 : ifalse < itrue))
1861 end_sequence ();
1862 return FALSE;
1865 diff = trunc_int_for_mode (diff, mode);
1866 target = expand_simple_binop (mode, AND,
1867 target, gen_int_mode (diff, mode),
1868 if_info->x, 0, OPTAB_WIDEN);
1869 if (target)
1870 target = expand_simple_binop (mode, PLUS,
1871 target, gen_int_mode (ifalse, mode),
1872 if_info->x, 0, OPTAB_WIDEN);
1873 if (target)
1875 if (target != if_info->x)
1876 noce_emit_move_insn (if_info->x, target);
1878 seq = end_ifcvt_sequence (if_info);
1879 if (!seq || !noce_conversion_profitable_p (seq, if_info))
1880 return FALSE;
1882 emit_insn_before_setloc (seq, if_info->jump,
1883 INSN_LOCATION (if_info->insn_a));
1884 if_info->transform_name = "noce_try_cmove";
1885 return TRUE;
1887 else
1889 end_sequence ();
1890 return FALSE;
1893 else
1894 end_sequence ();
1897 return FALSE;
1900 /* Return true if X contains a conditional code mode rtx. */
1902 static bool
1903 contains_ccmode_rtx_p (rtx x)
1905 subrtx_iterator::array_type array;
1906 FOR_EACH_SUBRTX (iter, array, x, ALL)
1907 if (GET_MODE_CLASS (GET_MODE (*iter)) == MODE_CC)
1908 return true;
1910 return false;
1913 /* Helper for bb_valid_for_noce_process_p. Validate that
1914 the rtx insn INSN is a single set that does not set
1915 the conditional register CC and is in general valid for
1916 if-conversion. */
1918 static bool
1919 insn_valid_noce_process_p (rtx_insn *insn, rtx cc)
1921 if (!insn
1922 || !NONJUMP_INSN_P (insn)
1923 || (cc && set_of (cc, insn)))
1924 return false;
1926 rtx sset = single_set (insn);
1928 /* Currently support only simple single sets in test_bb. */
1929 if (!sset
1930 || !noce_operand_ok (SET_DEST (sset))
1931 || contains_ccmode_rtx_p (SET_DEST (sset))
1932 || !noce_operand_ok (SET_SRC (sset)))
1933 return false;
1935 return true;
1939 /* Return true iff the registers that the insns in BB_A set do not get
1940 used in BB_B. If TO_RENAME is non-NULL then it is a location that will be
1941 renamed later by the caller and so conflicts on it should be ignored
1942 in this function. */
1944 static bool
1945 bbs_ok_for_cmove_arith (basic_block bb_a, basic_block bb_b, rtx to_rename)
1947 rtx_insn *a_insn;
1948 bitmap bba_sets = BITMAP_ALLOC (&reg_obstack);
1950 df_ref def;
1951 df_ref use;
1953 FOR_BB_INSNS (bb_a, a_insn)
1955 if (!active_insn_p (a_insn))
1956 continue;
1958 rtx sset_a = single_set (a_insn);
1960 if (!sset_a)
1962 BITMAP_FREE (bba_sets);
1963 return false;
1965 /* Record all registers that BB_A sets. */
1966 FOR_EACH_INSN_DEF (def, a_insn)
1967 if (!(to_rename && DF_REF_REG (def) == to_rename))
1968 bitmap_set_bit (bba_sets, DF_REF_REGNO (def));
1971 rtx_insn *b_insn;
1973 FOR_BB_INSNS (bb_b, b_insn)
1975 if (!active_insn_p (b_insn))
1976 continue;
1978 rtx sset_b = single_set (b_insn);
1980 if (!sset_b)
1982 BITMAP_FREE (bba_sets);
1983 return false;
1986 /* Make sure this is a REG and not some instance
1987 of ZERO_EXTRACT or SUBREG or other dangerous stuff.
1988 If we have a memory destination then we have a pair of simple
1989 basic blocks performing an operation of the form [addr] = c ? a : b.
1990 bb_valid_for_noce_process_p will have ensured that these are
1991 the only stores present. In that case [addr] should be the location
1992 to be renamed. Assert that the callers set this up properly. */
1993 if (MEM_P (SET_DEST (sset_b)))
1994 gcc_assert (rtx_equal_p (SET_DEST (sset_b), to_rename));
1995 else if (!REG_P (SET_DEST (sset_b)))
1997 BITMAP_FREE (bba_sets);
1998 return false;
2001 /* If the insn uses a reg set in BB_A return false. */
2002 FOR_EACH_INSN_USE (use, b_insn)
2004 if (bitmap_bit_p (bba_sets, DF_REF_REGNO (use)))
2006 BITMAP_FREE (bba_sets);
2007 return false;
2013 BITMAP_FREE (bba_sets);
2014 return true;
2017 /* Emit copies of all the active instructions in BB except the last.
2018 This is a helper for noce_try_cmove_arith. */
2020 static void
2021 noce_emit_all_but_last (basic_block bb)
2023 rtx_insn *last = last_active_insn (bb, FALSE);
2024 rtx_insn *insn;
2025 FOR_BB_INSNS (bb, insn)
2027 if (insn != last && active_insn_p (insn))
2029 rtx_insn *to_emit = as_a <rtx_insn *> (copy_rtx (insn));
2031 emit_insn (PATTERN (to_emit));
2036 /* Helper for noce_try_cmove_arith. Emit the pattern TO_EMIT and return
2037 the resulting insn or NULL if it's not a valid insn. */
2039 static rtx_insn *
2040 noce_emit_insn (rtx to_emit)
2042 gcc_assert (to_emit);
2043 rtx_insn *insn = emit_insn (to_emit);
2045 if (recog_memoized (insn) < 0)
2046 return NULL;
2048 return insn;
2051 /* Helper for noce_try_cmove_arith. Emit a copy of the insns up to
2052 and including the penultimate one in BB if it is not simple
2053 (as indicated by SIMPLE). Then emit LAST_INSN as the last
2054 insn in the block. The reason for that is that LAST_INSN may
2055 have been modified by the preparation in noce_try_cmove_arith. */
2057 static bool
2058 noce_emit_bb (rtx last_insn, basic_block bb, bool simple)
2060 if (bb && !simple)
2061 noce_emit_all_but_last (bb);
2063 if (last_insn && !noce_emit_insn (last_insn))
2064 return false;
2066 return true;
2069 /* Try more complex cases involving conditional_move. */
2071 static int
2072 noce_try_cmove_arith (struct noce_if_info *if_info)
2074 rtx a = if_info->a;
2075 rtx b = if_info->b;
2076 rtx x = if_info->x;
2077 rtx orig_a, orig_b;
2078 rtx_insn *insn_a, *insn_b;
2079 bool a_simple = if_info->then_simple;
2080 bool b_simple = if_info->else_simple;
2081 basic_block then_bb = if_info->then_bb;
2082 basic_block else_bb = if_info->else_bb;
2083 rtx target;
2084 int is_mem = 0;
2085 enum rtx_code code;
2086 rtx_insn *ifcvt_seq;
2088 /* A conditional move from two memory sources is equivalent to a
2089 conditional on their addresses followed by a load. Don't do this
2090 early because it'll screw alias analysis. Note that we've
2091 already checked for no side effects. */
2092 if (cse_not_expected
2093 && MEM_P (a) && MEM_P (b)
2094 && MEM_ADDR_SPACE (a) == MEM_ADDR_SPACE (b))
2096 machine_mode address_mode = get_address_mode (a);
2098 a = XEXP (a, 0);
2099 b = XEXP (b, 0);
2100 x = gen_reg_rtx (address_mode);
2101 is_mem = 1;
2104 /* ??? We could handle this if we knew that a load from A or B could
2105 not trap or fault. This is also true if we've already loaded
2106 from the address along the path from ENTRY. */
2107 else if (may_trap_or_fault_p (a) || may_trap_or_fault_p (b))
2108 return FALSE;
2110 /* if (test) x = a + b; else x = c - d;
2111 => y = a + b;
2112 x = c - d;
2113 if (test)
2114 x = y;
2117 code = GET_CODE (if_info->cond);
2118 insn_a = if_info->insn_a;
2119 insn_b = if_info->insn_b;
2121 machine_mode x_mode = GET_MODE (x);
2123 if (!can_conditionally_move_p (x_mode))
2124 return FALSE;
2126 /* Possibly rearrange operands to make things come out more natural. */
2127 if (reversed_comparison_code (if_info->cond, if_info->jump) != UNKNOWN)
2129 int reversep = 0;
2130 if (rtx_equal_p (b, x))
2131 reversep = 1;
2132 else if (general_operand (b, GET_MODE (b)))
2133 reversep = 1;
2135 if (reversep)
2137 code = reversed_comparison_code (if_info->cond, if_info->jump);
2138 std::swap (a, b);
2139 std::swap (insn_a, insn_b);
2140 std::swap (a_simple, b_simple);
2141 std::swap (then_bb, else_bb);
2145 if (then_bb && else_bb
2146 && (!bbs_ok_for_cmove_arith (then_bb, else_bb, if_info->orig_x)
2147 || !bbs_ok_for_cmove_arith (else_bb, then_bb, if_info->orig_x)))
2148 return FALSE;
2150 start_sequence ();
2152 /* If one of the blocks is empty then the corresponding B or A value
2153 came from the test block. The non-empty complex block that we will
2154 emit might clobber the register used by B or A, so move it to a pseudo
2155 first. */
2157 rtx tmp_a = NULL_RTX;
2158 rtx tmp_b = NULL_RTX;
2160 if (b_simple || !else_bb)
2161 tmp_b = gen_reg_rtx (x_mode);
2163 if (a_simple || !then_bb)
2164 tmp_a = gen_reg_rtx (x_mode);
2166 orig_a = a;
2167 orig_b = b;
2169 rtx emit_a = NULL_RTX;
2170 rtx emit_b = NULL_RTX;
2171 rtx_insn *tmp_insn = NULL;
2172 bool modified_in_a = false;
2173 bool modified_in_b = false;
2174 /* If either operand is complex, load it into a register first.
2175 The best way to do this is to copy the original insn. In this
2176 way we preserve any clobbers etc that the insn may have had.
2177 This is of course not possible in the IS_MEM case. */
2179 if (! general_operand (a, GET_MODE (a)) || tmp_a)
2182 if (is_mem)
2184 rtx reg = gen_reg_rtx (GET_MODE (a));
2185 emit_a = gen_rtx_SET (reg, a);
2187 else
2189 if (insn_a)
2191 a = tmp_a ? tmp_a : gen_reg_rtx (GET_MODE (a));
2193 rtx_insn *copy_of_a = as_a <rtx_insn *> (copy_rtx (insn_a));
2194 rtx set = single_set (copy_of_a);
2195 SET_DEST (set) = a;
2197 emit_a = PATTERN (copy_of_a);
2199 else
2201 rtx tmp_reg = tmp_a ? tmp_a : gen_reg_rtx (GET_MODE (a));
2202 emit_a = gen_rtx_SET (tmp_reg, a);
2203 a = tmp_reg;
2208 if (! general_operand (b, GET_MODE (b)) || tmp_b)
2210 if (is_mem)
2212 rtx reg = gen_reg_rtx (GET_MODE (b));
2213 emit_b = gen_rtx_SET (reg, b);
2215 else
2217 if (insn_b)
2219 b = tmp_b ? tmp_b : gen_reg_rtx (GET_MODE (b));
2220 rtx_insn *copy_of_b = as_a <rtx_insn *> (copy_rtx (insn_b));
2221 rtx set = single_set (copy_of_b);
2223 SET_DEST (set) = b;
2224 emit_b = PATTERN (copy_of_b);
2226 else
2228 rtx tmp_reg = tmp_b ? tmp_b : gen_reg_rtx (GET_MODE (b));
2229 emit_b = gen_rtx_SET (tmp_reg, b);
2230 b = tmp_reg;
2235 modified_in_a = emit_a != NULL_RTX && modified_in_p (orig_b, emit_a);
2236 if (tmp_b && then_bb)
2238 FOR_BB_INSNS (then_bb, tmp_insn)
2239 /* Don't check inside insn_a. We will have changed it to emit_a
2240 with a destination that doesn't conflict. */
2241 if (!(insn_a && tmp_insn == insn_a)
2242 && modified_in_p (orig_b, tmp_insn))
2244 modified_in_a = true;
2245 break;
2250 modified_in_b = emit_b != NULL_RTX && modified_in_p (orig_a, emit_b);
2251 if (tmp_a && else_bb)
2253 FOR_BB_INSNS (else_bb, tmp_insn)
2254 /* Don't check inside insn_b. We will have changed it to emit_b
2255 with a destination that doesn't conflict. */
2256 if (!(insn_b && tmp_insn == insn_b)
2257 && modified_in_p (orig_a, tmp_insn))
2259 modified_in_b = true;
2260 break;
2264 /* If insn to set up A clobbers any registers B depends on, try to
2265 swap insn that sets up A with the one that sets up B. If even
2266 that doesn't help, punt. */
2267 if (modified_in_a && !modified_in_b)
2269 if (!noce_emit_bb (emit_b, else_bb, b_simple))
2270 goto end_seq_and_fail;
2272 if (!noce_emit_bb (emit_a, then_bb, a_simple))
2273 goto end_seq_and_fail;
2275 else if (!modified_in_a)
2277 if (!noce_emit_bb (emit_a, then_bb, a_simple))
2278 goto end_seq_and_fail;
2280 if (!noce_emit_bb (emit_b, else_bb, b_simple))
2281 goto end_seq_and_fail;
2283 else
2284 goto end_seq_and_fail;
2286 target = noce_emit_cmove (if_info, x, code, XEXP (if_info->cond, 0),
2287 XEXP (if_info->cond, 1), a, b);
2289 if (! target)
2290 goto end_seq_and_fail;
2292 /* If we're handling a memory for above, emit the load now. */
2293 if (is_mem)
2295 rtx mem = gen_rtx_MEM (GET_MODE (if_info->x), target);
2297 /* Copy over flags as appropriate. */
2298 if (MEM_VOLATILE_P (if_info->a) || MEM_VOLATILE_P (if_info->b))
2299 MEM_VOLATILE_P (mem) = 1;
2300 if (MEM_ALIAS_SET (if_info->a) == MEM_ALIAS_SET (if_info->b))
2301 set_mem_alias_set (mem, MEM_ALIAS_SET (if_info->a));
2302 set_mem_align (mem,
2303 MIN (MEM_ALIGN (if_info->a), MEM_ALIGN (if_info->b)));
2305 gcc_assert (MEM_ADDR_SPACE (if_info->a) == MEM_ADDR_SPACE (if_info->b));
2306 set_mem_addr_space (mem, MEM_ADDR_SPACE (if_info->a));
2308 noce_emit_move_insn (if_info->x, mem);
2310 else if (target != x)
2311 noce_emit_move_insn (x, target);
2313 ifcvt_seq = end_ifcvt_sequence (if_info);
2314 if (!ifcvt_seq || !noce_conversion_profitable_p (ifcvt_seq, if_info))
2315 return FALSE;
2317 emit_insn_before_setloc (ifcvt_seq, if_info->jump,
2318 INSN_LOCATION (if_info->insn_a));
2319 if_info->transform_name = "noce_try_cmove_arith";
2320 return TRUE;
2322 end_seq_and_fail:
2323 end_sequence ();
2324 return FALSE;
2327 /* For most cases, the simplified condition we found is the best
2328 choice, but this is not the case for the min/max/abs transforms.
2329 For these we wish to know that it is A or B in the condition. */
2331 static rtx
2332 noce_get_alt_condition (struct noce_if_info *if_info, rtx target,
2333 rtx_insn **earliest)
2335 rtx cond, set;
2336 rtx_insn *insn;
2337 int reverse;
2339 /* If target is already mentioned in the known condition, return it. */
2340 if (reg_mentioned_p (target, if_info->cond))
2342 *earliest = if_info->cond_earliest;
2343 return if_info->cond;
2346 set = pc_set (if_info->jump);
2347 cond = XEXP (SET_SRC (set), 0);
2348 reverse
2349 = GET_CODE (XEXP (SET_SRC (set), 2)) == LABEL_REF
2350 && LABEL_REF_LABEL (XEXP (SET_SRC (set), 2)) == JUMP_LABEL (if_info->jump);
2351 if (if_info->then_else_reversed)
2352 reverse = !reverse;
2354 /* If we're looking for a constant, try to make the conditional
2355 have that constant in it. There are two reasons why it may
2356 not have the constant we want:
2358 1. GCC may have needed to put the constant in a register, because
2359 the target can't compare directly against that constant. For
2360 this case, we look for a SET immediately before the comparison
2361 that puts a constant in that register.
2363 2. GCC may have canonicalized the conditional, for example
2364 replacing "if x < 4" with "if x <= 3". We can undo that (or
2365 make equivalent types of changes) to get the constants we need
2366 if they're off by one in the right direction. */
2368 if (CONST_INT_P (target))
2370 enum rtx_code code = GET_CODE (if_info->cond);
2371 rtx op_a = XEXP (if_info->cond, 0);
2372 rtx op_b = XEXP (if_info->cond, 1);
2373 rtx_insn *prev_insn;
2375 /* First, look to see if we put a constant in a register. */
2376 prev_insn = prev_nonnote_insn (if_info->cond_earliest);
2377 if (prev_insn
2378 && BLOCK_FOR_INSN (prev_insn)
2379 == BLOCK_FOR_INSN (if_info->cond_earliest)
2380 && INSN_P (prev_insn)
2381 && GET_CODE (PATTERN (prev_insn)) == SET)
2383 rtx src = find_reg_equal_equiv_note (prev_insn);
2384 if (!src)
2385 src = SET_SRC (PATTERN (prev_insn));
2386 if (CONST_INT_P (src))
2388 if (rtx_equal_p (op_a, SET_DEST (PATTERN (prev_insn))))
2389 op_a = src;
2390 else if (rtx_equal_p (op_b, SET_DEST (PATTERN (prev_insn))))
2391 op_b = src;
2393 if (CONST_INT_P (op_a))
2395 std::swap (op_a, op_b);
2396 code = swap_condition (code);
2401 /* Now, look to see if we can get the right constant by
2402 adjusting the conditional. */
2403 if (CONST_INT_P (op_b))
2405 HOST_WIDE_INT desired_val = INTVAL (target);
2406 HOST_WIDE_INT actual_val = INTVAL (op_b);
2408 switch (code)
2410 case LT:
2411 if (desired_val != HOST_WIDE_INT_MAX
2412 && actual_val == desired_val + 1)
2414 code = LE;
2415 op_b = GEN_INT (desired_val);
2417 break;
2418 case LE:
2419 if (desired_val != HOST_WIDE_INT_MIN
2420 && actual_val == desired_val - 1)
2422 code = LT;
2423 op_b = GEN_INT (desired_val);
2425 break;
2426 case GT:
2427 if (desired_val != HOST_WIDE_INT_MIN
2428 && actual_val == desired_val - 1)
2430 code = GE;
2431 op_b = GEN_INT (desired_val);
2433 break;
2434 case GE:
2435 if (desired_val != HOST_WIDE_INT_MAX
2436 && actual_val == desired_val + 1)
2438 code = GT;
2439 op_b = GEN_INT (desired_val);
2441 break;
2442 default:
2443 break;
2447 /* If we made any changes, generate a new conditional that is
2448 equivalent to what we started with, but has the right
2449 constants in it. */
2450 if (code != GET_CODE (if_info->cond)
2451 || op_a != XEXP (if_info->cond, 0)
2452 || op_b != XEXP (if_info->cond, 1))
2454 cond = gen_rtx_fmt_ee (code, GET_MODE (cond), op_a, op_b);
2455 *earliest = if_info->cond_earliest;
2456 return cond;
2460 cond = canonicalize_condition (if_info->jump, cond, reverse,
2461 earliest, target, have_cbranchcc4, true);
2462 if (! cond || ! reg_mentioned_p (target, cond))
2463 return NULL;
2465 /* We almost certainly searched back to a different place.
2466 Need to re-verify correct lifetimes. */
2468 /* X may not be mentioned in the range (cond_earliest, jump]. */
2469 for (insn = if_info->jump; insn != *earliest; insn = PREV_INSN (insn))
2470 if (INSN_P (insn) && reg_overlap_mentioned_p (if_info->x, PATTERN (insn)))
2471 return NULL;
2473 /* A and B may not be modified in the range [cond_earliest, jump). */
2474 for (insn = *earliest; insn != if_info->jump; insn = NEXT_INSN (insn))
2475 if (INSN_P (insn)
2476 && (modified_in_p (if_info->a, insn)
2477 || modified_in_p (if_info->b, insn)))
2478 return NULL;
2480 return cond;
2483 /* Convert "if (a < b) x = a; else x = b;" to "x = min(a, b);", etc. */
2485 static int
2486 noce_try_minmax (struct noce_if_info *if_info)
2488 rtx cond, target;
2489 rtx_insn *earliest, *seq;
2490 enum rtx_code code, op;
2491 int unsignedp;
2493 if (!noce_simple_bbs (if_info))
2494 return FALSE;
2496 /* ??? Reject modes with NaNs or signed zeros since we don't know how
2497 they will be resolved with an SMIN/SMAX. It wouldn't be too hard
2498 to get the target to tell us... */
2499 if (HONOR_SIGNED_ZEROS (if_info->x)
2500 || HONOR_NANS (if_info->x))
2501 return FALSE;
2503 cond = noce_get_alt_condition (if_info, if_info->a, &earliest);
2504 if (!cond)
2505 return FALSE;
2507 /* Verify the condition is of the form we expect, and canonicalize
2508 the comparison code. */
2509 code = GET_CODE (cond);
2510 if (rtx_equal_p (XEXP (cond, 0), if_info->a))
2512 if (! rtx_equal_p (XEXP (cond, 1), if_info->b))
2513 return FALSE;
2515 else if (rtx_equal_p (XEXP (cond, 1), if_info->a))
2517 if (! rtx_equal_p (XEXP (cond, 0), if_info->b))
2518 return FALSE;
2519 code = swap_condition (code);
2521 else
2522 return FALSE;
2524 /* Determine what sort of operation this is. Note that the code is for
2525 a taken branch, so the code->operation mapping appears backwards. */
2526 switch (code)
2528 case LT:
2529 case LE:
2530 case UNLT:
2531 case UNLE:
2532 op = SMAX;
2533 unsignedp = 0;
2534 break;
2535 case GT:
2536 case GE:
2537 case UNGT:
2538 case UNGE:
2539 op = SMIN;
2540 unsignedp = 0;
2541 break;
2542 case LTU:
2543 case LEU:
2544 op = UMAX;
2545 unsignedp = 1;
2546 break;
2547 case GTU:
2548 case GEU:
2549 op = UMIN;
2550 unsignedp = 1;
2551 break;
2552 default:
2553 return FALSE;
2556 start_sequence ();
2558 target = expand_simple_binop (GET_MODE (if_info->x), op,
2559 if_info->a, if_info->b,
2560 if_info->x, unsignedp, OPTAB_WIDEN);
2561 if (! target)
2563 end_sequence ();
2564 return FALSE;
2566 if (target != if_info->x)
2567 noce_emit_move_insn (if_info->x, target);
2569 seq = end_ifcvt_sequence (if_info);
2570 if (!seq)
2571 return FALSE;
2573 emit_insn_before_setloc (seq, if_info->jump, INSN_LOCATION (if_info->insn_a));
2574 if_info->cond = cond;
2575 if_info->cond_earliest = earliest;
2576 if_info->transform_name = "noce_try_minmax";
2578 return TRUE;
2581 /* Convert "if (a < 0) x = -a; else x = a;" to "x = abs(a);",
2582 "if (a < 0) x = ~a; else x = a;" to "x = one_cmpl_abs(a);",
2583 etc. */
2585 static int
2586 noce_try_abs (struct noce_if_info *if_info)
2588 rtx cond, target, a, b, c;
2589 rtx_insn *earliest, *seq;
2590 int negate;
2591 bool one_cmpl = false;
2593 if (!noce_simple_bbs (if_info))
2594 return FALSE;
2596 /* Reject modes with signed zeros. */
2597 if (HONOR_SIGNED_ZEROS (if_info->x))
2598 return FALSE;
2600 /* Recognize A and B as constituting an ABS or NABS. The canonical
2601 form is a branch around the negation, taken when the object is the
2602 first operand of a comparison against 0 that evaluates to true. */
2603 a = if_info->a;
2604 b = if_info->b;
2605 if (GET_CODE (a) == NEG && rtx_equal_p (XEXP (a, 0), b))
2606 negate = 0;
2607 else if (GET_CODE (b) == NEG && rtx_equal_p (XEXP (b, 0), a))
2609 std::swap (a, b);
2610 negate = 1;
2612 else if (GET_CODE (a) == NOT && rtx_equal_p (XEXP (a, 0), b))
2614 negate = 0;
2615 one_cmpl = true;
2617 else if (GET_CODE (b) == NOT && rtx_equal_p (XEXP (b, 0), a))
2619 std::swap (a, b);
2620 negate = 1;
2621 one_cmpl = true;
2623 else
2624 return FALSE;
2626 cond = noce_get_alt_condition (if_info, b, &earliest);
2627 if (!cond)
2628 return FALSE;
2630 /* Verify the condition is of the form we expect. */
2631 if (rtx_equal_p (XEXP (cond, 0), b))
2632 c = XEXP (cond, 1);
2633 else if (rtx_equal_p (XEXP (cond, 1), b))
2635 c = XEXP (cond, 0);
2636 negate = !negate;
2638 else
2639 return FALSE;
2641 /* Verify that C is zero. Search one step backward for a
2642 REG_EQUAL note or a simple source if necessary. */
2643 if (REG_P (c))
2645 rtx set;
2646 rtx_insn *insn = prev_nonnote_insn (earliest);
2647 if (insn
2648 && BLOCK_FOR_INSN (insn) == BLOCK_FOR_INSN (earliest)
2649 && (set = single_set (insn))
2650 && rtx_equal_p (SET_DEST (set), c))
2652 rtx note = find_reg_equal_equiv_note (insn);
2653 if (note)
2654 c = XEXP (note, 0);
2655 else
2656 c = SET_SRC (set);
2658 else
2659 return FALSE;
2661 if (MEM_P (c)
2662 && GET_CODE (XEXP (c, 0)) == SYMBOL_REF
2663 && CONSTANT_POOL_ADDRESS_P (XEXP (c, 0)))
2664 c = get_pool_constant (XEXP (c, 0));
2666 /* Work around funny ideas get_condition has wrt canonicalization.
2667 Note that these rtx constants are known to be CONST_INT, and
2668 therefore imply integer comparisons.
2669 The one_cmpl case is more complicated, as we want to handle
2670 only x < 0 ? ~x : x or x >= 0 ? x : ~x to one_cmpl_abs (x)
2671 and x < 0 ? x : ~x or x >= 0 ? ~x : x to ~one_cmpl_abs (x),
2672 but not other cases (x > -1 is equivalent of x >= 0). */
2673 if (c == constm1_rtx && GET_CODE (cond) == GT)
2675 else if (c == const1_rtx && GET_CODE (cond) == LT)
2677 if (one_cmpl)
2678 return FALSE;
2680 else if (c == CONST0_RTX (GET_MODE (b)))
2682 if (one_cmpl
2683 && GET_CODE (cond) != GE
2684 && GET_CODE (cond) != LT)
2685 return FALSE;
2687 else
2688 return FALSE;
2690 /* Determine what sort of operation this is. */
2691 switch (GET_CODE (cond))
2693 case LT:
2694 case LE:
2695 case UNLT:
2696 case UNLE:
2697 negate = !negate;
2698 break;
2699 case GT:
2700 case GE:
2701 case UNGT:
2702 case UNGE:
2703 break;
2704 default:
2705 return FALSE;
2708 start_sequence ();
2709 if (one_cmpl)
2710 target = expand_one_cmpl_abs_nojump (GET_MODE (if_info->x), b,
2711 if_info->x);
2712 else
2713 target = expand_abs_nojump (GET_MODE (if_info->x), b, if_info->x, 1);
2715 /* ??? It's a quandary whether cmove would be better here, especially
2716 for integers. Perhaps combine will clean things up. */
2717 if (target && negate)
2719 if (one_cmpl)
2720 target = expand_simple_unop (GET_MODE (target), NOT, target,
2721 if_info->x, 0);
2722 else
2723 target = expand_simple_unop (GET_MODE (target), NEG, target,
2724 if_info->x, 0);
2727 if (! target)
2729 end_sequence ();
2730 return FALSE;
2733 if (target != if_info->x)
2734 noce_emit_move_insn (if_info->x, target);
2736 seq = end_ifcvt_sequence (if_info);
2737 if (!seq)
2738 return FALSE;
2740 emit_insn_before_setloc (seq, if_info->jump, INSN_LOCATION (if_info->insn_a));
2741 if_info->cond = cond;
2742 if_info->cond_earliest = earliest;
2743 if_info->transform_name = "noce_try_abs";
2745 return TRUE;
2748 /* Convert "if (m < 0) x = b; else x = 0;" to "x = (m >> C) & b;". */
2750 static int
2751 noce_try_sign_mask (struct noce_if_info *if_info)
2753 rtx cond, t, m, c;
2754 rtx_insn *seq;
2755 machine_mode mode;
2756 enum rtx_code code;
2757 bool t_unconditional;
2759 if (!noce_simple_bbs (if_info))
2760 return FALSE;
2762 cond = if_info->cond;
2763 code = GET_CODE (cond);
2764 m = XEXP (cond, 0);
2765 c = XEXP (cond, 1);
2767 t = NULL_RTX;
2768 if (if_info->a == const0_rtx)
2770 if ((code == LT && c == const0_rtx)
2771 || (code == LE && c == constm1_rtx))
2772 t = if_info->b;
2774 else if (if_info->b == const0_rtx)
2776 if ((code == GE && c == const0_rtx)
2777 || (code == GT && c == constm1_rtx))
2778 t = if_info->a;
2781 if (! t || side_effects_p (t))
2782 return FALSE;
2784 /* We currently don't handle different modes. */
2785 mode = GET_MODE (t);
2786 if (GET_MODE (m) != mode)
2787 return FALSE;
2789 /* This is only profitable if T is unconditionally executed/evaluated in the
2790 original insn sequence or T is cheap. The former happens if B is the
2791 non-zero (T) value and if INSN_B was taken from TEST_BB, or there was no
2792 INSN_B which can happen for e.g. conditional stores to memory. For the
2793 cost computation use the block TEST_BB where the evaluation will end up
2794 after the transformation. */
2795 t_unconditional =
2796 (t == if_info->b
2797 && (if_info->insn_b == NULL_RTX
2798 || BLOCK_FOR_INSN (if_info->insn_b) == if_info->test_bb));
2799 if (!(t_unconditional
2800 || (set_src_cost (t, mode, if_info->speed_p)
2801 < COSTS_N_INSNS (2))))
2802 return FALSE;
2804 start_sequence ();
2805 /* Use emit_store_flag to generate "m < 0 ? -1 : 0" instead of expanding
2806 "(signed) m >> 31" directly. This benefits targets with specialized
2807 insns to obtain the signmask, but still uses ashr_optab otherwise. */
2808 m = emit_store_flag (gen_reg_rtx (mode), LT, m, const0_rtx, mode, 0, -1);
2809 t = m ? expand_binop (mode, and_optab, m, t, NULL_RTX, 0, OPTAB_DIRECT)
2810 : NULL_RTX;
2812 if (!t)
2814 end_sequence ();
2815 return FALSE;
2818 noce_emit_move_insn (if_info->x, t);
2820 seq = end_ifcvt_sequence (if_info);
2821 if (!seq)
2822 return FALSE;
2824 emit_insn_before_setloc (seq, if_info->jump, INSN_LOCATION (if_info->insn_a));
2825 if_info->transform_name = "noce_try_sign_mask";
2827 return TRUE;
2831 /* Optimize away "if (x & C) x |= C" and similar bit manipulation
2832 transformations. */
2834 static int
2835 noce_try_bitop (struct noce_if_info *if_info)
2837 rtx cond, x, a, result;
2838 rtx_insn *seq;
2839 machine_mode mode;
2840 enum rtx_code code;
2841 int bitnum;
2843 x = if_info->x;
2844 cond = if_info->cond;
2845 code = GET_CODE (cond);
2847 if (!noce_simple_bbs (if_info))
2848 return FALSE;
2850 /* Check for no else condition. */
2851 if (! rtx_equal_p (x, if_info->b))
2852 return FALSE;
2854 /* Check for a suitable condition. */
2855 if (code != NE && code != EQ)
2856 return FALSE;
2857 if (XEXP (cond, 1) != const0_rtx)
2858 return FALSE;
2859 cond = XEXP (cond, 0);
2861 /* ??? We could also handle AND here. */
2862 if (GET_CODE (cond) == ZERO_EXTRACT)
2864 if (XEXP (cond, 1) != const1_rtx
2865 || !CONST_INT_P (XEXP (cond, 2))
2866 || ! rtx_equal_p (x, XEXP (cond, 0)))
2867 return FALSE;
2868 bitnum = INTVAL (XEXP (cond, 2));
2869 mode = GET_MODE (x);
2870 if (BITS_BIG_ENDIAN)
2871 bitnum = GET_MODE_BITSIZE (mode) - 1 - bitnum;
2872 if (bitnum < 0 || bitnum >= HOST_BITS_PER_WIDE_INT)
2873 return FALSE;
2875 else
2876 return FALSE;
2878 a = if_info->a;
2879 if (GET_CODE (a) == IOR || GET_CODE (a) == XOR)
2881 /* Check for "if (X & C) x = x op C". */
2882 if (! rtx_equal_p (x, XEXP (a, 0))
2883 || !CONST_INT_P (XEXP (a, 1))
2884 || (INTVAL (XEXP (a, 1)) & GET_MODE_MASK (mode))
2885 != HOST_WIDE_INT_1U << bitnum)
2886 return FALSE;
2888 /* if ((x & C) == 0) x |= C; is transformed to x |= C. */
2889 /* if ((x & C) != 0) x |= C; is transformed to nothing. */
2890 if (GET_CODE (a) == IOR)
2891 result = (code == NE) ? a : NULL_RTX;
2892 else if (code == NE)
2894 /* if ((x & C) == 0) x ^= C; is transformed to x |= C. */
2895 result = gen_int_mode (HOST_WIDE_INT_1 << bitnum, mode);
2896 result = simplify_gen_binary (IOR, mode, x, result);
2898 else
2900 /* if ((x & C) != 0) x ^= C; is transformed to x &= ~C. */
2901 result = gen_int_mode (~(HOST_WIDE_INT_1 << bitnum), mode);
2902 result = simplify_gen_binary (AND, mode, x, result);
2905 else if (GET_CODE (a) == AND)
2907 /* Check for "if (X & C) x &= ~C". */
2908 if (! rtx_equal_p (x, XEXP (a, 0))
2909 || !CONST_INT_P (XEXP (a, 1))
2910 || (INTVAL (XEXP (a, 1)) & GET_MODE_MASK (mode))
2911 != (~(HOST_WIDE_INT_1 << bitnum) & GET_MODE_MASK (mode)))
2912 return FALSE;
2914 /* if ((x & C) == 0) x &= ~C; is transformed to nothing. */
2915 /* if ((x & C) != 0) x &= ~C; is transformed to x &= ~C. */
2916 result = (code == EQ) ? a : NULL_RTX;
2918 else
2919 return FALSE;
2921 if (result)
2923 start_sequence ();
2924 noce_emit_move_insn (x, result);
2925 seq = end_ifcvt_sequence (if_info);
2926 if (!seq)
2927 return FALSE;
2929 emit_insn_before_setloc (seq, if_info->jump,
2930 INSN_LOCATION (if_info->insn_a));
2932 if_info->transform_name = "noce_try_bitop";
2933 return TRUE;
2937 /* Similar to get_condition, only the resulting condition must be
2938 valid at JUMP, instead of at EARLIEST.
2940 If THEN_ELSE_REVERSED is true, the fallthrough does not go to the
2941 THEN block of the caller, and we have to reverse the condition. */
2943 static rtx
2944 noce_get_condition (rtx_insn *jump, rtx_insn **earliest, bool then_else_reversed)
2946 rtx cond, set, tmp;
2947 bool reverse;
2949 if (! any_condjump_p (jump))
2950 return NULL_RTX;
2952 set = pc_set (jump);
2954 /* If this branches to JUMP_LABEL when the condition is false,
2955 reverse the condition. */
2956 reverse = (GET_CODE (XEXP (SET_SRC (set), 2)) == LABEL_REF
2957 && LABEL_REF_LABEL (XEXP (SET_SRC (set), 2)) == JUMP_LABEL (jump));
2959 /* We may have to reverse because the caller's if block is not canonical,
2960 i.e. the THEN block isn't the fallthrough block for the TEST block
2961 (see find_if_header). */
2962 if (then_else_reversed)
2963 reverse = !reverse;
2965 /* If the condition variable is a register and is MODE_INT, accept it. */
2967 cond = XEXP (SET_SRC (set), 0);
2968 tmp = XEXP (cond, 0);
2969 if (REG_P (tmp) && GET_MODE_CLASS (GET_MODE (tmp)) == MODE_INT
2970 && (GET_MODE (tmp) != BImode
2971 || !targetm.small_register_classes_for_mode_p (BImode)))
2973 *earliest = jump;
2975 if (reverse)
2976 cond = gen_rtx_fmt_ee (reverse_condition (GET_CODE (cond)),
2977 GET_MODE (cond), tmp, XEXP (cond, 1));
2978 return cond;
2981 /* Otherwise, fall back on canonicalize_condition to do the dirty
2982 work of manipulating MODE_CC values and COMPARE rtx codes. */
2983 tmp = canonicalize_condition (jump, cond, reverse, earliest,
2984 NULL_RTX, have_cbranchcc4, true);
2986 /* We don't handle side-effects in the condition, like handling
2987 REG_INC notes and making sure no duplicate conditions are emitted. */
2988 if (tmp != NULL_RTX && side_effects_p (tmp))
2989 return NULL_RTX;
2991 return tmp;
2994 /* Return true if OP is ok for if-then-else processing. */
2996 static int
2997 noce_operand_ok (const_rtx op)
2999 if (side_effects_p (op))
3000 return FALSE;
3002 /* We special-case memories, so handle any of them with
3003 no address side effects. */
3004 if (MEM_P (op))
3005 return ! side_effects_p (XEXP (op, 0));
3007 return ! may_trap_p (op);
3010 /* Return true if X contains a MEM subrtx. */
3012 static bool
3013 contains_mem_rtx_p (rtx x)
3015 subrtx_iterator::array_type array;
3016 FOR_EACH_SUBRTX (iter, array, x, ALL)
3017 if (MEM_P (*iter))
3018 return true;
3020 return false;
3023 /* Return true iff basic block TEST_BB is valid for noce if-conversion.
3024 The condition used in this if-conversion is in COND.
3025 In practice, check that TEST_BB ends with a single set
3026 x := a and all previous computations
3027 in TEST_BB don't produce any values that are live after TEST_BB.
3028 In other words, all the insns in TEST_BB are there only
3029 to compute a value for x. Add the rtx cost of the insns
3030 in TEST_BB to COST. Record whether TEST_BB is a single simple
3031 set instruction in SIMPLE_P. */
3033 static bool
3034 bb_valid_for_noce_process_p (basic_block test_bb, rtx cond,
3035 unsigned int *cost, bool *simple_p)
3037 if (!test_bb)
3038 return false;
3040 rtx_insn *last_insn = last_active_insn (test_bb, FALSE);
3041 rtx last_set = NULL_RTX;
3043 rtx cc = cc_in_cond (cond);
3045 if (!insn_valid_noce_process_p (last_insn, cc))
3046 return false;
3047 last_set = single_set (last_insn);
3049 rtx x = SET_DEST (last_set);
3050 rtx_insn *first_insn = first_active_insn (test_bb);
3051 rtx first_set = single_set (first_insn);
3053 if (!first_set)
3054 return false;
3056 /* We have a single simple set, that's okay. */
3057 bool speed_p = optimize_bb_for_speed_p (test_bb);
3059 if (first_insn == last_insn)
3061 *simple_p = noce_operand_ok (SET_DEST (first_set));
3062 *cost += insn_rtx_cost (first_set, speed_p);
3063 return *simple_p;
3066 rtx_insn *prev_last_insn = PREV_INSN (last_insn);
3067 gcc_assert (prev_last_insn);
3069 /* For now, disallow setting x multiple times in test_bb. */
3070 if (REG_P (x) && reg_set_between_p (x, first_insn, prev_last_insn))
3071 return false;
3073 bitmap test_bb_temps = BITMAP_ALLOC (&reg_obstack);
3075 /* The regs that are live out of test_bb. */
3076 bitmap test_bb_live_out = df_get_live_out (test_bb);
3078 int potential_cost = insn_rtx_cost (last_set, speed_p);
3079 rtx_insn *insn;
3080 FOR_BB_INSNS (test_bb, insn)
3082 if (insn != last_insn)
3084 if (!active_insn_p (insn))
3085 continue;
3087 if (!insn_valid_noce_process_p (insn, cc))
3088 goto free_bitmap_and_fail;
3090 rtx sset = single_set (insn);
3091 gcc_assert (sset);
3093 if (contains_mem_rtx_p (SET_SRC (sset))
3094 || !REG_P (SET_DEST (sset))
3095 || reg_overlap_mentioned_p (SET_DEST (sset), cond))
3096 goto free_bitmap_and_fail;
3098 potential_cost += insn_rtx_cost (sset, speed_p);
3099 bitmap_set_bit (test_bb_temps, REGNO (SET_DEST (sset)));
3103 /* If any of the intermediate results in test_bb are live after test_bb
3104 then fail. */
3105 if (bitmap_intersect_p (test_bb_live_out, test_bb_temps))
3106 goto free_bitmap_and_fail;
3108 BITMAP_FREE (test_bb_temps);
3109 *cost += potential_cost;
3110 *simple_p = false;
3111 return true;
3113 free_bitmap_and_fail:
3114 BITMAP_FREE (test_bb_temps);
3115 return false;
3118 /* We have something like:
3120 if (x > y)
3121 { i = a; j = b; k = c; }
3123 Make it:
3125 tmp_i = (x > y) ? a : i;
3126 tmp_j = (x > y) ? b : j;
3127 tmp_k = (x > y) ? c : k;
3128 i = tmp_i;
3129 j = tmp_j;
3130 k = tmp_k;
3132 Subsequent passes are expected to clean up the extra moves.
3134 Look for special cases such as writes to one register which are
3135 read back in another SET, as might occur in a swap idiom or
3136 similar.
3138 These look like:
3140 if (x > y)
3141 i = a;
3142 j = i;
3144 Which we want to rewrite to:
3146 tmp_i = (x > y) ? a : i;
3147 tmp_j = (x > y) ? tmp_i : j;
3148 i = tmp_i;
3149 j = tmp_j;
3151 We can catch these when looking at (SET x y) by keeping a list of the
3152 registers we would have targeted before if-conversion and looking back
3153 through it for an overlap with Y. If we find one, we rewire the
3154 conditional set to use the temporary we introduced earlier.
3156 IF_INFO contains the useful information about the block structure and
3157 jump instructions. */
3159 static int
3160 noce_convert_multiple_sets (struct noce_if_info *if_info)
3162 basic_block test_bb = if_info->test_bb;
3163 basic_block then_bb = if_info->then_bb;
3164 basic_block join_bb = if_info->join_bb;
3165 rtx_insn *jump = if_info->jump;
3166 rtx_insn *cond_earliest;
3167 rtx_insn *insn;
3169 start_sequence ();
3171 /* Decompose the condition attached to the jump. */
3172 rtx cond = noce_get_condition (jump, &cond_earliest, false);
3173 rtx x = XEXP (cond, 0);
3174 rtx y = XEXP (cond, 1);
3175 rtx_code cond_code = GET_CODE (cond);
3177 /* The true targets for a conditional move. */
3178 auto_vec<rtx> targets;
3179 /* The temporaries introduced to allow us to not consider register
3180 overlap. */
3181 auto_vec<rtx> temporaries;
3182 /* The insns we've emitted. */
3183 auto_vec<rtx_insn *> unmodified_insns;
3184 int count = 0;
3186 FOR_BB_INSNS (then_bb, insn)
3188 /* Skip over non-insns. */
3189 if (!active_insn_p (insn))
3190 continue;
3192 rtx set = single_set (insn);
3193 gcc_checking_assert (set);
3195 rtx target = SET_DEST (set);
3196 rtx temp = gen_reg_rtx (GET_MODE (target));
3197 rtx new_val = SET_SRC (set);
3198 rtx old_val = target;
3200 /* If we were supposed to read from an earlier write in this block,
3201 we've changed the register allocation. Rewire the read. While
3202 we are looking, also try to catch a swap idiom. */
3203 for (int i = count - 1; i >= 0; --i)
3204 if (reg_overlap_mentioned_p (new_val, targets[i]))
3206 /* Catch a "swap" style idiom. */
3207 if (find_reg_note (insn, REG_DEAD, new_val) != NULL_RTX)
3208 /* The write to targets[i] is only live until the read
3209 here. As the condition codes match, we can propagate
3210 the set to here. */
3211 new_val = SET_SRC (single_set (unmodified_insns[i]));
3212 else
3213 new_val = temporaries[i];
3214 break;
3217 /* If we had a non-canonical conditional jump (i.e. one where
3218 the fallthrough is to the "else" case) we need to reverse
3219 the conditional select. */
3220 if (if_info->then_else_reversed)
3221 std::swap (old_val, new_val);
3224 /* We allow simple lowpart register subreg SET sources in
3225 bb_ok_for_noce_convert_multiple_sets. Be careful when processing
3226 sequences like:
3227 (set (reg:SI r1) (reg:SI r2))
3228 (set (reg:HI r3) (subreg:HI (r1)))
3229 For the second insn new_val or old_val (r1 in this example) will be
3230 taken from the temporaries and have the wider mode which will not
3231 match with the mode of the other source of the conditional move, so
3232 we'll end up trying to emit r4:HI = cond ? (r1:SI) : (r3:HI).
3233 Wrap the two cmove operands into subregs if appropriate to prevent
3234 that. */
3235 if (GET_MODE (new_val) != GET_MODE (temp))
3237 machine_mode src_mode = GET_MODE (new_val);
3238 machine_mode dst_mode = GET_MODE (temp);
3239 if (GET_MODE_SIZE (src_mode) <= GET_MODE_SIZE (dst_mode))
3241 end_sequence ();
3242 return FALSE;
3244 new_val = lowpart_subreg (dst_mode, new_val, src_mode);
3246 if (GET_MODE (old_val) != GET_MODE (temp))
3248 machine_mode src_mode = GET_MODE (old_val);
3249 machine_mode dst_mode = GET_MODE (temp);
3250 if (GET_MODE_SIZE (src_mode) <= GET_MODE_SIZE (dst_mode))
3252 end_sequence ();
3253 return FALSE;
3255 old_val = lowpart_subreg (dst_mode, old_val, src_mode);
3258 /* Actually emit the conditional move. */
3259 rtx temp_dest = noce_emit_cmove (if_info, temp, cond_code,
3260 x, y, new_val, old_val);
3262 /* If we failed to expand the conditional move, drop out and don't
3263 try to continue. */
3264 if (temp_dest == NULL_RTX)
3266 end_sequence ();
3267 return FALSE;
3270 /* Bookkeeping. */
3271 count++;
3272 targets.safe_push (target);
3273 temporaries.safe_push (temp_dest);
3274 unmodified_insns.safe_push (insn);
3277 /* We must have seen some sort of insn to insert, otherwise we were
3278 given an empty BB to convert, and we can't handle that. */
3279 gcc_assert (!unmodified_insns.is_empty ());
3281 /* Now fixup the assignments. */
3282 for (int i = 0; i < count; i++)
3283 noce_emit_move_insn (targets[i], temporaries[i]);
3285 /* Actually emit the sequence if it isn't too expensive. */
3286 rtx_insn *seq = get_insns ();
3288 if (!noce_conversion_profitable_p (seq, if_info))
3290 end_sequence ();
3291 return FALSE;
3294 for (insn = seq; insn; insn = NEXT_INSN (insn))
3295 set_used_flags (insn);
3297 /* Mark all our temporaries and targets as used. */
3298 for (int i = 0; i < count; i++)
3300 set_used_flags (temporaries[i]);
3301 set_used_flags (targets[i]);
3304 set_used_flags (cond);
3305 set_used_flags (x);
3306 set_used_flags (y);
3308 unshare_all_rtl_in_chain (seq);
3309 end_sequence ();
3311 if (!seq)
3312 return FALSE;
3314 for (insn = seq; insn; insn = NEXT_INSN (insn))
3315 if (JUMP_P (insn)
3316 || recog_memoized (insn) == -1)
3317 return FALSE;
3319 emit_insn_before_setloc (seq, if_info->jump,
3320 INSN_LOCATION (unmodified_insns.last ()));
3322 /* Clean up THEN_BB and the edges in and out of it. */
3323 remove_edge (find_edge (test_bb, join_bb));
3324 remove_edge (find_edge (then_bb, join_bb));
3325 redirect_edge_and_branch_force (single_succ_edge (test_bb), join_bb);
3326 delete_basic_block (then_bb);
3327 num_true_changes++;
3329 /* Maybe merge blocks now the jump is simple enough. */
3330 if (can_merge_blocks_p (test_bb, join_bb))
3332 merge_blocks (test_bb, join_bb);
3333 num_true_changes++;
3336 num_updated_if_blocks++;
3337 if_info->transform_name = "noce_convert_multiple_sets";
3338 return TRUE;
3341 /* Return true iff basic block TEST_BB is comprised of only
3342 (SET (REG) (REG)) insns suitable for conversion to a series
3343 of conditional moves. Also check that we have more than one set
3344 (other routines can handle a single set better than we would), and
3345 fewer than PARAM_MAX_RTL_IF_CONVERSION_INSNS sets. */
3347 static bool
3348 bb_ok_for_noce_convert_multiple_sets (basic_block test_bb)
3350 rtx_insn *insn;
3351 unsigned count = 0;
3352 unsigned param = PARAM_VALUE (PARAM_MAX_RTL_IF_CONVERSION_INSNS);
3354 FOR_BB_INSNS (test_bb, insn)
3356 /* Skip over notes etc. */
3357 if (!active_insn_p (insn))
3358 continue;
3360 /* We only handle SET insns. */
3361 rtx set = single_set (insn);
3362 if (set == NULL_RTX)
3363 return false;
3365 rtx dest = SET_DEST (set);
3366 rtx src = SET_SRC (set);
3368 /* We can possibly relax this, but for now only handle REG to REG
3369 (including subreg) moves. This avoids any issues that might come
3370 from introducing loads/stores that might violate data-race-freedom
3371 guarantees. */
3372 if (!REG_P (dest))
3373 return false;
3375 if (!(REG_P (src)
3376 || (GET_CODE (src) == SUBREG && REG_P (SUBREG_REG (src))
3377 && subreg_lowpart_p (src))))
3378 return false;
3380 /* Destination must be appropriate for a conditional write. */
3381 if (!noce_operand_ok (dest))
3382 return false;
3384 /* We must be able to conditionally move in this mode. */
3385 if (!can_conditionally_move_p (GET_MODE (dest)))
3386 return false;
3388 count++;
3391 /* If we would only put out one conditional move, the other strategies
3392 this pass tries are better optimized and will be more appropriate.
3393 Some targets want to strictly limit the number of conditional moves
3394 that are emitted, they set this through PARAM, we need to respect
3395 that. */
3396 return count > 1 && count <= param;
3399 /* Given a simple IF-THEN-JOIN or IF-THEN-ELSE-JOIN block, attempt to convert
3400 it without using conditional execution. Return TRUE if we were successful
3401 at converting the block. */
3403 static int
3404 noce_process_if_block (struct noce_if_info *if_info)
3406 basic_block test_bb = if_info->test_bb; /* test block */
3407 basic_block then_bb = if_info->then_bb; /* THEN */
3408 basic_block else_bb = if_info->else_bb; /* ELSE or NULL */
3409 basic_block join_bb = if_info->join_bb; /* JOIN */
3410 rtx_insn *jump = if_info->jump;
3411 rtx cond = if_info->cond;
3412 rtx_insn *insn_a, *insn_b;
3413 rtx set_a, set_b;
3414 rtx orig_x, x, a, b;
3416 /* We're looking for patterns of the form
3418 (1) if (...) x = a; else x = b;
3419 (2) x = b; if (...) x = a;
3420 (3) if (...) x = a; // as if with an initial x = x.
3421 (4) if (...) { x = a; y = b; z = c; } // Like 3, for multiple SETS.
3422 The later patterns require jumps to be more expensive.
3423 For the if (...) x = a; else x = b; case we allow multiple insns
3424 inside the then and else blocks as long as their only effect is
3425 to calculate a value for x.
3426 ??? For future expansion, further expand the "multiple X" rules. */
3428 /* First look for multiple SETS. */
3429 if (!else_bb
3430 && HAVE_conditional_move
3431 && !HAVE_cc0
3432 && bb_ok_for_noce_convert_multiple_sets (then_bb))
3434 if (noce_convert_multiple_sets (if_info))
3436 if (dump_file && if_info->transform_name)
3437 fprintf (dump_file, "if-conversion succeeded through %s\n",
3438 if_info->transform_name);
3439 return TRUE;
3443 if (! bb_valid_for_noce_process_p (then_bb, cond, &if_info->original_cost,
3444 &if_info->then_simple))
3445 return false;
3447 if (else_bb
3448 && ! bb_valid_for_noce_process_p (else_bb, cond, &if_info->original_cost,
3449 &if_info->else_simple))
3450 return false;
3452 insn_a = last_active_insn (then_bb, FALSE);
3453 set_a = single_set (insn_a);
3454 gcc_assert (set_a);
3456 x = SET_DEST (set_a);
3457 a = SET_SRC (set_a);
3459 /* Look for the other potential set. Make sure we've got equivalent
3460 destinations. */
3461 /* ??? This is overconservative. Storing to two different mems is
3462 as easy as conditionally computing the address. Storing to a
3463 single mem merely requires a scratch memory to use as one of the
3464 destination addresses; often the memory immediately below the
3465 stack pointer is available for this. */
3466 set_b = NULL_RTX;
3467 if (else_bb)
3469 insn_b = last_active_insn (else_bb, FALSE);
3470 set_b = single_set (insn_b);
3471 gcc_assert (set_b);
3473 if (!rtx_interchangeable_p (x, SET_DEST (set_b)))
3474 return FALSE;
3476 else
3478 insn_b = prev_nonnote_nondebug_insn (if_info->cond_earliest);
3479 /* We're going to be moving the evaluation of B down from above
3480 COND_EARLIEST to JUMP. Make sure the relevant data is still
3481 intact. */
3482 if (! insn_b
3483 || BLOCK_FOR_INSN (insn_b) != BLOCK_FOR_INSN (if_info->cond_earliest)
3484 || !NONJUMP_INSN_P (insn_b)
3485 || (set_b = single_set (insn_b)) == NULL_RTX
3486 || ! rtx_interchangeable_p (x, SET_DEST (set_b))
3487 || ! noce_operand_ok (SET_SRC (set_b))
3488 || reg_overlap_mentioned_p (x, SET_SRC (set_b))
3489 || modified_between_p (SET_SRC (set_b), insn_b, jump)
3490 /* Avoid extending the lifetime of hard registers on small
3491 register class machines. */
3492 || (REG_P (SET_SRC (set_b))
3493 && HARD_REGISTER_P (SET_SRC (set_b))
3494 && targetm.small_register_classes_for_mode_p
3495 (GET_MODE (SET_SRC (set_b))))
3496 /* Likewise with X. In particular this can happen when
3497 noce_get_condition looks farther back in the instruction
3498 stream than one might expect. */
3499 || reg_overlap_mentioned_p (x, cond)
3500 || reg_overlap_mentioned_p (x, a)
3501 || modified_between_p (x, insn_b, jump))
3503 insn_b = NULL;
3504 set_b = NULL_RTX;
3508 /* If x has side effects then only the if-then-else form is safe to
3509 convert. But even in that case we would need to restore any notes
3510 (such as REG_INC) at then end. That can be tricky if
3511 noce_emit_move_insn expands to more than one insn, so disable the
3512 optimization entirely for now if there are side effects. */
3513 if (side_effects_p (x))
3514 return FALSE;
3516 b = (set_b ? SET_SRC (set_b) : x);
3518 /* Only operate on register destinations, and even then avoid extending
3519 the lifetime of hard registers on small register class machines. */
3520 orig_x = x;
3521 if_info->orig_x = orig_x;
3522 if (!REG_P (x)
3523 || (HARD_REGISTER_P (x)
3524 && targetm.small_register_classes_for_mode_p (GET_MODE (x))))
3526 if (GET_MODE (x) == BLKmode)
3527 return FALSE;
3529 if (GET_CODE (x) == ZERO_EXTRACT
3530 && (!CONST_INT_P (XEXP (x, 1))
3531 || !CONST_INT_P (XEXP (x, 2))))
3532 return FALSE;
3534 x = gen_reg_rtx (GET_MODE (GET_CODE (x) == STRICT_LOW_PART
3535 ? XEXP (x, 0) : x));
3538 /* Don't operate on sources that may trap or are volatile. */
3539 if (! noce_operand_ok (a) || ! noce_operand_ok (b))
3540 return FALSE;
3542 retry:
3543 /* Set up the info block for our subroutines. */
3544 if_info->insn_a = insn_a;
3545 if_info->insn_b = insn_b;
3546 if_info->x = x;
3547 if_info->a = a;
3548 if_info->b = b;
3550 /* Try optimizations in some approximation of a useful order. */
3551 /* ??? Should first look to see if X is live incoming at all. If it
3552 isn't, we don't need anything but an unconditional set. */
3554 /* Look and see if A and B are really the same. Avoid creating silly
3555 cmove constructs that no one will fix up later. */
3556 if (noce_simple_bbs (if_info)
3557 && rtx_interchangeable_p (a, b))
3559 /* If we have an INSN_B, we don't have to create any new rtl. Just
3560 move the instruction that we already have. If we don't have an
3561 INSN_B, that means that A == X, and we've got a noop move. In
3562 that case don't do anything and let the code below delete INSN_A. */
3563 if (insn_b && else_bb)
3565 rtx note;
3567 if (else_bb && insn_b == BB_END (else_bb))
3568 BB_END (else_bb) = PREV_INSN (insn_b);
3569 reorder_insns (insn_b, insn_b, PREV_INSN (jump));
3571 /* If there was a REG_EQUAL note, delete it since it may have been
3572 true due to this insn being after a jump. */
3573 if ((note = find_reg_note (insn_b, REG_EQUAL, NULL_RTX)) != 0)
3574 remove_note (insn_b, note);
3576 insn_b = NULL;
3578 /* If we have "x = b; if (...) x = a;", and x has side-effects, then
3579 x must be executed twice. */
3580 else if (insn_b && side_effects_p (orig_x))
3581 return FALSE;
3583 x = orig_x;
3584 goto success;
3587 if (!set_b && MEM_P (orig_x))
3588 /* We want to avoid store speculation to avoid cases like
3589 if (pthread_mutex_trylock(mutex))
3590 ++global_variable;
3591 Rather than go to much effort here, we rely on the SSA optimizers,
3592 which do a good enough job these days. */
3593 return FALSE;
3595 if (noce_try_move (if_info))
3596 goto success;
3597 if (noce_try_ifelse_collapse (if_info))
3598 goto success;
3599 if (noce_try_store_flag (if_info))
3600 goto success;
3601 if (noce_try_bitop (if_info))
3602 goto success;
3603 if (noce_try_minmax (if_info))
3604 goto success;
3605 if (noce_try_abs (if_info))
3606 goto success;
3607 if (noce_try_inverse_constants (if_info))
3608 goto success;
3609 if (!targetm.have_conditional_execution ()
3610 && noce_try_store_flag_constants (if_info))
3611 goto success;
3612 if (HAVE_conditional_move
3613 && noce_try_cmove (if_info))
3614 goto success;
3615 if (! targetm.have_conditional_execution ())
3617 if (noce_try_addcc (if_info))
3618 goto success;
3619 if (noce_try_store_flag_mask (if_info))
3620 goto success;
3621 if (HAVE_conditional_move
3622 && noce_try_cmove_arith (if_info))
3623 goto success;
3624 if (noce_try_sign_mask (if_info))
3625 goto success;
3628 if (!else_bb && set_b)
3630 insn_b = NULL;
3631 set_b = NULL_RTX;
3632 b = orig_x;
3633 goto retry;
3636 return FALSE;
3638 success:
3639 if (dump_file && if_info->transform_name)
3640 fprintf (dump_file, "if-conversion succeeded through %s\n",
3641 if_info->transform_name);
3643 /* If we used a temporary, fix it up now. */
3644 if (orig_x != x)
3646 rtx_insn *seq;
3648 start_sequence ();
3649 noce_emit_move_insn (orig_x, x);
3650 seq = get_insns ();
3651 set_used_flags (orig_x);
3652 unshare_all_rtl_in_chain (seq);
3653 end_sequence ();
3655 emit_insn_before_setloc (seq, BB_END (test_bb), INSN_LOCATION (insn_a));
3658 /* The original THEN and ELSE blocks may now be removed. The test block
3659 must now jump to the join block. If the test block and the join block
3660 can be merged, do so. */
3661 if (else_bb)
3663 delete_basic_block (else_bb);
3664 num_true_changes++;
3666 else
3667 remove_edge (find_edge (test_bb, join_bb));
3669 remove_edge (find_edge (then_bb, join_bb));
3670 redirect_edge_and_branch_force (single_succ_edge (test_bb), join_bb);
3671 delete_basic_block (then_bb);
3672 num_true_changes++;
3674 if (can_merge_blocks_p (test_bb, join_bb))
3676 merge_blocks (test_bb, join_bb);
3677 num_true_changes++;
3680 num_updated_if_blocks++;
3681 return TRUE;
3684 /* Check whether a block is suitable for conditional move conversion.
3685 Every insn must be a simple set of a register to a constant or a
3686 register. For each assignment, store the value in the pointer map
3687 VALS, keyed indexed by register pointer, then store the register
3688 pointer in REGS. COND is the condition we will test. */
3690 static int
3691 check_cond_move_block (basic_block bb,
3692 hash_map<rtx, rtx> *vals,
3693 vec<rtx> *regs,
3694 rtx cond)
3696 rtx_insn *insn;
3697 rtx cc = cc_in_cond (cond);
3699 /* We can only handle simple jumps at the end of the basic block.
3700 It is almost impossible to update the CFG otherwise. */
3701 insn = BB_END (bb);
3702 if (JUMP_P (insn) && !onlyjump_p (insn))
3703 return FALSE;
3705 FOR_BB_INSNS (bb, insn)
3707 rtx set, dest, src;
3709 if (!NONDEBUG_INSN_P (insn) || JUMP_P (insn))
3710 continue;
3711 set = single_set (insn);
3712 if (!set)
3713 return FALSE;
3715 dest = SET_DEST (set);
3716 src = SET_SRC (set);
3717 if (!REG_P (dest)
3718 || (HARD_REGISTER_P (dest)
3719 && targetm.small_register_classes_for_mode_p (GET_MODE (dest))))
3720 return FALSE;
3722 if (!CONSTANT_P (src) && !register_operand (src, VOIDmode))
3723 return FALSE;
3725 if (side_effects_p (src) || side_effects_p (dest))
3726 return FALSE;
3728 if (may_trap_p (src) || may_trap_p (dest))
3729 return FALSE;
3731 /* Don't try to handle this if the source register was
3732 modified earlier in the block. */
3733 if ((REG_P (src)
3734 && vals->get (src))
3735 || (GET_CODE (src) == SUBREG && REG_P (SUBREG_REG (src))
3736 && vals->get (SUBREG_REG (src))))
3737 return FALSE;
3739 /* Don't try to handle this if the destination register was
3740 modified earlier in the block. */
3741 if (vals->get (dest))
3742 return FALSE;
3744 /* Don't try to handle this if the condition uses the
3745 destination register. */
3746 if (reg_overlap_mentioned_p (dest, cond))
3747 return FALSE;
3749 /* Don't try to handle this if the source register is modified
3750 later in the block. */
3751 if (!CONSTANT_P (src)
3752 && modified_between_p (src, insn, NEXT_INSN (BB_END (bb))))
3753 return FALSE;
3755 /* Skip it if the instruction to be moved might clobber CC. */
3756 if (cc && set_of (cc, insn))
3757 return FALSE;
3759 vals->put (dest, src);
3761 regs->safe_push (dest);
3764 return TRUE;
3767 /* Given a basic block BB suitable for conditional move conversion,
3768 a condition COND, and pointer maps THEN_VALS and ELSE_VALS containing
3769 the register values depending on COND, emit the insns in the block as
3770 conditional moves. If ELSE_BLOCK is true, THEN_BB was already
3771 processed. The caller has started a sequence for the conversion.
3772 Return true if successful, false if something goes wrong. */
3774 static bool
3775 cond_move_convert_if_block (struct noce_if_info *if_infop,
3776 basic_block bb, rtx cond,
3777 hash_map<rtx, rtx> *then_vals,
3778 hash_map<rtx, rtx> *else_vals,
3779 bool else_block_p)
3781 enum rtx_code code;
3782 rtx_insn *insn;
3783 rtx cond_arg0, cond_arg1;
3785 code = GET_CODE (cond);
3786 cond_arg0 = XEXP (cond, 0);
3787 cond_arg1 = XEXP (cond, 1);
3789 FOR_BB_INSNS (bb, insn)
3791 rtx set, target, dest, t, e;
3793 /* ??? Maybe emit conditional debug insn? */
3794 if (!NONDEBUG_INSN_P (insn) || JUMP_P (insn))
3795 continue;
3796 set = single_set (insn);
3797 gcc_assert (set && REG_P (SET_DEST (set)));
3799 dest = SET_DEST (set);
3801 rtx *then_slot = then_vals->get (dest);
3802 rtx *else_slot = else_vals->get (dest);
3803 t = then_slot ? *then_slot : NULL_RTX;
3804 e = else_slot ? *else_slot : NULL_RTX;
3806 if (else_block_p)
3808 /* If this register was set in the then block, we already
3809 handled this case there. */
3810 if (t)
3811 continue;
3812 t = dest;
3813 gcc_assert (e);
3815 else
3817 gcc_assert (t);
3818 if (!e)
3819 e = dest;
3822 target = noce_emit_cmove (if_infop, dest, code, cond_arg0, cond_arg1,
3823 t, e);
3824 if (!target)
3825 return false;
3827 if (target != dest)
3828 noce_emit_move_insn (dest, target);
3831 return true;
3834 /* Given a simple IF-THEN-JOIN or IF-THEN-ELSE-JOIN block, attempt to convert
3835 it using only conditional moves. Return TRUE if we were successful at
3836 converting the block. */
3838 static int
3839 cond_move_process_if_block (struct noce_if_info *if_info)
3841 basic_block test_bb = if_info->test_bb;
3842 basic_block then_bb = if_info->then_bb;
3843 basic_block else_bb = if_info->else_bb;
3844 basic_block join_bb = if_info->join_bb;
3845 rtx_insn *jump = if_info->jump;
3846 rtx cond = if_info->cond;
3847 rtx_insn *seq, *loc_insn;
3848 rtx reg;
3849 int c;
3850 vec<rtx> then_regs = vNULL;
3851 vec<rtx> else_regs = vNULL;
3852 unsigned int i;
3853 int success_p = FALSE;
3854 int limit = PARAM_VALUE (PARAM_MAX_RTL_IF_CONVERSION_INSNS);
3856 /* Build a mapping for each block to the value used for each
3857 register. */
3858 hash_map<rtx, rtx> then_vals;
3859 hash_map<rtx, rtx> else_vals;
3861 /* Make sure the blocks are suitable. */
3862 if (!check_cond_move_block (then_bb, &then_vals, &then_regs, cond)
3863 || (else_bb
3864 && !check_cond_move_block (else_bb, &else_vals, &else_regs, cond)))
3865 goto done;
3867 /* Make sure the blocks can be used together. If the same register
3868 is set in both blocks, and is not set to a constant in both
3869 cases, then both blocks must set it to the same register. We
3870 have already verified that if it is set to a register, that the
3871 source register does not change after the assignment. Also count
3872 the number of registers set in only one of the blocks. */
3873 c = 0;
3874 FOR_EACH_VEC_ELT (then_regs, i, reg)
3876 rtx *then_slot = then_vals.get (reg);
3877 rtx *else_slot = else_vals.get (reg);
3879 gcc_checking_assert (then_slot);
3880 if (!else_slot)
3881 ++c;
3882 else
3884 rtx then_val = *then_slot;
3885 rtx else_val = *else_slot;
3886 if (!CONSTANT_P (then_val) && !CONSTANT_P (else_val)
3887 && !rtx_equal_p (then_val, else_val))
3888 goto done;
3892 /* Finish off c for MAX_CONDITIONAL_EXECUTE. */
3893 FOR_EACH_VEC_ELT (else_regs, i, reg)
3895 gcc_checking_assert (else_vals.get (reg));
3896 if (!then_vals.get (reg))
3897 ++c;
3900 /* Make sure it is reasonable to convert this block. What matters
3901 is the number of assignments currently made in only one of the
3902 branches, since if we convert we are going to always execute
3903 them. */
3904 if (c > MAX_CONDITIONAL_EXECUTE
3905 || c > limit)
3906 goto done;
3908 /* Try to emit the conditional moves. First do the then block,
3909 then do anything left in the else blocks. */
3910 start_sequence ();
3911 if (!cond_move_convert_if_block (if_info, then_bb, cond,
3912 &then_vals, &else_vals, false)
3913 || (else_bb
3914 && !cond_move_convert_if_block (if_info, else_bb, cond,
3915 &then_vals, &else_vals, true)))
3917 end_sequence ();
3918 goto done;
3920 seq = end_ifcvt_sequence (if_info);
3921 if (!seq)
3922 goto done;
3924 loc_insn = first_active_insn (then_bb);
3925 if (!loc_insn)
3927 loc_insn = first_active_insn (else_bb);
3928 gcc_assert (loc_insn);
3930 emit_insn_before_setloc (seq, jump, INSN_LOCATION (loc_insn));
3932 if (else_bb)
3934 delete_basic_block (else_bb);
3935 num_true_changes++;
3937 else
3938 remove_edge (find_edge (test_bb, join_bb));
3940 remove_edge (find_edge (then_bb, join_bb));
3941 redirect_edge_and_branch_force (single_succ_edge (test_bb), join_bb);
3942 delete_basic_block (then_bb);
3943 num_true_changes++;
3945 if (can_merge_blocks_p (test_bb, join_bb))
3947 merge_blocks (test_bb, join_bb);
3948 num_true_changes++;
3951 num_updated_if_blocks++;
3952 success_p = TRUE;
3954 done:
3955 then_regs.release ();
3956 else_regs.release ();
3957 return success_p;
3961 /* Determine if a given basic block heads a simple IF-THEN-JOIN or an
3962 IF-THEN-ELSE-JOIN block.
3964 If so, we'll try to convert the insns to not require the branch,
3965 using only transformations that do not require conditional execution.
3967 Return TRUE if we were successful at converting the block. */
3969 static int
3970 noce_find_if_block (basic_block test_bb, edge then_edge, edge else_edge,
3971 int pass)
3973 basic_block then_bb, else_bb, join_bb;
3974 bool then_else_reversed = false;
3975 rtx_insn *jump;
3976 rtx cond;
3977 rtx_insn *cond_earliest;
3978 struct noce_if_info if_info;
3979 bool speed_p = optimize_bb_for_speed_p (test_bb);
3981 /* We only ever should get here before reload. */
3982 gcc_assert (!reload_completed);
3984 /* Recognize an IF-THEN-ELSE-JOIN block. */
3985 if (single_pred_p (then_edge->dest)
3986 && single_succ_p (then_edge->dest)
3987 && single_pred_p (else_edge->dest)
3988 && single_succ_p (else_edge->dest)
3989 && single_succ (then_edge->dest) == single_succ (else_edge->dest))
3991 then_bb = then_edge->dest;
3992 else_bb = else_edge->dest;
3993 join_bb = single_succ (then_bb);
3995 /* Recognize an IF-THEN-JOIN block. */
3996 else if (single_pred_p (then_edge->dest)
3997 && single_succ_p (then_edge->dest)
3998 && single_succ (then_edge->dest) == else_edge->dest)
4000 then_bb = then_edge->dest;
4001 else_bb = NULL_BLOCK;
4002 join_bb = else_edge->dest;
4004 /* Recognize an IF-ELSE-JOIN block. We can have those because the order
4005 of basic blocks in cfglayout mode does not matter, so the fallthrough
4006 edge can go to any basic block (and not just to bb->next_bb, like in
4007 cfgrtl mode). */
4008 else if (single_pred_p (else_edge->dest)
4009 && single_succ_p (else_edge->dest)
4010 && single_succ (else_edge->dest) == then_edge->dest)
4012 /* The noce transformations do not apply to IF-ELSE-JOIN blocks.
4013 To make this work, we have to invert the THEN and ELSE blocks
4014 and reverse the jump condition. */
4015 then_bb = else_edge->dest;
4016 else_bb = NULL_BLOCK;
4017 join_bb = single_succ (then_bb);
4018 then_else_reversed = true;
4020 else
4021 /* Not a form we can handle. */
4022 return FALSE;
4024 /* The edges of the THEN and ELSE blocks cannot have complex edges. */
4025 if (single_succ_edge (then_bb)->flags & EDGE_COMPLEX)
4026 return FALSE;
4027 if (else_bb
4028 && single_succ_edge (else_bb)->flags & EDGE_COMPLEX)
4029 return FALSE;
4031 num_possible_if_blocks++;
4033 if (dump_file)
4035 fprintf (dump_file,
4036 "\nIF-THEN%s-JOIN block found, pass %d, test %d, then %d",
4037 (else_bb) ? "-ELSE" : "",
4038 pass, test_bb->index, then_bb->index);
4040 if (else_bb)
4041 fprintf (dump_file, ", else %d", else_bb->index);
4043 fprintf (dump_file, ", join %d\n", join_bb->index);
4046 /* If the conditional jump is more than just a conditional
4047 jump, then we can not do if-conversion on this block. */
4048 jump = BB_END (test_bb);
4049 if (! onlyjump_p (jump))
4050 return FALSE;
4052 /* If this is not a standard conditional jump, we can't parse it. */
4053 cond = noce_get_condition (jump, &cond_earliest, then_else_reversed);
4054 if (!cond)
4055 return FALSE;
4057 /* We must be comparing objects whose modes imply the size. */
4058 if (GET_MODE (XEXP (cond, 0)) == BLKmode)
4059 return FALSE;
4061 /* Initialize an IF_INFO struct to pass around. */
4062 memset (&if_info, 0, sizeof if_info);
4063 if_info.test_bb = test_bb;
4064 if_info.then_bb = then_bb;
4065 if_info.else_bb = else_bb;
4066 if_info.join_bb = join_bb;
4067 if_info.cond = cond;
4068 if_info.cond_earliest = cond_earliest;
4069 if_info.jump = jump;
4070 if_info.then_else_reversed = then_else_reversed;
4071 if_info.speed_p = speed_p;
4072 if_info.max_seq_cost
4073 = targetm.max_noce_ifcvt_seq_cost (then_edge);
4074 /* We'll add in the cost of THEN_BB and ELSE_BB later, when we check
4075 that they are valid to transform. We can't easily get back to the insn
4076 for COND (and it may not exist if we had to canonicalize to get COND),
4077 and jump_insns are always given a cost of 1 by seq_cost, so treat
4078 both instructions as having cost COSTS_N_INSNS (1). */
4079 if_info.original_cost = COSTS_N_INSNS (2);
4082 /* Do the real work. */
4084 if (noce_process_if_block (&if_info))
4085 return TRUE;
4087 if (HAVE_conditional_move
4088 && cond_move_process_if_block (&if_info))
4089 return TRUE;
4091 return FALSE;
4095 /* Merge the blocks and mark for local life update. */
4097 static void
4098 merge_if_block (struct ce_if_block * ce_info)
4100 basic_block test_bb = ce_info->test_bb; /* last test block */
4101 basic_block then_bb = ce_info->then_bb; /* THEN */
4102 basic_block else_bb = ce_info->else_bb; /* ELSE or NULL */
4103 basic_block join_bb = ce_info->join_bb; /* join block */
4104 basic_block combo_bb;
4106 /* All block merging is done into the lower block numbers. */
4108 combo_bb = test_bb;
4109 df_set_bb_dirty (test_bb);
4111 /* Merge any basic blocks to handle && and || subtests. Each of
4112 the blocks are on the fallthru path from the predecessor block. */
4113 if (ce_info->num_multiple_test_blocks > 0)
4115 basic_block bb = test_bb;
4116 basic_block last_test_bb = ce_info->last_test_bb;
4117 basic_block fallthru = block_fallthru (bb);
4121 bb = fallthru;
4122 fallthru = block_fallthru (bb);
4123 merge_blocks (combo_bb, bb);
4124 num_true_changes++;
4126 while (bb != last_test_bb);
4129 /* Merge TEST block into THEN block. Normally the THEN block won't have a
4130 label, but it might if there were || tests. That label's count should be
4131 zero, and it normally should be removed. */
4133 if (then_bb)
4135 /* If THEN_BB has no successors, then there's a BARRIER after it.
4136 If COMBO_BB has more than one successor (THEN_BB), then that BARRIER
4137 is no longer needed, and in fact it is incorrect to leave it in
4138 the insn stream. */
4139 if (EDGE_COUNT (then_bb->succs) == 0
4140 && EDGE_COUNT (combo_bb->succs) > 1)
4142 rtx_insn *end = NEXT_INSN (BB_END (then_bb));
4143 while (end && NOTE_P (end) && !NOTE_INSN_BASIC_BLOCK_P (end))
4144 end = NEXT_INSN (end);
4146 if (end && BARRIER_P (end))
4147 delete_insn (end);
4149 merge_blocks (combo_bb, then_bb);
4150 num_true_changes++;
4153 /* The ELSE block, if it existed, had a label. That label count
4154 will almost always be zero, but odd things can happen when labels
4155 get their addresses taken. */
4156 if (else_bb)
4158 /* If ELSE_BB has no successors, then there's a BARRIER after it.
4159 If COMBO_BB has more than one successor (ELSE_BB), then that BARRIER
4160 is no longer needed, and in fact it is incorrect to leave it in
4161 the insn stream. */
4162 if (EDGE_COUNT (else_bb->succs) == 0
4163 && EDGE_COUNT (combo_bb->succs) > 1)
4165 rtx_insn *end = NEXT_INSN (BB_END (else_bb));
4166 while (end && NOTE_P (end) && !NOTE_INSN_BASIC_BLOCK_P (end))
4167 end = NEXT_INSN (end);
4169 if (end && BARRIER_P (end))
4170 delete_insn (end);
4172 merge_blocks (combo_bb, else_bb);
4173 num_true_changes++;
4176 /* If there was no join block reported, that means it was not adjacent
4177 to the others, and so we cannot merge them. */
4179 if (! join_bb)
4181 rtx_insn *last = BB_END (combo_bb);
4183 /* The outgoing edge for the current COMBO block should already
4184 be correct. Verify this. */
4185 if (EDGE_COUNT (combo_bb->succs) == 0)
4186 gcc_assert (find_reg_note (last, REG_NORETURN, NULL)
4187 || (NONJUMP_INSN_P (last)
4188 && GET_CODE (PATTERN (last)) == TRAP_IF
4189 && (TRAP_CONDITION (PATTERN (last))
4190 == const_true_rtx)));
4192 else
4193 /* There should still be something at the end of the THEN or ELSE
4194 blocks taking us to our final destination. */
4195 gcc_assert (JUMP_P (last)
4196 || (EDGE_SUCC (combo_bb, 0)->dest
4197 == EXIT_BLOCK_PTR_FOR_FN (cfun)
4198 && CALL_P (last)
4199 && SIBLING_CALL_P (last))
4200 || ((EDGE_SUCC (combo_bb, 0)->flags & EDGE_EH)
4201 && can_throw_internal (last)));
4204 /* The JOIN block may have had quite a number of other predecessors too.
4205 Since we've already merged the TEST, THEN and ELSE blocks, we should
4206 have only one remaining edge from our if-then-else diamond. If there
4207 is more than one remaining edge, it must come from elsewhere. There
4208 may be zero incoming edges if the THEN block didn't actually join
4209 back up (as with a call to a non-return function). */
4210 else if (EDGE_COUNT (join_bb->preds) < 2
4211 && join_bb != EXIT_BLOCK_PTR_FOR_FN (cfun))
4213 /* We can merge the JOIN cleanly and update the dataflow try
4214 again on this pass.*/
4215 merge_blocks (combo_bb, join_bb);
4216 num_true_changes++;
4218 else
4220 /* We cannot merge the JOIN. */
4222 /* The outgoing edge for the current COMBO block should already
4223 be correct. Verify this. */
4224 gcc_assert (single_succ_p (combo_bb)
4225 && single_succ (combo_bb) == join_bb);
4227 /* Remove the jump and cruft from the end of the COMBO block. */
4228 if (join_bb != EXIT_BLOCK_PTR_FOR_FN (cfun))
4229 tidy_fallthru_edge (single_succ_edge (combo_bb));
4232 num_updated_if_blocks++;
4235 /* Find a block ending in a simple IF condition and try to transform it
4236 in some way. When converting a multi-block condition, put the new code
4237 in the first such block and delete the rest. Return a pointer to this
4238 first block if some transformation was done. Return NULL otherwise. */
4240 static basic_block
4241 find_if_header (basic_block test_bb, int pass)
4243 ce_if_block ce_info;
4244 edge then_edge;
4245 edge else_edge;
4247 /* The kind of block we're looking for has exactly two successors. */
4248 if (EDGE_COUNT (test_bb->succs) != 2)
4249 return NULL;
4251 then_edge = EDGE_SUCC (test_bb, 0);
4252 else_edge = EDGE_SUCC (test_bb, 1);
4254 if (df_get_bb_dirty (then_edge->dest))
4255 return NULL;
4256 if (df_get_bb_dirty (else_edge->dest))
4257 return NULL;
4259 /* Neither edge should be abnormal. */
4260 if ((then_edge->flags & EDGE_COMPLEX)
4261 || (else_edge->flags & EDGE_COMPLEX))
4262 return NULL;
4264 /* Nor exit the loop. */
4265 if ((then_edge->flags & EDGE_LOOP_EXIT)
4266 || (else_edge->flags & EDGE_LOOP_EXIT))
4267 return NULL;
4269 /* The THEN edge is canonically the one that falls through. */
4270 if (then_edge->flags & EDGE_FALLTHRU)
4272 else if (else_edge->flags & EDGE_FALLTHRU)
4273 std::swap (then_edge, else_edge);
4274 else
4275 /* Otherwise this must be a multiway branch of some sort. */
4276 return NULL;
4278 memset (&ce_info, 0, sizeof (ce_info));
4279 ce_info.test_bb = test_bb;
4280 ce_info.then_bb = then_edge->dest;
4281 ce_info.else_bb = else_edge->dest;
4282 ce_info.pass = pass;
4284 #ifdef IFCVT_MACHDEP_INIT
4285 IFCVT_MACHDEP_INIT (&ce_info);
4286 #endif
4288 if (!reload_completed
4289 && noce_find_if_block (test_bb, then_edge, else_edge, pass))
4290 goto success;
4292 if (reload_completed
4293 && targetm.have_conditional_execution ()
4294 && cond_exec_find_if_block (&ce_info))
4295 goto success;
4297 if (targetm.have_trap ()
4298 && optab_handler (ctrap_optab, word_mode) != CODE_FOR_nothing
4299 && find_cond_trap (test_bb, then_edge, else_edge))
4300 goto success;
4302 if (dom_info_state (CDI_POST_DOMINATORS) >= DOM_NO_FAST_QUERY
4303 && (reload_completed || !targetm.have_conditional_execution ()))
4305 if (find_if_case_1 (test_bb, then_edge, else_edge))
4306 goto success;
4307 if (find_if_case_2 (test_bb, then_edge, else_edge))
4308 goto success;
4311 return NULL;
4313 success:
4314 if (dump_file)
4315 fprintf (dump_file, "Conversion succeeded on pass %d.\n", pass);
4316 /* Set this so we continue looking. */
4317 cond_exec_changed_p = TRUE;
4318 return ce_info.test_bb;
4321 /* Return true if a block has two edges, one of which falls through to the next
4322 block, and the other jumps to a specific block, so that we can tell if the
4323 block is part of an && test or an || test. Returns either -1 or the number
4324 of non-note, non-jump, non-USE/CLOBBER insns in the block. */
4326 static int
4327 block_jumps_and_fallthru_p (basic_block cur_bb, basic_block target_bb)
4329 edge cur_edge;
4330 int fallthru_p = FALSE;
4331 int jump_p = FALSE;
4332 rtx_insn *insn;
4333 rtx_insn *end;
4334 int n_insns = 0;
4335 edge_iterator ei;
4337 if (!cur_bb || !target_bb)
4338 return -1;
4340 /* If no edges, obviously it doesn't jump or fallthru. */
4341 if (EDGE_COUNT (cur_bb->succs) == 0)
4342 return FALSE;
4344 FOR_EACH_EDGE (cur_edge, ei, cur_bb->succs)
4346 if (cur_edge->flags & EDGE_COMPLEX)
4347 /* Anything complex isn't what we want. */
4348 return -1;
4350 else if (cur_edge->flags & EDGE_FALLTHRU)
4351 fallthru_p = TRUE;
4353 else if (cur_edge->dest == target_bb)
4354 jump_p = TRUE;
4356 else
4357 return -1;
4360 if ((jump_p & fallthru_p) == 0)
4361 return -1;
4363 /* Don't allow calls in the block, since this is used to group && and ||
4364 together for conditional execution support. ??? we should support
4365 conditional execution support across calls for IA-64 some day, but
4366 for now it makes the code simpler. */
4367 end = BB_END (cur_bb);
4368 insn = BB_HEAD (cur_bb);
4370 while (insn != NULL_RTX)
4372 if (CALL_P (insn))
4373 return -1;
4375 if (INSN_P (insn)
4376 && !JUMP_P (insn)
4377 && !DEBUG_INSN_P (insn)
4378 && GET_CODE (PATTERN (insn)) != USE
4379 && GET_CODE (PATTERN (insn)) != CLOBBER)
4380 n_insns++;
4382 if (insn == end)
4383 break;
4385 insn = NEXT_INSN (insn);
4388 return n_insns;
4391 /* Determine if a given basic block heads a simple IF-THEN or IF-THEN-ELSE
4392 block. If so, we'll try to convert the insns to not require the branch.
4393 Return TRUE if we were successful at converting the block. */
4395 static int
4396 cond_exec_find_if_block (struct ce_if_block * ce_info)
4398 basic_block test_bb = ce_info->test_bb;
4399 basic_block then_bb = ce_info->then_bb;
4400 basic_block else_bb = ce_info->else_bb;
4401 basic_block join_bb = NULL_BLOCK;
4402 edge cur_edge;
4403 basic_block next;
4404 edge_iterator ei;
4406 ce_info->last_test_bb = test_bb;
4408 /* We only ever should get here after reload,
4409 and if we have conditional execution. */
4410 gcc_assert (reload_completed && targetm.have_conditional_execution ());
4412 /* Discover if any fall through predecessors of the current test basic block
4413 were && tests (which jump to the else block) or || tests (which jump to
4414 the then block). */
4415 if (single_pred_p (test_bb)
4416 && single_pred_edge (test_bb)->flags == EDGE_FALLTHRU)
4418 basic_block bb = single_pred (test_bb);
4419 basic_block target_bb;
4420 int max_insns = MAX_CONDITIONAL_EXECUTE;
4421 int n_insns;
4423 /* Determine if the preceding block is an && or || block. */
4424 if ((n_insns = block_jumps_and_fallthru_p (bb, else_bb)) >= 0)
4426 ce_info->and_and_p = TRUE;
4427 target_bb = else_bb;
4429 else if ((n_insns = block_jumps_and_fallthru_p (bb, then_bb)) >= 0)
4431 ce_info->and_and_p = FALSE;
4432 target_bb = then_bb;
4434 else
4435 target_bb = NULL_BLOCK;
4437 if (target_bb && n_insns <= max_insns)
4439 int total_insns = 0;
4440 int blocks = 0;
4442 ce_info->last_test_bb = test_bb;
4444 /* Found at least one && or || block, look for more. */
4447 ce_info->test_bb = test_bb = bb;
4448 total_insns += n_insns;
4449 blocks++;
4451 if (!single_pred_p (bb))
4452 break;
4454 bb = single_pred (bb);
4455 n_insns = block_jumps_and_fallthru_p (bb, target_bb);
4457 while (n_insns >= 0 && (total_insns + n_insns) <= max_insns);
4459 ce_info->num_multiple_test_blocks = blocks;
4460 ce_info->num_multiple_test_insns = total_insns;
4462 if (ce_info->and_and_p)
4463 ce_info->num_and_and_blocks = blocks;
4464 else
4465 ce_info->num_or_or_blocks = blocks;
4469 /* The THEN block of an IF-THEN combo must have exactly one predecessor,
4470 other than any || blocks which jump to the THEN block. */
4471 if ((EDGE_COUNT (then_bb->preds) - ce_info->num_or_or_blocks) != 1)
4472 return FALSE;
4474 /* The edges of the THEN and ELSE blocks cannot have complex edges. */
4475 FOR_EACH_EDGE (cur_edge, ei, then_bb->preds)
4477 if (cur_edge->flags & EDGE_COMPLEX)
4478 return FALSE;
4481 FOR_EACH_EDGE (cur_edge, ei, else_bb->preds)
4483 if (cur_edge->flags & EDGE_COMPLEX)
4484 return FALSE;
4487 /* The THEN block of an IF-THEN combo must have zero or one successors. */
4488 if (EDGE_COUNT (then_bb->succs) > 0
4489 && (!single_succ_p (then_bb)
4490 || (single_succ_edge (then_bb)->flags & EDGE_COMPLEX)
4491 || (epilogue_completed
4492 && tablejump_p (BB_END (then_bb), NULL, NULL))))
4493 return FALSE;
4495 /* If the THEN block has no successors, conditional execution can still
4496 make a conditional call. Don't do this unless the ELSE block has
4497 only one incoming edge -- the CFG manipulation is too ugly otherwise.
4498 Check for the last insn of the THEN block being an indirect jump, which
4499 is listed as not having any successors, but confuses the rest of the CE
4500 code processing. ??? we should fix this in the future. */
4501 if (EDGE_COUNT (then_bb->succs) == 0)
4503 if (single_pred_p (else_bb) && else_bb != EXIT_BLOCK_PTR_FOR_FN (cfun))
4505 rtx_insn *last_insn = BB_END (then_bb);
4507 while (last_insn
4508 && NOTE_P (last_insn)
4509 && last_insn != BB_HEAD (then_bb))
4510 last_insn = PREV_INSN (last_insn);
4512 if (last_insn
4513 && JUMP_P (last_insn)
4514 && ! simplejump_p (last_insn))
4515 return FALSE;
4517 join_bb = else_bb;
4518 else_bb = NULL_BLOCK;
4520 else
4521 return FALSE;
4524 /* If the THEN block's successor is the other edge out of the TEST block,
4525 then we have an IF-THEN combo without an ELSE. */
4526 else if (single_succ (then_bb) == else_bb)
4528 join_bb = else_bb;
4529 else_bb = NULL_BLOCK;
4532 /* If the THEN and ELSE block meet in a subsequent block, and the ELSE
4533 has exactly one predecessor and one successor, and the outgoing edge
4534 is not complex, then we have an IF-THEN-ELSE combo. */
4535 else if (single_succ_p (else_bb)
4536 && single_succ (then_bb) == single_succ (else_bb)
4537 && single_pred_p (else_bb)
4538 && !(single_succ_edge (else_bb)->flags & EDGE_COMPLEX)
4539 && !(epilogue_completed
4540 && tablejump_p (BB_END (else_bb), NULL, NULL)))
4541 join_bb = single_succ (else_bb);
4543 /* Otherwise it is not an IF-THEN or IF-THEN-ELSE combination. */
4544 else
4545 return FALSE;
4547 num_possible_if_blocks++;
4549 if (dump_file)
4551 fprintf (dump_file,
4552 "\nIF-THEN%s block found, pass %d, start block %d "
4553 "[insn %d], then %d [%d]",
4554 (else_bb) ? "-ELSE" : "",
4555 ce_info->pass,
4556 test_bb->index,
4557 BB_HEAD (test_bb) ? (int)INSN_UID (BB_HEAD (test_bb)) : -1,
4558 then_bb->index,
4559 BB_HEAD (then_bb) ? (int)INSN_UID (BB_HEAD (then_bb)) : -1);
4561 if (else_bb)
4562 fprintf (dump_file, ", else %d [%d]",
4563 else_bb->index,
4564 BB_HEAD (else_bb) ? (int)INSN_UID (BB_HEAD (else_bb)) : -1);
4566 fprintf (dump_file, ", join %d [%d]",
4567 join_bb->index,
4568 BB_HEAD (join_bb) ? (int)INSN_UID (BB_HEAD (join_bb)) : -1);
4570 if (ce_info->num_multiple_test_blocks > 0)
4571 fprintf (dump_file, ", %d %s block%s last test %d [%d]",
4572 ce_info->num_multiple_test_blocks,
4573 (ce_info->and_and_p) ? "&&" : "||",
4574 (ce_info->num_multiple_test_blocks == 1) ? "" : "s",
4575 ce_info->last_test_bb->index,
4576 ((BB_HEAD (ce_info->last_test_bb))
4577 ? (int)INSN_UID (BB_HEAD (ce_info->last_test_bb))
4578 : -1));
4580 fputc ('\n', dump_file);
4583 /* Make sure IF, THEN, and ELSE, blocks are adjacent. Actually, we get the
4584 first condition for free, since we've already asserted that there's a
4585 fallthru edge from IF to THEN. Likewise for the && and || blocks, since
4586 we checked the FALLTHRU flag, those are already adjacent to the last IF
4587 block. */
4588 /* ??? As an enhancement, move the ELSE block. Have to deal with
4589 BLOCK notes, if by no other means than backing out the merge if they
4590 exist. Sticky enough I don't want to think about it now. */
4591 next = then_bb;
4592 if (else_bb && (next = next->next_bb) != else_bb)
4593 return FALSE;
4594 if ((next = next->next_bb) != join_bb
4595 && join_bb != EXIT_BLOCK_PTR_FOR_FN (cfun))
4597 if (else_bb)
4598 join_bb = NULL;
4599 else
4600 return FALSE;
4603 /* Do the real work. */
4605 ce_info->else_bb = else_bb;
4606 ce_info->join_bb = join_bb;
4608 /* If we have && and || tests, try to first handle combining the && and ||
4609 tests into the conditional code, and if that fails, go back and handle
4610 it without the && and ||, which at present handles the && case if there
4611 was no ELSE block. */
4612 if (cond_exec_process_if_block (ce_info, TRUE))
4613 return TRUE;
4615 if (ce_info->num_multiple_test_blocks)
4617 cancel_changes (0);
4619 if (cond_exec_process_if_block (ce_info, FALSE))
4620 return TRUE;
4623 return FALSE;
4626 /* Convert a branch over a trap, or a branch
4627 to a trap, into a conditional trap. */
4629 static int
4630 find_cond_trap (basic_block test_bb, edge then_edge, edge else_edge)
4632 basic_block then_bb = then_edge->dest;
4633 basic_block else_bb = else_edge->dest;
4634 basic_block other_bb, trap_bb;
4635 rtx_insn *trap, *jump;
4636 rtx cond;
4637 rtx_insn *cond_earliest;
4638 enum rtx_code code;
4640 /* Locate the block with the trap instruction. */
4641 /* ??? While we look for no successors, we really ought to allow
4642 EH successors. Need to fix merge_if_block for that to work. */
4643 if ((trap = block_has_only_trap (then_bb)) != NULL)
4644 trap_bb = then_bb, other_bb = else_bb;
4645 else if ((trap = block_has_only_trap (else_bb)) != NULL)
4646 trap_bb = else_bb, other_bb = then_bb;
4647 else
4648 return FALSE;
4650 if (dump_file)
4652 fprintf (dump_file, "\nTRAP-IF block found, start %d, trap %d\n",
4653 test_bb->index, trap_bb->index);
4656 /* If this is not a standard conditional jump, we can't parse it. */
4657 jump = BB_END (test_bb);
4658 cond = noce_get_condition (jump, &cond_earliest, false);
4659 if (! cond)
4660 return FALSE;
4662 /* If the conditional jump is more than just a conditional jump, then
4663 we can not do if-conversion on this block. Give up for returnjump_p,
4664 changing a conditional return followed by unconditional trap for
4665 conditional trap followed by unconditional return is likely not
4666 beneficial and harder to handle. */
4667 if (! onlyjump_p (jump) || returnjump_p (jump))
4668 return FALSE;
4670 /* We must be comparing objects whose modes imply the size. */
4671 if (GET_MODE (XEXP (cond, 0)) == BLKmode)
4672 return FALSE;
4674 /* Reverse the comparison code, if necessary. */
4675 code = GET_CODE (cond);
4676 if (then_bb == trap_bb)
4678 code = reversed_comparison_code (cond, jump);
4679 if (code == UNKNOWN)
4680 return FALSE;
4683 /* Attempt to generate the conditional trap. */
4684 rtx_insn *seq = gen_cond_trap (code, copy_rtx (XEXP (cond, 0)),
4685 copy_rtx (XEXP (cond, 1)),
4686 TRAP_CODE (PATTERN (trap)));
4687 if (seq == NULL)
4688 return FALSE;
4690 /* Emit the new insns before cond_earliest. */
4691 emit_insn_before_setloc (seq, cond_earliest, INSN_LOCATION (trap));
4693 /* Delete the trap block if possible. */
4694 remove_edge (trap_bb == then_bb ? then_edge : else_edge);
4695 df_set_bb_dirty (test_bb);
4696 df_set_bb_dirty (then_bb);
4697 df_set_bb_dirty (else_bb);
4699 if (EDGE_COUNT (trap_bb->preds) == 0)
4701 delete_basic_block (trap_bb);
4702 num_true_changes++;
4705 /* Wire together the blocks again. */
4706 if (current_ir_type () == IR_RTL_CFGLAYOUT)
4707 single_succ_edge (test_bb)->flags |= EDGE_FALLTHRU;
4708 else if (trap_bb == then_bb)
4710 rtx lab = JUMP_LABEL (jump);
4711 rtx_insn *seq = targetm.gen_jump (lab);
4712 rtx_jump_insn *newjump = emit_jump_insn_after (seq, jump);
4713 LABEL_NUSES (lab) += 1;
4714 JUMP_LABEL (newjump) = lab;
4715 emit_barrier_after (newjump);
4717 delete_insn (jump);
4719 if (can_merge_blocks_p (test_bb, other_bb))
4721 merge_blocks (test_bb, other_bb);
4722 num_true_changes++;
4725 num_updated_if_blocks++;
4726 return TRUE;
4729 /* Subroutine of find_cond_trap: if BB contains only a trap insn,
4730 return it. */
4732 static rtx_insn *
4733 block_has_only_trap (basic_block bb)
4735 rtx_insn *trap;
4737 /* We're not the exit block. */
4738 if (bb == EXIT_BLOCK_PTR_FOR_FN (cfun))
4739 return NULL;
4741 /* The block must have no successors. */
4742 if (EDGE_COUNT (bb->succs) > 0)
4743 return NULL;
4745 /* The only instruction in the THEN block must be the trap. */
4746 trap = first_active_insn (bb);
4747 if (! (trap == BB_END (bb)
4748 && GET_CODE (PATTERN (trap)) == TRAP_IF
4749 && TRAP_CONDITION (PATTERN (trap)) == const_true_rtx))
4750 return NULL;
4752 return trap;
4755 /* Look for IF-THEN-ELSE cases in which one of THEN or ELSE is
4756 transformable, but not necessarily the other. There need be no
4757 JOIN block.
4759 Return TRUE if we were successful at converting the block.
4761 Cases we'd like to look at:
4764 if (test) goto over; // x not live
4765 x = a;
4766 goto label;
4767 over:
4769 becomes
4771 x = a;
4772 if (! test) goto label;
4775 if (test) goto E; // x not live
4776 x = big();
4777 goto L;
4779 x = b;
4780 goto M;
4782 becomes
4784 x = b;
4785 if (test) goto M;
4786 x = big();
4787 goto L;
4789 (3) // This one's really only interesting for targets that can do
4790 // multiway branching, e.g. IA-64 BBB bundles. For other targets
4791 // it results in multiple branches on a cache line, which often
4792 // does not sit well with predictors.
4794 if (test1) goto E; // predicted not taken
4795 x = a;
4796 if (test2) goto F;
4799 x = b;
4802 becomes
4804 x = a;
4805 if (test1) goto E;
4806 if (test2) goto F;
4808 Notes:
4810 (A) Don't do (2) if the branch is predicted against the block we're
4811 eliminating. Do it anyway if we can eliminate a branch; this requires
4812 that the sole successor of the eliminated block postdominate the other
4813 side of the if.
4815 (B) With CE, on (3) we can steal from both sides of the if, creating
4817 if (test1) x = a;
4818 if (!test1) x = b;
4819 if (test1) goto J;
4820 if (test2) goto F;
4824 Again, this is most useful if J postdominates.
4826 (C) CE substitutes for helpful life information.
4828 (D) These heuristics need a lot of work. */
4830 /* Tests for case 1 above. */
4832 static int
4833 find_if_case_1 (basic_block test_bb, edge then_edge, edge else_edge)
4835 basic_block then_bb = then_edge->dest;
4836 basic_block else_bb = else_edge->dest;
4837 basic_block new_bb;
4838 int then_bb_index, then_prob;
4839 rtx else_target = NULL_RTX;
4841 /* If we are partitioning hot/cold basic blocks, we don't want to
4842 mess up unconditional or indirect jumps that cross between hot
4843 and cold sections.
4845 Basic block partitioning may result in some jumps that appear to
4846 be optimizable (or blocks that appear to be mergeable), but which really
4847 must be left untouched (they are required to make it safely across
4848 partition boundaries). See the comments at the top of
4849 bb-reorder.c:partition_hot_cold_basic_blocks for complete details. */
4851 if ((BB_END (then_bb)
4852 && JUMP_P (BB_END (then_bb))
4853 && CROSSING_JUMP_P (BB_END (then_bb)))
4854 || (BB_END (test_bb)
4855 && JUMP_P (BB_END (test_bb))
4856 && CROSSING_JUMP_P (BB_END (test_bb)))
4857 || (BB_END (else_bb)
4858 && JUMP_P (BB_END (else_bb))
4859 && CROSSING_JUMP_P (BB_END (else_bb))))
4860 return FALSE;
4862 /* THEN has one successor. */
4863 if (!single_succ_p (then_bb))
4864 return FALSE;
4866 /* THEN does not fall through, but is not strange either. */
4867 if (single_succ_edge (then_bb)->flags & (EDGE_COMPLEX | EDGE_FALLTHRU))
4868 return FALSE;
4870 /* THEN has one predecessor. */
4871 if (!single_pred_p (then_bb))
4872 return FALSE;
4874 /* THEN must do something. */
4875 if (forwarder_block_p (then_bb))
4876 return FALSE;
4878 num_possible_if_blocks++;
4879 if (dump_file)
4880 fprintf (dump_file,
4881 "\nIF-CASE-1 found, start %d, then %d\n",
4882 test_bb->index, then_bb->index);
4884 if (then_edge->probability)
4885 then_prob = REG_BR_PROB_BASE - then_edge->probability;
4886 else
4887 then_prob = REG_BR_PROB_BASE / 2;
4889 /* We're speculating from the THEN path, we want to make sure the cost
4890 of speculation is within reason. */
4891 if (! cheap_bb_rtx_cost_p (then_bb, then_prob,
4892 COSTS_N_INSNS (BRANCH_COST (optimize_bb_for_speed_p (then_edge->src),
4893 predictable_edge_p (then_edge)))))
4894 return FALSE;
4896 if (else_bb == EXIT_BLOCK_PTR_FOR_FN (cfun))
4898 rtx_insn *jump = BB_END (else_edge->src);
4899 gcc_assert (JUMP_P (jump));
4900 else_target = JUMP_LABEL (jump);
4903 /* Registers set are dead, or are predicable. */
4904 if (! dead_or_predicable (test_bb, then_bb, else_bb,
4905 single_succ_edge (then_bb), 1))
4906 return FALSE;
4908 /* Conversion went ok, including moving the insns and fixing up the
4909 jump. Adjust the CFG to match. */
4911 /* We can avoid creating a new basic block if then_bb is immediately
4912 followed by else_bb, i.e. deleting then_bb allows test_bb to fall
4913 through to else_bb. */
4915 if (then_bb->next_bb == else_bb
4916 && then_bb->prev_bb == test_bb
4917 && else_bb != EXIT_BLOCK_PTR_FOR_FN (cfun))
4919 redirect_edge_succ (FALLTHRU_EDGE (test_bb), else_bb);
4920 new_bb = 0;
4922 else if (else_bb == EXIT_BLOCK_PTR_FOR_FN (cfun))
4923 new_bb = force_nonfallthru_and_redirect (FALLTHRU_EDGE (test_bb),
4924 else_bb, else_target);
4925 else
4926 new_bb = redirect_edge_and_branch_force (FALLTHRU_EDGE (test_bb),
4927 else_bb);
4929 df_set_bb_dirty (test_bb);
4930 df_set_bb_dirty (else_bb);
4932 then_bb_index = then_bb->index;
4933 delete_basic_block (then_bb);
4935 /* Make rest of code believe that the newly created block is the THEN_BB
4936 block we removed. */
4937 if (new_bb)
4939 df_bb_replace (then_bb_index, new_bb);
4940 /* This should have been done above via force_nonfallthru_and_redirect
4941 (possibly called from redirect_edge_and_branch_force). */
4942 gcc_checking_assert (BB_PARTITION (new_bb) == BB_PARTITION (test_bb));
4945 num_true_changes++;
4946 num_updated_if_blocks++;
4947 return TRUE;
4950 /* Test for case 2 above. */
4952 static int
4953 find_if_case_2 (basic_block test_bb, edge then_edge, edge else_edge)
4955 basic_block then_bb = then_edge->dest;
4956 basic_block else_bb = else_edge->dest;
4957 edge else_succ;
4958 int then_prob, else_prob;
4960 /* We do not want to speculate (empty) loop latches. */
4961 if (current_loops
4962 && else_bb->loop_father->latch == else_bb)
4963 return FALSE;
4965 /* If we are partitioning hot/cold basic blocks, we don't want to
4966 mess up unconditional or indirect jumps that cross between hot
4967 and cold sections.
4969 Basic block partitioning may result in some jumps that appear to
4970 be optimizable (or blocks that appear to be mergeable), but which really
4971 must be left untouched (they are required to make it safely across
4972 partition boundaries). See the comments at the top of
4973 bb-reorder.c:partition_hot_cold_basic_blocks for complete details. */
4975 if ((BB_END (then_bb)
4976 && JUMP_P (BB_END (then_bb))
4977 && CROSSING_JUMP_P (BB_END (then_bb)))
4978 || (BB_END (test_bb)
4979 && JUMP_P (BB_END (test_bb))
4980 && CROSSING_JUMP_P (BB_END (test_bb)))
4981 || (BB_END (else_bb)
4982 && JUMP_P (BB_END (else_bb))
4983 && CROSSING_JUMP_P (BB_END (else_bb))))
4984 return FALSE;
4986 /* ELSE has one successor. */
4987 if (!single_succ_p (else_bb))
4988 return FALSE;
4989 else
4990 else_succ = single_succ_edge (else_bb);
4992 /* ELSE outgoing edge is not complex. */
4993 if (else_succ->flags & EDGE_COMPLEX)
4994 return FALSE;
4996 /* ELSE has one predecessor. */
4997 if (!single_pred_p (else_bb))
4998 return FALSE;
5000 /* THEN is not EXIT. */
5001 if (then_bb->index < NUM_FIXED_BLOCKS)
5002 return FALSE;
5004 if (else_edge->probability)
5006 else_prob = else_edge->probability;
5007 then_prob = REG_BR_PROB_BASE - else_prob;
5009 else
5011 else_prob = REG_BR_PROB_BASE / 2;
5012 then_prob = REG_BR_PROB_BASE / 2;
5015 /* ELSE is predicted or SUCC(ELSE) postdominates THEN. */
5016 if (else_prob > then_prob)
5018 else if (else_succ->dest->index < NUM_FIXED_BLOCKS
5019 || dominated_by_p (CDI_POST_DOMINATORS, then_bb,
5020 else_succ->dest))
5022 else
5023 return FALSE;
5025 num_possible_if_blocks++;
5026 if (dump_file)
5027 fprintf (dump_file,
5028 "\nIF-CASE-2 found, start %d, else %d\n",
5029 test_bb->index, else_bb->index);
5031 /* We're speculating from the ELSE path, we want to make sure the cost
5032 of speculation is within reason. */
5033 if (! cheap_bb_rtx_cost_p (else_bb, else_prob,
5034 COSTS_N_INSNS (BRANCH_COST (optimize_bb_for_speed_p (else_edge->src),
5035 predictable_edge_p (else_edge)))))
5036 return FALSE;
5038 /* Registers set are dead, or are predicable. */
5039 if (! dead_or_predicable (test_bb, else_bb, then_bb, else_succ, 0))
5040 return FALSE;
5042 /* Conversion went ok, including moving the insns and fixing up the
5043 jump. Adjust the CFG to match. */
5045 df_set_bb_dirty (test_bb);
5046 df_set_bb_dirty (then_bb);
5047 delete_basic_block (else_bb);
5049 num_true_changes++;
5050 num_updated_if_blocks++;
5052 /* ??? We may now fallthru from one of THEN's successors into a join
5053 block. Rerun cleanup_cfg? Examine things manually? Wait? */
5055 return TRUE;
5058 /* Used by the code above to perform the actual rtl transformations.
5059 Return TRUE if successful.
5061 TEST_BB is the block containing the conditional branch. MERGE_BB
5062 is the block containing the code to manipulate. DEST_EDGE is an
5063 edge representing a jump to the join block; after the conversion,
5064 TEST_BB should be branching to its destination.
5065 REVERSEP is true if the sense of the branch should be reversed. */
5067 static int
5068 dead_or_predicable (basic_block test_bb, basic_block merge_bb,
5069 basic_block other_bb, edge dest_edge, int reversep)
5071 basic_block new_dest = dest_edge->dest;
5072 rtx_insn *head, *end, *jump;
5073 rtx_insn *earliest = NULL;
5074 rtx old_dest;
5075 bitmap merge_set = NULL;
5076 /* Number of pending changes. */
5077 int n_validated_changes = 0;
5078 rtx new_dest_label = NULL_RTX;
5080 jump = BB_END (test_bb);
5082 /* Find the extent of the real code in the merge block. */
5083 head = BB_HEAD (merge_bb);
5084 end = BB_END (merge_bb);
5086 while (DEBUG_INSN_P (end) && end != head)
5087 end = PREV_INSN (end);
5089 /* If merge_bb ends with a tablejump, predicating/moving insn's
5090 into test_bb and then deleting merge_bb will result in the jumptable
5091 that follows merge_bb being removed along with merge_bb and then we
5092 get an unresolved reference to the jumptable. */
5093 if (tablejump_p (end, NULL, NULL))
5094 return FALSE;
5096 if (LABEL_P (head))
5097 head = NEXT_INSN (head);
5098 while (DEBUG_INSN_P (head) && head != end)
5099 head = NEXT_INSN (head);
5100 if (NOTE_P (head))
5102 if (head == end)
5104 head = end = NULL;
5105 goto no_body;
5107 head = NEXT_INSN (head);
5108 while (DEBUG_INSN_P (head) && head != end)
5109 head = NEXT_INSN (head);
5112 if (JUMP_P (end))
5114 if (!onlyjump_p (end))
5115 return FALSE;
5116 if (head == end)
5118 head = end = NULL;
5119 goto no_body;
5121 end = PREV_INSN (end);
5122 while (DEBUG_INSN_P (end) && end != head)
5123 end = PREV_INSN (end);
5126 /* Don't move frame-related insn across the conditional branch. This
5127 can lead to one of the paths of the branch having wrong unwind info. */
5128 if (epilogue_completed)
5130 rtx_insn *insn = head;
5131 while (1)
5133 if (INSN_P (insn) && RTX_FRAME_RELATED_P (insn))
5134 return FALSE;
5135 if (insn == end)
5136 break;
5137 insn = NEXT_INSN (insn);
5141 /* Disable handling dead code by conditional execution if the machine needs
5142 to do anything funny with the tests, etc. */
5143 #ifndef IFCVT_MODIFY_TESTS
5144 if (targetm.have_conditional_execution ())
5146 /* In the conditional execution case, we have things easy. We know
5147 the condition is reversible. We don't have to check life info
5148 because we're going to conditionally execute the code anyway.
5149 All that's left is making sure the insns involved can actually
5150 be predicated. */
5152 rtx cond;
5154 cond = cond_exec_get_condition (jump);
5155 if (! cond)
5156 return FALSE;
5158 rtx note = find_reg_note (jump, REG_BR_PROB, NULL_RTX);
5159 int prob_val = (note ? XINT (note, 0) : -1);
5161 if (reversep)
5163 enum rtx_code rev = reversed_comparison_code (cond, jump);
5164 if (rev == UNKNOWN)
5165 return FALSE;
5166 cond = gen_rtx_fmt_ee (rev, GET_MODE (cond), XEXP (cond, 0),
5167 XEXP (cond, 1));
5168 if (prob_val >= 0)
5169 prob_val = REG_BR_PROB_BASE - prob_val;
5172 if (cond_exec_process_insns (NULL, head, end, cond, prob_val, 0)
5173 && verify_changes (0))
5174 n_validated_changes = num_validated_changes ();
5175 else
5176 cancel_changes (0);
5178 earliest = jump;
5180 #endif
5182 /* If we allocated new pseudos (e.g. in the conditional move
5183 expander called from noce_emit_cmove), we must resize the
5184 array first. */
5185 if (max_regno < max_reg_num ())
5186 max_regno = max_reg_num ();
5188 /* Try the NCE path if the CE path did not result in any changes. */
5189 if (n_validated_changes == 0)
5191 rtx cond;
5192 rtx_insn *insn;
5193 regset live;
5194 bool success;
5196 /* In the non-conditional execution case, we have to verify that there
5197 are no trapping operations, no calls, no references to memory, and
5198 that any registers modified are dead at the branch site. */
5200 if (!any_condjump_p (jump))
5201 return FALSE;
5203 /* Find the extent of the conditional. */
5204 cond = noce_get_condition (jump, &earliest, false);
5205 if (!cond)
5206 return FALSE;
5208 live = BITMAP_ALLOC (&reg_obstack);
5209 simulate_backwards_to_point (merge_bb, live, end);
5210 success = can_move_insns_across (head, end, earliest, jump,
5211 merge_bb, live,
5212 df_get_live_in (other_bb), NULL);
5213 BITMAP_FREE (live);
5214 if (!success)
5215 return FALSE;
5217 /* Collect the set of registers set in MERGE_BB. */
5218 merge_set = BITMAP_ALLOC (&reg_obstack);
5220 FOR_BB_INSNS (merge_bb, insn)
5221 if (NONDEBUG_INSN_P (insn))
5222 df_simulate_find_defs (insn, merge_set);
5224 /* If shrink-wrapping, disable this optimization when test_bb is
5225 the first basic block and merge_bb exits. The idea is to not
5226 move code setting up a return register as that may clobber a
5227 register used to pass function parameters, which then must be
5228 saved in caller-saved regs. A caller-saved reg requires the
5229 prologue, killing a shrink-wrap opportunity. */
5230 if ((SHRINK_WRAPPING_ENABLED && !epilogue_completed)
5231 && ENTRY_BLOCK_PTR_FOR_FN (cfun)->next_bb == test_bb
5232 && single_succ_p (new_dest)
5233 && single_succ (new_dest) == EXIT_BLOCK_PTR_FOR_FN (cfun)
5234 && bitmap_intersect_p (df_get_live_in (new_dest), merge_set))
5236 regset return_regs;
5237 unsigned int i;
5239 return_regs = BITMAP_ALLOC (&reg_obstack);
5241 /* Start off with the intersection of regs used to pass
5242 params and regs used to return values. */
5243 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
5244 if (FUNCTION_ARG_REGNO_P (i)
5245 && targetm.calls.function_value_regno_p (i))
5246 bitmap_set_bit (return_regs, INCOMING_REGNO (i));
5248 bitmap_and_into (return_regs,
5249 df_get_live_out (ENTRY_BLOCK_PTR_FOR_FN (cfun)));
5250 bitmap_and_into (return_regs,
5251 df_get_live_in (EXIT_BLOCK_PTR_FOR_FN (cfun)));
5252 if (!bitmap_empty_p (return_regs))
5254 FOR_BB_INSNS_REVERSE (new_dest, insn)
5255 if (NONDEBUG_INSN_P (insn))
5257 df_ref def;
5259 /* If this insn sets any reg in return_regs, add all
5260 reg uses to the set of regs we're interested in. */
5261 FOR_EACH_INSN_DEF (def, insn)
5262 if (bitmap_bit_p (return_regs, DF_REF_REGNO (def)))
5264 df_simulate_uses (insn, return_regs);
5265 break;
5268 if (bitmap_intersect_p (merge_set, return_regs))
5270 BITMAP_FREE (return_regs);
5271 BITMAP_FREE (merge_set);
5272 return FALSE;
5275 BITMAP_FREE (return_regs);
5279 no_body:
5280 /* We don't want to use normal invert_jump or redirect_jump because
5281 we don't want to delete_insn called. Also, we want to do our own
5282 change group management. */
5284 old_dest = JUMP_LABEL (jump);
5285 if (other_bb != new_dest)
5287 if (!any_condjump_p (jump))
5288 goto cancel;
5290 if (JUMP_P (BB_END (dest_edge->src)))
5291 new_dest_label = JUMP_LABEL (BB_END (dest_edge->src));
5292 else if (new_dest == EXIT_BLOCK_PTR_FOR_FN (cfun))
5293 new_dest_label = ret_rtx;
5294 else
5295 new_dest_label = block_label (new_dest);
5297 rtx_jump_insn *jump_insn = as_a <rtx_jump_insn *> (jump);
5298 if (reversep
5299 ? ! invert_jump_1 (jump_insn, new_dest_label)
5300 : ! redirect_jump_1 (jump_insn, new_dest_label))
5301 goto cancel;
5304 if (verify_changes (n_validated_changes))
5305 confirm_change_group ();
5306 else
5307 goto cancel;
5309 if (other_bb != new_dest)
5311 redirect_jump_2 (as_a <rtx_jump_insn *> (jump), old_dest, new_dest_label,
5312 0, reversep);
5314 redirect_edge_succ (BRANCH_EDGE (test_bb), new_dest);
5315 if (reversep)
5317 std::swap (BRANCH_EDGE (test_bb)->count,
5318 FALLTHRU_EDGE (test_bb)->count);
5319 std::swap (BRANCH_EDGE (test_bb)->probability,
5320 FALLTHRU_EDGE (test_bb)->probability);
5321 update_br_prob_note (test_bb);
5325 /* Move the insns out of MERGE_BB to before the branch. */
5326 if (head != NULL)
5328 rtx_insn *insn;
5330 if (end == BB_END (merge_bb))
5331 BB_END (merge_bb) = PREV_INSN (head);
5333 /* PR 21767: when moving insns above a conditional branch, the REG_EQUAL
5334 notes being moved might become invalid. */
5335 insn = head;
5338 rtx note;
5340 if (! INSN_P (insn))
5341 continue;
5342 note = find_reg_note (insn, REG_EQUAL, NULL_RTX);
5343 if (! note)
5344 continue;
5345 remove_note (insn, note);
5346 } while (insn != end && (insn = NEXT_INSN (insn)));
5348 /* PR46315: when moving insns above a conditional branch, the REG_EQUAL
5349 notes referring to the registers being set might become invalid. */
5350 if (merge_set)
5352 unsigned i;
5353 bitmap_iterator bi;
5355 EXECUTE_IF_SET_IN_BITMAP (merge_set, 0, i, bi)
5356 remove_reg_equal_equiv_notes_for_regno (i);
5358 BITMAP_FREE (merge_set);
5361 reorder_insns (head, end, PREV_INSN (earliest));
5364 /* Remove the jump and edge if we can. */
5365 if (other_bb == new_dest)
5367 delete_insn (jump);
5368 remove_edge (BRANCH_EDGE (test_bb));
5369 /* ??? Can't merge blocks here, as then_bb is still in use.
5370 At minimum, the merge will get done just before bb-reorder. */
5373 return TRUE;
5375 cancel:
5376 cancel_changes (0);
5378 if (merge_set)
5379 BITMAP_FREE (merge_set);
5381 return FALSE;
5384 /* Main entry point for all if-conversion. AFTER_COMBINE is true if
5385 we are after combine pass. */
5387 static void
5388 if_convert (bool after_combine)
5390 basic_block bb;
5391 int pass;
5393 if (optimize == 1)
5395 df_live_add_problem ();
5396 df_live_set_all_dirty ();
5399 /* Record whether we are after combine pass. */
5400 ifcvt_after_combine = after_combine;
5401 have_cbranchcc4 = (direct_optab_handler (cbranch_optab, CCmode)
5402 != CODE_FOR_nothing);
5403 num_possible_if_blocks = 0;
5404 num_updated_if_blocks = 0;
5405 num_true_changes = 0;
5407 loop_optimizer_init (AVOID_CFG_MODIFICATIONS);
5408 mark_loop_exit_edges ();
5409 loop_optimizer_finalize ();
5410 free_dominance_info (CDI_DOMINATORS);
5412 /* Compute postdominators. */
5413 calculate_dominance_info (CDI_POST_DOMINATORS);
5415 df_set_flags (DF_LR_RUN_DCE);
5417 /* Go through each of the basic blocks looking for things to convert. If we
5418 have conditional execution, we make multiple passes to allow us to handle
5419 IF-THEN{-ELSE} blocks within other IF-THEN{-ELSE} blocks. */
5420 pass = 0;
5423 df_analyze ();
5424 /* Only need to do dce on the first pass. */
5425 df_clear_flags (DF_LR_RUN_DCE);
5426 cond_exec_changed_p = FALSE;
5427 pass++;
5429 #ifdef IFCVT_MULTIPLE_DUMPS
5430 if (dump_file && pass > 1)
5431 fprintf (dump_file, "\n\n========== Pass %d ==========\n", pass);
5432 #endif
5434 FOR_EACH_BB_FN (bb, cfun)
5436 basic_block new_bb;
5437 while (!df_get_bb_dirty (bb)
5438 && (new_bb = find_if_header (bb, pass)) != NULL)
5439 bb = new_bb;
5442 #ifdef IFCVT_MULTIPLE_DUMPS
5443 if (dump_file && cond_exec_changed_p)
5444 print_rtl_with_bb (dump_file, get_insns (), dump_flags);
5445 #endif
5447 while (cond_exec_changed_p);
5449 #ifdef IFCVT_MULTIPLE_DUMPS
5450 if (dump_file)
5451 fprintf (dump_file, "\n\n========== no more changes\n");
5452 #endif
5454 free_dominance_info (CDI_POST_DOMINATORS);
5456 if (dump_file)
5457 fflush (dump_file);
5459 clear_aux_for_blocks ();
5461 /* If we allocated new pseudos, we must resize the array for sched1. */
5462 if (max_regno < max_reg_num ())
5463 max_regno = max_reg_num ();
5465 /* Write the final stats. */
5466 if (dump_file && num_possible_if_blocks > 0)
5468 fprintf (dump_file,
5469 "\n%d possible IF blocks searched.\n",
5470 num_possible_if_blocks);
5471 fprintf (dump_file,
5472 "%d IF blocks converted.\n",
5473 num_updated_if_blocks);
5474 fprintf (dump_file,
5475 "%d true changes made.\n\n\n",
5476 num_true_changes);
5479 if (optimize == 1)
5480 df_remove_problem (df_live);
5482 checking_verify_flow_info ();
5485 /* If-conversion and CFG cleanup. */
5486 static unsigned int
5487 rest_of_handle_if_conversion (void)
5489 if (flag_if_conversion)
5491 if (dump_file)
5493 dump_reg_info (dump_file);
5494 dump_flow_info (dump_file, dump_flags);
5496 cleanup_cfg (CLEANUP_EXPENSIVE);
5497 if_convert (false);
5500 cleanup_cfg (0);
5501 return 0;
5504 namespace {
5506 const pass_data pass_data_rtl_ifcvt =
5508 RTL_PASS, /* type */
5509 "ce1", /* name */
5510 OPTGROUP_NONE, /* optinfo_flags */
5511 TV_IFCVT, /* tv_id */
5512 0, /* properties_required */
5513 0, /* properties_provided */
5514 0, /* properties_destroyed */
5515 0, /* todo_flags_start */
5516 TODO_df_finish, /* todo_flags_finish */
5519 class pass_rtl_ifcvt : public rtl_opt_pass
5521 public:
5522 pass_rtl_ifcvt (gcc::context *ctxt)
5523 : rtl_opt_pass (pass_data_rtl_ifcvt, ctxt)
5526 /* opt_pass methods: */
5527 virtual bool gate (function *)
5529 return (optimize > 0) && dbg_cnt (if_conversion);
5532 virtual unsigned int execute (function *)
5534 return rest_of_handle_if_conversion ();
5537 }; // class pass_rtl_ifcvt
5539 } // anon namespace
5541 rtl_opt_pass *
5542 make_pass_rtl_ifcvt (gcc::context *ctxt)
5544 return new pass_rtl_ifcvt (ctxt);
5548 /* Rerun if-conversion, as combine may have simplified things enough
5549 to now meet sequence length restrictions. */
5551 namespace {
5553 const pass_data pass_data_if_after_combine =
5555 RTL_PASS, /* type */
5556 "ce2", /* name */
5557 OPTGROUP_NONE, /* optinfo_flags */
5558 TV_IFCVT, /* tv_id */
5559 0, /* properties_required */
5560 0, /* properties_provided */
5561 0, /* properties_destroyed */
5562 0, /* todo_flags_start */
5563 TODO_df_finish, /* todo_flags_finish */
5566 class pass_if_after_combine : public rtl_opt_pass
5568 public:
5569 pass_if_after_combine (gcc::context *ctxt)
5570 : rtl_opt_pass (pass_data_if_after_combine, ctxt)
5573 /* opt_pass methods: */
5574 virtual bool gate (function *)
5576 return optimize > 0 && flag_if_conversion
5577 && dbg_cnt (if_after_combine);
5580 virtual unsigned int execute (function *)
5582 if_convert (true);
5583 return 0;
5586 }; // class pass_if_after_combine
5588 } // anon namespace
5590 rtl_opt_pass *
5591 make_pass_if_after_combine (gcc::context *ctxt)
5593 return new pass_if_after_combine (ctxt);
5597 namespace {
5599 const pass_data pass_data_if_after_reload =
5601 RTL_PASS, /* type */
5602 "ce3", /* name */
5603 OPTGROUP_NONE, /* optinfo_flags */
5604 TV_IFCVT2, /* tv_id */
5605 0, /* properties_required */
5606 0, /* properties_provided */
5607 0, /* properties_destroyed */
5608 0, /* todo_flags_start */
5609 TODO_df_finish, /* todo_flags_finish */
5612 class pass_if_after_reload : public rtl_opt_pass
5614 public:
5615 pass_if_after_reload (gcc::context *ctxt)
5616 : rtl_opt_pass (pass_data_if_after_reload, ctxt)
5619 /* opt_pass methods: */
5620 virtual bool gate (function *)
5622 return optimize > 0 && flag_if_conversion2
5623 && dbg_cnt (if_after_reload);
5626 virtual unsigned int execute (function *)
5628 if_convert (true);
5629 return 0;
5632 }; // class pass_if_after_reload
5634 } // anon namespace
5636 rtl_opt_pass *
5637 make_pass_if_after_reload (gcc::context *ctxt)
5639 return new pass_if_after_reload (ctxt);