Various condition-handling calls
[gcc.git] / gcc / ifcvt.c
1 /* If-conversion support.
2 Copyright (C) 2000-2014 Free Software Foundation, Inc.
3
4 This file is part of GCC.
5
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.
10
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.
15
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/>. */
19
20 #include "config.h"
21 #include "system.h"
22 #include "coretypes.h"
23 #include "tm.h"
24
25 #include "rtl.h"
26 #include "regs.h"
27 #include "function.h"
28 #include "flags.h"
29 #include "insn-config.h"
30 #include "recog.h"
31 #include "except.h"
32 #include "hard-reg-set.h"
33 #include "basic-block.h"
34 #include "expr.h"
35 #include "output.h"
36 #include "optabs.h"
37 #include "diagnostic-core.h"
38 #include "tm_p.h"
39 #include "cfgloop.h"
40 #include "target.h"
41 #include "tree-pass.h"
42 #include "df.h"
43 #include "vec.h"
44 #include "dbgcnt.h"
45
46 #ifndef HAVE_conditional_move
47 #define HAVE_conditional_move 0
48 #endif
49 #ifndef HAVE_incscc
50 #define HAVE_incscc 0
51 #endif
52 #ifndef HAVE_decscc
53 #define HAVE_decscc 0
54 #endif
55 #ifndef HAVE_trap
56 #define HAVE_trap 0
57 #endif
58
59 #ifndef MAX_CONDITIONAL_EXECUTE
60 #define MAX_CONDITIONAL_EXECUTE \
61 (BRANCH_COST (optimize_function_for_speed_p (cfun), false) \
62 + 1)
63 #endif
64
65 #define IFCVT_MULTIPLE_DUMPS 1
66
67 #define NULL_BLOCK ((basic_block) NULL)
68
69 /* True if after combine pass. */
70 static bool ifcvt_after_combine;
71
72 /* # of IF-THEN or IF-THEN-ELSE blocks we looked at */
73 static int num_possible_if_blocks;
74
75 /* # of IF-THEN or IF-THEN-ELSE blocks were converted to conditional
76 execution. */
77 static int num_updated_if_blocks;
78
79 /* # of changes made. */
80 static int num_true_changes;
81
82 /* Whether conditional execution changes were made. */
83 static int cond_exec_changed_p;
84
85 /* Forward references. */
86 static int count_bb_insns (const_basic_block);
87 static bool cheap_bb_rtx_cost_p (const_basic_block, int, int);
88 static rtx_insn *first_active_insn (basic_block);
89 static rtx_insn *last_active_insn (basic_block, int);
90 static rtx find_active_insn_before (basic_block, rtx);
91 static rtx find_active_insn_after (basic_block, rtx);
92 static basic_block block_fallthru (basic_block);
93 static int cond_exec_process_insns (ce_if_block *, rtx, rtx, rtx, int, int);
94 static rtx cond_exec_get_condition (rtx);
95 static rtx noce_get_condition (rtx_insn *, rtx_insn **, bool);
96 static int noce_operand_ok (const_rtx);
97 static void merge_if_block (ce_if_block *);
98 static int find_cond_trap (basic_block, edge, edge);
99 static basic_block find_if_header (basic_block, int);
100 static int block_jumps_and_fallthru_p (basic_block, basic_block);
101 static int noce_find_if_block (basic_block, edge, edge, int);
102 static int cond_exec_find_if_block (ce_if_block *);
103 static int find_if_case_1 (basic_block, edge, edge);
104 static int find_if_case_2 (basic_block, edge, edge);
105 static int dead_or_predicable (basic_block, basic_block, basic_block,
106 edge, int);
107 static void noce_emit_move_insn (rtx, rtx);
108 static rtx_insn *block_has_only_trap (basic_block);
109 \f
110 /* Count the number of non-jump active insns in BB. */
111
112 static int
113 count_bb_insns (const_basic_block bb)
114 {
115 int count = 0;
116 rtx_insn *insn = BB_HEAD (bb);
117
118 while (1)
119 {
120 if (active_insn_p (insn) && !JUMP_P (insn))
121 count++;
122
123 if (insn == BB_END (bb))
124 break;
125 insn = NEXT_INSN (insn);
126 }
127
128 return count;
129 }
130
131 /* Determine whether the total insn_rtx_cost on non-jump insns in
132 basic block BB is less than MAX_COST. This function returns
133 false if the cost of any instruction could not be estimated.
134
135 The cost of the non-jump insns in BB is scaled by REG_BR_PROB_BASE
136 as those insns are being speculated. MAX_COST is scaled with SCALE
137 plus a small fudge factor. */
138
139 static bool
140 cheap_bb_rtx_cost_p (const_basic_block bb, int scale, int max_cost)
141 {
142 int count = 0;
143 rtx_insn *insn = BB_HEAD (bb);
144 bool speed = optimize_bb_for_speed_p (bb);
145
146 /* Set scale to REG_BR_PROB_BASE to void the identical scaling
147 applied to insn_rtx_cost when optimizing for size. Only do
148 this after combine because if-conversion might interfere with
149 passes before combine.
150
151 Use optimize_function_for_speed_p instead of the pre-defined
152 variable speed to make sure it is set to same value for all
153 basic blocks in one if-conversion transformation. */
154 if (!optimize_function_for_speed_p (cfun) && ifcvt_after_combine)
155 scale = REG_BR_PROB_BASE;
156 /* Our branch probability/scaling factors are just estimates and don't
157 account for cases where we can get speculation for free and other
158 secondary benefits. So we fudge the scale factor to make speculating
159 appear a little more profitable when optimizing for performance. */
160 else
161 scale += REG_BR_PROB_BASE / 8;
162
163
164 max_cost *= scale;
165
166 while (1)
167 {
168 if (NONJUMP_INSN_P (insn))
169 {
170 int cost = insn_rtx_cost (PATTERN (insn), speed) * REG_BR_PROB_BASE;
171 if (cost == 0)
172 return false;
173
174 /* If this instruction is the load or set of a "stack" register,
175 such as a floating point register on x87, then the cost of
176 speculatively executing this insn may need to include
177 the additional cost of popping its result off of the
178 register stack. Unfortunately, correctly recognizing and
179 accounting for this additional overhead is tricky, so for
180 now we simply prohibit such speculative execution. */
181 #ifdef STACK_REGS
182 {
183 rtx set = single_set (insn);
184 if (set && STACK_REG_P (SET_DEST (set)))
185 return false;
186 }
187 #endif
188
189 count += cost;
190 if (count >= max_cost)
191 return false;
192 }
193 else if (CALL_P (insn))
194 return false;
195
196 if (insn == BB_END (bb))
197 break;
198 insn = NEXT_INSN (insn);
199 }
200
201 return true;
202 }
203
204 /* Return the first non-jump active insn in the basic block. */
205
206 static rtx_insn *
207 first_active_insn (basic_block bb)
208 {
209 rtx_insn *insn = BB_HEAD (bb);
210
211 if (LABEL_P (insn))
212 {
213 if (insn == BB_END (bb))
214 return NULL;
215 insn = NEXT_INSN (insn);
216 }
217
218 while (NOTE_P (insn) || DEBUG_INSN_P (insn))
219 {
220 if (insn == BB_END (bb))
221 return NULL;
222 insn = NEXT_INSN (insn);
223 }
224
225 if (JUMP_P (insn))
226 return NULL;
227
228 return insn;
229 }
230
231 /* Return the last non-jump active (non-jump) insn in the basic block. */
232
233 static rtx_insn *
234 last_active_insn (basic_block bb, int skip_use_p)
235 {
236 rtx_insn *insn = BB_END (bb);
237 rtx_insn *head = BB_HEAD (bb);
238
239 while (NOTE_P (insn)
240 || JUMP_P (insn)
241 || DEBUG_INSN_P (insn)
242 || (skip_use_p
243 && NONJUMP_INSN_P (insn)
244 && GET_CODE (PATTERN (insn)) == USE))
245 {
246 if (insn == head)
247 return NULL;
248 insn = PREV_INSN (insn);
249 }
250
251 if (LABEL_P (insn))
252 return NULL;
253
254 return insn;
255 }
256
257 /* Return the active insn before INSN inside basic block CURR_BB. */
258
259 static rtx
260 find_active_insn_before (basic_block curr_bb, rtx insn)
261 {
262 if (!insn || insn == BB_HEAD (curr_bb))
263 return NULL_RTX;
264
265 while ((insn = PREV_INSN (insn)) != NULL_RTX)
266 {
267 if (NONJUMP_INSN_P (insn) || JUMP_P (insn) || CALL_P (insn))
268 break;
269
270 /* No other active insn all the way to the start of the basic block. */
271 if (insn == BB_HEAD (curr_bb))
272 return NULL_RTX;
273 }
274
275 return insn;
276 }
277
278 /* Return the active insn after INSN inside basic block CURR_BB. */
279
280 static rtx
281 find_active_insn_after (basic_block curr_bb, rtx insn)
282 {
283 if (!insn || insn == BB_END (curr_bb))
284 return NULL_RTX;
285
286 while ((insn = NEXT_INSN (insn)) != NULL_RTX)
287 {
288 if (NONJUMP_INSN_P (insn) || JUMP_P (insn) || CALL_P (insn))
289 break;
290
291 /* No other active insn all the way to the end of the basic block. */
292 if (insn == BB_END (curr_bb))
293 return NULL_RTX;
294 }
295
296 return insn;
297 }
298
299 /* Return the basic block reached by falling though the basic block BB. */
300
301 static basic_block
302 block_fallthru (basic_block bb)
303 {
304 edge e = find_fallthru_edge (bb->succs);
305
306 return (e) ? e->dest : NULL_BLOCK;
307 }
308
309 /* Return true if RTXs A and B can be safely interchanged. */
310
311 static bool
312 rtx_interchangeable_p (const_rtx a, const_rtx b)
313 {
314 if (!rtx_equal_p (a, b))
315 return false;
316
317 if (GET_CODE (a) != MEM)
318 return true;
319
320 /* A dead type-unsafe memory reference is legal, but a live type-unsafe memory
321 reference is not. Interchanging a dead type-unsafe memory reference with
322 a live type-safe one creates a live type-unsafe memory reference, in other
323 words, it makes the program illegal.
324 We check here conservatively whether the two memory references have equal
325 memory attributes. */
326
327 return mem_attrs_eq_p (get_mem_attrs (a), get_mem_attrs (b));
328 }
329
330 \f
331 /* Go through a bunch of insns, converting them to conditional
332 execution format if possible. Return TRUE if all of the non-note
333 insns were processed. */
334
335 static int
336 cond_exec_process_insns (ce_if_block *ce_info ATTRIBUTE_UNUSED,
337 /* if block information */rtx start,
338 /* first insn to look at */rtx end,
339 /* last insn to look at */rtx test,
340 /* conditional execution test */int prob_val,
341 /* probability of branch taken. */int mod_ok)
342 {
343 int must_be_last = FALSE;
344 rtx insn;
345 rtx xtest;
346 rtx pattern;
347
348 if (!start || !end)
349 return FALSE;
350
351 for (insn = start; ; insn = NEXT_INSN (insn))
352 {
353 /* dwarf2out can't cope with conditional prologues. */
354 if (NOTE_P (insn) && NOTE_KIND (insn) == NOTE_INSN_PROLOGUE_END)
355 return FALSE;
356
357 if (NOTE_P (insn) || DEBUG_INSN_P (insn))
358 goto insn_done;
359
360 gcc_assert (NONJUMP_INSN_P (insn) || CALL_P (insn));
361
362 /* dwarf2out can't cope with conditional unwind info. */
363 if (RTX_FRAME_RELATED_P (insn))
364 return FALSE;
365
366 /* Remove USE insns that get in the way. */
367 if (reload_completed && GET_CODE (PATTERN (insn)) == USE)
368 {
369 /* ??? Ug. Actually unlinking the thing is problematic,
370 given what we'd have to coordinate with our callers. */
371 SET_INSN_DELETED (insn);
372 goto insn_done;
373 }
374
375 /* Last insn wasn't last? */
376 if (must_be_last)
377 return FALSE;
378
379 if (modified_in_p (test, insn))
380 {
381 if (!mod_ok)
382 return FALSE;
383 must_be_last = TRUE;
384 }
385
386 /* Now build the conditional form of the instruction. */
387 pattern = PATTERN (insn);
388 xtest = copy_rtx (test);
389
390 /* If this is already a COND_EXEC, rewrite the test to be an AND of the
391 two conditions. */
392 if (GET_CODE (pattern) == COND_EXEC)
393 {
394 if (GET_MODE (xtest) != GET_MODE (COND_EXEC_TEST (pattern)))
395 return FALSE;
396
397 xtest = gen_rtx_AND (GET_MODE (xtest), xtest,
398 COND_EXEC_TEST (pattern));
399 pattern = COND_EXEC_CODE (pattern);
400 }
401
402 pattern = gen_rtx_COND_EXEC (VOIDmode, xtest, pattern);
403
404 /* If the machine needs to modify the insn being conditionally executed,
405 say for example to force a constant integer operand into a temp
406 register, do so here. */
407 #ifdef IFCVT_MODIFY_INSN
408 IFCVT_MODIFY_INSN (ce_info, pattern, insn);
409 if (! pattern)
410 return FALSE;
411 #endif
412
413 validate_change (insn, &PATTERN (insn), pattern, 1);
414
415 if (CALL_P (insn) && prob_val >= 0)
416 validate_change (insn, &REG_NOTES (insn),
417 gen_rtx_INT_LIST ((enum machine_mode) REG_BR_PROB,
418 prob_val, REG_NOTES (insn)), 1);
419
420 insn_done:
421 if (insn == end)
422 break;
423 }
424
425 return TRUE;
426 }
427
428 /* Return the condition for a jump. Do not do any special processing. */
429
430 static rtx
431 cond_exec_get_condition (rtx jump)
432 {
433 rtx test_if, cond;
434
435 if (any_condjump_p (jump))
436 test_if = SET_SRC (pc_set (jump));
437 else
438 return NULL_RTX;
439 cond = XEXP (test_if, 0);
440
441 /* If this branches to JUMP_LABEL when the condition is false,
442 reverse the condition. */
443 if (GET_CODE (XEXP (test_if, 2)) == LABEL_REF
444 && XEXP (XEXP (test_if, 2), 0) == JUMP_LABEL (jump))
445 {
446 enum rtx_code rev = reversed_comparison_code (cond, jump);
447 if (rev == UNKNOWN)
448 return NULL_RTX;
449
450 cond = gen_rtx_fmt_ee (rev, GET_MODE (cond), XEXP (cond, 0),
451 XEXP (cond, 1));
452 }
453
454 return cond;
455 }
456
457 /* Given a simple IF-THEN or IF-THEN-ELSE block, attempt to convert it
458 to conditional execution. Return TRUE if we were successful at
459 converting the block. */
460
461 static int
462 cond_exec_process_if_block (ce_if_block * ce_info,
463 /* if block information */int do_multiple_p)
464 {
465 basic_block test_bb = ce_info->test_bb; /* last test block */
466 basic_block then_bb = ce_info->then_bb; /* THEN */
467 basic_block else_bb = ce_info->else_bb; /* ELSE or NULL */
468 rtx test_expr; /* expression in IF_THEN_ELSE that is tested */
469 rtx then_start; /* first insn in THEN block */
470 rtx then_end; /* last insn + 1 in THEN block */
471 rtx else_start = NULL_RTX; /* first insn in ELSE block or NULL */
472 rtx else_end = NULL_RTX; /* last insn + 1 in ELSE block */
473 int max; /* max # of insns to convert. */
474 int then_mod_ok; /* whether conditional mods are ok in THEN */
475 rtx true_expr; /* test for else block insns */
476 rtx false_expr; /* test for then block insns */
477 int true_prob_val; /* probability of else block */
478 int false_prob_val; /* probability of then block */
479 rtx_insn *then_last_head = NULL; /* Last match at the head of THEN */
480 rtx_insn *else_last_head = NULL; /* Last match at the head of ELSE */
481 rtx_insn *then_first_tail = NULL; /* First match at the tail of THEN */
482 rtx_insn *else_first_tail = NULL; /* First match at the tail of ELSE */
483 int then_n_insns, else_n_insns, n_insns;
484 enum rtx_code false_code;
485 rtx note;
486
487 /* If test is comprised of && or || elements, and we've failed at handling
488 all of them together, just use the last test if it is the special case of
489 && elements without an ELSE block. */
490 if (!do_multiple_p && ce_info->num_multiple_test_blocks)
491 {
492 if (else_bb || ! ce_info->and_and_p)
493 return FALSE;
494
495 ce_info->test_bb = test_bb = ce_info->last_test_bb;
496 ce_info->num_multiple_test_blocks = 0;
497 ce_info->num_and_and_blocks = 0;
498 ce_info->num_or_or_blocks = 0;
499 }
500
501 /* Find the conditional jump to the ELSE or JOIN part, and isolate
502 the test. */
503 test_expr = cond_exec_get_condition (BB_END (test_bb));
504 if (! test_expr)
505 return FALSE;
506
507 /* If the conditional jump is more than just a conditional jump,
508 then we can not do conditional execution conversion on this block. */
509 if (! onlyjump_p (BB_END (test_bb)))
510 return FALSE;
511
512 /* Collect the bounds of where we're to search, skipping any labels, jumps
513 and notes at the beginning and end of the block. Then count the total
514 number of insns and see if it is small enough to convert. */
515 then_start = first_active_insn (then_bb);
516 then_end = last_active_insn (then_bb, TRUE);
517 then_n_insns = ce_info->num_then_insns = count_bb_insns (then_bb);
518 n_insns = then_n_insns;
519 max = MAX_CONDITIONAL_EXECUTE;
520
521 if (else_bb)
522 {
523 int n_matching;
524
525 max *= 2;
526 else_start = first_active_insn (else_bb);
527 else_end = last_active_insn (else_bb, TRUE);
528 else_n_insns = ce_info->num_else_insns = count_bb_insns (else_bb);
529 n_insns += else_n_insns;
530
531 /* Look for matching sequences at the head and tail of the two blocks,
532 and limit the range of insns to be converted if possible. */
533 n_matching = flow_find_cross_jump (then_bb, else_bb,
534 &then_first_tail, &else_first_tail,
535 NULL);
536 if (then_first_tail == BB_HEAD (then_bb))
537 then_start = then_end = NULL_RTX;
538 if (else_first_tail == BB_HEAD (else_bb))
539 else_start = else_end = NULL_RTX;
540
541 if (n_matching > 0)
542 {
543 if (then_end)
544 then_end = find_active_insn_before (then_bb, then_first_tail);
545 if (else_end)
546 else_end = find_active_insn_before (else_bb, else_first_tail);
547 n_insns -= 2 * n_matching;
548 }
549
550 if (then_start
551 && else_start
552 && then_n_insns > n_matching
553 && else_n_insns > n_matching)
554 {
555 int longest_match = MIN (then_n_insns - n_matching,
556 else_n_insns - n_matching);
557 n_matching
558 = flow_find_head_matching_sequence (then_bb, else_bb,
559 &then_last_head,
560 &else_last_head,
561 longest_match);
562
563 if (n_matching > 0)
564 {
565 rtx insn;
566
567 /* We won't pass the insns in the head sequence to
568 cond_exec_process_insns, so we need to test them here
569 to make sure that they don't clobber the condition. */
570 for (insn = BB_HEAD (then_bb);
571 insn != NEXT_INSN (then_last_head);
572 insn = NEXT_INSN (insn))
573 if (!LABEL_P (insn) && !NOTE_P (insn)
574 && !DEBUG_INSN_P (insn)
575 && modified_in_p (test_expr, insn))
576 return FALSE;
577 }
578
579 if (then_last_head == then_end)
580 then_start = then_end = NULL_RTX;
581 if (else_last_head == else_end)
582 else_start = else_end = NULL_RTX;
583
584 if (n_matching > 0)
585 {
586 if (then_start)
587 then_start = find_active_insn_after (then_bb, then_last_head);
588 if (else_start)
589 else_start = find_active_insn_after (else_bb, else_last_head);
590 n_insns -= 2 * n_matching;
591 }
592 }
593 }
594
595 if (n_insns > max)
596 return FALSE;
597
598 /* Map test_expr/test_jump into the appropriate MD tests to use on
599 the conditionally executed code. */
600
601 true_expr = test_expr;
602
603 false_code = reversed_comparison_code (true_expr, BB_END (test_bb));
604 if (false_code != UNKNOWN)
605 false_expr = gen_rtx_fmt_ee (false_code, GET_MODE (true_expr),
606 XEXP (true_expr, 0), XEXP (true_expr, 1));
607 else
608 false_expr = NULL_RTX;
609
610 #ifdef IFCVT_MODIFY_TESTS
611 /* If the machine description needs to modify the tests, such as setting a
612 conditional execution register from a comparison, it can do so here. */
613 IFCVT_MODIFY_TESTS (ce_info, true_expr, false_expr);
614
615 /* See if the conversion failed. */
616 if (!true_expr || !false_expr)
617 goto fail;
618 #endif
619
620 note = find_reg_note (BB_END (test_bb), REG_BR_PROB, NULL_RTX);
621 if (note)
622 {
623 true_prob_val = XINT (note, 0);
624 false_prob_val = REG_BR_PROB_BASE - true_prob_val;
625 }
626 else
627 {
628 true_prob_val = -1;
629 false_prob_val = -1;
630 }
631
632 /* If we have && or || tests, do them here. These tests are in the adjacent
633 blocks after the first block containing the test. */
634 if (ce_info->num_multiple_test_blocks > 0)
635 {
636 basic_block bb = test_bb;
637 basic_block last_test_bb = ce_info->last_test_bb;
638
639 if (! false_expr)
640 goto fail;
641
642 do
643 {
644 rtx start, end;
645 rtx t, f;
646 enum rtx_code f_code;
647
648 bb = block_fallthru (bb);
649 start = first_active_insn (bb);
650 end = last_active_insn (bb, TRUE);
651 if (start
652 && ! cond_exec_process_insns (ce_info, start, end, false_expr,
653 false_prob_val, FALSE))
654 goto fail;
655
656 /* If the conditional jump is more than just a conditional jump, then
657 we can not do conditional execution conversion on this block. */
658 if (! onlyjump_p (BB_END (bb)))
659 goto fail;
660
661 /* Find the conditional jump and isolate the test. */
662 t = cond_exec_get_condition (BB_END (bb));
663 if (! t)
664 goto fail;
665
666 f_code = reversed_comparison_code (t, BB_END (bb));
667 if (f_code == UNKNOWN)
668 goto fail;
669
670 f = gen_rtx_fmt_ee (f_code, GET_MODE (t), XEXP (t, 0), XEXP (t, 1));
671 if (ce_info->and_and_p)
672 {
673 t = gen_rtx_AND (GET_MODE (t), true_expr, t);
674 f = gen_rtx_IOR (GET_MODE (t), false_expr, f);
675 }
676 else
677 {
678 t = gen_rtx_IOR (GET_MODE (t), true_expr, t);
679 f = gen_rtx_AND (GET_MODE (t), false_expr, f);
680 }
681
682 /* If the machine description needs to modify the tests, such as
683 setting a conditional execution register from a comparison, it can
684 do so here. */
685 #ifdef IFCVT_MODIFY_MULTIPLE_TESTS
686 IFCVT_MODIFY_MULTIPLE_TESTS (ce_info, bb, t, f);
687
688 /* See if the conversion failed. */
689 if (!t || !f)
690 goto fail;
691 #endif
692
693 true_expr = t;
694 false_expr = f;
695 }
696 while (bb != last_test_bb);
697 }
698
699 /* For IF-THEN-ELSE blocks, we don't allow modifications of the test
700 on then THEN block. */
701 then_mod_ok = (else_bb == NULL_BLOCK);
702
703 /* Go through the THEN and ELSE blocks converting the insns if possible
704 to conditional execution. */
705
706 if (then_end
707 && (! false_expr
708 || ! cond_exec_process_insns (ce_info, then_start, then_end,
709 false_expr, false_prob_val,
710 then_mod_ok)))
711 goto fail;
712
713 if (else_bb && else_end
714 && ! cond_exec_process_insns (ce_info, else_start, else_end,
715 true_expr, true_prob_val, TRUE))
716 goto fail;
717
718 /* If we cannot apply the changes, fail. Do not go through the normal fail
719 processing, since apply_change_group will call cancel_changes. */
720 if (! apply_change_group ())
721 {
722 #ifdef IFCVT_MODIFY_CANCEL
723 /* Cancel any machine dependent changes. */
724 IFCVT_MODIFY_CANCEL (ce_info);
725 #endif
726 return FALSE;
727 }
728
729 #ifdef IFCVT_MODIFY_FINAL
730 /* Do any machine dependent final modifications. */
731 IFCVT_MODIFY_FINAL (ce_info);
732 #endif
733
734 /* Conversion succeeded. */
735 if (dump_file)
736 fprintf (dump_file, "%d insn%s converted to conditional execution.\n",
737 n_insns, (n_insns == 1) ? " was" : "s were");
738
739 /* Merge the blocks! If we had matching sequences, make sure to delete one
740 copy at the appropriate location first: delete the copy in the THEN branch
741 for a tail sequence so that the remaining one is executed last for both
742 branches, and delete the copy in the ELSE branch for a head sequence so
743 that the remaining one is executed first for both branches. */
744 if (then_first_tail)
745 {
746 rtx from = then_first_tail;
747 if (!INSN_P (from))
748 from = find_active_insn_after (then_bb, from);
749 delete_insn_chain (from, BB_END (then_bb), false);
750 }
751 if (else_last_head)
752 delete_insn_chain (first_active_insn (else_bb), else_last_head, false);
753
754 merge_if_block (ce_info);
755 cond_exec_changed_p = TRUE;
756 return TRUE;
757
758 fail:
759 #ifdef IFCVT_MODIFY_CANCEL
760 /* Cancel any machine dependent changes. */
761 IFCVT_MODIFY_CANCEL (ce_info);
762 #endif
763
764 cancel_changes (0);
765 return FALSE;
766 }
767 \f
768 /* Used by noce_process_if_block to communicate with its subroutines.
769
770 The subroutines know that A and B may be evaluated freely. They
771 know that X is a register. They should insert new instructions
772 before cond_earliest. */
773
774 struct noce_if_info
775 {
776 /* The basic blocks that make up the IF-THEN-{ELSE-,}JOIN block. */
777 basic_block test_bb, then_bb, else_bb, join_bb;
778
779 /* The jump that ends TEST_BB. */
780 rtx_insn *jump;
781
782 /* The jump condition. */
783 rtx cond;
784
785 /* New insns should be inserted before this one. */
786 rtx_insn *cond_earliest;
787
788 /* Insns in the THEN and ELSE block. There is always just this
789 one insns in those blocks. The insns are single_set insns.
790 If there was no ELSE block, INSN_B is the last insn before
791 COND_EARLIEST, or NULL_RTX. In the former case, the insn
792 operands are still valid, as if INSN_B was moved down below
793 the jump. */
794 rtx_insn *insn_a, *insn_b;
795
796 /* The SET_SRC of INSN_A and INSN_B. */
797 rtx a, b;
798
799 /* The SET_DEST of INSN_A. */
800 rtx x;
801
802 /* True if this if block is not canonical. In the canonical form of
803 if blocks, the THEN_BB is the block reached via the fallthru edge
804 from TEST_BB. For the noce transformations, we allow the symmetric
805 form as well. */
806 bool then_else_reversed;
807
808 /* Estimated cost of the particular branch instruction. */
809 int branch_cost;
810 };
811
812 static rtx noce_emit_store_flag (struct noce_if_info *, rtx, int, int);
813 static int noce_try_move (struct noce_if_info *);
814 static int noce_try_store_flag (struct noce_if_info *);
815 static int noce_try_addcc (struct noce_if_info *);
816 static int noce_try_store_flag_constants (struct noce_if_info *);
817 static int noce_try_store_flag_mask (struct noce_if_info *);
818 static rtx noce_emit_cmove (struct noce_if_info *, rtx, enum rtx_code, rtx,
819 rtx, rtx, rtx);
820 static int noce_try_cmove (struct noce_if_info *);
821 static int noce_try_cmove_arith (struct noce_if_info *);
822 static rtx noce_get_alt_condition (struct noce_if_info *, rtx, rtx_insn **);
823 static int noce_try_minmax (struct noce_if_info *);
824 static int noce_try_abs (struct noce_if_info *);
825 static int noce_try_sign_mask (struct noce_if_info *);
826
827 /* Helper function for noce_try_store_flag*. */
828
829 static rtx
830 noce_emit_store_flag (struct noce_if_info *if_info, rtx x, int reversep,
831 int normalize)
832 {
833 rtx cond = if_info->cond;
834 int cond_complex;
835 enum rtx_code code;
836
837 cond_complex = (! general_operand (XEXP (cond, 0), VOIDmode)
838 || ! general_operand (XEXP (cond, 1), VOIDmode));
839
840 /* If earliest == jump, or when the condition is complex, try to
841 build the store_flag insn directly. */
842
843 if (cond_complex)
844 {
845 rtx set = pc_set (if_info->jump);
846 cond = XEXP (SET_SRC (set), 0);
847 if (GET_CODE (XEXP (SET_SRC (set), 2)) == LABEL_REF
848 && XEXP (XEXP (SET_SRC (set), 2), 0) == JUMP_LABEL (if_info->jump))
849 reversep = !reversep;
850 if (if_info->then_else_reversed)
851 reversep = !reversep;
852 }
853
854 if (reversep)
855 code = reversed_comparison_code (cond, if_info->jump);
856 else
857 code = GET_CODE (cond);
858
859 if ((if_info->cond_earliest == if_info->jump || cond_complex)
860 && (normalize == 0 || STORE_FLAG_VALUE == normalize))
861 {
862 rtx tmp;
863
864 tmp = gen_rtx_fmt_ee (code, GET_MODE (x), XEXP (cond, 0),
865 XEXP (cond, 1));
866 tmp = gen_rtx_SET (VOIDmode, x, tmp);
867
868 start_sequence ();
869 tmp = emit_insn (tmp);
870
871 if (recog_memoized (tmp) >= 0)
872 {
873 tmp = get_insns ();
874 end_sequence ();
875 emit_insn (tmp);
876
877 if_info->cond_earliest = if_info->jump;
878
879 return x;
880 }
881
882 end_sequence ();
883 }
884
885 /* Don't even try if the comparison operands or the mode of X are weird. */
886 if (cond_complex || !SCALAR_INT_MODE_P (GET_MODE (x)))
887 return NULL_RTX;
888
889 return emit_store_flag (x, code, XEXP (cond, 0),
890 XEXP (cond, 1), VOIDmode,
891 (code == LTU || code == LEU
892 || code == GEU || code == GTU), normalize);
893 }
894
895 /* Emit instruction to move an rtx, possibly into STRICT_LOW_PART.
896 X is the destination/target and Y is the value to copy. */
897
898 static void
899 noce_emit_move_insn (rtx x, rtx y)
900 {
901 enum machine_mode outmode;
902 rtx outer, inner;
903 int bitpos;
904
905 if (GET_CODE (x) != STRICT_LOW_PART)
906 {
907 rtx seq, insn, target;
908 optab ot;
909
910 start_sequence ();
911 /* Check that the SET_SRC is reasonable before calling emit_move_insn,
912 otherwise construct a suitable SET pattern ourselves. */
913 insn = (OBJECT_P (y) || CONSTANT_P (y) || GET_CODE (y) == SUBREG)
914 ? emit_move_insn (x, y)
915 : emit_insn (gen_rtx_SET (VOIDmode, x, y));
916 seq = get_insns ();
917 end_sequence ();
918
919 if (recog_memoized (insn) <= 0)
920 {
921 if (GET_CODE (x) == ZERO_EXTRACT)
922 {
923 rtx op = XEXP (x, 0);
924 unsigned HOST_WIDE_INT size = INTVAL (XEXP (x, 1));
925 unsigned HOST_WIDE_INT start = INTVAL (XEXP (x, 2));
926
927 /* store_bit_field expects START to be relative to
928 BYTES_BIG_ENDIAN and adjusts this value for machines with
929 BITS_BIG_ENDIAN != BYTES_BIG_ENDIAN. In order to be able to
930 invoke store_bit_field again it is necessary to have the START
931 value from the first call. */
932 if (BITS_BIG_ENDIAN != BYTES_BIG_ENDIAN)
933 {
934 if (MEM_P (op))
935 start = BITS_PER_UNIT - start - size;
936 else
937 {
938 gcc_assert (REG_P (op));
939 start = BITS_PER_WORD - start - size;
940 }
941 }
942
943 gcc_assert (start < (MEM_P (op) ? BITS_PER_UNIT : BITS_PER_WORD));
944 store_bit_field (op, size, start, 0, 0, GET_MODE (x), y);
945 return;
946 }
947
948 switch (GET_RTX_CLASS (GET_CODE (y)))
949 {
950 case RTX_UNARY:
951 ot = code_to_optab (GET_CODE (y));
952 if (ot)
953 {
954 start_sequence ();
955 target = expand_unop (GET_MODE (y), ot, XEXP (y, 0), x, 0);
956 if (target != NULL_RTX)
957 {
958 if (target != x)
959 emit_move_insn (x, target);
960 seq = get_insns ();
961 }
962 end_sequence ();
963 }
964 break;
965
966 case RTX_BIN_ARITH:
967 case RTX_COMM_ARITH:
968 ot = code_to_optab (GET_CODE (y));
969 if (ot)
970 {
971 start_sequence ();
972 target = expand_binop (GET_MODE (y), ot,
973 XEXP (y, 0), XEXP (y, 1),
974 x, 0, OPTAB_DIRECT);
975 if (target != NULL_RTX)
976 {
977 if (target != x)
978 emit_move_insn (x, target);
979 seq = get_insns ();
980 }
981 end_sequence ();
982 }
983 break;
984
985 default:
986 break;
987 }
988 }
989
990 emit_insn (seq);
991 return;
992 }
993
994 outer = XEXP (x, 0);
995 inner = XEXP (outer, 0);
996 outmode = GET_MODE (outer);
997 bitpos = SUBREG_BYTE (outer) * BITS_PER_UNIT;
998 store_bit_field (inner, GET_MODE_BITSIZE (outmode), bitpos,
999 0, 0, outmode, y);
1000 }
1001
1002 /* Return sequence of instructions generated by if conversion. This
1003 function calls end_sequence() to end the current stream, ensures
1004 that are instructions are unshared, recognizable non-jump insns.
1005 On failure, this function returns a NULL_RTX. */
1006
1007 static rtx_insn *
1008 end_ifcvt_sequence (struct noce_if_info *if_info)
1009 {
1010 rtx_insn *insn;
1011 rtx_insn *seq = get_insns ();
1012
1013 set_used_flags (if_info->x);
1014 set_used_flags (if_info->cond);
1015 set_used_flags (if_info->a);
1016 set_used_flags (if_info->b);
1017 unshare_all_rtl_in_chain (seq);
1018 end_sequence ();
1019
1020 /* Make sure that all of the instructions emitted are recognizable,
1021 and that we haven't introduced a new jump instruction.
1022 As an exercise for the reader, build a general mechanism that
1023 allows proper placement of required clobbers. */
1024 for (insn = seq; insn; insn = NEXT_INSN (insn))
1025 if (JUMP_P (insn)
1026 || recog_memoized (insn) == -1)
1027 return NULL;
1028
1029 return seq;
1030 }
1031
1032 /* Convert "if (a != b) x = a; else x = b" into "x = a" and
1033 "if (a == b) x = a; else x = b" into "x = b". */
1034
1035 static int
1036 noce_try_move (struct noce_if_info *if_info)
1037 {
1038 rtx cond = if_info->cond;
1039 enum rtx_code code = GET_CODE (cond);
1040 rtx y;
1041 rtx_insn *seq;
1042
1043 if (code != NE && code != EQ)
1044 return FALSE;
1045
1046 /* This optimization isn't valid if either A or B could be a NaN
1047 or a signed zero. */
1048 if (HONOR_NANS (GET_MODE (if_info->x))
1049 || HONOR_SIGNED_ZEROS (GET_MODE (if_info->x)))
1050 return FALSE;
1051
1052 /* Check whether the operands of the comparison are A and in
1053 either order. */
1054 if ((rtx_equal_p (if_info->a, XEXP (cond, 0))
1055 && rtx_equal_p (if_info->b, XEXP (cond, 1)))
1056 || (rtx_equal_p (if_info->a, XEXP (cond, 1))
1057 && rtx_equal_p (if_info->b, XEXP (cond, 0))))
1058 {
1059 if (!rtx_interchangeable_p (if_info->a, if_info->b))
1060 return FALSE;
1061
1062 y = (code == EQ) ? if_info->a : if_info->b;
1063
1064 /* Avoid generating the move if the source is the destination. */
1065 if (! rtx_equal_p (if_info->x, y))
1066 {
1067 start_sequence ();
1068 noce_emit_move_insn (if_info->x, y);
1069 seq = end_ifcvt_sequence (if_info);
1070 if (!seq)
1071 return FALSE;
1072
1073 emit_insn_before_setloc (seq, if_info->jump,
1074 INSN_LOCATION (if_info->insn_a));
1075 }
1076 return TRUE;
1077 }
1078 return FALSE;
1079 }
1080
1081 /* Convert "if (test) x = 1; else x = 0".
1082
1083 Only try 0 and STORE_FLAG_VALUE here. Other combinations will be
1084 tried in noce_try_store_flag_constants after noce_try_cmove has had
1085 a go at the conversion. */
1086
1087 static int
1088 noce_try_store_flag (struct noce_if_info *if_info)
1089 {
1090 int reversep;
1091 rtx target;
1092 rtx_insn *seq;
1093
1094 if (CONST_INT_P (if_info->b)
1095 && INTVAL (if_info->b) == STORE_FLAG_VALUE
1096 && if_info->a == const0_rtx)
1097 reversep = 0;
1098 else if (if_info->b == const0_rtx
1099 && CONST_INT_P (if_info->a)
1100 && INTVAL (if_info->a) == STORE_FLAG_VALUE
1101 && (reversed_comparison_code (if_info->cond, if_info->jump)
1102 != UNKNOWN))
1103 reversep = 1;
1104 else
1105 return FALSE;
1106
1107 start_sequence ();
1108
1109 target = noce_emit_store_flag (if_info, if_info->x, reversep, 0);
1110 if (target)
1111 {
1112 if (target != if_info->x)
1113 noce_emit_move_insn (if_info->x, target);
1114
1115 seq = end_ifcvt_sequence (if_info);
1116 if (! seq)
1117 return FALSE;
1118
1119 emit_insn_before_setloc (seq, if_info->jump,
1120 INSN_LOCATION (if_info->insn_a));
1121 return TRUE;
1122 }
1123 else
1124 {
1125 end_sequence ();
1126 return FALSE;
1127 }
1128 }
1129
1130 /* Convert "if (test) x = a; else x = b", for A and B constant. */
1131
1132 static int
1133 noce_try_store_flag_constants (struct noce_if_info *if_info)
1134 {
1135 rtx target;
1136 rtx_insn *seq;
1137 int reversep;
1138 HOST_WIDE_INT itrue, ifalse, diff, tmp;
1139 int normalize, can_reverse;
1140 enum machine_mode mode;
1141
1142 if (CONST_INT_P (if_info->a)
1143 && CONST_INT_P (if_info->b))
1144 {
1145 mode = GET_MODE (if_info->x);
1146 ifalse = INTVAL (if_info->a);
1147 itrue = INTVAL (if_info->b);
1148
1149 diff = (unsigned HOST_WIDE_INT) itrue - ifalse;
1150 /* Make sure we can represent the difference between the two values. */
1151 if ((diff > 0)
1152 != ((ifalse < 0) != (itrue < 0) ? ifalse < 0 : ifalse < itrue))
1153 return FALSE;
1154
1155 diff = trunc_int_for_mode (diff, mode);
1156
1157 can_reverse = (reversed_comparison_code (if_info->cond, if_info->jump)
1158 != UNKNOWN);
1159
1160 reversep = 0;
1161 if (diff == STORE_FLAG_VALUE || diff == -STORE_FLAG_VALUE)
1162 normalize = 0;
1163 else if (ifalse == 0 && exact_log2 (itrue) >= 0
1164 && (STORE_FLAG_VALUE == 1
1165 || if_info->branch_cost >= 2))
1166 normalize = 1;
1167 else if (itrue == 0 && exact_log2 (ifalse) >= 0 && can_reverse
1168 && (STORE_FLAG_VALUE == 1 || if_info->branch_cost >= 2))
1169 normalize = 1, reversep = 1;
1170 else if (itrue == -1
1171 && (STORE_FLAG_VALUE == -1
1172 || if_info->branch_cost >= 2))
1173 normalize = -1;
1174 else if (ifalse == -1 && can_reverse
1175 && (STORE_FLAG_VALUE == -1 || if_info->branch_cost >= 2))
1176 normalize = -1, reversep = 1;
1177 else if ((if_info->branch_cost >= 2 && STORE_FLAG_VALUE == -1)
1178 || if_info->branch_cost >= 3)
1179 normalize = -1;
1180 else
1181 return FALSE;
1182
1183 if (reversep)
1184 {
1185 tmp = itrue; itrue = ifalse; ifalse = tmp;
1186 diff = trunc_int_for_mode (-(unsigned HOST_WIDE_INT) diff, mode);
1187 }
1188
1189 start_sequence ();
1190 target = noce_emit_store_flag (if_info, if_info->x, reversep, normalize);
1191 if (! target)
1192 {
1193 end_sequence ();
1194 return FALSE;
1195 }
1196
1197 /* if (test) x = 3; else x = 4;
1198 => x = 3 + (test == 0); */
1199 if (diff == STORE_FLAG_VALUE || diff == -STORE_FLAG_VALUE)
1200 {
1201 target = expand_simple_binop (mode,
1202 (diff == STORE_FLAG_VALUE
1203 ? PLUS : MINUS),
1204 gen_int_mode (ifalse, mode), target,
1205 if_info->x, 0, OPTAB_WIDEN);
1206 }
1207
1208 /* if (test) x = 8; else x = 0;
1209 => x = (test != 0) << 3; */
1210 else if (ifalse == 0 && (tmp = exact_log2 (itrue)) >= 0)
1211 {
1212 target = expand_simple_binop (mode, ASHIFT,
1213 target, GEN_INT (tmp), if_info->x, 0,
1214 OPTAB_WIDEN);
1215 }
1216
1217 /* if (test) x = -1; else x = b;
1218 => x = -(test != 0) | b; */
1219 else if (itrue == -1)
1220 {
1221 target = expand_simple_binop (mode, IOR,
1222 target, gen_int_mode (ifalse, mode),
1223 if_info->x, 0, OPTAB_WIDEN);
1224 }
1225
1226 /* if (test) x = a; else x = b;
1227 => x = (-(test != 0) & (b - a)) + a; */
1228 else
1229 {
1230 target = expand_simple_binop (mode, AND,
1231 target, gen_int_mode (diff, mode),
1232 if_info->x, 0, OPTAB_WIDEN);
1233 if (target)
1234 target = expand_simple_binop (mode, PLUS,
1235 target, gen_int_mode (ifalse, mode),
1236 if_info->x, 0, OPTAB_WIDEN);
1237 }
1238
1239 if (! target)
1240 {
1241 end_sequence ();
1242 return FALSE;
1243 }
1244
1245 if (target != if_info->x)
1246 noce_emit_move_insn (if_info->x, target);
1247
1248 seq = end_ifcvt_sequence (if_info);
1249 if (!seq)
1250 return FALSE;
1251
1252 emit_insn_before_setloc (seq, if_info->jump,
1253 INSN_LOCATION (if_info->insn_a));
1254 return TRUE;
1255 }
1256
1257 return FALSE;
1258 }
1259
1260 /* Convert "if (test) foo++" into "foo += (test != 0)", and
1261 similarly for "foo--". */
1262
1263 static int
1264 noce_try_addcc (struct noce_if_info *if_info)
1265 {
1266 rtx target;
1267 rtx_insn *seq;
1268 int subtract, normalize;
1269
1270 if (GET_CODE (if_info->a) == PLUS
1271 && rtx_equal_p (XEXP (if_info->a, 0), if_info->b)
1272 && (reversed_comparison_code (if_info->cond, if_info->jump)
1273 != UNKNOWN))
1274 {
1275 rtx cond = if_info->cond;
1276 enum rtx_code code = reversed_comparison_code (cond, if_info->jump);
1277
1278 /* First try to use addcc pattern. */
1279 if (general_operand (XEXP (cond, 0), VOIDmode)
1280 && general_operand (XEXP (cond, 1), VOIDmode))
1281 {
1282 start_sequence ();
1283 target = emit_conditional_add (if_info->x, code,
1284 XEXP (cond, 0),
1285 XEXP (cond, 1),
1286 VOIDmode,
1287 if_info->b,
1288 XEXP (if_info->a, 1),
1289 GET_MODE (if_info->x),
1290 (code == LTU || code == GEU
1291 || code == LEU || code == GTU));
1292 if (target)
1293 {
1294 if (target != if_info->x)
1295 noce_emit_move_insn (if_info->x, target);
1296
1297 seq = end_ifcvt_sequence (if_info);
1298 if (!seq)
1299 return FALSE;
1300
1301 emit_insn_before_setloc (seq, if_info->jump,
1302 INSN_LOCATION (if_info->insn_a));
1303 return TRUE;
1304 }
1305 end_sequence ();
1306 }
1307
1308 /* If that fails, construct conditional increment or decrement using
1309 setcc. */
1310 if (if_info->branch_cost >= 2
1311 && (XEXP (if_info->a, 1) == const1_rtx
1312 || XEXP (if_info->a, 1) == constm1_rtx))
1313 {
1314 start_sequence ();
1315 if (STORE_FLAG_VALUE == INTVAL (XEXP (if_info->a, 1)))
1316 subtract = 0, normalize = 0;
1317 else if (-STORE_FLAG_VALUE == INTVAL (XEXP (if_info->a, 1)))
1318 subtract = 1, normalize = 0;
1319 else
1320 subtract = 0, normalize = INTVAL (XEXP (if_info->a, 1));
1321
1322
1323 target = noce_emit_store_flag (if_info,
1324 gen_reg_rtx (GET_MODE (if_info->x)),
1325 1, normalize);
1326
1327 if (target)
1328 target = expand_simple_binop (GET_MODE (if_info->x),
1329 subtract ? MINUS : PLUS,
1330 if_info->b, target, if_info->x,
1331 0, OPTAB_WIDEN);
1332 if (target)
1333 {
1334 if (target != if_info->x)
1335 noce_emit_move_insn (if_info->x, target);
1336
1337 seq = end_ifcvt_sequence (if_info);
1338 if (!seq)
1339 return FALSE;
1340
1341 emit_insn_before_setloc (seq, if_info->jump,
1342 INSN_LOCATION (if_info->insn_a));
1343 return TRUE;
1344 }
1345 end_sequence ();
1346 }
1347 }
1348
1349 return FALSE;
1350 }
1351
1352 /* Convert "if (test) x = 0;" to "x &= -(test == 0);" */
1353
1354 static int
1355 noce_try_store_flag_mask (struct noce_if_info *if_info)
1356 {
1357 rtx target;
1358 rtx_insn *seq;
1359 int reversep;
1360
1361 reversep = 0;
1362 if ((if_info->branch_cost >= 2
1363 || STORE_FLAG_VALUE == -1)
1364 && ((if_info->a == const0_rtx
1365 && rtx_equal_p (if_info->b, if_info->x))
1366 || ((reversep = (reversed_comparison_code (if_info->cond,
1367 if_info->jump)
1368 != UNKNOWN))
1369 && if_info->b == const0_rtx
1370 && rtx_equal_p (if_info->a, if_info->x))))
1371 {
1372 start_sequence ();
1373 target = noce_emit_store_flag (if_info,
1374 gen_reg_rtx (GET_MODE (if_info->x)),
1375 reversep, -1);
1376 if (target)
1377 target = expand_simple_binop (GET_MODE (if_info->x), AND,
1378 if_info->x,
1379 target, if_info->x, 0,
1380 OPTAB_WIDEN);
1381
1382 if (target)
1383 {
1384 if (target != if_info->x)
1385 noce_emit_move_insn (if_info->x, target);
1386
1387 seq = end_ifcvt_sequence (if_info);
1388 if (!seq)
1389 return FALSE;
1390
1391 emit_insn_before_setloc (seq, if_info->jump,
1392 INSN_LOCATION (if_info->insn_a));
1393 return TRUE;
1394 }
1395
1396 end_sequence ();
1397 }
1398
1399 return FALSE;
1400 }
1401
1402 /* Helper function for noce_try_cmove and noce_try_cmove_arith. */
1403
1404 static rtx
1405 noce_emit_cmove (struct noce_if_info *if_info, rtx x, enum rtx_code code,
1406 rtx cmp_a, rtx cmp_b, rtx vfalse, rtx vtrue)
1407 {
1408 rtx target ATTRIBUTE_UNUSED;
1409 int unsignedp ATTRIBUTE_UNUSED;
1410
1411 /* If earliest == jump, try to build the cmove insn directly.
1412 This is helpful when combine has created some complex condition
1413 (like for alpha's cmovlbs) that we can't hope to regenerate
1414 through the normal interface. */
1415
1416 if (if_info->cond_earliest == if_info->jump)
1417 {
1418 rtx tmp;
1419
1420 tmp = gen_rtx_fmt_ee (code, GET_MODE (if_info->cond), cmp_a, cmp_b);
1421 tmp = gen_rtx_IF_THEN_ELSE (GET_MODE (x), tmp, vtrue, vfalse);
1422 tmp = gen_rtx_SET (VOIDmode, x, tmp);
1423
1424 start_sequence ();
1425 tmp = emit_insn (tmp);
1426
1427 if (recog_memoized (tmp) >= 0)
1428 {
1429 tmp = get_insns ();
1430 end_sequence ();
1431 emit_insn (tmp);
1432
1433 return x;
1434 }
1435
1436 end_sequence ();
1437 }
1438
1439 /* Don't even try if the comparison operands are weird. */
1440 if (! general_operand (cmp_a, GET_MODE (cmp_a))
1441 || ! general_operand (cmp_b, GET_MODE (cmp_b)))
1442 return NULL_RTX;
1443
1444 #if HAVE_conditional_move
1445 unsignedp = (code == LTU || code == GEU
1446 || code == LEU || code == GTU);
1447
1448 target = emit_conditional_move (x, code, cmp_a, cmp_b, VOIDmode,
1449 vtrue, vfalse, GET_MODE (x),
1450 unsignedp);
1451 if (target)
1452 return target;
1453
1454 /* We might be faced with a situation like:
1455
1456 x = (reg:M TARGET)
1457 vtrue = (subreg:M (reg:N VTRUE) BYTE)
1458 vfalse = (subreg:M (reg:N VFALSE) BYTE)
1459
1460 We can't do a conditional move in mode M, but it's possible that we
1461 could do a conditional move in mode N instead and take a subreg of
1462 the result.
1463
1464 If we can't create new pseudos, though, don't bother. */
1465 if (reload_completed)
1466 return NULL_RTX;
1467
1468 if (GET_CODE (vtrue) == SUBREG && GET_CODE (vfalse) == SUBREG)
1469 {
1470 rtx reg_vtrue = SUBREG_REG (vtrue);
1471 rtx reg_vfalse = SUBREG_REG (vfalse);
1472 unsigned int byte_vtrue = SUBREG_BYTE (vtrue);
1473 unsigned int byte_vfalse = SUBREG_BYTE (vfalse);
1474 rtx promoted_target;
1475
1476 if (GET_MODE (reg_vtrue) != GET_MODE (reg_vfalse)
1477 || byte_vtrue != byte_vfalse
1478 || (SUBREG_PROMOTED_VAR_P (vtrue)
1479 != SUBREG_PROMOTED_VAR_P (vfalse))
1480 || (SUBREG_PROMOTED_GET (vtrue)
1481 != SUBREG_PROMOTED_GET (vfalse)))
1482 return NULL_RTX;
1483
1484 promoted_target = gen_reg_rtx (GET_MODE (reg_vtrue));
1485
1486 target = emit_conditional_move (promoted_target, code, cmp_a, cmp_b,
1487 VOIDmode, reg_vtrue, reg_vfalse,
1488 GET_MODE (reg_vtrue), unsignedp);
1489 /* Nope, couldn't do it in that mode either. */
1490 if (!target)
1491 return NULL_RTX;
1492
1493 target = gen_rtx_SUBREG (GET_MODE (vtrue), promoted_target, byte_vtrue);
1494 SUBREG_PROMOTED_VAR_P (target) = SUBREG_PROMOTED_VAR_P (vtrue);
1495 SUBREG_PROMOTED_SET (target, SUBREG_PROMOTED_GET (vtrue));
1496 emit_move_insn (x, target);
1497 return x;
1498 }
1499 else
1500 return NULL_RTX;
1501 #else
1502 /* We'll never get here, as noce_process_if_block doesn't call the
1503 functions involved. Ifdef code, however, should be discouraged
1504 because it leads to typos in the code not selected. However,
1505 emit_conditional_move won't exist either. */
1506 return NULL_RTX;
1507 #endif
1508 }
1509
1510 /* Try only simple constants and registers here. More complex cases
1511 are handled in noce_try_cmove_arith after noce_try_store_flag_arith
1512 has had a go at it. */
1513
1514 static int
1515 noce_try_cmove (struct noce_if_info *if_info)
1516 {
1517 enum rtx_code code;
1518 rtx target;
1519 rtx_insn *seq;
1520
1521 if ((CONSTANT_P (if_info->a) || register_operand (if_info->a, VOIDmode))
1522 && (CONSTANT_P (if_info->b) || register_operand (if_info->b, VOIDmode)))
1523 {
1524 start_sequence ();
1525
1526 code = GET_CODE (if_info->cond);
1527 target = noce_emit_cmove (if_info, if_info->x, code,
1528 XEXP (if_info->cond, 0),
1529 XEXP (if_info->cond, 1),
1530 if_info->a, if_info->b);
1531
1532 if (target)
1533 {
1534 if (target != if_info->x)
1535 noce_emit_move_insn (if_info->x, target);
1536
1537 seq = end_ifcvt_sequence (if_info);
1538 if (!seq)
1539 return FALSE;
1540
1541 emit_insn_before_setloc (seq, if_info->jump,
1542 INSN_LOCATION (if_info->insn_a));
1543 return TRUE;
1544 }
1545 else
1546 {
1547 end_sequence ();
1548 return FALSE;
1549 }
1550 }
1551
1552 return FALSE;
1553 }
1554
1555 /* Try more complex cases involving conditional_move. */
1556
1557 static int
1558 noce_try_cmove_arith (struct noce_if_info *if_info)
1559 {
1560 rtx a = if_info->a;
1561 rtx b = if_info->b;
1562 rtx x = if_info->x;
1563 rtx orig_a, orig_b;
1564 rtx insn_a, insn_b;
1565 rtx tmp, target;
1566 int is_mem = 0;
1567 int insn_cost;
1568 enum rtx_code code;
1569
1570 /* A conditional move from two memory sources is equivalent to a
1571 conditional on their addresses followed by a load. Don't do this
1572 early because it'll screw alias analysis. Note that we've
1573 already checked for no side effects. */
1574 /* ??? FIXME: Magic number 5. */
1575 if (cse_not_expected
1576 && MEM_P (a) && MEM_P (b)
1577 && MEM_ADDR_SPACE (a) == MEM_ADDR_SPACE (b)
1578 && if_info->branch_cost >= 5)
1579 {
1580 enum machine_mode address_mode = get_address_mode (a);
1581
1582 a = XEXP (a, 0);
1583 b = XEXP (b, 0);
1584 x = gen_reg_rtx (address_mode);
1585 is_mem = 1;
1586 }
1587
1588 /* ??? We could handle this if we knew that a load from A or B could
1589 not trap or fault. This is also true if we've already loaded
1590 from the address along the path from ENTRY. */
1591 else if (may_trap_or_fault_p (a) || may_trap_or_fault_p (b))
1592 return FALSE;
1593
1594 /* if (test) x = a + b; else x = c - d;
1595 => y = a + b;
1596 x = c - d;
1597 if (test)
1598 x = y;
1599 */
1600
1601 code = GET_CODE (if_info->cond);
1602 insn_a = if_info->insn_a;
1603 insn_b = if_info->insn_b;
1604
1605 /* Total insn_rtx_cost should be smaller than branch cost. Exit
1606 if insn_rtx_cost can't be estimated. */
1607 if (insn_a)
1608 {
1609 insn_cost
1610 = insn_rtx_cost (PATTERN (insn_a),
1611 optimize_bb_for_speed_p (BLOCK_FOR_INSN (insn_a)));
1612 if (insn_cost == 0 || insn_cost > COSTS_N_INSNS (if_info->branch_cost))
1613 return FALSE;
1614 }
1615 else
1616 insn_cost = 0;
1617
1618 if (insn_b)
1619 {
1620 insn_cost
1621 += insn_rtx_cost (PATTERN (insn_b),
1622 optimize_bb_for_speed_p (BLOCK_FOR_INSN (insn_b)));
1623 if (insn_cost == 0 || insn_cost > COSTS_N_INSNS (if_info->branch_cost))
1624 return FALSE;
1625 }
1626
1627 /* Possibly rearrange operands to make things come out more natural. */
1628 if (reversed_comparison_code (if_info->cond, if_info->jump) != UNKNOWN)
1629 {
1630 int reversep = 0;
1631 if (rtx_equal_p (b, x))
1632 reversep = 1;
1633 else if (general_operand (b, GET_MODE (b)))
1634 reversep = 1;
1635
1636 if (reversep)
1637 {
1638 code = reversed_comparison_code (if_info->cond, if_info->jump);
1639 tmp = a, a = b, b = tmp;
1640 tmp = insn_a, insn_a = insn_b, insn_b = tmp;
1641 }
1642 }
1643
1644 start_sequence ();
1645
1646 orig_a = a;
1647 orig_b = b;
1648
1649 /* If either operand is complex, load it into a register first.
1650 The best way to do this is to copy the original insn. In this
1651 way we preserve any clobbers etc that the insn may have had.
1652 This is of course not possible in the IS_MEM case. */
1653 if (! general_operand (a, GET_MODE (a)))
1654 {
1655 rtx set;
1656
1657 if (is_mem)
1658 {
1659 tmp = gen_reg_rtx (GET_MODE (a));
1660 tmp = emit_insn (gen_rtx_SET (VOIDmode, tmp, a));
1661 }
1662 else if (! insn_a)
1663 goto end_seq_and_fail;
1664 else
1665 {
1666 a = gen_reg_rtx (GET_MODE (a));
1667 tmp = copy_rtx (insn_a);
1668 set = single_set (tmp);
1669 SET_DEST (set) = a;
1670 tmp = emit_insn (PATTERN (tmp));
1671 }
1672 if (recog_memoized (tmp) < 0)
1673 goto end_seq_and_fail;
1674 }
1675 if (! general_operand (b, GET_MODE (b)))
1676 {
1677 rtx set, last;
1678
1679 if (is_mem)
1680 {
1681 tmp = gen_reg_rtx (GET_MODE (b));
1682 tmp = gen_rtx_SET (VOIDmode, tmp, b);
1683 }
1684 else if (! insn_b)
1685 goto end_seq_and_fail;
1686 else
1687 {
1688 b = gen_reg_rtx (GET_MODE (b));
1689 tmp = copy_rtx (insn_b);
1690 set = single_set (tmp);
1691 SET_DEST (set) = b;
1692 tmp = PATTERN (tmp);
1693 }
1694
1695 /* If insn to set up A clobbers any registers B depends on, try to
1696 swap insn that sets up A with the one that sets up B. If even
1697 that doesn't help, punt. */
1698 last = get_last_insn ();
1699 if (last && modified_in_p (orig_b, last))
1700 {
1701 tmp = emit_insn_before (tmp, get_insns ());
1702 if (modified_in_p (orig_a, tmp))
1703 goto end_seq_and_fail;
1704 }
1705 else
1706 tmp = emit_insn (tmp);
1707
1708 if (recog_memoized (tmp) < 0)
1709 goto end_seq_and_fail;
1710 }
1711
1712 target = noce_emit_cmove (if_info, x, code, XEXP (if_info->cond, 0),
1713 XEXP (if_info->cond, 1), a, b);
1714
1715 if (! target)
1716 goto end_seq_and_fail;
1717
1718 /* If we're handling a memory for above, emit the load now. */
1719 if (is_mem)
1720 {
1721 tmp = gen_rtx_MEM (GET_MODE (if_info->x), target);
1722
1723 /* Copy over flags as appropriate. */
1724 if (MEM_VOLATILE_P (if_info->a) || MEM_VOLATILE_P (if_info->b))
1725 MEM_VOLATILE_P (tmp) = 1;
1726 if (MEM_ALIAS_SET (if_info->a) == MEM_ALIAS_SET (if_info->b))
1727 set_mem_alias_set (tmp, MEM_ALIAS_SET (if_info->a));
1728 set_mem_align (tmp,
1729 MIN (MEM_ALIGN (if_info->a), MEM_ALIGN (if_info->b)));
1730
1731 gcc_assert (MEM_ADDR_SPACE (if_info->a) == MEM_ADDR_SPACE (if_info->b));
1732 set_mem_addr_space (tmp, MEM_ADDR_SPACE (if_info->a));
1733
1734 noce_emit_move_insn (if_info->x, tmp);
1735 }
1736 else if (target != x)
1737 noce_emit_move_insn (x, target);
1738
1739 tmp = end_ifcvt_sequence (if_info);
1740 if (!tmp)
1741 return FALSE;
1742
1743 emit_insn_before_setloc (tmp, if_info->jump, INSN_LOCATION (if_info->insn_a));
1744 return TRUE;
1745
1746 end_seq_and_fail:
1747 end_sequence ();
1748 return FALSE;
1749 }
1750
1751 /* For most cases, the simplified condition we found is the best
1752 choice, but this is not the case for the min/max/abs transforms.
1753 For these we wish to know that it is A or B in the condition. */
1754
1755 static rtx
1756 noce_get_alt_condition (struct noce_if_info *if_info, rtx target,
1757 rtx_insn **earliest)
1758 {
1759 rtx cond, set;
1760 rtx_insn *insn;
1761 int reverse;
1762
1763 /* If target is already mentioned in the known condition, return it. */
1764 if (reg_mentioned_p (target, if_info->cond))
1765 {
1766 *earliest = if_info->cond_earliest;
1767 return if_info->cond;
1768 }
1769
1770 set = pc_set (if_info->jump);
1771 cond = XEXP (SET_SRC (set), 0);
1772 reverse
1773 = GET_CODE (XEXP (SET_SRC (set), 2)) == LABEL_REF
1774 && XEXP (XEXP (SET_SRC (set), 2), 0) == JUMP_LABEL (if_info->jump);
1775 if (if_info->then_else_reversed)
1776 reverse = !reverse;
1777
1778 /* If we're looking for a constant, try to make the conditional
1779 have that constant in it. There are two reasons why it may
1780 not have the constant we want:
1781
1782 1. GCC may have needed to put the constant in a register, because
1783 the target can't compare directly against that constant. For
1784 this case, we look for a SET immediately before the comparison
1785 that puts a constant in that register.
1786
1787 2. GCC may have canonicalized the conditional, for example
1788 replacing "if x < 4" with "if x <= 3". We can undo that (or
1789 make equivalent types of changes) to get the constants we need
1790 if they're off by one in the right direction. */
1791
1792 if (CONST_INT_P (target))
1793 {
1794 enum rtx_code code = GET_CODE (if_info->cond);
1795 rtx op_a = XEXP (if_info->cond, 0);
1796 rtx op_b = XEXP (if_info->cond, 1);
1797 rtx prev_insn;
1798
1799 /* First, look to see if we put a constant in a register. */
1800 prev_insn = prev_nonnote_insn (if_info->cond_earliest);
1801 if (prev_insn
1802 && BLOCK_FOR_INSN (prev_insn)
1803 == BLOCK_FOR_INSN (if_info->cond_earliest)
1804 && INSN_P (prev_insn)
1805 && GET_CODE (PATTERN (prev_insn)) == SET)
1806 {
1807 rtx src = find_reg_equal_equiv_note (prev_insn);
1808 if (!src)
1809 src = SET_SRC (PATTERN (prev_insn));
1810 if (CONST_INT_P (src))
1811 {
1812 if (rtx_equal_p (op_a, SET_DEST (PATTERN (prev_insn))))
1813 op_a = src;
1814 else if (rtx_equal_p (op_b, SET_DEST (PATTERN (prev_insn))))
1815 op_b = src;
1816
1817 if (CONST_INT_P (op_a))
1818 {
1819 rtx tmp = op_a;
1820 op_a = op_b;
1821 op_b = tmp;
1822 code = swap_condition (code);
1823 }
1824 }
1825 }
1826
1827 /* Now, look to see if we can get the right constant by
1828 adjusting the conditional. */
1829 if (CONST_INT_P (op_b))
1830 {
1831 HOST_WIDE_INT desired_val = INTVAL (target);
1832 HOST_WIDE_INT actual_val = INTVAL (op_b);
1833
1834 switch (code)
1835 {
1836 case LT:
1837 if (actual_val == desired_val + 1)
1838 {
1839 code = LE;
1840 op_b = GEN_INT (desired_val);
1841 }
1842 break;
1843 case LE:
1844 if (actual_val == desired_val - 1)
1845 {
1846 code = LT;
1847 op_b = GEN_INT (desired_val);
1848 }
1849 break;
1850 case GT:
1851 if (actual_val == desired_val - 1)
1852 {
1853 code = GE;
1854 op_b = GEN_INT (desired_val);
1855 }
1856 break;
1857 case GE:
1858 if (actual_val == desired_val + 1)
1859 {
1860 code = GT;
1861 op_b = GEN_INT (desired_val);
1862 }
1863 break;
1864 default:
1865 break;
1866 }
1867 }
1868
1869 /* If we made any changes, generate a new conditional that is
1870 equivalent to what we started with, but has the right
1871 constants in it. */
1872 if (code != GET_CODE (if_info->cond)
1873 || op_a != XEXP (if_info->cond, 0)
1874 || op_b != XEXP (if_info->cond, 1))
1875 {
1876 cond = gen_rtx_fmt_ee (code, GET_MODE (cond), op_a, op_b);
1877 *earliest = if_info->cond_earliest;
1878 return cond;
1879 }
1880 }
1881
1882 cond = canonicalize_condition (if_info->jump, cond, reverse,
1883 earliest, target, false, true);
1884 if (! cond || ! reg_mentioned_p (target, cond))
1885 return NULL;
1886
1887 /* We almost certainly searched back to a different place.
1888 Need to re-verify correct lifetimes. */
1889
1890 /* X may not be mentioned in the range (cond_earliest, jump]. */
1891 for (insn = if_info->jump; insn != *earliest; insn = PREV_INSN (insn))
1892 if (INSN_P (insn) && reg_overlap_mentioned_p (if_info->x, PATTERN (insn)))
1893 return NULL;
1894
1895 /* A and B may not be modified in the range [cond_earliest, jump). */
1896 for (insn = *earliest; insn != if_info->jump; insn = NEXT_INSN (insn))
1897 if (INSN_P (insn)
1898 && (modified_in_p (if_info->a, insn)
1899 || modified_in_p (if_info->b, insn)))
1900 return NULL;
1901
1902 return cond;
1903 }
1904
1905 /* Convert "if (a < b) x = a; else x = b;" to "x = min(a, b);", etc. */
1906
1907 static int
1908 noce_try_minmax (struct noce_if_info *if_info)
1909 {
1910 rtx cond, target;
1911 rtx_insn *earliest, *seq;
1912 enum rtx_code code, op;
1913 int unsignedp;
1914
1915 /* ??? Reject modes with NaNs or signed zeros since we don't know how
1916 they will be resolved with an SMIN/SMAX. It wouldn't be too hard
1917 to get the target to tell us... */
1918 if (HONOR_SIGNED_ZEROS (GET_MODE (if_info->x))
1919 || HONOR_NANS (GET_MODE (if_info->x)))
1920 return FALSE;
1921
1922 cond = noce_get_alt_condition (if_info, if_info->a, &earliest);
1923 if (!cond)
1924 return FALSE;
1925
1926 /* Verify the condition is of the form we expect, and canonicalize
1927 the comparison code. */
1928 code = GET_CODE (cond);
1929 if (rtx_equal_p (XEXP (cond, 0), if_info->a))
1930 {
1931 if (! rtx_equal_p (XEXP (cond, 1), if_info->b))
1932 return FALSE;
1933 }
1934 else if (rtx_equal_p (XEXP (cond, 1), if_info->a))
1935 {
1936 if (! rtx_equal_p (XEXP (cond, 0), if_info->b))
1937 return FALSE;
1938 code = swap_condition (code);
1939 }
1940 else
1941 return FALSE;
1942
1943 /* Determine what sort of operation this is. Note that the code is for
1944 a taken branch, so the code->operation mapping appears backwards. */
1945 switch (code)
1946 {
1947 case LT:
1948 case LE:
1949 case UNLT:
1950 case UNLE:
1951 op = SMAX;
1952 unsignedp = 0;
1953 break;
1954 case GT:
1955 case GE:
1956 case UNGT:
1957 case UNGE:
1958 op = SMIN;
1959 unsignedp = 0;
1960 break;
1961 case LTU:
1962 case LEU:
1963 op = UMAX;
1964 unsignedp = 1;
1965 break;
1966 case GTU:
1967 case GEU:
1968 op = UMIN;
1969 unsignedp = 1;
1970 break;
1971 default:
1972 return FALSE;
1973 }
1974
1975 start_sequence ();
1976
1977 target = expand_simple_binop (GET_MODE (if_info->x), op,
1978 if_info->a, if_info->b,
1979 if_info->x, unsignedp, OPTAB_WIDEN);
1980 if (! target)
1981 {
1982 end_sequence ();
1983 return FALSE;
1984 }
1985 if (target != if_info->x)
1986 noce_emit_move_insn (if_info->x, target);
1987
1988 seq = end_ifcvt_sequence (if_info);
1989 if (!seq)
1990 return FALSE;
1991
1992 emit_insn_before_setloc (seq, if_info->jump, INSN_LOCATION (if_info->insn_a));
1993 if_info->cond = cond;
1994 if_info->cond_earliest = earliest;
1995
1996 return TRUE;
1997 }
1998
1999 /* Convert "if (a < 0) x = -a; else x = a;" to "x = abs(a);",
2000 "if (a < 0) x = ~a; else x = a;" to "x = one_cmpl_abs(a);",
2001 etc. */
2002
2003 static int
2004 noce_try_abs (struct noce_if_info *if_info)
2005 {
2006 rtx cond, target, a, b, c;
2007 rtx_insn *earliest, *seq;
2008 int negate;
2009 bool one_cmpl = false;
2010
2011 /* Reject modes with signed zeros. */
2012 if (HONOR_SIGNED_ZEROS (GET_MODE (if_info->x)))
2013 return FALSE;
2014
2015 /* Recognize A and B as constituting an ABS or NABS. The canonical
2016 form is a branch around the negation, taken when the object is the
2017 first operand of a comparison against 0 that evaluates to true. */
2018 a = if_info->a;
2019 b = if_info->b;
2020 if (GET_CODE (a) == NEG && rtx_equal_p (XEXP (a, 0), b))
2021 negate = 0;
2022 else if (GET_CODE (b) == NEG && rtx_equal_p (XEXP (b, 0), a))
2023 {
2024 c = a; a = b; b = c;
2025 negate = 1;
2026 }
2027 else if (GET_CODE (a) == NOT && rtx_equal_p (XEXP (a, 0), b))
2028 {
2029 negate = 0;
2030 one_cmpl = true;
2031 }
2032 else if (GET_CODE (b) == NOT && rtx_equal_p (XEXP (b, 0), a))
2033 {
2034 c = a; a = b; b = c;
2035 negate = 1;
2036 one_cmpl = true;
2037 }
2038 else
2039 return FALSE;
2040
2041 cond = noce_get_alt_condition (if_info, b, &earliest);
2042 if (!cond)
2043 return FALSE;
2044
2045 /* Verify the condition is of the form we expect. */
2046 if (rtx_equal_p (XEXP (cond, 0), b))
2047 c = XEXP (cond, 1);
2048 else if (rtx_equal_p (XEXP (cond, 1), b))
2049 {
2050 c = XEXP (cond, 0);
2051 negate = !negate;
2052 }
2053 else
2054 return FALSE;
2055
2056 /* Verify that C is zero. Search one step backward for a
2057 REG_EQUAL note or a simple source if necessary. */
2058 if (REG_P (c))
2059 {
2060 rtx set, insn = prev_nonnote_insn (earliest);
2061 if (insn
2062 && BLOCK_FOR_INSN (insn) == BLOCK_FOR_INSN (earliest)
2063 && (set = single_set (insn))
2064 && rtx_equal_p (SET_DEST (set), c))
2065 {
2066 rtx note = find_reg_equal_equiv_note (insn);
2067 if (note)
2068 c = XEXP (note, 0);
2069 else
2070 c = SET_SRC (set);
2071 }
2072 else
2073 return FALSE;
2074 }
2075 if (MEM_P (c)
2076 && GET_CODE (XEXP (c, 0)) == SYMBOL_REF
2077 && CONSTANT_POOL_ADDRESS_P (XEXP (c, 0)))
2078 c = get_pool_constant (XEXP (c, 0));
2079
2080 /* Work around funny ideas get_condition has wrt canonicalization.
2081 Note that these rtx constants are known to be CONST_INT, and
2082 therefore imply integer comparisons. */
2083 if (c == constm1_rtx && GET_CODE (cond) == GT)
2084 ;
2085 else if (c == const1_rtx && GET_CODE (cond) == LT)
2086 ;
2087 else if (c != CONST0_RTX (GET_MODE (b)))
2088 return FALSE;
2089
2090 /* Determine what sort of operation this is. */
2091 switch (GET_CODE (cond))
2092 {
2093 case LT:
2094 case LE:
2095 case UNLT:
2096 case UNLE:
2097 negate = !negate;
2098 break;
2099 case GT:
2100 case GE:
2101 case UNGT:
2102 case UNGE:
2103 break;
2104 default:
2105 return FALSE;
2106 }
2107
2108 start_sequence ();
2109 if (one_cmpl)
2110 target = expand_one_cmpl_abs_nojump (GET_MODE (if_info->x), b,
2111 if_info->x);
2112 else
2113 target = expand_abs_nojump (GET_MODE (if_info->x), b, if_info->x, 1);
2114
2115 /* ??? It's a quandary whether cmove would be better here, especially
2116 for integers. Perhaps combine will clean things up. */
2117 if (target && negate)
2118 {
2119 if (one_cmpl)
2120 target = expand_simple_unop (GET_MODE (target), NOT, target,
2121 if_info->x, 0);
2122 else
2123 target = expand_simple_unop (GET_MODE (target), NEG, target,
2124 if_info->x, 0);
2125 }
2126
2127 if (! target)
2128 {
2129 end_sequence ();
2130 return FALSE;
2131 }
2132
2133 if (target != if_info->x)
2134 noce_emit_move_insn (if_info->x, target);
2135
2136 seq = end_ifcvt_sequence (if_info);
2137 if (!seq)
2138 return FALSE;
2139
2140 emit_insn_before_setloc (seq, if_info->jump, INSN_LOCATION (if_info->insn_a));
2141 if_info->cond = cond;
2142 if_info->cond_earliest = earliest;
2143
2144 return TRUE;
2145 }
2146
2147 /* Convert "if (m < 0) x = b; else x = 0;" to "x = (m >> C) & b;". */
2148
2149 static int
2150 noce_try_sign_mask (struct noce_if_info *if_info)
2151 {
2152 rtx cond, t, m, c;
2153 rtx_insn *seq;
2154 enum machine_mode mode;
2155 enum rtx_code code;
2156 bool t_unconditional;
2157
2158 cond = if_info->cond;
2159 code = GET_CODE (cond);
2160 m = XEXP (cond, 0);
2161 c = XEXP (cond, 1);
2162
2163 t = NULL_RTX;
2164 if (if_info->a == const0_rtx)
2165 {
2166 if ((code == LT && c == const0_rtx)
2167 || (code == LE && c == constm1_rtx))
2168 t = if_info->b;
2169 }
2170 else if (if_info->b == const0_rtx)
2171 {
2172 if ((code == GE && c == const0_rtx)
2173 || (code == GT && c == constm1_rtx))
2174 t = if_info->a;
2175 }
2176
2177 if (! t || side_effects_p (t))
2178 return FALSE;
2179
2180 /* We currently don't handle different modes. */
2181 mode = GET_MODE (t);
2182 if (GET_MODE (m) != mode)
2183 return FALSE;
2184
2185 /* This is only profitable if T is unconditionally executed/evaluated in the
2186 original insn sequence or T is cheap. The former happens if B is the
2187 non-zero (T) value and if INSN_B was taken from TEST_BB, or there was no
2188 INSN_B which can happen for e.g. conditional stores to memory. For the
2189 cost computation use the block TEST_BB where the evaluation will end up
2190 after the transformation. */
2191 t_unconditional =
2192 (t == if_info->b
2193 && (if_info->insn_b == NULL_RTX
2194 || BLOCK_FOR_INSN (if_info->insn_b) == if_info->test_bb));
2195 if (!(t_unconditional
2196 || (set_src_cost (t, optimize_bb_for_speed_p (if_info->test_bb))
2197 < COSTS_N_INSNS (2))))
2198 return FALSE;
2199
2200 start_sequence ();
2201 /* Use emit_store_flag to generate "m < 0 ? -1 : 0" instead of expanding
2202 "(signed) m >> 31" directly. This benefits targets with specialized
2203 insns to obtain the signmask, but still uses ashr_optab otherwise. */
2204 m = emit_store_flag (gen_reg_rtx (mode), LT, m, const0_rtx, mode, 0, -1);
2205 t = m ? expand_binop (mode, and_optab, m, t, NULL_RTX, 0, OPTAB_DIRECT)
2206 : NULL_RTX;
2207
2208 if (!t)
2209 {
2210 end_sequence ();
2211 return FALSE;
2212 }
2213
2214 noce_emit_move_insn (if_info->x, t);
2215
2216 seq = end_ifcvt_sequence (if_info);
2217 if (!seq)
2218 return FALSE;
2219
2220 emit_insn_before_setloc (seq, if_info->jump, INSN_LOCATION (if_info->insn_a));
2221 return TRUE;
2222 }
2223
2224
2225 /* Optimize away "if (x & C) x |= C" and similar bit manipulation
2226 transformations. */
2227
2228 static int
2229 noce_try_bitop (struct noce_if_info *if_info)
2230 {
2231 rtx cond, x, a, result;
2232 rtx_insn *seq;
2233 enum machine_mode mode;
2234 enum rtx_code code;
2235 int bitnum;
2236
2237 x = if_info->x;
2238 cond = if_info->cond;
2239 code = GET_CODE (cond);
2240
2241 /* Check for no else condition. */
2242 if (! rtx_equal_p (x, if_info->b))
2243 return FALSE;
2244
2245 /* Check for a suitable condition. */
2246 if (code != NE && code != EQ)
2247 return FALSE;
2248 if (XEXP (cond, 1) != const0_rtx)
2249 return FALSE;
2250 cond = XEXP (cond, 0);
2251
2252 /* ??? We could also handle AND here. */
2253 if (GET_CODE (cond) == ZERO_EXTRACT)
2254 {
2255 if (XEXP (cond, 1) != const1_rtx
2256 || !CONST_INT_P (XEXP (cond, 2))
2257 || ! rtx_equal_p (x, XEXP (cond, 0)))
2258 return FALSE;
2259 bitnum = INTVAL (XEXP (cond, 2));
2260 mode = GET_MODE (x);
2261 if (BITS_BIG_ENDIAN)
2262 bitnum = GET_MODE_BITSIZE (mode) - 1 - bitnum;
2263 if (bitnum < 0 || bitnum >= HOST_BITS_PER_WIDE_INT)
2264 return FALSE;
2265 }
2266 else
2267 return FALSE;
2268
2269 a = if_info->a;
2270 if (GET_CODE (a) == IOR || GET_CODE (a) == XOR)
2271 {
2272 /* Check for "if (X & C) x = x op C". */
2273 if (! rtx_equal_p (x, XEXP (a, 0))
2274 || !CONST_INT_P (XEXP (a, 1))
2275 || (INTVAL (XEXP (a, 1)) & GET_MODE_MASK (mode))
2276 != (unsigned HOST_WIDE_INT) 1 << bitnum)
2277 return FALSE;
2278
2279 /* if ((x & C) == 0) x |= C; is transformed to x |= C. */
2280 /* if ((x & C) != 0) x |= C; is transformed to nothing. */
2281 if (GET_CODE (a) == IOR)
2282 result = (code == NE) ? a : NULL_RTX;
2283 else if (code == NE)
2284 {
2285 /* if ((x & C) == 0) x ^= C; is transformed to x |= C. */
2286 result = gen_int_mode ((HOST_WIDE_INT) 1 << bitnum, mode);
2287 result = simplify_gen_binary (IOR, mode, x, result);
2288 }
2289 else
2290 {
2291 /* if ((x & C) != 0) x ^= C; is transformed to x &= ~C. */
2292 result = gen_int_mode (~((HOST_WIDE_INT) 1 << bitnum), mode);
2293 result = simplify_gen_binary (AND, mode, x, result);
2294 }
2295 }
2296 else if (GET_CODE (a) == AND)
2297 {
2298 /* Check for "if (X & C) x &= ~C". */
2299 if (! rtx_equal_p (x, XEXP (a, 0))
2300 || !CONST_INT_P (XEXP (a, 1))
2301 || (INTVAL (XEXP (a, 1)) & GET_MODE_MASK (mode))
2302 != (~((HOST_WIDE_INT) 1 << bitnum) & GET_MODE_MASK (mode)))
2303 return FALSE;
2304
2305 /* if ((x & C) == 0) x &= ~C; is transformed to nothing. */
2306 /* if ((x & C) != 0) x &= ~C; is transformed to x &= ~C. */
2307 result = (code == EQ) ? a : NULL_RTX;
2308 }
2309 else
2310 return FALSE;
2311
2312 if (result)
2313 {
2314 start_sequence ();
2315 noce_emit_move_insn (x, result);
2316 seq = end_ifcvt_sequence (if_info);
2317 if (!seq)
2318 return FALSE;
2319
2320 emit_insn_before_setloc (seq, if_info->jump,
2321 INSN_LOCATION (if_info->insn_a));
2322 }
2323 return TRUE;
2324 }
2325
2326
2327 /* Similar to get_condition, only the resulting condition must be
2328 valid at JUMP, instead of at EARLIEST.
2329
2330 If THEN_ELSE_REVERSED is true, the fallthrough does not go to the
2331 THEN block of the caller, and we have to reverse the condition. */
2332
2333 static rtx
2334 noce_get_condition (rtx_insn *jump, rtx_insn **earliest, bool then_else_reversed)
2335 {
2336 rtx cond, set, tmp;
2337 bool reverse;
2338
2339 if (! any_condjump_p (jump))
2340 return NULL_RTX;
2341
2342 set = pc_set (jump);
2343
2344 /* If this branches to JUMP_LABEL when the condition is false,
2345 reverse the condition. */
2346 reverse = (GET_CODE (XEXP (SET_SRC (set), 2)) == LABEL_REF
2347 && XEXP (XEXP (SET_SRC (set), 2), 0) == JUMP_LABEL (jump));
2348
2349 /* We may have to reverse because the caller's if block is not canonical,
2350 i.e. the THEN block isn't the fallthrough block for the TEST block
2351 (see find_if_header). */
2352 if (then_else_reversed)
2353 reverse = !reverse;
2354
2355 /* If the condition variable is a register and is MODE_INT, accept it. */
2356
2357 cond = XEXP (SET_SRC (set), 0);
2358 tmp = XEXP (cond, 0);
2359 if (REG_P (tmp) && GET_MODE_CLASS (GET_MODE (tmp)) == MODE_INT
2360 && (GET_MODE (tmp) != BImode
2361 || !targetm.small_register_classes_for_mode_p (BImode)))
2362 {
2363 *earliest = jump;
2364
2365 if (reverse)
2366 cond = gen_rtx_fmt_ee (reverse_condition (GET_CODE (cond)),
2367 GET_MODE (cond), tmp, XEXP (cond, 1));
2368 return cond;
2369 }
2370
2371 /* Otherwise, fall back on canonicalize_condition to do the dirty
2372 work of manipulating MODE_CC values and COMPARE rtx codes. */
2373 tmp = canonicalize_condition (jump, cond, reverse, earliest,
2374 NULL_RTX, false, true);
2375
2376 /* We don't handle side-effects in the condition, like handling
2377 REG_INC notes and making sure no duplicate conditions are emitted. */
2378 if (tmp != NULL_RTX && side_effects_p (tmp))
2379 return NULL_RTX;
2380
2381 return tmp;
2382 }
2383
2384 /* Return true if OP is ok for if-then-else processing. */
2385
2386 static int
2387 noce_operand_ok (const_rtx op)
2388 {
2389 if (side_effects_p (op))
2390 return FALSE;
2391
2392 /* We special-case memories, so handle any of them with
2393 no address side effects. */
2394 if (MEM_P (op))
2395 return ! side_effects_p (XEXP (op, 0));
2396
2397 return ! may_trap_p (op);
2398 }
2399
2400 /* Return true if a write into MEM may trap or fault. */
2401
2402 static bool
2403 noce_mem_write_may_trap_or_fault_p (const_rtx mem)
2404 {
2405 rtx addr;
2406
2407 if (MEM_READONLY_P (mem))
2408 return true;
2409
2410 if (may_trap_or_fault_p (mem))
2411 return true;
2412
2413 addr = XEXP (mem, 0);
2414
2415 /* Call target hook to avoid the effects of -fpic etc.... */
2416 addr = targetm.delegitimize_address (addr);
2417
2418 while (addr)
2419 switch (GET_CODE (addr))
2420 {
2421 case CONST:
2422 case PRE_DEC:
2423 case PRE_INC:
2424 case POST_DEC:
2425 case POST_INC:
2426 case POST_MODIFY:
2427 addr = XEXP (addr, 0);
2428 break;
2429 case LO_SUM:
2430 case PRE_MODIFY:
2431 addr = XEXP (addr, 1);
2432 break;
2433 case PLUS:
2434 if (CONST_INT_P (XEXP (addr, 1)))
2435 addr = XEXP (addr, 0);
2436 else
2437 return false;
2438 break;
2439 case LABEL_REF:
2440 return true;
2441 case SYMBOL_REF:
2442 if (SYMBOL_REF_DECL (addr)
2443 && decl_readonly_section (SYMBOL_REF_DECL (addr), 0))
2444 return true;
2445 return false;
2446 default:
2447 return false;
2448 }
2449
2450 return false;
2451 }
2452
2453 /* Return whether we can use store speculation for MEM. TOP_BB is the
2454 basic block above the conditional block where we are considering
2455 doing the speculative store. We look for whether MEM is set
2456 unconditionally later in the function. */
2457
2458 static bool
2459 noce_can_store_speculate_p (basic_block top_bb, const_rtx mem)
2460 {
2461 basic_block dominator;
2462
2463 for (dominator = get_immediate_dominator (CDI_POST_DOMINATORS, top_bb);
2464 dominator != NULL;
2465 dominator = get_immediate_dominator (CDI_POST_DOMINATORS, dominator))
2466 {
2467 rtx_insn *insn;
2468
2469 FOR_BB_INSNS (dominator, insn)
2470 {
2471 /* If we see something that might be a memory barrier, we
2472 have to stop looking. Even if the MEM is set later in
2473 the function, we still don't want to set it
2474 unconditionally before the barrier. */
2475 if (INSN_P (insn)
2476 && (volatile_insn_p (PATTERN (insn))
2477 || (CALL_P (insn) && (!RTL_CONST_CALL_P (insn)))))
2478 return false;
2479
2480 if (memory_must_be_modified_in_insn_p (mem, insn))
2481 return true;
2482 if (modified_in_p (XEXP (mem, 0), insn))
2483 return false;
2484
2485 }
2486 }
2487
2488 return false;
2489 }
2490
2491 /* Given a simple IF-THEN-JOIN or IF-THEN-ELSE-JOIN block, attempt to convert
2492 it without using conditional execution. Return TRUE if we were successful
2493 at converting the block. */
2494
2495 static int
2496 noce_process_if_block (struct noce_if_info *if_info)
2497 {
2498 basic_block test_bb = if_info->test_bb; /* test block */
2499 basic_block then_bb = if_info->then_bb; /* THEN */
2500 basic_block else_bb = if_info->else_bb; /* ELSE or NULL */
2501 basic_block join_bb = if_info->join_bb; /* JOIN */
2502 rtx jump = if_info->jump;
2503 rtx cond = if_info->cond;
2504 rtx_insn *insn_a, *insn_b;
2505 rtx set_a, set_b;
2506 rtx orig_x, x, a, b;
2507
2508 /* We're looking for patterns of the form
2509
2510 (1) if (...) x = a; else x = b;
2511 (2) x = b; if (...) x = a;
2512 (3) if (...) x = a; // as if with an initial x = x.
2513
2514 The later patterns require jumps to be more expensive.
2515
2516 ??? For future expansion, look for multiple X in such patterns. */
2517
2518 /* Look for one of the potential sets. */
2519 insn_a = first_active_insn (then_bb);
2520 if (! insn_a
2521 || insn_a != last_active_insn (then_bb, FALSE)
2522 || (set_a = single_set (insn_a)) == NULL_RTX)
2523 return FALSE;
2524
2525 x = SET_DEST (set_a);
2526 a = SET_SRC (set_a);
2527
2528 /* Look for the other potential set. Make sure we've got equivalent
2529 destinations. */
2530 /* ??? This is overconservative. Storing to two different mems is
2531 as easy as conditionally computing the address. Storing to a
2532 single mem merely requires a scratch memory to use as one of the
2533 destination addresses; often the memory immediately below the
2534 stack pointer is available for this. */
2535 set_b = NULL_RTX;
2536 if (else_bb)
2537 {
2538 insn_b = first_active_insn (else_bb);
2539 if (! insn_b
2540 || insn_b != last_active_insn (else_bb, FALSE)
2541 || (set_b = single_set (insn_b)) == NULL_RTX
2542 || ! rtx_interchangeable_p (x, SET_DEST (set_b)))
2543 return FALSE;
2544 }
2545 else
2546 {
2547 insn_b = prev_nonnote_nondebug_insn (if_info->cond_earliest);
2548 /* We're going to be moving the evaluation of B down from above
2549 COND_EARLIEST to JUMP. Make sure the relevant data is still
2550 intact. */
2551 if (! insn_b
2552 || BLOCK_FOR_INSN (insn_b) != BLOCK_FOR_INSN (if_info->cond_earliest)
2553 || !NONJUMP_INSN_P (insn_b)
2554 || (set_b = single_set (insn_b)) == NULL_RTX
2555 || ! rtx_interchangeable_p (x, SET_DEST (set_b))
2556 || ! noce_operand_ok (SET_SRC (set_b))
2557 || reg_overlap_mentioned_p (x, SET_SRC (set_b))
2558 || modified_between_p (SET_SRC (set_b), insn_b, jump)
2559 /* Avoid extending the lifetime of hard registers on small
2560 register class machines. */
2561 || (REG_P (SET_SRC (set_b))
2562 && HARD_REGISTER_P (SET_SRC (set_b))
2563 && targetm.small_register_classes_for_mode_p
2564 (GET_MODE (SET_SRC (set_b))))
2565 /* Likewise with X. In particular this can happen when
2566 noce_get_condition looks farther back in the instruction
2567 stream than one might expect. */
2568 || reg_overlap_mentioned_p (x, cond)
2569 || reg_overlap_mentioned_p (x, a)
2570 || modified_between_p (x, insn_b, jump))
2571 {
2572 insn_b = NULL;
2573 set_b = NULL_RTX;
2574 }
2575 }
2576
2577 /* If x has side effects then only the if-then-else form is safe to
2578 convert. But even in that case we would need to restore any notes
2579 (such as REG_INC) at then end. That can be tricky if
2580 noce_emit_move_insn expands to more than one insn, so disable the
2581 optimization entirely for now if there are side effects. */
2582 if (side_effects_p (x))
2583 return FALSE;
2584
2585 b = (set_b ? SET_SRC (set_b) : x);
2586
2587 /* Only operate on register destinations, and even then avoid extending
2588 the lifetime of hard registers on small register class machines. */
2589 orig_x = x;
2590 if (!REG_P (x)
2591 || (HARD_REGISTER_P (x)
2592 && targetm.small_register_classes_for_mode_p (GET_MODE (x))))
2593 {
2594 if (GET_MODE (x) == BLKmode)
2595 return FALSE;
2596
2597 if (GET_CODE (x) == ZERO_EXTRACT
2598 && (!CONST_INT_P (XEXP (x, 1))
2599 || !CONST_INT_P (XEXP (x, 2))))
2600 return FALSE;
2601
2602 x = gen_reg_rtx (GET_MODE (GET_CODE (x) == STRICT_LOW_PART
2603 ? XEXP (x, 0) : x));
2604 }
2605
2606 /* Don't operate on sources that may trap or are volatile. */
2607 if (! noce_operand_ok (a) || ! noce_operand_ok (b))
2608 return FALSE;
2609
2610 retry:
2611 /* Set up the info block for our subroutines. */
2612 if_info->insn_a = insn_a;
2613 if_info->insn_b = insn_b;
2614 if_info->x = x;
2615 if_info->a = a;
2616 if_info->b = b;
2617
2618 /* Try optimizations in some approximation of a useful order. */
2619 /* ??? Should first look to see if X is live incoming at all. If it
2620 isn't, we don't need anything but an unconditional set. */
2621
2622 /* Look and see if A and B are really the same. Avoid creating silly
2623 cmove constructs that no one will fix up later. */
2624 if (rtx_interchangeable_p (a, b))
2625 {
2626 /* If we have an INSN_B, we don't have to create any new rtl. Just
2627 move the instruction that we already have. If we don't have an
2628 INSN_B, that means that A == X, and we've got a noop move. In
2629 that case don't do anything and let the code below delete INSN_A. */
2630 if (insn_b && else_bb)
2631 {
2632 rtx note;
2633
2634 if (else_bb && insn_b == BB_END (else_bb))
2635 BB_END (else_bb) = PREV_INSN (insn_b);
2636 reorder_insns (insn_b, insn_b, PREV_INSN (jump));
2637
2638 /* If there was a REG_EQUAL note, delete it since it may have been
2639 true due to this insn being after a jump. */
2640 if ((note = find_reg_note (insn_b, REG_EQUAL, NULL_RTX)) != 0)
2641 remove_note (insn_b, note);
2642
2643 insn_b = NULL;
2644 }
2645 /* If we have "x = b; if (...) x = a;", and x has side-effects, then
2646 x must be executed twice. */
2647 else if (insn_b && side_effects_p (orig_x))
2648 return FALSE;
2649
2650 x = orig_x;
2651 goto success;
2652 }
2653
2654 if (!set_b && MEM_P (orig_x))
2655 {
2656 /* Disallow the "if (...) x = a;" form (implicit "else x = x;")
2657 for optimizations if writing to x may trap or fault,
2658 i.e. it's a memory other than a static var or a stack slot,
2659 is misaligned on strict aligned machines or is read-only. If
2660 x is a read-only memory, then the program is valid only if we
2661 avoid the store into it. If there are stores on both the
2662 THEN and ELSE arms, then we can go ahead with the conversion;
2663 either the program is broken, or the condition is always
2664 false such that the other memory is selected. */
2665 if (noce_mem_write_may_trap_or_fault_p (orig_x))
2666 return FALSE;
2667
2668 /* Avoid store speculation: given "if (...) x = a" where x is a
2669 MEM, we only want to do the store if x is always set
2670 somewhere in the function. This avoids cases like
2671 if (pthread_mutex_trylock(mutex))
2672 ++global_variable;
2673 where we only want global_variable to be changed if the mutex
2674 is held. FIXME: This should ideally be expressed directly in
2675 RTL somehow. */
2676 if (!noce_can_store_speculate_p (test_bb, orig_x))
2677 return FALSE;
2678 }
2679
2680 if (noce_try_move (if_info))
2681 goto success;
2682 if (noce_try_store_flag (if_info))
2683 goto success;
2684 if (noce_try_bitop (if_info))
2685 goto success;
2686 if (noce_try_minmax (if_info))
2687 goto success;
2688 if (noce_try_abs (if_info))
2689 goto success;
2690 if (HAVE_conditional_move
2691 && noce_try_cmove (if_info))
2692 goto success;
2693 if (! targetm.have_conditional_execution ())
2694 {
2695 if (noce_try_store_flag_constants (if_info))
2696 goto success;
2697 if (noce_try_addcc (if_info))
2698 goto success;
2699 if (noce_try_store_flag_mask (if_info))
2700 goto success;
2701 if (HAVE_conditional_move
2702 && noce_try_cmove_arith (if_info))
2703 goto success;
2704 if (noce_try_sign_mask (if_info))
2705 goto success;
2706 }
2707
2708 if (!else_bb && set_b)
2709 {
2710 insn_b = NULL;
2711 set_b = NULL_RTX;
2712 b = orig_x;
2713 goto retry;
2714 }
2715
2716 return FALSE;
2717
2718 success:
2719
2720 /* If we used a temporary, fix it up now. */
2721 if (orig_x != x)
2722 {
2723 rtx_insn *seq;
2724
2725 start_sequence ();
2726 noce_emit_move_insn (orig_x, x);
2727 seq = get_insns ();
2728 set_used_flags (orig_x);
2729 unshare_all_rtl_in_chain (seq);
2730 end_sequence ();
2731
2732 emit_insn_before_setloc (seq, BB_END (test_bb), INSN_LOCATION (insn_a));
2733 }
2734
2735 /* The original THEN and ELSE blocks may now be removed. The test block
2736 must now jump to the join block. If the test block and the join block
2737 can be merged, do so. */
2738 if (else_bb)
2739 {
2740 delete_basic_block (else_bb);
2741 num_true_changes++;
2742 }
2743 else
2744 remove_edge (find_edge (test_bb, join_bb));
2745
2746 remove_edge (find_edge (then_bb, join_bb));
2747 redirect_edge_and_branch_force (single_succ_edge (test_bb), join_bb);
2748 delete_basic_block (then_bb);
2749 num_true_changes++;
2750
2751 if (can_merge_blocks_p (test_bb, join_bb))
2752 {
2753 merge_blocks (test_bb, join_bb);
2754 num_true_changes++;
2755 }
2756
2757 num_updated_if_blocks++;
2758 return TRUE;
2759 }
2760
2761 /* Check whether a block is suitable for conditional move conversion.
2762 Every insn must be a simple set of a register to a constant or a
2763 register. For each assignment, store the value in the pointer map
2764 VALS, keyed indexed by register pointer, then store the register
2765 pointer in REGS. COND is the condition we will test. */
2766
2767 static int
2768 check_cond_move_block (basic_block bb,
2769 hash_map<rtx, rtx> *vals,
2770 vec<rtx> *regs,
2771 rtx cond)
2772 {
2773 rtx_insn *insn;
2774
2775 /* We can only handle simple jumps at the end of the basic block.
2776 It is almost impossible to update the CFG otherwise. */
2777 insn = BB_END (bb);
2778 if (JUMP_P (insn) && !onlyjump_p (insn))
2779 return FALSE;
2780
2781 FOR_BB_INSNS (bb, insn)
2782 {
2783 rtx set, dest, src;
2784
2785 if (!NONDEBUG_INSN_P (insn) || JUMP_P (insn))
2786 continue;
2787 set = single_set (insn);
2788 if (!set)
2789 return FALSE;
2790
2791 dest = SET_DEST (set);
2792 src = SET_SRC (set);
2793 if (!REG_P (dest)
2794 || (HARD_REGISTER_P (dest)
2795 && targetm.small_register_classes_for_mode_p (GET_MODE (dest))))
2796 return FALSE;
2797
2798 if (!CONSTANT_P (src) && !register_operand (src, VOIDmode))
2799 return FALSE;
2800
2801 if (side_effects_p (src) || side_effects_p (dest))
2802 return FALSE;
2803
2804 if (may_trap_p (src) || may_trap_p (dest))
2805 return FALSE;
2806
2807 /* Don't try to handle this if the source register was
2808 modified earlier in the block. */
2809 if ((REG_P (src)
2810 && vals->get (src))
2811 || (GET_CODE (src) == SUBREG && REG_P (SUBREG_REG (src))
2812 && vals->get (SUBREG_REG (src))))
2813 return FALSE;
2814
2815 /* Don't try to handle this if the destination register was
2816 modified earlier in the block. */
2817 if (vals->get (dest))
2818 return FALSE;
2819
2820 /* Don't try to handle this if the condition uses the
2821 destination register. */
2822 if (reg_overlap_mentioned_p (dest, cond))
2823 return FALSE;
2824
2825 /* Don't try to handle this if the source register is modified
2826 later in the block. */
2827 if (!CONSTANT_P (src)
2828 && modified_between_p (src, insn, NEXT_INSN (BB_END (bb))))
2829 return FALSE;
2830
2831 vals->put (dest, src);
2832
2833 regs->safe_push (dest);
2834 }
2835
2836 return TRUE;
2837 }
2838
2839 /* Given a basic block BB suitable for conditional move conversion,
2840 a condition COND, and pointer maps THEN_VALS and ELSE_VALS containing
2841 the register values depending on COND, emit the insns in the block as
2842 conditional moves. If ELSE_BLOCK is true, THEN_BB was already
2843 processed. The caller has started a sequence for the conversion.
2844 Return true if successful, false if something goes wrong. */
2845
2846 static bool
2847 cond_move_convert_if_block (struct noce_if_info *if_infop,
2848 basic_block bb, rtx cond,
2849 hash_map<rtx, rtx> *then_vals,
2850 hash_map<rtx, rtx> *else_vals,
2851 bool else_block_p)
2852 {
2853 enum rtx_code code;
2854 rtx_insn *insn;
2855 rtx cond_arg0, cond_arg1;
2856
2857 code = GET_CODE (cond);
2858 cond_arg0 = XEXP (cond, 0);
2859 cond_arg1 = XEXP (cond, 1);
2860
2861 FOR_BB_INSNS (bb, insn)
2862 {
2863 rtx set, target, dest, t, e;
2864
2865 /* ??? Maybe emit conditional debug insn? */
2866 if (!NONDEBUG_INSN_P (insn) || JUMP_P (insn))
2867 continue;
2868 set = single_set (insn);
2869 gcc_assert (set && REG_P (SET_DEST (set)));
2870
2871 dest = SET_DEST (set);
2872
2873 rtx *then_slot = then_vals->get (dest);
2874 rtx *else_slot = else_vals->get (dest);
2875 t = then_slot ? *then_slot : NULL_RTX;
2876 e = else_slot ? *else_slot : NULL_RTX;
2877
2878 if (else_block_p)
2879 {
2880 /* If this register was set in the then block, we already
2881 handled this case there. */
2882 if (t)
2883 continue;
2884 t = dest;
2885 gcc_assert (e);
2886 }
2887 else
2888 {
2889 gcc_assert (t);
2890 if (!e)
2891 e = dest;
2892 }
2893
2894 target = noce_emit_cmove (if_infop, dest, code, cond_arg0, cond_arg1,
2895 t, e);
2896 if (!target)
2897 return false;
2898
2899 if (target != dest)
2900 noce_emit_move_insn (dest, target);
2901 }
2902
2903 return true;
2904 }
2905
2906 /* Given a simple IF-THEN-JOIN or IF-THEN-ELSE-JOIN block, attempt to convert
2907 it using only conditional moves. Return TRUE if we were successful at
2908 converting the block. */
2909
2910 static int
2911 cond_move_process_if_block (struct noce_if_info *if_info)
2912 {
2913 basic_block test_bb = if_info->test_bb;
2914 basic_block then_bb = if_info->then_bb;
2915 basic_block else_bb = if_info->else_bb;
2916 basic_block join_bb = if_info->join_bb;
2917 rtx jump = if_info->jump;
2918 rtx cond = if_info->cond;
2919 rtx_insn *seq, *loc_insn;
2920 rtx reg;
2921 int c;
2922 vec<rtx> then_regs = vNULL;
2923 vec<rtx> else_regs = vNULL;
2924 unsigned int i;
2925 int success_p = FALSE;
2926
2927 /* Build a mapping for each block to the value used for each
2928 register. */
2929 hash_map<rtx, rtx> then_vals;
2930 hash_map<rtx, rtx> else_vals;
2931
2932 /* Make sure the blocks are suitable. */
2933 if (!check_cond_move_block (then_bb, &then_vals, &then_regs, cond)
2934 || (else_bb
2935 && !check_cond_move_block (else_bb, &else_vals, &else_regs, cond)))
2936 goto done;
2937
2938 /* Make sure the blocks can be used together. If the same register
2939 is set in both blocks, and is not set to a constant in both
2940 cases, then both blocks must set it to the same register. We
2941 have already verified that if it is set to a register, that the
2942 source register does not change after the assignment. Also count
2943 the number of registers set in only one of the blocks. */
2944 c = 0;
2945 FOR_EACH_VEC_ELT (then_regs, i, reg)
2946 {
2947 rtx *then_slot = then_vals.get (reg);
2948 rtx *else_slot = else_vals.get (reg);
2949
2950 gcc_checking_assert (then_slot);
2951 if (!else_slot)
2952 ++c;
2953 else
2954 {
2955 rtx then_val = *then_slot;
2956 rtx else_val = *else_slot;
2957 if (!CONSTANT_P (then_val) && !CONSTANT_P (else_val)
2958 && !rtx_equal_p (then_val, else_val))
2959 goto done;
2960 }
2961 }
2962
2963 /* Finish off c for MAX_CONDITIONAL_EXECUTE. */
2964 FOR_EACH_VEC_ELT (else_regs, i, reg)
2965 {
2966 gcc_checking_assert (else_vals.get (reg));
2967 if (!then_vals.get (reg))
2968 ++c;
2969 }
2970
2971 /* Make sure it is reasonable to convert this block. What matters
2972 is the number of assignments currently made in only one of the
2973 branches, since if we convert we are going to always execute
2974 them. */
2975 if (c > MAX_CONDITIONAL_EXECUTE)
2976 goto done;
2977
2978 /* Try to emit the conditional moves. First do the then block,
2979 then do anything left in the else blocks. */
2980 start_sequence ();
2981 if (!cond_move_convert_if_block (if_info, then_bb, cond,
2982 &then_vals, &else_vals, false)
2983 || (else_bb
2984 && !cond_move_convert_if_block (if_info, else_bb, cond,
2985 &then_vals, &else_vals, true)))
2986 {
2987 end_sequence ();
2988 goto done;
2989 }
2990 seq = end_ifcvt_sequence (if_info);
2991 if (!seq)
2992 goto done;
2993
2994 loc_insn = first_active_insn (then_bb);
2995 if (!loc_insn)
2996 {
2997 loc_insn = first_active_insn (else_bb);
2998 gcc_assert (loc_insn);
2999 }
3000 emit_insn_before_setloc (seq, jump, INSN_LOCATION (loc_insn));
3001
3002 if (else_bb)
3003 {
3004 delete_basic_block (else_bb);
3005 num_true_changes++;
3006 }
3007 else
3008 remove_edge (find_edge (test_bb, join_bb));
3009
3010 remove_edge (find_edge (then_bb, join_bb));
3011 redirect_edge_and_branch_force (single_succ_edge (test_bb), join_bb);
3012 delete_basic_block (then_bb);
3013 num_true_changes++;
3014
3015 if (can_merge_blocks_p (test_bb, join_bb))
3016 {
3017 merge_blocks (test_bb, join_bb);
3018 num_true_changes++;
3019 }
3020
3021 num_updated_if_blocks++;
3022
3023 success_p = TRUE;
3024
3025 done:
3026 then_regs.release ();
3027 else_regs.release ();
3028 return success_p;
3029 }
3030
3031 \f
3032 /* Determine if a given basic block heads a simple IF-THEN-JOIN or an
3033 IF-THEN-ELSE-JOIN block.
3034
3035 If so, we'll try to convert the insns to not require the branch,
3036 using only transformations that do not require conditional execution.
3037
3038 Return TRUE if we were successful at converting the block. */
3039
3040 static int
3041 noce_find_if_block (basic_block test_bb, edge then_edge, edge else_edge,
3042 int pass)
3043 {
3044 basic_block then_bb, else_bb, join_bb;
3045 bool then_else_reversed = false;
3046 rtx_insn *jump;
3047 rtx cond;
3048 rtx_insn *cond_earliest;
3049 struct noce_if_info if_info;
3050
3051 /* We only ever should get here before reload. */
3052 gcc_assert (!reload_completed);
3053
3054 /* Recognize an IF-THEN-ELSE-JOIN block. */
3055 if (single_pred_p (then_edge->dest)
3056 && single_succ_p (then_edge->dest)
3057 && single_pred_p (else_edge->dest)
3058 && single_succ_p (else_edge->dest)
3059 && single_succ (then_edge->dest) == single_succ (else_edge->dest))
3060 {
3061 then_bb = then_edge->dest;
3062 else_bb = else_edge->dest;
3063 join_bb = single_succ (then_bb);
3064 }
3065 /* Recognize an IF-THEN-JOIN block. */
3066 else if (single_pred_p (then_edge->dest)
3067 && single_succ_p (then_edge->dest)
3068 && single_succ (then_edge->dest) == else_edge->dest)
3069 {
3070 then_bb = then_edge->dest;
3071 else_bb = NULL_BLOCK;
3072 join_bb = else_edge->dest;
3073 }
3074 /* Recognize an IF-ELSE-JOIN block. We can have those because the order
3075 of basic blocks in cfglayout mode does not matter, so the fallthrough
3076 edge can go to any basic block (and not just to bb->next_bb, like in
3077 cfgrtl mode). */
3078 else if (single_pred_p (else_edge->dest)
3079 && single_succ_p (else_edge->dest)
3080 && single_succ (else_edge->dest) == then_edge->dest)
3081 {
3082 /* The noce transformations do not apply to IF-ELSE-JOIN blocks.
3083 To make this work, we have to invert the THEN and ELSE blocks
3084 and reverse the jump condition. */
3085 then_bb = else_edge->dest;
3086 else_bb = NULL_BLOCK;
3087 join_bb = single_succ (then_bb);
3088 then_else_reversed = true;
3089 }
3090 else
3091 /* Not a form we can handle. */
3092 return FALSE;
3093
3094 /* The edges of the THEN and ELSE blocks cannot have complex edges. */
3095 if (single_succ_edge (then_bb)->flags & EDGE_COMPLEX)
3096 return FALSE;
3097 if (else_bb
3098 && single_succ_edge (else_bb)->flags & EDGE_COMPLEX)
3099 return FALSE;
3100
3101 num_possible_if_blocks++;
3102
3103 if (dump_file)
3104 {
3105 fprintf (dump_file,
3106 "\nIF-THEN%s-JOIN block found, pass %d, test %d, then %d",
3107 (else_bb) ? "-ELSE" : "",
3108 pass, test_bb->index, then_bb->index);
3109
3110 if (else_bb)
3111 fprintf (dump_file, ", else %d", else_bb->index);
3112
3113 fprintf (dump_file, ", join %d\n", join_bb->index);
3114 }
3115
3116 /* If the conditional jump is more than just a conditional
3117 jump, then we can not do if-conversion on this block. */
3118 jump = BB_END (test_bb);
3119 if (! onlyjump_p (jump))
3120 return FALSE;
3121
3122 /* If this is not a standard conditional jump, we can't parse it. */
3123 cond = noce_get_condition (jump, &cond_earliest, then_else_reversed);
3124 if (!cond)
3125 return FALSE;
3126
3127 /* We must be comparing objects whose modes imply the size. */
3128 if (GET_MODE (XEXP (cond, 0)) == BLKmode)
3129 return FALSE;
3130
3131 /* Initialize an IF_INFO struct to pass around. */
3132 memset (&if_info, 0, sizeof if_info);
3133 if_info.test_bb = test_bb;
3134 if_info.then_bb = then_bb;
3135 if_info.else_bb = else_bb;
3136 if_info.join_bb = join_bb;
3137 if_info.cond = cond;
3138 if_info.cond_earliest = cond_earliest;
3139 if_info.jump = jump;
3140 if_info.then_else_reversed = then_else_reversed;
3141 if_info.branch_cost = BRANCH_COST (optimize_bb_for_speed_p (test_bb),
3142 predictable_edge_p (then_edge));
3143
3144 /* Do the real work. */
3145
3146 if (noce_process_if_block (&if_info))
3147 return TRUE;
3148
3149 if (HAVE_conditional_move
3150 && cond_move_process_if_block (&if_info))
3151 return TRUE;
3152
3153 return FALSE;
3154 }
3155 \f
3156
3157 /* Merge the blocks and mark for local life update. */
3158
3159 static void
3160 merge_if_block (struct ce_if_block * ce_info)
3161 {
3162 basic_block test_bb = ce_info->test_bb; /* last test block */
3163 basic_block then_bb = ce_info->then_bb; /* THEN */
3164 basic_block else_bb = ce_info->else_bb; /* ELSE or NULL */
3165 basic_block join_bb = ce_info->join_bb; /* join block */
3166 basic_block combo_bb;
3167
3168 /* All block merging is done into the lower block numbers. */
3169
3170 combo_bb = test_bb;
3171 df_set_bb_dirty (test_bb);
3172
3173 /* Merge any basic blocks to handle && and || subtests. Each of
3174 the blocks are on the fallthru path from the predecessor block. */
3175 if (ce_info->num_multiple_test_blocks > 0)
3176 {
3177 basic_block bb = test_bb;
3178 basic_block last_test_bb = ce_info->last_test_bb;
3179 basic_block fallthru = block_fallthru (bb);
3180
3181 do
3182 {
3183 bb = fallthru;
3184 fallthru = block_fallthru (bb);
3185 merge_blocks (combo_bb, bb);
3186 num_true_changes++;
3187 }
3188 while (bb != last_test_bb);
3189 }
3190
3191 /* Merge TEST block into THEN block. Normally the THEN block won't have a
3192 label, but it might if there were || tests. That label's count should be
3193 zero, and it normally should be removed. */
3194
3195 if (then_bb)
3196 {
3197 /* If THEN_BB has no successors, then there's a BARRIER after it.
3198 If COMBO_BB has more than one successor (THEN_BB), then that BARRIER
3199 is no longer needed, and in fact it is incorrect to leave it in
3200 the insn stream. */
3201 if (EDGE_COUNT (then_bb->succs) == 0
3202 && EDGE_COUNT (combo_bb->succs) > 1)
3203 {
3204 rtx end = NEXT_INSN (BB_END (then_bb));
3205 while (end && NOTE_P (end) && !NOTE_INSN_BASIC_BLOCK_P (end))
3206 end = NEXT_INSN (end);
3207
3208 if (end && BARRIER_P (end))
3209 delete_insn (end);
3210 }
3211 merge_blocks (combo_bb, then_bb);
3212 num_true_changes++;
3213 }
3214
3215 /* The ELSE block, if it existed, had a label. That label count
3216 will almost always be zero, but odd things can happen when labels
3217 get their addresses taken. */
3218 if (else_bb)
3219 {
3220 /* If ELSE_BB has no successors, then there's a BARRIER after it.
3221 If COMBO_BB has more than one successor (ELSE_BB), then that BARRIER
3222 is no longer needed, and in fact it is incorrect to leave it in
3223 the insn stream. */
3224 if (EDGE_COUNT (else_bb->succs) == 0
3225 && EDGE_COUNT (combo_bb->succs) > 1)
3226 {
3227 rtx end = NEXT_INSN (BB_END (else_bb));
3228 while (end && NOTE_P (end) && !NOTE_INSN_BASIC_BLOCK_P (end))
3229 end = NEXT_INSN (end);
3230
3231 if (end && BARRIER_P (end))
3232 delete_insn (end);
3233 }
3234 merge_blocks (combo_bb, else_bb);
3235 num_true_changes++;
3236 }
3237
3238 /* If there was no join block reported, that means it was not adjacent
3239 to the others, and so we cannot merge them. */
3240
3241 if (! join_bb)
3242 {
3243 rtx_insn *last = BB_END (combo_bb);
3244
3245 /* The outgoing edge for the current COMBO block should already
3246 be correct. Verify this. */
3247 if (EDGE_COUNT (combo_bb->succs) == 0)
3248 gcc_assert (find_reg_note (last, REG_NORETURN, NULL)
3249 || (NONJUMP_INSN_P (last)
3250 && GET_CODE (PATTERN (last)) == TRAP_IF
3251 && (TRAP_CONDITION (PATTERN (last))
3252 == const_true_rtx)));
3253
3254 else
3255 /* There should still be something at the end of the THEN or ELSE
3256 blocks taking us to our final destination. */
3257 gcc_assert (JUMP_P (last)
3258 || (EDGE_SUCC (combo_bb, 0)->dest
3259 == EXIT_BLOCK_PTR_FOR_FN (cfun)
3260 && CALL_P (last)
3261 && SIBLING_CALL_P (last))
3262 || ((EDGE_SUCC (combo_bb, 0)->flags & EDGE_EH)
3263 && can_throw_internal (last)));
3264 }
3265
3266 /* The JOIN block may have had quite a number of other predecessors too.
3267 Since we've already merged the TEST, THEN and ELSE blocks, we should
3268 have only one remaining edge from our if-then-else diamond. If there
3269 is more than one remaining edge, it must come from elsewhere. There
3270 may be zero incoming edges if the THEN block didn't actually join
3271 back up (as with a call to a non-return function). */
3272 else if (EDGE_COUNT (join_bb->preds) < 2
3273 && join_bb != EXIT_BLOCK_PTR_FOR_FN (cfun))
3274 {
3275 /* We can merge the JOIN cleanly and update the dataflow try
3276 again on this pass.*/
3277 merge_blocks (combo_bb, join_bb);
3278 num_true_changes++;
3279 }
3280 else
3281 {
3282 /* We cannot merge the JOIN. */
3283
3284 /* The outgoing edge for the current COMBO block should already
3285 be correct. Verify this. */
3286 gcc_assert (single_succ_p (combo_bb)
3287 && single_succ (combo_bb) == join_bb);
3288
3289 /* Remove the jump and cruft from the end of the COMBO block. */
3290 if (join_bb != EXIT_BLOCK_PTR_FOR_FN (cfun))
3291 tidy_fallthru_edge (single_succ_edge (combo_bb));
3292 }
3293
3294 num_updated_if_blocks++;
3295 }
3296 \f
3297 /* Find a block ending in a simple IF condition and try to transform it
3298 in some way. When converting a multi-block condition, put the new code
3299 in the first such block and delete the rest. Return a pointer to this
3300 first block if some transformation was done. Return NULL otherwise. */
3301
3302 static basic_block
3303 find_if_header (basic_block test_bb, int pass)
3304 {
3305 ce_if_block ce_info;
3306 edge then_edge;
3307 edge else_edge;
3308
3309 /* The kind of block we're looking for has exactly two successors. */
3310 if (EDGE_COUNT (test_bb->succs) != 2)
3311 return NULL;
3312
3313 then_edge = EDGE_SUCC (test_bb, 0);
3314 else_edge = EDGE_SUCC (test_bb, 1);
3315
3316 if (df_get_bb_dirty (then_edge->dest))
3317 return NULL;
3318 if (df_get_bb_dirty (else_edge->dest))
3319 return NULL;
3320
3321 /* Neither edge should be abnormal. */
3322 if ((then_edge->flags & EDGE_COMPLEX)
3323 || (else_edge->flags & EDGE_COMPLEX))
3324 return NULL;
3325
3326 /* Nor exit the loop. */
3327 if ((then_edge->flags & EDGE_LOOP_EXIT)
3328 || (else_edge->flags & EDGE_LOOP_EXIT))
3329 return NULL;
3330
3331 /* The THEN edge is canonically the one that falls through. */
3332 if (then_edge->flags & EDGE_FALLTHRU)
3333 ;
3334 else if (else_edge->flags & EDGE_FALLTHRU)
3335 {
3336 edge e = else_edge;
3337 else_edge = then_edge;
3338 then_edge = e;
3339 }
3340 else
3341 /* Otherwise this must be a multiway branch of some sort. */
3342 return NULL;
3343
3344 memset (&ce_info, 0, sizeof (ce_info));
3345 ce_info.test_bb = test_bb;
3346 ce_info.then_bb = then_edge->dest;
3347 ce_info.else_bb = else_edge->dest;
3348 ce_info.pass = pass;
3349
3350 #ifdef IFCVT_MACHDEP_INIT
3351 IFCVT_MACHDEP_INIT (&ce_info);
3352 #endif
3353
3354 if (!reload_completed
3355 && noce_find_if_block (test_bb, then_edge, else_edge, pass))
3356 goto success;
3357
3358 if (reload_completed
3359 && targetm.have_conditional_execution ()
3360 && cond_exec_find_if_block (&ce_info))
3361 goto success;
3362
3363 if (HAVE_trap
3364 && optab_handler (ctrap_optab, word_mode) != CODE_FOR_nothing
3365 && find_cond_trap (test_bb, then_edge, else_edge))
3366 goto success;
3367
3368 if (dom_info_state (CDI_POST_DOMINATORS) >= DOM_NO_FAST_QUERY
3369 && (reload_completed || !targetm.have_conditional_execution ()))
3370 {
3371 if (find_if_case_1 (test_bb, then_edge, else_edge))
3372 goto success;
3373 if (find_if_case_2 (test_bb, then_edge, else_edge))
3374 goto success;
3375 }
3376
3377 return NULL;
3378
3379 success:
3380 if (dump_file)
3381 fprintf (dump_file, "Conversion succeeded on pass %d.\n", pass);
3382 /* Set this so we continue looking. */
3383 cond_exec_changed_p = TRUE;
3384 return ce_info.test_bb;
3385 }
3386
3387 /* Return true if a block has two edges, one of which falls through to the next
3388 block, and the other jumps to a specific block, so that we can tell if the
3389 block is part of an && test or an || test. Returns either -1 or the number
3390 of non-note, non-jump, non-USE/CLOBBER insns in the block. */
3391
3392 static int
3393 block_jumps_and_fallthru_p (basic_block cur_bb, basic_block target_bb)
3394 {
3395 edge cur_edge;
3396 int fallthru_p = FALSE;
3397 int jump_p = FALSE;
3398 rtx_insn *insn;
3399 rtx_insn *end;
3400 int n_insns = 0;
3401 edge_iterator ei;
3402
3403 if (!cur_bb || !target_bb)
3404 return -1;
3405
3406 /* If no edges, obviously it doesn't jump or fallthru. */
3407 if (EDGE_COUNT (cur_bb->succs) == 0)
3408 return FALSE;
3409
3410 FOR_EACH_EDGE (cur_edge, ei, cur_bb->succs)
3411 {
3412 if (cur_edge->flags & EDGE_COMPLEX)
3413 /* Anything complex isn't what we want. */
3414 return -1;
3415
3416 else if (cur_edge->flags & EDGE_FALLTHRU)
3417 fallthru_p = TRUE;
3418
3419 else if (cur_edge->dest == target_bb)
3420 jump_p = TRUE;
3421
3422 else
3423 return -1;
3424 }
3425
3426 if ((jump_p & fallthru_p) == 0)
3427 return -1;
3428
3429 /* Don't allow calls in the block, since this is used to group && and ||
3430 together for conditional execution support. ??? we should support
3431 conditional execution support across calls for IA-64 some day, but
3432 for now it makes the code simpler. */
3433 end = BB_END (cur_bb);
3434 insn = BB_HEAD (cur_bb);
3435
3436 while (insn != NULL_RTX)
3437 {
3438 if (CALL_P (insn))
3439 return -1;
3440
3441 if (INSN_P (insn)
3442 && !JUMP_P (insn)
3443 && !DEBUG_INSN_P (insn)
3444 && GET_CODE (PATTERN (insn)) != USE
3445 && GET_CODE (PATTERN (insn)) != CLOBBER)
3446 n_insns++;
3447
3448 if (insn == end)
3449 break;
3450
3451 insn = NEXT_INSN (insn);
3452 }
3453
3454 return n_insns;
3455 }
3456
3457 /* Determine if a given basic block heads a simple IF-THEN or IF-THEN-ELSE
3458 block. If so, we'll try to convert the insns to not require the branch.
3459 Return TRUE if we were successful at converting the block. */
3460
3461 static int
3462 cond_exec_find_if_block (struct ce_if_block * ce_info)
3463 {
3464 basic_block test_bb = ce_info->test_bb;
3465 basic_block then_bb = ce_info->then_bb;
3466 basic_block else_bb = ce_info->else_bb;
3467 basic_block join_bb = NULL_BLOCK;
3468 edge cur_edge;
3469 basic_block next;
3470 edge_iterator ei;
3471
3472 ce_info->last_test_bb = test_bb;
3473
3474 /* We only ever should get here after reload,
3475 and if we have conditional execution. */
3476 gcc_assert (reload_completed && targetm.have_conditional_execution ());
3477
3478 /* Discover if any fall through predecessors of the current test basic block
3479 were && tests (which jump to the else block) or || tests (which jump to
3480 the then block). */
3481 if (single_pred_p (test_bb)
3482 && single_pred_edge (test_bb)->flags == EDGE_FALLTHRU)
3483 {
3484 basic_block bb = single_pred (test_bb);
3485 basic_block target_bb;
3486 int max_insns = MAX_CONDITIONAL_EXECUTE;
3487 int n_insns;
3488
3489 /* Determine if the preceding block is an && or || block. */
3490 if ((n_insns = block_jumps_and_fallthru_p (bb, else_bb)) >= 0)
3491 {
3492 ce_info->and_and_p = TRUE;
3493 target_bb = else_bb;
3494 }
3495 else if ((n_insns = block_jumps_and_fallthru_p (bb, then_bb)) >= 0)
3496 {
3497 ce_info->and_and_p = FALSE;
3498 target_bb = then_bb;
3499 }
3500 else
3501 target_bb = NULL_BLOCK;
3502
3503 if (target_bb && n_insns <= max_insns)
3504 {
3505 int total_insns = 0;
3506 int blocks = 0;
3507
3508 ce_info->last_test_bb = test_bb;
3509
3510 /* Found at least one && or || block, look for more. */
3511 do
3512 {
3513 ce_info->test_bb = test_bb = bb;
3514 total_insns += n_insns;
3515 blocks++;
3516
3517 if (!single_pred_p (bb))
3518 break;
3519
3520 bb = single_pred (bb);
3521 n_insns = block_jumps_and_fallthru_p (bb, target_bb);
3522 }
3523 while (n_insns >= 0 && (total_insns + n_insns) <= max_insns);
3524
3525 ce_info->num_multiple_test_blocks = blocks;
3526 ce_info->num_multiple_test_insns = total_insns;
3527
3528 if (ce_info->and_and_p)
3529 ce_info->num_and_and_blocks = blocks;
3530 else
3531 ce_info->num_or_or_blocks = blocks;
3532 }
3533 }
3534
3535 /* The THEN block of an IF-THEN combo must have exactly one predecessor,
3536 other than any || blocks which jump to the THEN block. */
3537 if ((EDGE_COUNT (then_bb->preds) - ce_info->num_or_or_blocks) != 1)
3538 return FALSE;
3539
3540 /* The edges of the THEN and ELSE blocks cannot have complex edges. */
3541 FOR_EACH_EDGE (cur_edge, ei, then_bb->preds)
3542 {
3543 if (cur_edge->flags & EDGE_COMPLEX)
3544 return FALSE;
3545 }
3546
3547 FOR_EACH_EDGE (cur_edge, ei, else_bb->preds)
3548 {
3549 if (cur_edge->flags & EDGE_COMPLEX)
3550 return FALSE;
3551 }
3552
3553 /* The THEN block of an IF-THEN combo must have zero or one successors. */
3554 if (EDGE_COUNT (then_bb->succs) > 0
3555 && (!single_succ_p (then_bb)
3556 || (single_succ_edge (then_bb)->flags & EDGE_COMPLEX)
3557 || (epilogue_completed
3558 && tablejump_p (BB_END (then_bb), NULL, NULL))))
3559 return FALSE;
3560
3561 /* If the THEN block has no successors, conditional execution can still
3562 make a conditional call. Don't do this unless the ELSE block has
3563 only one incoming edge -- the CFG manipulation is too ugly otherwise.
3564 Check for the last insn of the THEN block being an indirect jump, which
3565 is listed as not having any successors, but confuses the rest of the CE
3566 code processing. ??? we should fix this in the future. */
3567 if (EDGE_COUNT (then_bb->succs) == 0)
3568 {
3569 if (single_pred_p (else_bb) && else_bb != EXIT_BLOCK_PTR_FOR_FN (cfun))
3570 {
3571 rtx last_insn = BB_END (then_bb);
3572
3573 while (last_insn
3574 && NOTE_P (last_insn)
3575 && last_insn != BB_HEAD (then_bb))
3576 last_insn = PREV_INSN (last_insn);
3577
3578 if (last_insn
3579 && JUMP_P (last_insn)
3580 && ! simplejump_p (last_insn))
3581 return FALSE;
3582
3583 join_bb = else_bb;
3584 else_bb = NULL_BLOCK;
3585 }
3586 else
3587 return FALSE;
3588 }
3589
3590 /* If the THEN block's successor is the other edge out of the TEST block,
3591 then we have an IF-THEN combo without an ELSE. */
3592 else if (single_succ (then_bb) == else_bb)
3593 {
3594 join_bb = else_bb;
3595 else_bb = NULL_BLOCK;
3596 }
3597
3598 /* If the THEN and ELSE block meet in a subsequent block, and the ELSE
3599 has exactly one predecessor and one successor, and the outgoing edge
3600 is not complex, then we have an IF-THEN-ELSE combo. */
3601 else if (single_succ_p (else_bb)
3602 && single_succ (then_bb) == single_succ (else_bb)
3603 && single_pred_p (else_bb)
3604 && !(single_succ_edge (else_bb)->flags & EDGE_COMPLEX)
3605 && !(epilogue_completed
3606 && tablejump_p (BB_END (else_bb), NULL, NULL)))
3607 join_bb = single_succ (else_bb);
3608
3609 /* Otherwise it is not an IF-THEN or IF-THEN-ELSE combination. */
3610 else
3611 return FALSE;
3612
3613 num_possible_if_blocks++;
3614
3615 if (dump_file)
3616 {
3617 fprintf (dump_file,
3618 "\nIF-THEN%s block found, pass %d, start block %d "
3619 "[insn %d], then %d [%d]",
3620 (else_bb) ? "-ELSE" : "",
3621 ce_info->pass,
3622 test_bb->index,
3623 BB_HEAD (test_bb) ? (int)INSN_UID (BB_HEAD (test_bb)) : -1,
3624 then_bb->index,
3625 BB_HEAD (then_bb) ? (int)INSN_UID (BB_HEAD (then_bb)) : -1);
3626
3627 if (else_bb)
3628 fprintf (dump_file, ", else %d [%d]",
3629 else_bb->index,
3630 BB_HEAD (else_bb) ? (int)INSN_UID (BB_HEAD (else_bb)) : -1);
3631
3632 fprintf (dump_file, ", join %d [%d]",
3633 join_bb->index,
3634 BB_HEAD (join_bb) ? (int)INSN_UID (BB_HEAD (join_bb)) : -1);
3635
3636 if (ce_info->num_multiple_test_blocks > 0)
3637 fprintf (dump_file, ", %d %s block%s last test %d [%d]",
3638 ce_info->num_multiple_test_blocks,
3639 (ce_info->and_and_p) ? "&&" : "||",
3640 (ce_info->num_multiple_test_blocks == 1) ? "" : "s",
3641 ce_info->last_test_bb->index,
3642 ((BB_HEAD (ce_info->last_test_bb))
3643 ? (int)INSN_UID (BB_HEAD (ce_info->last_test_bb))
3644 : -1));
3645
3646 fputc ('\n', dump_file);
3647 }
3648
3649 /* Make sure IF, THEN, and ELSE, blocks are adjacent. Actually, we get the
3650 first condition for free, since we've already asserted that there's a
3651 fallthru edge from IF to THEN. Likewise for the && and || blocks, since
3652 we checked the FALLTHRU flag, those are already adjacent to the last IF
3653 block. */
3654 /* ??? As an enhancement, move the ELSE block. Have to deal with
3655 BLOCK notes, if by no other means than backing out the merge if they
3656 exist. Sticky enough I don't want to think about it now. */
3657 next = then_bb;
3658 if (else_bb && (next = next->next_bb) != else_bb)
3659 return FALSE;
3660 if ((next = next->next_bb) != join_bb
3661 && join_bb != EXIT_BLOCK_PTR_FOR_FN (cfun))
3662 {
3663 if (else_bb)
3664 join_bb = NULL;
3665 else
3666 return FALSE;
3667 }
3668
3669 /* Do the real work. */
3670
3671 ce_info->else_bb = else_bb;
3672 ce_info->join_bb = join_bb;
3673
3674 /* If we have && and || tests, try to first handle combining the && and ||
3675 tests into the conditional code, and if that fails, go back and handle
3676 it without the && and ||, which at present handles the && case if there
3677 was no ELSE block. */
3678 if (cond_exec_process_if_block (ce_info, TRUE))
3679 return TRUE;
3680
3681 if (ce_info->num_multiple_test_blocks)
3682 {
3683 cancel_changes (0);
3684
3685 if (cond_exec_process_if_block (ce_info, FALSE))
3686 return TRUE;
3687 }
3688
3689 return FALSE;
3690 }
3691
3692 /* Convert a branch over a trap, or a branch
3693 to a trap, into a conditional trap. */
3694
3695 static int
3696 find_cond_trap (basic_block test_bb, edge then_edge, edge else_edge)
3697 {
3698 basic_block then_bb = then_edge->dest;
3699 basic_block else_bb = else_edge->dest;
3700 basic_block other_bb, trap_bb;
3701 rtx_insn *trap, *jump;
3702 rtx cond, seq;
3703 rtx_insn *cond_earliest;
3704 enum rtx_code code;
3705
3706 /* Locate the block with the trap instruction. */
3707 /* ??? While we look for no successors, we really ought to allow
3708 EH successors. Need to fix merge_if_block for that to work. */
3709 if ((trap = block_has_only_trap (then_bb)) != NULL)
3710 trap_bb = then_bb, other_bb = else_bb;
3711 else if ((trap = block_has_only_trap (else_bb)) != NULL)
3712 trap_bb = else_bb, other_bb = then_bb;
3713 else
3714 return FALSE;
3715
3716 if (dump_file)
3717 {
3718 fprintf (dump_file, "\nTRAP-IF block found, start %d, trap %d\n",
3719 test_bb->index, trap_bb->index);
3720 }
3721
3722 /* If this is not a standard conditional jump, we can't parse it. */
3723 jump = BB_END (test_bb);
3724 cond = noce_get_condition (jump, &cond_earliest, false);
3725 if (! cond)
3726 return FALSE;
3727
3728 /* If the conditional jump is more than just a conditional jump, then
3729 we can not do if-conversion on this block. */
3730 if (! onlyjump_p (jump))
3731 return FALSE;
3732
3733 /* We must be comparing objects whose modes imply the size. */
3734 if (GET_MODE (XEXP (cond, 0)) == BLKmode)
3735 return FALSE;
3736
3737 /* Reverse the comparison code, if necessary. */
3738 code = GET_CODE (cond);
3739 if (then_bb == trap_bb)
3740 {
3741 code = reversed_comparison_code (cond, jump);
3742 if (code == UNKNOWN)
3743 return FALSE;
3744 }
3745
3746 /* Attempt to generate the conditional trap. */
3747 seq = gen_cond_trap (code, copy_rtx (XEXP (cond, 0)),
3748 copy_rtx (XEXP (cond, 1)),
3749 TRAP_CODE (PATTERN (trap)));
3750 if (seq == NULL)
3751 return FALSE;
3752
3753 /* Emit the new insns before cond_earliest. */
3754 emit_insn_before_setloc (seq, cond_earliest, INSN_LOCATION (trap));
3755
3756 /* Delete the trap block if possible. */
3757 remove_edge (trap_bb == then_bb ? then_edge : else_edge);
3758 df_set_bb_dirty (test_bb);
3759 df_set_bb_dirty (then_bb);
3760 df_set_bb_dirty (else_bb);
3761
3762 if (EDGE_COUNT (trap_bb->preds) == 0)
3763 {
3764 delete_basic_block (trap_bb);
3765 num_true_changes++;
3766 }
3767
3768 /* Wire together the blocks again. */
3769 if (current_ir_type () == IR_RTL_CFGLAYOUT)
3770 single_succ_edge (test_bb)->flags |= EDGE_FALLTHRU;
3771 else if (trap_bb == then_bb)
3772 {
3773 rtx lab;
3774 rtx_insn *newjump;
3775
3776 lab = JUMP_LABEL (jump);
3777 newjump = emit_jump_insn_after (gen_jump (lab), jump);
3778 LABEL_NUSES (lab) += 1;
3779 JUMP_LABEL (newjump) = lab;
3780 emit_barrier_after (newjump);
3781 }
3782 delete_insn (jump);
3783
3784 if (can_merge_blocks_p (test_bb, other_bb))
3785 {
3786 merge_blocks (test_bb, other_bb);
3787 num_true_changes++;
3788 }
3789
3790 num_updated_if_blocks++;
3791 return TRUE;
3792 }
3793
3794 /* Subroutine of find_cond_trap: if BB contains only a trap insn,
3795 return it. */
3796
3797 static rtx_insn *
3798 block_has_only_trap (basic_block bb)
3799 {
3800 rtx_insn *trap;
3801
3802 /* We're not the exit block. */
3803 if (bb == EXIT_BLOCK_PTR_FOR_FN (cfun))
3804 return NULL;
3805
3806 /* The block must have no successors. */
3807 if (EDGE_COUNT (bb->succs) > 0)
3808 return NULL;
3809
3810 /* The only instruction in the THEN block must be the trap. */
3811 trap = first_active_insn (bb);
3812 if (! (trap == BB_END (bb)
3813 && GET_CODE (PATTERN (trap)) == TRAP_IF
3814 && TRAP_CONDITION (PATTERN (trap)) == const_true_rtx))
3815 return NULL;
3816
3817 return trap;
3818 }
3819
3820 /* Look for IF-THEN-ELSE cases in which one of THEN or ELSE is
3821 transformable, but not necessarily the other. There need be no
3822 JOIN block.
3823
3824 Return TRUE if we were successful at converting the block.
3825
3826 Cases we'd like to look at:
3827
3828 (1)
3829 if (test) goto over; // x not live
3830 x = a;
3831 goto label;
3832 over:
3833
3834 becomes
3835
3836 x = a;
3837 if (! test) goto label;
3838
3839 (2)
3840 if (test) goto E; // x not live
3841 x = big();
3842 goto L;
3843 E:
3844 x = b;
3845 goto M;
3846
3847 becomes
3848
3849 x = b;
3850 if (test) goto M;
3851 x = big();
3852 goto L;
3853
3854 (3) // This one's really only interesting for targets that can do
3855 // multiway branching, e.g. IA-64 BBB bundles. For other targets
3856 // it results in multiple branches on a cache line, which often
3857 // does not sit well with predictors.
3858
3859 if (test1) goto E; // predicted not taken
3860 x = a;
3861 if (test2) goto F;
3862 ...
3863 E:
3864 x = b;
3865 J:
3866
3867 becomes
3868
3869 x = a;
3870 if (test1) goto E;
3871 if (test2) goto F;
3872
3873 Notes:
3874
3875 (A) Don't do (2) if the branch is predicted against the block we're
3876 eliminating. Do it anyway if we can eliminate a branch; this requires
3877 that the sole successor of the eliminated block postdominate the other
3878 side of the if.
3879
3880 (B) With CE, on (3) we can steal from both sides of the if, creating
3881
3882 if (test1) x = a;
3883 if (!test1) x = b;
3884 if (test1) goto J;
3885 if (test2) goto F;
3886 ...
3887 J:
3888
3889 Again, this is most useful if J postdominates.
3890
3891 (C) CE substitutes for helpful life information.
3892
3893 (D) These heuristics need a lot of work. */
3894
3895 /* Tests for case 1 above. */
3896
3897 static int
3898 find_if_case_1 (basic_block test_bb, edge then_edge, edge else_edge)
3899 {
3900 basic_block then_bb = then_edge->dest;
3901 basic_block else_bb = else_edge->dest;
3902 basic_block new_bb;
3903 int then_bb_index, then_prob;
3904 rtx else_target = NULL_RTX;
3905
3906 /* If we are partitioning hot/cold basic blocks, we don't want to
3907 mess up unconditional or indirect jumps that cross between hot
3908 and cold sections.
3909
3910 Basic block partitioning may result in some jumps that appear to
3911 be optimizable (or blocks that appear to be mergeable), but which really
3912 must be left untouched (they are required to make it safely across
3913 partition boundaries). See the comments at the top of
3914 bb-reorder.c:partition_hot_cold_basic_blocks for complete details. */
3915
3916 if ((BB_END (then_bb)
3917 && JUMP_P (BB_END (then_bb))
3918 && CROSSING_JUMP_P (BB_END (then_bb)))
3919 || (BB_END (test_bb)
3920 && JUMP_P (BB_END (test_bb))
3921 && CROSSING_JUMP_P (BB_END (test_bb)))
3922 || (BB_END (else_bb)
3923 && JUMP_P (BB_END (else_bb))
3924 && CROSSING_JUMP_P (BB_END (else_bb))))
3925 return FALSE;
3926
3927 /* THEN has one successor. */
3928 if (!single_succ_p (then_bb))
3929 return FALSE;
3930
3931 /* THEN does not fall through, but is not strange either. */
3932 if (single_succ_edge (then_bb)->flags & (EDGE_COMPLEX | EDGE_FALLTHRU))
3933 return FALSE;
3934
3935 /* THEN has one predecessor. */
3936 if (!single_pred_p (then_bb))
3937 return FALSE;
3938
3939 /* THEN must do something. */
3940 if (forwarder_block_p (then_bb))
3941 return FALSE;
3942
3943 num_possible_if_blocks++;
3944 if (dump_file)
3945 fprintf (dump_file,
3946 "\nIF-CASE-1 found, start %d, then %d\n",
3947 test_bb->index, then_bb->index);
3948
3949 if (then_edge->probability)
3950 then_prob = REG_BR_PROB_BASE - then_edge->probability;
3951 else
3952 then_prob = REG_BR_PROB_BASE / 2;
3953
3954 /* We're speculating from the THEN path, we want to make sure the cost
3955 of speculation is within reason. */
3956 if (! cheap_bb_rtx_cost_p (then_bb, then_prob,
3957 COSTS_N_INSNS (BRANCH_COST (optimize_bb_for_speed_p (then_edge->src),
3958 predictable_edge_p (then_edge)))))
3959 return FALSE;
3960
3961 if (else_bb == EXIT_BLOCK_PTR_FOR_FN (cfun))
3962 {
3963 rtx_insn *jump = BB_END (else_edge->src);
3964 gcc_assert (JUMP_P (jump));
3965 else_target = JUMP_LABEL (jump);
3966 }
3967
3968 /* Registers set are dead, or are predicable. */
3969 if (! dead_or_predicable (test_bb, then_bb, else_bb,
3970 single_succ_edge (then_bb), 1))
3971 return FALSE;
3972
3973 /* Conversion went ok, including moving the insns and fixing up the
3974 jump. Adjust the CFG to match. */
3975
3976 /* We can avoid creating a new basic block if then_bb is immediately
3977 followed by else_bb, i.e. deleting then_bb allows test_bb to fall
3978 through to else_bb. */
3979
3980 if (then_bb->next_bb == else_bb
3981 && then_bb->prev_bb == test_bb
3982 && else_bb != EXIT_BLOCK_PTR_FOR_FN (cfun))
3983 {
3984 redirect_edge_succ (FALLTHRU_EDGE (test_bb), else_bb);
3985 new_bb = 0;
3986 }
3987 else if (else_bb == EXIT_BLOCK_PTR_FOR_FN (cfun))
3988 new_bb = force_nonfallthru_and_redirect (FALLTHRU_EDGE (test_bb),
3989 else_bb, else_target);
3990 else
3991 new_bb = redirect_edge_and_branch_force (FALLTHRU_EDGE (test_bb),
3992 else_bb);
3993
3994 df_set_bb_dirty (test_bb);
3995 df_set_bb_dirty (else_bb);
3996
3997 then_bb_index = then_bb->index;
3998 delete_basic_block (then_bb);
3999
4000 /* Make rest of code believe that the newly created block is the THEN_BB
4001 block we removed. */
4002 if (new_bb)
4003 {
4004 df_bb_replace (then_bb_index, new_bb);
4005 /* This should have been done above via force_nonfallthru_and_redirect
4006 (possibly called from redirect_edge_and_branch_force). */
4007 gcc_checking_assert (BB_PARTITION (new_bb) == BB_PARTITION (test_bb));
4008 }
4009
4010 num_true_changes++;
4011 num_updated_if_blocks++;
4012
4013 return TRUE;
4014 }
4015
4016 /* Test for case 2 above. */
4017
4018 static int
4019 find_if_case_2 (basic_block test_bb, edge then_edge, edge else_edge)
4020 {
4021 basic_block then_bb = then_edge->dest;
4022 basic_block else_bb = else_edge->dest;
4023 edge else_succ;
4024 int then_prob, else_prob;
4025
4026 /* We do not want to speculate (empty) loop latches. */
4027 if (current_loops
4028 && else_bb->loop_father->latch == else_bb)
4029 return FALSE;
4030
4031 /* If we are partitioning hot/cold basic blocks, we don't want to
4032 mess up unconditional or indirect jumps that cross between hot
4033 and cold sections.
4034
4035 Basic block partitioning may result in some jumps that appear to
4036 be optimizable (or blocks that appear to be mergeable), but which really
4037 must be left untouched (they are required to make it safely across
4038 partition boundaries). See the comments at the top of
4039 bb-reorder.c:partition_hot_cold_basic_blocks for complete details. */
4040
4041 if ((BB_END (then_bb)
4042 && JUMP_P (BB_END (then_bb))
4043 && CROSSING_JUMP_P (BB_END (then_bb)))
4044 || (BB_END (test_bb)
4045 && JUMP_P (BB_END (test_bb))
4046 && CROSSING_JUMP_P (BB_END (test_bb)))
4047 || (BB_END (else_bb)
4048 && JUMP_P (BB_END (else_bb))
4049 && CROSSING_JUMP_P (BB_END (else_bb))))
4050 return FALSE;
4051
4052 /* ELSE has one successor. */
4053 if (!single_succ_p (else_bb))
4054 return FALSE;
4055 else
4056 else_succ = single_succ_edge (else_bb);
4057
4058 /* ELSE outgoing edge is not complex. */
4059 if (else_succ->flags & EDGE_COMPLEX)
4060 return FALSE;
4061
4062 /* ELSE has one predecessor. */
4063 if (!single_pred_p (else_bb))
4064 return FALSE;
4065
4066 /* THEN is not EXIT. */
4067 if (then_bb->index < NUM_FIXED_BLOCKS)
4068 return FALSE;
4069
4070 if (else_edge->probability)
4071 {
4072 else_prob = else_edge->probability;
4073 then_prob = REG_BR_PROB_BASE - else_prob;
4074 }
4075 else
4076 {
4077 else_prob = REG_BR_PROB_BASE / 2;
4078 then_prob = REG_BR_PROB_BASE / 2;
4079 }
4080
4081 /* ELSE is predicted or SUCC(ELSE) postdominates THEN. */
4082 if (else_prob > then_prob)
4083 ;
4084 else if (else_succ->dest->index < NUM_FIXED_BLOCKS
4085 || dominated_by_p (CDI_POST_DOMINATORS, then_bb,
4086 else_succ->dest))
4087 ;
4088 else
4089 return FALSE;
4090
4091 num_possible_if_blocks++;
4092 if (dump_file)
4093 fprintf (dump_file,
4094 "\nIF-CASE-2 found, start %d, else %d\n",
4095 test_bb->index, else_bb->index);
4096
4097 /* We're speculating from the ELSE path, we want to make sure the cost
4098 of speculation is within reason. */
4099 if (! cheap_bb_rtx_cost_p (else_bb, else_prob,
4100 COSTS_N_INSNS (BRANCH_COST (optimize_bb_for_speed_p (else_edge->src),
4101 predictable_edge_p (else_edge)))))
4102 return FALSE;
4103
4104 /* Registers set are dead, or are predicable. */
4105 if (! dead_or_predicable (test_bb, else_bb, then_bb, else_succ, 0))
4106 return FALSE;
4107
4108 /* Conversion went ok, including moving the insns and fixing up the
4109 jump. Adjust the CFG to match. */
4110
4111 df_set_bb_dirty (test_bb);
4112 df_set_bb_dirty (then_bb);
4113 delete_basic_block (else_bb);
4114
4115 num_true_changes++;
4116 num_updated_if_blocks++;
4117
4118 /* ??? We may now fallthru from one of THEN's successors into a join
4119 block. Rerun cleanup_cfg? Examine things manually? Wait? */
4120
4121 return TRUE;
4122 }
4123
4124 /* Used by the code above to perform the actual rtl transformations.
4125 Return TRUE if successful.
4126
4127 TEST_BB is the block containing the conditional branch. MERGE_BB
4128 is the block containing the code to manipulate. DEST_EDGE is an
4129 edge representing a jump to the join block; after the conversion,
4130 TEST_BB should be branching to its destination.
4131 REVERSEP is true if the sense of the branch should be reversed. */
4132
4133 static int
4134 dead_or_predicable (basic_block test_bb, basic_block merge_bb,
4135 basic_block other_bb, edge dest_edge, int reversep)
4136 {
4137 basic_block new_dest = dest_edge->dest;
4138 rtx_insn *head, *end, *jump;
4139 rtx_insn *earliest = NULL;
4140 rtx old_dest;
4141 bitmap merge_set = NULL;
4142 /* Number of pending changes. */
4143 int n_validated_changes = 0;
4144 rtx new_dest_label = NULL_RTX;
4145
4146 jump = BB_END (test_bb);
4147
4148 /* Find the extent of the real code in the merge block. */
4149 head = BB_HEAD (merge_bb);
4150 end = BB_END (merge_bb);
4151
4152 while (DEBUG_INSN_P (end) && end != head)
4153 end = PREV_INSN (end);
4154
4155 /* If merge_bb ends with a tablejump, predicating/moving insn's
4156 into test_bb and then deleting merge_bb will result in the jumptable
4157 that follows merge_bb being removed along with merge_bb and then we
4158 get an unresolved reference to the jumptable. */
4159 if (tablejump_p (end, NULL, NULL))
4160 return FALSE;
4161
4162 if (LABEL_P (head))
4163 head = NEXT_INSN (head);
4164 while (DEBUG_INSN_P (head) && head != end)
4165 head = NEXT_INSN (head);
4166 if (NOTE_P (head))
4167 {
4168 if (head == end)
4169 {
4170 head = end = NULL;
4171 goto no_body;
4172 }
4173 head = NEXT_INSN (head);
4174 while (DEBUG_INSN_P (head) && head != end)
4175 head = NEXT_INSN (head);
4176 }
4177
4178 if (JUMP_P (end))
4179 {
4180 if (!onlyjump_p (end))
4181 return FALSE;
4182 if (head == end)
4183 {
4184 head = end = NULL;
4185 goto no_body;
4186 }
4187 end = PREV_INSN (end);
4188 while (DEBUG_INSN_P (end) && end != head)
4189 end = PREV_INSN (end);
4190 }
4191
4192 /* Don't move frame-related insn across the conditional branch. This
4193 can lead to one of the paths of the branch having wrong unwind info. */
4194 if (epilogue_completed)
4195 {
4196 rtx_insn *insn = head;
4197 while (1)
4198 {
4199 if (INSN_P (insn) && RTX_FRAME_RELATED_P (insn))
4200 return FALSE;
4201 if (insn == end)
4202 break;
4203 insn = NEXT_INSN (insn);
4204 }
4205 }
4206
4207 /* Disable handling dead code by conditional execution if the machine needs
4208 to do anything funny with the tests, etc. */
4209 #ifndef IFCVT_MODIFY_TESTS
4210 if (targetm.have_conditional_execution ())
4211 {
4212 /* In the conditional execution case, we have things easy. We know
4213 the condition is reversible. We don't have to check life info
4214 because we're going to conditionally execute the code anyway.
4215 All that's left is making sure the insns involved can actually
4216 be predicated. */
4217
4218 rtx cond;
4219
4220 cond = cond_exec_get_condition (jump);
4221 if (! cond)
4222 return FALSE;
4223
4224 rtx note = find_reg_note (jump, REG_BR_PROB, NULL_RTX);
4225 int prob_val = (note ? XINT (note, 0) : -1);
4226
4227 if (reversep)
4228 {
4229 enum rtx_code rev = reversed_comparison_code (cond, jump);
4230 if (rev == UNKNOWN)
4231 return FALSE;
4232 cond = gen_rtx_fmt_ee (rev, GET_MODE (cond), XEXP (cond, 0),
4233 XEXP (cond, 1));
4234 if (prob_val >= 0)
4235 prob_val = REG_BR_PROB_BASE - prob_val;
4236 }
4237
4238 if (cond_exec_process_insns (NULL, head, end, cond, prob_val, 0)
4239 && verify_changes (0))
4240 n_validated_changes = num_validated_changes ();
4241 else
4242 cancel_changes (0);
4243
4244 earliest = jump;
4245 }
4246 #endif
4247
4248 /* If we allocated new pseudos (e.g. in the conditional move
4249 expander called from noce_emit_cmove), we must resize the
4250 array first. */
4251 if (max_regno < max_reg_num ())
4252 max_regno = max_reg_num ();
4253
4254 /* Try the NCE path if the CE path did not result in any changes. */
4255 if (n_validated_changes == 0)
4256 {
4257 rtx cond;
4258 rtx_insn *insn;
4259 regset live;
4260 bool success;
4261
4262 /* In the non-conditional execution case, we have to verify that there
4263 are no trapping operations, no calls, no references to memory, and
4264 that any registers modified are dead at the branch site. */
4265
4266 if (!any_condjump_p (jump))
4267 return FALSE;
4268
4269 /* Find the extent of the conditional. */
4270 cond = noce_get_condition (jump, &earliest, false);
4271 if (!cond)
4272 return FALSE;
4273
4274 live = BITMAP_ALLOC (&reg_obstack);
4275 simulate_backwards_to_point (merge_bb, live, end);
4276 success = can_move_insns_across (head, end, earliest, jump,
4277 merge_bb, live,
4278 df_get_live_in (other_bb), NULL);
4279 BITMAP_FREE (live);
4280 if (!success)
4281 return FALSE;
4282
4283 /* Collect the set of registers set in MERGE_BB. */
4284 merge_set = BITMAP_ALLOC (&reg_obstack);
4285
4286 FOR_BB_INSNS (merge_bb, insn)
4287 if (NONDEBUG_INSN_P (insn))
4288 df_simulate_find_defs (insn, merge_set);
4289
4290 #ifdef HAVE_simple_return
4291 /* If shrink-wrapping, disable this optimization when test_bb is
4292 the first basic block and merge_bb exits. The idea is to not
4293 move code setting up a return register as that may clobber a
4294 register used to pass function parameters, which then must be
4295 saved in caller-saved regs. A caller-saved reg requires the
4296 prologue, killing a shrink-wrap opportunity. */
4297 if ((flag_shrink_wrap && HAVE_simple_return && !epilogue_completed)
4298 && ENTRY_BLOCK_PTR_FOR_FN (cfun)->next_bb == test_bb
4299 && single_succ_p (new_dest)
4300 && single_succ (new_dest) == EXIT_BLOCK_PTR_FOR_FN (cfun)
4301 && bitmap_intersect_p (df_get_live_in (new_dest), merge_set))
4302 {
4303 regset return_regs;
4304 unsigned int i;
4305
4306 return_regs = BITMAP_ALLOC (&reg_obstack);
4307
4308 /* Start off with the intersection of regs used to pass
4309 params and regs used to return values. */
4310 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
4311 if (FUNCTION_ARG_REGNO_P (i)
4312 && targetm.calls.function_value_regno_p (i))
4313 bitmap_set_bit (return_regs, INCOMING_REGNO (i));
4314
4315 bitmap_and_into (return_regs,
4316 df_get_live_out (ENTRY_BLOCK_PTR_FOR_FN (cfun)));
4317 bitmap_and_into (return_regs,
4318 df_get_live_in (EXIT_BLOCK_PTR_FOR_FN (cfun)));
4319 if (!bitmap_empty_p (return_regs))
4320 {
4321 FOR_BB_INSNS_REVERSE (new_dest, insn)
4322 if (NONDEBUG_INSN_P (insn))
4323 {
4324 df_ref def;
4325
4326 /* If this insn sets any reg in return_regs, add all
4327 reg uses to the set of regs we're interested in. */
4328 FOR_EACH_INSN_DEF (def, insn)
4329 if (bitmap_bit_p (return_regs, DF_REF_REGNO (def)))
4330 {
4331 df_simulate_uses (insn, return_regs);
4332 break;
4333 }
4334 }
4335 if (bitmap_intersect_p (merge_set, return_regs))
4336 {
4337 BITMAP_FREE (return_regs);
4338 BITMAP_FREE (merge_set);
4339 return FALSE;
4340 }
4341 }
4342 BITMAP_FREE (return_regs);
4343 }
4344 #endif
4345 }
4346
4347 no_body:
4348 /* We don't want to use normal invert_jump or redirect_jump because
4349 we don't want to delete_insn called. Also, we want to do our own
4350 change group management. */
4351
4352 old_dest = JUMP_LABEL (jump);
4353 if (other_bb != new_dest)
4354 {
4355 if (JUMP_P (BB_END (dest_edge->src)))
4356 new_dest_label = JUMP_LABEL (BB_END (dest_edge->src));
4357 else if (new_dest == EXIT_BLOCK_PTR_FOR_FN (cfun))
4358 new_dest_label = ret_rtx;
4359 else
4360 new_dest_label = block_label (new_dest);
4361
4362 if (reversep
4363 ? ! invert_jump_1 (jump, new_dest_label)
4364 : ! redirect_jump_1 (jump, new_dest_label))
4365 goto cancel;
4366 }
4367
4368 if (verify_changes (n_validated_changes))
4369 confirm_change_group ();
4370 else
4371 goto cancel;
4372
4373 if (other_bb != new_dest)
4374 {
4375 redirect_jump_2 (jump, old_dest, new_dest_label, 0, reversep);
4376
4377 redirect_edge_succ (BRANCH_EDGE (test_bb), new_dest);
4378 if (reversep)
4379 {
4380 gcov_type count, probability;
4381 count = BRANCH_EDGE (test_bb)->count;
4382 BRANCH_EDGE (test_bb)->count = FALLTHRU_EDGE (test_bb)->count;
4383 FALLTHRU_EDGE (test_bb)->count = count;
4384 probability = BRANCH_EDGE (test_bb)->probability;
4385 BRANCH_EDGE (test_bb)->probability
4386 = FALLTHRU_EDGE (test_bb)->probability;
4387 FALLTHRU_EDGE (test_bb)->probability = probability;
4388 update_br_prob_note (test_bb);
4389 }
4390 }
4391
4392 /* Move the insns out of MERGE_BB to before the branch. */
4393 if (head != NULL)
4394 {
4395 rtx_insn *insn;
4396
4397 if (end == BB_END (merge_bb))
4398 BB_END (merge_bb) = PREV_INSN (head);
4399
4400 /* PR 21767: when moving insns above a conditional branch, the REG_EQUAL
4401 notes being moved might become invalid. */
4402 insn = head;
4403 do
4404 {
4405 rtx note, set;
4406
4407 if (! INSN_P (insn))
4408 continue;
4409 note = find_reg_note (insn, REG_EQUAL, NULL_RTX);
4410 if (! note)
4411 continue;
4412 set = single_set (insn);
4413 if (!set || !function_invariant_p (SET_SRC (set))
4414 || !function_invariant_p (XEXP (note, 0)))
4415 remove_note (insn, note);
4416 } while (insn != end && (insn = NEXT_INSN (insn)));
4417
4418 /* PR46315: when moving insns above a conditional branch, the REG_EQUAL
4419 notes referring to the registers being set might become invalid. */
4420 if (merge_set)
4421 {
4422 unsigned i;
4423 bitmap_iterator bi;
4424
4425 EXECUTE_IF_SET_IN_BITMAP (merge_set, 0, i, bi)
4426 remove_reg_equal_equiv_notes_for_regno (i);
4427
4428 BITMAP_FREE (merge_set);
4429 }
4430
4431 reorder_insns (head, end, PREV_INSN (earliest));
4432 }
4433
4434 /* Remove the jump and edge if we can. */
4435 if (other_bb == new_dest)
4436 {
4437 delete_insn (jump);
4438 remove_edge (BRANCH_EDGE (test_bb));
4439 /* ??? Can't merge blocks here, as then_bb is still in use.
4440 At minimum, the merge will get done just before bb-reorder. */
4441 }
4442
4443 return TRUE;
4444
4445 cancel:
4446 cancel_changes (0);
4447
4448 if (merge_set)
4449 BITMAP_FREE (merge_set);
4450
4451 return FALSE;
4452 }
4453 \f
4454 /* Main entry point for all if-conversion. AFTER_COMBINE is true if
4455 we are after combine pass. */
4456
4457 static void
4458 if_convert (bool after_combine)
4459 {
4460 basic_block bb;
4461 int pass;
4462
4463 if (optimize == 1)
4464 {
4465 df_live_add_problem ();
4466 df_live_set_all_dirty ();
4467 }
4468
4469 /* Record whether we are after combine pass. */
4470 ifcvt_after_combine = after_combine;
4471 num_possible_if_blocks = 0;
4472 num_updated_if_blocks = 0;
4473 num_true_changes = 0;
4474
4475 loop_optimizer_init (AVOID_CFG_MODIFICATIONS);
4476 mark_loop_exit_edges ();
4477 loop_optimizer_finalize ();
4478 free_dominance_info (CDI_DOMINATORS);
4479
4480 /* Compute postdominators. */
4481 calculate_dominance_info (CDI_POST_DOMINATORS);
4482
4483 df_set_flags (DF_LR_RUN_DCE);
4484
4485 /* Go through each of the basic blocks looking for things to convert. If we
4486 have conditional execution, we make multiple passes to allow us to handle
4487 IF-THEN{-ELSE} blocks within other IF-THEN{-ELSE} blocks. */
4488 pass = 0;
4489 do
4490 {
4491 df_analyze ();
4492 /* Only need to do dce on the first pass. */
4493 df_clear_flags (DF_LR_RUN_DCE);
4494 cond_exec_changed_p = FALSE;
4495 pass++;
4496
4497 #ifdef IFCVT_MULTIPLE_DUMPS
4498 if (dump_file && pass > 1)
4499 fprintf (dump_file, "\n\n========== Pass %d ==========\n", pass);
4500 #endif
4501
4502 FOR_EACH_BB_FN (bb, cfun)
4503 {
4504 basic_block new_bb;
4505 while (!df_get_bb_dirty (bb)
4506 && (new_bb = find_if_header (bb, pass)) != NULL)
4507 bb = new_bb;
4508 }
4509
4510 #ifdef IFCVT_MULTIPLE_DUMPS
4511 if (dump_file && cond_exec_changed_p)
4512 print_rtl_with_bb (dump_file, get_insns (), dump_flags);
4513 #endif
4514 }
4515 while (cond_exec_changed_p);
4516
4517 #ifdef IFCVT_MULTIPLE_DUMPS
4518 if (dump_file)
4519 fprintf (dump_file, "\n\n========== no more changes\n");
4520 #endif
4521
4522 free_dominance_info (CDI_POST_DOMINATORS);
4523
4524 if (dump_file)
4525 fflush (dump_file);
4526
4527 clear_aux_for_blocks ();
4528
4529 /* If we allocated new pseudos, we must resize the array for sched1. */
4530 if (max_regno < max_reg_num ())
4531 max_regno = max_reg_num ();
4532
4533 /* Write the final stats. */
4534 if (dump_file && num_possible_if_blocks > 0)
4535 {
4536 fprintf (dump_file,
4537 "\n%d possible IF blocks searched.\n",
4538 num_possible_if_blocks);
4539 fprintf (dump_file,
4540 "%d IF blocks converted.\n",
4541 num_updated_if_blocks);
4542 fprintf (dump_file,
4543 "%d true changes made.\n\n\n",
4544 num_true_changes);
4545 }
4546
4547 if (optimize == 1)
4548 df_remove_problem (df_live);
4549
4550 #ifdef ENABLE_CHECKING
4551 verify_flow_info ();
4552 #endif
4553 }
4554 \f
4555 /* If-conversion and CFG cleanup. */
4556 static unsigned int
4557 rest_of_handle_if_conversion (void)
4558 {
4559 if (flag_if_conversion)
4560 {
4561 if (dump_file)
4562 {
4563 dump_reg_info (dump_file);
4564 dump_flow_info (dump_file, dump_flags);
4565 }
4566 cleanup_cfg (CLEANUP_EXPENSIVE);
4567 if_convert (false);
4568 }
4569
4570 cleanup_cfg (0);
4571 return 0;
4572 }
4573
4574 namespace {
4575
4576 const pass_data pass_data_rtl_ifcvt =
4577 {
4578 RTL_PASS, /* type */
4579 "ce1", /* name */
4580 OPTGROUP_NONE, /* optinfo_flags */
4581 TV_IFCVT, /* tv_id */
4582 0, /* properties_required */
4583 0, /* properties_provided */
4584 0, /* properties_destroyed */
4585 0, /* todo_flags_start */
4586 TODO_df_finish, /* todo_flags_finish */
4587 };
4588
4589 class pass_rtl_ifcvt : public rtl_opt_pass
4590 {
4591 public:
4592 pass_rtl_ifcvt (gcc::context *ctxt)
4593 : rtl_opt_pass (pass_data_rtl_ifcvt, ctxt)
4594 {}
4595
4596 /* opt_pass methods: */
4597 virtual bool gate (function *)
4598 {
4599 return (optimize > 0) && dbg_cnt (if_conversion);
4600 }
4601
4602 virtual unsigned int execute (function *)
4603 {
4604 return rest_of_handle_if_conversion ();
4605 }
4606
4607 }; // class pass_rtl_ifcvt
4608
4609 } // anon namespace
4610
4611 rtl_opt_pass *
4612 make_pass_rtl_ifcvt (gcc::context *ctxt)
4613 {
4614 return new pass_rtl_ifcvt (ctxt);
4615 }
4616
4617
4618 /* Rerun if-conversion, as combine may have simplified things enough
4619 to now meet sequence length restrictions. */
4620
4621 namespace {
4622
4623 const pass_data pass_data_if_after_combine =
4624 {
4625 RTL_PASS, /* type */
4626 "ce2", /* name */
4627 OPTGROUP_NONE, /* optinfo_flags */
4628 TV_IFCVT, /* tv_id */
4629 0, /* properties_required */
4630 0, /* properties_provided */
4631 0, /* properties_destroyed */
4632 0, /* todo_flags_start */
4633 TODO_df_finish, /* todo_flags_finish */
4634 };
4635
4636 class pass_if_after_combine : public rtl_opt_pass
4637 {
4638 public:
4639 pass_if_after_combine (gcc::context *ctxt)
4640 : rtl_opt_pass (pass_data_if_after_combine, ctxt)
4641 {}
4642
4643 /* opt_pass methods: */
4644 virtual bool gate (function *)
4645 {
4646 return optimize > 0 && flag_if_conversion
4647 && dbg_cnt (if_after_combine);
4648 }
4649
4650 virtual unsigned int execute (function *)
4651 {
4652 if_convert (true);
4653 return 0;
4654 }
4655
4656 }; // class pass_if_after_combine
4657
4658 } // anon namespace
4659
4660 rtl_opt_pass *
4661 make_pass_if_after_combine (gcc::context *ctxt)
4662 {
4663 return new pass_if_after_combine (ctxt);
4664 }
4665
4666
4667 namespace {
4668
4669 const pass_data pass_data_if_after_reload =
4670 {
4671 RTL_PASS, /* type */
4672 "ce3", /* name */
4673 OPTGROUP_NONE, /* optinfo_flags */
4674 TV_IFCVT2, /* tv_id */
4675 0, /* properties_required */
4676 0, /* properties_provided */
4677 0, /* properties_destroyed */
4678 0, /* todo_flags_start */
4679 TODO_df_finish, /* todo_flags_finish */
4680 };
4681
4682 class pass_if_after_reload : public rtl_opt_pass
4683 {
4684 public:
4685 pass_if_after_reload (gcc::context *ctxt)
4686 : rtl_opt_pass (pass_data_if_after_reload, ctxt)
4687 {}
4688
4689 /* opt_pass methods: */
4690 virtual bool gate (function *)
4691 {
4692 return optimize > 0 && flag_if_conversion2
4693 && dbg_cnt (if_after_reload);
4694 }
4695
4696 virtual unsigned int execute (function *)
4697 {
4698 if_convert (true);
4699 return 0;
4700 }
4701
4702 }; // class pass_if_after_reload
4703
4704 } // anon namespace
4705
4706 rtl_opt_pass *
4707 make_pass_if_after_reload (gcc::context *ctxt)
4708 {
4709 return new pass_if_after_reload (ctxt);
4710 }