OSDN Git Service

* config/i386/i386.md ("*sse_prologue_save_insn"): Use braced output
[pf3gnuchains/gcc-fork.git] / gcc / tree-ssa-ifcombine.c
1 /* Combining of if-expressions on trees.
2    Copyright (C) 2007 Free Software Foundation, Inc.
3    Contributed by Richard Guenther <rguenther@suse.de>
4
5 This file is part of GCC.
6
7 GCC is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3, or (at your option)
10 any later version.
11
12 GCC is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 GNU General Public 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 COPYING3.  If not see
19 <http://www.gnu.org/licenses/>.  */
20
21 #include "config.h"
22 #include "system.h"
23 #include "coretypes.h"
24 #include "tm.h"
25 #include "tree.h"
26 #include "basic-block.h"
27 #include "timevar.h"
28 #include "diagnostic.h"
29 #include "tree-flow.h"
30 #include "tree-pass.h"
31 #include "tree-dump.h"
32
33 /* This pass combines COND_EXPRs to simplify control flow.  It
34    currently recognizes bit tests and comparisons in chains that
35    represent logical and or logical or of two COND_EXPRs.
36
37    It does so by walking basic blocks in a approximate reverse
38    post-dominator order and trying to match CFG patterns that
39    represent logical and or logical or of two COND_EXPRs.
40    Transformations are done if the COND_EXPR conditions match
41    either
42
43      1. two single bit tests X & (1 << Yn) (for logical and)
44
45      2. two bit tests X & Yn (for logical or)
46
47      3. two comparisons X OPn Y (for logical or)
48
49    To simplify this pass, removing basic blocks and dead code
50    is left to CFG cleanup and DCE.  */
51
52
53 /* Recognize a if-then-else CFG pattern starting to match with the
54    COND_BB basic-block containing the COND_EXPR.  The recognized
55    then end else blocks are stored to *THEN_BB and *ELSE_BB.  If
56    *THEN_BB and/or *ELSE_BB are already set, they are required to
57    match the then and else basic-blocks to make the pattern match.
58    Returns true if the pattern matched, false otherwise.  */
59
60 static bool
61 recognize_if_then_else (basic_block cond_bb,
62                         basic_block *then_bb, basic_block *else_bb)
63 {
64   edge t, e;
65
66   if (EDGE_COUNT (cond_bb->succs) != 2)
67     return false;
68
69   /* Find the then/else edges.  */
70   t = EDGE_SUCC (cond_bb, 0);
71   e = EDGE_SUCC (cond_bb, 1);
72   if (!(t->flags & EDGE_TRUE_VALUE))
73     {
74       edge tmp = t;
75       t = e;
76       e = tmp;
77     }
78   if (!(t->flags & EDGE_TRUE_VALUE)
79       || !(e->flags & EDGE_FALSE_VALUE))
80     return false;
81
82   /* Check if the edge destinations point to the required block.  */
83   if (*then_bb
84       && t->dest != *then_bb)
85     return false;
86   if (*else_bb
87       && e->dest != *else_bb)
88     return false;
89
90   if (!*then_bb)
91     *then_bb = t->dest;
92   if (!*else_bb)
93     *else_bb = e->dest;
94
95   return true;
96 }
97
98 /* Verify if the basic block BB does not have side-effects.  Return
99    true in this case, else false.  */
100
101 static bool
102 bb_no_side_effects_p (basic_block bb)
103 {
104   block_stmt_iterator bsi;
105
106   for (bsi = bsi_start (bb); !bsi_end_p (bsi); bsi_next (&bsi))
107     {
108       tree stmt = bsi_stmt (bsi);
109       stmt_ann_t ann = stmt_ann (stmt);
110
111       if (ann->has_volatile_ops
112           || !ZERO_SSA_OPERANDS (stmt, SSA_OP_ALL_VIRTUALS))
113         return false;
114     }
115
116   return true;
117 }
118
119 /* Verify if all PHI node arguments in DEST for edges from BB1 or
120    BB2 to DEST are the same.  This makes the CFG merge point
121    free from side-effects.  Return true in this case, else false.  */
122
123 static bool
124 same_phi_args_p (basic_block bb1, basic_block bb2, basic_block dest)
125 {
126   edge e1 = find_edge (bb1, dest);
127   edge e2 = find_edge (bb2, dest);
128   tree phi;
129
130   for (phi = phi_nodes (dest); phi; phi = PHI_CHAIN (phi))
131     if (!operand_equal_p (PHI_ARG_DEF_FROM_EDGE (phi, e1),
132                           PHI_ARG_DEF_FROM_EDGE (phi, e2), 0))
133       return false;
134
135   return true;
136 }
137
138 /* Recognize a single bit test pattern in COND_EXPR and its defining
139    statements.  Store the name being tested in *NAME and the bit
140    in *BIT.  The COND_EXPR computes *NAME & (1 << *BIT).
141    Returns true if the pattern matched, false otherwise.  */
142
143 static bool
144 recognize_single_bit_test (tree cond_expr, tree *name, tree *bit)
145 {
146   tree t;
147
148   /* Get at the definition of the result of the bit test.  */
149   t = TREE_OPERAND (cond_expr, 0);
150   if (TREE_CODE (t) == NE_EXPR
151       && integer_zerop (TREE_OPERAND (t, 1)))
152     t = TREE_OPERAND (t, 0);
153   if (TREE_CODE (t) != SSA_NAME)
154     return false;
155   t = SSA_NAME_DEF_STMT (t);
156   if (TREE_CODE (t) != GIMPLE_MODIFY_STMT)
157     return false;
158   t = GIMPLE_STMT_OPERAND (t, 1);
159
160   /* Look at which bit is tested.  One form to recognize is
161      D.1985_5 = state_3(D) >> control1_4(D);
162      D.1986_6 = (int) D.1985_5;
163      D.1987_7 = op0 & 1;
164      if (D.1987_7 != 0)  */
165   if (TREE_CODE (t) == BIT_AND_EXPR
166       && integer_onep (TREE_OPERAND (t, 1))
167       && TREE_CODE (TREE_OPERAND (t, 0)) == SSA_NAME)
168     {
169       tree orig_name = TREE_OPERAND (t, 0);
170
171       /* Look through copies and conversions to eventually
172          find the stmt that computes the shift.  */
173       t = orig_name;
174       do {
175         t = SSA_NAME_DEF_STMT (t);
176         if (TREE_CODE (t) != GIMPLE_MODIFY_STMT)
177           break;
178         t = GIMPLE_STMT_OPERAND (t, 1);
179         if (TREE_CODE (t) == NOP_EXPR
180             || TREE_CODE (t) == CONVERT_EXPR)
181           t = TREE_OPERAND (t, 0);
182       } while (TREE_CODE (t) == SSA_NAME);
183
184       /* If we found such, decompose it.  */
185       if (TREE_CODE (t) == RSHIFT_EXPR)
186         {
187           /* op0 & (1 << op1) */
188           *bit = TREE_OPERAND (t, 1);
189           *name = TREE_OPERAND (t, 0);
190         }
191       else
192         {
193           /* t & 1 */
194           *bit = integer_zero_node;
195           *name = orig_name;
196         }
197
198       return true;
199     }
200
201   /* Another form is
202      D.1987_7 = op0 & (1 << CST)
203      if (D.1987_7 != 0)  */
204   if (TREE_CODE (t) == BIT_AND_EXPR
205       && TREE_CODE (TREE_OPERAND (t, 0)) == SSA_NAME
206       && integer_pow2p (TREE_OPERAND (t, 1)))
207     {
208       *name = TREE_OPERAND (t, 0);
209       *bit = build_int_cst (integer_type_node,
210                             tree_log2 (TREE_OPERAND (t, 1)));
211       return true;
212     }
213
214   /* Another form is
215      D.1986_6 = 1 << control1_4(D)
216      D.1987_7 = op0 & D.1986_6
217      if (D.1987_7 != 0)  */
218   if (TREE_CODE (t) == BIT_AND_EXPR
219       && TREE_CODE (TREE_OPERAND (t, 0)) == SSA_NAME
220       && TREE_CODE (TREE_OPERAND (t, 1)) == SSA_NAME)
221     {
222       tree tmp;
223
224       /* Both arguments of the BIT_AND_EXPR can be the single-bit
225          specifying expression.  */
226       tmp = SSA_NAME_DEF_STMT (TREE_OPERAND (t, 0));
227       if (TREE_CODE (tmp) == GIMPLE_MODIFY_STMT
228           && TREE_CODE (GIMPLE_STMT_OPERAND (tmp, 1)) == LSHIFT_EXPR
229           && integer_onep (TREE_OPERAND (GIMPLE_STMT_OPERAND (tmp, 1), 0)))
230         {
231           *name = TREE_OPERAND (t, 1);
232           *bit = TREE_OPERAND (GIMPLE_STMT_OPERAND (tmp, 1), 1);
233           return true;
234         }
235
236       tmp = SSA_NAME_DEF_STMT (TREE_OPERAND (t, 1));
237       if (TREE_CODE (tmp) == GIMPLE_MODIFY_STMT
238           && TREE_CODE (GIMPLE_STMT_OPERAND (tmp, 1)) == LSHIFT_EXPR
239           && integer_onep (TREE_OPERAND (GIMPLE_STMT_OPERAND (tmp, 1), 0)))
240         {
241           *name = TREE_OPERAND (t, 0);
242           *bit = TREE_OPERAND (GIMPLE_STMT_OPERAND (tmp, 1), 1);
243           return true;
244         }
245     }
246
247   return false;
248 }
249
250 /* Recognize a bit test pattern in COND_EXPR and its defining
251    statements.  Store the name being tested in *NAME and the bits
252    in *BITS.  The COND_EXPR computes *NAME & *BITS.
253    Returns true if the pattern matched, false otherwise.  */
254
255 static bool
256 recognize_bits_test (tree cond_expr, tree *name, tree *bits)
257 {
258   tree t;
259
260   /* Get at the definition of the result of the bit test.  */
261   t = TREE_OPERAND (cond_expr, 0);
262   if (TREE_CODE (t) == NE_EXPR
263       && integer_zerop (TREE_OPERAND (t, 1)))
264     t = TREE_OPERAND (t, 0);
265   if (TREE_CODE (t) != SSA_NAME)
266     return false;
267   t = SSA_NAME_DEF_STMT (t);
268   if (TREE_CODE (t) != GIMPLE_MODIFY_STMT)
269     return false;
270   t = GIMPLE_STMT_OPERAND (t, 1);
271
272   if (TREE_CODE (t) != BIT_AND_EXPR)
273     return false;
274
275   *name = TREE_OPERAND (t, 0);
276   *bits = TREE_OPERAND (t, 1);
277
278   return true;
279 }
280
281 /* If-convert on a and pattern with a common else block.  The inner
282    if is specified by its INNER_COND_BB, the outer by OUTER_COND_BB.
283    Returns true if the edges to the common else basic-block were merged.  */
284
285 static bool
286 ifcombine_ifandif (basic_block inner_cond_bb, basic_block outer_cond_bb)
287 {
288   block_stmt_iterator bsi;
289   tree inner_cond, outer_cond;
290   tree name1, name2, bit1, bit2;
291
292   inner_cond = last_stmt (inner_cond_bb);
293   if (!inner_cond
294       || TREE_CODE (inner_cond) != COND_EXPR)
295     return false;
296
297   outer_cond = last_stmt (outer_cond_bb);
298   if (!outer_cond
299       || TREE_CODE (outer_cond) != COND_EXPR)
300     return false;
301
302   /* See if we test a single bit of the same name in both tests.  In
303      that case remove the outer test, merging both else edges,
304      and change the inner one to test for
305      name & (bit1 | bit2) == (bit1 | bit2).  */
306   if (recognize_single_bit_test (inner_cond, &name1, &bit1)
307       && recognize_single_bit_test (outer_cond, &name2, &bit2)
308       && name1 == name2)
309     {
310       tree t, t2;
311
312       /* Do it.  */
313       bsi = bsi_for_stmt (inner_cond);
314       t = fold_build2 (LSHIFT_EXPR, TREE_TYPE (name1),
315                        build_int_cst (TREE_TYPE (name1), 1), bit1);
316       t2 = fold_build2 (LSHIFT_EXPR, TREE_TYPE (name1),
317                         build_int_cst (TREE_TYPE (name1), 1), bit2);
318       t = fold_build2 (BIT_IOR_EXPR, TREE_TYPE (name1), t, t2);
319       t = force_gimple_operand_bsi (&bsi, t, true, NULL_TREE,
320                                     true, BSI_SAME_STMT);
321       t2 = fold_build2 (BIT_AND_EXPR, TREE_TYPE (name1), name1, t);
322       t2 = force_gimple_operand_bsi (&bsi, t2, true, NULL_TREE,
323                                      true, BSI_SAME_STMT);
324       COND_EXPR_COND (inner_cond) = fold_build2 (EQ_EXPR, boolean_type_node,
325                                                  t2, t);
326       update_stmt (inner_cond);
327
328       /* Leave CFG optimization to cfg_cleanup.  */
329       COND_EXPR_COND (outer_cond) = boolean_true_node;
330       update_stmt (outer_cond);
331
332       if (dump_file)
333         {
334           fprintf (dump_file, "optimizing double bit test to ");
335           print_generic_expr (dump_file, name1, 0);
336           fprintf (dump_file, " & T == T\nwith temporary T = (1 << ");
337           print_generic_expr (dump_file, bit1, 0);
338           fprintf (dump_file, ") | (1 << ");
339           print_generic_expr (dump_file, bit2, 0);
340           fprintf (dump_file, ")\n");
341         }
342
343       return true;
344     }
345
346   return false;
347 }
348
349 /* If-convert on a or pattern with a common then block.  The inner
350    if is specified by its INNER_COND_BB, the outer by OUTER_COND_BB.
351    Returns true, if the edges leading to the common then basic-block
352    were merged.  */
353
354 static bool
355 ifcombine_iforif (basic_block inner_cond_bb, basic_block outer_cond_bb)
356 {
357   tree inner_cond, outer_cond;
358   tree name1, name2, bits1, bits2;
359
360   inner_cond = last_stmt (inner_cond_bb);
361   if (!inner_cond
362       || TREE_CODE (inner_cond) != COND_EXPR)
363     return false;
364
365   outer_cond = last_stmt (outer_cond_bb);
366   if (!outer_cond
367       || TREE_CODE (outer_cond) != COND_EXPR)
368     return false;
369
370   /* See if we have two bit tests of the same name in both tests.
371      In that case remove the outer test and change the inner one to
372      test for name & (bits1 | bits2) != 0.  */
373   if (recognize_bits_test (inner_cond, &name1, &bits1)
374       && recognize_bits_test (outer_cond, &name2, &bits2))
375     {
376       block_stmt_iterator bsi;
377       tree t;
378
379       /* Find the common name which is bit-tested.  */
380       if (name1 == name2)
381         ;
382       else if (bits1 == bits2)
383         {
384           t = name2;
385           name2 = bits2;
386           bits2 = t;
387           t = name1;
388           name1 = bits1;
389           bits1 = t;
390         }
391       else if (name1 == bits2)
392         {
393           t = name2;
394           name2 = bits2;
395           bits2 = t;
396         }
397       else if (bits1 == name2)
398         {
399           t = name1;
400           name1 = bits1;
401           bits1 = t;
402         }
403       else
404         return false;
405
406       /* Do it.  */
407       bsi = bsi_for_stmt (inner_cond);
408       t = fold_build2 (BIT_IOR_EXPR, TREE_TYPE (name1), bits1, bits2);
409       t = force_gimple_operand_bsi (&bsi, t, true, NULL_TREE,
410                                     true, BSI_SAME_STMT);
411       t = fold_build2 (BIT_AND_EXPR, TREE_TYPE (name1), name1, t);
412       t = force_gimple_operand_bsi (&bsi, t, true, NULL_TREE,
413                                     true, BSI_SAME_STMT);
414       COND_EXPR_COND (inner_cond) = fold_build2 (NE_EXPR, boolean_type_node, t,
415                                                  build_int_cst (TREE_TYPE (t), 0));
416       update_stmt (inner_cond);
417
418       /* Leave CFG optimization to cfg_cleanup.  */
419       COND_EXPR_COND (outer_cond) = boolean_false_node;
420       update_stmt (outer_cond);
421
422       if (dump_file)
423         {
424           fprintf (dump_file, "optimizing bits or bits test to ");
425           print_generic_expr (dump_file, name1, 0);
426           fprintf (dump_file, " & T != 0\nwith temporary T = ");
427           print_generic_expr (dump_file, bits1, 0);
428           fprintf (dump_file, " | ");
429           print_generic_expr (dump_file, bits2, 0);
430           fprintf (dump_file, "\n");
431         }
432
433       return true;
434     }
435
436   /* See if we have two comparisons that we can merge into one.
437      This happens for C++ operator overloading where for example
438      GE_EXPR is implemented as GT_EXPR || EQ_EXPR.  */
439   else if (COMPARISON_CLASS_P (COND_EXPR_COND (inner_cond))
440            && COMPARISON_CLASS_P (COND_EXPR_COND (outer_cond))
441            && operand_equal_p (TREE_OPERAND (COND_EXPR_COND (inner_cond), 0),
442                                TREE_OPERAND (COND_EXPR_COND (outer_cond), 0), 0)
443            && operand_equal_p (TREE_OPERAND (COND_EXPR_COND (inner_cond), 1),
444                                TREE_OPERAND (COND_EXPR_COND (outer_cond), 1), 0))
445     {
446       tree ccond1 = COND_EXPR_COND (inner_cond);
447       tree ccond2 = COND_EXPR_COND (outer_cond);
448       enum tree_code code1 = TREE_CODE (ccond1);
449       enum tree_code code2 = TREE_CODE (ccond2);
450       enum tree_code code;
451       tree t;
452
453 #define CHK(a,b) ((code1 == a ## _EXPR && code2 == b ## _EXPR) \
454                   || (code2 == a ## _EXPR && code1 == b ## _EXPR))
455       /* Merge the two condition codes if possible.  */
456       if (code1 == code2)
457         code = code1;
458       else if (CHK (EQ, LT))
459         code = LE_EXPR;
460       else if (CHK (EQ, GT))
461         code = GE_EXPR;
462       else if (CHK (LT, LE))
463         code = LE_EXPR;
464       else if (CHK (GT, GE))
465         code = GE_EXPR;
466       else if (INTEGRAL_TYPE_P (TREE_TYPE (TREE_OPERAND (ccond1, 0)))
467                || flag_unsafe_math_optimizations)
468         {
469           if (CHK (LT, GT))
470             code = NE_EXPR;
471           else if (CHK (LT, NE))
472             code = NE_EXPR;
473           else if (CHK (GT, NE))
474             code = NE_EXPR;
475           else
476             return false;
477         }
478       /* We could check for combinations leading to trivial true/false.  */
479       else
480         return false;
481 #undef CHK
482
483       /* Do it.  */
484       t = fold_build2 (code, boolean_type_node,
485                        TREE_OPERAND (ccond2, 0), TREE_OPERAND (ccond2, 1));
486       t = canonicalize_cond_expr_cond (t);
487       if (!t)
488         return false;
489       COND_EXPR_COND (inner_cond) = t;
490       update_stmt (inner_cond);
491
492       /* Leave CFG optimization to cfg_cleanup.  */
493       COND_EXPR_COND (outer_cond) = boolean_false_node;
494       update_stmt (outer_cond);
495
496       if (dump_file)
497         {
498           fprintf (dump_file, "optimizing two comparisons to ");
499           print_generic_expr (dump_file, t, 0);
500           fprintf (dump_file, "\n");
501         }
502
503       return true;
504     }
505
506   return false;
507 }
508
509 /* Recognize a CFG pattern and dispatch to the appropriate
510    if-conversion helper.  We start with BB as the innermost
511    worker basic-block.  Returns true if a transformation was done.  */
512
513 static bool
514 tree_ssa_ifcombine_bb (basic_block inner_cond_bb)
515 {
516   basic_block then_bb = NULL, else_bb = NULL;
517
518   if (!recognize_if_then_else (inner_cond_bb, &then_bb, &else_bb))
519     return false;
520
521   /* Recognize && and || of two conditions with a common
522      then/else block which entry edges we can merge.  That is:
523        if (a || b)
524          ;
525      and
526        if (a && b)
527          ;
528      This requires a single predecessor of the inner cond_bb.  */
529   if (single_pred_p (inner_cond_bb))
530     {
531       basic_block outer_cond_bb = single_pred (inner_cond_bb);
532
533       /* The && form is characterized by a common else_bb with
534          the two edges leading to it mergable.  The latter is
535          guaranteed by matching PHI arguments in the else_bb and
536          the inner cond_bb having no side-effects.  */
537       if (recognize_if_then_else (outer_cond_bb, &inner_cond_bb, &else_bb)
538           && same_phi_args_p (outer_cond_bb, inner_cond_bb, else_bb)
539           && bb_no_side_effects_p (inner_cond_bb))
540         {
541           /* We have
542                <outer_cond_bb>
543                  if (q) goto inner_cond_bb; else goto else_bb;
544                <inner_cond_bb>
545                  if (p) goto ...; else goto else_bb;
546                  ...
547                <else_bb>
548                  ...
549            */
550           return ifcombine_ifandif (inner_cond_bb, outer_cond_bb);
551         }
552
553       /* The || form is characterized by a common then_bb with the
554          two edges leading to it mergable.  The latter is guaranteed
555          by matching PHI arguments in the then_bb and the inner cond_bb
556          having no side-effects.  */
557       if (recognize_if_then_else (outer_cond_bb, &then_bb, &inner_cond_bb)
558           && same_phi_args_p (outer_cond_bb, inner_cond_bb, then_bb)
559           && bb_no_side_effects_p (inner_cond_bb))
560         {
561           /* We have
562                <outer_cond_bb>
563                  if (q) goto then_bb; else goto inner_cond_bb;
564                <inner_cond_bb>
565                  if (q) goto then_bb; else goto ...;
566                <then_bb>
567                  ...
568            */
569           return ifcombine_iforif (inner_cond_bb, outer_cond_bb);
570         }
571     }
572
573   return false;
574 }
575
576 /* Main entry for the tree if-conversion pass.  */
577
578 static unsigned int
579 tree_ssa_ifcombine (void)
580 {
581   basic_block *bbs;
582   bool cfg_changed = false;
583   int i;
584
585   bbs = blocks_in_phiopt_order ();
586
587   for (i = 0; i < n_basic_blocks - NUM_FIXED_BLOCKS; ++i)
588     {
589       basic_block bb = bbs[i];
590       tree stmt = last_stmt (bb);
591
592       if (stmt
593           && TREE_CODE (stmt) == COND_EXPR)
594         cfg_changed |= tree_ssa_ifcombine_bb (bb);
595     }
596
597   free (bbs);
598
599   return cfg_changed ? TODO_cleanup_cfg : 0;
600 }
601
602 static bool
603 gate_ifcombine (void)
604 {
605   return 1;
606 }
607
608 struct gimple_opt_pass pass_tree_ifcombine = 
609 {
610  {
611   GIMPLE_PASS,
612   "ifcombine",                  /* name */
613   gate_ifcombine,               /* gate */
614   tree_ssa_ifcombine,           /* execute */
615   NULL,                         /* sub */
616   NULL,                         /* next */
617   0,                            /* static_pass_number */
618   TV_TREE_IFCOMBINE,            /* tv_id */
619   PROP_cfg | PROP_ssa,          /* properties_required */
620   0,                            /* properties_provided */
621   0,                            /* properties_destroyed */
622   0,                            /* todo_flags_start */
623   TODO_dump_func
624   | TODO_ggc_collect
625   | TODO_update_ssa
626   | TODO_verify_ssa             /* todo_flags_finish */
627  }
628 };