OSDN Git Service

* Makefile.in (final.o): Depend on BASIC_BLOCK_H.
[pf3gnuchains/gcc-fork.git] / gcc / ifcvt.c
1 /* If-conversion support.
2    Copyright (C) 2000 Free Software Foundation, Inc.
3
4    This file is part of GNU CC.
5
6    GNU CC is free software; you can redistribute it and/or modify
7    it under the terms of the GNU General Public License as published by
8    the Free Software Foundation; either version 2, or (at your option)
9    any later version.
10
11    GNU CC is distributed in the hope that it will be useful,
12    but WITHOUT ANY WARRANTY; without even the implied warranty of
13    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14    GNU General Public License for more details.
15
16    You should have received a copy of the GNU General Public License
17    along with GNU CC; see the file COPYING.  If not, write to
18    the Free Software Foundation, 59 Temple Place - Suite 330,
19    Boston, MA 02111-1307, USA.  */
20
21 #include "config.h"
22 #include "system.h"
23
24 #include "rtl.h"
25 #include "regs.h"
26 #include "function.h"
27 #include "flags.h"
28 #include "insn-config.h"
29 #include "recog.h"
30 #include "basic-block.h"
31 #include "expr.h"
32 #include "output.h"
33 #include "hard-reg-set.h"
34 #include "tm_p.h"
35
36
37 #ifndef HAVE_conditional_execution
38 #define HAVE_conditional_execution 0
39 #endif
40 #ifndef HAVE_conditional_move
41 #define HAVE_conditional_move 0
42 #endif
43 #ifndef HAVE_incscc
44 #define HAVE_incscc 0
45 #endif
46 #ifndef HAVE_decscc
47 #define HAVE_decscc 0
48 #endif
49
50 #ifndef MAX_CONDITIONAL_EXECUTE
51 #define MAX_CONDITIONAL_EXECUTE   (BRANCH_COST + 1)
52 #endif
53
54 #define EDGE_COMPLEX    (EDGE_ABNORMAL | EDGE_ABNORMAL_CALL | EDGE_EH)
55
56 #define NULL_EDGE       ((struct edge_def *)NULL)
57 #define NULL_BLOCK      ((struct basic_block_def *)NULL)
58
59 /* # of IF-THEN or IF-THEN-ELSE blocks we looked at  */
60 static int num_possible_if_blocks;
61
62 /* # of IF-THEN or IF-THEN-ELSE blocks were converted to conditional
63    execution.  */
64 static int num_updated_if_blocks;
65
66 /* # of basic blocks that were removed.  */
67 static int num_removed_blocks;
68
69 /* The post-dominator relation on the original block numbers.  */
70 static sbitmap *post_dominators;
71
72 /* Forward references.  */
73 static int count_bb_insns               PARAMS ((basic_block));
74 static rtx first_active_insn            PARAMS ((basic_block));
75 static int last_active_insn_p           PARAMS ((basic_block, rtx));
76
77 static int cond_exec_process_insns      PARAMS ((rtx, rtx, rtx, rtx, int));
78 static rtx cond_exec_get_condition      PARAMS ((rtx));
79 static int cond_exec_process_if_block   PARAMS ((basic_block, basic_block,
80                                                  basic_block, basic_block));
81
82 static rtx noce_get_condition           PARAMS ((rtx, rtx *));
83 static int noce_process_if_block        PARAMS ((basic_block, basic_block,
84                                                  basic_block, basic_block));
85
86 static int process_if_block             PARAMS ((basic_block, basic_block,
87                                                  basic_block, basic_block));
88 static void merge_if_block              PARAMS ((basic_block, basic_block,
89                                                  basic_block, basic_block));
90
91 static int find_if_header               PARAMS ((basic_block));
92 static int find_if_block                PARAMS ((basic_block, edge, edge));
93 static int find_if_case_1               PARAMS ((basic_block, edge, edge));
94 static int find_if_case_2               PARAMS ((basic_block, edge, edge));
95 static int find_memory                  PARAMS ((rtx *, void *));
96 static int dead_or_predicable           PARAMS ((basic_block, basic_block,
97                                                  basic_block, rtx, int));
98 \f
99 /* Abuse the basic_block AUX field to store the original block index,
100    as well as a flag indicating that the block should be rescaned for
101    life analysis.  */
102
103 #define SET_ORIG_INDEX(BB,I)    ((BB)->aux = (void *)((size_t)(I) << 1))
104 #define ORIG_INDEX(BB)          ((size_t)(BB)->aux >> 1)
105 #define SET_UPDATE_LIFE(BB)     ((BB)->aux = (void *)((size_t)(BB)->aux | 1))
106 #define UPDATE_LIFE(BB)         ((size_t)(BB)->aux & 1)
107
108 \f
109 /* Count the number of non-jump active insns in BB.  */
110
111 static int
112 count_bb_insns (bb)
113      basic_block bb;
114 {
115   int count = 0;
116   rtx insn = bb->head;
117
118   while (1)
119     {
120       if (GET_CODE (insn) == CALL_INSN || GET_CODE (insn) == INSN)
121         count++;
122
123       if (insn == bb->end)
124         break;
125       insn = NEXT_INSN (insn);
126     }
127
128   return count;
129 }
130
131 /* Return the first non-jump active insn in the basic block.  */
132
133 static rtx
134 first_active_insn (bb)
135      basic_block bb;
136 {
137   rtx insn = bb->head;
138
139   if (GET_CODE (insn) == CODE_LABEL)
140     {
141       if (insn == bb->end)
142         return NULL_RTX;
143       insn = NEXT_INSN (insn);
144     }
145
146   while (GET_CODE (insn) == NOTE)
147     {
148       if (insn == bb->end)
149         return NULL_RTX;
150       insn = NEXT_INSN (insn);
151     }
152
153   if (GET_CODE (insn) == JUMP_INSN)
154     return NULL_RTX;
155
156   return insn;
157 }
158
159 /* Return true if INSN is the last active non-jump insn in BB.  */
160
161 static int
162 last_active_insn_p (bb, insn)
163      basic_block bb;
164      rtx insn;
165 {
166   do
167     {
168       if (insn == bb->end)
169         return TRUE;
170       insn = NEXT_INSN (insn);
171     }
172   while (GET_CODE (insn) == NOTE);
173
174   return GET_CODE (insn) == JUMP_INSN;
175 }
176 \f
177 /* Go through a bunch of insns, converting them to conditional
178    execution format if possible.  Return TRUE if all of the non-note
179    insns were processed.  */
180
181 static int
182 cond_exec_process_insns (start, end, test, prob_val, mod_ok)
183      rtx start;                 /* first insn to look at */
184      rtx end;                   /* last insn to look at */
185      rtx test;                  /* conditional execution test */
186      rtx prob_val;              /* probability of branch taken.  */
187      int mod_ok;                /* true if modifications ok last insn.  */
188 {
189   int must_be_last = FALSE;
190   rtx insn;
191
192   for (insn = start; ; insn = NEXT_INSN (insn))
193     {
194       if (GET_CODE (insn) == NOTE)
195         goto insn_done;
196
197       if (GET_CODE (insn) != INSN && GET_CODE (insn) != CALL_INSN)
198         abort ();
199
200       /* Last insn wasn't last?  */
201       if (must_be_last)
202         return FALSE;
203
204       if (modified_in_p (test, insn))
205         {
206           if (!mod_ok)
207             return FALSE;
208           must_be_last = TRUE;
209         }
210
211       /* Now build the conditional form of the instruction.  */
212       validate_change (insn, &PATTERN (insn),
213                        gen_rtx_COND_EXEC (VOIDmode, copy_rtx (test),
214                                           PATTERN (insn)), 1);
215
216       if (GET_CODE (insn) == CALL_INSN && prob_val)
217         validate_change (insn, &REG_NOTES (insn),
218                          alloc_EXPR_LIST (REG_BR_PROB, prob_val,
219                                           REG_NOTES (insn)), 1);
220
221     insn_done:
222       if (insn == end)
223         break;
224     }
225
226   return TRUE;
227 }
228
229 /* Return the condition for a jump.  Do not do any special processing.  */
230
231 static rtx
232 cond_exec_get_condition (jump)
233      rtx jump;
234 {
235   rtx test_if, cond;
236
237   if (condjump_p (jump))
238     test_if = SET_SRC (PATTERN (jump));
239   else if (condjump_in_parallel_p (jump))
240     test_if = SET_SRC (XVECEXP (PATTERN (jump), 0, 0));
241   else
242     return NULL_RTX;
243   cond = XEXP (test_if, 0);
244
245   /* If this branches to JUMP_LABEL when the condition is false,
246      reverse the condition.  */
247   if (GET_CODE (XEXP (test_if, 2)) == LABEL_REF
248       && XEXP (XEXP (test_if, 2), 0) == JUMP_LABEL (jump))
249     cond = gen_rtx_fmt_ee (reverse_condition (GET_CODE (cond)),
250                            GET_MODE (cond), XEXP (cond, 0),
251                            XEXP (cond, 1));
252
253   return cond;
254 }
255
256 /* Given a simple IF-THEN or IF-THEN-ELSE block, attempt to convert it
257    to conditional execution.  Return TRUE if we were successful at
258    converting the the block.  */
259
260 static int
261 cond_exec_process_if_block (test_bb, then_bb, else_bb, join_bb)
262      basic_block test_bb;       /* Basic block test is in */
263      basic_block then_bb;       /* Basic block for THEN block */
264      basic_block else_bb;       /* Basic block for ELSE block */
265      basic_block join_bb;       /* Basic block the join label is in */
266 {
267   rtx test_expr;                /* expression in IF_THEN_ELSE that is tested */
268   rtx then_start;               /* first insn in THEN block */
269   rtx then_end;                 /* last insn + 1 in THEN block */
270   rtx else_start;               /* first insn in ELSE block or NULL */
271   rtx else_end;                 /* last insn + 1 in ELSE block */
272   int max;                      /* max # of insns to convert. */
273   int then_mod_ok;              /* whether conditional mods are ok in THEN */
274   rtx true_expr;                /* test for else block insns */
275   rtx false_expr;               /* test for then block insns */
276   rtx true_prob_val;            /* probability of else block */
277   rtx false_prob_val;           /* probability of then block */
278   int n_insns;
279
280   /* Find the conditional jump to the ELSE or JOIN part, and isolate
281      the test.  */
282   test_expr = cond_exec_get_condition (test_bb->end);
283   if (! test_expr)
284     return FALSE;
285
286   /* Collect the bounds of where we're to search.  */
287
288   then_start = then_bb->head;
289   then_end = then_bb->end;
290
291   /* Skip a (use (const_int 0)) or branch as the final insn.  */
292   if (GET_CODE (then_end) == INSN
293       && GET_CODE (PATTERN (then_end)) == USE
294       && GET_CODE (XEXP (PATTERN (then_end), 0)) == CONST_INT)
295     then_end = PREV_INSN (then_end);
296   else if (GET_CODE (then_end) == JUMP_INSN)
297     then_end = PREV_INSN (then_end);
298
299   if (else_bb)
300     {
301       /* Skip the ELSE block's label.  */
302       else_start = NEXT_INSN (else_bb->head);
303       else_end = else_bb->end;
304
305       /* Skip a (use (const_int 0)) or branch as the final insn.  */
306       if (GET_CODE (else_end) == INSN
307           && GET_CODE (PATTERN (else_end)) == USE
308           && GET_CODE (XEXP (PATTERN (else_end), 0)) == CONST_INT)
309         else_end = PREV_INSN (else_end);
310       else if (GET_CODE (else_end) == JUMP_INSN)
311         else_end = PREV_INSN (else_end);
312     }
313
314   /* How many instructions should we convert in total?  */
315   n_insns = 0;
316   if (else_bb)
317     {
318       max = 2 * MAX_CONDITIONAL_EXECUTE;
319       n_insns = count_bb_insns (else_bb);
320     }
321   else
322     max = MAX_CONDITIONAL_EXECUTE;
323   n_insns += count_bb_insns (then_bb);
324   if (n_insns > max)
325     return FALSE;
326
327   /* Map test_expr/test_jump into the appropriate MD tests to use on
328      the conditionally executed code.  */
329   
330   true_expr = test_expr;
331   false_expr = gen_rtx_fmt_ee (reverse_condition (GET_CODE (true_expr)),
332                                GET_MODE (true_expr), XEXP (true_expr, 0),
333                                XEXP (true_expr, 1));
334
335   true_prob_val = find_reg_note (test_bb->end, REG_BR_PROB, NULL_RTX);
336   if (true_prob_val)
337     {
338       true_prob_val = XEXP (true_prob_val, 0);
339       false_prob_val = GEN_INT (REG_BR_PROB_BASE - INTVAL (true_prob_val));
340     }
341   else
342     false_prob_val = NULL_RTX;
343
344   /* For IF-THEN-ELSE blocks, we don't allow modifications of the test
345      on then THEN block.  */
346   then_mod_ok = (else_bb == NULL_BLOCK);
347
348   /* Go through the THEN and ELSE blocks converting the insns if possible
349      to conditional execution.  */
350
351   if (then_end
352       && ! cond_exec_process_insns (then_start, then_end,
353                                     false_expr, false_prob_val, then_mod_ok))
354     goto fail;
355
356   if (else_bb
357       && ! cond_exec_process_insns (else_start, else_end,
358                                     true_expr, true_prob_val, TRUE))
359     goto fail;
360
361   if (! apply_change_group ())
362     return FALSE;
363
364   /* Conversion succeeded.  */
365   if (rtl_dump_file)
366     fprintf (rtl_dump_file, "%d insn%s converted to conditional execution.\n",
367              n_insns, (n_insns == 1) ? " was" : "s were");
368
369   /* Merge the blocks!  */
370   merge_if_block (test_bb, then_bb, else_bb, join_bb);
371   return TRUE;
372
373  fail:
374   cancel_changes (0);
375   return FALSE;
376 }
377 \f
378 /* Used by noce_process_if_block to communicate with its subroutines. 
379
380    The subroutines know that A and B may be evaluated freely.  They
381    know that X is a register.  They should insert new instructions 
382    before cond_earliest.  */
383
384 struct noce_if_info
385 {
386   rtx insn_a, insn_b;
387   rtx x, a, b;
388   rtx jump, cond, cond_earliest;
389 };
390
391 static rtx noce_emit_store_flag         PARAMS ((struct noce_if_info *,
392                                                  rtx, int, int));
393 static int noce_try_store_flag          PARAMS ((struct noce_if_info *));
394 static int noce_try_store_flag_inc      PARAMS ((struct noce_if_info *));
395 static int noce_try_store_flag_constants PARAMS ((struct noce_if_info *));
396 static int noce_try_store_flag_mask     PARAMS ((struct noce_if_info *));
397 static rtx noce_emit_cmove              PARAMS ((struct noce_if_info *,
398                                                  rtx, enum rtx_code, rtx,
399                                                  rtx, rtx, rtx));
400 static int noce_try_cmove               PARAMS ((struct noce_if_info *));
401 static int noce_try_cmove_arith         PARAMS ((struct noce_if_info *));
402
403 /* Helper function for noce_try_store_flag*.  */
404
405 static rtx
406 noce_emit_store_flag (if_info, x, reversep, normalize)
407      struct noce_if_info *if_info;
408      rtx x;
409      int reversep, normalize;
410 {
411   rtx cond = if_info->cond;
412   int cond_complex;
413   enum rtx_code code;
414
415   cond_complex = (! general_operand (XEXP (cond, 0), VOIDmode)
416                   || ! general_operand (XEXP (cond, 1), VOIDmode));
417
418   /* If earliest == jump, or when the condition is complex, try to
419      build the store_flag insn directly.  */
420
421   if (cond_complex)
422     cond = XEXP (SET_SRC (PATTERN (if_info->jump)), 0);
423
424   if ((if_info->cond_earliest == if_info->jump || cond_complex)
425       && (normalize == 0 || STORE_FLAG_VALUE == normalize))
426     {
427       rtx tmp;
428
429       code = GET_CODE (cond);
430       if (reversep)
431         code = reverse_condition (code);
432
433       tmp = gen_rtx_fmt_ee (code, GET_MODE (x), XEXP (cond, 0),
434                             XEXP (cond, 1));
435       tmp = gen_rtx_SET (VOIDmode, x, tmp);
436
437       start_sequence ();
438       tmp = emit_insn (tmp);
439
440       if (recog_memoized (tmp) >= 0)
441         {
442           tmp = get_insns ();
443           end_sequence ();
444           emit_insns (tmp);
445
446           if_info->cond_earliest = if_info->jump;
447
448           return x;
449         }
450
451       end_sequence ();
452     }
453
454   /* Don't even try if the comparison operands are weird.  */
455   if (cond_complex)
456     return NULL_RTX;
457
458   code = GET_CODE (cond);
459   if (reversep)
460     code = reverse_condition (code);
461
462   return emit_store_flag (x, code, XEXP (cond, 0),
463                           XEXP (cond, 1), VOIDmode,
464                           (code == LTU || code == LEU
465                            || code == GEU || code == GTU), normalize);
466 }
467
468 /* Convert "if (test) x = 1; else x = 0".
469
470    Only try 0 and STORE_FLAG_VALUE here.  Other combinations will be
471    tried in noce_try_store_flag_constants after noce_try_cmove has had
472    a go at the conversion.  */
473
474 static int
475 noce_try_store_flag (if_info)
476      struct noce_if_info *if_info;
477 {
478   int reversep;
479   rtx target, seq;
480
481   if (GET_CODE (if_info->b) == CONST_INT
482       && INTVAL (if_info->b) == STORE_FLAG_VALUE
483       && if_info->a == const0_rtx)
484     reversep = 0;
485   else if (if_info->b == const0_rtx
486            && GET_CODE (if_info->a) == CONST_INT
487            && INTVAL (if_info->a) == STORE_FLAG_VALUE
488            && can_reverse_comparison_p (if_info->cond, if_info->jump))
489     reversep = 1;
490   else
491     return FALSE;
492
493   start_sequence ();
494
495   target = noce_emit_store_flag (if_info, if_info->x, reversep, 0);
496   if (target)
497     {
498       if (target != if_info->x)
499         emit_move_insn (if_info->x, target);
500
501       seq = get_insns ();
502       end_sequence ();
503       emit_insns_before (seq, if_info->cond_earliest);
504
505       return TRUE;
506     }
507   else
508     {
509       end_sequence ();
510       return FALSE;
511     }
512 }
513
514 /* Convert "if (test) x = a; else x = b", for A and B constant.  */
515
516 static int
517 noce_try_store_flag_constants (if_info)
518      struct noce_if_info *if_info;
519 {
520   rtx target, seq;
521   int reversep;
522   HOST_WIDE_INT itrue, ifalse, diff, tmp;
523   int normalize, can_reverse;
524
525   if (! no_new_pseudos
526       && GET_CODE (if_info->a) == CONST_INT
527       && GET_CODE (if_info->b) == CONST_INT)
528     {
529       ifalse = INTVAL (if_info->a);
530       itrue = INTVAL (if_info->b);
531       diff = itrue - ifalse;
532
533       can_reverse = can_reverse_comparison_p (if_info->cond, if_info->jump);
534
535       reversep = 0;
536       if (diff == STORE_FLAG_VALUE || diff == -STORE_FLAG_VALUE)
537         normalize = 0;
538       else if (ifalse == 0 && exact_log2 (itrue) >= 0
539                && (STORE_FLAG_VALUE == 1
540                    || BRANCH_COST >= 2))
541         normalize = 1;
542       else if (itrue == 0 && exact_log2 (ifalse) >= 0 && can_reverse
543                && (STORE_FLAG_VALUE == 1 || BRANCH_COST >= 2))
544         normalize = 1, reversep = 1;
545       else if (itrue == -1
546                && (STORE_FLAG_VALUE == -1
547                    || BRANCH_COST >= 2))
548         normalize = -1;
549       else if (ifalse == -1 && can_reverse
550                && (STORE_FLAG_VALUE == -1 || BRANCH_COST >= 2))
551         normalize = -1, reversep = 1;
552       else if ((BRANCH_COST >= 2 && STORE_FLAG_VALUE == -1)
553                || BRANCH_COST >= 3)
554         normalize = -1;
555       else
556         return FALSE;
557
558       if (reversep)
559         {
560           tmp = itrue; itrue = ifalse; ifalse = tmp;
561           diff = -diff;
562         }
563
564       start_sequence ();
565       target = noce_emit_store_flag (if_info, if_info->x, reversep, normalize);
566       if (! target)
567         {
568           end_sequence ();
569           return FALSE;
570         }
571
572       /* if (test) x = 3; else x = 4;
573          =>   x = 3 + (test == 0);  */
574       if (diff == STORE_FLAG_VALUE || diff == -STORE_FLAG_VALUE)
575         {
576           target = expand_binop (GET_MODE (if_info->x),
577                                  (diff == STORE_FLAG_VALUE
578                                   ? add_optab : sub_optab),
579                                  GEN_INT (ifalse), target, if_info->x, 0,
580                                  OPTAB_WIDEN);
581         }
582
583       /* if (test) x = 8; else x = 0;
584          =>   x = (test != 0) << 3;  */
585       else if (ifalse == 0 && (tmp = exact_log2 (itrue)) >= 0)
586         {
587           target = expand_binop (GET_MODE (if_info->x), ashl_optab,
588                                  target, GEN_INT (tmp), if_info->x, 0,
589                                  OPTAB_WIDEN);
590         }
591
592       /* if (test) x = -1; else x = b;
593          =>   x = -(test != 0) | b;  */
594       else if (itrue == -1)
595         {
596           target = expand_binop (GET_MODE (if_info->x), ior_optab,
597                                  target, GEN_INT (ifalse), if_info->x, 0,
598                                  OPTAB_WIDEN);
599         }
600
601       /* if (test) x = a; else x = b;
602          =>   x = (-(test != 0) & (b - a)) + a;  */
603       else
604         {
605           target = expand_binop (GET_MODE (if_info->x), and_optab,
606                                  target, GEN_INT (diff), if_info->x, 0,
607                                  OPTAB_WIDEN);
608           if (target)
609             target = expand_binop (GET_MODE (if_info->x), add_optab,
610                                    target, GEN_INT (ifalse), if_info->x, 0,
611                                    OPTAB_WIDEN);
612         }
613
614       if (! target)
615         {
616           end_sequence ();
617           return FALSE;
618         }
619
620       if (target != if_info->x)
621         emit_move_insn (if_info->x, target);
622
623       seq = get_insns ();
624       end_sequence ();
625       emit_insns_before (seq, if_info->cond_earliest);
626
627       return TRUE;
628     }
629
630   return FALSE;
631 }
632
633 /* Convert "if (test) foo++" into "foo += (test != 0)", and 
634    similarly for "foo--".  */
635
636 static int
637 noce_try_store_flag_inc (if_info)
638      struct noce_if_info *if_info;
639 {
640   rtx target, seq;
641   int subtract, normalize;
642
643   if (! no_new_pseudos
644       && (BRANCH_COST >= 2
645           || HAVE_incscc
646           || HAVE_decscc)
647       /* Should be no `else' case to worry about.  */
648       && if_info->b == if_info->x
649       && GET_CODE (if_info->a) == PLUS
650       && (XEXP (if_info->a, 1) == const1_rtx
651           || XEXP (if_info->a, 1) == constm1_rtx)
652       && rtx_equal_p (XEXP (if_info->a, 0), if_info->x)
653       && can_reverse_comparison_p (if_info->cond, if_info->jump))
654     {
655       if (STORE_FLAG_VALUE == INTVAL (XEXP (if_info->a, 1)))
656         subtract = 0, normalize = 0;
657       else if (-STORE_FLAG_VALUE == INTVAL (XEXP (if_info->a, 1)))
658         subtract = 1, normalize = 0;
659       else
660         subtract = 0, normalize = INTVAL (XEXP (if_info->a, 1));
661       
662       start_sequence ();
663
664       target = noce_emit_store_flag (if_info,
665                                      gen_reg_rtx (GET_MODE (if_info->x)),
666                                      1, normalize);
667
668       if (target)
669         target = expand_binop (GET_MODE (if_info->x),
670                                subtract ? sub_optab : add_optab,
671                                if_info->x, target, if_info->x, 0, OPTAB_WIDEN);
672       if (target)
673         {
674           if (target != if_info->x)
675             emit_move_insn (if_info->x, target);
676
677           seq = get_insns ();
678           end_sequence ();
679           emit_insns_before (seq, if_info->cond_earliest);
680
681           return TRUE;
682         }
683
684       end_sequence ();
685     }
686
687   return FALSE;
688 }
689
690 /* Convert "if (test) x = 0;" to "x &= -(test == 0);"  */
691
692 static int
693 noce_try_store_flag_mask (if_info)
694      struct noce_if_info *if_info;
695 {
696   rtx target, seq;
697   int reversep;
698
699   reversep = 0;
700   if (! no_new_pseudos
701       && (BRANCH_COST >= 2
702           || STORE_FLAG_VALUE == -1)
703       && ((if_info->a == const0_rtx
704            && rtx_equal_p (if_info->b, if_info->x))
705           || ((reversep = can_reverse_comparison_p (if_info->cond,
706                                                     if_info->jump))
707               && if_info->b == const0_rtx
708               && rtx_equal_p (if_info->a, if_info->x))))
709     {
710       start_sequence ();
711       target = noce_emit_store_flag (if_info,
712                                      gen_reg_rtx (GET_MODE (if_info->x)),
713                                      reversep, -1);
714       if (target)
715         target = expand_binop (GET_MODE (if_info->x), and_optab,
716                                if_info->x, target, if_info->x, 0,
717                                OPTAB_WIDEN);
718
719       if (target)
720         {
721           if (target != if_info->x)
722             emit_move_insn (if_info->x, target);
723
724           seq = get_insns ();
725           end_sequence ();
726           emit_insns_before (seq, if_info->cond_earliest);
727
728           return TRUE;
729         }
730
731       end_sequence ();
732     }
733
734   return FALSE;
735 }
736
737 /* Helper function for noce_try_cmove and noce_try_cmove_arith.  */
738
739 static rtx
740 noce_emit_cmove (if_info, x, code, cmp_a, cmp_b, vfalse, vtrue)
741      struct noce_if_info *if_info;
742      rtx x, cmp_a, cmp_b, vfalse, vtrue;
743      enum rtx_code code;
744 {
745   /* If earliest == jump, try to build the cmove insn directly.
746      This is helpful when combine has created some complex condition
747      (like for alpha's cmovlbs) that we can't hope to regenerate
748      through the normal interface.  */
749
750   if (if_info->cond_earliest == if_info->jump)
751     {
752       rtx tmp;
753
754       tmp = gen_rtx_fmt_ee (code, GET_MODE (if_info->cond), cmp_a, cmp_b);
755       tmp = gen_rtx_IF_THEN_ELSE (GET_MODE (x), tmp, vtrue, vfalse);
756       tmp = gen_rtx_SET (VOIDmode, x, tmp);
757
758       start_sequence ();
759       tmp = emit_insn (tmp);
760
761       if (recog_memoized (tmp) >= 0)
762         {
763           tmp = get_insns ();
764           end_sequence ();
765           emit_insns (tmp);
766
767           return x;
768         }
769
770       end_sequence ();
771     }
772
773   /* Don't even try if the comparison operands are weird.  */
774   if (! general_operand (cmp_a, GET_MODE (cmp_a))
775       || ! general_operand (cmp_b, GET_MODE (cmp_b)))
776     return NULL_RTX;
777
778 #if HAVE_conditional_move
779   return emit_conditional_move (x, code, cmp_a, cmp_b, VOIDmode,
780                                 vtrue, vfalse, GET_MODE (x),
781                                 (code == LTU || code == GEU
782                                  || code == LEU || code == GTU));
783 #else
784   /* We'll never get here, as noce_process_if_block doesn't call the
785      functions involved.  Ifdef code, however, should be discouraged
786      because it leads to typos in the code not selected.  However, 
787      emit_conditional_move won't exist either.  */
788   return NULL_RTX;
789 #endif
790 }
791
792 /* Try only simple constants and registers here.  More complex cases
793    are handled in noce_try_cmove_arith after noce_try_store_flag_arith
794    has had a go at it.  */
795
796 static int
797 noce_try_cmove (if_info)
798      struct noce_if_info *if_info;
799 {
800   enum rtx_code code;
801   rtx target, seq;
802
803   if ((CONSTANT_P (if_info->a) || register_operand (if_info->a, VOIDmode))
804       && (CONSTANT_P (if_info->b) || register_operand (if_info->b, VOIDmode)))
805     {
806       start_sequence ();
807
808       code = GET_CODE (if_info->cond);
809       target = noce_emit_cmove (if_info, if_info->x, code,
810                                 XEXP (if_info->cond, 0),
811                                 XEXP (if_info->cond, 1),
812                                 if_info->a, if_info->b);
813
814       if (target)
815         {
816           if (target != if_info->x)
817             emit_move_insn (if_info->x, target);
818
819           seq = get_insns ();
820           end_sequence ();
821           emit_insns_before (seq, if_info->cond_earliest);
822           return TRUE;
823         }
824       else
825         {
826           end_sequence ();
827           return FALSE;
828         }
829     }
830
831   return FALSE;
832 }
833
834 /* Try more complex cases involving conditional_move.  */
835
836 static int
837 noce_try_cmove_arith (if_info)
838      struct noce_if_info *if_info;
839 {
840   rtx a = if_info->a;
841   rtx b = if_info->b;
842   rtx x = if_info->x;
843   rtx insn_a, insn_b;
844   rtx tmp, target;
845   int is_mem = 0;
846   enum rtx_code code;
847
848   /* A conditional move from two memory sources is equivalent to a
849      conditional on their addresses followed by a load.  Don't do this
850      early because it'll screw alias analysis.  Note that we've
851      already checked for no side effects.  */
852   if (! no_new_pseudos && cse_not_expected
853       && GET_CODE (a) == MEM && GET_CODE (b) == MEM
854       && BRANCH_COST >= 5)
855     {
856       a = XEXP (a, 0);
857       b = XEXP (b, 0);
858       x = gen_reg_rtx (Pmode);
859       is_mem = 1;
860     }
861
862   /* ??? We could handle this if we knew that a load from A or B could
863      not fault.  This is also true if we've already loaded
864      from the address along the path from ENTRY.  */
865   else if (may_trap_p (a) || may_trap_p (b))
866     return FALSE;
867
868   /* if (test) x = a + b; else x = c - d;
869      => y = a + b;
870         x = c - d;
871         if (test)
872           x = y;
873   */
874   
875   code = GET_CODE (if_info->cond);
876   insn_a = if_info->insn_a;
877   insn_b = if_info->insn_b;
878
879   /* Possibly rearrange operands to make things come out more natural.  */
880   if (can_reverse_comparison_p (if_info->cond, if_info->jump))
881     {
882       int reversep = 0;
883       if (rtx_equal_p (b, x))
884         reversep = 1;
885       else if (general_operand (b, GET_MODE (b)))
886         reversep = 1;
887
888       if (reversep)
889         {
890           code = reverse_condition (code);
891           tmp = a, a = b, b = tmp;
892           tmp = insn_a, insn_a = insn_b, insn_b = tmp;
893         }
894     }
895
896   start_sequence ();
897
898   /* If either operand is complex, load it into a register first.
899      The best way to do this is to copy the original insn.  In this
900      way we preserve any clobbers etc that the insn may have had.  
901      This is of course not possible in the IS_MEM case.  */
902   if (! general_operand (a, GET_MODE (a)))
903     {
904       rtx set;
905
906       if (no_new_pseudos)
907         goto end_seq_and_fail;
908
909       if (is_mem)
910         {
911           tmp = gen_reg_rtx (GET_MODE (a));
912           tmp = emit_insn (gen_rtx_SET (VOIDmode, tmp, a));
913         }
914       else if (! insn_a)
915         goto end_seq_and_fail;
916       else
917         {
918           a = gen_reg_rtx (GET_MODE (a));
919           tmp = copy_rtx (insn_a);
920           set = single_set (tmp);
921           SET_DEST (set) = a;
922           tmp = emit_insn (PATTERN (tmp));
923         }
924       if (recog_memoized (tmp) < 0)
925         goto end_seq_and_fail;
926     }
927   if (! general_operand (b, GET_MODE (b)))
928     {
929       rtx set;
930
931       if (no_new_pseudos)
932         goto end_seq_and_fail;
933
934       if (is_mem)
935         {
936           tmp = gen_reg_rtx (GET_MODE (b));
937           tmp = emit_insn (gen_rtx_SET (VOIDmode, tmp, b));
938         }
939       else if (! insn_b)
940         goto end_seq_and_fail;
941       else
942         {
943           b = gen_reg_rtx (GET_MODE (b));
944           tmp = copy_rtx (insn_b);
945           set = single_set (tmp);
946           SET_DEST (set) = b;
947           tmp = emit_insn (PATTERN (tmp));
948         }
949       if (recog_memoized (tmp) < 0)
950         goto end_seq_and_fail;
951     }
952
953   target = noce_emit_cmove (if_info, x, code, XEXP (if_info->cond, 0),
954                             XEXP (if_info->cond, 1), a, b);
955
956   if (! target)
957     goto end_seq_and_fail;
958
959   /* If we're handling a memory for above, emit the load now.  */
960   if (is_mem)
961     {
962       tmp = gen_rtx_MEM (GET_MODE (if_info->x), target);
963
964       /* Copy over flags as appropriate.  */
965       if (MEM_VOLATILE_P (if_info->a) || MEM_VOLATILE_P (if_info->b))
966         MEM_VOLATILE_P (tmp) = 1;
967       if (MEM_IN_STRUCT_P (if_info->a) && MEM_IN_STRUCT_P (if_info->b))
968         MEM_IN_STRUCT_P (tmp) = 1;
969       if (MEM_SCALAR_P (if_info->a) && MEM_SCALAR_P (if_info->b))
970         MEM_SCALAR_P (tmp) = 1;
971       if (MEM_ALIAS_SET (if_info->a) == MEM_ALIAS_SET (if_info->b))
972         MEM_ALIAS_SET (tmp) = MEM_ALIAS_SET (if_info->a);
973
974       emit_move_insn (if_info->x, tmp);
975     }
976   else if (target != x)
977     emit_move_insn (x, target);
978
979   tmp = get_insns ();
980   end_sequence ();
981   emit_insns_before (tmp, if_info->cond_earliest);
982   return TRUE;
983
984  end_seq_and_fail:
985   end_sequence ();
986   return FALSE;
987 }
988
989 /* Look for the condition for the jump first.  We'd prefer to avoid
990    get_condition if we can -- it tries to look back for the contents
991    of an original compare.  On targets that use normal integers for
992    comparisons, e.g. alpha, this is wasteful.  */
993
994 static rtx
995 noce_get_condition (jump, earliest)
996      rtx jump;
997      rtx *earliest;
998 {
999   rtx cond;
1000
1001   /* If the condition variable is a register and is MODE_INT, accept it.
1002      Otherwise, fall back on get_condition.  */
1003
1004   if (! condjump_p (jump))
1005     return NULL_RTX;
1006
1007   cond = XEXP (SET_SRC (PATTERN (jump)), 0);
1008   if (GET_CODE (XEXP (cond, 0)) == REG
1009       && GET_MODE_CLASS (GET_MODE (XEXP (cond, 0))) == MODE_INT)
1010     {
1011       *earliest = jump;
1012
1013       /* If this branches to JUMP_LABEL when the condition is false,
1014          reverse the condition.  */
1015       if (GET_CODE (XEXP (SET_SRC (PATTERN (jump)), 2)) == LABEL_REF
1016           && XEXP (XEXP (SET_SRC (PATTERN (jump)), 2), 0) == JUMP_LABEL (jump))
1017         cond = gen_rtx_fmt_ee (reverse_condition (GET_CODE (cond)),
1018                                GET_MODE (cond), XEXP (cond, 0),
1019                                XEXP (cond, 1));
1020     }
1021   else
1022     cond = get_condition (jump, earliest);
1023
1024   return cond;
1025 }
1026
1027 /* Given a simple IF-THEN or IF-THEN-ELSE block, attempt to convert it
1028    without using conditional execution.  Return TRUE if we were
1029    successful at converting the the block.  */
1030
1031 static int
1032 noce_process_if_block (test_bb, then_bb, else_bb, join_bb)
1033      basic_block test_bb;       /* Basic block test is in */
1034      basic_block then_bb;       /* Basic block for THEN block */
1035      basic_block else_bb;       /* Basic block for ELSE block */
1036      basic_block join_bb;       /* Basic block the join label is in */
1037 {
1038   /* We're looking for patterns of the form
1039
1040      (1) if (...) x = a; else x = b;
1041      (2) x = b; if (...) x = a;
1042      (3) if (...) x = a;   // as if with an initial x = x.
1043
1044      The later patterns require jumps to be more expensive.
1045
1046      ??? For future expansion, look for multiple X in such patterns.  */
1047
1048   struct noce_if_info if_info;
1049   rtx insn_a, insn_b;
1050   rtx set_a, set_b;
1051   rtx orig_x, x, a, b;
1052   rtx jump, cond, insn;
1053
1054   /* If this is not a standard conditional jump, we can't parse it.  */
1055   jump = test_bb->end;
1056   cond = noce_get_condition (jump, &if_info.cond_earliest);
1057   if (! cond)
1058     return FALSE;
1059
1060   /* We must be comparing objects whose modes imply the size.  */
1061   if (GET_MODE (XEXP (cond, 0)) == BLKmode)
1062     return FALSE;
1063
1064   /* Look for one of the potential sets.  */
1065   insn_a = first_active_insn (then_bb);
1066   if (! insn_a
1067       || ! last_active_insn_p (then_bb, insn_a)
1068       || (set_a = single_set (insn_a)) == NULL_RTX)
1069     return FALSE;
1070
1071   x = SET_DEST (set_a);
1072   a = SET_SRC (set_a);
1073
1074   /* Look for the other potential set.  Make sure we've got equivalent
1075      destinations.  */
1076   /* ??? This is overconservative.  Storing to two different mems is
1077      as easy as conditionally computing the address.  Storing to a
1078      single mem merely requires a scratch memory to use as one of the
1079      destination addresses; often the memory immediately below the
1080      stack pointer is available for this.  */
1081   set_b = NULL_RTX;
1082   if (else_bb)
1083     {
1084       insn_b = first_active_insn (else_bb);
1085       if (! insn_b
1086           || ! last_active_insn_p (else_bb, insn_b)
1087           || (set_b = single_set (insn_b)) == NULL_RTX
1088           || ! rtx_equal_p (x, SET_DEST (set_b)))
1089         return FALSE;
1090     }
1091   else
1092     {
1093       insn_b = prev_nonnote_insn (if_info.cond_earliest);
1094       if (! insn_b
1095           || GET_CODE (insn_b) != INSN
1096           || (set_b = single_set (insn_b)) == NULL_RTX
1097           || ! rtx_equal_p (x, SET_DEST (set_b))
1098           || reg_mentioned_p (x, cond)
1099           || reg_mentioned_p (x, a)
1100           || reg_mentioned_p (x, SET_SRC (set_b)))
1101         insn_b = set_b = NULL_RTX;
1102     }
1103   b = (set_b ? SET_SRC (set_b) : x);
1104
1105   /* X may not be mentioned in the range (cond_earliest, jump].  */
1106   for (insn = jump; insn != if_info.cond_earliest; insn = PREV_INSN (insn))
1107     if (INSN_P (insn) && reg_mentioned_p (x, insn))
1108       return FALSE;
1109
1110   /* A and B may not be modified in the range [cond_earliest, jump).  */
1111   for (insn = if_info.cond_earliest; insn != jump; insn = NEXT_INSN (insn))
1112     if (INSN_P (insn)
1113         && (modified_in_p (a, insn) || modified_in_p (b, insn)))
1114       return FALSE;
1115
1116   /* Only operate on register destinations, and even then avoid extending
1117      the lifetime of hard registers on small register class machines.  */
1118   orig_x = x;
1119   if (GET_CODE (x) != REG
1120       || (SMALL_REGISTER_CLASSES
1121           && REGNO (x) < FIRST_PSEUDO_REGISTER))
1122     {
1123       if (no_new_pseudos)
1124         return FALSE;
1125       x = gen_reg_rtx (GET_MODE (x));
1126     }
1127
1128   /* Don't operate on sources that may trap or are volatile.  */
1129   if (side_effects_p (a) || side_effects_p (b)
1130       || (GET_CODE (a) != MEM && may_trap_p (a))
1131       || (GET_CODE (b) != MEM && may_trap_p (b)))
1132     return FALSE;
1133
1134   /* Set up the info block for our subroutines.  */
1135   if_info.cond = cond;
1136   if_info.jump = jump;
1137   if_info.insn_a = insn_a;
1138   if_info.insn_b = insn_b;
1139   if_info.x = x;
1140   if_info.a = a;
1141   if_info.b = b;
1142
1143   /* Try optimizations in some approximation of a useful order.  */
1144   /* ??? Should first look to see if X is live incoming at all.  If it
1145      isn't, we don't need anything but an unconditional set.  */
1146
1147   /* Look and see if A and B are really the same.  Avoid creating silly
1148      cmove constructs that no one will fix up later.  */
1149   if (rtx_equal_p (a, b))
1150     {
1151       /* If we have an INSN_B, we don't have to create any new rtl.  Just
1152          move the instruction that we already have.  If we don't have an
1153          INSN_B, that means that A == X, and we've got a noop move.  In
1154          that case don't do anything and let the code below delete INSN_A.  */
1155       if (insn_b && else_bb)
1156         {
1157           if (else_bb && insn_b == else_bb->end)
1158             else_bb->end = PREV_INSN (insn_b);
1159           reorder_insns (insn_b, insn_b, PREV_INSN (if_info.cond_earliest));
1160           insn_b = NULL_RTX;
1161         }
1162       x = orig_x;
1163       goto success;
1164     }
1165
1166   if (noce_try_store_flag (&if_info))
1167     goto success;
1168   if (HAVE_conditional_move
1169       && noce_try_cmove (&if_info))
1170     goto success;
1171   if (! HAVE_conditional_execution)
1172     {
1173       if (noce_try_store_flag_constants (&if_info))
1174         goto success;
1175       if (noce_try_store_flag_inc (&if_info))
1176         goto success;
1177       if (noce_try_store_flag_mask (&if_info))
1178         goto success;
1179       if (HAVE_conditional_move
1180           && noce_try_cmove_arith (&if_info))
1181         goto success;
1182     }
1183
1184   return FALSE;
1185
1186  success:
1187   /* The original sets may now be killed.  */
1188   if (insn_a == then_bb->end)
1189     then_bb->end = PREV_INSN (insn_a);
1190   flow_delete_insn (insn_a);
1191
1192   /* Several special cases here: First, we may have reused insn_b above,
1193      in which case insn_b is now NULL.  Second, we want to delete insn_b
1194      if it came from the ELSE block, because follows the now correct
1195      write that appears in the TEST block.  However, if we got insn_b from
1196      the TEST block, it may in fact be loading data needed for the comparison.
1197      We'll let life_analysis remove the insn if it's really dead.  */
1198   if (insn_b && else_bb)
1199     {
1200       if (insn_b == else_bb->end)
1201         else_bb->end = PREV_INSN (insn_b);
1202       flow_delete_insn (insn_b);
1203     }
1204
1205   /* The new insns will have been inserted before cond_earliest.  We should
1206      be able to remove the jump with impunity, but the condition itself may
1207      have been modified by gcse to be shared across basic blocks.  */
1208   test_bb->end = PREV_INSN (jump);
1209   flow_delete_insn (jump);
1210
1211   /* If we used a temporary, fix it up now.  */
1212   if (orig_x != x)
1213     {
1214       start_sequence ();
1215       emit_move_insn (orig_x, x);
1216       insn_b = gen_sequence ();
1217       end_sequence ();
1218
1219       test_bb->end = emit_insn_after (insn_b, test_bb->end);
1220     }
1221
1222   /* Merge the blocks!  */
1223   merge_if_block (test_bb, then_bb, else_bb, join_bb);
1224
1225   return TRUE;
1226 }
1227 \f
1228 /* Attempt to convert an IF-THEN or IF-THEN-ELSE block into
1229    straight line code.  Return true if successful.  */
1230
1231 static int
1232 process_if_block (test_bb, then_bb, else_bb, join_bb)
1233      basic_block test_bb;       /* Basic block test is in */
1234      basic_block then_bb;       /* Basic block for THEN block */
1235      basic_block else_bb;       /* Basic block for ELSE block */
1236      basic_block join_bb;       /* Basic block the join label is in */
1237 {
1238   if (! reload_completed
1239       && noce_process_if_block (test_bb, then_bb, else_bb, join_bb))
1240     return TRUE;
1241
1242   if (HAVE_conditional_execution
1243       && reload_completed
1244       && cond_exec_process_if_block (test_bb, then_bb, else_bb, join_bb))
1245     return TRUE;
1246
1247   return FALSE;
1248 }
1249
1250 /* Merge the blocks and mark for local life update.  */
1251
1252 static void
1253 merge_if_block (test_bb, then_bb, else_bb, join_bb)
1254      basic_block test_bb;       /* Basic block test is in */
1255      basic_block then_bb;       /* Basic block for THEN block */
1256      basic_block else_bb;       /* Basic block for ELSE block */
1257      basic_block join_bb;       /* Basic block the join label is in */
1258 {
1259   basic_block combo_bb;
1260
1261   /* All block merging is done into the lower block numbers.  */
1262
1263   combo_bb = test_bb;
1264
1265   /* First merge TEST block into THEN block.  This is a no-brainer since
1266      the THEN block did not have a code label to begin with.  */
1267
1268   if (combo_bb->global_live_at_end)
1269     COPY_REG_SET (combo_bb->global_live_at_end, then_bb->global_live_at_end);
1270   merge_blocks_nomove (combo_bb, then_bb);
1271   num_removed_blocks++;
1272
1273   /* The ELSE block, if it existed, had a label.  That label count
1274      will almost always be zero, but odd things can happen when labels
1275      get their addresses taken.  */
1276   if (else_bb)
1277     {
1278       merge_blocks_nomove (combo_bb, else_bb);
1279       num_removed_blocks++;
1280     }
1281
1282   /* If there was no join block reported, that means it was not adjacent
1283      to the others, and so we cannot merge them.  */
1284
1285   if (! join_bb)
1286     {
1287       /* The outgoing edge for the current COMBO block should already
1288          be correct.  Verify this.  */
1289       if (combo_bb->succ == NULL_EDGE)
1290         abort ();
1291
1292       /* There should sill be a branch at the end of the THEN or ELSE
1293          blocks taking us to our final destination.  */
1294       if (! simplejump_p (combo_bb->end)
1295           && ! returnjump_p (combo_bb->end))
1296         abort ();
1297     }
1298
1299   /* The JOIN block may have had quite a number of other predecessors too.
1300      Since we've already merged the TEST, THEN and ELSE blocks, we should
1301      have only one remaining edge from our if-then-else diamond.  If there
1302      is more than one remaining edge, it must come from elsewhere.  */
1303   else if (join_bb->pred->pred_next == NULL)
1304     {
1305       /* We can merge the JOIN.  */
1306       if (combo_bb->global_live_at_end)
1307         COPY_REG_SET (combo_bb->global_live_at_end,
1308                       join_bb->global_live_at_end);
1309       merge_blocks_nomove (combo_bb, join_bb);
1310       num_removed_blocks++;
1311     }
1312   else
1313     {
1314       /* We cannot merge the JOIN.  */
1315
1316       /* The outgoing edge for the current COMBO block should already
1317          be correct.  Verify this.  */
1318       if (combo_bb->succ->succ_next != NULL_EDGE
1319           || combo_bb->succ->dest != join_bb)
1320         abort ();
1321
1322       /* Remove the jump and cruft from the end of the COMBO block.  */
1323       tidy_fallthru_edge (combo_bb->succ, combo_bb, join_bb);
1324     }
1325
1326   /* Make sure we update life info properly.  */
1327   SET_UPDATE_LIFE (combo_bb);
1328
1329   num_updated_if_blocks++;
1330 }
1331 \f
1332 /* Find a block ending in a simple IF condition.  Return TRUE if
1333    we were able to transform it in some way.  */
1334
1335 static int
1336 find_if_header (test_bb)
1337      basic_block test_bb;
1338 {
1339   edge then_edge;
1340   edge else_edge;
1341
1342   /* The kind of block we're looking for has exactly two successors.  */
1343   if ((then_edge = test_bb->succ) == NULL_EDGE
1344       || (else_edge = then_edge->succ_next) == NULL_EDGE
1345       || else_edge->succ_next != NULL_EDGE)
1346     return FALSE;
1347
1348   /* Neither edge should be abnormal.  */
1349   if ((then_edge->flags & EDGE_COMPLEX)
1350       || (else_edge->flags & EDGE_COMPLEX))
1351     return FALSE;
1352
1353   /* The THEN edge is canonically the one that falls through.  */
1354   if (then_edge->flags & EDGE_FALLTHRU)
1355     ;
1356   else if (else_edge->flags & EDGE_FALLTHRU)
1357     {
1358       edge e = else_edge;
1359       else_edge = then_edge;
1360       then_edge = e;
1361     }
1362   else
1363     /* Otherwise this must be a multiway branch of some sort.  */
1364     return FALSE;
1365
1366   if (find_if_block (test_bb, then_edge, else_edge))
1367     goto success;
1368   if (post_dominators
1369       && (! HAVE_conditional_execution || reload_completed))
1370     {
1371       if (find_if_case_1 (test_bb, then_edge, else_edge))
1372         goto success;
1373       if (find_if_case_2 (test_bb, then_edge, else_edge))
1374         goto success;
1375     }
1376
1377   return FALSE;
1378
1379  success:
1380   if (rtl_dump_file)
1381     fprintf (rtl_dump_file, "Conversion succeeded.\n");
1382   return TRUE;
1383 }
1384
1385 /* Determine if a given basic block heads a simple IF-THEN or IF-THEN-ELSE
1386    block.  If so, we'll try to convert the insns to not require the branch.
1387    Return TRUE if we were successful at converting the the block.  */
1388
1389 static int
1390 find_if_block (test_bb, then_edge, else_edge)
1391       basic_block test_bb;
1392       edge then_edge, else_edge;
1393 {
1394   basic_block then_bb = then_edge->dest;
1395   basic_block else_bb = else_edge->dest;
1396   basic_block join_bb = NULL_BLOCK;
1397   edge then_succ = then_bb->succ;
1398   edge else_succ = else_bb->succ;
1399   int next_index;
1400
1401   /* The THEN block of an IF-THEN combo must have exactly one predecessor.  */
1402   if (then_bb->pred->pred_next != NULL_EDGE)
1403     return FALSE;
1404
1405   /* The THEN block of an IF-THEN combo must have exactly one successor.  */
1406   if (then_succ == NULL_EDGE
1407       || then_succ->succ_next != NULL_EDGE
1408       || (then_succ->flags & EDGE_COMPLEX))
1409     return FALSE;
1410
1411   /* If the THEN block's successor is the other edge out of the TEST block,
1412      then we have an IF-THEN combo without an ELSE.  */
1413   if (then_succ->dest == else_bb)
1414     {
1415       join_bb = else_bb;
1416       else_bb = NULL_BLOCK;
1417     }
1418
1419   /* If the THEN and ELSE block meet in a subsequent block, and the ELSE
1420      has exactly one predecessor and one successor, and the outgoing edge
1421      is not complex, then we have an IF-THEN-ELSE combo.  */
1422   else if (else_succ != NULL_EDGE
1423            && then_succ->dest == else_succ->dest
1424            && else_bb->pred->pred_next == NULL_EDGE
1425            && else_succ->succ_next == NULL_EDGE
1426            && ! (else_succ->flags & EDGE_COMPLEX))
1427     join_bb = else_succ->dest;
1428
1429   /* Otherwise it is not an IF-THEN or IF-THEN-ELSE combination.  */
1430   else
1431     return FALSE;          
1432
1433   num_possible_if_blocks++;
1434
1435   if (rtl_dump_file)
1436     {
1437       if (else_bb)
1438         fprintf (rtl_dump_file,
1439                  "\nIF-THEN-ELSE block found, start %d, then %d, else %d, join %d\n",
1440                  test_bb->index, then_bb->index, else_bb->index,
1441                  join_bb->index);
1442       else
1443         fprintf (rtl_dump_file,
1444                  "\nIF-THEN block found, start %d, then %d, join %d\n",
1445                  test_bb->index, then_bb->index, join_bb->index);
1446     }
1447
1448   /* Make sure IF, THEN, and ELSE, blocks are adjacent.  Actually, we
1449      get the first condition for free, since we've already asserted that
1450      there's a fallthru edge from IF to THEN.  */
1451   /* ??? As an enhancement, move the ELSE block.  Have to deal with EH and
1452      BLOCK notes, if by no other means than aborting the merge if they
1453      exist.  Sticky enough I don't want to think about it now.  */
1454   next_index = then_bb->index;
1455   if (else_bb && ++next_index != else_bb->index)
1456     return FALSE;
1457   if (++next_index != join_bb->index)
1458     {
1459       if (else_bb)
1460         join_bb = NULL;
1461       else
1462         return FALSE;
1463     }
1464
1465   /* Do the real work.  */
1466   return process_if_block (test_bb, then_bb, else_bb, join_bb);
1467 }
1468
1469 /* Look for IF-THEN-ELSE cases in which one of THEN or ELSE is
1470    transformable, but not necessarily the other.  There need be no
1471    JOIN block.
1472
1473    Return TRUE if we were successful at converting the the block.
1474
1475    Cases we'd like to look at:
1476
1477    (1)
1478         if (test) goto over; // x not live
1479         x = a;
1480         goto label;
1481         over:
1482
1483    becomes
1484
1485         x = a;
1486         if (! test) goto label;
1487
1488    (2)
1489         if (test) goto E; // x not live
1490         x = big();
1491         goto L;
1492         E:
1493         x = b;
1494         goto M;
1495
1496    becomes
1497
1498         x = b;
1499         if (test) goto M;
1500         x = big();
1501         goto L;
1502
1503    (3) // This one's really only interesting for targets that can do
1504        // multiway branching, e.g. IA-64 BBB bundles.  For other targets
1505        // it results in multiple branches on a cache line, which often
1506        // does not sit well with predictors.
1507
1508         if (test1) goto E; // predicted not taken
1509         x = a;
1510         if (test2) goto F;
1511         ...
1512         E:
1513         x = b;
1514         J:
1515
1516    becomes
1517
1518         x = a;
1519         if (test1) goto E;
1520         if (test2) goto F;
1521
1522    Notes:
1523
1524    (A) Don't do (2) if the branch is predicted against the block we're
1525    eliminating.  Do it anyway if we can eliminate a branch; this requires
1526    that the sole successor of the eliminated block postdominate the other
1527    side of the if.
1528
1529    (B) With CE, on (3) we can steal from both sides of the if, creating
1530
1531         if (test1) x = a;
1532         if (!test1) x = b;
1533         if (test1) goto J;
1534         if (test2) goto F;
1535         ...
1536         J:
1537
1538    Again, this is most useful if J postdominates.
1539
1540    (C) CE substitutes for helpful life information.
1541
1542    (D) These heuristics need a lot of work.  */
1543
1544 /* Tests for case 1 above.  */
1545
1546 static int
1547 find_if_case_1 (test_bb, then_edge, else_edge)
1548       basic_block test_bb;
1549       edge then_edge, else_edge;
1550 {
1551   basic_block then_bb = then_edge->dest;
1552   basic_block else_bb = else_edge->dest;
1553   edge then_succ = then_bb->succ;
1554   rtx new_lab;
1555
1556   /* THEN has one successor.  */
1557   if (!then_succ || then_succ->succ_next != NULL)
1558     return FALSE;
1559
1560   /* THEN does not fall through, but is not strange either.  */
1561   if (then_succ->flags & (EDGE_COMPLEX | EDGE_FALLTHRU))
1562     return FALSE;
1563
1564   /* THEN has one predecessor.  */
1565   if (then_bb->pred->pred_next != NULL)
1566     return FALSE;
1567
1568   /* ELSE follows THEN.  (??? could be moved)  */
1569   if (else_bb->index != then_bb->index + 1)
1570     return FALSE;
1571
1572   num_possible_if_blocks++;
1573   if (rtl_dump_file)
1574     fprintf (rtl_dump_file,
1575              "\nIF-CASE-1 found, start %d, then %d\n",
1576              test_bb->index, then_bb->index);
1577
1578   /* THEN is small.  */
1579   if (count_bb_insns (then_bb) > BRANCH_COST)
1580     return FALSE;
1581
1582   /* Find the label for THEN's destination.  */
1583   if (then_succ->dest == EXIT_BLOCK_PTR)
1584     new_lab = NULL_RTX;
1585   else
1586     {
1587       new_lab = JUMP_LABEL (then_bb->end);
1588       if (! new_lab)
1589         abort ();
1590     }
1591
1592   /* Registers set are dead, or are predicable.  */
1593   if (! dead_or_predicable (test_bb, then_bb, else_bb, new_lab, 1))
1594     return FALSE;
1595
1596   /* Conversion went ok, including moving the insns and fixing up the
1597      jump.  Adjust the CFG to match.  */
1598
1599   SET_UPDATE_LIFE (test_bb);
1600   bitmap_operation (test_bb->global_live_at_end,
1601                     else_bb->global_live_at_start,
1602                     then_bb->global_live_at_end, BITMAP_IOR);
1603   
1604   make_edge (NULL, test_bb, then_succ->dest, 0);
1605   flow_delete_block (then_bb);
1606   tidy_fallthru_edge (else_edge, test_bb, else_bb);
1607
1608   num_removed_blocks++;
1609   num_updated_if_blocks++;
1610
1611   return TRUE;
1612 }
1613
1614 /* Test for case 2 above.  */
1615
1616 static int
1617 find_if_case_2 (test_bb, then_edge, else_edge)
1618       basic_block test_bb;
1619       edge then_edge, else_edge;
1620 {
1621   basic_block then_bb = then_edge->dest;
1622   basic_block else_bb = else_edge->dest;
1623   edge else_succ = else_bb->succ;
1624   rtx new_lab, note;
1625
1626   /* ELSE has one successor.  */
1627   if (!else_succ || else_succ->succ_next != NULL)
1628     return FALSE;
1629
1630   /* ELSE outgoing edge is not complex.  */
1631   if (else_succ->flags & EDGE_COMPLEX)
1632     return FALSE;
1633
1634   /* ELSE has one predecessor.  */
1635   if (else_bb->pred->pred_next != NULL)
1636     return FALSE;
1637
1638   /* ELSE is predicted or SUCC(ELSE) postdominates THEN.  */
1639   note = find_reg_note (test_bb->end, REG_BR_PROB, NULL_RTX);
1640   if (note && INTVAL (XEXP (note, 0)) >= REG_BR_PROB_BASE / 2)
1641     ;
1642   else if (else_succ->dest->index < 0
1643            || (then_bb->index >= 0
1644                && TEST_BIT (post_dominators[ORIG_INDEX (then_bb)], 
1645                             ORIG_INDEX (else_succ->dest))))
1646     ;
1647   else
1648     return FALSE;
1649
1650   num_possible_if_blocks++;
1651   if (rtl_dump_file)
1652     fprintf (rtl_dump_file,
1653              "\nIF-CASE-2 found, start %d, else %d\n",
1654              test_bb->index, else_bb->index);
1655
1656   /* ELSE is small.  */
1657   if (count_bb_insns (then_bb) > BRANCH_COST)
1658     return FALSE;
1659
1660   /* Find the label for ELSE's destination.  */
1661   if (else_succ->dest == EXIT_BLOCK_PTR)
1662     new_lab = NULL_RTX;
1663   else
1664     {
1665       if (else_succ->flags & EDGE_FALLTHRU)
1666         {
1667           new_lab = else_succ->dest->head;
1668           if (GET_CODE (new_lab) != CODE_LABEL)
1669             abort ();
1670         }
1671       else
1672         {
1673           new_lab = JUMP_LABEL (else_bb->end);
1674           if (! new_lab)
1675             abort ();
1676         }
1677     }
1678
1679   /* Registers set are dead, or are predicable.  */
1680   if (! dead_or_predicable (test_bb, else_bb, then_bb, new_lab, 0))
1681     return FALSE;
1682
1683   /* Conversion went ok, including moving the insns and fixing up the
1684      jump.  Adjust the CFG to match.  */
1685
1686   SET_UPDATE_LIFE (test_bb);
1687   bitmap_operation (test_bb->global_live_at_end,
1688                     then_bb->global_live_at_start,
1689                     else_bb->global_live_at_end, BITMAP_IOR);
1690   
1691   remove_edge (else_edge);
1692   make_edge (NULL, test_bb, else_succ->dest, 0);
1693   flow_delete_block (else_bb);
1694
1695   num_removed_blocks++;
1696   num_updated_if_blocks++;
1697
1698   /* ??? We may now fallthru from one of THEN's successors into a join
1699      block.  Rerun cleanup_cfg?  Examine things manually?  Wait?  */
1700
1701   return TRUE;
1702 }
1703
1704 /* A subroutine of dead_or_predicable called through for_each_rtx.
1705    Return 1 if a memory is found.  */
1706
1707 static int
1708 find_memory (px, data)
1709      rtx *px;
1710      void *data ATTRIBUTE_UNUSED;
1711 {
1712   return GET_CODE (*px) == MEM;
1713 }
1714
1715 /* Used by the code above to perform the actual rtl transformations.
1716    Return TRUE if successful.
1717
1718    TEST_BB is the block containing the conditional branch.  MERGE_BB
1719    is the block containing the code to manipulate.  NEW_DEST is the
1720    label TEST_BB should be branching to after the conversion.
1721    REVERSEP is true if the sense of the branch should be reversed.  */
1722
1723 static int
1724 dead_or_predicable (test_bb, merge_bb, other_bb, new_dest, reversep)
1725      basic_block test_bb, merge_bb, other_bb;
1726      rtx new_dest;
1727      int reversep;
1728 {
1729   rtx head, end, jump, earliest, old_dest;
1730
1731   jump = test_bb->end;
1732
1733   /* Find the extent of the real code in the merge block.  */
1734   head = merge_bb->head;
1735   end = merge_bb->end;
1736
1737   if (GET_CODE (head) == CODE_LABEL)
1738     head = NEXT_INSN (head);
1739   if (GET_CODE (head) == NOTE)
1740     {
1741       if (head == end)
1742         {
1743           head = end = NULL_RTX;
1744           goto no_body;
1745         }
1746       head = NEXT_INSN (head);
1747     }
1748
1749   if (GET_CODE (end) == JUMP_INSN)
1750     {
1751       if (head == end)
1752         {
1753           head = end = NULL_RTX;
1754           goto no_body;
1755         }
1756       end = PREV_INSN (end);
1757     }
1758
1759   if (HAVE_conditional_execution)
1760     {
1761       /* In the conditional execution case, we have things easy.  We know
1762          the condition is reversable.  We don't have to check life info,
1763          becase we're going to conditionally execute the code anyway.
1764          All that's left is making sure the insns involved can actually
1765          be predicated.  */
1766
1767       rtx cond, prob_val;
1768
1769       cond = cond_exec_get_condition (jump);
1770
1771       prob_val = find_reg_note (jump, REG_BR_PROB, NULL_RTX);
1772       if (prob_val)
1773         prob_val = XEXP (prob_val, 0);
1774
1775       if (reversep)
1776         {
1777           cond = gen_rtx_fmt_ee (reverse_condition (GET_CODE (cond)),
1778                                  GET_MODE (cond), XEXP (cond, 0),
1779                                  XEXP (cond, 1));
1780           if (prob_val)
1781             prob_val = GEN_INT (REG_BR_PROB_BASE - INTVAL (prob_val));
1782         }
1783
1784       if (! cond_exec_process_insns (head, end, cond, prob_val, 0))
1785         goto cancel;
1786
1787       earliest = jump;
1788     }
1789   else
1790     {
1791       /* In the non-conditional execution case, we have to verify that there
1792          are no trapping operations, no calls, no references to memory, and
1793          that any registers modified are dead at the branch site.  */
1794
1795       rtx insn, cond, prev;
1796       regset_head merge_set_head, tmp_head, test_live_head, test_set_head;
1797       regset merge_set, tmp, test_live, test_set;
1798       struct propagate_block_info *pbi;
1799       int i, fail = 0;
1800
1801       /* Check for no calls or trapping operations.  */
1802       for (insn = head; ; insn = NEXT_INSN (insn))
1803         {
1804           if (GET_CODE (insn) == CALL_INSN)
1805             return FALSE;
1806           if (INSN_P (insn))
1807             {
1808               if (may_trap_p (PATTERN (insn)))
1809                 return FALSE;
1810
1811               /* ??? Even non-trapping memories such as stack frame
1812                  references must be avoided.  For stores, we collect
1813                  no lifetime info; for reads, we'd have to assert
1814                  true_dependance false against every store in the
1815                  TEST range.  */
1816               if (for_each_rtx (&PATTERN (insn), find_memory, NULL))
1817                 return FALSE;
1818             }
1819           if (insn == end)
1820             break;
1821         }
1822
1823       if (! condjump_p (jump))
1824         return FALSE;
1825
1826       /* Find the extent of the conditional.  */
1827       cond = noce_get_condition (jump, &earliest);
1828       if (! cond)
1829         return FALSE;
1830
1831       /* Collect:
1832            MERGE_SET = set of registers set in MERGE_BB
1833            TEST_LIVE = set of registers live at EARLIEST
1834            TEST_SET  = set of registers set between EARLIEST and the
1835                        end of the block.  */
1836
1837       tmp = INITIALIZE_REG_SET (tmp_head);
1838       merge_set = INITIALIZE_REG_SET (merge_set_head);
1839       test_live = INITIALIZE_REG_SET (test_live_head);
1840       test_set = INITIALIZE_REG_SET (test_set_head);
1841
1842       /* ??? bb->local_set is only valid during calculate_global_regs_live,
1843          so we must recompute usage for MERGE_BB.  Not so bad, I suppose, 
1844          since we've already asserted that MERGE_BB is small.  */
1845       propagate_block (merge_bb, tmp, merge_set, 0);
1846
1847       /* For small register class machines, don't lengthen lifetimes of
1848          hard registers before reload.  */
1849       if (SMALL_REGISTER_CLASSES && ! reload_completed)
1850         {
1851           EXECUTE_IF_SET_IN_BITMAP
1852             (merge_set, 0, i,
1853              {
1854                if (i < FIRST_PSEUDO_REGISTER
1855                    && ! fixed_regs[i]
1856                    && ! global_regs[i])
1857                 fail = 1;
1858              });
1859         }
1860
1861       /* For TEST, we're interested in a range of insns, not a whole block.
1862          Moreover, we're interested in the insns live from OTHER_BB.  */
1863
1864       COPY_REG_SET (test_live, other_bb->global_live_at_start);
1865       pbi = init_propagate_block_info (test_bb, test_live, test_set, 0);
1866
1867       for (insn = jump; ; insn = prev)
1868         {
1869           prev = propagate_one_insn (pbi, insn);
1870           if (insn == earliest)
1871             break;
1872         }
1873
1874       free_propagate_block_info (pbi);
1875
1876       /* We can perform the transformation if
1877            MERGE_SET & (TEST_SET | TEST_LIVE)
1878          and
1879            TEST_SET & merge_bb->global_live_at_start
1880          are empty.  */
1881
1882       bitmap_operation (tmp, test_set, test_live, BITMAP_IOR);
1883       bitmap_operation (tmp, tmp, merge_set, BITMAP_AND);
1884       EXECUTE_IF_SET_IN_BITMAP(tmp, 0, i, fail = 1);
1885
1886       bitmap_operation (tmp, test_set, merge_bb->global_live_at_start,
1887                         BITMAP_AND);
1888       EXECUTE_IF_SET_IN_BITMAP(tmp, 0, i, fail = 1);
1889
1890       FREE_REG_SET (tmp);
1891       FREE_REG_SET (merge_set);
1892       FREE_REG_SET (test_live);
1893       FREE_REG_SET (test_set);
1894
1895       if (fail)
1896         return FALSE;
1897     }
1898
1899  no_body:
1900   /* We don't want to use normal invert_jump or redirect_jump because
1901      we don't want to delete_insn called.  Also, we want to do our own
1902      change group management.  */
1903
1904   old_dest = JUMP_LABEL (jump);
1905   if (reversep
1906       ? ! invert_jump_1 (jump, new_dest)
1907       : ! redirect_jump_1 (jump, new_dest))
1908     goto cancel;
1909
1910   if (! apply_change_group ())
1911     return FALSE;
1912
1913   if (old_dest)
1914     LABEL_NUSES (old_dest) -= 1;
1915   if (new_dest)
1916     LABEL_NUSES (new_dest) += 1;
1917   JUMP_LABEL (jump) = new_dest;
1918
1919   if (reversep)
1920     {
1921       rtx note = find_reg_note (jump, REG_BR_PROB, NULL_RTX);
1922       if (note)
1923         XEXP (note, 0) = GEN_INT (REG_BR_PROB_BASE - INTVAL (XEXP (note, 0)));
1924     }
1925
1926   /* Move the insns out of MERGE_BB to before the branch.  */
1927   if (head != NULL)
1928     {
1929       if (end == merge_bb->end)
1930         merge_bb->end = PREV_INSN (head);
1931
1932       head = squeeze_notes (head, end);
1933       if (GET_CODE (end) == NOTE
1934           && (NOTE_LINE_NUMBER (end) == NOTE_INSN_BLOCK_END
1935               || NOTE_LINE_NUMBER (end) == NOTE_INSN_BLOCK_BEG
1936               || NOTE_LINE_NUMBER (end) == NOTE_INSN_LOOP_BEG
1937               || NOTE_LINE_NUMBER (end) == NOTE_INSN_LOOP_END
1938               || NOTE_LINE_NUMBER (end) == NOTE_INSN_LOOP_CONT
1939               || NOTE_LINE_NUMBER (end) == NOTE_INSN_LOOP_VTOP))
1940         {
1941           if (head == end)
1942             return TRUE;
1943           end = PREV_INSN (end);
1944         }
1945
1946       reorder_insns (head, end, PREV_INSN (earliest));
1947     }
1948   return TRUE;
1949
1950  cancel:
1951   cancel_changes (0);
1952   return FALSE;
1953 }
1954 \f
1955 /* Main entry point for all if-conversion.  */
1956
1957 void
1958 if_convert (life_data_ok)
1959      int life_data_ok;
1960 {
1961   int block_num;
1962
1963   num_possible_if_blocks = 0;
1964   num_updated_if_blocks = 0;
1965   num_removed_blocks = 0;
1966
1967   /* Free up basic_block_for_insn so that we don't have to keep it 
1968      up to date, either here or in merge_blocks_nomove.  */
1969   free_basic_block_vars (1);
1970
1971   /* Compute postdominators if we think we'll use them.  */
1972   post_dominators = NULL;
1973   if (HAVE_conditional_execution || life_data_ok)
1974     {
1975       post_dominators = sbitmap_vector_alloc (n_basic_blocks, n_basic_blocks);
1976       compute_flow_dominators (NULL, post_dominators);
1977     }
1978
1979   /* Record initial block numbers.  */
1980   for (block_num = 0; block_num < n_basic_blocks; block_num++)
1981     SET_ORIG_INDEX (BASIC_BLOCK (block_num), block_num);
1982
1983   /* Go through each of the basic blocks looking for things to convert.  */
1984   for (block_num = 0; block_num < n_basic_blocks; )
1985     {
1986       basic_block bb = BASIC_BLOCK (block_num);
1987       if (find_if_header (bb))
1988         block_num = bb->index;
1989       else 
1990         block_num++;
1991     }
1992
1993   sbitmap_vector_free (post_dominators);
1994
1995   if (rtl_dump_file)
1996     fflush (rtl_dump_file);
1997
1998   /* Rebuild basic_block_for_insn for update_life_info and for gcse.  */
1999   compute_bb_for_insn (get_max_uid ());
2000
2001   /* Rebuild life info for basic blocks that require it.  */
2002   if (num_removed_blocks && life_data_ok)
2003     {
2004       sbitmap update_life_blocks = sbitmap_alloc (n_basic_blocks);
2005       sbitmap_zero (update_life_blocks);
2006
2007       /* If we allocated new pseudos, we must resize the array for sched1.  */
2008       if (max_regno < max_reg_num ())
2009         {
2010           max_regno = max_reg_num ();
2011           allocate_reg_info (max_regno, FALSE, FALSE);
2012         }
2013
2014       for (block_num = 0; block_num < n_basic_blocks; block_num++)
2015         if (UPDATE_LIFE (BASIC_BLOCK (block_num)))
2016           SET_BIT (update_life_blocks, block_num);
2017
2018       count_or_remove_death_notes (update_life_blocks, 1);
2019       update_life_info (update_life_blocks, UPDATE_LIFE_LOCAL,
2020                         PROP_DEATH_NOTES);
2021
2022       sbitmap_free (update_life_blocks);
2023     }
2024
2025   /* Write the final stats.  */
2026   if (rtl_dump_file && num_possible_if_blocks > 0)
2027     {
2028       fprintf (rtl_dump_file,
2029                "\n%d possible IF blocks searched.\n",
2030                num_possible_if_blocks);
2031       fprintf (rtl_dump_file,
2032                "%d IF blocks converted.\n",
2033                num_updated_if_blocks);
2034       fprintf (rtl_dump_file,
2035                "%d basic blocks deleted.\n\n\n",
2036                num_removed_blocks);
2037     }
2038
2039 #ifdef ENABLE_CHECKING
2040   verify_flow_info ();
2041 #endif
2042 }