OSDN Git Service

2005-04-19 Roman Kennke <roman@kennke.org>
[pf3gnuchains/gcc-fork.git] / gcc / ifcvt.c
1 /* If-conversion support.
2    Copyright (C) 2000, 2001, 2002, 2003, 2004, 2005
3    Free Software Foundation, Inc.
4
5    This file is part of GCC.
6
7    GCC is free software; you can redistribute it and/or modify it
8    under the terms of the GNU General Public License as published by
9    the Free Software Foundation; either version 2, or (at your option)
10    any later version.
11
12    GCC is distributed in the hope that it will be useful, but WITHOUT
13    ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
14    or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public
15    License for more details.
16
17    You should have received a copy of the GNU General Public License
18    along with GCC; see the file COPYING.  If not, write to the Free
19    Software Foundation, 59 Temple Place - Suite 330, Boston, MA
20    02111-1307, USA.  */
21
22 #include "config.h"
23 #include "system.h"
24 #include "coretypes.h"
25 #include "tm.h"
26
27 #include "rtl.h"
28 #include "regs.h"
29 #include "function.h"
30 #include "flags.h"
31 #include "insn-config.h"
32 #include "recog.h"
33 #include "except.h"
34 #include "hard-reg-set.h"
35 #include "basic-block.h"
36 #include "expr.h"
37 #include "real.h"
38 #include "output.h"
39 #include "optabs.h"
40 #include "toplev.h"
41 #include "tm_p.h"
42 #include "cfgloop.h"
43 #include "target.h"
44
45
46 #ifndef HAVE_conditional_execution
47 #define HAVE_conditional_execution 0
48 #endif
49 #ifndef HAVE_conditional_move
50 #define HAVE_conditional_move 0
51 #endif
52 #ifndef HAVE_incscc
53 #define HAVE_incscc 0
54 #endif
55 #ifndef HAVE_decscc
56 #define HAVE_decscc 0
57 #endif
58 #ifndef HAVE_trap
59 #define HAVE_trap 0
60 #endif
61 #ifndef HAVE_conditional_trap
62 #define HAVE_conditional_trap 0
63 #endif
64
65 #ifndef MAX_CONDITIONAL_EXECUTE
66 #define MAX_CONDITIONAL_EXECUTE   (BRANCH_COST + 1)
67 #endif
68
69 #define NULL_BLOCK      ((basic_block) NULL)
70
71 /* # of IF-THEN or IF-THEN-ELSE blocks we looked at  */
72 static int num_possible_if_blocks;
73
74 /* # of IF-THEN or IF-THEN-ELSE blocks were converted to conditional
75    execution.  */
76 static int num_updated_if_blocks;
77
78 /* # of changes made which require life information to be updated.  */
79 static int num_true_changes;
80
81 /* Whether conditional execution changes were made.  */
82 static int cond_exec_changed_p;
83
84 /* True if life data ok at present.  */
85 static bool life_data_ok;
86
87 /* Forward references.  */
88 static int count_bb_insns (basic_block);
89 static bool cheap_bb_rtx_cost_p (basic_block, int);
90 static rtx first_active_insn (basic_block);
91 static rtx last_active_insn (basic_block, int);
92 static basic_block block_fallthru (basic_block);
93 static int cond_exec_process_insns (ce_if_block_t *, rtx, rtx, rtx, rtx, int);
94 static rtx cond_exec_get_condition (rtx);
95 static int cond_exec_process_if_block (ce_if_block_t *, int);
96 static rtx noce_get_condition (rtx, rtx *);
97 static int noce_operand_ok (rtx);
98 static int noce_process_if_block (ce_if_block_t *);
99 static int process_if_block (ce_if_block_t *);
100 static void merge_if_block (ce_if_block_t *);
101 static int find_cond_trap (basic_block, edge, edge);
102 static basic_block find_if_header (basic_block, int);
103 static int block_jumps_and_fallthru_p (basic_block, basic_block);
104 static int find_if_block (ce_if_block_t *);
105 static int find_if_case_1 (basic_block, edge, edge);
106 static int find_if_case_2 (basic_block, edge, edge);
107 static int find_memory (rtx *, void *);
108 static int dead_or_predicable (basic_block, basic_block, basic_block,
109                                basic_block, int);
110 static void noce_emit_move_insn (rtx, rtx);
111 static rtx block_has_only_trap (basic_block);
112 \f
113 /* Count the number of non-jump active insns in BB.  */
114
115 static int
116 count_bb_insns (basic_block bb)
117 {
118   int count = 0;
119   rtx insn = BB_HEAD (bb);
120
121   while (1)
122     {
123       if (CALL_P (insn) || NONJUMP_INSN_P (insn))
124         count++;
125
126       if (insn == BB_END (bb))
127         break;
128       insn = NEXT_INSN (insn);
129     }
130
131   return count;
132 }
133
134 /* Determine whether the total insn_rtx_cost on non-jump insns in
135    basic block BB is less than MAX_COST.  This function returns
136    false if the cost of any instruction could not be estimated.  */
137
138 static bool
139 cheap_bb_rtx_cost_p (basic_block bb, int max_cost)
140 {
141   int count = 0;
142   rtx insn = BB_HEAD (bb);
143
144   while (1)
145     {
146       if (NONJUMP_INSN_P (insn))
147         {
148           int cost = insn_rtx_cost (PATTERN (insn));
149           if (cost == 0)
150             return false;
151
152           /* If this instruction is the load or set of a "stack" register,
153              such as a floating point register on x87, then the cost of
154              speculatively executing this instruction needs to include
155              the additional cost of popping this register off of the
156              register stack.  */
157 #ifdef STACK_REGS
158           {
159             rtx set = single_set (insn);
160             if (set && STACK_REG_P (SET_DEST (set)))
161               cost += COSTS_N_INSNS (1);
162           }
163 #endif
164
165           count += cost;
166           if (count >= max_cost)
167             return false;
168         }
169       else if (CALL_P (insn))
170         return false;
171  
172       if (insn == BB_END (bb))
173         break;
174       insn = NEXT_INSN (insn);
175     }
176
177   return true;
178 }
179
180 /* Return the first non-jump active insn in the basic block.  */
181
182 static rtx
183 first_active_insn (basic_block bb)
184 {
185   rtx insn = BB_HEAD (bb);
186
187   if (LABEL_P (insn))
188     {
189       if (insn == BB_END (bb))
190         return NULL_RTX;
191       insn = NEXT_INSN (insn);
192     }
193
194   while (NOTE_P (insn))
195     {
196       if (insn == BB_END (bb))
197         return NULL_RTX;
198       insn = NEXT_INSN (insn);
199     }
200
201   if (JUMP_P (insn))
202     return NULL_RTX;
203
204   return insn;
205 }
206
207 /* Return the last non-jump active (non-jump) insn in the basic block.  */
208
209 static rtx
210 last_active_insn (basic_block bb, int skip_use_p)
211 {
212   rtx insn = BB_END (bb);
213   rtx head = BB_HEAD (bb);
214
215   while (NOTE_P (insn)
216          || JUMP_P (insn)
217          || (skip_use_p
218              && NONJUMP_INSN_P (insn)
219              && GET_CODE (PATTERN (insn)) == USE))
220     {
221       if (insn == head)
222         return NULL_RTX;
223       insn = PREV_INSN (insn);
224     }
225
226   if (LABEL_P (insn))
227     return NULL_RTX;
228
229   return insn;
230 }
231
232 /* Return the basic block reached by falling though the basic block BB.  */
233
234 static basic_block
235 block_fallthru (basic_block bb)
236 {
237   edge e;
238   edge_iterator ei;
239
240   FOR_EACH_EDGE (e, ei, bb->succs)
241     if (e->flags & EDGE_FALLTHRU)
242       break;
243
244   return (e) ? e->dest : NULL_BLOCK;
245 }
246 \f
247 /* Go through a bunch of insns, converting them to conditional
248    execution format if possible.  Return TRUE if all of the non-note
249    insns were processed.  */
250
251 static int
252 cond_exec_process_insns (ce_if_block_t *ce_info ATTRIBUTE_UNUSED,
253                          /* if block information */rtx start,
254                          /* first insn to look at */rtx end,
255                          /* last insn to look at */rtx test,
256                          /* conditional execution test */rtx prob_val,
257                          /* probability of branch taken. */int mod_ok)
258 {
259   int must_be_last = FALSE;
260   rtx insn;
261   rtx xtest;
262   rtx pattern;
263
264   if (!start || !end)
265     return FALSE;
266
267   for (insn = start; ; insn = NEXT_INSN (insn))
268     {
269       if (NOTE_P (insn))
270         goto insn_done;
271
272       if (!NONJUMP_INSN_P (insn) && !CALL_P (insn))
273         abort ();
274
275       /* Remove USE insns that get in the way.  */
276       if (reload_completed && GET_CODE (PATTERN (insn)) == USE)
277         {
278           /* ??? Ug.  Actually unlinking the thing is problematic,
279              given what we'd have to coordinate with our callers.  */
280           SET_INSN_DELETED (insn);
281           goto insn_done;
282         }
283
284       /* Last insn wasn't last?  */
285       if (must_be_last)
286         return FALSE;
287
288       if (modified_in_p (test, insn))
289         {
290           if (!mod_ok)
291             return FALSE;
292           must_be_last = TRUE;
293         }
294
295       /* Now build the conditional form of the instruction.  */
296       pattern = PATTERN (insn);
297       xtest = copy_rtx (test);
298
299       /* If this is already a COND_EXEC, rewrite the test to be an AND of the
300          two conditions.  */
301       if (GET_CODE (pattern) == COND_EXEC)
302         {
303           if (GET_MODE (xtest) != GET_MODE (COND_EXEC_TEST (pattern)))
304             return FALSE;
305
306           xtest = gen_rtx_AND (GET_MODE (xtest), xtest,
307                                COND_EXEC_TEST (pattern));
308           pattern = COND_EXEC_CODE (pattern);
309         }
310
311       pattern = gen_rtx_COND_EXEC (VOIDmode, xtest, pattern);
312
313       /* If the machine needs to modify the insn being conditionally executed,
314          say for example to force a constant integer operand into a temp
315          register, do so here.  */
316 #ifdef IFCVT_MODIFY_INSN
317       IFCVT_MODIFY_INSN (ce_info, pattern, insn);
318       if (! pattern)
319         return FALSE;
320 #endif
321
322       validate_change (insn, &PATTERN (insn), pattern, 1);
323
324       if (CALL_P (insn) && prob_val)
325         validate_change (insn, &REG_NOTES (insn),
326                          alloc_EXPR_LIST (REG_BR_PROB, prob_val,
327                                           REG_NOTES (insn)), 1);
328
329     insn_done:
330       if (insn == end)
331         break;
332     }
333
334   return TRUE;
335 }
336
337 /* Return the condition for a jump.  Do not do any special processing.  */
338
339 static rtx
340 cond_exec_get_condition (rtx jump)
341 {
342   rtx test_if, cond;
343
344   if (any_condjump_p (jump))
345     test_if = SET_SRC (pc_set (jump));
346   else
347     return NULL_RTX;
348   cond = XEXP (test_if, 0);
349
350   /* If this branches to JUMP_LABEL when the condition is false,
351      reverse the condition.  */
352   if (GET_CODE (XEXP (test_if, 2)) == LABEL_REF
353       && XEXP (XEXP (test_if, 2), 0) == JUMP_LABEL (jump))
354     {
355       enum rtx_code rev = reversed_comparison_code (cond, jump);
356       if (rev == UNKNOWN)
357         return NULL_RTX;
358
359       cond = gen_rtx_fmt_ee (rev, GET_MODE (cond), XEXP (cond, 0),
360                              XEXP (cond, 1));
361     }
362
363   return cond;
364 }
365
366 /* Given a simple IF-THEN or IF-THEN-ELSE block, attempt to convert it
367    to conditional execution.  Return TRUE if we were successful at
368    converting the block.  */
369
370 static int
371 cond_exec_process_if_block (ce_if_block_t * ce_info,
372                             /* if block information */int do_multiple_p)
373 {
374   basic_block test_bb = ce_info->test_bb;       /* last test block */
375   basic_block then_bb = ce_info->then_bb;       /* THEN */
376   basic_block else_bb = ce_info->else_bb;       /* ELSE or NULL */
377   rtx test_expr;                /* expression in IF_THEN_ELSE that is tested */
378   rtx then_start;               /* first insn in THEN block */
379   rtx then_end;                 /* last insn + 1 in THEN block */
380   rtx else_start = NULL_RTX;    /* first insn in ELSE block or NULL */
381   rtx else_end = NULL_RTX;      /* last insn + 1 in ELSE block */
382   int max;                      /* max # of insns to convert.  */
383   int then_mod_ok;              /* whether conditional mods are ok in THEN */
384   rtx true_expr;                /* test for else block insns */
385   rtx false_expr;               /* test for then block insns */
386   rtx true_prob_val;            /* probability of else block */
387   rtx false_prob_val;           /* probability of then block */
388   int n_insns;
389   enum rtx_code false_code;
390
391   /* If test is comprised of && or || elements, and we've failed at handling
392      all of them together, just use the last test if it is the special case of
393      && elements without an ELSE block.  */
394   if (!do_multiple_p && ce_info->num_multiple_test_blocks)
395     {
396       if (else_bb || ! ce_info->and_and_p)
397         return FALSE;
398
399       ce_info->test_bb = test_bb = ce_info->last_test_bb;
400       ce_info->num_multiple_test_blocks = 0;
401       ce_info->num_and_and_blocks = 0;
402       ce_info->num_or_or_blocks = 0;
403     }
404
405   /* Find the conditional jump to the ELSE or JOIN part, and isolate
406      the test.  */
407   test_expr = cond_exec_get_condition (BB_END (test_bb));
408   if (! test_expr)
409     return FALSE;
410
411   /* If the conditional jump is more than just a conditional jump,
412      then we can not do conditional execution conversion on this block.  */
413   if (! onlyjump_p (BB_END (test_bb)))
414     return FALSE;
415
416   /* Collect the bounds of where we're to search, skipping any labels, jumps
417      and notes at the beginning and end of the block.  Then count the total
418      number of insns and see if it is small enough to convert.  */
419   then_start = first_active_insn (then_bb);
420   then_end = last_active_insn (then_bb, TRUE);
421   n_insns = ce_info->num_then_insns = count_bb_insns (then_bb);
422   max = MAX_CONDITIONAL_EXECUTE;
423
424   if (else_bb)
425     {
426       max *= 2;
427       else_start = first_active_insn (else_bb);
428       else_end = last_active_insn (else_bb, TRUE);
429       n_insns += ce_info->num_else_insns = count_bb_insns (else_bb);
430     }
431
432   if (n_insns > max)
433     return FALSE;
434
435   /* Map test_expr/test_jump into the appropriate MD tests to use on
436      the conditionally executed code.  */
437
438   true_expr = test_expr;
439
440   false_code = reversed_comparison_code (true_expr, BB_END (test_bb));
441   if (false_code != UNKNOWN)
442     false_expr = gen_rtx_fmt_ee (false_code, GET_MODE (true_expr),
443                                  XEXP (true_expr, 0), XEXP (true_expr, 1));
444   else
445     false_expr = NULL_RTX;
446
447 #ifdef IFCVT_MODIFY_TESTS
448   /* If the machine description needs to modify the tests, such as setting a
449      conditional execution register from a comparison, it can do so here.  */
450   IFCVT_MODIFY_TESTS (ce_info, true_expr, false_expr);
451
452   /* See if the conversion failed.  */
453   if (!true_expr || !false_expr)
454     goto fail;
455 #endif
456
457   true_prob_val = find_reg_note (BB_END (test_bb), REG_BR_PROB, NULL_RTX);
458   if (true_prob_val)
459     {
460       true_prob_val = XEXP (true_prob_val, 0);
461       false_prob_val = GEN_INT (REG_BR_PROB_BASE - INTVAL (true_prob_val));
462     }
463   else
464     false_prob_val = NULL_RTX;
465
466   /* If we have && or || tests, do them here.  These tests are in the adjacent
467      blocks after the first block containing the test.  */
468   if (ce_info->num_multiple_test_blocks > 0)
469     {
470       basic_block bb = test_bb;
471       basic_block last_test_bb = ce_info->last_test_bb;
472
473       if (! false_expr)
474         goto fail;
475
476       do
477         {
478           rtx start, end;
479           rtx t, f;
480           enum rtx_code f_code;
481
482           bb = block_fallthru (bb);
483           start = first_active_insn (bb);
484           end = last_active_insn (bb, TRUE);
485           if (start
486               && ! cond_exec_process_insns (ce_info, start, end, false_expr,
487                                             false_prob_val, FALSE))
488             goto fail;
489
490           /* If the conditional jump is more than just a conditional jump, then
491              we can not do conditional execution conversion on this block.  */
492           if (! onlyjump_p (BB_END (bb)))
493             goto fail;
494
495           /* Find the conditional jump and isolate the test.  */
496           t = cond_exec_get_condition (BB_END (bb));
497           if (! t)
498             goto fail;
499
500           f_code = reversed_comparison_code (t, BB_END (bb));
501           if (f_code == UNKNOWN)
502             goto fail;
503
504           f = gen_rtx_fmt_ee (f_code, GET_MODE (t), XEXP (t, 0), XEXP (t, 1));
505           if (ce_info->and_and_p)
506             {
507               t = gen_rtx_AND (GET_MODE (t), true_expr, t);
508               f = gen_rtx_IOR (GET_MODE (t), false_expr, f);
509             }
510           else
511             {
512               t = gen_rtx_IOR (GET_MODE (t), true_expr, t);
513               f = gen_rtx_AND (GET_MODE (t), false_expr, f);
514             }
515
516           /* If the machine description needs to modify the tests, such as
517              setting a conditional execution register from a comparison, it can
518              do so here.  */
519 #ifdef IFCVT_MODIFY_MULTIPLE_TESTS
520           IFCVT_MODIFY_MULTIPLE_TESTS (ce_info, bb, t, f);
521
522           /* See if the conversion failed.  */
523           if (!t || !f)
524             goto fail;
525 #endif
526
527           true_expr = t;
528           false_expr = f;
529         }
530       while (bb != last_test_bb);
531     }
532
533   /* For IF-THEN-ELSE blocks, we don't allow modifications of the test
534      on then THEN block.  */
535   then_mod_ok = (else_bb == NULL_BLOCK);
536
537   /* Go through the THEN and ELSE blocks converting the insns if possible
538      to conditional execution.  */
539
540   if (then_end
541       && (! false_expr
542           || ! cond_exec_process_insns (ce_info, then_start, then_end,
543                                         false_expr, false_prob_val,
544                                         then_mod_ok)))
545     goto fail;
546
547   if (else_bb && else_end
548       && ! cond_exec_process_insns (ce_info, else_start, else_end,
549                                     true_expr, true_prob_val, TRUE))
550     goto fail;
551
552   /* If we cannot apply the changes, fail.  Do not go through the normal fail
553      processing, since apply_change_group will call cancel_changes.  */
554   if (! apply_change_group ())
555     {
556 #ifdef IFCVT_MODIFY_CANCEL
557       /* Cancel any machine dependent changes.  */
558       IFCVT_MODIFY_CANCEL (ce_info);
559 #endif
560       return FALSE;
561     }
562
563 #ifdef IFCVT_MODIFY_FINAL
564   /* Do any machine dependent final modifications.  */
565   IFCVT_MODIFY_FINAL (ce_info);
566 #endif
567
568   /* Conversion succeeded.  */
569   if (dump_file)
570     fprintf (dump_file, "%d insn%s converted to conditional execution.\n",
571              n_insns, (n_insns == 1) ? " was" : "s were");
572
573   /* Merge the blocks!  */
574   merge_if_block (ce_info);
575   cond_exec_changed_p = TRUE;
576   return TRUE;
577
578  fail:
579 #ifdef IFCVT_MODIFY_CANCEL
580   /* Cancel any machine dependent changes.  */
581   IFCVT_MODIFY_CANCEL (ce_info);
582 #endif
583
584   cancel_changes (0);
585   return FALSE;
586 }
587 \f
588 /* Used by noce_process_if_block to communicate with its subroutines.
589
590    The subroutines know that A and B may be evaluated freely.  They
591    know that X is a register.  They should insert new instructions
592    before cond_earliest.  */
593
594 struct noce_if_info
595 {
596   basic_block test_bb;
597   rtx insn_a, insn_b;
598   rtx x, a, b;
599   rtx jump, cond, cond_earliest;
600   /* True if "b" was originally evaluated unconditionally.  */
601   bool b_unconditional;
602 };
603
604 static rtx noce_emit_store_flag (struct noce_if_info *, rtx, int, int);
605 static int noce_try_move (struct noce_if_info *);
606 static int noce_try_store_flag (struct noce_if_info *);
607 static int noce_try_addcc (struct noce_if_info *);
608 static int noce_try_store_flag_constants (struct noce_if_info *);
609 static int noce_try_store_flag_mask (struct noce_if_info *);
610 static rtx noce_emit_cmove (struct noce_if_info *, rtx, enum rtx_code, rtx,
611                             rtx, rtx, rtx);
612 static int noce_try_cmove (struct noce_if_info *);
613 static int noce_try_cmove_arith (struct noce_if_info *);
614 static rtx noce_get_alt_condition (struct noce_if_info *, rtx, rtx *);
615 static int noce_try_minmax (struct noce_if_info *);
616 static int noce_try_abs (struct noce_if_info *);
617 static int noce_try_sign_mask (struct noce_if_info *);
618
619 /* Helper function for noce_try_store_flag*.  */
620
621 static rtx
622 noce_emit_store_flag (struct noce_if_info *if_info, rtx x, int reversep,
623                       int normalize)
624 {
625   rtx cond = if_info->cond;
626   int cond_complex;
627   enum rtx_code code;
628
629   cond_complex = (! general_operand (XEXP (cond, 0), VOIDmode)
630                   || ! general_operand (XEXP (cond, 1), VOIDmode));
631
632   /* If earliest == jump, or when the condition is complex, try to
633      build the store_flag insn directly.  */
634
635   if (cond_complex)
636     cond = XEXP (SET_SRC (pc_set (if_info->jump)), 0);
637
638   if (reversep)
639     code = reversed_comparison_code (cond, if_info->jump);
640   else
641     code = GET_CODE (cond);
642
643   if ((if_info->cond_earliest == if_info->jump || cond_complex)
644       && (normalize == 0 || STORE_FLAG_VALUE == normalize))
645     {
646       rtx tmp;
647
648       tmp = gen_rtx_fmt_ee (code, GET_MODE (x), XEXP (cond, 0),
649                             XEXP (cond, 1));
650       tmp = gen_rtx_SET (VOIDmode, x, tmp);
651
652       start_sequence ();
653       tmp = emit_insn (tmp);
654
655       if (recog_memoized (tmp) >= 0)
656         {
657           tmp = get_insns ();
658           end_sequence ();
659           emit_insn (tmp);
660
661           if_info->cond_earliest = if_info->jump;
662
663           return x;
664         }
665
666       end_sequence ();
667     }
668
669   /* Don't even try if the comparison operands or the mode of X are weird.  */
670   if (cond_complex || !SCALAR_INT_MODE_P (GET_MODE (x)))
671     return NULL_RTX;
672
673   return emit_store_flag (x, code, XEXP (cond, 0),
674                           XEXP (cond, 1), VOIDmode,
675                           (code == LTU || code == LEU
676                            || code == GEU || code == GTU), normalize);
677 }
678
679 /* Emit instruction to move an rtx, possibly into STRICT_LOW_PART.
680    X is the destination/target and Y is the value to copy.  */
681
682 static void
683 noce_emit_move_insn (rtx x, rtx y)
684 {
685   enum machine_mode outmode;
686   rtx outer, inner;
687   int bitpos;
688
689   if (GET_CODE (x) != STRICT_LOW_PART)
690     {
691       emit_move_insn (x, y);
692       return;
693     }
694
695   outer = XEXP (x, 0);
696   inner = XEXP (outer, 0);
697   outmode = GET_MODE (outer);
698   bitpos = SUBREG_BYTE (outer) * BITS_PER_UNIT;
699   store_bit_field (inner, GET_MODE_BITSIZE (outmode), bitpos, outmode, y);
700 }
701
702 /* Return sequence of instructions generated by if conversion.  This
703    function calls end_sequence() to end the current stream, ensures
704    that are instructions are unshared, recognizable non-jump insns.
705    On failure, this function returns a NULL_RTX.  */
706
707 static rtx
708 end_ifcvt_sequence (struct noce_if_info *if_info)
709 {
710   rtx insn;
711   rtx seq = get_insns ();
712
713   set_used_flags (if_info->x);
714   set_used_flags (if_info->cond);
715   unshare_all_rtl_in_chain (seq);
716   end_sequence ();
717
718   /* Make sure that all of the instructions emitted are recognizable,
719      and that we haven't introduced a new jump instruction.
720      As an exercise for the reader, build a general mechanism that
721      allows proper placement of required clobbers.  */
722   for (insn = seq; insn; insn = NEXT_INSN (insn))
723     if (JUMP_P (insn)
724         || recog_memoized (insn) == -1)
725       return NULL_RTX;
726
727   return seq;
728 }
729
730 /* Convert "if (a != b) x = a; else x = b" into "x = a" and
731    "if (a == b) x = a; else x = b" into "x = b".  */
732
733 static int
734 noce_try_move (struct noce_if_info *if_info)
735 {
736   rtx cond = if_info->cond;
737   enum rtx_code code = GET_CODE (cond);
738   rtx y, seq;
739
740   if (code != NE && code != EQ)
741     return FALSE;
742
743   /* This optimization isn't valid if either A or B could be a NaN
744      or a signed zero.  */
745   if (HONOR_NANS (GET_MODE (if_info->x))
746       || HONOR_SIGNED_ZEROS (GET_MODE (if_info->x)))
747     return FALSE;
748
749   /* Check whether the operands of the comparison are A and in
750      either order.  */
751   if ((rtx_equal_p (if_info->a, XEXP (cond, 0))
752        && rtx_equal_p (if_info->b, XEXP (cond, 1)))
753       || (rtx_equal_p (if_info->a, XEXP (cond, 1))
754           && rtx_equal_p (if_info->b, XEXP (cond, 0))))
755     {
756       y = (code == EQ) ? if_info->a : if_info->b;
757
758       /* Avoid generating the move if the source is the destination.  */
759       if (! rtx_equal_p (if_info->x, y))
760         {
761           start_sequence ();
762           noce_emit_move_insn (if_info->x, y);
763           seq = end_ifcvt_sequence (if_info);
764           if (!seq)
765             return FALSE;
766
767           emit_insn_before_setloc (seq, if_info->jump,
768                                    INSN_LOCATOR (if_info->insn_a));
769         }
770       return TRUE;
771     }
772   return FALSE;
773 }
774
775 /* Convert "if (test) x = 1; else x = 0".
776
777    Only try 0 and STORE_FLAG_VALUE here.  Other combinations will be
778    tried in noce_try_store_flag_constants after noce_try_cmove has had
779    a go at the conversion.  */
780
781 static int
782 noce_try_store_flag (struct noce_if_info *if_info)
783 {
784   int reversep;
785   rtx target, seq;
786
787   if (GET_CODE (if_info->b) == CONST_INT
788       && INTVAL (if_info->b) == STORE_FLAG_VALUE
789       && if_info->a == const0_rtx)
790     reversep = 0;
791   else if (if_info->b == const0_rtx
792            && GET_CODE (if_info->a) == CONST_INT
793            && INTVAL (if_info->a) == STORE_FLAG_VALUE
794            && (reversed_comparison_code (if_info->cond, if_info->jump)
795                != UNKNOWN))
796     reversep = 1;
797   else
798     return FALSE;
799
800   start_sequence ();
801
802   target = noce_emit_store_flag (if_info, if_info->x, reversep, 0);
803   if (target)
804     {
805       if (target != if_info->x)
806         noce_emit_move_insn (if_info->x, target);
807
808       seq = end_ifcvt_sequence (if_info);
809       if (! seq)
810         return FALSE;
811
812       emit_insn_before_setloc (seq, if_info->jump,
813                                INSN_LOCATOR (if_info->insn_a));
814       return TRUE;
815     }
816   else
817     {
818       end_sequence ();
819       return FALSE;
820     }
821 }
822
823 /* Convert "if (test) x = a; else x = b", for A and B constant.  */
824
825 static int
826 noce_try_store_flag_constants (struct noce_if_info *if_info)
827 {
828   rtx target, seq;
829   int reversep;
830   HOST_WIDE_INT itrue, ifalse, diff, tmp;
831   int normalize, can_reverse;
832   enum machine_mode mode;
833
834   if (! no_new_pseudos
835       && GET_CODE (if_info->a) == CONST_INT
836       && GET_CODE (if_info->b) == CONST_INT)
837     {
838       mode = GET_MODE (if_info->x);
839       ifalse = INTVAL (if_info->a);
840       itrue = INTVAL (if_info->b);
841
842       /* Make sure we can represent the difference between the two values.  */
843       if ((itrue - ifalse > 0)
844           != ((ifalse < 0) != (itrue < 0) ? ifalse < 0 : ifalse < itrue))
845         return FALSE;
846
847       diff = trunc_int_for_mode (itrue - ifalse, mode);
848
849       can_reverse = (reversed_comparison_code (if_info->cond, if_info->jump)
850                      != UNKNOWN);
851
852       reversep = 0;
853       if (diff == STORE_FLAG_VALUE || diff == -STORE_FLAG_VALUE)
854         normalize = 0;
855       else if (ifalse == 0 && exact_log2 (itrue) >= 0
856                && (STORE_FLAG_VALUE == 1
857                    || BRANCH_COST >= 2))
858         normalize = 1;
859       else if (itrue == 0 && exact_log2 (ifalse) >= 0 && can_reverse
860                && (STORE_FLAG_VALUE == 1 || BRANCH_COST >= 2))
861         normalize = 1, reversep = 1;
862       else if (itrue == -1
863                && (STORE_FLAG_VALUE == -1
864                    || BRANCH_COST >= 2))
865         normalize = -1;
866       else if (ifalse == -1 && can_reverse
867                && (STORE_FLAG_VALUE == -1 || BRANCH_COST >= 2))
868         normalize = -1, reversep = 1;
869       else if ((BRANCH_COST >= 2 && STORE_FLAG_VALUE == -1)
870                || BRANCH_COST >= 3)
871         normalize = -1;
872       else
873         return FALSE;
874
875       if (reversep)
876         {
877           tmp = itrue; itrue = ifalse; ifalse = tmp;
878           diff = trunc_int_for_mode (-diff, mode);
879         }
880
881       start_sequence ();
882       target = noce_emit_store_flag (if_info, if_info->x, reversep, normalize);
883       if (! target)
884         {
885           end_sequence ();
886           return FALSE;
887         }
888
889       /* if (test) x = 3; else x = 4;
890          =>   x = 3 + (test == 0);  */
891       if (diff == STORE_FLAG_VALUE || diff == -STORE_FLAG_VALUE)
892         {
893           target = expand_simple_binop (mode,
894                                         (diff == STORE_FLAG_VALUE
895                                          ? PLUS : MINUS),
896                                         GEN_INT (ifalse), target, if_info->x, 0,
897                                         OPTAB_WIDEN);
898         }
899
900       /* if (test) x = 8; else x = 0;
901          =>   x = (test != 0) << 3;  */
902       else if (ifalse == 0 && (tmp = exact_log2 (itrue)) >= 0)
903         {
904           target = expand_simple_binop (mode, ASHIFT,
905                                         target, GEN_INT (tmp), if_info->x, 0,
906                                         OPTAB_WIDEN);
907         }
908
909       /* if (test) x = -1; else x = b;
910          =>   x = -(test != 0) | b;  */
911       else if (itrue == -1)
912         {
913           target = expand_simple_binop (mode, IOR,
914                                         target, GEN_INT (ifalse), if_info->x, 0,
915                                         OPTAB_WIDEN);
916         }
917
918       /* if (test) x = a; else x = b;
919          =>   x = (-(test != 0) & (b - a)) + a;  */
920       else
921         {
922           target = expand_simple_binop (mode, AND,
923                                         target, GEN_INT (diff), if_info->x, 0,
924                                         OPTAB_WIDEN);
925           if (target)
926             target = expand_simple_binop (mode, PLUS,
927                                           target, GEN_INT (ifalse),
928                                           if_info->x, 0, OPTAB_WIDEN);
929         }
930
931       if (! target)
932         {
933           end_sequence ();
934           return FALSE;
935         }
936
937       if (target != if_info->x)
938         noce_emit_move_insn (if_info->x, target);
939
940       seq = end_ifcvt_sequence (if_info);
941       if (!seq)
942         return FALSE;
943
944       emit_insn_before_setloc (seq, if_info->jump,
945                                INSN_LOCATOR (if_info->insn_a));
946       return TRUE;
947     }
948
949   return FALSE;
950 }
951
952 /* Convert "if (test) foo++" into "foo += (test != 0)", and
953    similarly for "foo--".  */
954
955 static int
956 noce_try_addcc (struct noce_if_info *if_info)
957 {
958   rtx target, seq;
959   int subtract, normalize;
960
961   if (! no_new_pseudos
962       && GET_CODE (if_info->a) == PLUS
963       && rtx_equal_p (XEXP (if_info->a, 0), if_info->b)
964       && (reversed_comparison_code (if_info->cond, if_info->jump)
965           != UNKNOWN))
966     {
967       rtx cond = if_info->cond;
968       enum rtx_code code = reversed_comparison_code (cond, if_info->jump);
969
970       /* First try to use addcc pattern.  */
971       if (general_operand (XEXP (cond, 0), VOIDmode)
972           && general_operand (XEXP (cond, 1), VOIDmode))
973         {
974           start_sequence ();
975           target = emit_conditional_add (if_info->x, code,
976                                          XEXP (cond, 0),
977                                          XEXP (cond, 1),
978                                          VOIDmode,
979                                          if_info->b,
980                                          XEXP (if_info->a, 1),
981                                          GET_MODE (if_info->x),
982                                          (code == LTU || code == GEU
983                                           || code == LEU || code == GTU));
984           if (target)
985             {
986               if (target != if_info->x)
987                 noce_emit_move_insn (if_info->x, target);
988
989               seq = end_ifcvt_sequence (if_info);
990               if (!seq)
991                 return FALSE;
992
993               emit_insn_before_setloc (seq, if_info->jump,
994                                        INSN_LOCATOR (if_info->insn_a));
995               return TRUE;
996             }
997           end_sequence ();
998         }
999
1000       /* If that fails, construct conditional increment or decrement using
1001          setcc.  */
1002       if (BRANCH_COST >= 2
1003           && (XEXP (if_info->a, 1) == const1_rtx
1004               || XEXP (if_info->a, 1) == constm1_rtx))
1005         {
1006           start_sequence ();
1007           if (STORE_FLAG_VALUE == INTVAL (XEXP (if_info->a, 1)))
1008             subtract = 0, normalize = 0;
1009           else if (-STORE_FLAG_VALUE == INTVAL (XEXP (if_info->a, 1)))
1010             subtract = 1, normalize = 0;
1011           else
1012             subtract = 0, normalize = INTVAL (XEXP (if_info->a, 1));
1013
1014
1015           target = noce_emit_store_flag (if_info,
1016                                          gen_reg_rtx (GET_MODE (if_info->x)),
1017                                          1, normalize);
1018
1019           if (target)
1020             target = expand_simple_binop (GET_MODE (if_info->x),
1021                                           subtract ? MINUS : PLUS,
1022                                           if_info->b, target, if_info->x,
1023                                           0, OPTAB_WIDEN);
1024           if (target)
1025             {
1026               if (target != if_info->x)
1027                 noce_emit_move_insn (if_info->x, target);
1028
1029               seq = end_ifcvt_sequence (if_info);
1030               if (!seq)
1031                 return FALSE;
1032
1033               emit_insn_before_setloc (seq, if_info->jump,
1034                                        INSN_LOCATOR (if_info->insn_a));
1035               return TRUE;
1036             }
1037           end_sequence ();
1038         }
1039     }
1040
1041   return FALSE;
1042 }
1043
1044 /* Convert "if (test) x = 0;" to "x &= -(test == 0);"  */
1045
1046 static int
1047 noce_try_store_flag_mask (struct noce_if_info *if_info)
1048 {
1049   rtx target, seq;
1050   int reversep;
1051
1052   reversep = 0;
1053   if (! no_new_pseudos
1054       && (BRANCH_COST >= 2
1055           || STORE_FLAG_VALUE == -1)
1056       && ((if_info->a == const0_rtx
1057            && rtx_equal_p (if_info->b, if_info->x))
1058           || ((reversep = (reversed_comparison_code (if_info->cond,
1059                                                      if_info->jump)
1060                            != UNKNOWN))
1061               && if_info->b == const0_rtx
1062               && rtx_equal_p (if_info->a, if_info->x))))
1063     {
1064       start_sequence ();
1065       target = noce_emit_store_flag (if_info,
1066                                      gen_reg_rtx (GET_MODE (if_info->x)),
1067                                      reversep, -1);
1068       if (target)
1069         target = expand_simple_binop (GET_MODE (if_info->x), AND,
1070                                       if_info->x,
1071                                       target, if_info->x, 0,
1072                                       OPTAB_WIDEN);
1073
1074       if (target)
1075         {
1076           if (target != if_info->x)
1077             noce_emit_move_insn (if_info->x, target);
1078
1079           seq = end_ifcvt_sequence (if_info);
1080           if (!seq)
1081             return FALSE;
1082
1083           emit_insn_before_setloc (seq, if_info->jump,
1084                                    INSN_LOCATOR (if_info->insn_a));
1085           return TRUE;
1086         }
1087
1088       end_sequence ();
1089     }
1090
1091   return FALSE;
1092 }
1093
1094 /* Helper function for noce_try_cmove and noce_try_cmove_arith.  */
1095
1096 static rtx
1097 noce_emit_cmove (struct noce_if_info *if_info, rtx x, enum rtx_code code,
1098                  rtx cmp_a, rtx cmp_b, rtx vfalse, rtx vtrue)
1099 {
1100   /* If earliest == jump, try to build the cmove insn directly.
1101      This is helpful when combine has created some complex condition
1102      (like for alpha's cmovlbs) that we can't hope to regenerate
1103      through the normal interface.  */
1104
1105   if (if_info->cond_earliest == if_info->jump)
1106     {
1107       rtx tmp;
1108
1109       tmp = gen_rtx_fmt_ee (code, GET_MODE (if_info->cond), cmp_a, cmp_b);
1110       tmp = gen_rtx_IF_THEN_ELSE (GET_MODE (x), tmp, vtrue, vfalse);
1111       tmp = gen_rtx_SET (VOIDmode, x, tmp);
1112
1113       start_sequence ();
1114       tmp = emit_insn (tmp);
1115
1116       if (recog_memoized (tmp) >= 0)
1117         {
1118           tmp = get_insns ();
1119           end_sequence ();
1120           emit_insn (tmp);
1121
1122           return x;
1123         }
1124
1125       end_sequence ();
1126     }
1127
1128   /* Don't even try if the comparison operands are weird.  */
1129   if (! general_operand (cmp_a, GET_MODE (cmp_a))
1130       || ! general_operand (cmp_b, GET_MODE (cmp_b)))
1131     return NULL_RTX;
1132
1133 #if HAVE_conditional_move
1134   return emit_conditional_move (x, code, cmp_a, cmp_b, VOIDmode,
1135                                 vtrue, vfalse, GET_MODE (x),
1136                                 (code == LTU || code == GEU
1137                                  || code == LEU || code == GTU));
1138 #else
1139   /* We'll never get here, as noce_process_if_block doesn't call the
1140      functions involved.  Ifdef code, however, should be discouraged
1141      because it leads to typos in the code not selected.  However,
1142      emit_conditional_move won't exist either.  */
1143   return NULL_RTX;
1144 #endif
1145 }
1146
1147 /* Try only simple constants and registers here.  More complex cases
1148    are handled in noce_try_cmove_arith after noce_try_store_flag_arith
1149    has had a go at it.  */
1150
1151 static int
1152 noce_try_cmove (struct noce_if_info *if_info)
1153 {
1154   enum rtx_code code;
1155   rtx target, seq;
1156
1157   if ((CONSTANT_P (if_info->a) || register_operand (if_info->a, VOIDmode))
1158       && (CONSTANT_P (if_info->b) || register_operand (if_info->b, VOIDmode)))
1159     {
1160       start_sequence ();
1161
1162       code = GET_CODE (if_info->cond);
1163       target = noce_emit_cmove (if_info, if_info->x, code,
1164                                 XEXP (if_info->cond, 0),
1165                                 XEXP (if_info->cond, 1),
1166                                 if_info->a, if_info->b);
1167
1168       if (target)
1169         {
1170           if (target != if_info->x)
1171             noce_emit_move_insn (if_info->x, target);
1172
1173           seq = end_ifcvt_sequence (if_info);
1174           if (!seq)
1175             return FALSE;
1176
1177           emit_insn_before_setloc (seq, if_info->jump,
1178                                    INSN_LOCATOR (if_info->insn_a));
1179           return TRUE;
1180         }
1181       else
1182         {
1183           end_sequence ();
1184           return FALSE;
1185         }
1186     }
1187
1188   return FALSE;
1189 }
1190
1191 /* Try more complex cases involving conditional_move.  */
1192
1193 static int
1194 noce_try_cmove_arith (struct noce_if_info *if_info)
1195 {
1196   rtx a = if_info->a;
1197   rtx b = if_info->b;
1198   rtx x = if_info->x;
1199   rtx orig_a, orig_b;
1200   rtx insn_a, insn_b;
1201   rtx tmp, target;
1202   int is_mem = 0;
1203   int insn_cost;
1204   enum rtx_code code;
1205
1206   /* A conditional move from two memory sources is equivalent to a
1207      conditional on their addresses followed by a load.  Don't do this
1208      early because it'll screw alias analysis.  Note that we've
1209      already checked for no side effects.  */
1210   if (! no_new_pseudos && cse_not_expected
1211       && MEM_P (a) && MEM_P (b)
1212       && BRANCH_COST >= 5)
1213     {
1214       a = XEXP (a, 0);
1215       b = XEXP (b, 0);
1216       x = gen_reg_rtx (Pmode);
1217       is_mem = 1;
1218     }
1219
1220   /* ??? We could handle this if we knew that a load from A or B could
1221      not fault.  This is also true if we've already loaded
1222      from the address along the path from ENTRY.  */
1223   else if (may_trap_p (a) || may_trap_p (b))
1224     return FALSE;
1225
1226   /* if (test) x = a + b; else x = c - d;
1227      => y = a + b;
1228         x = c - d;
1229         if (test)
1230           x = y;
1231   */
1232
1233   code = GET_CODE (if_info->cond);
1234   insn_a = if_info->insn_a;
1235   insn_b = if_info->insn_b;
1236
1237   /* Total insn_rtx_cost should be smaller than branch cost.  Exit
1238      if insn_rtx_cost can't be estimated.  */
1239   if (insn_a)
1240     {
1241       insn_cost = insn_rtx_cost (PATTERN (insn_a));
1242       if (insn_cost == 0 || insn_cost > COSTS_N_INSNS (BRANCH_COST))
1243         return FALSE;
1244     }
1245   else
1246     {
1247       insn_cost = 0;
1248     }
1249
1250   if (insn_b) {
1251     insn_cost += insn_rtx_cost (PATTERN (insn_b));
1252     if (insn_cost == 0 || insn_cost > COSTS_N_INSNS (BRANCH_COST))
1253       return FALSE;
1254   }
1255
1256   /* Possibly rearrange operands to make things come out more natural.  */
1257   if (reversed_comparison_code (if_info->cond, if_info->jump) != UNKNOWN)
1258     {
1259       int reversep = 0;
1260       if (rtx_equal_p (b, x))
1261         reversep = 1;
1262       else if (general_operand (b, GET_MODE (b)))
1263         reversep = 1;
1264
1265       if (reversep)
1266         {
1267           code = reversed_comparison_code (if_info->cond, if_info->jump);
1268           tmp = a, a = b, b = tmp;
1269           tmp = insn_a, insn_a = insn_b, insn_b = tmp;
1270         }
1271     }
1272
1273   start_sequence ();
1274
1275   orig_a = a;
1276   orig_b = b;
1277
1278   /* If either operand is complex, load it into a register first.
1279      The best way to do this is to copy the original insn.  In this
1280      way we preserve any clobbers etc that the insn may have had.
1281      This is of course not possible in the IS_MEM case.  */
1282   if (! general_operand (a, GET_MODE (a)))
1283     {
1284       rtx set;
1285
1286       if (no_new_pseudos)
1287         goto end_seq_and_fail;
1288
1289       if (is_mem)
1290         {
1291           tmp = gen_reg_rtx (GET_MODE (a));
1292           tmp = emit_insn (gen_rtx_SET (VOIDmode, tmp, a));
1293         }
1294       else if (! insn_a)
1295         goto end_seq_and_fail;
1296       else
1297         {
1298           a = gen_reg_rtx (GET_MODE (a));
1299           tmp = copy_rtx (insn_a);
1300           set = single_set (tmp);
1301           SET_DEST (set) = a;
1302           tmp = emit_insn (PATTERN (tmp));
1303         }
1304       if (recog_memoized (tmp) < 0)
1305         goto end_seq_and_fail;
1306     }
1307   if (! general_operand (b, GET_MODE (b)))
1308     {
1309       rtx set, last;
1310
1311       if (no_new_pseudos)
1312         goto end_seq_and_fail;
1313
1314       if (is_mem)
1315         {
1316           tmp = gen_reg_rtx (GET_MODE (b));
1317           tmp = gen_rtx_SET (VOIDmode, tmp, b);
1318         }
1319       else if (! insn_b)
1320         goto end_seq_and_fail;
1321       else
1322         {
1323           b = gen_reg_rtx (GET_MODE (b));
1324           tmp = copy_rtx (insn_b);
1325           set = single_set (tmp);
1326           SET_DEST (set) = b;
1327           tmp = PATTERN (tmp);
1328         }
1329
1330       /* If insn to set up A clobbers any registers B depends on, try to
1331          swap insn that sets up A with the one that sets up B.  If even
1332          that doesn't help, punt.  */
1333       last = get_last_insn ();
1334       if (last && modified_in_p (orig_b, last))
1335         {
1336           tmp = emit_insn_before (tmp, get_insns ());
1337           if (modified_in_p (orig_a, tmp))
1338             goto end_seq_and_fail;
1339         }
1340       else
1341         tmp = emit_insn (tmp);
1342
1343       if (recog_memoized (tmp) < 0)
1344         goto end_seq_and_fail;
1345     }
1346
1347   target = noce_emit_cmove (if_info, x, code, XEXP (if_info->cond, 0),
1348                             XEXP (if_info->cond, 1), a, b);
1349
1350   if (! target)
1351     goto end_seq_and_fail;
1352
1353   /* If we're handling a memory for above, emit the load now.  */
1354   if (is_mem)
1355     {
1356       tmp = gen_rtx_MEM (GET_MODE (if_info->x), target);
1357
1358       /* Copy over flags as appropriate.  */
1359       if (MEM_VOLATILE_P (if_info->a) || MEM_VOLATILE_P (if_info->b))
1360         MEM_VOLATILE_P (tmp) = 1;
1361       if (MEM_IN_STRUCT_P (if_info->a) && MEM_IN_STRUCT_P (if_info->b))
1362         MEM_IN_STRUCT_P (tmp) = 1;
1363       if (MEM_SCALAR_P (if_info->a) && MEM_SCALAR_P (if_info->b))
1364         MEM_SCALAR_P (tmp) = 1;
1365       if (MEM_ALIAS_SET (if_info->a) == MEM_ALIAS_SET (if_info->b))
1366         set_mem_alias_set (tmp, MEM_ALIAS_SET (if_info->a));
1367       set_mem_align (tmp,
1368                      MIN (MEM_ALIGN (if_info->a), MEM_ALIGN (if_info->b)));
1369
1370       noce_emit_move_insn (if_info->x, tmp);
1371     }
1372   else if (target != x)
1373     noce_emit_move_insn (x, target);
1374
1375   tmp = end_ifcvt_sequence (if_info);
1376   if (!tmp)
1377     return FALSE;
1378
1379   emit_insn_before_setloc (tmp, if_info->jump, INSN_LOCATOR (if_info->insn_a));
1380   return TRUE;
1381
1382  end_seq_and_fail:
1383   end_sequence ();
1384   return FALSE;
1385 }
1386
1387 /* For most cases, the simplified condition we found is the best
1388    choice, but this is not the case for the min/max/abs transforms.
1389    For these we wish to know that it is A or B in the condition.  */
1390
1391 static rtx
1392 noce_get_alt_condition (struct noce_if_info *if_info, rtx target,
1393                         rtx *earliest)
1394 {
1395   rtx cond, set, insn;
1396   int reverse;
1397
1398   /* If target is already mentioned in the known condition, return it.  */
1399   if (reg_mentioned_p (target, if_info->cond))
1400     {
1401       *earliest = if_info->cond_earliest;
1402       return if_info->cond;
1403     }
1404
1405   set = pc_set (if_info->jump);
1406   cond = XEXP (SET_SRC (set), 0);
1407   reverse
1408     = GET_CODE (XEXP (SET_SRC (set), 2)) == LABEL_REF
1409       && XEXP (XEXP (SET_SRC (set), 2), 0) == JUMP_LABEL (if_info->jump);
1410
1411   /* If we're looking for a constant, try to make the conditional
1412      have that constant in it.  There are two reasons why it may
1413      not have the constant we want:
1414
1415      1. GCC may have needed to put the constant in a register, because
1416         the target can't compare directly against that constant.  For
1417         this case, we look for a SET immediately before the comparison
1418         that puts a constant in that register.
1419
1420      2. GCC may have canonicalized the conditional, for example
1421         replacing "if x < 4" with "if x <= 3".  We can undo that (or
1422         make equivalent types of changes) to get the constants we need
1423         if they're off by one in the right direction.  */
1424
1425   if (GET_CODE (target) == CONST_INT)
1426     {
1427       enum rtx_code code = GET_CODE (if_info->cond);
1428       rtx op_a = XEXP (if_info->cond, 0);
1429       rtx op_b = XEXP (if_info->cond, 1);
1430       rtx prev_insn;
1431
1432       /* First, look to see if we put a constant in a register.  */
1433       prev_insn = PREV_INSN (if_info->cond_earliest);
1434       if (prev_insn
1435           && INSN_P (prev_insn)
1436           && GET_CODE (PATTERN (prev_insn)) == SET)
1437         {
1438           rtx src = find_reg_equal_equiv_note (prev_insn);
1439           if (!src)
1440             src = SET_SRC (PATTERN (prev_insn));
1441           if (GET_CODE (src) == CONST_INT)
1442             {
1443               if (rtx_equal_p (op_a, SET_DEST (PATTERN (prev_insn))))
1444                 op_a = src;
1445               else if (rtx_equal_p (op_b, SET_DEST (PATTERN (prev_insn))))
1446                 op_b = src;
1447
1448               if (GET_CODE (op_a) == CONST_INT)
1449                 {
1450                   rtx tmp = op_a;
1451                   op_a = op_b;
1452                   op_b = tmp;
1453                   code = swap_condition (code);
1454                 }
1455             }
1456         }
1457
1458       /* Now, look to see if we can get the right constant by
1459          adjusting the conditional.  */
1460       if (GET_CODE (op_b) == CONST_INT)
1461         {
1462           HOST_WIDE_INT desired_val = INTVAL (target);
1463           HOST_WIDE_INT actual_val = INTVAL (op_b);
1464
1465           switch (code)
1466             {
1467             case LT:
1468               if (actual_val == desired_val + 1)
1469                 {
1470                   code = LE;
1471                   op_b = GEN_INT (desired_val);
1472                 }
1473               break;
1474             case LE:
1475               if (actual_val == desired_val - 1)
1476                 {
1477                   code = LT;
1478                   op_b = GEN_INT (desired_val);
1479                 }
1480               break;
1481             case GT:
1482               if (actual_val == desired_val - 1)
1483                 {
1484                   code = GE;
1485                   op_b = GEN_INT (desired_val);
1486                 }
1487               break;
1488             case GE:
1489               if (actual_val == desired_val + 1)
1490                 {
1491                   code = GT;
1492                   op_b = GEN_INT (desired_val);
1493                 }
1494               break;
1495             default:
1496               break;
1497             }
1498         }
1499
1500       /* If we made any changes, generate a new conditional that is
1501          equivalent to what we started with, but has the right
1502          constants in it.  */
1503       if (code != GET_CODE (if_info->cond)
1504           || op_a != XEXP (if_info->cond, 0)
1505           || op_b != XEXP (if_info->cond, 1))
1506         {
1507           cond = gen_rtx_fmt_ee (code, GET_MODE (cond), op_a, op_b);
1508           *earliest = if_info->cond_earliest;
1509           return cond;
1510         }
1511     }
1512
1513   cond = canonicalize_condition (if_info->jump, cond, reverse,
1514                                  earliest, target, false, true);
1515   if (! cond || ! reg_mentioned_p (target, cond))
1516     return NULL;
1517
1518   /* We almost certainly searched back to a different place.
1519      Need to re-verify correct lifetimes.  */
1520
1521   /* X may not be mentioned in the range (cond_earliest, jump].  */
1522   for (insn = if_info->jump; insn != *earliest; insn = PREV_INSN (insn))
1523     if (INSN_P (insn) && reg_overlap_mentioned_p (if_info->x, PATTERN (insn)))
1524       return NULL;
1525
1526   /* A and B may not be modified in the range [cond_earliest, jump).  */
1527   for (insn = *earliest; insn != if_info->jump; insn = NEXT_INSN (insn))
1528     if (INSN_P (insn)
1529         && (modified_in_p (if_info->a, insn)
1530             || modified_in_p (if_info->b, insn)))
1531       return NULL;
1532
1533   return cond;
1534 }
1535
1536 /* Convert "if (a < b) x = a; else x = b;" to "x = min(a, b);", etc.  */
1537
1538 static int
1539 noce_try_minmax (struct noce_if_info *if_info)
1540 {
1541   rtx cond, earliest, target, seq;
1542   enum rtx_code code, op;
1543   int unsignedp;
1544
1545   /* ??? Can't guarantee that expand_binop won't create pseudos.  */
1546   if (no_new_pseudos)
1547     return FALSE;
1548
1549   /* ??? Reject modes with NaNs or signed zeros since we don't know how
1550      they will be resolved with an SMIN/SMAX.  It wouldn't be too hard
1551      to get the target to tell us...  */
1552   if (HONOR_SIGNED_ZEROS (GET_MODE (if_info->x))
1553       || HONOR_NANS (GET_MODE (if_info->x)))
1554     return FALSE;
1555
1556   cond = noce_get_alt_condition (if_info, if_info->a, &earliest);
1557   if (!cond)
1558     return FALSE;
1559
1560   /* Verify the condition is of the form we expect, and canonicalize
1561      the comparison code.  */
1562   code = GET_CODE (cond);
1563   if (rtx_equal_p (XEXP (cond, 0), if_info->a))
1564     {
1565       if (! rtx_equal_p (XEXP (cond, 1), if_info->b))
1566         return FALSE;
1567     }
1568   else if (rtx_equal_p (XEXP (cond, 1), if_info->a))
1569     {
1570       if (! rtx_equal_p (XEXP (cond, 0), if_info->b))
1571         return FALSE;
1572       code = swap_condition (code);
1573     }
1574   else
1575     return FALSE;
1576
1577   /* Determine what sort of operation this is.  Note that the code is for
1578      a taken branch, so the code->operation mapping appears backwards.  */
1579   switch (code)
1580     {
1581     case LT:
1582     case LE:
1583     case UNLT:
1584     case UNLE:
1585       op = SMAX;
1586       unsignedp = 0;
1587       break;
1588     case GT:
1589     case GE:
1590     case UNGT:
1591     case UNGE:
1592       op = SMIN;
1593       unsignedp = 0;
1594       break;
1595     case LTU:
1596     case LEU:
1597       op = UMAX;
1598       unsignedp = 1;
1599       break;
1600     case GTU:
1601     case GEU:
1602       op = UMIN;
1603       unsignedp = 1;
1604       break;
1605     default:
1606       return FALSE;
1607     }
1608
1609   start_sequence ();
1610
1611   target = expand_simple_binop (GET_MODE (if_info->x), op,
1612                                 if_info->a, if_info->b,
1613                                 if_info->x, unsignedp, OPTAB_WIDEN);
1614   if (! target)
1615     {
1616       end_sequence ();
1617       return FALSE;
1618     }
1619   if (target != if_info->x)
1620     noce_emit_move_insn (if_info->x, target);
1621
1622   seq = end_ifcvt_sequence (if_info);
1623   if (!seq)
1624     return FALSE;
1625
1626   emit_insn_before_setloc (seq, if_info->jump, INSN_LOCATOR (if_info->insn_a));
1627   if_info->cond = cond;
1628   if_info->cond_earliest = earliest;
1629
1630   return TRUE;
1631 }
1632
1633 /* Convert "if (a < 0) x = -a; else x = a;" to "x = abs(a);", etc.  */
1634
1635 static int
1636 noce_try_abs (struct noce_if_info *if_info)
1637 {
1638   rtx cond, earliest, target, seq, a, b, c;
1639   int negate;
1640
1641   /* ??? Can't guarantee that expand_binop won't create pseudos.  */
1642   if (no_new_pseudos)
1643     return FALSE;
1644
1645   /* Recognize A and B as constituting an ABS or NABS.  */
1646   a = if_info->a;
1647   b = if_info->b;
1648   if (GET_CODE (a) == NEG && rtx_equal_p (XEXP (a, 0), b))
1649     negate = 0;
1650   else if (GET_CODE (b) == NEG && rtx_equal_p (XEXP (b, 0), a))
1651     {
1652       c = a; a = b; b = c;
1653       negate = 1;
1654     }
1655   else
1656     return FALSE;
1657
1658   cond = noce_get_alt_condition (if_info, b, &earliest);
1659   if (!cond)
1660     return FALSE;
1661
1662   /* Verify the condition is of the form we expect.  */
1663   if (rtx_equal_p (XEXP (cond, 0), b))
1664     c = XEXP (cond, 1);
1665   else if (rtx_equal_p (XEXP (cond, 1), b))
1666     c = XEXP (cond, 0);
1667   else
1668     return FALSE;
1669
1670   /* Verify that C is zero.  Search backward through the block for
1671      a REG_EQUAL note if necessary.  */
1672   if (REG_P (c))
1673     {
1674       rtx insn, note = NULL;
1675       for (insn = earliest;
1676            insn != BB_HEAD (if_info->test_bb);
1677            insn = PREV_INSN (insn))
1678         if (INSN_P (insn)
1679             && ((note = find_reg_note (insn, REG_EQUAL, c))
1680                 || (note = find_reg_note (insn, REG_EQUIV, c))))
1681           break;
1682       if (! note)
1683         return FALSE;
1684       c = XEXP (note, 0);
1685     }
1686   if (MEM_P (c)
1687       && GET_CODE (XEXP (c, 0)) == SYMBOL_REF
1688       && CONSTANT_POOL_ADDRESS_P (XEXP (c, 0)))
1689     c = get_pool_constant (XEXP (c, 0));
1690
1691   /* Work around funny ideas get_condition has wrt canonicalization.
1692      Note that these rtx constants are known to be CONST_INT, and
1693      therefore imply integer comparisons.  */
1694   if (c == constm1_rtx && GET_CODE (cond) == GT)
1695     ;
1696   else if (c == const1_rtx && GET_CODE (cond) == LT)
1697     ;
1698   else if (c != CONST0_RTX (GET_MODE (b)))
1699     return FALSE;
1700
1701   /* Determine what sort of operation this is.  */
1702   switch (GET_CODE (cond))
1703     {
1704     case LT:
1705     case LE:
1706     case UNLT:
1707     case UNLE:
1708       negate = !negate;
1709       break;
1710     case GT:
1711     case GE:
1712     case UNGT:
1713     case UNGE:
1714       break;
1715     default:
1716       return FALSE;
1717     }
1718
1719   start_sequence ();
1720
1721   target = expand_abs_nojump (GET_MODE (if_info->x), b, if_info->x, 1);
1722
1723   /* ??? It's a quandary whether cmove would be better here, especially
1724      for integers.  Perhaps combine will clean things up.  */
1725   if (target && negate)
1726     target = expand_simple_unop (GET_MODE (target), NEG, target, if_info->x, 0);
1727
1728   if (! target)
1729     {
1730       end_sequence ();
1731       return FALSE;
1732     }
1733
1734   if (target != if_info->x)
1735     noce_emit_move_insn (if_info->x, target);
1736
1737   seq = end_ifcvt_sequence (if_info);
1738   if (!seq)
1739     return FALSE;
1740
1741   emit_insn_before_setloc (seq, if_info->jump, INSN_LOCATOR (if_info->insn_a));
1742   if_info->cond = cond;
1743   if_info->cond_earliest = earliest;
1744
1745   return TRUE;
1746 }
1747
1748 /* Convert "if (m < 0) x = b; else x = 0;" to "x = (m >> C) & b;".  */
1749
1750 static int
1751 noce_try_sign_mask (struct noce_if_info *if_info)
1752 {
1753   rtx cond, t, m, c, seq;
1754   enum machine_mode mode;
1755   enum rtx_code code;
1756
1757   if (no_new_pseudos)
1758     return FALSE;
1759
1760   cond = if_info->cond;
1761   code = GET_CODE (cond);
1762   m = XEXP (cond, 0);
1763   c = XEXP (cond, 1);
1764
1765   t = NULL_RTX;
1766   if (if_info->a == const0_rtx)
1767     {
1768       if ((code == LT && c == const0_rtx)
1769           || (code == LE && c == constm1_rtx))
1770         t = if_info->b;
1771     }
1772   else if (if_info->b == const0_rtx)
1773     {
1774       if ((code == GE && c == const0_rtx)
1775           || (code == GT && c == constm1_rtx))
1776         t = if_info->a;
1777     }
1778
1779   if (! t || side_effects_p (t))
1780     return FALSE;
1781
1782   /* We currently don't handle different modes.  */
1783   mode = GET_MODE (t);
1784   if (GET_MODE (m) != mode)
1785     return FALSE;
1786
1787   /* This is only profitable if T is cheap, or T is unconditionally
1788      executed/evaluated in the original insn sequence.  */
1789   if (rtx_cost (t, SET) >= COSTS_N_INSNS (2)
1790       && (!if_info->b_unconditional
1791           || t != if_info->b))
1792     return FALSE;
1793
1794   start_sequence ();
1795   /* Use emit_store_flag to generate "m < 0 ? -1 : 0" instead of expanding
1796      "(signed) m >> 31" directly.  This benefits targets with specialized
1797      insns to obtain the signmask, but still uses ashr_optab otherwise.  */
1798   m = emit_store_flag (gen_reg_rtx (mode), LT, m, const0_rtx, mode, 0, -1);
1799   t = m ? expand_binop (mode, and_optab, m, t, NULL_RTX, 0, OPTAB_DIRECT)
1800         : NULL_RTX;
1801
1802   if (!t)
1803     {
1804       end_sequence ();
1805       return FALSE;
1806     }
1807
1808   noce_emit_move_insn (if_info->x, t);
1809
1810   seq = end_ifcvt_sequence (if_info);
1811   if (!seq)
1812     return FALSE;
1813
1814   emit_insn_before_setloc (seq, if_info->jump, INSN_LOCATOR (if_info->insn_a));
1815   return TRUE;
1816 }
1817
1818
1819 /* Similar to get_condition, only the resulting condition must be
1820    valid at JUMP, instead of at EARLIEST.  */
1821
1822 static rtx
1823 noce_get_condition (rtx jump, rtx *earliest)
1824 {
1825   rtx cond, set, tmp;
1826   bool reverse;
1827
1828   if (! any_condjump_p (jump))
1829     return NULL_RTX;
1830
1831   set = pc_set (jump);
1832
1833   /* If this branches to JUMP_LABEL when the condition is false,
1834      reverse the condition.  */
1835   reverse = (GET_CODE (XEXP (SET_SRC (set), 2)) == LABEL_REF
1836              && XEXP (XEXP (SET_SRC (set), 2), 0) == JUMP_LABEL (jump));
1837
1838   /* If the condition variable is a register and is MODE_INT, accept it.  */
1839
1840   cond = XEXP (SET_SRC (set), 0);
1841   tmp = XEXP (cond, 0);
1842   if (REG_P (tmp) && GET_MODE_CLASS (GET_MODE (tmp)) == MODE_INT)
1843     {
1844       *earliest = jump;
1845
1846       if (reverse)
1847         cond = gen_rtx_fmt_ee (reverse_condition (GET_CODE (cond)),
1848                                GET_MODE (cond), tmp, XEXP (cond, 1));
1849       return cond;
1850     }
1851
1852   /* Otherwise, fall back on canonicalize_condition to do the dirty
1853      work of manipulating MODE_CC values and COMPARE rtx codes.  */
1854   return canonicalize_condition (jump, cond, reverse, earliest,
1855                                  NULL_RTX, false, true);
1856 }
1857
1858 /* Return true if OP is ok for if-then-else processing.  */
1859
1860 static int
1861 noce_operand_ok (rtx op)
1862 {
1863   /* We special-case memories, so handle any of them with
1864      no address side effects.  */
1865   if (MEM_P (op))
1866     return ! side_effects_p (XEXP (op, 0));
1867
1868   if (side_effects_p (op))
1869     return FALSE;
1870
1871   return ! may_trap_p (op);
1872 }
1873
1874 /* Given a simple IF-THEN or IF-THEN-ELSE block, attempt to convert it
1875    without using conditional execution.  Return TRUE if we were
1876    successful at converting the block.  */
1877
1878 static int
1879 noce_process_if_block (struct ce_if_block * ce_info)
1880 {
1881   basic_block test_bb = ce_info->test_bb;       /* test block */
1882   basic_block then_bb = ce_info->then_bb;       /* THEN */
1883   basic_block else_bb = ce_info->else_bb;       /* ELSE or NULL */
1884   struct noce_if_info if_info;
1885   rtx insn_a, insn_b;
1886   rtx set_a, set_b;
1887   rtx orig_x, x, a, b;
1888   rtx jump, cond;
1889
1890   /* We're looking for patterns of the form
1891
1892      (1) if (...) x = a; else x = b;
1893      (2) x = b; if (...) x = a;
1894      (3) if (...) x = a;   // as if with an initial x = x.
1895
1896      The later patterns require jumps to be more expensive.
1897
1898      ??? For future expansion, look for multiple X in such patterns.  */
1899
1900   /* If test is comprised of && or || elements, don't handle it unless it is
1901      the special case of && elements without an ELSE block.  */
1902   if (ce_info->num_multiple_test_blocks)
1903     {
1904       if (else_bb || ! ce_info->and_and_p)
1905         return FALSE;
1906
1907       ce_info->test_bb = test_bb = ce_info->last_test_bb;
1908       ce_info->num_multiple_test_blocks = 0;
1909       ce_info->num_and_and_blocks = 0;
1910       ce_info->num_or_or_blocks = 0;
1911     }
1912
1913   /* If this is not a standard conditional jump, we can't parse it.  */
1914   jump = BB_END (test_bb);
1915   cond = noce_get_condition (jump, &if_info.cond_earliest);
1916   if (! cond)
1917     return FALSE;
1918
1919   /* If the conditional jump is more than just a conditional
1920      jump, then we can not do if-conversion on this block.  */
1921   if (! onlyjump_p (jump))
1922     return FALSE;
1923
1924   /* We must be comparing objects whose modes imply the size.  */
1925   if (GET_MODE (XEXP (cond, 0)) == BLKmode)
1926     return FALSE;
1927
1928   /* Look for one of the potential sets.  */
1929   insn_a = first_active_insn (then_bb);
1930   if (! insn_a
1931       || insn_a != last_active_insn (then_bb, FALSE)
1932       || (set_a = single_set (insn_a)) == NULL_RTX)
1933     return FALSE;
1934
1935   x = SET_DEST (set_a);
1936   a = SET_SRC (set_a);
1937
1938   /* Look for the other potential set.  Make sure we've got equivalent
1939      destinations.  */
1940   /* ??? This is overconservative.  Storing to two different mems is
1941      as easy as conditionally computing the address.  Storing to a
1942      single mem merely requires a scratch memory to use as one of the
1943      destination addresses; often the memory immediately below the
1944      stack pointer is available for this.  */
1945   set_b = NULL_RTX;
1946   if (else_bb)
1947     {
1948       insn_b = first_active_insn (else_bb);
1949       if (! insn_b
1950           || insn_b != last_active_insn (else_bb, FALSE)
1951           || (set_b = single_set (insn_b)) == NULL_RTX
1952           || ! rtx_equal_p (x, SET_DEST (set_b)))
1953         return FALSE;
1954     }
1955   else
1956     {
1957       insn_b = prev_nonnote_insn (if_info.cond_earliest);
1958       /* We're going to be moving the evaluation of B down from above
1959          COND_EARLIEST to JUMP.  Make sure the relevant data is still
1960          intact.  */
1961       if (! insn_b
1962           || !NONJUMP_INSN_P (insn_b)
1963           || (set_b = single_set (insn_b)) == NULL_RTX
1964           || ! rtx_equal_p (x, SET_DEST (set_b))
1965           || reg_overlap_mentioned_p (x, SET_SRC (set_b))
1966           || modified_between_p (SET_SRC (set_b),
1967                                  PREV_INSN (if_info.cond_earliest), jump)
1968           /* Likewise with X.  In particular this can happen when
1969              noce_get_condition looks farther back in the instruction
1970              stream than one might expect.  */
1971           || reg_overlap_mentioned_p (x, cond)
1972           || reg_overlap_mentioned_p (x, a)
1973           || modified_between_p (x, PREV_INSN (if_info.cond_earliest), jump))
1974         insn_b = set_b = NULL_RTX;
1975     }
1976
1977   /* If x has side effects then only the if-then-else form is safe to
1978      convert.  But even in that case we would need to restore any notes
1979      (such as REG_INC) at then end.  That can be tricky if
1980      noce_emit_move_insn expands to more than one insn, so disable the
1981      optimization entirely for now if there are side effects.  */
1982   if (side_effects_p (x))
1983     return FALSE;
1984
1985   b = (set_b ? SET_SRC (set_b) : x);
1986
1987   /* Only operate on register destinations, and even then avoid extending
1988      the lifetime of hard registers on small register class machines.  */
1989   orig_x = x;
1990   if (!REG_P (x)
1991       || (SMALL_REGISTER_CLASSES
1992           && REGNO (x) < FIRST_PSEUDO_REGISTER))
1993     {
1994       if (no_new_pseudos || GET_MODE (x) == BLKmode)
1995         return FALSE;
1996       x = gen_reg_rtx (GET_MODE (GET_CODE (x) == STRICT_LOW_PART
1997                                  ? XEXP (x, 0) : x));
1998     }
1999
2000   /* Don't operate on sources that may trap or are volatile.  */
2001   if (! noce_operand_ok (a) || ! noce_operand_ok (b))
2002     return FALSE;
2003
2004   /* Set up the info block for our subroutines.  */
2005   if_info.test_bb = test_bb;
2006   if_info.cond = cond;
2007   if_info.jump = jump;
2008   if_info.insn_a = insn_a;
2009   if_info.insn_b = insn_b;
2010   if_info.x = x;
2011   if_info.a = a;
2012   if_info.b = b;
2013   if_info.b_unconditional = else_bb == 0;
2014
2015   /* Try optimizations in some approximation of a useful order.  */
2016   /* ??? Should first look to see if X is live incoming at all.  If it
2017      isn't, we don't need anything but an unconditional set.  */
2018
2019   /* Look and see if A and B are really the same.  Avoid creating silly
2020      cmove constructs that no one will fix up later.  */
2021   if (rtx_equal_p (a, b))
2022     {
2023       /* If we have an INSN_B, we don't have to create any new rtl.  Just
2024          move the instruction that we already have.  If we don't have an
2025          INSN_B, that means that A == X, and we've got a noop move.  In
2026          that case don't do anything and let the code below delete INSN_A.  */
2027       if (insn_b && else_bb)
2028         {
2029           rtx note;
2030
2031           if (else_bb && insn_b == BB_END (else_bb))
2032             BB_END (else_bb) = PREV_INSN (insn_b);
2033           reorder_insns (insn_b, insn_b, PREV_INSN (jump));
2034
2035           /* If there was a REG_EQUAL note, delete it since it may have been
2036              true due to this insn being after a jump.  */
2037           if ((note = find_reg_note (insn_b, REG_EQUAL, NULL_RTX)) != 0)
2038             remove_note (insn_b, note);
2039
2040           insn_b = NULL_RTX;
2041         }
2042       /* If we have "x = b; if (...) x = a;", and x has side-effects, then
2043          x must be executed twice.  */
2044       else if (insn_b && side_effects_p (orig_x))
2045         return FALSE;
2046
2047       x = orig_x;
2048       goto success;
2049     }
2050
2051   /* Disallow the "if (...) x = a;" form (with an implicit "else x = x;")
2052      for most optimizations if writing to x may trap, i.e. it's a memory
2053      other than a static var or a stack slot.  */
2054   if (! set_b
2055       && MEM_P (orig_x)
2056       && ! MEM_NOTRAP_P (orig_x)
2057       && rtx_addr_can_trap_p (XEXP (orig_x, 0)))
2058     {
2059       if (HAVE_conditional_move)
2060         {
2061           if (noce_try_cmove (&if_info))
2062             goto success;
2063           if (! HAVE_conditional_execution
2064               && noce_try_cmove_arith (&if_info))
2065             goto success;
2066         }
2067       return FALSE;
2068     }
2069
2070   if (noce_try_move (&if_info))
2071     goto success;
2072   if (noce_try_store_flag (&if_info))
2073     goto success;
2074   if (noce_try_minmax (&if_info))
2075     goto success;
2076   if (noce_try_abs (&if_info))
2077     goto success;
2078   if (HAVE_conditional_move
2079       && noce_try_cmove (&if_info))
2080     goto success;
2081   if (! HAVE_conditional_execution)
2082     {
2083       if (noce_try_store_flag_constants (&if_info))
2084         goto success;
2085       if (noce_try_addcc (&if_info))
2086         goto success;
2087       if (noce_try_store_flag_mask (&if_info))
2088         goto success;
2089       if (HAVE_conditional_move
2090           && noce_try_cmove_arith (&if_info))
2091         goto success;
2092       if (noce_try_sign_mask (&if_info))
2093         goto success;
2094     }
2095
2096   return FALSE;
2097
2098  success:
2099   /* The original sets may now be killed.  */
2100   delete_insn (insn_a);
2101
2102   /* Several special cases here: First, we may have reused insn_b above,
2103      in which case insn_b is now NULL.  Second, we want to delete insn_b
2104      if it came from the ELSE block, because follows the now correct
2105      write that appears in the TEST block.  However, if we got insn_b from
2106      the TEST block, it may in fact be loading data needed for the comparison.
2107      We'll let life_analysis remove the insn if it's really dead.  */
2108   if (insn_b && else_bb)
2109     delete_insn (insn_b);
2110
2111   /* The new insns will have been inserted immediately before the jump.  We
2112      should be able to remove the jump with impunity, but the condition itself
2113      may have been modified by gcse to be shared across basic blocks.  */
2114   delete_insn (jump);
2115
2116   /* If we used a temporary, fix it up now.  */
2117   if (orig_x != x)
2118     {
2119       start_sequence ();
2120       noce_emit_move_insn (orig_x, x);
2121       insn_b = get_insns ();
2122       set_used_flags (orig_x);
2123       unshare_all_rtl_in_chain (insn_b);
2124       end_sequence ();
2125
2126       emit_insn_after_setloc (insn_b, BB_END (test_bb), INSN_LOCATOR (insn_a));
2127     }
2128
2129   /* Merge the blocks!  */
2130   merge_if_block (ce_info);
2131
2132   return TRUE;
2133 }
2134 \f
2135 /* Attempt to convert an IF-THEN or IF-THEN-ELSE block into
2136    straight line code.  Return true if successful.  */
2137
2138 static int
2139 process_if_block (struct ce_if_block * ce_info)
2140 {
2141   if (! reload_completed
2142       && noce_process_if_block (ce_info))
2143     return TRUE;
2144
2145   if (HAVE_conditional_execution && reload_completed)
2146     {
2147       /* If we have && and || tests, try to first handle combining the && and
2148          || tests into the conditional code, and if that fails, go back and
2149          handle it without the && and ||, which at present handles the && case
2150          if there was no ELSE block.  */
2151       if (cond_exec_process_if_block (ce_info, TRUE))
2152         return TRUE;
2153
2154       if (ce_info->num_multiple_test_blocks)
2155         {
2156           cancel_changes (0);
2157
2158           if (cond_exec_process_if_block (ce_info, FALSE))
2159             return TRUE;
2160         }
2161     }
2162
2163   return FALSE;
2164 }
2165
2166 /* Merge the blocks and mark for local life update.  */
2167
2168 static void
2169 merge_if_block (struct ce_if_block * ce_info)
2170 {
2171   basic_block test_bb = ce_info->test_bb;       /* last test block */
2172   basic_block then_bb = ce_info->then_bb;       /* THEN */
2173   basic_block else_bb = ce_info->else_bb;       /* ELSE or NULL */
2174   basic_block join_bb = ce_info->join_bb;       /* join block */
2175   basic_block combo_bb;
2176
2177   /* All block merging is done into the lower block numbers.  */
2178
2179   combo_bb = test_bb;
2180
2181   /* Merge any basic blocks to handle && and || subtests.  Each of
2182      the blocks are on the fallthru path from the predecessor block.  */
2183   if (ce_info->num_multiple_test_blocks > 0)
2184     {
2185       basic_block bb = test_bb;
2186       basic_block last_test_bb = ce_info->last_test_bb;
2187       basic_block fallthru = block_fallthru (bb);
2188
2189       do
2190         {
2191           bb = fallthru;
2192           fallthru = block_fallthru (bb);
2193           merge_blocks (combo_bb, bb);
2194           num_true_changes++;
2195         }
2196       while (bb != last_test_bb);
2197     }
2198
2199   /* Merge TEST block into THEN block.  Normally the THEN block won't have a
2200      label, but it might if there were || tests.  That label's count should be
2201      zero, and it normally should be removed.  */
2202
2203   if (then_bb)
2204     {
2205       if (combo_bb->global_live_at_end)
2206         COPY_REG_SET (combo_bb->global_live_at_end,
2207                       then_bb->global_live_at_end);
2208       merge_blocks (combo_bb, then_bb);
2209       num_true_changes++;
2210     }
2211
2212   /* The ELSE block, if it existed, had a label.  That label count
2213      will almost always be zero, but odd things can happen when labels
2214      get their addresses taken.  */
2215   if (else_bb)
2216     {
2217       merge_blocks (combo_bb, else_bb);
2218       num_true_changes++;
2219     }
2220
2221   /* If there was no join block reported, that means it was not adjacent
2222      to the others, and so we cannot merge them.  */
2223
2224   if (! join_bb)
2225     {
2226       rtx last = BB_END (combo_bb);
2227
2228       /* The outgoing edge for the current COMBO block should already
2229          be correct.  Verify this.  */
2230       if (EDGE_COUNT (combo_bb->succs) == 0)
2231         {
2232           if (find_reg_note (last, REG_NORETURN, NULL))
2233             ;
2234           else if (NONJUMP_INSN_P (last)
2235                    && GET_CODE (PATTERN (last)) == TRAP_IF
2236                    && TRAP_CONDITION (PATTERN (last)) == const_true_rtx)
2237             ;
2238           else
2239             abort ();
2240         }
2241
2242       /* There should still be something at the end of the THEN or ELSE
2243          blocks taking us to our final destination.  */
2244       else if (JUMP_P (last))
2245         ;
2246       else if (EDGE_SUCC (combo_bb, 0)->dest == EXIT_BLOCK_PTR
2247                && CALL_P (last)
2248                && SIBLING_CALL_P (last))
2249         ;
2250       else if ((EDGE_SUCC (combo_bb, 0)->flags & EDGE_EH)
2251                && can_throw_internal (last))
2252         ;
2253       else
2254         abort ();
2255     }
2256
2257   /* The JOIN block may have had quite a number of other predecessors too.
2258      Since we've already merged the TEST, THEN and ELSE blocks, we should
2259      have only one remaining edge from our if-then-else diamond.  If there
2260      is more than one remaining edge, it must come from elsewhere.  There
2261      may be zero incoming edges if the THEN block didn't actually join
2262      back up (as with a call to abort).  */
2263   else if (EDGE_COUNT (join_bb->preds) < 2
2264            && join_bb != EXIT_BLOCK_PTR)
2265     {
2266       /* We can merge the JOIN.  */
2267       if (combo_bb->global_live_at_end)
2268         COPY_REG_SET (combo_bb->global_live_at_end,
2269                       join_bb->global_live_at_end);
2270
2271       merge_blocks (combo_bb, join_bb);
2272       num_true_changes++;
2273     }
2274   else
2275     {
2276       /* We cannot merge the JOIN.  */
2277
2278       /* The outgoing edge for the current COMBO block should already
2279          be correct.  Verify this.  */
2280       gcc_assert (single_succ_p (combo_bb)
2281                   && single_succ (combo_bb) == join_bb);
2282
2283       /* Remove the jump and cruft from the end of the COMBO block.  */
2284       if (join_bb != EXIT_BLOCK_PTR)
2285         tidy_fallthru_edge (single_succ_edge (combo_bb));
2286     }
2287
2288   num_updated_if_blocks++;
2289 }
2290 \f
2291 /* Find a block ending in a simple IF condition and try to transform it
2292    in some way.  When converting a multi-block condition, put the new code
2293    in the first such block and delete the rest.  Return a pointer to this
2294    first block if some transformation was done.  Return NULL otherwise.  */
2295
2296 static basic_block
2297 find_if_header (basic_block test_bb, int pass)
2298 {
2299   ce_if_block_t ce_info;
2300   edge then_edge;
2301   edge else_edge;
2302
2303   /* The kind of block we're looking for has exactly two successors.  */
2304   if (EDGE_COUNT (test_bb->succs) != 2)
2305     return NULL;
2306
2307   then_edge = EDGE_SUCC (test_bb, 0);
2308   else_edge = EDGE_SUCC (test_bb, 1);
2309
2310   /* Neither edge should be abnormal.  */
2311   if ((then_edge->flags & EDGE_COMPLEX)
2312       || (else_edge->flags & EDGE_COMPLEX))
2313     return NULL;
2314
2315   /* Nor exit the loop.  */
2316   if ((then_edge->flags & EDGE_LOOP_EXIT)
2317       || (else_edge->flags & EDGE_LOOP_EXIT))
2318     return NULL;
2319
2320   /* The THEN edge is canonically the one that falls through.  */
2321   if (then_edge->flags & EDGE_FALLTHRU)
2322     ;
2323   else if (else_edge->flags & EDGE_FALLTHRU)
2324     {
2325       edge e = else_edge;
2326       else_edge = then_edge;
2327       then_edge = e;
2328     }
2329   else
2330     /* Otherwise this must be a multiway branch of some sort.  */
2331     return NULL;
2332
2333   memset (&ce_info, '\0', sizeof (ce_info));
2334   ce_info.test_bb = test_bb;
2335   ce_info.then_bb = then_edge->dest;
2336   ce_info.else_bb = else_edge->dest;
2337   ce_info.pass = pass;
2338
2339 #ifdef IFCVT_INIT_EXTRA_FIELDS
2340   IFCVT_INIT_EXTRA_FIELDS (&ce_info);
2341 #endif
2342
2343   if (find_if_block (&ce_info))
2344     goto success;
2345
2346   if (HAVE_trap && HAVE_conditional_trap
2347       && find_cond_trap (test_bb, then_edge, else_edge))
2348     goto success;
2349
2350   if (dom_computed[CDI_POST_DOMINATORS] >= DOM_NO_FAST_QUERY
2351       && (! HAVE_conditional_execution || reload_completed))
2352     {
2353       if (find_if_case_1 (test_bb, then_edge, else_edge))
2354         goto success;
2355       if (find_if_case_2 (test_bb, then_edge, else_edge))
2356         goto success;
2357     }
2358
2359   return NULL;
2360
2361  success:
2362   if (dump_file)
2363     fprintf (dump_file, "Conversion succeeded on pass %d.\n", pass);
2364   return ce_info.test_bb;
2365 }
2366
2367 /* Return true if a block has two edges, one of which falls through to the next
2368    block, and the other jumps to a specific block, so that we can tell if the
2369    block is part of an && test or an || test.  Returns either -1 or the number
2370    of non-note, non-jump, non-USE/CLOBBER insns in the block.  */
2371
2372 static int
2373 block_jumps_and_fallthru_p (basic_block cur_bb, basic_block target_bb)
2374 {
2375   edge cur_edge;
2376   int fallthru_p = FALSE;
2377   int jump_p = FALSE;
2378   rtx insn;
2379   rtx end;
2380   int n_insns = 0;
2381   edge_iterator ei;
2382
2383   if (!cur_bb || !target_bb)
2384     return -1;
2385
2386   /* If no edges, obviously it doesn't jump or fallthru.  */
2387   if (EDGE_COUNT (cur_bb->succs) == 0)
2388     return FALSE;
2389
2390   FOR_EACH_EDGE (cur_edge, ei, cur_bb->succs)
2391     {
2392       if (cur_edge->flags & EDGE_COMPLEX)
2393         /* Anything complex isn't what we want.  */
2394         return -1;
2395
2396       else if (cur_edge->flags & EDGE_FALLTHRU)
2397         fallthru_p = TRUE;
2398
2399       else if (cur_edge->dest == target_bb)
2400         jump_p = TRUE;
2401
2402       else
2403         return -1;
2404     }
2405
2406   if ((jump_p & fallthru_p) == 0)
2407     return -1;
2408
2409   /* Don't allow calls in the block, since this is used to group && and ||
2410      together for conditional execution support.  ??? we should support
2411      conditional execution support across calls for IA-64 some day, but
2412      for now it makes the code simpler.  */
2413   end = BB_END (cur_bb);
2414   insn = BB_HEAD (cur_bb);
2415
2416   while (insn != NULL_RTX)
2417     {
2418       if (CALL_P (insn))
2419         return -1;
2420
2421       if (INSN_P (insn)
2422           && !JUMP_P (insn)
2423           && GET_CODE (PATTERN (insn)) != USE
2424           && GET_CODE (PATTERN (insn)) != CLOBBER)
2425         n_insns++;
2426
2427       if (insn == end)
2428         break;
2429
2430       insn = NEXT_INSN (insn);
2431     }
2432
2433   return n_insns;
2434 }
2435
2436 /* Determine if a given basic block heads a simple IF-THEN or IF-THEN-ELSE
2437    block.  If so, we'll try to convert the insns to not require the branch.
2438    Return TRUE if we were successful at converting the block.  */
2439
2440 static int
2441 find_if_block (struct ce_if_block * ce_info)
2442 {
2443   basic_block test_bb = ce_info->test_bb;
2444   basic_block then_bb = ce_info->then_bb;
2445   basic_block else_bb = ce_info->else_bb;
2446   basic_block join_bb = NULL_BLOCK;
2447   edge cur_edge;
2448   basic_block next;
2449   edge_iterator ei;
2450
2451   ce_info->last_test_bb = test_bb;
2452
2453   /* Discover if any fall through predecessors of the current test basic block
2454      were && tests (which jump to the else block) or || tests (which jump to
2455      the then block).  */
2456   if (HAVE_conditional_execution && reload_completed
2457       && single_pred_p (test_bb)
2458       && single_pred_edge (test_bb)->flags == EDGE_FALLTHRU)
2459     {
2460       basic_block bb = single_pred (test_bb);
2461       basic_block target_bb;
2462       int max_insns = MAX_CONDITIONAL_EXECUTE;
2463       int n_insns;
2464
2465       /* Determine if the preceding block is an && or || block.  */
2466       if ((n_insns = block_jumps_and_fallthru_p (bb, else_bb)) >= 0)
2467         {
2468           ce_info->and_and_p = TRUE;
2469           target_bb = else_bb;
2470         }
2471       else if ((n_insns = block_jumps_and_fallthru_p (bb, then_bb)) >= 0)
2472         {
2473           ce_info->and_and_p = FALSE;
2474           target_bb = then_bb;
2475         }
2476       else
2477         target_bb = NULL_BLOCK;
2478
2479       if (target_bb && n_insns <= max_insns)
2480         {
2481           int total_insns = 0;
2482           int blocks = 0;
2483
2484           ce_info->last_test_bb = test_bb;
2485
2486           /* Found at least one && or || block, look for more.  */
2487           do
2488             {
2489               ce_info->test_bb = test_bb = bb;
2490               total_insns += n_insns;
2491               blocks++;
2492
2493               if (!single_pred_p (bb))
2494                 break;
2495
2496               bb = single_pred (bb);
2497               n_insns = block_jumps_and_fallthru_p (bb, target_bb);
2498             }
2499           while (n_insns >= 0 && (total_insns + n_insns) <= max_insns);
2500
2501           ce_info->num_multiple_test_blocks = blocks;
2502           ce_info->num_multiple_test_insns = total_insns;
2503
2504           if (ce_info->and_and_p)
2505             ce_info->num_and_and_blocks = blocks;
2506           else
2507             ce_info->num_or_or_blocks = blocks;
2508         }
2509     }
2510
2511   /* The THEN block of an IF-THEN combo must have exactly one predecessor,
2512      other than any || blocks which jump to the THEN block.  */
2513   if ((EDGE_COUNT (then_bb->preds) - ce_info->num_or_or_blocks) != 1)
2514     return FALSE;
2515     
2516   /* The edges of the THEN and ELSE blocks cannot have complex edges.  */
2517   FOR_EACH_EDGE (cur_edge, ei, then_bb->preds)
2518     {
2519       if (cur_edge->flags & EDGE_COMPLEX)
2520         return FALSE;
2521     }
2522
2523   FOR_EACH_EDGE (cur_edge, ei, else_bb->preds)
2524     {
2525       if (cur_edge->flags & EDGE_COMPLEX)
2526         return FALSE;
2527     }
2528
2529   /* The THEN block of an IF-THEN combo must have zero or one successors.  */
2530   if (EDGE_COUNT (then_bb->succs) > 0
2531       && (!single_succ_p (then_bb)
2532           || (single_succ_edge (then_bb)->flags & EDGE_COMPLEX)
2533           || (flow2_completed && tablejump_p (BB_END (then_bb), NULL, NULL))))
2534     return FALSE;
2535
2536   /* If the THEN block has no successors, conditional execution can still
2537      make a conditional call.  Don't do this unless the ELSE block has
2538      only one incoming edge -- the CFG manipulation is too ugly otherwise.
2539      Check for the last insn of the THEN block being an indirect jump, which
2540      is listed as not having any successors, but confuses the rest of the CE
2541      code processing.  ??? we should fix this in the future.  */
2542   if (EDGE_COUNT (then_bb->succs) == 0)
2543     {
2544       if (single_pred_p (else_bb))
2545         {
2546           rtx last_insn = BB_END (then_bb);
2547
2548           while (last_insn
2549                  && NOTE_P (last_insn)
2550                  && last_insn != BB_HEAD (then_bb))
2551             last_insn = PREV_INSN (last_insn);
2552
2553           if (last_insn
2554               && JUMP_P (last_insn)
2555               && ! simplejump_p (last_insn))
2556             return FALSE;
2557
2558           join_bb = else_bb;
2559           else_bb = NULL_BLOCK;
2560         }
2561       else
2562         return FALSE;
2563     }
2564
2565   /* If the THEN block's successor is the other edge out of the TEST block,
2566      then we have an IF-THEN combo without an ELSE.  */
2567   else if (single_succ (then_bb) == else_bb)
2568     {
2569       join_bb = else_bb;
2570       else_bb = NULL_BLOCK;
2571     }
2572
2573   /* If the THEN and ELSE block meet in a subsequent block, and the ELSE
2574      has exactly one predecessor and one successor, and the outgoing edge
2575      is not complex, then we have an IF-THEN-ELSE combo.  */
2576   else if (single_succ_p (else_bb)
2577            && single_succ (then_bb) == single_succ (else_bb)
2578            && single_pred_p (else_bb)
2579            && ! (single_succ_edge (else_bb)->flags & EDGE_COMPLEX)
2580            && ! (flow2_completed && tablejump_p (BB_END (else_bb), NULL, NULL)))
2581     join_bb = single_succ (else_bb);
2582
2583   /* Otherwise it is not an IF-THEN or IF-THEN-ELSE combination.  */
2584   else
2585     return FALSE;
2586
2587   num_possible_if_blocks++;
2588
2589   if (dump_file)
2590     {
2591       fprintf (dump_file,
2592                "\nIF-THEN%s block found, pass %d, start block %d "
2593                "[insn %d], then %d [%d]",
2594                (else_bb) ? "-ELSE" : "",
2595                ce_info->pass,
2596                test_bb->index,
2597                BB_HEAD (test_bb) ? (int)INSN_UID (BB_HEAD (test_bb)) : -1,
2598                then_bb->index,
2599                BB_HEAD (then_bb) ? (int)INSN_UID (BB_HEAD (then_bb)) : -1);
2600
2601       if (else_bb)
2602         fprintf (dump_file, ", else %d [%d]",
2603                  else_bb->index,
2604                  BB_HEAD (else_bb) ? (int)INSN_UID (BB_HEAD (else_bb)) : -1);
2605
2606       fprintf (dump_file, ", join %d [%d]",
2607                join_bb->index,
2608                BB_HEAD (join_bb) ? (int)INSN_UID (BB_HEAD (join_bb)) : -1);
2609
2610       if (ce_info->num_multiple_test_blocks > 0)
2611         fprintf (dump_file, ", %d %s block%s last test %d [%d]",
2612                  ce_info->num_multiple_test_blocks,
2613                  (ce_info->and_and_p) ? "&&" : "||",
2614                  (ce_info->num_multiple_test_blocks == 1) ? "" : "s",
2615                  ce_info->last_test_bb->index,
2616                  ((BB_HEAD (ce_info->last_test_bb))
2617                   ? (int)INSN_UID (BB_HEAD (ce_info->last_test_bb))
2618                   : -1));
2619
2620       fputc ('\n', dump_file);
2621     }
2622
2623   /* Make sure IF, THEN, and ELSE, blocks are adjacent.  Actually, we get the
2624      first condition for free, since we've already asserted that there's a
2625      fallthru edge from IF to THEN.  Likewise for the && and || blocks, since
2626      we checked the FALLTHRU flag, those are already adjacent to the last IF
2627      block.  */
2628   /* ??? As an enhancement, move the ELSE block.  Have to deal with
2629      BLOCK notes, if by no other means than aborting the merge if they
2630      exist.  Sticky enough I don't want to think about it now.  */
2631   next = then_bb;
2632   if (else_bb && (next = next->next_bb) != else_bb)
2633     return FALSE;
2634   if ((next = next->next_bb) != join_bb && join_bb != EXIT_BLOCK_PTR)
2635     {
2636       if (else_bb)
2637         join_bb = NULL;
2638       else
2639         return FALSE;
2640     }
2641
2642   /* Do the real work.  */
2643   ce_info->else_bb = else_bb;
2644   ce_info->join_bb = join_bb;
2645
2646   return process_if_block (ce_info);
2647 }
2648
2649 /* Convert a branch over a trap, or a branch
2650    to a trap, into a conditional trap.  */
2651
2652 static int
2653 find_cond_trap (basic_block test_bb, edge then_edge, edge else_edge)
2654 {
2655   basic_block then_bb = then_edge->dest;
2656   basic_block else_bb = else_edge->dest;
2657   basic_block other_bb, trap_bb;
2658   rtx trap, jump, cond, cond_earliest, seq;
2659   enum rtx_code code;
2660
2661   /* Locate the block with the trap instruction.  */
2662   /* ??? While we look for no successors, we really ought to allow
2663      EH successors.  Need to fix merge_if_block for that to work.  */
2664   if ((trap = block_has_only_trap (then_bb)) != NULL)
2665     trap_bb = then_bb, other_bb = else_bb;
2666   else if ((trap = block_has_only_trap (else_bb)) != NULL)
2667     trap_bb = else_bb, other_bb = then_bb;
2668   else
2669     return FALSE;
2670
2671   if (dump_file)
2672     {
2673       fprintf (dump_file, "\nTRAP-IF block found, start %d, trap %d\n",
2674                test_bb->index, trap_bb->index);
2675     }
2676
2677   /* If this is not a standard conditional jump, we can't parse it.  */
2678   jump = BB_END (test_bb);
2679   cond = noce_get_condition (jump, &cond_earliest);
2680   if (! cond)
2681     return FALSE;
2682
2683   /* If the conditional jump is more than just a conditional jump, then
2684      we can not do if-conversion on this block.  */
2685   if (! onlyjump_p (jump))
2686     return FALSE;
2687
2688   /* We must be comparing objects whose modes imply the size.  */
2689   if (GET_MODE (XEXP (cond, 0)) == BLKmode)
2690     return FALSE;
2691
2692   /* Reverse the comparison code, if necessary.  */
2693   code = GET_CODE (cond);
2694   if (then_bb == trap_bb)
2695     {
2696       code = reversed_comparison_code (cond, jump);
2697       if (code == UNKNOWN)
2698         return FALSE;
2699     }
2700
2701   /* Attempt to generate the conditional trap.  */
2702   seq = gen_cond_trap (code, XEXP (cond, 0),
2703                        XEXP (cond, 1),
2704                        TRAP_CODE (PATTERN (trap)));
2705   if (seq == NULL)
2706     return FALSE;
2707
2708   num_true_changes++;
2709
2710   /* Emit the new insns before cond_earliest.  */
2711   emit_insn_before_setloc (seq, cond_earliest, INSN_LOCATOR (trap));
2712
2713   /* Delete the trap block if possible.  */
2714   remove_edge (trap_bb == then_bb ? then_edge : else_edge);
2715   if (EDGE_COUNT (trap_bb->preds) == 0)
2716     delete_basic_block (trap_bb);
2717
2718   /* If the non-trap block and the test are now adjacent, merge them.
2719      Otherwise we must insert a direct branch.  */
2720   if (test_bb->next_bb == other_bb)
2721     {
2722       struct ce_if_block new_ce_info;
2723       delete_insn (jump);
2724       memset (&new_ce_info, '\0', sizeof (new_ce_info));
2725       new_ce_info.test_bb = test_bb;
2726       new_ce_info.then_bb = NULL;
2727       new_ce_info.else_bb = NULL;
2728       new_ce_info.join_bb = other_bb;
2729       merge_if_block (&new_ce_info);
2730     }
2731   else
2732     {
2733       rtx lab, newjump;
2734
2735       lab = JUMP_LABEL (jump);
2736       newjump = emit_jump_insn_after (gen_jump (lab), jump);
2737       LABEL_NUSES (lab) += 1;
2738       JUMP_LABEL (newjump) = lab;
2739       emit_barrier_after (newjump);
2740
2741       delete_insn (jump);
2742     }
2743
2744   return TRUE;
2745 }
2746
2747 /* Subroutine of find_cond_trap: if BB contains only a trap insn,
2748    return it.  */
2749
2750 static rtx
2751 block_has_only_trap (basic_block bb)
2752 {
2753   rtx trap;
2754
2755   /* We're not the exit block.  */
2756   if (bb == EXIT_BLOCK_PTR)
2757     return NULL_RTX;
2758
2759   /* The block must have no successors.  */
2760   if (EDGE_COUNT (bb->succs) > 0)
2761     return NULL_RTX;
2762
2763   /* The only instruction in the THEN block must be the trap.  */
2764   trap = first_active_insn (bb);
2765   if (! (trap == BB_END (bb)
2766          && GET_CODE (PATTERN (trap)) == TRAP_IF
2767          && TRAP_CONDITION (PATTERN (trap)) == const_true_rtx))
2768     return NULL_RTX;
2769
2770   return trap;
2771 }
2772
2773 /* Look for IF-THEN-ELSE cases in which one of THEN or ELSE is
2774    transformable, but not necessarily the other.  There need be no
2775    JOIN block.
2776
2777    Return TRUE if we were successful at converting the block.
2778
2779    Cases we'd like to look at:
2780
2781    (1)
2782         if (test) goto over; // x not live
2783         x = a;
2784         goto label;
2785         over:
2786
2787    becomes
2788
2789         x = a;
2790         if (! test) goto label;
2791
2792    (2)
2793         if (test) goto E; // x not live
2794         x = big();
2795         goto L;
2796         E:
2797         x = b;
2798         goto M;
2799
2800    becomes
2801
2802         x = b;
2803         if (test) goto M;
2804         x = big();
2805         goto L;
2806
2807    (3) // This one's really only interesting for targets that can do
2808        // multiway branching, e.g. IA-64 BBB bundles.  For other targets
2809        // it results in multiple branches on a cache line, which often
2810        // does not sit well with predictors.
2811
2812         if (test1) goto E; // predicted not taken
2813         x = a;
2814         if (test2) goto F;
2815         ...
2816         E:
2817         x = b;
2818         J:
2819
2820    becomes
2821
2822         x = a;
2823         if (test1) goto E;
2824         if (test2) goto F;
2825
2826    Notes:
2827
2828    (A) Don't do (2) if the branch is predicted against the block we're
2829    eliminating.  Do it anyway if we can eliminate a branch; this requires
2830    that the sole successor of the eliminated block postdominate the other
2831    side of the if.
2832
2833    (B) With CE, on (3) we can steal from both sides of the if, creating
2834
2835         if (test1) x = a;
2836         if (!test1) x = b;
2837         if (test1) goto J;
2838         if (test2) goto F;
2839         ...
2840         J:
2841
2842    Again, this is most useful if J postdominates.
2843
2844    (C) CE substitutes for helpful life information.
2845
2846    (D) These heuristics need a lot of work.  */
2847
2848 /* Tests for case 1 above.  */
2849
2850 static int
2851 find_if_case_1 (basic_block test_bb, edge then_edge, edge else_edge)
2852 {
2853   basic_block then_bb = then_edge->dest;
2854   basic_block else_bb = else_edge->dest, new_bb;
2855   int then_bb_index;
2856
2857   /* If we are partitioning hot/cold basic blocks, we don't want to
2858      mess up unconditional or indirect jumps that cross between hot
2859      and cold sections.
2860   
2861      Basic block partitioning may result in some jumps that appear to
2862      be optimizable (or blocks that appear to be mergeable), but which really 
2863      must be left untouched (they are required to make it safely across 
2864      partition boundaries).  See  the comments at the top of 
2865      bb-reorder.c:partition_hot_cold_basic_blocks for complete details.  */
2866
2867   if ((BB_END (then_bb) 
2868        && find_reg_note (BB_END (then_bb), REG_CROSSING_JUMP, NULL_RTX))
2869       || (BB_END (test_bb)
2870           && find_reg_note (BB_END (test_bb), REG_CROSSING_JUMP, NULL_RTX))
2871       || (BB_END (else_bb)
2872           && find_reg_note (BB_END (else_bb), REG_CROSSING_JUMP, 
2873                             NULL_RTX)))
2874     return FALSE;
2875
2876   /* THEN has one successor.  */
2877   if (!single_succ_p (then_bb))
2878     return FALSE;
2879
2880   /* THEN does not fall through, but is not strange either.  */
2881   if (single_succ_edge (then_bb)->flags & (EDGE_COMPLEX | EDGE_FALLTHRU))
2882     return FALSE;
2883
2884   /* THEN has one predecessor.  */
2885   if (!single_pred_p (then_bb))
2886     return FALSE;
2887
2888   /* THEN must do something.  */
2889   if (forwarder_block_p (then_bb))
2890     return FALSE;
2891
2892   num_possible_if_blocks++;
2893   if (dump_file)
2894     fprintf (dump_file,
2895              "\nIF-CASE-1 found, start %d, then %d\n",
2896              test_bb->index, then_bb->index);
2897
2898   /* THEN is small.  */
2899   if (! cheap_bb_rtx_cost_p (then_bb, COSTS_N_INSNS (BRANCH_COST)))
2900     return FALSE;
2901
2902   /* Registers set are dead, or are predicable.  */
2903   if (! dead_or_predicable (test_bb, then_bb, else_bb,
2904                             single_succ (then_bb), 1))
2905     return FALSE;
2906
2907   /* Conversion went ok, including moving the insns and fixing up the
2908      jump.  Adjust the CFG to match.  */
2909
2910   bitmap_ior (test_bb->global_live_at_end,
2911               else_bb->global_live_at_start,
2912               then_bb->global_live_at_end);
2913
2914
2915   /* We can avoid creating a new basic block if then_bb is immediately
2916      followed by else_bb, i.e. deleting then_bb allows test_bb to fall
2917      thru to else_bb.  */
2918
2919   if (then_bb->next_bb == else_bb
2920       && then_bb->prev_bb == test_bb
2921       && else_bb != EXIT_BLOCK_PTR)
2922     {
2923       redirect_edge_succ (FALLTHRU_EDGE (test_bb), else_bb);
2924       new_bb = 0;
2925     }
2926   else
2927     new_bb = redirect_edge_and_branch_force (FALLTHRU_EDGE (test_bb),
2928                                              else_bb);
2929
2930   then_bb_index = then_bb->index;
2931   delete_basic_block (then_bb);
2932
2933   /* Make rest of code believe that the newly created block is the THEN_BB
2934      block we removed.  */
2935   if (new_bb)
2936     {
2937       new_bb->index = then_bb_index;
2938       BASIC_BLOCK (then_bb_index) = new_bb;
2939       /* Since the fallthru edge was redirected from test_bb to new_bb,
2940          we need to ensure that new_bb is in the same partition as
2941          test bb (you can not fall through across section boundaries).  */
2942       BB_COPY_PARTITION (new_bb, test_bb);
2943     }
2944   /* We've possibly created jump to next insn, cleanup_cfg will solve that
2945      later.  */
2946
2947   num_true_changes++;
2948   num_updated_if_blocks++;
2949
2950   return TRUE;
2951 }
2952
2953 /* Test for case 2 above.  */
2954
2955 static int
2956 find_if_case_2 (basic_block test_bb, edge then_edge, edge else_edge)
2957 {
2958   basic_block then_bb = then_edge->dest;
2959   basic_block else_bb = else_edge->dest;
2960   edge else_succ;
2961   rtx note;
2962
2963   /* If we are partitioning hot/cold basic blocks, we don't want to
2964      mess up unconditional or indirect jumps that cross between hot
2965      and cold sections.
2966   
2967      Basic block partitioning may result in some jumps that appear to
2968      be optimizable (or blocks that appear to be mergeable), but which really 
2969      must be left untouched (they are required to make it safely across 
2970      partition boundaries).  See  the comments at the top of 
2971      bb-reorder.c:partition_hot_cold_basic_blocks for complete details.  */
2972
2973   if ((BB_END (then_bb)
2974        && find_reg_note (BB_END (then_bb), REG_CROSSING_JUMP, NULL_RTX))
2975       || (BB_END (test_bb)
2976           && find_reg_note (BB_END (test_bb), REG_CROSSING_JUMP, NULL_RTX))
2977       || (BB_END (else_bb) 
2978           && find_reg_note (BB_END (else_bb), REG_CROSSING_JUMP, 
2979                             NULL_RTX)))
2980     return FALSE;
2981
2982   /* ELSE has one successor.  */
2983   if (!single_succ_p (else_bb))
2984     return FALSE;
2985   else
2986     else_succ = single_succ_edge (else_bb);
2987
2988   /* ELSE outgoing edge is not complex.  */
2989   if (else_succ->flags & EDGE_COMPLEX)
2990     return FALSE;
2991
2992   /* ELSE has one predecessor.  */
2993   if (!single_pred_p (else_bb))
2994     return FALSE;
2995
2996   /* THEN is not EXIT.  */
2997   if (then_bb->index < 0)
2998     return FALSE;
2999
3000   /* ELSE is predicted or SUCC(ELSE) postdominates THEN.  */
3001   note = find_reg_note (BB_END (test_bb), REG_BR_PROB, NULL_RTX);
3002   if (note && INTVAL (XEXP (note, 0)) >= REG_BR_PROB_BASE / 2)
3003     ;
3004   else if (else_succ->dest->index < 0
3005            || dominated_by_p (CDI_POST_DOMINATORS, then_bb,
3006                               else_succ->dest))
3007     ;
3008   else
3009     return FALSE;
3010
3011   num_possible_if_blocks++;
3012   if (dump_file)
3013     fprintf (dump_file,
3014              "\nIF-CASE-2 found, start %d, else %d\n",
3015              test_bb->index, else_bb->index);
3016
3017   /* ELSE is small.  */
3018   if (! cheap_bb_rtx_cost_p (else_bb, COSTS_N_INSNS (BRANCH_COST)))
3019     return FALSE;
3020
3021   /* Registers set are dead, or are predicable.  */
3022   if (! dead_or_predicable (test_bb, else_bb, then_bb, else_succ->dest, 0))
3023     return FALSE;
3024
3025   /* Conversion went ok, including moving the insns and fixing up the
3026      jump.  Adjust the CFG to match.  */
3027
3028   bitmap_ior (test_bb->global_live_at_end,
3029               then_bb->global_live_at_start,
3030               else_bb->global_live_at_end);
3031
3032   delete_basic_block (else_bb);
3033
3034   num_true_changes++;
3035   num_updated_if_blocks++;
3036
3037   /* ??? We may now fallthru from one of THEN's successors into a join
3038      block.  Rerun cleanup_cfg?  Examine things manually?  Wait?  */
3039
3040   return TRUE;
3041 }
3042
3043 /* A subroutine of dead_or_predicable called through for_each_rtx.
3044    Return 1 if a memory is found.  */
3045
3046 static int
3047 find_memory (rtx *px, void *data ATTRIBUTE_UNUSED)
3048 {
3049   return MEM_P (*px);
3050 }
3051
3052 /* Used by the code above to perform the actual rtl transformations.
3053    Return TRUE if successful.
3054
3055    TEST_BB is the block containing the conditional branch.  MERGE_BB
3056    is the block containing the code to manipulate.  NEW_DEST is the
3057    label TEST_BB should be branching to after the conversion.
3058    REVERSEP is true if the sense of the branch should be reversed.  */
3059
3060 static int
3061 dead_or_predicable (basic_block test_bb, basic_block merge_bb,
3062                     basic_block other_bb, basic_block new_dest, int reversep)
3063 {
3064   rtx head, end, jump, earliest = NULL_RTX, old_dest, new_label = NULL_RTX;
3065
3066   jump = BB_END (test_bb);
3067
3068   /* Find the extent of the real code in the merge block.  */
3069   head = BB_HEAD (merge_bb);
3070   end = BB_END (merge_bb);
3071
3072   if (LABEL_P (head))
3073     head = NEXT_INSN (head);
3074   if (NOTE_P (head))
3075     {
3076       if (head == end)
3077         {
3078           head = end = NULL_RTX;
3079           goto no_body;
3080         }
3081       head = NEXT_INSN (head);
3082     }
3083
3084   if (JUMP_P (end))
3085     {
3086       if (head == end)
3087         {
3088           head = end = NULL_RTX;
3089           goto no_body;
3090         }
3091       end = PREV_INSN (end);
3092     }
3093
3094   /* Disable handling dead code by conditional execution if the machine needs
3095      to do anything funny with the tests, etc.  */
3096 #ifndef IFCVT_MODIFY_TESTS
3097   if (HAVE_conditional_execution)
3098     {
3099       /* In the conditional execution case, we have things easy.  We know
3100          the condition is reversible.  We don't have to check life info
3101          because we're going to conditionally execute the code anyway.
3102          All that's left is making sure the insns involved can actually
3103          be predicated.  */
3104
3105       rtx cond, prob_val;
3106
3107       cond = cond_exec_get_condition (jump);
3108       if (! cond)
3109         return FALSE;
3110
3111       prob_val = find_reg_note (jump, REG_BR_PROB, NULL_RTX);
3112       if (prob_val)
3113         prob_val = XEXP (prob_val, 0);
3114
3115       if (reversep)
3116         {
3117           enum rtx_code rev = reversed_comparison_code (cond, jump);
3118           if (rev == UNKNOWN)
3119             return FALSE;
3120           cond = gen_rtx_fmt_ee (rev, GET_MODE (cond), XEXP (cond, 0),
3121                                  XEXP (cond, 1));
3122           if (prob_val)
3123             prob_val = GEN_INT (REG_BR_PROB_BASE - INTVAL (prob_val));
3124         }
3125
3126       if (! cond_exec_process_insns ((ce_if_block_t *)0, head, end, cond,
3127                                      prob_val, 0))
3128         goto cancel;
3129
3130       earliest = jump;
3131     }
3132   else
3133 #endif
3134     {
3135       /* In the non-conditional execution case, we have to verify that there
3136          are no trapping operations, no calls, no references to memory, and
3137          that any registers modified are dead at the branch site.  */
3138
3139       rtx insn, cond, prev;
3140       regset merge_set, tmp, test_live, test_set;
3141       struct propagate_block_info *pbi;
3142       unsigned i, fail = 0;
3143       bitmap_iterator bi;
3144
3145       /* Check for no calls or trapping operations.  */
3146       for (insn = head; ; insn = NEXT_INSN (insn))
3147         {
3148           if (CALL_P (insn))
3149             return FALSE;
3150           if (INSN_P (insn))
3151             {
3152               if (may_trap_p (PATTERN (insn)))
3153                 return FALSE;
3154
3155               /* ??? Even non-trapping memories such as stack frame
3156                  references must be avoided.  For stores, we collect
3157                  no lifetime info; for reads, we'd have to assert
3158                  true_dependence false against every store in the
3159                  TEST range.  */
3160               if (for_each_rtx (&PATTERN (insn), find_memory, NULL))
3161                 return FALSE;
3162             }
3163           if (insn == end)
3164             break;
3165         }
3166
3167       if (! any_condjump_p (jump))
3168         return FALSE;
3169
3170       /* Find the extent of the conditional.  */
3171       cond = noce_get_condition (jump, &earliest);
3172       if (! cond)
3173         return FALSE;
3174
3175       /* Collect:
3176            MERGE_SET = set of registers set in MERGE_BB
3177            TEST_LIVE = set of registers live at EARLIEST
3178            TEST_SET  = set of registers set between EARLIEST and the
3179                        end of the block.  */
3180
3181       tmp = ALLOC_REG_SET (&reg_obstack);
3182       merge_set = ALLOC_REG_SET (&reg_obstack);
3183       test_live = ALLOC_REG_SET (&reg_obstack);
3184       test_set = ALLOC_REG_SET (&reg_obstack);
3185
3186       /* ??? bb->local_set is only valid during calculate_global_regs_live,
3187          so we must recompute usage for MERGE_BB.  Not so bad, I suppose,
3188          since we've already asserted that MERGE_BB is small.  */
3189       propagate_block (merge_bb, tmp, merge_set, merge_set, 0);
3190
3191       /* For small register class machines, don't lengthen lifetimes of
3192          hard registers before reload.  */
3193       if (SMALL_REGISTER_CLASSES && ! reload_completed)
3194         {
3195           EXECUTE_IF_SET_IN_BITMAP (merge_set, 0, i, bi)
3196             {
3197               if (i < FIRST_PSEUDO_REGISTER
3198                   && ! fixed_regs[i]
3199                   && ! global_regs[i])
3200                 fail = 1;
3201             }
3202         }
3203
3204       /* For TEST, we're interested in a range of insns, not a whole block.
3205          Moreover, we're interested in the insns live from OTHER_BB.  */
3206
3207       COPY_REG_SET (test_live, other_bb->global_live_at_start);
3208       pbi = init_propagate_block_info (test_bb, test_live, test_set, test_set,
3209                                        0);
3210
3211       for (insn = jump; ; insn = prev)
3212         {
3213           prev = propagate_one_insn (pbi, insn);
3214           if (insn == earliest)
3215             break;
3216         }
3217
3218       free_propagate_block_info (pbi);
3219
3220       /* We can perform the transformation if
3221            MERGE_SET & (TEST_SET | TEST_LIVE)
3222          and
3223            TEST_SET & merge_bb->global_live_at_start
3224          are empty.  */
3225
3226       if (bitmap_intersect_p (test_set, merge_set)
3227           || bitmap_intersect_p (test_live, merge_set)
3228           || bitmap_intersect_p (test_set, merge_bb->global_live_at_start))
3229         fail = 1;
3230
3231       FREE_REG_SET (tmp);
3232       FREE_REG_SET (merge_set);
3233       FREE_REG_SET (test_live);
3234       FREE_REG_SET (test_set);
3235
3236       if (fail)
3237         return FALSE;
3238     }
3239
3240  no_body:
3241   /* We don't want to use normal invert_jump or redirect_jump because
3242      we don't want to delete_insn called.  Also, we want to do our own
3243      change group management.  */
3244
3245   old_dest = JUMP_LABEL (jump);
3246   if (other_bb != new_dest)
3247     {
3248       new_label = block_label (new_dest);
3249       if (reversep
3250           ? ! invert_jump_1 (jump, new_label)
3251           : ! redirect_jump_1 (jump, new_label))
3252         goto cancel;
3253     }
3254
3255   if (! apply_change_group ())
3256     return FALSE;
3257
3258   if (other_bb != new_dest)
3259     {
3260       redirect_jump_2 (jump, old_dest, new_label, -1, reversep);
3261
3262       redirect_edge_succ (BRANCH_EDGE (test_bb), new_dest);
3263       if (reversep)
3264         {
3265           gcov_type count, probability;
3266           count = BRANCH_EDGE (test_bb)->count;
3267           BRANCH_EDGE (test_bb)->count = FALLTHRU_EDGE (test_bb)->count;
3268           FALLTHRU_EDGE (test_bb)->count = count;
3269           probability = BRANCH_EDGE (test_bb)->probability;
3270           BRANCH_EDGE (test_bb)->probability
3271             = FALLTHRU_EDGE (test_bb)->probability;
3272           FALLTHRU_EDGE (test_bb)->probability = probability;
3273           update_br_prob_note (test_bb);
3274         }
3275     }
3276
3277   /* Move the insns out of MERGE_BB to before the branch.  */
3278   if (head != NULL)
3279     {
3280       if (end == BB_END (merge_bb))
3281         BB_END (merge_bb) = PREV_INSN (head);
3282
3283       if (squeeze_notes (&head, &end))
3284         return TRUE;
3285
3286       reorder_insns (head, end, PREV_INSN (earliest));
3287     }
3288
3289   /* Remove the jump and edge if we can.  */
3290   if (other_bb == new_dest)
3291     {
3292       delete_insn (jump);
3293       remove_edge (BRANCH_EDGE (test_bb));
3294       /* ??? Can't merge blocks here, as then_bb is still in use.
3295          At minimum, the merge will get done just before bb-reorder.  */
3296     }
3297
3298   return TRUE;
3299
3300  cancel:
3301   cancel_changes (0);
3302   return FALSE;
3303 }
3304 \f
3305 /* Main entry point for all if-conversion.  */
3306
3307 void
3308 if_convert (int x_life_data_ok)
3309 {
3310   basic_block bb;
3311   int pass;
3312
3313   num_possible_if_blocks = 0;
3314   num_updated_if_blocks = 0;
3315   num_true_changes = 0;
3316   life_data_ok = (x_life_data_ok != 0);
3317
3318   if ((! targetm.cannot_modify_jumps_p ())
3319       && (!flag_reorder_blocks_and_partition || !no_new_pseudos
3320           || !targetm.have_named_sections))
3321     {
3322       struct loops loops;
3323
3324       flow_loops_find (&loops);
3325       mark_loop_exit_edges (&loops);
3326       flow_loops_free (&loops);
3327       free_dominance_info (CDI_DOMINATORS);
3328     }
3329
3330   /* Compute postdominators if we think we'll use them.  */
3331   if (HAVE_conditional_execution || life_data_ok)
3332     calculate_dominance_info (CDI_POST_DOMINATORS);
3333
3334   if (life_data_ok)
3335     clear_bb_flags ();
3336
3337   /* Go through each of the basic blocks looking for things to convert.  If we
3338      have conditional execution, we make multiple passes to allow us to handle
3339      IF-THEN{-ELSE} blocks within other IF-THEN{-ELSE} blocks.  */
3340   pass = 0;
3341   do
3342     {
3343       cond_exec_changed_p = FALSE;
3344       pass++;
3345
3346 #ifdef IFCVT_MULTIPLE_DUMPS
3347       if (dump_file && pass > 1)
3348         fprintf (dump_file, "\n\n========== Pass %d ==========\n", pass);
3349 #endif
3350
3351       FOR_EACH_BB (bb)
3352         {
3353           basic_block new_bb;
3354           while ((new_bb = find_if_header (bb, pass)))
3355             bb = new_bb;
3356         }
3357
3358 #ifdef IFCVT_MULTIPLE_DUMPS
3359       if (dump_file && cond_exec_changed_p)
3360         print_rtl_with_bb (dump_file, get_insns ());
3361 #endif
3362     }
3363   while (cond_exec_changed_p);
3364
3365 #ifdef IFCVT_MULTIPLE_DUMPS
3366   if (dump_file)
3367     fprintf (dump_file, "\n\n========== no more changes\n");
3368 #endif
3369
3370   free_dominance_info (CDI_POST_DOMINATORS);
3371
3372   if (dump_file)
3373     fflush (dump_file);
3374
3375   clear_aux_for_blocks ();
3376
3377   /* Rebuild life info for basic blocks that require it.  */
3378   if (num_true_changes && life_data_ok)
3379     {
3380       /* If we allocated new pseudos, we must resize the array for sched1.  */
3381       if (max_regno < max_reg_num ())
3382         {
3383           max_regno = max_reg_num ();
3384           allocate_reg_info (max_regno, FALSE, FALSE);
3385         }
3386       update_life_info_in_dirty_blocks (UPDATE_LIFE_GLOBAL_RM_NOTES,
3387                                         PROP_DEATH_NOTES | PROP_SCAN_DEAD_CODE
3388                                         | PROP_KILL_DEAD_CODE);
3389     }
3390
3391   /* Write the final stats.  */
3392   if (dump_file && num_possible_if_blocks > 0)
3393     {
3394       fprintf (dump_file,
3395                "\n%d possible IF blocks searched.\n",
3396                num_possible_if_blocks);
3397       fprintf (dump_file,
3398                "%d IF blocks converted.\n",
3399                num_updated_if_blocks);
3400       fprintf (dump_file,
3401                "%d true changes made.\n\n\n",
3402                num_true_changes);
3403     }
3404
3405 #ifdef ENABLE_CHECKING
3406   verify_flow_info ();
3407 #endif
3408 }