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