Debian Codegraph @

gcc/ifcvt.cc

raw
1 /* If-conversion support.
2 Copyright (C) 2000-2025 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 "backend.h"
24 #include "target.h"
25 #include "rtl.h"
26 #include "tree.h"
27 #include "cfghooks.h"
28 #include "df.h"
29 #include "memmodel.h"
30 #include "tm_p.h"
31 #include "expmed.h"
32 #include "optabs.h"
33 #include "regs.h"
34 #include "emit-rtl.h"
35 #include "recog.h"
36
37 #include "cfgrtl.h"
38 #include "cfganal.h"
39 #include "cfgcleanup.h"
40 #include "expr.h"
41 #include "output.h"
42 #include "cfgloop.h"
43 #include "tree-pass.h"
44 #include "dbgcnt.h"
45 #include "shrink-wrap.h"
46 #include "rtl-iter.h"
47 #include "ifcvt.h"
48
49 #ifndef MAX_CONDITIONAL_EXECUTE
50 #define MAX_CONDITIONAL_EXECUTE \
51 (BRANCH_COST (optimize_function_for_speed_p (cfun), false) \
52 + 1)
53 #endif
54
55 #define IFCVT_MULTIPLE_DUMPS 1
56
57 #define NULL_BLOCK ((basic_block) NULL)
58
59 /* True if after combine pass. */
60 static bool ifcvt_after_combine;
61
62 /* True if the target has the cbranchcc4 optab. */
63 static bool have_cbranchcc4;
64
65 /* # of IF-THEN or IF-THEN-ELSE blocks we looked at */
66 static int num_possible_if_blocks;
67
68 /* # of IF-THEN or IF-THEN-ELSE blocks were converted to conditional
69 execution. */
70 static int num_updated_if_blocks;
71
72 /* # of changes made. */
73 static int num_true_changes;
74
75 /* Whether conditional execution changes were made. */
76 static bool cond_exec_changed_p;
77
78 /* Forward references. */
79 static int count_bb_insns (const_basic_block);
80 static bool cheap_bb_rtx_cost_p (const_basic_block, profile_probability, int);
81 static rtx_insn *first_active_insn (basic_block);
82 static rtx_insn *last_active_insn (basic_block, bool);
83 static rtx_insn *find_active_insn_before (basic_block, rtx_insn *);
84 static rtx_insn *find_active_insn_after (basic_block, rtx_insn *);
85 static basic_block block_fallthru (basic_block);
86 static rtx cond_exec_get_condition (rtx_insn *, bool);
87 static rtx noce_get_condition (rtx_insn *, rtx_insn **, bool);
88 static bool noce_operand_ok (const_rtx);
89 static void merge_if_block (ce_if_block *);
90 static bool find_cond_trap (basic_block, edge, edge);
91 static basic_block find_if_header (basic_block, int);
92 static int block_jumps_and_fallthru (basic_block, basic_block);
93 static bool noce_find_if_block (basic_block, edge, edge, int);
94 static bool cond_exec_find_if_block (ce_if_block *);
95 static bool find_if_case_1 (basic_block, edge, edge);
96 static bool find_if_case_2 (basic_block, edge, edge);
97 static bool dead_or_predicable (basic_block, basic_block, basic_block,
98 edge, bool);
99 static void noce_emit_move_insn (rtx, rtx);
100 static rtx_insn *block_has_only_trap (basic_block);
101 static void init_noce_multiple_sets_info (basic_block,
102 auto_delete_vec<noce_multiple_sets_info> &);
103 static bool noce_convert_multiple_sets_1 (struct noce_if_info *,
104 auto_delete_vec<noce_multiple_sets_info> &, int *);
105
106 /* Count the number of non-jump active insns in BB. */
107
108 static int
109 count_bb_insns (const_basic_block bb)
110 {
111 int count = 0;
112 rtx_insn *insn = BB_HEAD (bb);
113
114 while (1)
115 {
116 if (active_insn_p (insn) && !JUMP_P (insn))
117 count++;
118
119 if (insn == BB_END (bb))
120 break;
121 insn = NEXT_INSN (insn);
122 }
123
124 return count;
125 }
126
127 /* Determine whether the total insn_cost on non-jump insns in
128 basic block BB is less than MAX_COST. This function returns
129 false if the cost of any instruction could not be estimated.
130
131 The cost of the non-jump insns in BB is scaled by REG_BR_PROB_BASE
132 as those insns are being speculated. MAX_COST is scaled with SCALE
133 plus a small fudge factor. */
134
135 static bool
136 cheap_bb_rtx_cost_p (const_basic_block bb,
137 profile_probability prob, int max_cost)
138 {
139 int count = 0;
140 rtx_insn *insn = BB_HEAD (bb);
141 bool speed = optimize_bb_for_speed_p (bb);
142 int scale = prob.initialized_p () ? prob.to_reg_br_prob_base ()
143 : REG_BR_PROB_BASE;
144
145 /* Set scale to REG_BR_PROB_BASE to void the identical scaling
146 applied to insn_cost when optimizing for size. Only do
147 this after combine because if-conversion might interfere with
148 passes before combine.
149
150 Use optimize_function_for_speed_p instead of the pre-defined
151 variable speed to make sure it is set to same value for all
152 basic blocks in one if-conversion transformation. */
153 if (!optimize_function_for_speed_p (cfun) && ifcvt_after_combine)
154 scale = REG_BR_PROB_BASE;
155 /* Our branch probability/scaling factors are just estimates and don't
156 account for cases where we can get speculation for free and other
157 secondary benefits. So we fudge the scale factor to make speculating
158 appear a little more profitable when optimizing for performance. */
159 else
160 scale += REG_BR_PROB_BASE / 8;
161
162
163 max_cost *= scale;
164
165 while (1)
166 {
167 if (NONJUMP_INSN_P (insn))
168 {
169 /* Inline-asm's cost is not very estimatable.
170 It could be a costly instruction but the
171 estimate would be the same as a non costly
172 instruction. */
173 if (asm_noperands (PATTERN (insn)) >= 0)
174 return false;
175
176 int cost = insn_cost (insn, speed) * REG_BR_PROB_BASE;
177 if (cost == 0)
178 return false;
179
180 /* If this instruction is the load or set of a "stack" register,
181 such as a floating point register on x87, then the cost of
182 speculatively executing this insn may need to include
183 the additional cost of popping its result off of the
184 register stack. Unfortunately, correctly recognizing and
185 accounting for this additional overhead is tricky, so for
186 now we simply prohibit such speculative execution. */
187 #ifdef STACK_REGS
188 {
189 rtx set = single_set (insn);
190 if (set && STACK_REG_P (SET_DEST (set)))
191 return false;
192 }
193 #endif
194
195 count += cost;
196 if (count >= max_cost)
197 return false;
198 }
199 else if (CALL_P (insn))
200 return false;
201
202 if (insn == BB_END (bb))
203 break;
204 insn = NEXT_INSN (insn);
205 }
206
207 return true;
208 }
209
210 /* Return the first non-jump active insn in the basic block. */
211
212 static rtx_insn *
213 first_active_insn (basic_block bb)
214 {
215 rtx_insn *insn = BB_HEAD (bb);
216
217 if (LABEL_P (insn))
218 {
219 if (insn == BB_END (bb))
220 return NULL;
221 insn = NEXT_INSN (insn);
222 }
223
224 while (NOTE_P (insn) || DEBUG_INSN_P (insn))
225 {
226 if (insn == BB_END (bb))
227 return NULL;
228 insn = NEXT_INSN (insn);
229 }
230
231 if (JUMP_P (insn))
232 return NULL;
233
234 return insn;
235 }
236
237 /* Return the last non-jump active (non-jump) insn in the basic block. */
238
239 static rtx_insn *
240 last_active_insn (basic_block bb, bool skip_use_p)
241 {
242 rtx_insn *insn = BB_END (bb);
243 rtx_insn *head = BB_HEAD (bb);
244
245 while (NOTE_P (insn)
246 || JUMP_P (insn)
247 || DEBUG_INSN_P (insn)
248 || (skip_use_p
249 && NONJUMP_INSN_P (insn)
250 && GET_CODE (PATTERN (insn)) == USE))
251 {
252 if (insn == head)
253 return NULL;
254 insn = PREV_INSN (insn);
255 }
256
257 if (LABEL_P (insn))
258 return NULL;
259
260 return insn;
261 }
262
263 /* Return the active insn before INSN inside basic block CURR_BB. */
264
265 static rtx_insn *
266 find_active_insn_before (basic_block curr_bb, rtx_insn *insn)
267 {
268 if (!insn || insn == BB_HEAD (curr_bb))
269 return NULL;
270
271 while ((insn = PREV_INSN (insn)) != NULL_RTX)
272 {
273 if (NONJUMP_INSN_P (insn) || JUMP_P (insn) || CALL_P (insn))
274 break;
275
276 /* No other active insn all the way to the start of the basic block. */
277 if (insn == BB_HEAD (curr_bb))
278 return NULL;
279 }
280
281 return insn;
282 }
283
284 /* Return the active insn after INSN inside basic block CURR_BB. */
285
286 static rtx_insn *
287 find_active_insn_after (basic_block curr_bb, rtx_insn *insn)
288 {
289 if (!insn || insn == BB_END (curr_bb))
290 return NULL;
291
292 while ((insn = NEXT_INSN (insn)) != NULL_RTX)
293 {
294 if (NONJUMP_INSN_P (insn) || JUMP_P (insn) || CALL_P (insn))
295 break;
296
297 /* No other active insn all the way to the end of the basic block. */
298 if (insn == BB_END (curr_bb))
299 return NULL;
300 }
301
302 return insn;
303 }
304
305 /* Return the basic block reached by falling though the basic block BB. */
306
307 static basic_block
308 block_fallthru (basic_block bb)
309 {
310 edge e = find_fallthru_edge (bb->succs);
311
312 return (e) ? e->dest : NULL_BLOCK;
313 }
314
315 /* Return true if RTXs A and B can be safely interchanged. */
316
317 static bool
318 rtx_interchangeable_p (const_rtx a, const_rtx b)
319 {
320 if (!rtx_equal_p (a, b))
321 return false;
322
323 if (GET_CODE (a) != MEM)
324 return true;
325
326 /* A dead type-unsafe memory reference is legal, but a live type-unsafe memory
327 reference is not. Interchanging a dead type-unsafe memory reference with
328 a live type-safe one creates a live type-unsafe memory reference, in other
329 words, it makes the program illegal.
330 We check here conservatively whether the two memory references have equal
331 memory attributes. */
332
333 return mem_attrs_eq_p (get_mem_attrs (a), get_mem_attrs (b));
334 }
335
336
337 /* Go through a bunch of insns, converting them to conditional
338 execution format if possible. Return TRUE if all of the non-note
339 insns were processed. */
340
341 static bool
342 cond_exec_process_insns (ce_if_block *ce_info ATTRIBUTE_UNUSED,
343 /* if block information */rtx_insn *start,
344 /* first insn to look at */rtx end,
345 /* last insn to look at */rtx test,
346 /* conditional execution test */profile_probability
347 prob_val,
348 /* probability of branch taken. */bool mod_ok)
349 {
350 bool must_be_last = false;
351 rtx_insn *insn;
352 rtx xtest;
353 rtx pattern;
354
355 if (!start || !end)
356 return false;
357
358 for (insn = start; ; insn = NEXT_INSN (insn))
359 {
360 /* dwarf2out can't cope with conditional prologues. */
361 if (NOTE_P (insn) && NOTE_KIND (insn) == NOTE_INSN_PROLOGUE_END)
362 return false;
363
364 if (NOTE_P (insn) || DEBUG_INSN_P (insn))
365 goto insn_done;
366
367 gcc_assert (NONJUMP_INSN_P (insn) || CALL_P (insn));
368
369 /* dwarf2out can't cope with conditional unwind info. */
370 if (RTX_FRAME_RELATED_P (insn))
371 return false;
372
373 /* Remove USE insns that get in the way. */
374 if (reload_completed && GET_CODE (PATTERN (insn)) == USE)
375 {
376 /* ??? Ug. Actually unlinking the thing is problematic,
377 given what we'd have to coordinate with our callers. */
378 SET_INSN_DELETED (insn);
379 goto insn_done;
380 }
381
382 /* Last insn wasn't last? */
383 if (must_be_last)
384 return false;
385
386 if (modified_in_p (test, insn))
387 {
388 if (!mod_ok)
389 return false;
390 must_be_last = true;
391 }
392
393 /* Now build the conditional form of the instruction. */
394 pattern = PATTERN (insn);
395 xtest = copy_rtx (test);
396
397 /* If this is already a COND_EXEC, rewrite the test to be an AND of the
398 two conditions. */
399 if (GET_CODE (pattern) == COND_EXEC)
400 {
401 if (GET_MODE (xtest) != GET_MODE (COND_EXEC_TEST (pattern)))
402 return false;
403
404 xtest = gen_rtx_AND (GET_MODE (xtest), xtest,
405 COND_EXEC_TEST (pattern));
406 pattern = COND_EXEC_CODE (pattern);
407 }
408
409 pattern = gen_rtx_COND_EXEC (VOIDmode, xtest, pattern);
410
411 /* If the machine needs to modify the insn being conditionally executed,
412 say for example to force a constant integer operand into a temp
413 register, do so here. */
414 #ifdef IFCVT_MODIFY_INSN
415 IFCVT_MODIFY_INSN (ce_info, pattern, insn);
416 if (! pattern)
417 return false;
418 #endif
419
420 validate_change (insn, &PATTERN (insn), pattern, 1);
421
422 if (CALL_P (insn) && prob_val.initialized_p ())
423 validate_change (insn, &REG_NOTES (insn),
424 gen_rtx_INT_LIST ((machine_mode) REG_BR_PROB,
425 prob_val.to_reg_br_prob_note (),
426 REG_NOTES (insn)), 1);
427
428 insn_done:
429 if (insn == end)
430 break;
431 }
432
433 return true;
434 }
435
436 /* Return the condition for a jump. Do not do any special processing. */
437
438 static rtx
439 cond_exec_get_condition (rtx_insn *jump, bool get_reversed = false)
440 {
441 rtx test_if, cond;
442
443 if (any_condjump_p (jump))
444 test_if = SET_SRC (pc_set (jump));
445 else
446 return NULL_RTX;
447 cond = XEXP (test_if, 0);
448
449 /* If this branches to JUMP_LABEL when the condition is false,
450 reverse the condition. */
451 if (get_reversed
452 || (GET_CODE (XEXP (test_if, 2)) == LABEL_REF
453 && label_ref_label (XEXP (test_if, 2))
454 == JUMP_LABEL (jump)))
455 {
456 enum rtx_code rev = reversed_comparison_code (cond, jump);
457 if (rev == UNKNOWN)
458 return NULL_RTX;
459
460 cond = gen_rtx_fmt_ee (rev, GET_MODE (cond), XEXP (cond, 0),
461 XEXP (cond, 1));
462 }
463
464 return cond;
465 }
466
467 /* Given a simple IF-THEN or IF-THEN-ELSE block, attempt to convert it
468 to conditional execution. Return TRUE if we were successful at
469 converting the block. */
470
471 static bool
472 cond_exec_process_if_block (ce_if_block * ce_info,
473 /* if block information */bool do_multiple_p)
474 {
475 basic_block test_bb = ce_info->test_bb; /* last test block */
476 basic_block then_bb = ce_info->then_bb; /* THEN */
477 basic_block else_bb = ce_info->else_bb; /* ELSE or NULL */
478 rtx test_expr; /* expression in IF_THEN_ELSE that is tested */
479 rtx_insn *then_start; /* first insn in THEN block */
480 rtx_insn *then_end; /* last insn + 1 in THEN block */
481 rtx_insn *else_start = NULL; /* first insn in ELSE block or NULL */
482 rtx_insn *else_end = NULL; /* last insn + 1 in ELSE block */
483 int max; /* max # of insns to convert. */
484 bool then_mod_ok; /* whether conditional mods are ok in THEN */
485 rtx true_expr; /* test for else block insns */
486 rtx false_expr; /* test for then block insns */
487 profile_probability true_prob_val;/* probability of else block */
488 profile_probability false_prob_val;/* probability of then block */
489 rtx_insn *then_last_head = NULL; /* Last match at the head of THEN */
490 rtx_insn *else_last_head = NULL; /* Last match at the head of ELSE */
491 rtx_insn *then_first_tail = NULL; /* First match at the tail of THEN */
492 rtx_insn *else_first_tail = NULL; /* First match at the tail of ELSE */
493 int then_n_insns, else_n_insns, n_insns;
494 enum rtx_code false_code;
495 rtx note;
496
497 /* If test is comprised of && or || elements, and we've failed at handling
498 all of them together, just use the last test if it is the special case of
499 && elements without an ELSE block. */
500 if (!do_multiple_p && ce_info->num_multiple_test_blocks)
501 {
502 if (else_bb || ! ce_info->and_and_p)
503 return false;
504
505 ce_info->test_bb = test_bb = ce_info->last_test_bb;
506 ce_info->num_multiple_test_blocks = 0;
507 ce_info->num_and_and_blocks = 0;
508 ce_info->num_or_or_blocks = 0;
509 }
510
511 /* Find the conditional jump to the ELSE or JOIN part, and isolate
512 the test. */
513 test_expr = cond_exec_get_condition (BB_END (test_bb));
514 if (! test_expr)
515 return false;
516
517 /* If the conditional jump is more than just a conditional jump,
518 then we cannot do conditional execution conversion on this block. */
519 if (! onlyjump_p (BB_END (test_bb)))
520 return false;
521
522 /* Collect the bounds of where we're to search, skipping any labels, jumps
523 and notes at the beginning and end of the block. Then count the total
524 number of insns and see if it is small enough to convert. */
525 then_start = first_active_insn (then_bb);
526 then_end = last_active_insn (then_bb, true);
527 then_n_insns = ce_info->num_then_insns = count_bb_insns (then_bb);
528 n_insns = then_n_insns;
529 max = MAX_CONDITIONAL_EXECUTE;
530
531 if (else_bb)
532 {
533 int n_matching;
534
535 max *= 2;
536 else_start = first_active_insn (else_bb);
537 else_end = last_active_insn (else_bb, true);
538 else_n_insns = ce_info->num_else_insns = count_bb_insns (else_bb);
539 n_insns += else_n_insns;
540
541 /* Look for matching sequences at the head and tail of the two blocks,
542 and limit the range of insns to be converted if possible. */
543 n_matching = flow_find_cross_jump (then_bb, else_bb,
544 &then_first_tail, &else_first_tail,
545 NULL);
546 if (then_first_tail == BB_HEAD (then_bb))
547 then_start = then_end = NULL;
548 if (else_first_tail == BB_HEAD (else_bb))
549 else_start = else_end = NULL;
550
551 if (n_matching > 0)
552 {
553 if (then_end)
554 then_end = find_active_insn_before (then_bb, then_first_tail);
555 if (else_end)
556 else_end = find_active_insn_before (else_bb, else_first_tail);
557 n_insns -= 2 * n_matching;
558 }
559
560 if (then_start
561 && else_start
562 && then_n_insns > n_matching
563 && else_n_insns > n_matching)
564 {
565 int longest_match = MIN (then_n_insns - n_matching,
566 else_n_insns - n_matching);
567 n_matching
568 = flow_find_head_matching_sequence (then_bb, else_bb,
569 &then_last_head,
570 &else_last_head,
571 longest_match);
572
573 if (n_matching > 0)
574 {
575 rtx_insn *insn;
576
577 /* We won't pass the insns in the head sequence to
578 cond_exec_process_insns, so we need to test them here
579 to make sure that they don't clobber the condition. */
580 for (insn = BB_HEAD (then_bb);
581 insn != NEXT_INSN (then_last_head);
582 insn = NEXT_INSN (insn))
583 if (!LABEL_P (insn) && !NOTE_P (insn)
584 && !DEBUG_INSN_P (insn)
585 && modified_in_p (test_expr, insn))
586 return false;
587 }
588
589 if (then_last_head == then_end)
590 then_start = then_end = NULL;
591 if (else_last_head == else_end)
592 else_start = else_end = NULL;
593
594 if (n_matching > 0)
595 {
596 if (then_start)
597 then_start = find_active_insn_after (then_bb, then_last_head);
598 if (else_start)
599 else_start = find_active_insn_after (else_bb, else_last_head);
600 n_insns -= 2 * n_matching;
601 }
602 }
603 }
604
605 if (n_insns > max)
606 return false;
607
608 /* Map test_expr/test_jump into the appropriate MD tests to use on
609 the conditionally executed code. */
610
611 true_expr = test_expr;
612
613 false_code = reversed_comparison_code (true_expr, BB_END (test_bb));
614 if (false_code != UNKNOWN)
615 false_expr = gen_rtx_fmt_ee (false_code, GET_MODE (true_expr),
616 XEXP (true_expr, 0), XEXP (true_expr, 1));
617 else
618 false_expr = NULL_RTX;
619
620 #ifdef IFCVT_MODIFY_TESTS
621 /* If the machine description needs to modify the tests, such as setting a
622 conditional execution register from a comparison, it can do so here. */
623 IFCVT_MODIFY_TESTS (ce_info, true_expr, false_expr);
624
625 /* See if the conversion failed. */
626 if (!true_expr || !false_expr)
627 goto fail;
628 #endif
629
630 note = find_reg_note (BB_END (test_bb), REG_BR_PROB, NULL_RTX);
631 if (note)
632 {
633 true_prob_val = profile_probability::from_reg_br_prob_note (XINT (note, 0));
634 false_prob_val = true_prob_val.invert ();
635 }
636 else
637 {
638 true_prob_val = profile_probability::uninitialized ();
639 false_prob_val = profile_probability::uninitialized ();
640 }
641
642 /* If we have && or || tests, do them here. These tests are in the adjacent
643 blocks after the first block containing the test. */
644 if (ce_info->num_multiple_test_blocks > 0)
645 {
646 basic_block bb = test_bb;
647 basic_block last_test_bb = ce_info->last_test_bb;
648
649 if (! false_expr)
650 goto fail;
651
652 do
653 {
654 rtx_insn *start, *end;
655 rtx t, f;
656 enum rtx_code f_code;
657
658 bb = block_fallthru (bb);
659 start = first_active_insn (bb);
660 end = last_active_insn (bb, true);
661 if (start
662 && ! cond_exec_process_insns (ce_info, start, end, false_expr,
663 false_prob_val, false))
664 goto fail;
665
666 /* If the conditional jump is more than just a conditional jump, then
667 we cannot do conditional execution conversion on this block. */
668 if (! onlyjump_p (BB_END (bb)))
669 goto fail;
670
671 /* Find the conditional jump and isolate the test. */
672 t = cond_exec_get_condition (BB_END (bb));
673 if (! t)
674 goto fail;
675
676 f_code = reversed_comparison_code (t, BB_END (bb));
677 if (f_code == UNKNOWN)
678 goto fail;
679
680 f = gen_rtx_fmt_ee (f_code, GET_MODE (t), XEXP (t, 0), XEXP (t, 1));
681 if (ce_info->and_and_p)
682 {
683 t = gen_rtx_AND (GET_MODE (t), true_expr, t);
684 f = gen_rtx_IOR (GET_MODE (t), false_expr, f);
685 }
686 else
687 {
688 t = gen_rtx_IOR (GET_MODE (t), true_expr, t);
689 f = gen_rtx_AND (GET_MODE (t), false_expr, f);
690 }
691
692 /* If the machine description needs to modify the tests, such as
693 setting a conditional execution register from a comparison, it can
694 do so here. */
695 #ifdef IFCVT_MODIFY_MULTIPLE_TESTS
696 IFCVT_MODIFY_MULTIPLE_TESTS (ce_info, bb, t, f);
697
698 /* See if the conversion failed. */
699 if (!t || !f)
700 goto fail;
701 #endif
702
703 true_expr = t;
704 false_expr = f;
705 }
706 while (bb != last_test_bb);
707 }
708
709 /* For IF-THEN-ELSE blocks, we don't allow modifications of the test
710 on then THEN block. */
711 then_mod_ok = (else_bb == NULL_BLOCK);
712
713 /* Go through the THEN and ELSE blocks converting the insns if possible
714 to conditional execution. */
715
716 if (then_end
717 && (! false_expr
718 || ! cond_exec_process_insns (ce_info, then_start, then_end,
719 false_expr, false_prob_val,
720 then_mod_ok)))
721 goto fail;
722
723 if (else_bb && else_end
724 && ! cond_exec_process_insns (ce_info, else_start, else_end,
725 true_expr, true_prob_val, true))
726 goto fail;
727
728 /* If we cannot apply the changes, fail. Do not go through the normal fail
729 processing, since apply_change_group will call cancel_changes. */
730 if (! apply_change_group ())
731 {
732 #ifdef IFCVT_MODIFY_CANCEL
733 /* Cancel any machine dependent changes. */
734 IFCVT_MODIFY_CANCEL (ce_info);
735 #endif
736 return false;
737 }
738
739 #ifdef IFCVT_MODIFY_FINAL
740 /* Do any machine dependent final modifications. */
741 IFCVT_MODIFY_FINAL (ce_info);
742 #endif
743
744 /* Conversion succeeded. */
745 if (dump_file)
746 fprintf (dump_file, "%d insn%s converted to conditional execution.\n",
747 n_insns, (n_insns == 1) ? " was" : "s were");
748
749 /* Merge the blocks! If we had matching sequences, make sure to delete one
750 copy at the appropriate location first: delete the copy in the THEN branch
751 for a tail sequence so that the remaining one is executed last for both
752 branches, and delete the copy in the ELSE branch for a head sequence so
753 that the remaining one is executed first for both branches. */
754 if (then_first_tail)
755 {
756 rtx_insn *from = then_first_tail;
757 if (!INSN_P (from))
758 from = find_active_insn_after (then_bb, from);
759 delete_insn_chain (from, get_last_bb_insn (then_bb), false);
760 }
761 if (else_last_head)
762 delete_insn_chain (first_active_insn (else_bb), else_last_head, false);
763
764 merge_if_block (ce_info);
765 cond_exec_changed_p = true;
766 return true;
767
768 fail:
769 #ifdef IFCVT_MODIFY_CANCEL
770 /* Cancel any machine dependent changes. */
771 IFCVT_MODIFY_CANCEL (ce_info);
772 #endif
773
774 cancel_changes (0);
775 return false;
776 }
777
778 static rtx noce_emit_store_flag (struct noce_if_info *, rtx, bool, int);
779 static bool noce_try_move (struct noce_if_info *);
780 static bool noce_try_ifelse_collapse (struct noce_if_info *);
781 static bool noce_try_store_flag (struct noce_if_info *);
782 static bool noce_try_addcc (struct noce_if_info *);
783 static bool noce_try_store_flag_constants (struct noce_if_info *);
784 static bool noce_try_store_flag_mask (struct noce_if_info *);
785 static rtx noce_emit_cmove (struct noce_if_info *, rtx, enum rtx_code, rtx,
786 rtx, rtx, rtx, rtx = NULL, rtx = NULL);
787 static bool noce_try_cmove (struct noce_if_info *);
788 static bool noce_try_cmove_arith (struct noce_if_info *);
789 static rtx noce_get_alt_condition (struct noce_if_info *, rtx, rtx_insn **);
790 static bool noce_try_minmax (struct noce_if_info *);
791 static bool noce_try_abs (struct noce_if_info *);
792 static bool noce_try_sign_mask (struct noce_if_info *);
793 static int noce_try_cond_zero_arith (struct noce_if_info *);
794
795 /* Return the comparison code for reversed condition for IF_INFO,
796 or UNKNOWN if reversing the condition is not possible. */
797
798 static inline enum rtx_code
799 noce_reversed_cond_code (struct noce_if_info *if_info)
800 {
801 if (if_info->rev_cond)
802 return GET_CODE (if_info->rev_cond);
803 return reversed_comparison_code (if_info->cond, if_info->jump);
804 }
805
806 /* Return true if SEQ is a good candidate as a replacement for the
807 if-convertible sequence described in IF_INFO.
808 This is the default implementation that targets can override
809 through a target hook. */
810
811 bool
812 default_noce_conversion_profitable_p (rtx_insn *seq,
813 struct noce_if_info *if_info)
814 {
815 bool speed_p = if_info->speed_p;
816
817 /* Cost up the new sequence. */
818 unsigned int cost = seq_cost (seq, speed_p);
819
820 if (cost <= if_info->original_cost)
821 return true;
822
823 /* When compiling for size, we can make a reasonably accurately guess
824 at the size growth. When compiling for speed, use the maximum. */
825 return speed_p && cost <= if_info->max_seq_cost;
826 }
827
828 /* Helper function for noce_try_store_flag*. */
829
830 static rtx
831 noce_emit_store_flag (struct noce_if_info *if_info, rtx x, bool reversep,
832 int normalize)
833 {
834 rtx cond = if_info->cond;
835 bool cond_complex;
836 enum rtx_code code;
837
838 cond_complex = (! general_operand (XEXP (cond, 0), VOIDmode)
839 || ! general_operand (XEXP (cond, 1), VOIDmode));
840
841 /* If earliest == jump, or when the condition is complex, try to
842 build the store_flag insn directly. */
843
844 if (cond_complex)
845 {
846 rtx set = pc_set (if_info->jump);
847 cond = XEXP (SET_SRC (set), 0);
848 if (GET_CODE (XEXP (SET_SRC (set), 2)) == LABEL_REF
849 && label_ref_label (XEXP (SET_SRC (set), 2)) == JUMP_LABEL (if_info->jump))
850 reversep = !reversep;
851 if (if_info->then_else_reversed)
852 reversep = !reversep;
853 }
854 else if (reversep
855 && if_info->rev_cond
856 && general_operand (XEXP (if_info->rev_cond, 0), VOIDmode)
857 && general_operand (XEXP (if_info->rev_cond, 1), VOIDmode))
858 {
859 cond = if_info->rev_cond;
860 reversep = false;
861 }
862
863 if (reversep)
864 code = reversed_comparison_code (cond, if_info->jump);
865 else
866 code = GET_CODE (cond);
867
868 if ((if_info->cond_earliest == if_info->jump || cond_complex)
869 && (normalize == 0 || STORE_FLAG_VALUE == normalize))
870 {
871 rtx src = gen_rtx_fmt_ee (code, GET_MODE (x), XEXP (cond, 0),
872 XEXP (cond, 1));
873 rtx set = gen_rtx_SET (x, src);
874
875 start_sequence ();
876 rtx_insn *insn = emit_insn (set);
877
878 if (recog_memoized (insn) >= 0)
879 {
880 rtx_insn *seq = get_insns ();
881 end_sequence ();
882 emit_insn (seq);
883
884 if_info->cond_earliest = if_info->jump;
885
886 return x;
887 }
888
889 end_sequence ();
890 }
891
892 /* Don't even try if the comparison operands or the mode of X are weird. */
893 if (cond_complex || !SCALAR_INT_MODE_P (GET_MODE (x)))
894 return NULL_RTX;
895
896 /* Don't try if mode of X is more than the max fixed mode size. */
897 if (known_le (MAX_FIXED_MODE_SIZE, GET_MODE_BITSIZE (GET_MODE (x))))
898 return NULL_RTX;
899
900 return emit_store_flag (x, code, XEXP (cond, 0),
901 XEXP (cond, 1), VOIDmode,
902 (code == LTU || code == LEU
903 || code == GEU || code == GTU), normalize);
904 }
905
906 /* Return true if X can be safely forced into a register by copy_to_mode_reg
907 / force_operand. */
908
909 static bool
910 noce_can_force_operand (rtx x)
911 {
912 if (general_operand (x, VOIDmode))
913 return true;
914 if (SUBREG_P (x))
915 {
916 if (!noce_can_force_operand (SUBREG_REG (x)))
917 return false;
918 return true;
919 }
920 if (ARITHMETIC_P (x))
921 {
922 if (!noce_can_force_operand (XEXP (x, 0))
923 || !noce_can_force_operand (XEXP (x, 1)))
924 return false;
925 switch (GET_CODE (x))
926 {
927 case MULT:
928 case MOD:
929 case UDIV:
930 case UMOD:
931 return true;
932 case DIV:
933 if (INTEGRAL_MODE_P (GET_MODE (x)))
934 return true;
935 /* FALLTHRU */
936 default:
937 auto optab = code_to_optab (GET_CODE (x));
938 if (!optab)
939 return false;
940 return optab_handler (optab, GET_MODE (x));
941 }
942 }
943 if (UNARY_P (x))
944 {
945 if (!noce_can_force_operand (XEXP (x, 0)))
946 return false;
947 switch (GET_CODE (x))
948 {
949 case ZERO_EXTEND:
950 case SIGN_EXTEND:
951 case TRUNCATE:
952 case FLOAT_EXTEND:
953 case FLOAT_TRUNCATE:
954 case FIX:
955 case UNSIGNED_FIX:
956 case FLOAT:
957 case UNSIGNED_FLOAT:
958 return true;
959 default:
960 auto optab = code_to_optab (GET_CODE (x));
961 if (!optab)
962 return false;
963 return optab_handler (optab, GET_MODE (x));
964 }
965 }
966 return false;
967 }
968
969 /* Emit instruction to move an rtx, possibly into STRICT_LOW_PART.
970 X is the destination/target and Y is the value to copy. */
971
972 static void
973 noce_emit_move_insn (rtx x, rtx y)
974 {
975 machine_mode outmode;
976 rtx outer, inner;
977 poly_int64 bitpos;
978
979 if (GET_CODE (x) != STRICT_LOW_PART)
980 {
981 rtx_insn *seq, *insn;
982 rtx target;
983 optab ot;
984
985 start_sequence ();
986 /* Check that the SET_SRC is reasonable before calling emit_move_insn,
987 otherwise construct a suitable SET pattern ourselves. */
988 insn = (OBJECT_P (y) || CONSTANT_P (y) || GET_CODE (y) == SUBREG)
989 ? emit_move_insn (x, y)
990 : emit_insn (gen_rtx_SET (x, y));
991 seq = get_insns ();
992 end_sequence ();
993
994 if (recog_memoized (insn) <= 0)
995 {
996 if (GET_CODE (x) == ZERO_EXTRACT)
997 {
998 rtx op = XEXP (x, 0);
999 unsigned HOST_WIDE_INT size = INTVAL (XEXP (x, 1));
1000 unsigned HOST_WIDE_INT start = INTVAL (XEXP (x, 2));
1001
1002 /* store_bit_field expects START to be relative to
1003 BYTES_BIG_ENDIAN and adjusts this value for machines with
1004 BITS_BIG_ENDIAN != BYTES_BIG_ENDIAN. In order to be able to
1005 invoke store_bit_field again it is necessary to have the START
1006 value from the first call. */
1007 if (BITS_BIG_ENDIAN != BYTES_BIG_ENDIAN)
1008 {
1009 if (MEM_P (op))
1010 start = BITS_PER_UNIT - start - size;
1011 else
1012 {
1013 gcc_assert (REG_P (op));
1014 start = BITS_PER_WORD - start - size;
1015 }
1016 }
1017
1018 gcc_assert (start < (MEM_P (op) ? BITS_PER_UNIT : BITS_PER_WORD));
1019 store_bit_field (op, size, start, 0, 0, GET_MODE (x), y, false,
1020 false);
1021 return;
1022 }
1023
1024 switch (GET_RTX_CLASS (GET_CODE (y)))
1025 {
1026 case RTX_UNARY:
1027 ot = code_to_optab (GET_CODE (y));
1028 if (ot && noce_can_force_operand (XEXP (y, 0)))
1029 {
1030 start_sequence ();
1031 target = expand_unop (GET_MODE (y), ot, XEXP (y, 0), x, 0);
1032 if (target != NULL_RTX)
1033 {
1034 if (target != x)
1035 emit_move_insn (x, target);
1036 seq = get_insns ();
1037 }
1038 end_sequence ();
1039 }
1040 break;
1041
1042 case RTX_BIN_ARITH:
1043 case RTX_COMM_ARITH:
1044 ot = code_to_optab (GET_CODE (y));
1045 if (ot
1046 && noce_can_force_operand (XEXP (y, 0))
1047 && noce_can_force_operand (XEXP (y, 1)))
1048 {
1049 start_sequence ();
1050 target = expand_binop (GET_MODE (y), ot,
1051 XEXP (y, 0), XEXP (y, 1),
1052 x, 0, OPTAB_DIRECT);
1053 if (target != NULL_RTX)
1054 {
1055 if (target != x)
1056 emit_move_insn (x, target);
1057 seq = get_insns ();
1058 }
1059 end_sequence ();
1060 }
1061 break;
1062
1063 default:
1064 break;
1065 }
1066 }
1067
1068 emit_insn (seq);
1069 return;
1070 }
1071
1072 outer = XEXP (x, 0);
1073 inner = XEXP (outer, 0);
1074 outmode = GET_MODE (outer);
1075 bitpos = SUBREG_BYTE (outer) * BITS_PER_UNIT;
1076 store_bit_field (inner, GET_MODE_BITSIZE (outmode), bitpos,
1077 0, 0, outmode, y, false, false);
1078 }
1079
1080 /* Return the CC reg if it is used in COND. */
1081
1082 static rtx
1083 cc_in_cond (rtx cond)
1084 {
1085 if (have_cbranchcc4 && cond
1086 && GET_MODE_CLASS (GET_MODE (XEXP (cond, 0))) == MODE_CC)
1087 return XEXP (cond, 0);
1088
1089 return NULL_RTX;
1090 }
1091
1092 /* Return sequence of instructions generated by if conversion. This
1093 function calls end_sequence() to end the current stream, ensures
1094 that the instructions are unshared, recognizable non-jump insns.
1095 On failure, this function returns a NULL_RTX. */
1096
1097 static rtx_insn *
1098 end_ifcvt_sequence (struct noce_if_info *if_info)
1099 {
1100 rtx_insn *insn;
1101 rtx_insn *seq = get_insns ();
1102 rtx cc = cc_in_cond (if_info->cond);
1103
1104 set_used_flags (if_info->x);
1105 set_used_flags (if_info->cond);
1106 set_used_flags (if_info->a);
1107 set_used_flags (if_info->b);
1108
1109 for (insn = seq; insn; insn = NEXT_INSN (insn))
1110 set_used_flags (insn);
1111
1112 unshare_all_rtl_in_chain (seq);
1113 end_sequence ();
1114
1115 /* Make sure that all of the instructions emitted are recognizable,
1116 and that we haven't introduced a new jump instruction.
1117 As an exercise for the reader, build a general mechanism that
1118 allows proper placement of required clobbers. */
1119 for (insn = seq; insn; insn = NEXT_INSN (insn))
1120 if (JUMP_P (insn)
1121 || recog_memoized (insn) == -1
1122 /* Make sure new generated code does not clobber CC. */
1123 || (cc && set_of (cc, insn)))
1124 return NULL;
1125
1126 return seq;
1127 }
1128
1129 /* Return true iff the then and else basic block (if it exists)
1130 consist of a single simple set instruction. */
1131
1132 static bool
1133 noce_simple_bbs (struct noce_if_info *if_info)
1134 {
1135 if (!if_info->then_simple)
1136 return false;
1137
1138 if (if_info->else_bb)
1139 return if_info->else_simple;
1140
1141 return true;
1142 }
1143
1144 /* Convert "if (a != b) x = a; else x = b" into "x = a" and
1145 "if (a == b) x = a; else x = b" into "x = b". */
1146
1147 static bool
1148 noce_try_move (struct noce_if_info *if_info)
1149 {
1150 rtx cond = if_info->cond;
1151 enum rtx_code code = GET_CODE (cond);
1152 rtx y;
1153 rtx_insn *seq;
1154
1155 if (code != NE && code != EQ)
1156 return false;
1157
1158 if (!noce_simple_bbs (if_info))
1159 return false;
1160
1161 /* This optimization isn't valid if either A or B could be a NaN
1162 or a signed zero. */
1163 if (HONOR_NANS (if_info->x)
1164 || HONOR_SIGNED_ZEROS (if_info->x))
1165 return false;
1166
1167 /* Check whether the operands of the comparison are A and in
1168 either order. */
1169 if ((rtx_equal_p (if_info->a, XEXP (cond, 0))
1170 && rtx_equal_p (if_info->b, XEXP (cond, 1)))
1171 || (rtx_equal_p (if_info->a, XEXP (cond, 1))
1172 && rtx_equal_p (if_info->b, XEXP (cond, 0))))
1173 {
1174 if (!rtx_interchangeable_p (if_info->a, if_info->b))
1175 return false;
1176
1177 y = (code == EQ) ? if_info->a : if_info->b;
1178
1179 /* Avoid generating the move if the source is the destination. */
1180 if (! rtx_equal_p (if_info->x, y))
1181 {
1182 start_sequence ();
1183 noce_emit_move_insn (if_info->x, y);
1184 seq = end_ifcvt_sequence (if_info);
1185 if (!seq)
1186 return false;
1187
1188 emit_insn_before_setloc (seq, if_info->jump,
1189 INSN_LOCATION (if_info->insn_a));
1190 }
1191 if_info->transform_name = "noce_try_move";
1192 return true;
1193 }
1194 return false;
1195 }
1196
1197 /* Try forming an IF_THEN_ELSE (cond, b, a) and collapsing that
1198 through simplify_rtx. Sometimes that can eliminate the IF_THEN_ELSE.
1199 If that is the case, emit the result into x. */
1200
1201 static bool
1202 noce_try_ifelse_collapse (struct noce_if_info * if_info)
1203 {
1204 if (!noce_simple_bbs (if_info))
1205 return false;
1206
1207 machine_mode mode = GET_MODE (if_info->x);
1208 rtx if_then_else = simplify_gen_ternary (IF_THEN_ELSE, mode, mode,
1209 if_info->cond, if_info->b,
1210 if_info->a);
1211
1212 if (GET_CODE (if_then_else) == IF_THEN_ELSE)
1213 return false;
1214
1215 rtx_insn *seq;
1216 start_sequence ();
1217 noce_emit_move_insn (if_info->x, if_then_else);
1218 seq = end_ifcvt_sequence (if_info);
1219 if (!seq)
1220 return false;
1221
1222 emit_insn_before_setloc (seq, if_info->jump,
1223 INSN_LOCATION (if_info->insn_a));
1224
1225 if_info->transform_name = "noce_try_ifelse_collapse";
1226 return true;
1227 }
1228
1229
1230 /* Convert "if (test) x = 1; else x = 0".
1231
1232 Only try 0 and STORE_FLAG_VALUE here. Other combinations will be
1233 tried in noce_try_store_flag_constants after noce_try_cmove has had
1234 a go at the conversion. */
1235
1236 static bool
1237 noce_try_store_flag (struct noce_if_info *if_info)
1238 {
1239 bool reversep;
1240 rtx target;
1241 rtx_insn *seq;
1242
1243 if (!noce_simple_bbs (if_info))
1244 return false;
1245
1246 if (CONST_INT_P (if_info->b)
1247 && INTVAL (if_info->b) == STORE_FLAG_VALUE
1248 && if_info->a == const0_rtx)
1249 reversep = false;
1250 else if (if_info->b == const0_rtx
1251 && CONST_INT_P (if_info->a)
1252 && INTVAL (if_info->a) == STORE_FLAG_VALUE
1253 && noce_reversed_cond_code (if_info) != UNKNOWN)
1254 reversep = true;
1255 else
1256 return false;
1257
1258 start_sequence ();
1259
1260 target = noce_emit_store_flag (if_info, if_info->x, reversep, 0);
1261 if (target)
1262 {
1263 if (target != if_info->x)
1264 noce_emit_move_insn (if_info->x, target);
1265
1266 seq = end_ifcvt_sequence (if_info);
1267 if (! seq)
1268 return false;
1269
1270 emit_insn_before_setloc (seq, if_info->jump,
1271 INSN_LOCATION (if_info->insn_a));
1272 if_info->transform_name = "noce_try_store_flag";
1273 return true;
1274 }
1275 else
1276 {
1277 end_sequence ();
1278 return false;
1279 }
1280 }
1281
1282
1283 /* Convert "if (test) x = -A; else x = A" into
1284 x = A; if (test) x = -x if the machine can do the
1285 conditional negate form of this cheaply.
1286 Try this before noce_try_cmove that will just load the
1287 immediates into two registers and do a conditional select
1288 between them. If the target has a conditional negate or
1289 conditional invert operation we can save a potentially
1290 expensive constant synthesis. */
1291
1292 static bool
1293 noce_try_inverse_constants (struct noce_if_info *if_info)
1294 {
1295 if (!noce_simple_bbs (if_info))
1296 return false;
1297
1298 if (!CONST_INT_P (if_info->a)
1299 || !CONST_INT_P (if_info->b)
1300 || !REG_P (if_info->x))
1301 return false;
1302
1303 machine_mode mode = GET_MODE (if_info->x);
1304
1305 HOST_WIDE_INT val_a = INTVAL (if_info->a);
1306 HOST_WIDE_INT val_b = INTVAL (if_info->b);
1307
1308 rtx cond = if_info->cond;
1309
1310 rtx x = if_info->x;
1311 rtx target;
1312
1313 start_sequence ();
1314
1315 rtx_code code;
1316 if (val_b != HOST_WIDE_INT_MIN && val_a == -val_b)
1317 code = NEG;
1318 else if (val_a == ~val_b)
1319 code = NOT;
1320 else
1321 {
1322 end_sequence ();
1323 return false;
1324 }
1325
1326 rtx tmp = gen_reg_rtx (mode);
1327 noce_emit_move_insn (tmp, if_info->a);
1328
1329 target = emit_conditional_neg_or_complement (x, code, mode, cond, tmp, tmp);
1330
1331 if (target)
1332 {
1333 rtx_insn *seq = get_insns ();
1334
1335 if (!seq)
1336 {
1337 end_sequence ();
1338 return false;
1339 }
1340
1341 if (target != if_info->x)
1342 noce_emit_move_insn (if_info->x, target);
1343
1344 seq = end_ifcvt_sequence (if_info);
1345
1346 if (!seq)
1347 return false;
1348
1349 emit_insn_before_setloc (seq, if_info->jump,
1350 INSN_LOCATION (if_info->insn_a));
1351 if_info->transform_name = "noce_try_inverse_constants";
1352 return true;
1353 }
1354
1355 end_sequence ();
1356 return false;
1357 }
1358
1359
1360 /* Convert "if (test) x = a; else x = b", for A and B constant.
1361 Also allow A = y + c1, B = y + c2, with a common y between A
1362 and B. */
1363
1364 static bool
1365 noce_try_store_flag_constants (struct noce_if_info *if_info)
1366 {
1367 rtx target;
1368 rtx_insn *seq;
1369 bool reversep;
1370 HOST_WIDE_INT itrue, ifalse, diff, tmp;
1371 int normalize;
1372 bool can_reverse;
1373 machine_mode mode = GET_MODE (if_info->x);
1374 rtx common = NULL_RTX;
1375
1376 rtx a = if_info->a;
1377 rtx b = if_info->b;
1378
1379 /* Handle cases like x := test ? y + 3 : y + 4. */
1380 if (GET_CODE (a) == PLUS
1381 && GET_CODE (b) == PLUS
1382 && CONST_INT_P (XEXP (a, 1))
1383 && CONST_INT_P (XEXP (b, 1))
1384 && rtx_equal_p (XEXP (a, 0), XEXP (b, 0))
1385 /* Allow expressions that are not using the result or plain
1386 registers where we handle overlap below. */
1387 && (REG_P (XEXP (a, 0))
1388 || (noce_operand_ok (XEXP (a, 0))
1389 && ! reg_overlap_mentioned_p (if_info->x, XEXP (a, 0)))))
1390 {
1391 common = XEXP (a, 0);
1392 a = XEXP (a, 1);
1393 b = XEXP (b, 1);
1394 }
1395
1396 if (!noce_simple_bbs (if_info))
1397 return false;
1398
1399 if (CONST_INT_P (a)
1400 && CONST_INT_P (b))
1401 {
1402 ifalse = INTVAL (a);
1403 itrue = INTVAL (b);
1404 bool subtract_flag_p = false;
1405
1406 diff = (unsigned HOST_WIDE_INT) itrue - ifalse;
1407 /* Make sure we can represent the difference between the two values. */
1408 if ((diff > 0)
1409 != ((ifalse < 0) != (itrue < 0) ? ifalse < 0 : ifalse < itrue))
1410 return false;
1411
1412 diff = trunc_int_for_mode (diff, mode);
1413
1414 can_reverse = noce_reversed_cond_code (if_info) != UNKNOWN;
1415 reversep = false;
1416 if (diff == STORE_FLAG_VALUE || diff == -STORE_FLAG_VALUE)
1417 {
1418 normalize = 0;
1419 /* We could collapse these cases but it is easier to follow the
1420 diff/STORE_FLAG_VALUE combinations when they are listed
1421 explicitly. */
1422
1423 /* test ? 3 : 4
1424 => 4 + (test != 0). */
1425 if (diff < 0 && STORE_FLAG_VALUE < 0)
1426 reversep = false;
1427 /* test ? 4 : 3
1428 => can_reverse | 4 + (test == 0)
1429 !can_reverse | 3 - (test != 0). */
1430 else if (diff > 0 && STORE_FLAG_VALUE < 0)
1431 {
1432 reversep = can_reverse;
1433 subtract_flag_p = !can_reverse;
1434 /* If we need to subtract the flag and we have PLUS-immediate
1435 A and B then it is unlikely to be beneficial to play tricks
1436 here. */
1437 if (subtract_flag_p && common)
1438 return false;
1439 }
1440 /* test ? 3 : 4
1441 => can_reverse | 3 + (test == 0)
1442 !can_reverse | 4 - (test != 0). */
1443 else if (diff < 0 && STORE_FLAG_VALUE > 0)
1444 {
1445 reversep = can_reverse;
1446 subtract_flag_p = !can_reverse;
1447 /* If we need to subtract the flag and we have PLUS-immediate
1448 A and B then it is unlikely to be beneficial to play tricks
1449 here. */
1450 if (subtract_flag_p && common)
1451 return false;
1452 }
1453 /* test ? 4 : 3
1454 => 4 + (test != 0). */
1455 else if (diff > 0 && STORE_FLAG_VALUE > 0)
1456 reversep = false;
1457 else
1458 gcc_unreachable ();
1459 }
1460 /* Is this (cond) ? 2^n : 0? */
1461 else if (ifalse == 0 && pow2p_hwi (itrue)
1462 && STORE_FLAG_VALUE == 1)
1463 normalize = 1;
1464 /* Is this (cond) ? 0 : 2^n? */
1465 else if (itrue == 0 && pow2p_hwi (ifalse) && can_reverse
1466 && STORE_FLAG_VALUE == 1)
1467 {
1468 normalize = 1;
1469 reversep = true;
1470 }
1471 /* Is this (cond) ? -1 : x? */
1472 else if (itrue == -1
1473 && STORE_FLAG_VALUE == -1)
1474 normalize = -1;
1475 /* Is this (cond) ? x : -1? */
1476 else if (ifalse == -1 && can_reverse
1477 && STORE_FLAG_VALUE == -1)
1478 {
1479 normalize = -1;
1480 reversep = true;
1481 }
1482 else
1483 return false;
1484
1485 if (reversep)
1486 {
1487 std::swap (itrue, ifalse);
1488 diff = trunc_int_for_mode (-(unsigned HOST_WIDE_INT) diff, mode);
1489 }
1490
1491 start_sequence ();
1492
1493 /* If we have x := test ? x + 3 : x + 4 then move the original
1494 x out of the way while we store flags. */
1495 if (common && rtx_equal_p (common, if_info->x))
1496 {
1497 common = gen_reg_rtx (mode);
1498 noce_emit_move_insn (common, if_info->x);
1499 }
1500
1501 target = noce_emit_store_flag (if_info, if_info->x, reversep, normalize);
1502 if (! target)
1503 {
1504 end_sequence ();
1505 return false;
1506 }
1507
1508 /* if (test) x = 3; else x = 4;
1509 => x = 3 + (test == 0); */
1510 if (diff == STORE_FLAG_VALUE || diff == -STORE_FLAG_VALUE)
1511 {
1512 /* Add the common part now. This may allow combine to merge this
1513 with the store flag operation earlier into some sort of conditional
1514 increment/decrement if the target allows it. */
1515 if (common)
1516 target = expand_simple_binop (mode, PLUS,
1517 target, common,
1518 target, 0, OPTAB_WIDEN);
1519
1520 /* Always use ifalse here. It should have been swapped with itrue
1521 when appropriate when reversep is true. */
1522 target = expand_simple_binop (mode, subtract_flag_p ? MINUS : PLUS,
1523 gen_int_mode (ifalse, mode), target,
1524 if_info->x, 0, OPTAB_WIDEN);
1525 }
1526 /* Other cases are not beneficial when the original A and B are PLUS
1527 expressions. */
1528 else if (common)
1529 {
1530 end_sequence ();
1531 return false;
1532 }
1533 /* if (test) x = 8; else x = 0;
1534 => x = (test != 0) << 3; */
1535 else if (ifalse == 0 && (tmp = exact_log2 (itrue)) >= 0)
1536 {
1537 target = expand_simple_binop (mode, ASHIFT,
1538 target, GEN_INT (tmp), if_info->x, 0,
1539 OPTAB_WIDEN);
1540 }
1541
1542 /* if (test) x = -1; else x = b;
1543 => x = -(test != 0) | b; */
1544 else if (itrue == -1)
1545 {
1546 target = expand_simple_binop (mode, IOR,
1547 target, gen_int_mode (ifalse, mode),
1548 if_info->x, 0, OPTAB_WIDEN);
1549 }
1550 else
1551 {
1552 end_sequence ();
1553 return false;
1554 }
1555
1556 if (! target)
1557 {
1558 end_sequence ();
1559 return false;
1560 }
1561
1562 if (target != if_info->x)
1563 noce_emit_move_insn (if_info->x, target);
1564
1565 seq = end_ifcvt_sequence (if_info);
1566 if (!seq || !targetm.noce_conversion_profitable_p (seq, if_info))
1567 return false;
1568
1569 emit_insn_before_setloc (seq, if_info->jump,
1570 INSN_LOCATION (if_info->insn_a));
1571 if_info->transform_name = "noce_try_store_flag_constants";
1572
1573 return true;
1574 }
1575
1576 return false;
1577 }
1578
1579 /* Convert "if (test) foo++" into "foo += (test != 0)", and
1580 similarly for "foo--". */
1581
1582 static bool
1583 noce_try_addcc (struct noce_if_info *if_info)
1584 {
1585 rtx target;
1586 rtx_insn *seq;
1587 bool subtract;
1588 int normalize;
1589
1590 if (!noce_simple_bbs (if_info))
1591 return false;
1592
1593 if (GET_CODE (if_info->a) == PLUS
1594 && rtx_equal_p (XEXP (if_info->a, 0), if_info->b)
1595 && noce_reversed_cond_code (if_info) != UNKNOWN)
1596 {
1597 rtx cond = if_info->rev_cond;
1598 enum rtx_code code;
1599
1600 if (cond == NULL_RTX)
1601 {
1602 cond = if_info->cond;
1603 code = reversed_comparison_code (cond, if_info->jump);
1604 }
1605 else
1606 code = GET_CODE (cond);
1607
1608 /* First try to use addcc pattern. */
1609 if (general_operand (XEXP (cond, 0), VOIDmode)
1610 && general_operand (XEXP (cond, 1), VOIDmode))
1611 {
1612 start_sequence ();
1613 target = emit_conditional_add (if_info->x, code,
1614 XEXP (cond, 0),
1615 XEXP (cond, 1),
1616 VOIDmode,
1617 if_info->b,
1618 XEXP (if_info->a, 1),
1619 GET_MODE (if_info->x),
1620 (code == LTU || code == GEU
1621 || code == LEU || code == GTU));
1622 if (target)
1623 {
1624 if (target != if_info->x)
1625 noce_emit_move_insn (if_info->x, target);
1626
1627 seq = end_ifcvt_sequence (if_info);
1628 if (!seq || !targetm.noce_conversion_profitable_p (seq, if_info))
1629 return false;
1630
1631 emit_insn_before_setloc (seq, if_info->jump,
1632 INSN_LOCATION (if_info->insn_a));
1633 if_info->transform_name = "noce_try_addcc";
1634
1635 return true;
1636 }
1637 end_sequence ();
1638 }
1639
1640 /* If that fails, construct conditional increment or decrement using
1641 setcc. We're changing a branch and an increment to a comparison and
1642 an ADD/SUB. */
1643 if (XEXP (if_info->a, 1) == const1_rtx
1644 || XEXP (if_info->a, 1) == constm1_rtx)
1645 {
1646 start_sequence ();
1647 if (STORE_FLAG_VALUE == INTVAL (XEXP (if_info->a, 1)))
1648 subtract = false, normalize = 0;
1649 else if (-STORE_FLAG_VALUE == INTVAL (XEXP (if_info->a, 1)))
1650 subtract = true, normalize = 0;
1651 else
1652 subtract = false, normalize = INTVAL (XEXP (if_info->a, 1));
1653
1654
1655 target = noce_emit_store_flag (if_info,
1656 gen_reg_rtx (GET_MODE (if_info->x)),
1657 true, normalize);
1658
1659 if (target)
1660 target = expand_simple_binop (GET_MODE (if_info->x),
1661 subtract ? MINUS : PLUS,
1662 if_info->b, target, if_info->x,
1663 0, OPTAB_WIDEN);
1664 if (target)
1665 {
1666 if (target != if_info->x)
1667 noce_emit_move_insn (if_info->x, target);
1668
1669 seq = end_ifcvt_sequence (if_info);
1670 if (!seq || !targetm.noce_conversion_profitable_p (seq, if_info))
1671 return false;
1672
1673 emit_insn_before_setloc (seq, if_info->jump,
1674 INSN_LOCATION (if_info->insn_a));
1675 if_info->transform_name = "noce_try_addcc";
1676 return true;
1677 }
1678 end_sequence ();
1679 }
1680 }
1681
1682 return false;
1683 }
1684
1685 /* Convert "if (test) x = 0;" to "x &= -(test == 0);" */
1686
1687 static bool
1688 noce_try_store_flag_mask (struct noce_if_info *if_info)
1689 {
1690 rtx target;
1691 rtx_insn *seq;
1692 bool reversep;
1693
1694 if (!noce_simple_bbs (if_info))
1695 return false;
1696
1697 reversep = false;
1698
1699 if ((if_info->a == const0_rtx
1700 && (REG_P (if_info->b) || rtx_equal_p (if_info->b, if_info->x)))
1701 || ((reversep = (noce_reversed_cond_code (if_info) != UNKNOWN))
1702 && if_info->b == const0_rtx
1703 && (REG_P (if_info->a) || rtx_equal_p (if_info->a, if_info->x))))
1704 {
1705 start_sequence ();
1706 target = noce_emit_store_flag (if_info,
1707 gen_reg_rtx (GET_MODE (if_info->x)),
1708 reversep, -1);
1709 if (target)
1710 target = expand_simple_binop (GET_MODE (if_info->x), AND,
1711 reversep ? if_info->a : if_info->b,
1712 target, if_info->x, 0,
1713 OPTAB_WIDEN);
1714
1715 if (target)
1716 {
1717 if (target != if_info->x)
1718 noce_emit_move_insn (if_info->x, target);
1719
1720 seq = end_ifcvt_sequence (if_info);
1721 if (!seq || !targetm.noce_conversion_profitable_p (seq, if_info))
1722 return false;
1723
1724 emit_insn_before_setloc (seq, if_info->jump,
1725 INSN_LOCATION (if_info->insn_a));
1726 if_info->transform_name = "noce_try_store_flag_mask";
1727
1728 return true;
1729 }
1730
1731 end_sequence ();
1732 }
1733
1734 return false;
1735 }
1736
1737 /* Helper function for noce_try_cmove and noce_try_cmove_arith. */
1738
1739 static rtx
1740 noce_emit_cmove (struct noce_if_info *if_info, rtx x, enum rtx_code code,
1741 rtx cmp_a, rtx cmp_b, rtx vfalse, rtx vtrue, rtx cc_cmp,
1742 rtx rev_cc_cmp)
1743 {
1744 rtx target ATTRIBUTE_UNUSED;
1745 bool unsignedp ATTRIBUTE_UNUSED;
1746
1747 /* If earliest == jump, try to build the cmove insn directly.
1748 This is helpful when combine has created some complex condition
1749 (like for alpha's cmovlbs) that we can't hope to regenerate
1750 through the normal interface. */
1751
1752 if (if_info->cond_earliest == if_info->jump)
1753 {
1754 rtx cond = gen_rtx_fmt_ee (code, GET_MODE (if_info->cond), cmp_a, cmp_b);
1755 rtx if_then_else = gen_rtx_IF_THEN_ELSE (GET_MODE (x),
1756 cond, vtrue, vfalse);
1757 rtx set = gen_rtx_SET (x, if_then_else);
1758
1759 start_sequence ();
1760 rtx_insn *insn = emit_insn (set);
1761
1762 if (recog_memoized (insn) >= 0)
1763 {
1764 rtx_insn *seq = get_insns ();
1765 end_sequence ();
1766 emit_insn (seq);
1767
1768 return x;
1769 }
1770
1771 end_sequence ();
1772 }
1773
1774 unsignedp = (code == LTU || code == GEU
1775 || code == LEU || code == GTU);
1776
1777 if (cc_cmp != NULL_RTX && rev_cc_cmp != NULL_RTX)
1778 target = emit_conditional_move (x, cc_cmp, rev_cc_cmp,
1779 vtrue, vfalse, GET_MODE (x));
1780 else
1781 {
1782 /* Don't even try if the comparison operands are weird
1783 except that the target supports cbranchcc4. */
1784 if (! general_operand (cmp_a, GET_MODE (cmp_a))
1785 || ! general_operand (cmp_b, GET_MODE (cmp_b)))
1786 {
1787 if (!have_cbranchcc4
1788 || GET_MODE_CLASS (GET_MODE (cmp_a)) != MODE_CC
1789 || cmp_b != const0_rtx)
1790 return NULL_RTX;
1791 }
1792
1793 target = emit_conditional_move (x, { code, cmp_a, cmp_b, VOIDmode },
1794 vtrue, vfalse, GET_MODE (x),
1795 unsignedp);
1796 }
1797
1798 if (target)
1799 return target;
1800
1801 /* We might be faced with a situation like:
1802
1803 x = (reg:M TARGET)
1804 vtrue = (subreg:M (reg:N VTRUE) BYTE)
1805 vfalse = (subreg:M (reg:N VFALSE) BYTE)
1806
1807 We can't do a conditional move in mode M, but it's possible that we
1808 could do a conditional move in mode N instead and take a subreg of
1809 the result.
1810
1811 If we can't create new pseudos, though, don't bother. */
1812 if (reload_completed)
1813 return NULL_RTX;
1814
1815 if (GET_CODE (vtrue) == SUBREG && GET_CODE (vfalse) == SUBREG)
1816 {
1817 rtx reg_vtrue = SUBREG_REG (vtrue);
1818 rtx reg_vfalse = SUBREG_REG (vfalse);
1819 poly_uint64 byte_vtrue = SUBREG_BYTE (vtrue);
1820 poly_uint64 byte_vfalse = SUBREG_BYTE (vfalse);
1821 rtx promoted_target;
1822
1823 if (GET_MODE (reg_vtrue) != GET_MODE (reg_vfalse)
1824 || maybe_ne (byte_vtrue, byte_vfalse)
1825 || (SUBREG_PROMOTED_VAR_P (vtrue)
1826 != SUBREG_PROMOTED_VAR_P (vfalse))
1827 || (SUBREG_PROMOTED_GET (vtrue)
1828 != SUBREG_PROMOTED_GET (vfalse)))
1829 return NULL_RTX;
1830
1831 promoted_target = gen_reg_rtx (GET_MODE (reg_vtrue));
1832
1833 target = emit_conditional_move (promoted_target,
1834 { code, cmp_a, cmp_b, VOIDmode },
1835 reg_vtrue, reg_vfalse,
1836 GET_MODE (reg_vtrue), unsignedp);
1837 /* Nope, couldn't do it in that mode either. */
1838 if (!target)
1839 return NULL_RTX;
1840
1841 target = gen_rtx_SUBREG (GET_MODE (vtrue), promoted_target, byte_vtrue);
1842 SUBREG_PROMOTED_VAR_P (target) = SUBREG_PROMOTED_VAR_P (vtrue);
1843 SUBREG_PROMOTED_SET (target, SUBREG_PROMOTED_GET (vtrue));
1844 emit_move_insn (x, target);
1845 return x;
1846 }
1847 else
1848 return NULL_RTX;
1849 }
1850
1851 /* Emit a conditional zero, returning TARGET or NULL_RTX upon failure.
1852 IF_INFO describes the if-conversion scenario under consideration.
1853 CZERO_CODE selects the condition (EQ/NE).
1854 NON_ZERO_OP is the nonzero operand of the conditional move
1855 TARGET is the desired output register. */
1856
1857 static rtx
1858 noce_emit_czero (struct noce_if_info *if_info, enum rtx_code czero_code,
1859 rtx non_zero_op, rtx target)
1860 {
1861 machine_mode mode = GET_MODE (target);
1862 rtx cond_op0 = XEXP (if_info->cond, 0);
1863 rtx czero_cond
1864 = gen_rtx_fmt_ee (czero_code, GET_MODE (cond_op0), cond_op0, const0_rtx);
1865 rtx if_then_else
1866 = gen_rtx_IF_THEN_ELSE (mode, czero_cond, const0_rtx, non_zero_op);
1867 rtx set = gen_rtx_SET (target, if_then_else);
1868
1869 rtx_insn *insn = make_insn_raw (set);
1870
1871 if (recog_memoized (insn) >= 0)
1872 {
1873 add_insn (insn);
1874 return target;
1875 }
1876
1877 return NULL_RTX;
1878 }
1879
1880 /* Try only simple constants and registers here. More complex cases
1881 are handled in noce_try_cmove_arith after noce_try_store_flag_arith
1882 has had a go at it. */
1883
1884 static bool
1885 noce_try_cmove (struct noce_if_info *if_info)
1886 {
1887 enum rtx_code code;
1888 rtx target;
1889 rtx_insn *seq;
1890
1891 if (!noce_simple_bbs (if_info))
1892 return false;
1893
1894 if ((CONSTANT_P (if_info->a) || register_operand (if_info->a, VOIDmode))
1895 && (CONSTANT_P (if_info->b) || register_operand (if_info->b, VOIDmode)))
1896 {
1897 start_sequence ();
1898
1899 code = GET_CODE (if_info->cond);
1900 target = noce_emit_cmove (if_info, if_info->x, code,
1901 XEXP (if_info->cond, 0),
1902 XEXP (if_info->cond, 1),
1903 if_info->a, if_info->b);
1904
1905 if (target)
1906 {
1907 if (target != if_info->x)
1908 noce_emit_move_insn (if_info->x, target);
1909
1910 seq = end_ifcvt_sequence (if_info);
1911 if (!seq || !targetm.noce_conversion_profitable_p (seq, if_info))
1912 return false;
1913
1914 emit_insn_before_setloc (seq, if_info->jump,
1915 INSN_LOCATION (if_info->insn_a));
1916 if_info->transform_name = "noce_try_cmove";
1917
1918 return true;
1919 }
1920 /* If both a and b are constants try a last-ditch transformation:
1921 if (test) x = a; else x = b;
1922 => x = (-(test != 0) & (b - a)) + a;
1923 Try this only if the target-specific expansion above has failed.
1924 The target-specific expander may want to generate sequences that
1925 we don't know about, so give them a chance before trying this
1926 approach. */
1927 else if (!targetm.have_conditional_execution ()
1928 && CONST_INT_P (if_info->a) && CONST_INT_P (if_info->b))
1929 {
1930 machine_mode mode = GET_MODE (if_info->x);
1931 HOST_WIDE_INT ifalse = INTVAL (if_info->a);
1932 HOST_WIDE_INT itrue = INTVAL (if_info->b);
1933 rtx target = noce_emit_store_flag (if_info, if_info->x, false, -1);
1934 if (!target)
1935 {
1936 end_sequence ();
1937 return false;
1938 }
1939
1940 HOST_WIDE_INT diff = (unsigned HOST_WIDE_INT) itrue - ifalse;
1941 /* Make sure we can represent the difference
1942 between the two values. */
1943 if ((diff > 0)
1944 != ((ifalse < 0) != (itrue < 0) ? ifalse < 0 : ifalse < itrue))
1945 {
1946 end_sequence ();
1947 return false;
1948 }
1949
1950 diff = trunc_int_for_mode (diff, mode);
1951 target = expand_simple_binop (mode, AND,
1952 target, gen_int_mode (diff, mode),
1953 if_info->x, 0, OPTAB_WIDEN);
1954 if (target)
1955 target = expand_simple_binop (mode, PLUS,
1956 target, gen_int_mode (ifalse, mode),
1957 if_info->x, 0, OPTAB_WIDEN);
1958 if (target)
1959 {
1960 if (target != if_info->x)
1961 noce_emit_move_insn (if_info->x, target);
1962
1963 seq = end_ifcvt_sequence (if_info);
1964 if (!seq || !targetm.noce_conversion_profitable_p (seq, if_info))
1965 return false;
1966
1967 emit_insn_before_setloc (seq, if_info->jump,
1968 INSN_LOCATION (if_info->insn_a));
1969 if_info->transform_name = "noce_try_cmove";
1970 return true;
1971 }
1972 else
1973 {
1974 end_sequence ();
1975 return false;
1976 }
1977 }
1978 else
1979 end_sequence ();
1980 }
1981
1982 return false;
1983 }
1984
1985 /* Return true if X contains a conditional code mode rtx. */
1986
1987 static bool
1988 contains_ccmode_rtx_p (rtx x)
1989 {
1990 subrtx_iterator::array_type array;
1991 FOR_EACH_SUBRTX (iter, array, x, ALL)
1992 if (GET_MODE_CLASS (GET_MODE (*iter)) == MODE_CC)
1993 return true;
1994
1995 return false;
1996 }
1997
1998 /* Helper for bb_valid_for_noce_process_p. Validate that
1999 the rtx insn INSN is a single set that does not set
2000 the conditional register CC and is in general valid for
2001 if-conversion. */
2002
2003 static bool
2004 insn_valid_noce_process_p (rtx_insn *insn, rtx cc)
2005 {
2006 if (!insn
2007 || !NONJUMP_INSN_P (insn)
2008 || (cc && set_of (cc, insn)))
2009 return false;
2010
2011 rtx sset = single_set (insn);
2012
2013 /* Currently support only simple single sets in test_bb. */
2014 if (!sset
2015 || !noce_operand_ok (SET_DEST (sset))
2016 || contains_ccmode_rtx_p (SET_DEST (sset))
2017 || !noce_operand_ok (SET_SRC (sset)))
2018 return false;
2019
2020 return true;
2021 }
2022
2023
2024 /* Return true iff the registers that the insns in BB_A set do not get
2025 used in BB_B. If TO_RENAME is non-NULL then it is a location that will be
2026 renamed later by the caller and so conflicts on it should be ignored
2027 in this function. */
2028
2029 static bool
2030 bbs_ok_for_cmove_arith (basic_block bb_a, basic_block bb_b, rtx to_rename)
2031 {
2032 rtx_insn *a_insn;
2033 bitmap bba_sets = BITMAP_ALLOC (&reg_obstack);
2034
2035 df_ref def;
2036 df_ref use;
2037
2038 FOR_BB_INSNS (bb_a, a_insn)
2039 {
2040 if (!active_insn_p (a_insn))
2041 continue;
2042
2043 rtx sset_a = single_set (a_insn);
2044
2045 if (!sset_a)
2046 {
2047 BITMAP_FREE (bba_sets);
2048 return false;
2049 }
2050 /* Record all registers that BB_A sets. */
2051 FOR_EACH_INSN_DEF (def, a_insn)
2052 if (!(to_rename && DF_REF_REG (def) == to_rename))
2053 bitmap_set_bit (bba_sets, DF_REF_REGNO (def));
2054 }
2055
2056 rtx_insn *b_insn;
2057
2058 FOR_BB_INSNS (bb_b, b_insn)
2059 {
2060 if (!active_insn_p (b_insn))
2061 continue;
2062
2063 rtx sset_b = single_set (b_insn);
2064
2065 if (!sset_b)
2066 {
2067 BITMAP_FREE (bba_sets);
2068 return false;
2069 }
2070
2071 /* Make sure this is a REG and not some instance
2072 of ZERO_EXTRACT or non-paradoxical SUBREG or other dangerous stuff.
2073 If we have a memory destination then we have a pair of simple
2074 basic blocks performing an operation of the form [addr] = c ? a : b.
2075 bb_valid_for_noce_process_p will have ensured that these are
2076 the only stores present. In that case [addr] should be the location
2077 to be renamed. Assert that the callers set this up properly. */
2078 if (MEM_P (SET_DEST (sset_b)))
2079 gcc_assert (rtx_equal_p (SET_DEST (sset_b), to_rename));
2080 else if (!REG_P (SET_DEST (sset_b))
2081 && !paradoxical_subreg_p (SET_DEST (sset_b)))
2082 {
2083 BITMAP_FREE (bba_sets);
2084 return false;
2085 }
2086
2087 /* If the insn uses a reg set in BB_A return false. */
2088 FOR_EACH_INSN_USE (use, b_insn)
2089 {
2090 if (bitmap_bit_p (bba_sets, DF_REF_REGNO (use)))
2091 {
2092 BITMAP_FREE (bba_sets);
2093 return false;
2094 }
2095 }
2096
2097 }
2098
2099 BITMAP_FREE (bba_sets);
2100 return true;
2101 }
2102
2103 /* Emit copies of all the active instructions in BB except the last.
2104 This is a helper for noce_try_cmove_arith. */
2105
2106 static void
2107 noce_emit_all_but_last (basic_block bb)
2108 {
2109 rtx_insn *last = last_active_insn (bb, false);
2110 rtx_insn *insn;
2111 FOR_BB_INSNS (bb, insn)
2112 {
2113 if (insn != last && active_insn_p (insn))
2114 {
2115 rtx_insn *to_emit = as_a <rtx_insn *> (copy_rtx (insn));
2116
2117 emit_insn (PATTERN (to_emit));
2118 }
2119 }
2120 }
2121
2122 /* Helper for noce_try_cmove_arith. Emit the pattern TO_EMIT and return
2123 the resulting insn or NULL if it's not a valid insn. */
2124
2125 static rtx_insn *
2126 noce_emit_insn (rtx to_emit)
2127 {
2128 gcc_assert (to_emit);
2129 rtx_insn *insn = emit_insn (to_emit);
2130
2131 if (recog_memoized (insn) < 0)
2132 return NULL;
2133
2134 return insn;
2135 }
2136
2137 /* Helper for noce_try_cmove_arith. Emit a copy of the insns up to
2138 and including the penultimate one in BB if it is not simple
2139 (as indicated by SIMPLE). Then emit LAST_INSN as the last
2140 insn in the block. The reason for that is that LAST_INSN may
2141 have been modified by the preparation in noce_try_cmove_arith. */
2142
2143 static bool
2144 noce_emit_bb (rtx last_insn, basic_block bb, bool simple)
2145 {
2146 if (bb && !simple)
2147 noce_emit_all_but_last (bb);
2148
2149 if (last_insn && !noce_emit_insn (last_insn))
2150 return false;
2151
2152 return true;
2153 }
2154
2155 /* Try more complex cases involving conditional_move. */
2156
2157 static bool
2158 noce_try_cmove_arith (struct noce_if_info *if_info)
2159 {
2160 rtx a = if_info->a;
2161 rtx b = if_info->b;
2162 rtx x = if_info->x;
2163 rtx orig_a, orig_b;
2164 rtx_insn *insn_a, *insn_b;
2165 bool a_simple = if_info->then_simple;
2166 bool b_simple = if_info->else_simple;
2167 basic_block then_bb = if_info->then_bb;
2168 basic_block else_bb = if_info->else_bb;
2169 rtx target;
2170 bool is_mem = false;
2171 enum rtx_code code;
2172 rtx cond = if_info->cond;
2173 rtx_insn *ifcvt_seq;
2174
2175 /* A conditional move from two memory sources is equivalent to a
2176 conditional on their addresses followed by a load. Don't do this
2177 early because it'll screw alias analysis. Note that we've
2178 already checked for no side effects. */
2179 if (cse_not_expected
2180 && MEM_P (a) && MEM_P (b)
2181 && MEM_ADDR_SPACE (a) == MEM_ADDR_SPACE (b))
2182 {
2183 machine_mode address_mode = get_address_mode (a);
2184
2185 a = XEXP (a, 0);
2186 b = XEXP (b, 0);
2187 x = gen_reg_rtx (address_mode);
2188 is_mem = true;
2189 }
2190
2191 /* ??? We could handle this if we knew that a load from A or B could
2192 not trap or fault. This is also true if we've already loaded
2193 from the address along the path from ENTRY. */
2194 else if (may_trap_or_fault_p (a) || may_trap_or_fault_p (b))
2195 return false;
2196
2197 /* if (test) x = a + b; else x = c - d;
2198 => y = a + b;
2199 x = c - d;
2200 if (test)
2201 x = y;
2202 */
2203
2204 code = GET_CODE (cond);
2205 insn_a = if_info->insn_a;
2206 insn_b = if_info->insn_b;
2207
2208 machine_mode x_mode = GET_MODE (x);
2209
2210 if (!can_conditionally_move_p (x_mode))
2211 return false;
2212
2213 /* Possibly rearrange operands to make things come out more natural. */
2214 if (noce_reversed_cond_code (if_info) != UNKNOWN)
2215 {
2216 bool reversep = false;
2217 if (rtx_equal_p (b, x))
2218 reversep = true;
2219 else if (general_operand (b, GET_MODE (b)))
2220 reversep = true;
2221
2222 if (reversep)
2223 {
2224 if (if_info->rev_cond)
2225 {
2226 cond = if_info->rev_cond;
2227 code = GET_CODE (cond);
2228 }
2229 else
2230 code = reversed_comparison_code (cond, if_info->jump);
2231 std::swap (a, b);
2232 std::swap (insn_a, insn_b);
2233 std::swap (a_simple, b_simple);
2234 std::swap (then_bb, else_bb);
2235 }
2236 }
2237
2238 if (then_bb && else_bb
2239 && (!bbs_ok_for_cmove_arith (then_bb, else_bb, if_info->orig_x)
2240 || !bbs_ok_for_cmove_arith (else_bb, then_bb, if_info->orig_x)))
2241 return false;
2242
2243 start_sequence ();
2244
2245 /* If one of the blocks is empty then the corresponding B or A value
2246 came from the test block. The non-empty complex block that we will
2247 emit might clobber the register used by B or A, so move it to a pseudo
2248 first. */
2249
2250 rtx tmp_a = NULL_RTX;
2251 rtx tmp_b = NULL_RTX;
2252
2253 if (b_simple || !else_bb)
2254 tmp_b = gen_reg_rtx (x_mode);
2255
2256 if (a_simple || !then_bb)
2257 tmp_a = gen_reg_rtx (x_mode);
2258
2259 orig_a = a;
2260 orig_b = b;
2261
2262 rtx emit_a = NULL_RTX;
2263 rtx emit_b = NULL_RTX;
2264 rtx_insn *tmp_insn = NULL;
2265 bool modified_in_a = false;
2266 bool modified_in_b = false;
2267 /* If either operand is complex, load it into a register first.
2268 The best way to do this is to copy the original insn. In this
2269 way we preserve any clobbers etc that the insn may have had.
2270 This is of course not possible in the IS_MEM case. */
2271
2272 if (! general_operand (a, GET_MODE (a)) || tmp_a)
2273 {
2274
2275 if (is_mem)
2276 {
2277 rtx reg = gen_reg_rtx (GET_MODE (a));
2278 emit_a = gen_rtx_SET (reg, a);
2279 }
2280 else
2281 {
2282 if (insn_a)
2283 {
2284 a = tmp_a ? tmp_a : gen_reg_rtx (GET_MODE (a));
2285
2286 rtx_insn *copy_of_a = as_a <rtx_insn *> (copy_rtx (insn_a));
2287 rtx set = single_set (copy_of_a);
2288 SET_DEST (set) = a;
2289
2290 emit_a = PATTERN (copy_of_a);
2291 }
2292 else
2293 {
2294 rtx tmp_reg = tmp_a ? tmp_a : gen_reg_rtx (GET_MODE (a));
2295 emit_a = gen_rtx_SET (tmp_reg, a);
2296 a = tmp_reg;
2297 }
2298 }
2299 }
2300
2301 if (! general_operand (b, GET_MODE (b)) || tmp_b)
2302 {
2303 if (is_mem)
2304 {
2305 rtx reg = gen_reg_rtx (GET_MODE (b));
2306 emit_b = gen_rtx_SET (reg, b);
2307 }
2308 else
2309 {
2310 if (insn_b)
2311 {
2312 b = tmp_b ? tmp_b : gen_reg_rtx (GET_MODE (b));
2313 rtx_insn *copy_of_b = as_a <rtx_insn *> (copy_rtx (insn_b));
2314 rtx set = single_set (copy_of_b);
2315
2316 SET_DEST (set) = b;
2317 emit_b = PATTERN (copy_of_b);
2318 }
2319 else
2320 {
2321 rtx tmp_reg = tmp_b ? tmp_b : gen_reg_rtx (GET_MODE (b));
2322 emit_b = gen_rtx_SET (tmp_reg, b);
2323 b = tmp_reg;
2324 }
2325 }
2326 }
2327
2328 modified_in_a = emit_a != NULL_RTX && modified_in_p (orig_b, emit_a);
2329 if (tmp_b && then_bb)
2330 {
2331 FOR_BB_INSNS (then_bb, tmp_insn)
2332 /* Don't check inside insn_a. We will have changed it to emit_a
2333 with a destination that doesn't conflict. */
2334 if (!(insn_a && tmp_insn == insn_a)
2335 && modified_in_p (orig_b, tmp_insn))
2336 {
2337 modified_in_a = true;
2338 break;
2339 }
2340
2341 }
2342
2343 modified_in_b = emit_b != NULL_RTX && modified_in_p (orig_a, emit_b);
2344 if (tmp_a && else_bb)
2345 {
2346 FOR_BB_INSNS (else_bb, tmp_insn)
2347 /* Don't check inside insn_b. We will have changed it to emit_b
2348 with a destination that doesn't conflict. */
2349 if (!(insn_b && tmp_insn == insn_b)
2350 && modified_in_p (orig_a, tmp_insn))
2351 {
2352 modified_in_b = true;
2353 break;
2354 }
2355 }
2356
2357 /* If insn to set up A clobbers any registers B depends on, try to
2358 swap insn that sets up A with the one that sets up B. If even
2359 that doesn't help, punt. */
2360 if (modified_in_a && !modified_in_b)
2361 {
2362 if (!noce_emit_bb (emit_b, else_bb, b_simple))
2363 goto end_seq_and_fail;
2364
2365 if (!noce_emit_bb (emit_a, then_bb, a_simple))
2366 goto end_seq_and_fail;
2367 }
2368 else if (!modified_in_a)
2369 {
2370 if (!noce_emit_bb (emit_a, then_bb, a_simple))
2371 goto end_seq_and_fail;
2372
2373 if (!noce_emit_bb (emit_b, else_bb, b_simple))
2374 goto end_seq_and_fail;
2375 }
2376 else
2377 goto end_seq_and_fail;
2378
2379 target = noce_emit_cmove (if_info, x, code, XEXP (cond, 0), XEXP (cond, 1),
2380 a, b);
2381
2382 if (! target)
2383 goto end_seq_and_fail;
2384
2385 /* If we're handling a memory for above, emit the load now. */
2386 if (is_mem)
2387 {
2388 rtx mem = gen_rtx_MEM (GET_MODE (if_info->x), target);
2389
2390 /* Copy over flags as appropriate. */
2391 if (MEM_VOLATILE_P (if_info->a) || MEM_VOLATILE_P (if_info->b))
2392 MEM_VOLATILE_P (mem) = 1;
2393 if (MEM_ALIAS_SET (if_info->a) == MEM_ALIAS_SET (if_info->b))
2394 set_mem_alias_set (mem, MEM_ALIAS_SET (if_info->a));
2395 set_mem_align (mem,
2396 MIN (MEM_ALIGN (if_info->a), MEM_ALIGN (if_info->b)));
2397
2398 gcc_assert (MEM_ADDR_SPACE (if_info->a) == MEM_ADDR_SPACE (if_info->b));
2399 set_mem_addr_space (mem, MEM_ADDR_SPACE (if_info->a));
2400
2401 noce_emit_move_insn (if_info->x, mem);
2402 }
2403 else if (target != x)
2404 noce_emit_move_insn (x, target);
2405
2406 ifcvt_seq = end_ifcvt_sequence (if_info);
2407 if (!ifcvt_seq || !targetm.noce_conversion_profitable_p (ifcvt_seq, if_info))
2408 return false;
2409
2410 emit_insn_before_setloc (ifcvt_seq, if_info->jump,
2411 INSN_LOCATION (if_info->insn_a));
2412 if_info->transform_name = "noce_try_cmove_arith";
2413 return true;
2414
2415 end_seq_and_fail:
2416 end_sequence ();
2417 return false;
2418 }
2419
2420 /* For most cases, the simplified condition we found is the best
2421 choice, but this is not the case for the min/max/abs transforms.
2422 For these we wish to know that it is A or B in the condition. */
2423
2424 static rtx
2425 noce_get_alt_condition (struct noce_if_info *if_info, rtx target,
2426 rtx_insn **earliest)
2427 {
2428 rtx cond, set;
2429 rtx_insn *insn;
2430 bool reverse;
2431
2432 /* If target is already mentioned in the known condition, return it. */
2433 if (reg_mentioned_p (target, if_info->cond))
2434 {
2435 *earliest = if_info->cond_earliest;
2436 return if_info->cond;
2437 }
2438
2439 set = pc_set (if_info->jump);
2440 cond = XEXP (SET_SRC (set), 0);
2441 reverse
2442 = GET_CODE (XEXP (SET_SRC (set), 2)) == LABEL_REF
2443 && label_ref_label (XEXP (SET_SRC (set), 2)) == JUMP_LABEL (if_info->jump);
2444 if (if_info->then_else_reversed)
2445 reverse = !reverse;
2446
2447 /* If we're looking for a constant, try to make the conditional
2448 have that constant in it. There are two reasons why it may
2449 not have the constant we want:
2450
2451 1. GCC may have needed to put the constant in a register, because
2452 the target can't compare directly against that constant. For
2453 this case, we look for a SET immediately before the comparison
2454 that puts a constant in that register.
2455
2456 2. GCC may have canonicalized the conditional, for example
2457 replacing "if x < 4" with "if x <= 3". We can undo that (or
2458 make equivalent types of changes) to get the constants we need
2459 if they're off by one in the right direction. */
2460
2461 if (CONST_INT_P (target))
2462 {
2463 enum rtx_code code = GET_CODE (if_info->cond);
2464 rtx op_a = XEXP (if_info->cond, 0);
2465 rtx op_b = XEXP (if_info->cond, 1);
2466 rtx_insn *prev_insn;
2467
2468 /* First, look to see if we put a constant in a register. */
2469 prev_insn = prev_nonnote_nondebug_insn (if_info->cond_earliest);
2470 if (prev_insn
2471 && BLOCK_FOR_INSN (prev_insn)
2472 == BLOCK_FOR_INSN (if_info->cond_earliest)
2473 && INSN_P (prev_insn)
2474 && GET_CODE (PATTERN (prev_insn)) == SET)
2475 {
2476 rtx src = find_reg_equal_equiv_note (prev_insn);
2477 if (!src)
2478 src = SET_SRC (PATTERN (prev_insn));
2479 if (CONST_INT_P (src))
2480 {
2481 if (rtx_equal_p (op_a, SET_DEST (PATTERN (prev_insn))))
2482 op_a = src;
2483 else if (rtx_equal_p (op_b, SET_DEST (PATTERN (prev_insn))))
2484 op_b = src;
2485
2486 if (CONST_INT_P (op_a))
2487 {
2488 std::swap (op_a, op_b);
2489 code = swap_condition (code);
2490 }
2491 }
2492 }
2493
2494 /* Now, look to see if we can get the right constant by
2495 adjusting the conditional. */
2496 if (CONST_INT_P (op_b))
2497 {
2498 HOST_WIDE_INT desired_val = INTVAL (target);
2499 HOST_WIDE_INT actual_val = INTVAL (op_b);
2500
2501 switch (code)
2502 {
2503 case LT:
2504 if (desired_val != HOST_WIDE_INT_MAX
2505 && actual_val == desired_val + 1)
2506 {
2507 code = LE;
2508 op_b = GEN_INT (desired_val);
2509 }
2510 break;
2511 case LE:
2512 if (desired_val != HOST_WIDE_INT_MIN
2513 && actual_val == desired_val - 1)
2514 {
2515 code = LT;
2516 op_b = GEN_INT (desired_val);
2517 }
2518 break;
2519 case GT:
2520 if (desired_val != HOST_WIDE_INT_MIN
2521 && actual_val == desired_val - 1)
2522 {
2523 code = GE;
2524 op_b = GEN_INT (desired_val);
2525 }
2526 break;
2527 case GE:
2528 if (desired_val != HOST_WIDE_INT_MAX
2529 && actual_val == desired_val + 1)
2530 {
2531 code = GT;
2532 op_b = GEN_INT (desired_val);
2533 }
2534 break;
2535 default:
2536 break;
2537 }
2538 }
2539
2540 /* If we made any changes, generate a new conditional that is
2541 equivalent to what we started with, but has the right
2542 constants in it. */
2543 if (code != GET_CODE (if_info->cond)
2544 || op_a != XEXP (if_info->cond, 0)
2545 || op_b != XEXP (if_info->cond, 1))
2546 {
2547 cond = gen_rtx_fmt_ee (code, GET_MODE (cond), op_a, op_b);
2548 *earliest = if_info->cond_earliest;
2549 return cond;
2550 }
2551 }
2552
2553 cond = canonicalize_condition (if_info->jump, cond, reverse,
2554 earliest, target, have_cbranchcc4, true);
2555 if (! cond || ! reg_mentioned_p (target, cond))
2556 return NULL;
2557
2558 /* We almost certainly searched back to a different place.
2559 Need to re-verify correct lifetimes. */
2560
2561 /* X may not be mentioned in the range (cond_earliest, jump]. */
2562 for (insn = if_info->jump; insn != *earliest; insn = PREV_INSN (insn))
2563 if (INSN_P (insn) && reg_overlap_mentioned_p (if_info->x, PATTERN (insn)))
2564 return NULL;
2565
2566 /* A and B may not be modified in the range [cond_earliest, jump). */
2567 for (insn = *earliest; insn != if_info->jump; insn = NEXT_INSN (insn))
2568 if (INSN_P (insn)
2569 && (modified_in_p (if_info->a, insn)
2570 || modified_in_p (if_info->b, insn)))
2571 return NULL;
2572
2573 return cond;
2574 }
2575
2576 /* Convert "if (a < b) x = a; else x = b;" to "x = min(a, b);", etc. */
2577
2578 static bool
2579 noce_try_minmax (struct noce_if_info *if_info)
2580 {
2581 rtx cond, target;
2582 rtx_insn *earliest, *seq;
2583 enum rtx_code code, op;
2584 bool unsignedp;
2585
2586 if (!noce_simple_bbs (if_info))
2587 return false;
2588
2589 /* ??? Reject modes with NaNs or signed zeros since we don't know how
2590 they will be resolved with an SMIN/SMAX. It wouldn't be too hard
2591 to get the target to tell us... */
2592 if (HONOR_SIGNED_ZEROS (if_info->x)
2593 || HONOR_NANS (if_info->x))
2594 return false;
2595
2596 cond = noce_get_alt_condition (if_info, if_info->a, &earliest);
2597 if (!cond)
2598 return false;
2599
2600 /* Verify the condition is of the form we expect, and canonicalize
2601 the comparison code. */
2602 code = GET_CODE (cond);
2603 if (rtx_equal_p (XEXP (cond, 0), if_info->a))
2604 {
2605 if (! rtx_equal_p (XEXP (cond, 1), if_info->b))
2606 return false;
2607 }
2608 else if (rtx_equal_p (XEXP (cond, 1), if_info->a))
2609 {
2610 if (! rtx_equal_p (XEXP (cond, 0), if_info->b))
2611 return false;
2612 code = swap_condition (code);
2613 }
2614 else
2615 return false;
2616
2617 /* Determine what sort of operation this is. Note that the code is for
2618 a taken branch, so the code->operation mapping appears backwards. */
2619 switch (code)
2620 {
2621 case LT:
2622 case LE:
2623 case UNLT:
2624 case UNLE:
2625 op = SMAX;
2626 unsignedp = false;
2627 break;
2628 case GT:
2629 case GE:
2630 case UNGT:
2631 case UNGE:
2632 op = SMIN;
2633 unsignedp = false;
2634 break;
2635 case LTU:
2636 case LEU:
2637 op = UMAX;
2638 unsignedp = true;
2639 break;
2640 case GTU:
2641 case GEU:
2642 op = UMIN;
2643 unsignedp = true;
2644 break;
2645 default:
2646 return false;
2647 }
2648
2649 start_sequence ();
2650
2651 target = expand_simple_binop (GET_MODE (if_info->x), op,
2652 if_info->a, if_info->b,
2653 if_info->x, unsignedp, OPTAB_WIDEN);
2654 if (! target)
2655 {
2656 end_sequence ();
2657 return false;
2658 }
2659 if (target != if_info->x)
2660 noce_emit_move_insn (if_info->x, target);
2661
2662 seq = end_ifcvt_sequence (if_info);
2663 if (!seq)
2664 return false;
2665
2666 emit_insn_before_setloc (seq, if_info->jump, INSN_LOCATION (if_info->insn_a));
2667 if_info->cond = cond;
2668 if_info->cond_earliest = earliest;
2669 if_info->rev_cond = NULL_RTX;
2670 if_info->transform_name = "noce_try_minmax";
2671
2672 return true;
2673 }
2674
2675 /* Convert "if (a < 0) x = -a; else x = a;" to "x = abs(a);",
2676 "if (a < 0) x = ~a; else x = a;" to "x = one_cmpl_abs(a);",
2677 etc. */
2678
2679 static bool
2680 noce_try_abs (struct noce_if_info *if_info)
2681 {
2682 rtx cond, target, a, b, c;
2683 rtx_insn *earliest, *seq;
2684 bool negate;
2685 bool one_cmpl = false;
2686
2687 if (!noce_simple_bbs (if_info))
2688 return false;
2689
2690 /* Reject modes with signed zeros. */
2691 if (HONOR_SIGNED_ZEROS (if_info->x))
2692 return false;
2693
2694 /* Recognize A and B as constituting an ABS or NABS. The canonical
2695 form is a branch around the negation, taken when the object is the
2696 first operand of a comparison against 0 that evaluates to true. */
2697 a = if_info->a;
2698 b = if_info->b;
2699 if (GET_CODE (a) == NEG && rtx_equal_p (XEXP (a, 0), b))
2700 negate = false;
2701 else if (GET_CODE (b) == NEG && rtx_equal_p (XEXP (b, 0), a))
2702 {
2703 std::swap (a, b);
2704 negate = true;
2705 }
2706 else if (GET_CODE (a) == NOT && rtx_equal_p (XEXP (a, 0), b))
2707 {
2708 negate = false;
2709 one_cmpl = true;
2710 }
2711 else if (GET_CODE (b) == NOT && rtx_equal_p (XEXP (b, 0), a))
2712 {
2713 std::swap (a, b);
2714 negate = true;
2715 one_cmpl = true;
2716 }
2717 else
2718 return false;
2719
2720 cond = noce_get_alt_condition (if_info, b, &earliest);
2721 if (!cond)
2722 return false;
2723
2724 /* Verify the condition is of the form we expect. */
2725 if (rtx_equal_p (XEXP (cond, 0), b))
2726 c = XEXP (cond, 1);
2727 else if (rtx_equal_p (XEXP (cond, 1), b))
2728 {
2729 c = XEXP (cond, 0);
2730 negate = !negate;
2731 }
2732 else
2733 return false;
2734
2735 /* Verify that C is zero. Search one step backward for a
2736 REG_EQUAL note or a simple source if necessary. */
2737 if (REG_P (c))
2738 {
2739 rtx set;
2740 rtx_insn *insn = prev_nonnote_nondebug_insn (earliest);
2741 if (insn
2742 && BLOCK_FOR_INSN (insn) == BLOCK_FOR_INSN (earliest)
2743 && (set = single_set (insn))
2744 && rtx_equal_p (SET_DEST (set), c))
2745 {
2746 rtx note = find_reg_equal_equiv_note (insn);
2747 if (note)
2748 c = XEXP (note, 0);
2749 else
2750 c = SET_SRC (set);
2751 }
2752 else
2753 return false;
2754 }
2755 if (MEM_P (c)
2756 && GET_CODE (XEXP (c, 0)) == SYMBOL_REF
2757 && CONSTANT_POOL_ADDRESS_P (XEXP (c, 0)))
2758 c = get_pool_constant (XEXP (c, 0));
2759
2760 /* Work around funny ideas get_condition has wrt canonicalization.
2761 Note that these rtx constants are known to be CONST_INT, and
2762 therefore imply integer comparisons.
2763 The one_cmpl case is more complicated, as we want to handle
2764 only x < 0 ? ~x : x or x >= 0 ? x : ~x to one_cmpl_abs (x)
2765 and x < 0 ? x : ~x or x >= 0 ? ~x : x to ~one_cmpl_abs (x),
2766 but not other cases (x > -1 is equivalent of x >= 0). */
2767 if (c == constm1_rtx && GET_CODE (cond) == GT)
2768 ;
2769 else if (c == const1_rtx && GET_CODE (cond) == LT)
2770 {
2771 if (one_cmpl)
2772 return false;
2773 }
2774 else if (c == CONST0_RTX (GET_MODE (b)))
2775 {
2776 if (one_cmpl
2777 && GET_CODE (cond) != GE
2778 && GET_CODE (cond) != LT)
2779 return false;
2780 }
2781 else
2782 return false;
2783
2784 /* Determine what sort of operation this is. */
2785 switch (GET_CODE (cond))
2786 {
2787 case LT:
2788 case LE:
2789 case UNLT:
2790 case UNLE:
2791 negate = !negate;
2792 break;
2793 case GT:
2794 case GE:
2795 case UNGT:
2796 case UNGE:
2797 break;
2798 default:
2799 return false;
2800 }
2801
2802 start_sequence ();
2803 if (one_cmpl)
2804 target = expand_one_cmpl_abs_nojump (GET_MODE (if_info->x), b,
2805 if_info->x);
2806 else
2807 target = expand_abs_nojump (GET_MODE (if_info->x), b, if_info->x, 1);
2808
2809 /* ??? It's a quandary whether cmove would be better here, especially
2810 for integers. Perhaps combine will clean things up. */
2811 if (target && negate)
2812 {
2813 if (one_cmpl)
2814 target = expand_simple_unop (GET_MODE (target), NOT, target,
2815 if_info->x, 0);
2816 else
2817 target = expand_simple_unop (GET_MODE (target), NEG, target,
2818 if_info->x, 0);
2819 }
2820
2821 if (! target)
2822 {
2823 end_sequence ();
2824 return false;
2825 }
2826
2827 if (target != if_info->x)
2828 noce_emit_move_insn (if_info->x, target);
2829
2830 seq = end_ifcvt_sequence (if_info);
2831 if (!seq)
2832 return false;
2833
2834 emit_insn_before_setloc (seq, if_info->jump, INSN_LOCATION (if_info->insn_a));
2835 if_info->cond = cond;
2836 if_info->cond_earliest = earliest;
2837 if_info->rev_cond = NULL_RTX;
2838 if_info->transform_name = "noce_try_abs";
2839
2840 return true;
2841 }
2842
2843 /* Convert "if (m < 0) x = b; else x = 0;" to "x = (m >> C) & b;". */
2844
2845 static bool
2846 noce_try_sign_mask (struct noce_if_info *if_info)
2847 {
2848 rtx cond, t, m, c;
2849 rtx_insn *seq;
2850 machine_mode mode;
2851 enum rtx_code code;
2852 bool t_unconditional;
2853
2854 if (!noce_simple_bbs (if_info))
2855 return false;
2856
2857 cond = if_info->cond;
2858 code = GET_CODE (cond);
2859 m = XEXP (cond, 0);
2860 c = XEXP (cond, 1);
2861
2862 t = NULL_RTX;
2863 if (if_info->a == const0_rtx)
2864 {
2865 if ((code == LT && c == const0_rtx)
2866 || (code == LE && c == constm1_rtx))
2867 t = if_info->b;
2868 }
2869 else if (if_info->b == const0_rtx)
2870 {
2871 if ((code == GE && c == const0_rtx)
2872 || (code == GT && c == constm1_rtx))
2873 t = if_info->a;
2874 }
2875
2876 if (! t || side_effects_p (t))
2877 return false;
2878
2879 /* We currently don't handle different modes. */
2880 mode = GET_MODE (t);
2881 if (GET_MODE (m) != mode)
2882 return false;
2883
2884 /* This is only profitable if T is unconditionally executed/evaluated in the
2885 original insn sequence or T is cheap and can't trap or fault. The former
2886 happens if B is the non-zero (T) value and if INSN_B was taken from
2887 TEST_BB, or there was no INSN_B which can happen for e.g. conditional
2888 stores to memory. For the cost computation use the block TEST_BB where
2889 the evaluation will end up after the transformation. */
2890 t_unconditional
2891 = (t == if_info->b
2892 && (if_info->insn_b == NULL_RTX
2893 || BLOCK_FOR_INSN (if_info->insn_b) == if_info->test_bb));
2894 if (!(t_unconditional
2895 || ((set_src_cost (t, mode, if_info->speed_p)
2896 < COSTS_N_INSNS (2))
2897 && !may_trap_or_fault_p (t))))
2898 return false;
2899
2900 if (!noce_can_force_operand (t))
2901 return false;
2902
2903 start_sequence ();
2904 /* Use emit_store_flag to generate "m < 0 ? -1 : 0" instead of expanding
2905 "(signed) m >> 31" directly. This benefits targets with specialized
2906 insns to obtain the signmask, but still uses ashr_optab otherwise. */
2907 m = emit_store_flag (gen_reg_rtx (mode), LT, m, const0_rtx, mode, 0, -1);
2908 t = m ? expand_binop (mode, and_optab, m, t, NULL_RTX, 0, OPTAB_DIRECT)
2909 : NULL_RTX;
2910
2911 if (!t)
2912 {
2913 end_sequence ();
2914 return false;
2915 }
2916
2917 noce_emit_move_insn (if_info->x, t);
2918
2919 seq = end_ifcvt_sequence (if_info);
2920 if (!seq)
2921 return false;
2922
2923 emit_insn_before_setloc (seq, if_info->jump, INSN_LOCATION (if_info->insn_a));
2924 if_info->transform_name = "noce_try_sign_mask";
2925
2926 return true;
2927 }
2928
2929 /* Check if OP is supported by conditional zero based if conversion,
2930 returning TRUE if satisfied otherwise FALSE.
2931
2932 OP is the operation to check. */
2933
2934 static bool
2935 noce_cond_zero_binary_op_supported (rtx op)
2936 {
2937 enum rtx_code opcode = GET_CODE (op);
2938
2939 if (opcode == PLUS || opcode == MINUS || opcode == IOR || opcode == XOR
2940 || opcode == ASHIFT || opcode == ASHIFTRT || opcode == LSHIFTRT
2941 || opcode == ROTATE || opcode == ROTATERT || opcode == AND)
2942 return true;
2943
2944 return false;
2945 }
2946
2947 /* Helper function to return REG itself,
2948 otherwise NULL_RTX for other RTX_CODE. */
2949
2950 static rtx
2951 get_base_reg (rtx exp)
2952 {
2953 if (REG_P (exp))
2954 return exp;
2955
2956 return NULL_RTX;
2957 }
2958
2959 /* Check if IF-BB and THEN-BB satisfy the condition for conditional zero
2960 based if conversion, returning TRUE if satisfied otherwise FALSE.
2961
2962 IF_INFO describes the if-conversion scenario under consideration.
2963 COMMON_PTR points to the common REG of canonicalized IF_INFO->A and
2964 IF_INFO->B.
2965 CZERO_CODE_PTR points to the comparison code to use in czero RTX.
2966 A_PTR points to the A expression of canonicalized IF_INFO->A.
2967 TO_REPLACE points to the RTX to be replaced by czero RTX destnation. */
2968
2969 static bool
2970 noce_bbs_ok_for_cond_zero_arith (struct noce_if_info *if_info, rtx *common_ptr,
2971 rtx *bin_exp_ptr,
2972 enum rtx_code *czero_code_ptr, rtx *a_ptr,
2973 rtx **to_replace)
2974 {
2975 rtx common = NULL_RTX;
2976 rtx cond = if_info->cond;
2977 rtx a = copy_rtx (if_info->a);
2978 rtx b = copy_rtx (if_info->b);
2979 rtx bin_op1 = NULL_RTX;
2980 enum rtx_code czero_code = UNKNOWN;
2981 bool reverse = false;
2982 rtx op0, op1, bin_exp;
2983
2984 if (!noce_simple_bbs (if_info))
2985 return false;
2986
2987 /* COND must be EQ or NE comparision of a reg and 0. */
2988 if (GET_CODE (cond) != NE && GET_CODE (cond) != EQ)
2989 return false;
2990 if (!REG_P (XEXP (cond, 0)) || !rtx_equal_p (XEXP (cond, 1), const0_rtx))
2991 return false;
2992
2993 /* Canonicalize x = y : (y op z) to x = (y op z) : y. */
2994 if (REG_P (a) && noce_cond_zero_binary_op_supported (b))
2995 {
2996 std::swap (a, b);
2997 reverse = !reverse;
2998 }
2999
3000 /* Check if x = (y op z) : y is supported by czero based ifcvt. */
3001 if (!(noce_cond_zero_binary_op_supported (a) && REG_P (b)))
3002 return false;
3003
3004 bin_exp = a;
3005
3006 /* Canonicalize x = (z op y) : y to x = (y op z) : y */
3007 op1 = get_base_reg (XEXP (bin_exp, 1));
3008 if (op1 && rtx_equal_p (op1, b) && COMMUTATIVE_ARITH_P (bin_exp))
3009 std::swap (XEXP (bin_exp, 0), XEXP (bin_exp, 1));
3010
3011 op0 = get_base_reg (XEXP (bin_exp, 0));
3012 if (op0 && rtx_equal_p (op0, b))
3013 {
3014 common = b;
3015 bin_op1 = XEXP (bin_exp, 1);
3016 czero_code = (reverse ^ (GET_CODE (bin_exp) == AND))
3017 ? noce_reversed_cond_code (if_info)
3018 : GET_CODE (cond);
3019 }
3020 else
3021 return false;
3022
3023 if (czero_code == UNKNOWN)
3024 return false;
3025
3026 if (REG_P (bin_op1))
3027 *to_replace = &XEXP (bin_exp, 1);
3028 else
3029 return false;
3030
3031 *common_ptr = common;
3032 *bin_exp_ptr = bin_exp;
3033 *czero_code_ptr = czero_code;
3034 *a_ptr = a;
3035
3036 return true;
3037 }
3038
3039 /* Try to covert if-then-else with conditional zero,
3040 returning TURE on success or FALSE on failure.
3041 IF_INFO describes the if-conversion scenario under consideration. */
3042
3043 static int
3044 noce_try_cond_zero_arith (struct noce_if_info *if_info)
3045 {
3046 rtx target, rtmp, a;
3047 rtx_insn *seq;
3048 machine_mode mode = GET_MODE (if_info->x);
3049 rtx common = NULL_RTX;
3050 enum rtx_code czero_code = UNKNOWN;
3051 rtx bin_exp = NULL_RTX;
3052 enum rtx_code bin_code = UNKNOWN;
3053 rtx non_zero_op = NULL_RTX;
3054 rtx *to_replace = NULL;
3055
3056 if (!noce_bbs_ok_for_cond_zero_arith (if_info, &common, &bin_exp, &czero_code,
3057 &a, &to_replace))
3058 return false;
3059
3060 start_sequence ();
3061
3062 bin_code = GET_CODE (bin_exp);
3063
3064 if (bin_code == AND)
3065 {
3066 rtmp = gen_reg_rtx (mode);
3067 noce_emit_move_insn (rtmp, a);
3068
3069 target = noce_emit_czero (if_info, czero_code, common, if_info->x);
3070 if (!target)
3071 {
3072 end_sequence ();
3073 return false;
3074 }
3075
3076 target = expand_simple_binop (mode, IOR, rtmp, target, if_info->x, 0,
3077 OPTAB_WIDEN);
3078 if (!target)
3079 {
3080 end_sequence ();
3081 return false;
3082 }
3083
3084 if (target != if_info->x)
3085 noce_emit_move_insn (if_info->x, target);
3086 }
3087 else
3088 {
3089 non_zero_op = *to_replace;
3090 /* If x is used in both input and out like x = c ? x + z : x,
3091 use a new reg to avoid modifying x */
3092 if (common && rtx_equal_p (common, if_info->x))
3093 target = gen_reg_rtx (mode);
3094 else
3095 target = if_info->x;
3096
3097 target = noce_emit_czero (if_info, czero_code, non_zero_op, target);
3098 if (!target || !to_replace)
3099 {
3100 end_sequence ();
3101 return false;
3102 }
3103
3104 *to_replace = target;
3105 noce_emit_move_insn (if_info->x, a);
3106 }
3107
3108 seq = end_ifcvt_sequence (if_info);
3109 if (!seq || !targetm.noce_conversion_profitable_p (seq, if_info))
3110 return false;
3111
3112 emit_insn_before_setloc (seq, if_info->jump, INSN_LOCATION (if_info->insn_a));
3113 if_info->transform_name = "noce_try_cond_zero_arith";
3114 return true;
3115 }
3116
3117 /* Optimize away "if (x & C) x |= C" and similar bit manipulation
3118 transformations. */
3119
3120 static bool
3121 noce_try_bitop (struct noce_if_info *if_info)
3122 {
3123 rtx cond, x, a, result;
3124 rtx_insn *seq;
3125 scalar_int_mode mode;
3126 enum rtx_code code;
3127 int bitnum;
3128
3129 x = if_info->x;
3130 cond = if_info->cond;
3131 code = GET_CODE (cond);
3132
3133 /* Check for an integer operation. */
3134 if (!is_a <scalar_int_mode> (GET_MODE (x), &mode))
3135 return false;
3136
3137 if (!noce_simple_bbs (if_info))
3138 return false;
3139
3140 /* Check for no else condition. */
3141 if (! rtx_equal_p (x, if_info->b))
3142 return false;
3143
3144 /* Check for a suitable condition. */
3145 if (code != NE && code != EQ)
3146 return false;
3147 if (XEXP (cond, 1) != const0_rtx)
3148 return false;
3149 cond = XEXP (cond, 0);
3150
3151 /* ??? We could also handle AND here. */
3152 if (GET_CODE (cond) == ZERO_EXTRACT)
3153 {
3154 if (XEXP (cond, 1) != const1_rtx
3155 || !CONST_INT_P (XEXP (cond, 2))
3156 || ! rtx_equal_p (x, XEXP (cond, 0)))
3157 return false;
3158 bitnum = INTVAL (XEXP (cond, 2));
3159 if (BITS_BIG_ENDIAN)
3160 bitnum = GET_MODE_BITSIZE (mode) - 1 - bitnum;
3161 if (bitnum < 0 || bitnum >= HOST_BITS_PER_WIDE_INT)
3162 return false;
3163 }
3164 else
3165 return false;
3166
3167 a = if_info->a;
3168 if (GET_CODE (a) == IOR || GET_CODE (a) == XOR)
3169 {
3170 /* Check for "if (X & C) x = x op C". */
3171 if (! rtx_equal_p (x, XEXP (a, 0))
3172 || !CONST_INT_P (XEXP (a, 1))
3173 || (INTVAL (XEXP (a, 1)) & GET_MODE_MASK (mode))
3174 != HOST_WIDE_INT_1U << bitnum)
3175 return false;
3176
3177 /* if ((x & C) == 0) x |= C; is transformed to x |= C. */
3178 /* if ((x & C) != 0) x |= C; is transformed to nothing. */
3179 if (GET_CODE (a) == IOR)
3180 result = (code == NE) ? a : NULL_RTX;
3181 else if (code == NE)
3182 {
3183 /* if ((x & C) == 0) x ^= C; is transformed to x |= C. */
3184 result = gen_int_mode (HOST_WIDE_INT_1 << bitnum, mode);
3185 result = simplify_gen_binary (IOR, mode, x, result);
3186 }
3187 else
3188 {
3189 /* if ((x & C) != 0) x ^= C; is transformed to x &= ~C. */
3190 result = gen_int_mode (~(HOST_WIDE_INT_1 << bitnum), mode);
3191 result = simplify_gen_binary (AND, mode, x, result);
3192 }
3193 }
3194 else if (GET_CODE (a) == AND)
3195 {
3196 /* Check for "if (X & C) x &= ~C". */
3197 if (! rtx_equal_p (x, XEXP (a, 0))
3198 || !CONST_INT_P (XEXP (a, 1))
3199 || (INTVAL (XEXP (a, 1)) & GET_MODE_MASK (mode))
3200 != (~(HOST_WIDE_INT_1 << bitnum) & GET_MODE_MASK (mode)))
3201 return false;
3202
3203 /* if ((x & C) == 0) x &= ~C; is transformed to nothing. */
3204 /* if ((x & C) != 0) x &= ~C; is transformed to x &= ~C. */
3205 result = (code == EQ) ? a : NULL_RTX;
3206 }
3207 else
3208 return false;
3209
3210 if (result)
3211 {
3212 start_sequence ();
3213 noce_emit_move_insn (x, result);
3214 seq = end_ifcvt_sequence (if_info);
3215 if (!seq)
3216 return false;
3217
3218 emit_insn_before_setloc (seq, if_info->jump,
3219 INSN_LOCATION (if_info->insn_a));
3220 }
3221 if_info->transform_name = "noce_try_bitop";
3222 return true;
3223 }
3224
3225
3226 /* Similar to get_condition, only the resulting condition must be
3227 valid at JUMP, instead of at EARLIEST.
3228
3229 If THEN_ELSE_REVERSED is true, the fallthrough does not go to the
3230 THEN block of the caller, and we have to reverse the condition. */
3231
3232 static rtx
3233 noce_get_condition (rtx_insn *jump, rtx_insn **earliest,
3234 bool then_else_reversed)
3235 {
3236 rtx cond, set, tmp;
3237 bool reverse;
3238
3239 if (! any_condjump_p (jump))
3240 return NULL_RTX;
3241
3242 set = pc_set (jump);
3243
3244 /* If this branches to JUMP_LABEL when the condition is false,
3245 reverse the condition. */
3246 reverse = (GET_CODE (XEXP (SET_SRC (set), 2)) == LABEL_REF
3247 && label_ref_label (XEXP (SET_SRC (set), 2)) == JUMP_LABEL (jump));
3248
3249 /* We may have to reverse because the caller's if block is not canonical,
3250 i.e. the THEN block isn't the fallthrough block for the TEST block
3251 (see find_if_header). */
3252 if (then_else_reversed)
3253 reverse = !reverse;
3254
3255 /* If the condition variable is a register and is MODE_INT, accept it. */
3256
3257 cond = XEXP (SET_SRC (set), 0);
3258 tmp = XEXP (cond, 0);
3259 if (REG_P (tmp) && GET_MODE_CLASS (GET_MODE (tmp)) == MODE_INT
3260 && (GET_MODE (tmp) != BImode
3261 || !targetm.small_register_classes_for_mode_p (BImode)))
3262 {
3263 *earliest = jump;
3264
3265 if (reverse)
3266 cond = gen_rtx_fmt_ee (reverse_condition (GET_CODE (cond)),
3267 GET_MODE (cond), tmp, XEXP (cond, 1));
3268 return cond;
3269 }
3270
3271 /* Otherwise, fall back on canonicalize_condition to do the dirty
3272 work of manipulating MODE_CC values and COMPARE rtx codes. */
3273 tmp = canonicalize_condition (jump, cond, reverse, earliest,
3274 NULL_RTX, have_cbranchcc4, true);
3275
3276 /* We don't handle side-effects in the condition, like handling
3277 REG_INC notes and making sure no duplicate conditions are emitted. */
3278 if (tmp != NULL_RTX && side_effects_p (tmp))
3279 return NULL_RTX;
3280
3281 return tmp;
3282 }
3283
3284 /* Return true if OP is ok for if-then-else processing. */
3285
3286 static bool
3287 noce_operand_ok (const_rtx op)
3288 {
3289 if (side_effects_p (op))
3290 return false;
3291
3292 /* We special-case memories, so handle any of them with
3293 no address side effects. */
3294 if (MEM_P (op))
3295 return ! side_effects_p (XEXP (op, 0));
3296
3297 return ! may_trap_p (op);
3298 }
3299
3300 /* Return true iff basic block TEST_BB is valid for noce if-conversion.
3301 The condition used in this if-conversion is in COND.
3302 In practice, check that TEST_BB ends with a single set
3303 x := a and all previous computations
3304 in TEST_BB don't produce any values that are live after TEST_BB.
3305 In other words, all the insns in TEST_BB are there only
3306 to compute a value for x. Add the rtx cost of the insns
3307 in TEST_BB to COST. Record whether TEST_BB is a single simple
3308 set instruction in SIMPLE_P. */
3309
3310 static bool
3311 bb_valid_for_noce_process_p (basic_block test_bb, rtx cond,
3312 unsigned int *cost, bool *simple_p)
3313 {
3314 if (!test_bb)
3315 return false;
3316
3317 rtx_insn *last_insn = last_active_insn (test_bb, false);
3318 rtx last_set = NULL_RTX;
3319
3320 rtx cc = cc_in_cond (cond);
3321
3322 if (!insn_valid_noce_process_p (last_insn, cc))
3323 return false;
3324
3325 /* Punt on blocks ending with asm goto or jumps with other side-effects,
3326 last_active_insn ignores JUMP_INSNs. */
3327 if (JUMP_P (BB_END (test_bb)) && !onlyjump_p (BB_END (test_bb)))
3328 return false;
3329
3330 last_set = single_set (last_insn);
3331
3332 rtx x = SET_DEST (last_set);
3333 rtx_insn *first_insn = first_active_insn (test_bb);
3334 rtx first_set = single_set (first_insn);
3335
3336 if (!first_set)
3337 return false;
3338
3339 /* We have a single simple set, that's okay. */
3340 bool speed_p = optimize_bb_for_speed_p (test_bb);
3341
3342 if (first_insn == last_insn)
3343 {
3344 *simple_p = noce_operand_ok (SET_DEST (first_set));
3345 *cost += pattern_cost (first_set, speed_p);
3346 return *simple_p;
3347 }
3348
3349 rtx_insn *prev_last_insn = PREV_INSN (last_insn);
3350 gcc_assert (prev_last_insn);
3351
3352 /* For now, disallow setting x multiple times in test_bb. */
3353 if (REG_P (x) && reg_set_between_p (x, first_insn, prev_last_insn))
3354 return false;
3355
3356 bitmap test_bb_temps = BITMAP_ALLOC (&reg_obstack);
3357
3358 /* The regs that are live out of test_bb. */
3359 bitmap test_bb_live_out = df_get_live_out (test_bb);
3360
3361 int potential_cost = pattern_cost (last_set, speed_p);
3362 rtx_insn *insn;
3363 FOR_BB_INSNS (test_bb, insn)
3364 {
3365 if (insn != last_insn)
3366 {
3367 if (!active_insn_p (insn))
3368 continue;
3369
3370 if (!insn_valid_noce_process_p (insn, cc))
3371 goto free_bitmap_and_fail;
3372
3373 rtx sset = single_set (insn);
3374 gcc_assert (sset);
3375 rtx dest = SET_DEST (sset);
3376 if (SUBREG_P (dest))
3377 dest = SUBREG_REG (dest);
3378
3379 if (contains_mem_rtx_p (SET_SRC (sset))
3380 || !REG_P (dest)
3381 || reg_overlap_mentioned_p (dest, cond))
3382 goto free_bitmap_and_fail;
3383
3384 potential_cost += pattern_cost (sset, speed_p);
3385 bitmap_set_bit (test_bb_temps, REGNO (dest));
3386 }
3387 }
3388
3389 /* If any of the intermediate results in test_bb are live after test_bb
3390 then fail. */
3391 if (bitmap_intersect_p (test_bb_live_out, test_bb_temps))
3392 goto free_bitmap_and_fail;
3393
3394 BITMAP_FREE (test_bb_temps);
3395 *cost += potential_cost;
3396 *simple_p = false;
3397 return true;
3398
3399 free_bitmap_and_fail:
3400 BITMAP_FREE (test_bb_temps);
3401 return false;
3402 }
3403
3404 /* Helper function to emit a cmov sequence encapsulated in
3405 start_sequence () and end_sequence (). If NEED_CMOV is true
3406 we call noce_emit_cmove to create a cmove sequence. Otherwise emit
3407 a simple move. If successful, store the first instruction of the
3408 sequence in TEMP_DEST and the sequence costs in SEQ_COST. */
3409
3410 static rtx_insn*
3411 try_emit_cmove_seq (struct noce_if_info *if_info, rtx temp,
3412 rtx cond, rtx new_val, rtx old_val, bool need_cmov,
3413 unsigned *cost, rtx *temp_dest,
3414 rtx cc_cmp = NULL, rtx rev_cc_cmp = NULL)
3415 {
3416 rtx_insn *seq = NULL;
3417 *cost = 0;
3418
3419 rtx x = XEXP (cond, 0);
3420 rtx y = XEXP (cond, 1);
3421 rtx_code cond_code = GET_CODE (cond);
3422
3423 start_sequence ();
3424
3425 if (need_cmov)
3426 *temp_dest = noce_emit_cmove (if_info, temp, cond_code,
3427 x, y, new_val, old_val, cc_cmp, rev_cc_cmp);
3428 else
3429 {
3430 *temp_dest = temp;
3431 if (if_info->then_else_reversed)
3432 noce_emit_move_insn (temp, old_val);
3433 else
3434 noce_emit_move_insn (temp, new_val);
3435 }
3436
3437 if (*temp_dest != NULL_RTX)
3438 {
3439 seq = get_insns ();
3440 *cost = seq_cost (seq, if_info->speed_p);
3441 }
3442
3443 end_sequence ();
3444
3445 return seq;
3446 }
3447
3448 /* We have something like:
3449
3450 if (x > y)
3451 { i = EXPR_A; j = EXPR_B; k = EXPR_C; }
3452
3453 Make it:
3454
3455 tmp_i = (x > y) ? EXPR_A : i;
3456 tmp_j = (x > y) ? EXPR_B : j;
3457 tmp_k = (x > y) ? EXPR_C : k;
3458 i = tmp_i;
3459 j = tmp_j;
3460 k = tmp_k;
3461
3462 Subsequent passes are expected to clean up the extra moves.
3463
3464 Look for special cases such as writes to one register which are
3465 read back in another SET, as might occur in a swap idiom or
3466 similar.
3467
3468 These look like:
3469
3470 if (x > y)
3471 i = a;
3472 j = i;
3473
3474 Which we want to rewrite to:
3475
3476 tmp_i = (x > y) ? a : i;
3477 tmp_j = (x > y) ? tmp_i : j;
3478 i = tmp_i;
3479 j = tmp_j;
3480
3481 We can catch these when looking at (SET x y) by keeping a list of the
3482 registers we would have targeted before if-conversion and looking back
3483 through it for an overlap with Y. If we find one, we rewire the
3484 conditional set to use the temporary we introduced earlier.
3485
3486 IF_INFO contains the useful information about the block structure and
3487 jump instructions. */
3488
3489 static bool
3490 noce_convert_multiple_sets (struct noce_if_info *if_info)
3491 {
3492 basic_block test_bb = if_info->test_bb;
3493 basic_block then_bb = if_info->then_bb;
3494 basic_block join_bb = if_info->join_bb;
3495 rtx_insn *jump = if_info->jump;
3496 rtx_insn *cond_earliest;
3497 rtx_insn *insn;
3498
3499 start_sequence ();
3500
3501 /* Decompose the condition attached to the jump. */
3502 rtx cond = noce_get_condition (jump, &cond_earliest, false);
3503 rtx x = XEXP (cond, 0);
3504 rtx y = XEXP (cond, 1);
3505
3506 auto_delete_vec<noce_multiple_sets_info> insn_info;
3507 init_noce_multiple_sets_info (then_bb, insn_info);
3508
3509 int last_needs_comparison = -1;
3510
3511 bool ok = noce_convert_multiple_sets_1
3512 (if_info, insn_info, &last_needs_comparison);
3513 if (!ok)
3514 return false;
3515
3516 /* If there are insns that overwrite part of the initial
3517 comparison, we can still omit creating temporaries for
3518 the last of them.
3519 As the second try will always create a less expensive,
3520 valid sequence, we do not need to compare and can discard
3521 the first one. */
3522 if (last_needs_comparison != -1)
3523 {
3524 end_sequence ();
3525 start_sequence ();
3526 ok = noce_convert_multiple_sets_1
3527 (if_info, insn_info, &last_needs_comparison);
3528 /* Actually we should not fail anymore if we reached here,
3529 but better still check. */
3530 if (!ok)
3531 return false;
3532 }
3533
3534 /* We must have seen some sort of insn to insert, otherwise we were
3535 given an empty BB to convert, and we can't handle that. */
3536 gcc_assert (!insn_info.is_empty ());
3537
3538 /* Now fixup the assignments.
3539 PR116405: Iterate in reverse order and keep track of the targets so that
3540 a move does not overwrite a subsequent value when multiple instructions
3541 have the same target. */
3542 unsigned i;
3543 noce_multiple_sets_info *info;
3544 bitmap set_targets = BITMAP_ALLOC (&reg_obstack);
3545 FOR_EACH_VEC_ELT_REVERSE (insn_info, i, info)
3546 {
3547 gcc_checking_assert (REG_P (info->target));
3548
3549 if (info->target != info->temporary
3550 && !bitmap_bit_p (set_targets, REGNO (info->target)))
3551 noce_emit_move_insn (info->target, info->temporary);
3552
3553 bitmap_set_bit (set_targets, REGNO (info->target));
3554 }
3555 BITMAP_FREE (set_targets);
3556
3557 /* Actually emit the sequence if it isn't too expensive. */
3558 rtx_insn *seq = get_insns ();
3559
3560 if (!targetm.noce_conversion_profitable_p (seq, if_info))
3561 {
3562 end_sequence ();
3563 return false;
3564 }
3565
3566 for (insn = seq; insn; insn = NEXT_INSN (insn))
3567 set_used_flags (insn);
3568
3569 /* Mark all our temporaries and targets as used. */
3570 for (unsigned i = 0; i < insn_info.length (); i++)
3571 {
3572 set_used_flags (insn_info[i]->temporary);
3573 set_used_flags (insn_info[i]->target);
3574 }
3575
3576 set_used_flags (cond);
3577 set_used_flags (x);
3578 set_used_flags (y);
3579
3580 unshare_all_rtl_in_chain (seq);
3581 end_sequence ();
3582
3583 if (!seq)
3584 return false;
3585
3586 for (insn = seq; insn; insn = NEXT_INSN (insn))
3587 if (JUMP_P (insn) || CALL_P (insn)
3588 || recog_memoized (insn) == -1)
3589 return false;
3590
3591 emit_insn_before_setloc (seq, if_info->jump,
3592 INSN_LOCATION (insn_info.last ()->unmodified_insn));
3593
3594 /* Clean up THEN_BB and the edges in and out of it. */
3595 remove_edge (find_edge (test_bb, join_bb));
3596 remove_edge (find_edge (then_bb, join_bb));
3597 redirect_edge_and_branch_force (single_succ_edge (test_bb), join_bb);
3598 delete_basic_block (then_bb);
3599 num_true_changes++;
3600
3601 /* Maybe merge blocks now the jump is simple enough. */
3602 if (can_merge_blocks_p (test_bb, join_bb))
3603 {
3604 merge_blocks (test_bb, join_bb);
3605 num_true_changes++;
3606 }
3607
3608 num_updated_if_blocks++;
3609 if_info->transform_name = "noce_convert_multiple_sets";
3610 return true;
3611 }
3612
3613 /* This goes through all relevant insns of IF_INFO->then_bb and tries to create
3614 conditional moves. Information for the insns is kept in INSN_INFO. */
3615
3616 static bool
3617 noce_convert_multiple_sets_1 (struct noce_if_info *if_info,
3618 auto_delete_vec<noce_multiple_sets_info> &insn_info,
3619 int *last_needs_comparison)
3620 {
3621 basic_block then_bb = if_info->then_bb;
3622 rtx_insn *jump = if_info->jump;
3623 rtx_insn *cond_earliest;
3624
3625 /* Decompose the condition attached to the jump. */
3626 rtx cond = noce_get_condition (jump, &cond_earliest, false);
3627
3628 rtx cc_cmp = cond_exec_get_condition (jump);
3629 if (cc_cmp)
3630 cc_cmp = copy_rtx (cc_cmp);
3631 rtx rev_cc_cmp = cond_exec_get_condition (jump, /* get_reversed */ true);
3632 if (rev_cc_cmp)
3633 rev_cc_cmp = copy_rtx (rev_cc_cmp);
3634
3635 rtx_insn *insn;
3636 int count = 0;
3637 bool second_try = *last_needs_comparison != -1;
3638
3639 FOR_BB_INSNS (then_bb, insn)
3640 {
3641 /* Skip over non-insns. */
3642 if (!active_insn_p (insn))
3643 continue;
3644
3645 noce_multiple_sets_info *info = insn_info[count];
3646
3647 rtx set = single_set (insn);
3648 gcc_checking_assert (set);
3649
3650 rtx target = SET_DEST (set);
3651 rtx temp;
3652
3653 rtx new_val = SET_SRC (set);
3654
3655 int i, ii;
3656 FOR_EACH_VEC_ELT (info->rewired_src, i, ii)
3657 new_val = simplify_replace_rtx (new_val, insn_info[ii]->target,
3658 insn_info[ii]->temporary);
3659
3660 rtx old_val = target;
3661
3662 /* As we are transforming
3663 if (x > y)
3664 {
3665 a = b;
3666 c = d;
3667 }
3668 into
3669 a = (x > y) ...
3670 c = (x > y) ...
3671
3672 we potentially check x > y before every set.
3673 Even though the check might be removed by subsequent passes, this means
3674 that we cannot transform
3675 if (x > y)
3676 {
3677 x = y;
3678 ...
3679 }
3680 into
3681 x = (x > y) ...
3682 ...
3683 since this would invalidate x and the following to-be-removed checks.
3684 Therefore we introduce a temporary every time we are about to
3685 overwrite a variable used in the check. Costing of a sequence with
3686 these is going to be inaccurate so only use temporaries when
3687 needed.
3688
3689 If performing a second try, we know how many insns require a
3690 temporary. For the last of these, we can omit creating one. */
3691 if (reg_overlap_mentioned_p (target, cond)
3692 && (!second_try || count < *last_needs_comparison))
3693 temp = gen_reg_rtx (GET_MODE (target));
3694 else
3695 temp = target;
3696
3697 /* We have identified swap-style idioms before. A normal
3698 set will need to be a cmov while the first instruction of a swap-style
3699 idiom can be a regular move. This helps with costing. */
3700 bool need_cmov = info->need_cmov;
3701
3702 /* If we had a non-canonical conditional jump (i.e. one where
3703 the fallthrough is to the "else" case) we need to reverse
3704 the conditional select. */
3705 if (if_info->then_else_reversed)
3706 std::swap (old_val, new_val);
3707
3708 /* Try emitting a conditional move passing the backend the
3709 canonicalized comparison. The backend is then able to
3710 recognize expressions like
3711
3712 if (x > y)
3713 y = x;
3714
3715 as min/max and emit an insn, accordingly. */
3716 unsigned cost1 = 0, cost2 = 0;
3717 rtx_insn *seq, *seq1, *seq2 = NULL;
3718 rtx temp_dest = NULL_RTX, temp_dest1 = NULL_RTX, temp_dest2 = NULL_RTX;
3719 bool read_comparison = false;
3720
3721 seq1 = try_emit_cmove_seq (if_info, temp, cond,
3722 new_val, old_val, need_cmov,
3723 &cost1, &temp_dest1);
3724
3725 /* Here, we try to pass the backend a non-canonicalized cc comparison
3726 as well. This allows the backend to emit a cmov directly without
3727 creating an additional compare for each. If successful, costing
3728 is easier and this sequence is usually preferred. */
3729 if (cc_cmp)
3730 {
3731 seq2 = try_emit_cmove_seq (if_info, temp, cond,
3732 new_val, old_val, need_cmov,
3733 &cost2, &temp_dest2, cc_cmp, rev_cc_cmp);
3734
3735 /* The if_then_else in SEQ2 may be affected when cc_cmp/rev_cc_cmp is
3736 clobbered. We can't safely use the sequence in this case. */
3737 for (rtx_insn *iter = seq2; iter; iter = NEXT_INSN (iter))
3738 if (modified_in_p (cc_cmp, iter)
3739 || (rev_cc_cmp && modified_in_p (rev_cc_cmp, iter)))
3740 {
3741 seq2 = NULL;
3742 break;
3743 }
3744 }
3745
3746 /* The backend might have created a sequence that uses the
3747 condition as a value. Check this. */
3748
3749 /* We cannot handle anything more complex than a reg or constant. */
3750 if (!REG_P (XEXP (cond, 0)) && !CONSTANT_P (XEXP (cond, 0)))
3751 read_comparison = true;
3752
3753 if (!REG_P (XEXP (cond, 1)) && !CONSTANT_P (XEXP (cond, 1)))
3754 read_comparison = true;
3755
3756 rtx_insn *walk = seq2;
3757 int if_then_else_count = 0;
3758 while (walk && !read_comparison)
3759 {
3760 rtx exprs_to_check[2];
3761 unsigned int exprs_count = 0;
3762
3763 rtx set = single_set (walk);
3764 if (set && XEXP (set, 1)
3765 && GET_CODE (XEXP (set, 1)) == IF_THEN_ELSE)
3766 {
3767 /* We assume that this is the cmove created by the backend that
3768 naturally uses the condition. */
3769 exprs_to_check[exprs_count++] = XEXP (XEXP (set, 1), 1);
3770 exprs_to_check[exprs_count++] = XEXP (XEXP (set, 1), 2);
3771 if_then_else_count++;
3772 }
3773 else if (NONDEBUG_INSN_P (walk))
3774 exprs_to_check[exprs_count++] = PATTERN (walk);
3775
3776 /* Bail if we get more than one if_then_else because the assumption
3777 above may be incorrect. */
3778 if (if_then_else_count > 1)
3779 {
3780 read_comparison = true;
3781 break;
3782 }
3783
3784 for (unsigned int i = 0; i < exprs_count; i++)
3785 {
3786 subrtx_iterator::array_type array;
3787 FOR_EACH_SUBRTX (iter, array, exprs_to_check[i], NONCONST)
3788 if (*iter != NULL_RTX
3789 && (reg_overlap_mentioned_p (XEXP (cond, 0), *iter)
3790 || reg_overlap_mentioned_p (XEXP (cond, 1), *iter)))
3791 {
3792 read_comparison = true;
3793 break;
3794 }
3795 }
3796
3797 walk = NEXT_INSN (walk);
3798 }
3799
3800 /* Check which version is less expensive. */
3801 if (seq1 != NULL_RTX && (cost1 <= cost2 || seq2 == NULL_RTX))
3802 {
3803 seq = seq1;
3804 temp_dest = temp_dest1;
3805 if (!second_try)
3806 *last_needs_comparison = count;
3807 }
3808 else if (seq2 != NULL_RTX)
3809 {
3810 seq = seq2;
3811 temp_dest = temp_dest2;
3812 if (!second_try && read_comparison)
3813 *last_needs_comparison = count;
3814 }
3815 else
3816 {
3817 /* Nothing worked, bail out. */
3818 end_sequence ();
3819 return false;
3820 }
3821
3822 /* Although we use temporaries if there is register overlap of COND and
3823 TARGET, it is possible that SEQ modifies COND anyway. For example,
3824 COND may use the flags register and if INSN clobbers flags then
3825 we may be unable to emit a valid sequence (e.g. in x86 that would
3826 require saving and restoring the flags register). */
3827 if (!second_try)
3828 for (rtx_insn *iter = seq; iter; iter = NEXT_INSN (iter))
3829 if (modified_in_p (cond, iter))
3830 {
3831 end_sequence ();
3832 return false;
3833 }
3834
3835 if (cc_cmp && seq == seq1)
3836 {
3837 /* Check if SEQ can clobber registers mentioned in cc_cmp/rev_cc_cmp.
3838 If yes, we need to use only SEQ1 from that point on.
3839 Only check when we use SEQ1 since we have already tested SEQ2. */
3840 for (rtx_insn *iter = seq; iter; iter = NEXT_INSN (iter))
3841 if (modified_in_p (cc_cmp, iter)
3842 || (rev_cc_cmp && modified_in_p (rev_cc_cmp, iter)))
3843 {
3844 cc_cmp = NULL_RTX;
3845 rev_cc_cmp = NULL_RTX;
3846 break;
3847 }
3848 }
3849
3850 /* End the sub sequence and emit to the main sequence. */
3851 emit_insn (seq);
3852
3853 /* Bookkeeping. */
3854 count++;
3855
3856 info->target = target;
3857 info->temporary = temp_dest;
3858 info->unmodified_insn = insn;
3859 }
3860
3861 /* Even if we did not actually need the comparison, we want to make sure
3862 to try a second time in order to get rid of the temporaries. */
3863 if (*last_needs_comparison == -1)
3864 *last_needs_comparison = 0;
3865
3866 return true;
3867 }
3868
3869 /* Find local swap-style idioms in BB and mark the first insn (1)
3870 that is only a temporary as not needing a conditional move as
3871 it is going to be dead afterwards anyway.
3872
3873 (1) int tmp = a;
3874 a = b;
3875 b = tmp;
3876
3877 ifcvt
3878 -->
3879
3880 tmp = a;
3881 a = cond ? b : a_old;
3882 b = cond ? tmp : b_old;
3883
3884 Additionally, store the index of insns like (2) when a subsequent
3885 SET reads from their destination.
3886
3887 (2) int c = a;
3888 int d = c;
3889
3890 ifcvt
3891 -->
3892
3893 c = cond ? a : c_old;
3894 d = cond ? d : c; // Need to use c rather than c_old here.
3895 */
3896
3897 static void
3898 init_noce_multiple_sets_info (basic_block bb,
3899 auto_delete_vec<noce_multiple_sets_info> &insn_info)
3900 {
3901 rtx_insn *insn;
3902 int count = 0;
3903 auto_vec<rtx> dests;
3904 bitmap bb_live_out = df_get_live_out (bb);
3905
3906 /* Iterate over all SETs, storing the destinations in DEST.
3907 - If we encounter a previously changed register,
3908 rewire the read to the original source.
3909 - If we encounter a SET that writes to a destination
3910 that is not live after this block then the register
3911 does not need to be moved conditionally. */
3912 FOR_BB_INSNS (bb, insn)
3913 {
3914 if (!active_insn_p (insn))
3915 continue;
3916
3917 noce_multiple_sets_info *info = new noce_multiple_sets_info;
3918 info->target = NULL_RTX;
3919 info->temporary = NULL_RTX;
3920 info->unmodified_insn = NULL;
3921 insn_info.safe_push (info);
3922
3923 rtx set = single_set (insn);
3924 gcc_checking_assert (set);
3925
3926 rtx src = SET_SRC (set);
3927 rtx dest = SET_DEST (set);
3928
3929 gcc_checking_assert (REG_P (dest));
3930 info->need_cmov = bitmap_bit_p (bb_live_out, REGNO (dest));
3931
3932 /* Check if the current SET's source is the same
3933 as any previously seen destination.
3934 This is quadratic but the number of insns in BB
3935 is bounded by PARAM_MAX_RTL_IF_CONVERSION_INSNS. */
3936 for (int i = count - 1; i >= 0; --i)
3937 if (reg_mentioned_p (dests[i], src))
3938 insn_info[count]->rewired_src.safe_push (i);
3939
3940 dests.safe_push (dest);
3941 count++;
3942 }
3943 }
3944
3945 /* Return true iff basic block TEST_BB is suitable for conversion to a
3946 series of conditional moves. Also check that we have more than one
3947 set (other routines can handle a single set better than we would),
3948 and fewer than PARAM_MAX_RTL_IF_CONVERSION_INSNS sets. While going
3949 through the insns store the sum of their potential costs in COST. */
3950
3951 static bool
3952 bb_ok_for_noce_convert_multiple_sets (basic_block test_bb, unsigned *cost)
3953 {
3954 rtx_insn *insn;
3955 unsigned count = 0;
3956 unsigned param = param_max_rtl_if_conversion_insns;
3957 bool speed_p = optimize_bb_for_speed_p (test_bb);
3958 unsigned potential_cost = 0;
3959
3960 FOR_BB_INSNS (test_bb, insn)
3961 {
3962 /* Skip over notes etc. */
3963 if (!active_insn_p (insn))
3964 continue;
3965
3966 /* We only handle SET insns. */
3967 rtx set = single_set (insn);
3968 if (set == NULL_RTX)
3969 return false;
3970
3971 rtx dest = SET_DEST (set);
3972 rtx src = SET_SRC (set);
3973
3974 /* Do not handle anything involving memory loads/stores since it might
3975 violate data-race-freedom guarantees. Make sure we can force SRC
3976 to a register as that may be needed in try_emit_cmove_seq. */
3977 if (!REG_P (dest) || contains_mem_rtx_p (src)
3978 || !noce_can_force_operand (src))
3979 return false;
3980
3981 /* Destination and source must be appropriate. */
3982 if (!noce_operand_ok (dest) || !noce_operand_ok (src))
3983 return false;
3984
3985 /* We must be able to conditionally move in this mode. */
3986 if (!can_conditionally_move_p (GET_MODE (dest)))
3987 return false;
3988
3989 potential_cost += insn_cost (insn, speed_p);
3990
3991 count++;
3992 }
3993
3994 *cost += potential_cost;
3995
3996 /* If we would only put out one conditional move, the other strategies
3997 this pass tries are better optimized and will be more appropriate.
3998 Some targets want to strictly limit the number of conditional moves
3999 that are emitted, they set this through PARAM, we need to respect
4000 that. */
4001 return count > 1 && count <= param;
4002 }
4003
4004 /* Compute average of two given costs weighted by relative probabilities
4005 of respective basic blocks in an IF-THEN-ELSE. E is the IF-THEN edge.
4006 With P as the probability to take the IF-THEN branch, return
4007 P * THEN_COST + (1 - P) * ELSE_COST. */
4008 static unsigned
4009 average_cost (unsigned then_cost, unsigned else_cost, edge e)
4010 {
4011 return else_cost + e->probability.apply ((signed) (then_cost - else_cost));
4012 }
4013
4014 /* Given a simple IF-THEN-JOIN or IF-THEN-ELSE-JOIN block, attempt to convert
4015 it without using conditional execution. Return TRUE if we were successful
4016 at converting the block. */
4017
4018 static bool
4019 noce_process_if_block (struct noce_if_info *if_info)
4020 {
4021 basic_block test_bb = if_info->test_bb; /* test block */
4022 basic_block then_bb = if_info->then_bb; /* THEN */
4023 basic_block else_bb = if_info->else_bb; /* ELSE or NULL */
4024 basic_block join_bb = if_info->join_bb; /* JOIN */
4025 rtx_insn *jump = if_info->jump;
4026 rtx cond = if_info->cond;
4027 rtx_insn *insn_a, *insn_b;
4028 rtx set_a, set_b;
4029 rtx orig_x, x, a, b;
4030
4031 /* We're looking for patterns of the form
4032
4033 (1) if (...) x = a; else x = b;
4034 (2) x = b; if (...) x = a;
4035 (3) if (...) x = a; // as if with an initial x = x.
4036 (4) if (...) { x = a; y = b; z = c; } // Like 3, for multiple SETS.
4037 The later patterns require jumps to be more expensive.
4038 For the if (...) x = a; else x = b; case we allow multiple insns
4039 inside the then and else blocks as long as their only effect is
4040 to calculate a value for x.
4041 ??? For future expansion, further expand the "multiple X" rules. */
4042
4043 /* First look for multiple SETS. The original costs already include
4044 a base cost of COSTS_N_INSNS (2): one instruction for the compare
4045 (which we will be needing either way) and one instruction for the
4046 branch. When comparing costs we want to use the branch instruction
4047 cost and the sets vs. the cmovs generated here. Therefore subtract
4048 the costs of the compare before checking.
4049 ??? Actually, instead of the branch instruction costs we might want
4050 to use COSTS_N_INSNS (BRANCH_COST ()) as in other places. */
4051
4052 unsigned potential_cost = if_info->original_cost - COSTS_N_INSNS (1);
4053 unsigned old_cost = if_info->original_cost;
4054 if (!else_bb
4055 && HAVE_conditional_move
4056 && bb_ok_for_noce_convert_multiple_sets (then_bb, &potential_cost))
4057 {
4058 /* Temporarily set the original costs to what we estimated so
4059 we can determine if the transformation is worth it. */
4060 if_info->original_cost = potential_cost;
4061 if (noce_convert_multiple_sets (if_info))
4062 {
4063 if (dump_file && if_info->transform_name)
4064 fprintf (dump_file, "if-conversion succeeded through %s\n",
4065 if_info->transform_name);
4066 return true;
4067 }
4068
4069 /* Restore the original costs. */
4070 if_info->original_cost = old_cost;
4071 }
4072
4073 bool speed_p = optimize_bb_for_speed_p (test_bb);
4074 unsigned int then_cost = 0, else_cost = 0;
4075 if (!bb_valid_for_noce_process_p (then_bb, cond, &then_cost,
4076 &if_info->then_simple))
4077 return false;
4078
4079 if (else_bb
4080 && !bb_valid_for_noce_process_p (else_bb, cond, &else_cost,
4081 &if_info->else_simple))
4082 return false;
4083
4084 if (speed_p)
4085 if_info->original_cost += average_cost (then_cost, else_cost,
4086 find_edge (test_bb, then_bb));
4087 else
4088 if_info->original_cost += then_cost + else_cost;
4089
4090 insn_a = last_active_insn (then_bb, false);
4091 set_a = single_set (insn_a);
4092 gcc_assert (set_a);
4093
4094 x = SET_DEST (set_a);
4095 a = SET_SRC (set_a);
4096
4097 /* Look for the other potential set. Make sure we've got equivalent
4098 destinations. */
4099 /* ??? This is overconservative. Storing to two different mems is
4100 as easy as conditionally computing the address. Storing to a
4101 single mem merely requires a scratch memory to use as one of the
4102 destination addresses; often the memory immediately below the
4103 stack pointer is available for this. */
4104 set_b = NULL_RTX;
4105 if (else_bb)
4106 {
4107 insn_b = last_active_insn (else_bb, false);
4108 set_b = single_set (insn_b);
4109 gcc_assert (set_b);
4110
4111 if (!rtx_interchangeable_p (x, SET_DEST (set_b)))
4112 return false;
4113 }
4114 else
4115 {
4116 insn_b = if_info->cond_earliest;
4117 do
4118 insn_b = prev_nonnote_nondebug_insn (insn_b);
4119 while (insn_b
4120 && (BLOCK_FOR_INSN (insn_b)
4121 == BLOCK_FOR_INSN (if_info->cond_earliest))
4122 && !modified_in_p (x, insn_b));
4123
4124 /* We're going to be moving the evaluation of B down from above
4125 COND_EARLIEST to JUMP. Make sure the relevant data is still
4126 intact. */
4127 if (! insn_b
4128 || BLOCK_FOR_INSN (insn_b) != BLOCK_FOR_INSN (if_info->cond_earliest)
4129 || !NONJUMP_INSN_P (insn_b)
4130 || (set_b = single_set (insn_b)) == NULL_RTX
4131 || ! rtx_interchangeable_p (x, SET_DEST (set_b))
4132 || ! noce_operand_ok (SET_SRC (set_b))
4133 || reg_overlap_mentioned_p (x, SET_SRC (set_b))
4134 || modified_between_p (SET_SRC (set_b), insn_b, jump)
4135 /* Avoid extending the lifetime of hard registers on small
4136 register class machines. */
4137 || (REG_P (SET_SRC (set_b))
4138 && HARD_REGISTER_P (SET_SRC (set_b))
4139 && targetm.small_register_classes_for_mode_p
4140 (GET_MODE (SET_SRC (set_b))))
4141 /* Likewise with X. In particular this can happen when
4142 noce_get_condition looks farther back in the instruction
4143 stream than one might expect. */
4144 || reg_overlap_mentioned_p (x, cond)
4145 || reg_overlap_mentioned_p (x, a)
4146 || modified_between_p (x, insn_b, jump))
4147 {
4148 insn_b = NULL;
4149 set_b = NULL_RTX;
4150 }
4151 }
4152
4153 /* If x has side effects then only the if-then-else form is safe to
4154 convert. But even in that case we would need to restore any notes
4155 (such as REG_INC) at then end. That can be tricky if
4156 noce_emit_move_insn expands to more than one insn, so disable the
4157 optimization entirely for now if there are side effects. */
4158 if (side_effects_p (x))
4159 return false;
4160
4161 b = (set_b ? SET_SRC (set_b) : x);
4162
4163 /* Only operate on register destinations, and even then avoid extending
4164 the lifetime of hard registers on small register class machines. */
4165 orig_x = x;
4166 if_info->orig_x = orig_x;
4167 if (!REG_P (x)
4168 || (HARD_REGISTER_P (x)
4169 && targetm.small_register_classes_for_mode_p (GET_MODE (x))))
4170 {
4171 if (GET_MODE (x) == BLKmode)
4172 return false;
4173
4174 if (GET_CODE (x) == ZERO_EXTRACT
4175 && (!CONST_INT_P (XEXP (x, 1))
4176 || !CONST_INT_P (XEXP (x, 2))))
4177 return false;
4178
4179 x = gen_reg_rtx (GET_MODE (GET_CODE (x) == STRICT_LOW_PART
4180 ? XEXP (x, 0) : x));
4181 }
4182
4183 /* Don't operate on sources that may trap or are volatile. */
4184 if (! noce_operand_ok (a) || ! noce_operand_ok (b))
4185 return false;
4186
4187 retry:
4188 /* Set up the info block for our subroutines. */
4189 if_info->insn_a = insn_a;
4190 if_info->insn_b = insn_b;
4191 if_info->x = x;
4192 if_info->a = a;
4193 if_info->b = b;
4194
4195 /* Try optimizations in some approximation of a useful order. */
4196 /* ??? Should first look to see if X is live incoming at all. If it
4197 isn't, we don't need anything but an unconditional set. */
4198
4199 /* Look and see if A and B are really the same. Avoid creating silly
4200 cmove constructs that no one will fix up later. */
4201 if (noce_simple_bbs (if_info)
4202 && rtx_interchangeable_p (a, b))
4203 {
4204 /* If we have an INSN_B, we don't have to create any new rtl. Just
4205 move the instruction that we already have. If we don't have an
4206 INSN_B, that means that A == X, and we've got a noop move. In
4207 that case don't do anything and let the code below delete INSN_A. */
4208 if (insn_b && else_bb)
4209 {
4210 rtx note;
4211
4212 if (else_bb && insn_b == BB_END (else_bb))
4213 BB_END (else_bb) = PREV_INSN (insn_b);
4214 reorder_insns (insn_b, insn_b, PREV_INSN (jump));
4215
4216 /* If there was a REG_EQUAL note, delete it since it may have been
4217 true due to this insn being after a jump. */
4218 if ((note = find_reg_note (insn_b, REG_EQUAL, NULL_RTX)) != 0)
4219 remove_note (insn_b, note);
4220
4221 insn_b = NULL;
4222 }
4223 /* If we have "x = b; if (...) x = a;", and x has side-effects, then
4224 x must be executed twice. */
4225 else if (insn_b && side_effects_p (orig_x))
4226 return false;
4227
4228 x = orig_x;
4229 goto success;
4230 }
4231
4232 if (!set_b && MEM_P (orig_x))
4233 /* We want to avoid store speculation to avoid cases like
4234 if (pthread_mutex_trylock(mutex))
4235 ++global_variable;
4236 Rather than go to much effort here, we rely on the SSA optimizers,
4237 which do a good enough job these days. */
4238 return false;
4239
4240 if (noce_try_move (if_info))
4241 goto success;
4242 if (noce_try_ifelse_collapse (if_info))
4243 goto success;
4244 if (noce_try_store_flag (if_info))
4245 goto success;
4246 if (noce_try_bitop (if_info))
4247 goto success;
4248 if (noce_try_minmax (if_info))
4249 goto success;
4250 if (noce_try_abs (if_info))
4251 goto success;
4252 if (noce_try_inverse_constants (if_info))
4253 goto success;
4254 if (!targetm.have_conditional_execution ()
4255 && noce_try_store_flag_constants (if_info))
4256 goto success;
4257 if (HAVE_conditional_move
4258 && noce_try_cmove (if_info))
4259 goto success;
4260 if (! targetm.have_conditional_execution ())
4261 {
4262 if (noce_try_addcc (if_info))
4263 goto success;
4264 if (noce_try_store_flag_mask (if_info))
4265 goto success;
4266 if (HAVE_conditional_move
4267 && noce_try_cond_zero_arith (if_info))
4268 goto success;
4269 if (HAVE_conditional_move
4270 && noce_try_cmove_arith (if_info))
4271 goto success;
4272 if (noce_try_sign_mask (if_info))
4273 goto success;
4274 }
4275
4276 if (!else_bb && set_b)
4277 {
4278 insn_b = NULL;
4279 set_b = NULL_RTX;
4280 b = orig_x;
4281 goto retry;
4282 }
4283
4284 return false;
4285
4286 success:
4287 if (dump_file && if_info->transform_name)
4288 fprintf (dump_file, "if-conversion succeeded through %s\n",
4289 if_info->transform_name);
4290
4291 /* If we used a temporary, fix it up now. */
4292 if (orig_x != x)
4293 {
4294 rtx_insn *seq;
4295
4296 start_sequence ();
4297 noce_emit_move_insn (orig_x, x);
4298 seq = get_insns ();
4299 set_used_flags (orig_x);
4300 unshare_all_rtl_in_chain (seq);
4301 end_sequence ();
4302
4303 emit_insn_before_setloc (seq, BB_END (test_bb), INSN_LOCATION (insn_a));
4304 }
4305
4306 /* The original THEN and ELSE blocks may now be removed. The test block
4307 must now jump to the join block. If the test block and the join block
4308 can be merged, do so. */
4309 if (else_bb)
4310 {
4311 delete_basic_block (else_bb);
4312 num_true_changes++;
4313 }
4314 else
4315 remove_edge (find_edge (test_bb, join_bb));
4316
4317 remove_edge (find_edge (then_bb, join_bb));
4318 redirect_edge_and_branch_force (single_succ_edge (test_bb), join_bb);
4319 delete_basic_block (then_bb);
4320 num_true_changes++;
4321
4322 if (can_merge_blocks_p (test_bb, join_bb))
4323 {
4324 merge_blocks (test_bb, join_bb);
4325 num_true_changes++;
4326 }
4327
4328 num_updated_if_blocks++;
4329 return true;
4330 }
4331
4332 /* Check whether a block is suitable for conditional move conversion.
4333 Every insn must be a simple set of a register to a constant or a
4334 register. For each assignment, store the value in the pointer map
4335 VALS, keyed indexed by register pointer, then store the register
4336 pointer in REGS. COND is the condition we will test. */
4337
4338 static bool
4339 check_cond_move_block (basic_block bb,
4340 hash_map<rtx, rtx> *vals,
4341 vec<rtx> *regs,
4342 rtx cond)
4343 {
4344 rtx_insn *insn;
4345 rtx cc = cc_in_cond (cond);
4346
4347 /* We can only handle simple jumps at the end of the basic block.
4348 It is almost impossible to update the CFG otherwise. */
4349 insn = BB_END (bb);
4350 if (JUMP_P (insn) && !onlyjump_p (insn))
4351 return false;
4352
4353 FOR_BB_INSNS (bb, insn)
4354 {
4355 rtx set, dest, src;
4356
4357 if (!NONDEBUG_INSN_P (insn) || JUMP_P (insn))
4358 continue;
4359 set = single_set (insn);
4360 if (!set)
4361 return false;
4362
4363 dest = SET_DEST (set);
4364 src = SET_SRC (set);
4365 if (!REG_P (dest)
4366 || (HARD_REGISTER_P (dest)
4367 && targetm.small_register_classes_for_mode_p (GET_MODE (dest))))
4368 return false;
4369
4370 if (!CONSTANT_P (src) && !register_operand (src, VOIDmode))
4371 return false;
4372
4373 if (side_effects_p (src) || side_effects_p (dest))
4374 return false;
4375
4376 if (may_trap_p (src) || may_trap_p (dest))
4377 return false;
4378
4379 /* Don't try to handle this if the source register was
4380 modified earlier in the block. */
4381 if ((REG_P (src)
4382 && vals->get (src))
4383 || (GET_CODE (src) == SUBREG && REG_P (SUBREG_REG (src))
4384 && vals->get (SUBREG_REG (src))))
4385 return false;
4386
4387 /* Don't try to handle this if the destination register was
4388 modified earlier in the block. */
4389 if (vals->get (dest))
4390 return false;
4391
4392 /* Don't try to handle this if the condition uses the
4393 destination register. */
4394 if (reg_overlap_mentioned_p (dest, cond))
4395 return false;
4396
4397 /* Don't try to handle this if the source register is modified
4398 later in the block. */
4399 if (!CONSTANT_P (src)
4400 && modified_between_p (src, insn, NEXT_INSN (BB_END (bb))))
4401 return false;
4402
4403 /* Skip it if the instruction to be moved might clobber CC. */
4404 if (cc && set_of (cc, insn))
4405 return false;
4406
4407 vals->put (dest, src);
4408
4409 regs->safe_push (dest);
4410 }
4411
4412 return true;
4413 }
4414
4415 /* Given a basic block BB suitable for conditional move conversion,
4416 a condition COND, and pointer maps THEN_VALS and ELSE_VALS containing
4417 the register values depending on COND, emit the insns in the block as
4418 conditional moves. If ELSE_BLOCK is true, THEN_BB was already
4419 processed. The caller has started a sequence for the conversion.
4420 Return true if successful, false if something goes wrong. */
4421
4422 static bool
4423 cond_move_convert_if_block (struct noce_if_info *if_infop,
4424 basic_block bb, rtx cond,
4425 hash_map<rtx, rtx> *then_vals,
4426 hash_map<rtx, rtx> *else_vals,
4427 bool else_block_p)
4428 {
4429 enum rtx_code code;
4430 rtx_insn *insn;
4431 rtx cond_arg0, cond_arg1;
4432
4433 code = GET_CODE (cond);
4434 cond_arg0 = XEXP (cond, 0);
4435 cond_arg1 = XEXP (cond, 1);
4436
4437 FOR_BB_INSNS (bb, insn)
4438 {
4439 rtx set, target, dest, t, e;
4440
4441 /* ??? Maybe emit conditional debug insn? */
4442 if (!NONDEBUG_INSN_P (insn) || JUMP_P (insn))
4443 continue;
4444 set = single_set (insn);
4445 gcc_assert (set && REG_P (SET_DEST (set)));
4446
4447 dest = SET_DEST (set);
4448
4449 rtx *then_slot = then_vals->get (dest);
4450 rtx *else_slot = else_vals->get (dest);
4451 t = then_slot ? *then_slot : NULL_RTX;
4452 e = else_slot ? *else_slot : NULL_RTX;
4453
4454 if (else_block_p)
4455 {
4456 /* If this register was set in the then block, we already
4457 handled this case there. */
4458 if (t)
4459 continue;
4460 t = dest;
4461 gcc_assert (e);
4462 }
4463 else
4464 {
4465 gcc_assert (t);
4466 if (!e)
4467 e = dest;
4468 }
4469
4470 if (if_infop->cond_inverted)
4471 std::swap (t, e);
4472
4473 target = noce_emit_cmove (if_infop, dest, code, cond_arg0, cond_arg1,
4474 t, e);
4475 if (!target)
4476 return false;
4477
4478 if (target != dest)
4479 noce_emit_move_insn (dest, target);
4480 }
4481
4482 return true;
4483 }
4484
4485 /* Given a simple IF-THEN-JOIN or IF-THEN-ELSE-JOIN block, attempt to convert
4486 it using only conditional moves. Return TRUE if we were successful at
4487 converting the block. */
4488
4489 static bool
4490 cond_move_process_if_block (struct noce_if_info *if_info)
4491 {
4492 basic_block test_bb = if_info->test_bb;
4493 basic_block then_bb = if_info->then_bb;
4494 basic_block else_bb = if_info->else_bb;
4495 basic_block join_bb = if_info->join_bb;
4496 rtx_insn *jump = if_info->jump;
4497 rtx cond = if_info->cond;
4498 rtx_insn *seq, *loc_insn;
4499 int c;
4500 vec<rtx> then_regs = vNULL;
4501 vec<rtx> else_regs = vNULL;
4502 bool success_p = false;
4503 int limit = param_max_rtl_if_conversion_insns;
4504
4505 /* Build a mapping for each block to the value used for each
4506 register. */
4507 hash_map<rtx, rtx> then_vals;
4508 hash_map<rtx, rtx> else_vals;
4509
4510 /* Make sure the blocks are suitable. */
4511 if (!check_cond_move_block (then_bb, &then_vals, &then_regs, cond)
4512 || (else_bb
4513 && !check_cond_move_block (else_bb, &else_vals, &else_regs, cond)))
4514 goto done;
4515
4516 /* Make sure the blocks can be used together. If the same register
4517 is set in both blocks, and is not set to a constant in both
4518 cases, then both blocks must set it to the same register. We
4519 have already verified that if it is set to a register, that the
4520 source register does not change after the assignment. Also count
4521 the number of registers set in only one of the blocks. */
4522 c = 0;
4523 for (rtx reg : then_regs)
4524 {
4525 rtx *then_slot = then_vals.get (reg);
4526 rtx *else_slot = else_vals.get (reg);
4527
4528 gcc_checking_assert (then_slot);
4529 if (!else_slot)
4530 ++c;
4531 else
4532 {
4533 rtx then_val = *then_slot;
4534 rtx else_val = *else_slot;
4535 if (!CONSTANT_P (then_val) && !CONSTANT_P (else_val)
4536 && !rtx_equal_p (then_val, else_val))
4537 goto done;
4538 }
4539 }
4540
4541 /* Finish off c for MAX_CONDITIONAL_EXECUTE. */
4542 for (rtx reg : else_regs)
4543 {
4544 gcc_checking_assert (else_vals.get (reg));
4545 if (!then_vals.get (reg))
4546 ++c;
4547 }
4548
4549 /* Make sure it is reasonable to convert this block. What matters
4550 is the number of assignments currently made in only one of the
4551 branches, since if we convert we are going to always execute
4552 them. */
4553 if (c > MAX_CONDITIONAL_EXECUTE
4554 || c > limit)
4555 goto done;
4556
4557 /* Try to emit the conditional moves. First do the then block,
4558 then do anything left in the else blocks. */
4559 start_sequence ();
4560 if (!cond_move_convert_if_block (if_info, then_bb, cond,
4561 &then_vals, &else_vals, false)
4562 || (else_bb
4563 && !cond_move_convert_if_block (if_info, else_bb, cond,
4564 &then_vals, &else_vals, true)))
4565 {
4566 end_sequence ();
4567 goto done;
4568 }
4569 seq = end_ifcvt_sequence (if_info);
4570 if (!seq || !targetm.noce_conversion_profitable_p (seq, if_info))
4571 goto done;
4572
4573 loc_insn = first_active_insn (then_bb);
4574 if (!loc_insn)
4575 {
4576 loc_insn = first_active_insn (else_bb);
4577 gcc_assert (loc_insn);
4578 }
4579 emit_insn_before_setloc (seq, jump, INSN_LOCATION (loc_insn));
4580
4581 if (else_bb)
4582 {
4583 delete_basic_block (else_bb);
4584 num_true_changes++;
4585 }
4586 else
4587 remove_edge (find_edge (test_bb, join_bb));
4588
4589 remove_edge (find_edge (then_bb, join_bb));
4590 redirect_edge_and_branch_force (single_succ_edge (test_bb), join_bb);
4591 delete_basic_block (then_bb);
4592 num_true_changes++;
4593
4594 if (can_merge_blocks_p (test_bb, join_bb))
4595 {
4596 merge_blocks (test_bb, join_bb);
4597 num_true_changes++;
4598 }
4599
4600 num_updated_if_blocks++;
4601 success_p = true;
4602
4603 done:
4604 then_regs.release ();
4605 else_regs.release ();
4606 return success_p;
4607 }
4608
4609
4610 /* Determine if a given basic block heads a simple IF-THEN-JOIN or an
4611 IF-THEN-ELSE-JOIN block.
4612
4613 If so, we'll try to convert the insns to not require the branch,
4614 using only transformations that do not require conditional execution.
4615
4616 Return TRUE if we were successful at converting the block. */
4617
4618 static bool
4619 noce_find_if_block (basic_block test_bb, edge then_edge, edge else_edge,
4620 int pass)
4621 {
4622 basic_block then_bb, else_bb, join_bb;
4623 bool then_else_reversed = false;
4624 rtx_insn *jump;
4625 rtx_insn *cond_earliest;
4626 struct noce_if_info if_info;
4627 bool speed_p = optimize_bb_for_speed_p (test_bb);
4628
4629 /* We only ever should get here before reload. */
4630 gcc_assert (!reload_completed);
4631
4632 /* Recognize an IF-THEN-ELSE-JOIN block. */
4633 if (single_pred_p (then_edge->dest)
4634 && single_succ_p (then_edge->dest)
4635 && single_pred_p (else_edge->dest)
4636 && single_succ_p (else_edge->dest)
4637 && single_succ (then_edge->dest) == single_succ (else_edge->dest))
4638 {
4639 then_bb = then_edge->dest;
4640 else_bb = else_edge->dest;
4641 join_bb = single_succ (then_bb);
4642 }
4643 /* Recognize an IF-THEN-JOIN block. */
4644 else if (single_pred_p (then_edge->dest)
4645 && single_succ_p (then_edge->dest)
4646 && single_succ (then_edge->dest) == else_edge->dest)
4647 {
4648 then_bb = then_edge->dest;
4649 else_bb = NULL_BLOCK;
4650 join_bb = else_edge->dest;
4651 }
4652 /* Recognize an IF-ELSE-JOIN block. We can have those because the order
4653 of basic blocks in cfglayout mode does not matter, so the fallthrough
4654 edge can go to any basic block (and not just to bb->next_bb, like in
4655 cfgrtl mode). */
4656 else if (single_pred_p (else_edge->dest)
4657 && single_succ_p (else_edge->dest)
4658 && single_succ (else_edge->dest) == then_edge->dest)
4659 {
4660 /* The noce transformations do not apply to IF-ELSE-JOIN blocks.
4661 To make this work, we have to invert the THEN and ELSE blocks
4662 and reverse the jump condition. */
4663 then_bb = else_edge->dest;
4664 else_bb = NULL_BLOCK;
4665 join_bb = single_succ (then_bb);
4666 then_else_reversed = true;
4667 }
4668 else
4669 /* Not a form we can handle. */
4670 return false;
4671
4672 /* The edges of the THEN and ELSE blocks cannot have complex edges. */
4673 if (single_succ_edge (then_bb)->flags & EDGE_COMPLEX)
4674 return false;
4675 if (else_bb
4676 && single_succ_edge (else_bb)->flags & EDGE_COMPLEX)
4677 return false;
4678
4679 num_possible_if_blocks++;
4680
4681 if (dump_file)
4682 {
4683 fprintf (dump_file,
4684 "\nIF-THEN%s-JOIN block found, pass %d, test %d, then %d",
4685 (else_bb) ? "-ELSE" : "",
4686 pass, test_bb->index, then_bb->index);
4687
4688 if (else_bb)
4689 fprintf (dump_file, ", else %d", else_bb->index);
4690
4691 fprintf (dump_file, ", join %d\n", join_bb->index);
4692 }
4693
4694 /* If the conditional jump is more than just a conditional
4695 jump, then we cannot do if-conversion on this block. */
4696 jump = BB_END (test_bb);
4697 if (! onlyjump_p (jump))
4698 return false;
4699
4700 /* Initialize an IF_INFO struct to pass around. */
4701 memset (&if_info, 0, sizeof if_info);
4702 if_info.test_bb = test_bb;
4703 if_info.then_bb = then_bb;
4704 if_info.else_bb = else_bb;
4705 if_info.join_bb = join_bb;
4706 if_info.cond = noce_get_condition (jump, &cond_earliest,
4707 then_else_reversed);
4708 rtx_insn *rev_cond_earliest;
4709 if_info.rev_cond = noce_get_condition (jump, &rev_cond_earliest,
4710 !then_else_reversed);
4711 if (!if_info.cond && !if_info.rev_cond)
4712 return false;
4713 if (!if_info.cond)
4714 {
4715 std::swap (if_info.cond, if_info.rev_cond);
4716 std::swap (cond_earliest, rev_cond_earliest);
4717 if_info.cond_inverted = true;
4718 }
4719 /* We must be comparing objects whose modes imply the size. */
4720 if (GET_MODE (XEXP (if_info.cond, 0)) == BLKmode)
4721 return false;
4722 gcc_assert (if_info.rev_cond == NULL_RTX
4723 || rev_cond_earliest == cond_earliest);
4724 if_info.cond_earliest = cond_earliest;
4725 if_info.jump = jump;
4726 if_info.then_else_reversed = then_else_reversed;
4727 if_info.speed_p = speed_p;
4728 if_info.max_seq_cost
4729 = targetm.max_noce_ifcvt_seq_cost (then_edge);
4730 /* We'll add in the cost of THEN_BB and ELSE_BB later, when we check
4731 that they are valid to transform. We can't easily get back to the insn
4732 for COND (and it may not exist if we had to canonicalize to get COND),
4733 and jump_insns are always given a cost of 1 by seq_cost, so treat
4734 both instructions as having cost COSTS_N_INSNS (1). */
4735 if_info.original_cost = COSTS_N_INSNS (2);
4736
4737
4738 /* Do the real work. */
4739
4740 /* ??? noce_process_if_block has not yet been updated to handle
4741 inverted conditions. */
4742 if (!if_info.cond_inverted && noce_process_if_block (&if_info))
4743 return true;
4744
4745 if (HAVE_conditional_move
4746 && cond_move_process_if_block (&if_info))
4747 return true;
4748
4749 return false;
4750 }
4751
4752
4753 /* Merge the blocks and mark for local life update. */
4754
4755 static void
4756 merge_if_block (struct ce_if_block * ce_info)
4757 {
4758 basic_block test_bb = ce_info->test_bb; /* last test block */
4759 basic_block then_bb = ce_info->then_bb; /* THEN */
4760 basic_block else_bb = ce_info->else_bb; /* ELSE or NULL */
4761 basic_block join_bb = ce_info->join_bb; /* join block */
4762 basic_block combo_bb;
4763
4764 /* All block merging is done into the lower block numbers. */
4765
4766 combo_bb = test_bb;
4767 df_set_bb_dirty (test_bb);
4768
4769 /* Merge any basic blocks to handle && and || subtests. Each of
4770 the blocks are on the fallthru path from the predecessor block. */
4771 if (ce_info->num_multiple_test_blocks > 0)
4772 {
4773 basic_block bb = test_bb;
4774 basic_block last_test_bb = ce_info->last_test_bb;
4775 basic_block fallthru = block_fallthru (bb);
4776
4777 do
4778 {
4779 bb = fallthru;
4780 fallthru = block_fallthru (bb);
4781 merge_blocks (combo_bb, bb);
4782 num_true_changes++;
4783 }
4784 while (bb != last_test_bb);
4785 }
4786
4787 /* Merge TEST block into THEN block. Normally the THEN block won't have a
4788 label, but it might if there were || tests. That label's count should be
4789 zero, and it normally should be removed. */
4790
4791 if (then_bb)
4792 {
4793 /* If THEN_BB has no successors, then there's a BARRIER after it.
4794 If COMBO_BB has more than one successor (THEN_BB), then that BARRIER
4795 is no longer needed, and in fact it is incorrect to leave it in
4796 the insn stream. */
4797 if (EDGE_COUNT (then_bb->succs) == 0
4798 && EDGE_COUNT (combo_bb->succs) > 1)
4799 {
4800 rtx_insn *end = NEXT_INSN (BB_END (then_bb));
4801 while (end && NOTE_P (end) && !NOTE_INSN_BASIC_BLOCK_P (end))
4802 end = NEXT_INSN (end);
4803
4804 if (end && BARRIER_P (end))
4805 delete_insn (end);
4806 }
4807 merge_blocks (combo_bb, then_bb);
4808 num_true_changes++;
4809 }
4810
4811 /* The ELSE block, if it existed, had a label. That label count
4812 will almost always be zero, but odd things can happen when labels
4813 get their addresses taken. */
4814 if (else_bb)
4815 {
4816 /* If ELSE_BB has no successors, then there's a BARRIER after it.
4817 If COMBO_BB has more than one successor (ELSE_BB), then that BARRIER
4818 is no longer needed, and in fact it is incorrect to leave it in
4819 the insn stream. */
4820 if (EDGE_COUNT (else_bb->succs) == 0
4821 && EDGE_COUNT (combo_bb->succs) > 1)
4822 {
4823 rtx_insn *end = NEXT_INSN (BB_END (else_bb));
4824 while (end && NOTE_P (end) && !NOTE_INSN_BASIC_BLOCK_P (end))
4825 end = NEXT_INSN (end);
4826
4827 if (end && BARRIER_P (end))
4828 delete_insn (end);
4829 }
4830 merge_blocks (combo_bb, else_bb);
4831 num_true_changes++;
4832 }
4833
4834 /* If there was no join block reported, that means it was not adjacent
4835 to the others, and so we cannot merge them. */
4836
4837 if (! join_bb)
4838 {
4839 rtx_insn *last = BB_END (combo_bb);
4840
4841 /* The outgoing edge for the current COMBO block should already
4842 be correct. Verify this. */
4843 if (EDGE_COUNT (combo_bb->succs) == 0)
4844 gcc_assert (find_reg_note (last, REG_NORETURN, NULL)
4845 || (NONJUMP_INSN_P (last)
4846 && GET_CODE (PATTERN (last)) == TRAP_IF
4847 && (TRAP_CONDITION (PATTERN (last))
4848 == const_true_rtx)));
4849
4850 else
4851 /* There should still be something at the end of the THEN or ELSE
4852 blocks taking us to our final destination. */
4853 gcc_assert (JUMP_P (last)
4854 || (EDGE_SUCC (combo_bb, 0)->dest
4855 == EXIT_BLOCK_PTR_FOR_FN (cfun)
4856 && CALL_P (last)
4857 && SIBLING_CALL_P (last))
4858 || ((EDGE_SUCC (combo_bb, 0)->flags & EDGE_EH)
4859 && can_throw_internal (last)));
4860 }
4861
4862 /* The JOIN block may have had quite a number of other predecessors too.
4863 Since we've already merged the TEST, THEN and ELSE blocks, we should
4864 have only one remaining edge from our if-then-else diamond. If there
4865 is more than one remaining edge, it must come from elsewhere. There
4866 may be zero incoming edges if the THEN block didn't actually join
4867 back up (as with a call to a non-return function). */
4868 else if (EDGE_COUNT (join_bb->preds) < 2
4869 && join_bb != EXIT_BLOCK_PTR_FOR_FN (cfun))
4870 {
4871 /* We can merge the JOIN cleanly and update the dataflow try
4872 again on this pass.*/
4873 merge_blocks (combo_bb, join_bb);
4874 num_true_changes++;
4875 }
4876 else
4877 {
4878 /* We cannot merge the JOIN. */
4879
4880 /* The outgoing edge for the current COMBO block should already
4881 be correct. Verify this. */
4882 gcc_assert (single_succ_p (combo_bb)
4883 && single_succ (combo_bb) == join_bb);
4884
4885 /* Remove the jump and cruft from the end of the COMBO block. */
4886 if (join_bb != EXIT_BLOCK_PTR_FOR_FN (cfun))
4887 tidy_fallthru_edge (single_succ_edge (combo_bb));
4888 }
4889
4890 num_updated_if_blocks++;
4891 }
4892
4893 /* Find a block ending in a simple IF condition and try to transform it
4894 in some way. When converting a multi-block condition, put the new code
4895 in the first such block and delete the rest. Return a pointer to this
4896 first block if some transformation was done. Return NULL otherwise. */
4897
4898 static basic_block
4899 find_if_header (basic_block test_bb, int pass)
4900 {
4901 ce_if_block ce_info;
4902 edge then_edge;
4903 edge else_edge;
4904
4905 /* The kind of block we're looking for has exactly two successors. */
4906 if (EDGE_COUNT (test_bb->succs) != 2)
4907 return NULL;
4908
4909 then_edge = EDGE_SUCC (test_bb, 0);
4910 else_edge = EDGE_SUCC (test_bb, 1);
4911
4912 if (df_get_bb_dirty (then_edge->dest))
4913 return NULL;
4914 if (df_get_bb_dirty (else_edge->dest))
4915 return NULL;
4916
4917 /* Neither edge should be abnormal. */
4918 if ((then_edge->flags & EDGE_COMPLEX)
4919 || (else_edge->flags & EDGE_COMPLEX))
4920 return NULL;
4921
4922 /* Nor exit the loop. */
4923 if ((then_edge->flags & EDGE_LOOP_EXIT)
4924 || (else_edge->flags & EDGE_LOOP_EXIT))
4925 return NULL;
4926
4927 /* The THEN edge is canonically the one that falls through. */
4928 if (then_edge->flags & EDGE_FALLTHRU)
4929 ;
4930 else if (else_edge->flags & EDGE_FALLTHRU)
4931 std::swap (then_edge, else_edge);
4932 else
4933 /* Otherwise this must be a multiway branch of some sort. */
4934 return NULL;
4935
4936 memset (&ce_info, 0, sizeof (ce_info));
4937 ce_info.test_bb = test_bb;
4938 ce_info.then_bb = then_edge->dest;
4939 ce_info.else_bb = else_edge->dest;
4940 ce_info.pass = pass;
4941
4942 #ifdef IFCVT_MACHDEP_INIT
4943 IFCVT_MACHDEP_INIT (&ce_info);
4944 #endif
4945
4946 if (!reload_completed
4947 && noce_find_if_block (test_bb, then_edge, else_edge, pass))
4948 goto success;
4949
4950 if (reload_completed
4951 && targetm.have_conditional_execution ()
4952 && cond_exec_find_if_block (&ce_info))
4953 goto success;
4954
4955 if (!reload_completed && targetm.have_trap ()
4956 && optab_handler (ctrap_optab, word_mode) != CODE_FOR_nothing
4957 && find_cond_trap (test_bb, then_edge, else_edge))
4958 goto success;
4959
4960 if (dom_info_state (CDI_POST_DOMINATORS) >= DOM_NO_FAST_QUERY
4961 && (reload_completed || !targetm.have_conditional_execution ()))
4962 {
4963 if (find_if_case_1 (test_bb, then_edge, else_edge))
4964 goto success;
4965 if (find_if_case_2 (test_bb, then_edge, else_edge))
4966 goto success;
4967 }
4968
4969 return NULL;
4970
4971 success:
4972 if (dump_file)
4973 fprintf (dump_file, "Conversion succeeded on pass %d.\n", pass);
4974 /* Set this so we continue looking. */
4975 cond_exec_changed_p = true;
4976 return ce_info.test_bb;
4977 }
4978
4979 /* Return true if a block has two edges, one of which falls through to the next
4980 block, and the other jumps to a specific block, so that we can tell if the
4981 block is part of an && test or an || test. Returns either -1 or the number
4982 of non-note, non-jump, non-USE/CLOBBER insns in the block. */
4983
4984 static int
4985 block_jumps_and_fallthru (basic_block cur_bb, basic_block target_bb)
4986 {
4987 edge cur_edge;
4988 bool fallthru_p = false;
4989 bool jump_p = false;
4990 rtx_insn *insn;
4991 rtx_insn *end;
4992 int n_insns = 0;
4993 edge_iterator ei;
4994
4995 if (!cur_bb || !target_bb)
4996 return -1;
4997
4998 /* If no edges, obviously it doesn't jump or fallthru. */
4999 if (EDGE_COUNT (cur_bb->succs) == 0)
5000 return 0;
5001
5002 FOR_EACH_EDGE (cur_edge, ei, cur_bb->succs)
5003 {
5004 if (cur_edge->flags & EDGE_COMPLEX)
5005 /* Anything complex isn't what we want. */
5006 return -1;
5007
5008 else if (cur_edge->flags & EDGE_FALLTHRU)
5009 fallthru_p = true;
5010
5011 else if (cur_edge->dest == target_bb)
5012 jump_p = true;
5013
5014 else
5015 return -1;
5016 }
5017
5018 if ((jump_p & fallthru_p) == 0)
5019 return -1;
5020
5021 /* Don't allow calls in the block, since this is used to group && and ||
5022 together for conditional execution support. ??? we should support
5023 conditional execution support across calls for IA-64 some day, but
5024 for now it makes the code simpler. */
5025 end = BB_END (cur_bb);
5026 insn = BB_HEAD (cur_bb);
5027
5028 while (insn != NULL_RTX)
5029 {
5030 if (CALL_P (insn))
5031 return -1;
5032
5033 if (INSN_P (insn)
5034 && !JUMP_P (insn)
5035 && !DEBUG_INSN_P (insn)
5036 && GET_CODE (PATTERN (insn)) != USE
5037 && GET_CODE (PATTERN (insn)) != CLOBBER)
5038 n_insns++;
5039
5040 if (insn == end)
5041 break;
5042
5043 insn = NEXT_INSN (insn);
5044 }
5045
5046 return n_insns;
5047 }
5048
5049 /* Determine if a given basic block heads a simple IF-THEN or IF-THEN-ELSE
5050 block. If so, we'll try to convert the insns to not require the branch.
5051 Return TRUE if we were successful at converting the block. */
5052
5053 static bool
5054 cond_exec_find_if_block (struct ce_if_block * ce_info)
5055 {
5056 basic_block test_bb = ce_info->test_bb;
5057 basic_block then_bb = ce_info->then_bb;
5058 basic_block else_bb = ce_info->else_bb;
5059 basic_block join_bb = NULL_BLOCK;
5060 edge cur_edge;
5061 basic_block next;
5062 edge_iterator ei;
5063
5064 ce_info->last_test_bb = test_bb;
5065
5066 /* We only ever should get here after reload,
5067 and if we have conditional execution. */
5068 gcc_assert (reload_completed && targetm.have_conditional_execution ());
5069
5070 /* Discover if any fall through predecessors of the current test basic block
5071 were && tests (which jump to the else block) or || tests (which jump to
5072 the then block). */
5073 if (single_pred_p (test_bb)
5074 && single_pred_edge (test_bb)->flags == EDGE_FALLTHRU)
5075 {
5076 basic_block bb = single_pred (test_bb);
5077 basic_block target_bb;
5078 int max_insns = MAX_CONDITIONAL_EXECUTE;
5079 int n_insns;
5080
5081 /* Determine if the preceding block is an && or || block. */
5082 if ((n_insns = block_jumps_and_fallthru (bb, else_bb)) >= 0)
5083 {
5084 ce_info->and_and_p = true;
5085 target_bb = else_bb;
5086 }
5087 else if ((n_insns = block_jumps_and_fallthru (bb, then_bb)) >= 0)
5088 {
5089 ce_info->and_and_p = false;
5090 target_bb = then_bb;
5091 }
5092 else
5093 target_bb = NULL_BLOCK;
5094
5095 if (target_bb && n_insns <= max_insns)
5096 {
5097 int total_insns = 0;
5098 int blocks = 0;
5099
5100 ce_info->last_test_bb = test_bb;
5101
5102 /* Found at least one && or || block, look for more. */
5103 do
5104 {
5105 ce_info->test_bb = test_bb = bb;
5106 total_insns += n_insns;
5107 blocks++;
5108
5109 if (!single_pred_p (bb))
5110 break;
5111
5112 bb = single_pred (bb);
5113 n_insns = block_jumps_and_fallthru (bb, target_bb);
5114 }
5115 while (n_insns >= 0 && (total_insns + n_insns) <= max_insns);
5116
5117 ce_info->num_multiple_test_blocks = blocks;
5118 ce_info->num_multiple_test_insns = total_insns;
5119
5120 if (ce_info->and_and_p)
5121 ce_info->num_and_and_blocks = blocks;
5122 else
5123 ce_info->num_or_or_blocks = blocks;
5124 }
5125 }
5126
5127 /* The THEN block of an IF-THEN combo must have exactly one predecessor,
5128 other than any || blocks which jump to the THEN block. */
5129 if ((EDGE_COUNT (then_bb->preds) - ce_info->num_or_or_blocks) != 1)
5130 return false;
5131
5132 /* The edges of the THEN and ELSE blocks cannot have complex edges. */
5133 FOR_EACH_EDGE (cur_edge, ei, then_bb->preds)
5134 {
5135 if (cur_edge->flags & EDGE_COMPLEX)
5136 return false;
5137 }
5138
5139 FOR_EACH_EDGE (cur_edge, ei, else_bb->preds)
5140 {
5141 if (cur_edge->flags & EDGE_COMPLEX)
5142 return false;
5143 }
5144
5145 /* The THEN block of an IF-THEN combo must have zero or one successors. */
5146 if (EDGE_COUNT (then_bb->succs) > 0
5147 && (!single_succ_p (then_bb)
5148 || (single_succ_edge (then_bb)->flags & EDGE_COMPLEX)
5149 || (epilogue_completed
5150 && tablejump_p (BB_END (then_bb), NULL, NULL))))
5151 return false;
5152
5153 /* If the THEN block has no successors, conditional execution can still
5154 make a conditional call. Don't do this unless the ELSE block has
5155 only one incoming edge -- the CFG manipulation is too ugly otherwise.
5156 Check for the last insn of the THEN block being an indirect jump, which
5157 is listed as not having any successors, but confuses the rest of the CE
5158 code processing. ??? we should fix this in the future. */
5159 if (EDGE_COUNT (then_bb->succs) == 0)
5160 {
5161 if (single_pred_p (else_bb) && else_bb != EXIT_BLOCK_PTR_FOR_FN (cfun))
5162 {
5163 rtx_insn *last_insn = BB_END (then_bb);
5164
5165 while (last_insn
5166 && NOTE_P (last_insn)
5167 && last_insn != BB_HEAD (then_bb))
5168 last_insn = PREV_INSN (last_insn);
5169
5170 if (last_insn
5171 && JUMP_P (last_insn)
5172 && ! simplejump_p (last_insn))
5173 return false;
5174
5175 join_bb = else_bb;
5176 else_bb = NULL_BLOCK;
5177 }
5178 else
5179 return false;
5180 }
5181
5182 /* If the THEN block's successor is the other edge out of the TEST block,
5183 then we have an IF-THEN combo without an ELSE. */
5184 else if (single_succ (then_bb) == else_bb)
5185 {
5186 join_bb = else_bb;
5187 else_bb = NULL_BLOCK;
5188 }
5189
5190 /* If the THEN and ELSE block meet in a subsequent block, and the ELSE
5191 has exactly one predecessor and one successor, and the outgoing edge
5192 is not complex, then we have an IF-THEN-ELSE combo. */
5193 else if (single_succ_p (else_bb)
5194 && single_succ (then_bb) == single_succ (else_bb)
5195 && single_pred_p (else_bb)
5196 && !(single_succ_edge (else_bb)->flags & EDGE_COMPLEX)
5197 && !(epilogue_completed
5198 && tablejump_p (BB_END (else_bb), NULL, NULL)))
5199 join_bb = single_succ (else_bb);
5200
5201 /* Otherwise it is not an IF-THEN or IF-THEN-ELSE combination. */
5202 else
5203 return false;
5204
5205 num_possible_if_blocks++;
5206
5207 if (dump_file)
5208 {
5209 fprintf (dump_file,
5210 "\nIF-THEN%s block found, pass %d, start block %d "
5211 "[insn %d], then %d [%d]",
5212 (else_bb) ? "-ELSE" : "",
5213 ce_info->pass,
5214 test_bb->index,
5215 BB_HEAD (test_bb) ? (int)INSN_UID (BB_HEAD (test_bb)) : -1,
5216 then_bb->index,
5217 BB_HEAD (then_bb) ? (int)INSN_UID (BB_HEAD (then_bb)) : -1);
5218
5219 if (else_bb)
5220 fprintf (dump_file, ", else %d [%d]",
5221 else_bb->index,
5222 BB_HEAD (else_bb) ? (int)INSN_UID (BB_HEAD (else_bb)) : -1);
5223
5224 fprintf (dump_file, ", join %d [%d]",
5225 join_bb->index,
5226 BB_HEAD (join_bb) ? (int)INSN_UID (BB_HEAD (join_bb)) : -1);
5227
5228 if (ce_info->num_multiple_test_blocks > 0)
5229 fprintf (dump_file, ", %d %s block%s last test %d [%d]",
5230 ce_info->num_multiple_test_blocks,
5231 (ce_info->and_and_p) ? "&&" : "||",
5232 (ce_info->num_multiple_test_blocks == 1) ? "" : "s",
5233 ce_info->last_test_bb->index,
5234 ((BB_HEAD (ce_info->last_test_bb))
5235 ? (int)INSN_UID (BB_HEAD (ce_info->last_test_bb))
5236 : -1));
5237
5238 fputc ('\n', dump_file);
5239 }
5240
5241 /* Make sure IF, THEN, and ELSE, blocks are adjacent. Actually, we get the
5242 first condition for free, since we've already asserted that there's a
5243 fallthru edge from IF to THEN. Likewise for the && and || blocks, since
5244 we checked the FALLTHRU flag, those are already adjacent to the last IF
5245 block. */
5246 /* ??? As an enhancement, move the ELSE block. Have to deal with
5247 BLOCK notes, if by no other means than backing out the merge if they
5248 exist. Sticky enough I don't want to think about it now. */
5249 next = then_bb;
5250 if (else_bb && (next = next->next_bb) != else_bb)
5251 return false;
5252 if ((next = next->next_bb) != join_bb
5253 && join_bb != EXIT_BLOCK_PTR_FOR_FN (cfun))
5254 {
5255 if (else_bb)
5256 join_bb = NULL;
5257 else
5258 return false;
5259 }
5260
5261 /* Do the real work. */
5262
5263 ce_info->else_bb = else_bb;
5264 ce_info->join_bb = join_bb;
5265
5266 /* If we have && and || tests, try to first handle combining the && and ||
5267 tests into the conditional code, and if that fails, go back and handle
5268 it without the && and ||, which at present handles the && case if there
5269 was no ELSE block. */
5270 if (cond_exec_process_if_block (ce_info, true))
5271 return true;
5272
5273 if (ce_info->num_multiple_test_blocks)
5274 {
5275 cancel_changes (0);
5276
5277 if (cond_exec_process_if_block (ce_info, false))
5278 return true;
5279 }
5280
5281 return false;
5282 }
5283
5284 /* Convert a branch over a trap, or a branch
5285 to a trap, into a conditional trap. */
5286
5287 static bool
5288 find_cond_trap (basic_block test_bb, edge then_edge, edge else_edge)
5289 {
5290 basic_block then_bb = then_edge->dest;
5291 basic_block else_bb = else_edge->dest;
5292 basic_block other_bb, trap_bb;
5293 rtx_insn *trap, *jump;
5294 rtx cond;
5295 rtx_insn *cond_earliest;
5296
5297 /* Locate the block with the trap instruction. */
5298 /* ??? While we look for no successors, we really ought to allow
5299 EH successors. Need to fix merge_if_block for that to work. */
5300 if ((trap = block_has_only_trap (then_bb)) != NULL)
5301 trap_bb = then_bb, other_bb = else_bb;
5302 else if ((trap = block_has_only_trap (else_bb)) != NULL)
5303 trap_bb = else_bb, other_bb = then_bb;
5304 else
5305 return false;
5306
5307 if (dump_file)
5308 {
5309 fprintf (dump_file, "\nTRAP-IF block found, start %d, trap %d\n",
5310 test_bb->index, trap_bb->index);
5311 }
5312
5313 /* If this is not a standard conditional jump, we can't parse it. */
5314 jump = BB_END (test_bb);
5315 cond = noce_get_condition (jump, &cond_earliest, then_bb == trap_bb);
5316 if (! cond)
5317 return false;
5318
5319 /* If the conditional jump is more than just a conditional jump, then
5320 we cannot do if-conversion on this block. Give up for returnjump_p,
5321 changing a conditional return followed by unconditional trap for
5322 conditional trap followed by unconditional return is likely not
5323 beneficial and harder to handle. */
5324 if (! onlyjump_p (jump) || returnjump_p (jump))
5325 return false;
5326
5327 /* We must be comparing objects whose modes imply the size. */
5328 if (GET_MODE (XEXP (cond, 0)) == BLKmode)
5329 return false;
5330
5331 /* Attempt to generate the conditional trap. */
5332 rtx_insn *seq = gen_cond_trap (GET_CODE (cond), copy_rtx (XEXP (cond, 0)),
5333 copy_rtx (XEXP (cond, 1)),
5334 TRAP_CODE (PATTERN (trap)));
5335 if (seq == NULL)
5336 return false;
5337
5338 /* If that results in an invalid insn, back out. */
5339 for (rtx_insn *x = seq; x; x = NEXT_INSN (x))
5340 if (reload_completed
5341 ? !valid_insn_p (x)
5342 : recog_memoized (x) < 0)
5343 return false;
5344
5345 /* Emit the new insns before cond_earliest. */
5346 emit_insn_before_setloc (seq, cond_earliest, INSN_LOCATION (trap));
5347
5348 /* Delete the trap block if possible. */
5349 remove_edge (trap_bb == then_bb ? then_edge : else_edge);
5350 df_set_bb_dirty (test_bb);
5351 df_set_bb_dirty (then_bb);
5352 df_set_bb_dirty (else_bb);
5353
5354 if (EDGE_COUNT (trap_bb->preds) == 0)
5355 {
5356 delete_basic_block (trap_bb);
5357 num_true_changes++;
5358 }
5359
5360 /* Wire together the blocks again. */
5361 if (current_ir_type () == IR_RTL_CFGLAYOUT)
5362 single_succ_edge (test_bb)->flags |= EDGE_FALLTHRU;
5363 else if (trap_bb == then_bb)
5364 {
5365 rtx lab = JUMP_LABEL (jump);
5366 rtx_insn *seq = targetm.gen_jump (lab);
5367 rtx_jump_insn *newjump = emit_jump_insn_after (seq, jump);
5368 LABEL_NUSES (lab) += 1;
5369 JUMP_LABEL (newjump) = lab;
5370 emit_barrier_after (newjump);
5371 }
5372 delete_insn (jump);
5373
5374 if (can_merge_blocks_p (test_bb, other_bb))
5375 {
5376 merge_blocks (test_bb, other_bb);
5377 num_true_changes++;
5378 }
5379
5380 num_updated_if_blocks++;
5381 return true;
5382 }
5383
5384 /* Subroutine of find_cond_trap: if BB contains only a trap insn,
5385 return it. */
5386
5387 static rtx_insn *
5388 block_has_only_trap (basic_block bb)
5389 {
5390 rtx_insn *trap;
5391
5392 /* We're not the exit block. */
5393 if (bb == EXIT_BLOCK_PTR_FOR_FN (cfun))
5394 return NULL;
5395
5396 /* The block must have no successors. */
5397 if (EDGE_COUNT (bb->succs) > 0)
5398 return NULL;
5399
5400 /* The only instruction in the THEN block must be the trap. */
5401 trap = first_active_insn (bb);
5402 if (! (trap == BB_END (bb)
5403 && GET_CODE (PATTERN (trap)) == TRAP_IF
5404 && TRAP_CONDITION (PATTERN (trap)) == const_true_rtx))
5405 return NULL;
5406
5407 return trap;
5408 }
5409
5410 /* Look for IF-THEN-ELSE cases in which one of THEN or ELSE is
5411 transformable, but not necessarily the other. There need be no
5412 JOIN block.
5413
5414 Return TRUE if we were successful at converting the block.
5415
5416 Cases we'd like to look at:
5417
5418 (1)
5419 if (test) goto over; // x not live
5420 x = a;
5421 goto label;
5422 over:
5423
5424 becomes
5425
5426 x = a;
5427 if (! test) goto label;
5428
5429 (2)
5430 if (test) goto E; // x not live
5431 x = big();
5432 goto L;
5433 E:
5434 x = b;
5435 goto M;
5436
5437 becomes
5438
5439 x = b;
5440 if (test) goto M;
5441 x = big();
5442 goto L;
5443
5444 (3) // This one's really only interesting for targets that can do
5445 // multiway branching, e.g. IA-64 BBB bundles. For other targets
5446 // it results in multiple branches on a cache line, which often
5447 // does not sit well with predictors.
5448
5449 if (test1) goto E; // predicted not taken
5450 x = a;
5451 if (test2) goto F;
5452 ...
5453 E:
5454 x = b;
5455 J:
5456
5457 becomes
5458
5459 x = a;
5460 if (test1) goto E;
5461 if (test2) goto F;
5462
5463 Notes:
5464
5465 (A) Don't do (2) if the branch is predicted against the block we're
5466 eliminating. Do it anyway if we can eliminate a branch; this requires
5467 that the sole successor of the eliminated block postdominate the other
5468 side of the if.
5469
5470 (B) With CE, on (3) we can steal from both sides of the if, creating
5471
5472 if (test1) x = a;
5473 if (!test1) x = b;
5474 if (test1) goto J;
5475 if (test2) goto F;
5476 ...
5477 J:
5478
5479 Again, this is most useful if J postdominates.
5480
5481 (C) CE substitutes for helpful life information.
5482
5483 (D) These heuristics need a lot of work. */
5484
5485 /* Tests for case 1 above. */
5486
5487 static bool
5488 find_if_case_1 (basic_block test_bb, edge then_edge, edge else_edge)
5489 {
5490 basic_block then_bb = then_edge->dest;
5491 basic_block else_bb = else_edge->dest;
5492 basic_block new_bb;
5493 int then_bb_index;
5494 profile_probability then_prob;
5495 rtx else_target = NULL_RTX;
5496
5497 /* If we are partitioning hot/cold basic blocks, we don't want to
5498 mess up unconditional or indirect jumps that cross between hot
5499 and cold sections.
5500
5501 Basic block partitioning may result in some jumps that appear to
5502 be optimizable (or blocks that appear to be mergeable), but which really
5503 must be left untouched (they are required to make it safely across
5504 partition boundaries). See the comments at the top of
5505 bb-reorder.cc:partition_hot_cold_basic_blocks for complete details. */
5506
5507 if ((BB_END (then_bb)
5508 && JUMP_P (BB_END (then_bb))
5509 && CROSSING_JUMP_P (BB_END (then_bb)))
5510 || (JUMP_P (BB_END (test_bb))
5511 && CROSSING_JUMP_P (BB_END (test_bb)))
5512 || (BB_END (else_bb)
5513 && JUMP_P (BB_END (else_bb))
5514 && CROSSING_JUMP_P (BB_END (else_bb))))
5515 return false;
5516
5517 /* Verify test_bb ends in a conditional jump with no other side-effects. */
5518 if (!onlyjump_p (BB_END (test_bb)))
5519 return false;
5520
5521 /* THEN has one successor. */
5522 if (!single_succ_p (then_bb))
5523 return false;
5524
5525 /* THEN does not fall through, but is not strange either. */
5526 if (single_succ_edge (then_bb)->flags & (EDGE_COMPLEX | EDGE_FALLTHRU))
5527 return false;
5528
5529 /* THEN has one predecessor. */
5530 if (!single_pred_p (then_bb))
5531 return false;
5532
5533 /* THEN must do something. */
5534 if (forwarder_block_p (then_bb))
5535 return false;
5536
5537 num_possible_if_blocks++;
5538 if (dump_file)
5539 fprintf (dump_file,
5540 "\nIF-CASE-1 found, start %d, then %d\n",
5541 test_bb->index, then_bb->index);
5542
5543 then_prob = then_edge->probability.invert ();
5544
5545 /* We're speculating from the THEN path, we want to make sure the cost
5546 of speculation is within reason. */
5547 if (! cheap_bb_rtx_cost_p (then_bb, then_prob,
5548 COSTS_N_INSNS (BRANCH_COST (optimize_bb_for_speed_p (then_edge->src),
5549 predictable_edge_p (then_edge)))))
5550 return false;
5551
5552 if (else_bb == EXIT_BLOCK_PTR_FOR_FN (cfun))
5553 {
5554 rtx_insn *jump = BB_END (else_edge->src);
5555 gcc_assert (JUMP_P (jump));
5556 else_target = JUMP_LABEL (jump);
5557 }
5558
5559 /* Registers set are dead, or are predicable. */
5560 if (! dead_or_predicable (test_bb, then_bb, else_bb,
5561 single_succ_edge (then_bb), true))
5562 return false;
5563
5564 /* Conversion went ok, including moving the insns and fixing up the
5565 jump. Adjust the CFG to match. */
5566
5567 /* We can avoid creating a new basic block if then_bb is immediately
5568 followed by else_bb, i.e. deleting then_bb allows test_bb to fall
5569 through to else_bb. */
5570
5571 if (then_bb->next_bb == else_bb
5572 && then_bb->prev_bb == test_bb
5573 && else_bb != EXIT_BLOCK_PTR_FOR_FN (cfun))
5574 {
5575 redirect_edge_succ (FALLTHRU_EDGE (test_bb), else_bb);
5576 new_bb = 0;
5577 }
5578 else if (else_bb == EXIT_BLOCK_PTR_FOR_FN (cfun))
5579 new_bb = force_nonfallthru_and_redirect (FALLTHRU_EDGE (test_bb),
5580 else_bb, else_target);
5581 else
5582 new_bb = redirect_edge_and_branch_force (FALLTHRU_EDGE (test_bb),
5583 else_bb);
5584
5585 df_set_bb_dirty (test_bb);
5586 df_set_bb_dirty (else_bb);
5587
5588 then_bb_index = then_bb->index;
5589 delete_basic_block (then_bb);
5590
5591 /* Make rest of code believe that the newly created block is the THEN_BB
5592 block we removed. */
5593 if (new_bb)
5594 {
5595 df_bb_replace (then_bb_index, new_bb);
5596 /* This should have been done above via force_nonfallthru_and_redirect
5597 (possibly called from redirect_edge_and_branch_force). */
5598 gcc_checking_assert (BB_PARTITION (new_bb) == BB_PARTITION (test_bb));
5599 }
5600
5601 num_true_changes++;
5602 num_updated_if_blocks++;
5603 return true;
5604 }
5605
5606 /* Test for case 2 above. */
5607
5608 static bool
5609 find_if_case_2 (basic_block test_bb, edge then_edge, edge else_edge)
5610 {
5611 basic_block then_bb = then_edge->dest;
5612 basic_block else_bb = else_edge->dest;
5613 edge else_succ;
5614 profile_probability then_prob, else_prob;
5615
5616 /* We do not want to speculate (empty) loop latches. */
5617 if (current_loops
5618 && else_bb->loop_father->latch == else_bb)
5619 return false;
5620
5621 /* If we are partitioning hot/cold basic blocks, we don't want to
5622 mess up unconditional or indirect jumps that cross between hot
5623 and cold sections.
5624
5625 Basic block partitioning may result in some jumps that appear to
5626 be optimizable (or blocks that appear to be mergeable), but which really
5627 must be left untouched (they are required to make it safely across
5628 partition boundaries). See the comments at the top of
5629 bb-reorder.cc:partition_hot_cold_basic_blocks for complete details. */
5630
5631 if ((BB_END (then_bb)
5632 && JUMP_P (BB_END (then_bb))
5633 && CROSSING_JUMP_P (BB_END (then_bb)))
5634 || (JUMP_P (BB_END (test_bb))
5635 && CROSSING_JUMP_P (BB_END (test_bb)))
5636 || (BB_END (else_bb)
5637 && JUMP_P (BB_END (else_bb))
5638 && CROSSING_JUMP_P (BB_END (else_bb))))
5639 return false;
5640
5641 /* Verify test_bb ends in a conditional jump with no other side-effects. */
5642 if (!onlyjump_p (BB_END (test_bb)))
5643 return false;
5644
5645 /* ELSE has one successor. */
5646 if (!single_succ_p (else_bb))
5647 return false;
5648 else
5649 else_succ = single_succ_edge (else_bb);
5650
5651 /* ELSE outgoing edge is not complex. */
5652 if (else_succ->flags & EDGE_COMPLEX)
5653 return false;
5654
5655 /* ELSE has one predecessor. */
5656 if (!single_pred_p (else_bb))
5657 return false;
5658
5659 /* THEN is not EXIT. */
5660 if (then_bb->index < NUM_FIXED_BLOCKS)
5661 return false;
5662
5663 else_prob = else_edge->probability;
5664 then_prob = else_prob.invert ();
5665
5666 /* ELSE is predicted or SUCC(ELSE) postdominates THEN. */
5667 if (else_prob > then_prob)
5668 ;
5669 else if (else_succ->dest->index < NUM_FIXED_BLOCKS
5670 || dominated_by_p (CDI_POST_DOMINATORS, then_bb,
5671 else_succ->dest))
5672 ;
5673 else
5674 return false;
5675
5676 num_possible_if_blocks++;
5677 if (dump_file)
5678 fprintf (dump_file,
5679 "\nIF-CASE-2 found, start %d, else %d\n",
5680 test_bb->index, else_bb->index);
5681
5682 /* We're speculating from the ELSE path, we want to make sure the cost
5683 of speculation is within reason. */
5684 if (! cheap_bb_rtx_cost_p (else_bb, else_prob,
5685 COSTS_N_INSNS (BRANCH_COST (optimize_bb_for_speed_p (else_edge->src),
5686 predictable_edge_p (else_edge)))))
5687 return false;
5688
5689 /* Registers set are dead, or are predicable. */
5690 if (! dead_or_predicable (test_bb, else_bb, then_bb, else_succ, false))
5691 return false;
5692
5693 /* Conversion went ok, including moving the insns and fixing up the
5694 jump. Adjust the CFG to match. */
5695
5696 df_set_bb_dirty (test_bb);
5697 df_set_bb_dirty (then_bb);
5698 delete_basic_block (else_bb);
5699
5700 num_true_changes++;
5701 num_updated_if_blocks++;
5702
5703 /* ??? We may now fallthru from one of THEN's successors into a join
5704 block. Rerun cleanup_cfg? Examine things manually? Wait? */
5705
5706 return true;
5707 }
5708
5709 /* Used by the code above to perform the actual rtl transformations.
5710 Return TRUE if successful.
5711
5712 TEST_BB is the block containing the conditional branch. MERGE_BB
5713 is the block containing the code to manipulate. DEST_EDGE is an
5714 edge representing a jump to the join block; after the conversion,
5715 TEST_BB should be branching to its destination.
5716 REVERSEP is true if the sense of the branch should be reversed. */
5717
5718 static bool
5719 dead_or_predicable (basic_block test_bb, basic_block merge_bb,
5720 basic_block other_bb, edge dest_edge, bool reversep)
5721 {
5722 basic_block new_dest = dest_edge->dest;
5723 rtx_insn *head, *end, *jump;
5724 rtx_insn *earliest = NULL;
5725 rtx old_dest;
5726 bitmap merge_set = NULL;
5727 /* Number of pending changes. */
5728 int n_validated_changes = 0;
5729 rtx new_dest_label = NULL_RTX;
5730
5731 jump = BB_END (test_bb);
5732
5733 /* Find the extent of the real code in the merge block. */
5734 head = BB_HEAD (merge_bb);
5735 end = BB_END (merge_bb);
5736
5737 while (DEBUG_INSN_P (end) && end != head)
5738 end = PREV_INSN (end);
5739
5740 /* If merge_bb ends with a tablejump, predicating/moving insn's
5741 into test_bb and then deleting merge_bb will result in the jumptable
5742 that follows merge_bb being removed along with merge_bb and then we
5743 get an unresolved reference to the jumptable. */
5744 if (tablejump_p (end, NULL, NULL))
5745 return false;
5746
5747 if (LABEL_P (head))
5748 head = NEXT_INSN (head);
5749 while (DEBUG_INSN_P (head) && head != end)
5750 head = NEXT_INSN (head);
5751 if (NOTE_P (head))
5752 {
5753 if (head == end)
5754 {
5755 head = end = NULL;
5756 goto no_body;
5757 }
5758 head = NEXT_INSN (head);
5759 while (DEBUG_INSN_P (head) && head != end)
5760 head = NEXT_INSN (head);
5761 }
5762
5763 if (JUMP_P (end))
5764 {
5765 if (!onlyjump_p (end))
5766 return false;
5767 if (head == end)
5768 {
5769 head = end = NULL;
5770 goto no_body;
5771 }
5772 end = PREV_INSN (end);
5773 while (DEBUG_INSN_P (end) && end != head)
5774 end = PREV_INSN (end);
5775 }
5776
5777 /* Don't move frame-related insn across the conditional branch. This
5778 can lead to one of the paths of the branch having wrong unwind info. */
5779 if (epilogue_completed)
5780 {
5781 rtx_insn *insn = head;
5782 while (1)
5783 {
5784 if (INSN_P (insn) && RTX_FRAME_RELATED_P (insn))
5785 return false;
5786 if (insn == end)
5787 break;
5788 insn = NEXT_INSN (insn);
5789 }
5790 }
5791
5792 /* Disable handling dead code by conditional execution if the machine needs
5793 to do anything funny with the tests, etc. */
5794 #ifndef IFCVT_MODIFY_TESTS
5795 if (targetm.have_conditional_execution ())
5796 {
5797 /* In the conditional execution case, we have things easy. We know
5798 the condition is reversible. We don't have to check life info
5799 because we're going to conditionally execute the code anyway.
5800 All that's left is making sure the insns involved can actually
5801 be predicated. */
5802
5803 rtx cond;
5804
5805 /* If the conditional jump is more than just a conditional jump,
5806 then we cannot do conditional execution conversion on this block. */
5807 if (!onlyjump_p (jump))
5808 goto nce;
5809
5810 cond = cond_exec_get_condition (jump);
5811 if (! cond)
5812 goto nce;
5813
5814 rtx note = find_reg_note (jump, REG_BR_PROB, NULL_RTX);
5815 profile_probability prob_val
5816 = (note ? profile_probability::from_reg_br_prob_note (XINT (note, 0))
5817 : profile_probability::uninitialized ());
5818
5819 if (reversep)
5820 {
5821 enum rtx_code rev = reversed_comparison_code (cond, jump);
5822 if (rev == UNKNOWN)
5823 return false;
5824 cond = gen_rtx_fmt_ee (rev, GET_MODE (cond), XEXP (cond, 0),
5825 XEXP (cond, 1));
5826 prob_val = prob_val.invert ();
5827 }
5828
5829 if (cond_exec_process_insns (NULL, head, end, cond, prob_val, false)
5830 && verify_changes (0))
5831 n_validated_changes = num_validated_changes ();
5832 else
5833 cancel_changes (0);
5834
5835 earliest = jump;
5836 }
5837 nce:
5838 #endif
5839
5840 /* If we allocated new pseudos (e.g. in the conditional move
5841 expander called from noce_emit_cmove), we must resize the
5842 array first. */
5843 if (max_regno < max_reg_num ())
5844 max_regno = max_reg_num ();
5845
5846 /* Try the NCE path if the CE path did not result in any changes. */
5847 if (n_validated_changes == 0)
5848 {
5849 rtx cond;
5850 rtx_insn *insn;
5851 regset live;
5852 bool success;
5853
5854 /* In the non-conditional execution case, we have to verify that there
5855 are no trapping operations, no calls, no references to memory, and
5856 that any registers modified are dead at the branch site. */
5857
5858 if (!any_condjump_p (jump))
5859 return false;
5860
5861 /* Find the extent of the conditional. */
5862 cond = noce_get_condition (jump, &earliest, false);
5863 if (!cond)
5864 return false;
5865
5866 live = BITMAP_ALLOC (&reg_obstack);
5867 simulate_backwards_to_point (merge_bb, live, end);
5868 success = can_move_insns_across (head, end, earliest, jump,
5869 merge_bb, live,
5870 df_get_live_in (other_bb), NULL);
5871 BITMAP_FREE (live);
5872 if (!success)
5873 return false;
5874
5875 /* Collect the set of registers set in MERGE_BB. */
5876 merge_set = BITMAP_ALLOC (&reg_obstack);
5877
5878 FOR_BB_INSNS (merge_bb, insn)
5879 if (NONDEBUG_INSN_P (insn))
5880 df_simulate_find_defs (insn, merge_set);
5881
5882 /* If shrink-wrapping, disable this optimization when test_bb is
5883 the first basic block and merge_bb exits. The idea is to not
5884 move code setting up a return register as that may clobber a
5885 register used to pass function parameters, which then must be
5886 saved in caller-saved regs. A caller-saved reg requires the
5887 prologue, killing a shrink-wrap opportunity. */
5888 if ((SHRINK_WRAPPING_ENABLED && !epilogue_completed)
5889 && ENTRY_BLOCK_PTR_FOR_FN (cfun)->next_bb == test_bb
5890 && single_succ_p (new_dest)
5891 && single_succ (new_dest) == EXIT_BLOCK_PTR_FOR_FN (cfun)
5892 && bitmap_intersect_p (df_get_live_in (new_dest), merge_set))
5893 {
5894 regset return_regs;
5895 unsigned int i;
5896
5897 return_regs = BITMAP_ALLOC (&reg_obstack);
5898
5899 /* Start off with the intersection of regs used to pass
5900 params and regs used to return values. */
5901 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
5902 if (FUNCTION_ARG_REGNO_P (i)
5903 && targetm.calls.function_value_regno_p (i))
5904 bitmap_set_bit (return_regs, INCOMING_REGNO (i));
5905
5906 bitmap_and_into (return_regs,
5907 df_get_live_out (ENTRY_BLOCK_PTR_FOR_FN (cfun)));
5908 bitmap_and_into (return_regs,
5909 df_get_live_in (EXIT_BLOCK_PTR_FOR_FN (cfun)));
5910 if (!bitmap_empty_p (return_regs))
5911 {
5912 FOR_BB_INSNS_REVERSE (new_dest, insn)
5913 if (NONDEBUG_INSN_P (insn))
5914 {
5915 df_ref def;
5916
5917 /* If this insn sets any reg in return_regs, add all
5918 reg uses to the set of regs we're interested in. */
5919 FOR_EACH_INSN_DEF (def, insn)
5920 if (bitmap_bit_p (return_regs, DF_REF_REGNO (def)))
5921 {
5922 df_simulate_uses (insn, return_regs);
5923 break;
5924 }
5925 }
5926 if (bitmap_intersect_p (merge_set, return_regs))
5927 {
5928 BITMAP_FREE (return_regs);
5929 BITMAP_FREE (merge_set);
5930 return false;
5931 }
5932 }
5933 BITMAP_FREE (return_regs);
5934 }
5935 }
5936
5937 no_body:
5938 /* We don't want to use normal invert_jump or redirect_jump because
5939 we don't want to delete_insn called. Also, we want to do our own
5940 change group management. */
5941
5942 old_dest = JUMP_LABEL (jump);
5943 if (other_bb != new_dest)
5944 {
5945 if (!any_condjump_p (jump))
5946 goto cancel;
5947
5948 if (JUMP_P (BB_END (dest_edge->src)))
5949 new_dest_label = JUMP_LABEL (BB_END (dest_edge->src));
5950 else if (new_dest == EXIT_BLOCK_PTR_FOR_FN (cfun))
5951 new_dest_label = ret_rtx;
5952 else
5953 new_dest_label = block_label (new_dest);
5954
5955 rtx_jump_insn *jump_insn = as_a <rtx_jump_insn *> (jump);
5956 if (reversep
5957 ? ! invert_jump_1 (jump_insn, new_dest_label)
5958 : ! redirect_jump_1 (jump_insn, new_dest_label))
5959 goto cancel;
5960 }
5961
5962 if (verify_changes (n_validated_changes))
5963 confirm_change_group ();
5964 else
5965 goto cancel;
5966
5967 if (other_bb != new_dest)
5968 {
5969 redirect_jump_2 (as_a <rtx_jump_insn *> (jump), old_dest, new_dest_label,
5970 0, reversep);
5971
5972 redirect_edge_succ (BRANCH_EDGE (test_bb), new_dest);
5973 if (reversep)
5974 {
5975 std::swap (BRANCH_EDGE (test_bb)->probability,
5976 FALLTHRU_EDGE (test_bb)->probability);
5977 update_br_prob_note (test_bb);
5978 }
5979 }
5980
5981 /* Move the insns out of MERGE_BB to before the branch. */
5982 if (head != NULL)
5983 {
5984 rtx_insn *insn;
5985
5986 if (end == BB_END (merge_bb))
5987 BB_END (merge_bb) = PREV_INSN (head);
5988
5989 /* PR 21767: when moving insns above a conditional branch, the REG_EQUAL
5990 notes being moved might become invalid. */
5991 insn = head;
5992 do
5993 {
5994 rtx note;
5995
5996 if (! INSN_P (insn))
5997 continue;
5998 note = find_reg_note (insn, REG_EQUAL, NULL_RTX);
5999 if (! note)
6000 continue;
6001 remove_note (insn, note);
6002 } while (insn != end && (insn = NEXT_INSN (insn)));
6003
6004 /* PR46315: when moving insns above a conditional branch, the REG_EQUAL
6005 notes referring to the registers being set might become invalid. */
6006 if (merge_set)
6007 {
6008 unsigned i;
6009 bitmap_iterator bi;
6010
6011 EXECUTE_IF_SET_IN_BITMAP (merge_set, 0, i, bi)
6012 remove_reg_equal_equiv_notes_for_regno (i);
6013
6014 BITMAP_FREE (merge_set);
6015 }
6016
6017 reorder_insns (head, end, PREV_INSN (earliest));
6018 }
6019
6020 /* Remove the jump and edge if we can. */
6021 if (other_bb == new_dest)
6022 {
6023 delete_insn (jump);
6024 remove_edge (BRANCH_EDGE (test_bb));
6025 /* ??? Can't merge blocks here, as then_bb is still in use.
6026 At minimum, the merge will get done just before bb-reorder. */
6027 }
6028
6029 return true;
6030
6031 cancel:
6032 cancel_changes (0);
6033
6034 if (merge_set)
6035 BITMAP_FREE (merge_set);
6036
6037 return false;
6038 }
6039
6040 /* Main entry point for all if-conversion. AFTER_COMBINE is true if
6041 we are after combine pass. */
6042
6043 static void
6044 if_convert (bool after_combine)
6045 {
6046 basic_block bb;
6047 int pass;
6048
6049 if (optimize == 1)
6050 {
6051 df_live_add_problem ();
6052 df_live_set_all_dirty ();
6053 }
6054
6055 /* Record whether we are after combine pass. */
6056 ifcvt_after_combine = after_combine;
6057 have_cbranchcc4 = (direct_optab_handler (cbranch_optab, CCmode)
6058 != CODE_FOR_nothing);
6059 num_possible_if_blocks = 0;
6060 num_updated_if_blocks = 0;
6061 num_true_changes = 0;
6062
6063 loop_optimizer_init (AVOID_CFG_MODIFICATIONS);
6064 mark_loop_exit_edges ();
6065 loop_optimizer_finalize ();
6066 free_dominance_info (CDI_DOMINATORS);
6067
6068 /* Compute postdominators. */
6069 calculate_dominance_info (CDI_POST_DOMINATORS);
6070
6071 df_set_flags (DF_LR_RUN_DCE);
6072
6073 /* Go through each of the basic blocks looking for things to convert. If we
6074 have conditional execution, we make multiple passes to allow us to handle
6075 IF-THEN{-ELSE} blocks within other IF-THEN{-ELSE} blocks. */
6076 pass = 0;
6077 do
6078 {
6079 df_analyze ();
6080 /* Only need to do dce on the first pass. */
6081 df_clear_flags (DF_LR_RUN_DCE);
6082 cond_exec_changed_p = false;
6083 pass++;
6084
6085 #ifdef IFCVT_MULTIPLE_DUMPS
6086 if (dump_file && pass > 1)
6087 fprintf (dump_file, "\n\n========== Pass %d ==========\n", pass);
6088 #endif
6089
6090 FOR_EACH_BB_FN (bb, cfun)
6091 {
6092 basic_block new_bb;
6093 while (!df_get_bb_dirty (bb)
6094 && (new_bb = find_if_header (bb, pass)) != NULL)
6095 bb = new_bb;
6096 }
6097
6098 #ifdef IFCVT_MULTIPLE_DUMPS
6099 if (dump_file && cond_exec_changed_p)
6100 print_rtl_with_bb (dump_file, get_insns (), dump_flags);
6101 #endif
6102 }
6103 while (cond_exec_changed_p);
6104
6105 #ifdef IFCVT_MULTIPLE_DUMPS
6106 if (dump_file)
6107 fprintf (dump_file, "\n\n========== no more changes\n");
6108 #endif
6109
6110 free_dominance_info (CDI_POST_DOMINATORS);
6111
6112 if (dump_file)
6113 fflush (dump_file);
6114
6115 clear_aux_for_blocks ();
6116
6117 /* If we allocated new pseudos, we must resize the array for sched1. */
6118 if (max_regno < max_reg_num ())
6119 max_regno = max_reg_num ();
6120
6121 /* Write the final stats. */
6122 if (dump_file && num_possible_if_blocks > 0)
6123 {
6124 fprintf (dump_file,
6125 "\n%d possible IF blocks searched.\n",
6126 num_possible_if_blocks);
6127 fprintf (dump_file,
6128 "%d IF blocks converted.\n",
6129 num_updated_if_blocks);
6130 fprintf (dump_file,
6131 "%d true changes made.\n\n\n",
6132 num_true_changes);
6133 }
6134
6135 if (optimize == 1)
6136 df_remove_problem (df_live);
6137
6138 /* Some non-cold blocks may now be only reachable from cold blocks.
6139 Fix that up. */
6140 fixup_partitions ();
6141
6142 checking_verify_flow_info ();
6143 }
6144
6145 /* If-conversion and CFG cleanup. */
6146 static void
6147 rest_of_handle_if_conversion (void)
6148 {
6149 int flags = 0;
6150
6151 if (flag_if_conversion)
6152 {
6153 if (dump_file)
6154 {
6155 dump_reg_info (dump_file);
6156 dump_flow_info (dump_file, dump_flags);
6157 }
6158 cleanup_cfg (CLEANUP_EXPENSIVE);
6159 if_convert (false);
6160 if (num_updated_if_blocks)
6161 /* Get rid of any dead CC-related instructions. */
6162 flags |= CLEANUP_FORCE_FAST_DCE;
6163 }
6164
6165 cleanup_cfg (flags);
6166 }
6167
6168 namespace {
6169
6170 const pass_data pass_data_rtl_ifcvt =
6171 {
6172 RTL_PASS, /* type */
6173 "ce1", /* name */
6174 OPTGROUP_NONE, /* optinfo_flags */
6175 TV_IFCVT, /* tv_id */
6176 0, /* properties_required */
6177 0, /* properties_provided */
6178 0, /* properties_destroyed */
6179 0, /* todo_flags_start */
6180 TODO_df_finish, /* todo_flags_finish */
6181 };
6182
6183 class pass_rtl_ifcvt : public rtl_opt_pass
6184 {
6185 public:
6186 pass_rtl_ifcvt (gcc::context *ctxt)
6187 : rtl_opt_pass (pass_data_rtl_ifcvt, ctxt)
6188 {}
6189
6190 /* opt_pass methods: */
6191 bool gate (function *) final override
6192 {
6193 return (optimize > 0) && dbg_cnt (if_conversion);
6194 }
6195
6196 unsigned int execute (function *) final override
6197 {
6198 rest_of_handle_if_conversion ();
6199 return 0;
6200 }
6201
6202 }; // class pass_rtl_ifcvt
6203
6204 } // anon namespace
6205
6206 rtl_opt_pass *
6207 make_pass_rtl_ifcvt (gcc::context *ctxt)
6208 {
6209 return new pass_rtl_ifcvt (ctxt);
6210 }
6211
6212
6213 /* Rerun if-conversion, as combine may have simplified things enough
6214 to now meet sequence length restrictions. */
6215
6216 namespace {
6217
6218 const pass_data pass_data_if_after_combine =
6219 {
6220 RTL_PASS, /* type */
6221 "ce2", /* name */
6222 OPTGROUP_NONE, /* optinfo_flags */
6223 TV_IFCVT, /* tv_id */
6224 0, /* properties_required */
6225 0, /* properties_provided */
6226 0, /* properties_destroyed */
6227 0, /* todo_flags_start */
6228 TODO_df_finish, /* todo_flags_finish */
6229 };
6230
6231 class pass_if_after_combine : public rtl_opt_pass
6232 {
6233 public:
6234 pass_if_after_combine (gcc::context *ctxt)
6235 : rtl_opt_pass (pass_data_if_after_combine, ctxt)
6236 {}
6237
6238 /* opt_pass methods: */
6239 bool gate (function *) final override
6240 {
6241 return optimize > 0 && flag_if_conversion
6242 && dbg_cnt (if_after_combine);
6243 }
6244
6245 unsigned int execute (function *) final override
6246 {
6247 if_convert (true);
6248 return 0;
6249 }
6250
6251 }; // class pass_if_after_combine
6252
6253 } // anon namespace
6254
6255 rtl_opt_pass *
6256 make_pass_if_after_combine (gcc::context *ctxt)
6257 {
6258 return new pass_if_after_combine (ctxt);
6259 }
6260
6261
6262 namespace {
6263
6264 const pass_data pass_data_if_after_reload =
6265 {
6266 RTL_PASS, /* type */
6267 "ce3", /* name */
6268 OPTGROUP_NONE, /* optinfo_flags */
6269 TV_IFCVT2, /* tv_id */
6270 0, /* properties_required */
6271 0, /* properties_provided */
6272 0, /* properties_destroyed */
6273 0, /* todo_flags_start */
6274 TODO_df_finish, /* todo_flags_finish */
6275 };
6276
6277 class pass_if_after_reload : public rtl_opt_pass
6278 {
6279 public:
6280 pass_if_after_reload (gcc::context *ctxt)
6281 : rtl_opt_pass (pass_data_if_after_reload, ctxt)
6282 {}
6283
6284 /* opt_pass methods: */
6285 bool gate (function *) final override
6286 {
6287 return optimize > 0 && flag_if_conversion2
6288 && dbg_cnt (if_after_reload);
6289 }
6290
6291 unsigned int execute (function *) final override
6292 {
6293 if_convert (true);
6294 return 0;
6295 }
6296
6297 }; // class pass_if_after_reload
6298
6299 } // anon namespace
6300
6301 rtl_opt_pass *
6302 make_pass_if_after_reload (gcc::context *ctxt)
6303 {
6304 return new pass_if_after_reload (ctxt);
6305 }