PR59170 make pretty printers check for singular iterators
[official-gcc.git] / gcc / tree-ssa-ifcombine.c
blob8e5258f8879d767f0e99adaafcb7449684c46fb1
1 /* Combining of if-expressions on trees.
2 Copyright (C) 2007-2016 Free Software Foundation, Inc.
3 Contributed by Richard Guenther <rguenther@suse.de>
5 This file is part of GCC.
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.
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.
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/>. */
21 #include "config.h"
22 #include "system.h"
23 #include "coretypes.h"
24 #include "backend.h"
25 #include "rtl.h"
26 #include "tree.h"
27 #include "gimple.h"
28 #include "cfghooks.h"
29 #include "tree-pass.h"
30 #include "memmodel.h"
31 #include "tm_p.h"
32 #include "ssa.h"
33 #include "tree-pretty-print.h"
34 /* rtl is needed only because arm back-end requires it for
35 BRANCH_COST. */
36 #include "fold-const.h"
37 #include "cfganal.h"
38 #include "gimple-fold.h"
39 #include "gimple-iterator.h"
40 #include "gimplify-me.h"
41 #include "tree-cfg.h"
42 #include "tree-ssa.h"
44 #ifndef LOGICAL_OP_NON_SHORT_CIRCUIT
45 #define LOGICAL_OP_NON_SHORT_CIRCUIT \
46 (BRANCH_COST (optimize_function_for_speed_p (cfun), \
47 false) >= 2)
48 #endif
50 /* This pass combines COND_EXPRs to simplify control flow. It
51 currently recognizes bit tests and comparisons in chains that
52 represent logical and or logical or of two COND_EXPRs.
54 It does so by walking basic blocks in a approximate reverse
55 post-dominator order and trying to match CFG patterns that
56 represent logical and or logical or of two COND_EXPRs.
57 Transformations are done if the COND_EXPR conditions match
58 either
60 1. two single bit tests X & (1 << Yn) (for logical and)
62 2. two bit tests X & Yn (for logical or)
64 3. two comparisons X OPn Y (for logical or)
66 To simplify this pass, removing basic blocks and dead code
67 is left to CFG cleanup and DCE. */
70 /* Recognize a if-then-else CFG pattern starting to match with the
71 COND_BB basic-block containing the COND_EXPR. The recognized
72 then end else blocks are stored to *THEN_BB and *ELSE_BB. If
73 *THEN_BB and/or *ELSE_BB are already set, they are required to
74 match the then and else basic-blocks to make the pattern match.
75 Returns true if the pattern matched, false otherwise. */
77 static bool
78 recognize_if_then_else (basic_block cond_bb,
79 basic_block *then_bb, basic_block *else_bb)
81 edge t, e;
83 if (EDGE_COUNT (cond_bb->succs) != 2)
84 return false;
86 /* Find the then/else edges. */
87 t = EDGE_SUCC (cond_bb, 0);
88 e = EDGE_SUCC (cond_bb, 1);
89 if (!(t->flags & EDGE_TRUE_VALUE))
90 std::swap (t, e);
91 if (!(t->flags & EDGE_TRUE_VALUE)
92 || !(e->flags & EDGE_FALSE_VALUE))
93 return false;
95 /* Check if the edge destinations point to the required block. */
96 if (*then_bb
97 && t->dest != *then_bb)
98 return false;
99 if (*else_bb
100 && e->dest != *else_bb)
101 return false;
103 if (!*then_bb)
104 *then_bb = t->dest;
105 if (!*else_bb)
106 *else_bb = e->dest;
108 return true;
111 /* Verify if the basic block BB does not have side-effects. Return
112 true in this case, else false. */
114 static bool
115 bb_no_side_effects_p (basic_block bb)
117 gimple_stmt_iterator gsi;
119 for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
121 gimple *stmt = gsi_stmt (gsi);
123 if (is_gimple_debug (stmt))
124 continue;
126 if (gimple_has_side_effects (stmt)
127 || gimple_uses_undefined_value_p (stmt)
128 || gimple_could_trap_p (stmt)
129 || gimple_vuse (stmt)
130 /* const calls don't match any of the above, yet they could
131 still have some side-effects - they could contain
132 gimple_could_trap_p statements, like floating point
133 exceptions or integer division by zero. See PR70586.
134 FIXME: perhaps gimple_has_side_effects or gimple_could_trap_p
135 should handle this. */
136 || is_gimple_call (stmt))
137 return false;
140 return true;
143 /* Return true if BB is an empty forwarder block to TO_BB. */
145 static bool
146 forwarder_block_to (basic_block bb, basic_block to_bb)
148 return empty_block_p (bb)
149 && single_succ_p (bb)
150 && single_succ (bb) == to_bb;
153 /* Verify if all PHI node arguments in DEST for edges from BB1 or
154 BB2 to DEST are the same. This makes the CFG merge point
155 free from side-effects. Return true in this case, else false. */
157 static bool
158 same_phi_args_p (basic_block bb1, basic_block bb2, basic_block dest)
160 edge e1 = find_edge (bb1, dest);
161 edge e2 = find_edge (bb2, dest);
162 gphi_iterator gsi;
163 gphi *phi;
165 for (gsi = gsi_start_phis (dest); !gsi_end_p (gsi); gsi_next (&gsi))
167 phi = gsi.phi ();
168 if (!operand_equal_p (PHI_ARG_DEF_FROM_EDGE (phi, e1),
169 PHI_ARG_DEF_FROM_EDGE (phi, e2), 0))
170 return false;
173 return true;
176 /* Return the best representative SSA name for CANDIDATE which is used
177 in a bit test. */
179 static tree
180 get_name_for_bit_test (tree candidate)
182 /* Skip single-use names in favor of using the name from a
183 non-widening conversion definition. */
184 if (TREE_CODE (candidate) == SSA_NAME
185 && has_single_use (candidate))
187 gimple *def_stmt = SSA_NAME_DEF_STMT (candidate);
188 if (is_gimple_assign (def_stmt)
189 && CONVERT_EXPR_CODE_P (gimple_assign_rhs_code (def_stmt)))
191 if (TYPE_PRECISION (TREE_TYPE (candidate))
192 <= TYPE_PRECISION (TREE_TYPE (gimple_assign_rhs1 (def_stmt))))
193 return gimple_assign_rhs1 (def_stmt);
197 return candidate;
200 /* Recognize a single bit test pattern in GIMPLE_COND and its defining
201 statements. Store the name being tested in *NAME and the bit
202 in *BIT. The GIMPLE_COND computes *NAME & (1 << *BIT).
203 Returns true if the pattern matched, false otherwise. */
205 static bool
206 recognize_single_bit_test (gcond *cond, tree *name, tree *bit, bool inv)
208 gimple *stmt;
210 /* Get at the definition of the result of the bit test. */
211 if (gimple_cond_code (cond) != (inv ? EQ_EXPR : NE_EXPR)
212 || TREE_CODE (gimple_cond_lhs (cond)) != SSA_NAME
213 || !integer_zerop (gimple_cond_rhs (cond)))
214 return false;
215 stmt = SSA_NAME_DEF_STMT (gimple_cond_lhs (cond));
216 if (!is_gimple_assign (stmt))
217 return false;
219 /* Look at which bit is tested. One form to recognize is
220 D.1985_5 = state_3(D) >> control1_4(D);
221 D.1986_6 = (int) D.1985_5;
222 D.1987_7 = op0 & 1;
223 if (D.1987_7 != 0) */
224 if (gimple_assign_rhs_code (stmt) == BIT_AND_EXPR
225 && integer_onep (gimple_assign_rhs2 (stmt))
226 && TREE_CODE (gimple_assign_rhs1 (stmt)) == SSA_NAME)
228 tree orig_name = gimple_assign_rhs1 (stmt);
230 /* Look through copies and conversions to eventually
231 find the stmt that computes the shift. */
232 stmt = SSA_NAME_DEF_STMT (orig_name);
234 while (is_gimple_assign (stmt)
235 && ((CONVERT_EXPR_CODE_P (gimple_assign_rhs_code (stmt))
236 && (TYPE_PRECISION (TREE_TYPE (gimple_assign_lhs (stmt)))
237 <= TYPE_PRECISION (TREE_TYPE (gimple_assign_rhs1 (stmt))))
238 && TREE_CODE (gimple_assign_rhs1 (stmt)) == SSA_NAME)
239 || gimple_assign_ssa_name_copy_p (stmt)))
240 stmt = SSA_NAME_DEF_STMT (gimple_assign_rhs1 (stmt));
242 /* If we found such, decompose it. */
243 if (is_gimple_assign (stmt)
244 && gimple_assign_rhs_code (stmt) == RSHIFT_EXPR)
246 /* op0 & (1 << op1) */
247 *bit = gimple_assign_rhs2 (stmt);
248 *name = gimple_assign_rhs1 (stmt);
250 else
252 /* t & 1 */
253 *bit = integer_zero_node;
254 *name = get_name_for_bit_test (orig_name);
257 return true;
260 /* Another form is
261 D.1987_7 = op0 & (1 << CST)
262 if (D.1987_7 != 0) */
263 if (gimple_assign_rhs_code (stmt) == BIT_AND_EXPR
264 && TREE_CODE (gimple_assign_rhs1 (stmt)) == SSA_NAME
265 && integer_pow2p (gimple_assign_rhs2 (stmt)))
267 *name = gimple_assign_rhs1 (stmt);
268 *bit = build_int_cst (integer_type_node,
269 tree_log2 (gimple_assign_rhs2 (stmt)));
270 return true;
273 /* Another form is
274 D.1986_6 = 1 << control1_4(D)
275 D.1987_7 = op0 & D.1986_6
276 if (D.1987_7 != 0) */
277 if (gimple_assign_rhs_code (stmt) == BIT_AND_EXPR
278 && TREE_CODE (gimple_assign_rhs1 (stmt)) == SSA_NAME
279 && TREE_CODE (gimple_assign_rhs2 (stmt)) == SSA_NAME)
281 gimple *tmp;
283 /* Both arguments of the BIT_AND_EXPR can be the single-bit
284 specifying expression. */
285 tmp = SSA_NAME_DEF_STMT (gimple_assign_rhs1 (stmt));
286 if (is_gimple_assign (tmp)
287 && gimple_assign_rhs_code (tmp) == LSHIFT_EXPR
288 && integer_onep (gimple_assign_rhs1 (tmp)))
290 *name = gimple_assign_rhs2 (stmt);
291 *bit = gimple_assign_rhs2 (tmp);
292 return true;
295 tmp = SSA_NAME_DEF_STMT (gimple_assign_rhs2 (stmt));
296 if (is_gimple_assign (tmp)
297 && gimple_assign_rhs_code (tmp) == LSHIFT_EXPR
298 && integer_onep (gimple_assign_rhs1 (tmp)))
300 *name = gimple_assign_rhs1 (stmt);
301 *bit = gimple_assign_rhs2 (tmp);
302 return true;
306 return false;
309 /* Recognize a bit test pattern in a GIMPLE_COND and its defining
310 statements. Store the name being tested in *NAME and the bits
311 in *BITS. The COND_EXPR computes *NAME & *BITS.
312 Returns true if the pattern matched, false otherwise. */
314 static bool
315 recognize_bits_test (gcond *cond, tree *name, tree *bits, bool inv)
317 gimple *stmt;
319 /* Get at the definition of the result of the bit test. */
320 if (gimple_cond_code (cond) != (inv ? EQ_EXPR : NE_EXPR)
321 || TREE_CODE (gimple_cond_lhs (cond)) != SSA_NAME
322 || !integer_zerop (gimple_cond_rhs (cond)))
323 return false;
324 stmt = SSA_NAME_DEF_STMT (gimple_cond_lhs (cond));
325 if (!is_gimple_assign (stmt)
326 || gimple_assign_rhs_code (stmt) != BIT_AND_EXPR)
327 return false;
329 *name = get_name_for_bit_test (gimple_assign_rhs1 (stmt));
330 *bits = gimple_assign_rhs2 (stmt);
332 return true;
335 /* If-convert on a and pattern with a common else block. The inner
336 if is specified by its INNER_COND_BB, the outer by OUTER_COND_BB.
337 inner_inv, outer_inv and result_inv indicate whether the conditions
338 are inverted.
339 Returns true if the edges to the common else basic-block were merged. */
341 static bool
342 ifcombine_ifandif (basic_block inner_cond_bb, bool inner_inv,
343 basic_block outer_cond_bb, bool outer_inv, bool result_inv)
345 gimple_stmt_iterator gsi;
346 gimple *inner_stmt, *outer_stmt;
347 gcond *inner_cond, *outer_cond;
348 tree name1, name2, bit1, bit2, bits1, bits2;
350 inner_stmt = last_stmt (inner_cond_bb);
351 if (!inner_stmt
352 || gimple_code (inner_stmt) != GIMPLE_COND)
353 return false;
354 inner_cond = as_a <gcond *> (inner_stmt);
356 outer_stmt = last_stmt (outer_cond_bb);
357 if (!outer_stmt
358 || gimple_code (outer_stmt) != GIMPLE_COND)
359 return false;
360 outer_cond = as_a <gcond *> (outer_stmt);
362 /* See if we test a single bit of the same name in both tests. In
363 that case remove the outer test, merging both else edges,
364 and change the inner one to test for
365 name & (bit1 | bit2) == (bit1 | bit2). */
366 if (recognize_single_bit_test (inner_cond, &name1, &bit1, inner_inv)
367 && recognize_single_bit_test (outer_cond, &name2, &bit2, outer_inv)
368 && name1 == name2)
370 tree t, t2;
372 /* Do it. */
373 gsi = gsi_for_stmt (inner_cond);
374 t = fold_build2 (LSHIFT_EXPR, TREE_TYPE (name1),
375 build_int_cst (TREE_TYPE (name1), 1), bit1);
376 t2 = fold_build2 (LSHIFT_EXPR, TREE_TYPE (name1),
377 build_int_cst (TREE_TYPE (name1), 1), bit2);
378 t = fold_build2 (BIT_IOR_EXPR, TREE_TYPE (name1), t, t2);
379 t = force_gimple_operand_gsi (&gsi, t, true, NULL_TREE,
380 true, GSI_SAME_STMT);
381 t2 = fold_build2 (BIT_AND_EXPR, TREE_TYPE (name1), name1, t);
382 t2 = force_gimple_operand_gsi (&gsi, t2, true, NULL_TREE,
383 true, GSI_SAME_STMT);
384 t = fold_build2 (result_inv ? NE_EXPR : EQ_EXPR,
385 boolean_type_node, t2, t);
386 t = canonicalize_cond_expr_cond (t);
387 if (!t)
388 return false;
389 gimple_cond_set_condition_from_tree (inner_cond, t);
390 update_stmt (inner_cond);
392 /* Leave CFG optimization to cfg_cleanup. */
393 gimple_cond_set_condition_from_tree (outer_cond,
394 outer_inv ? boolean_false_node : boolean_true_node);
395 update_stmt (outer_cond);
397 if (dump_file)
399 fprintf (dump_file, "optimizing double bit test to ");
400 print_generic_expr (dump_file, name1, 0);
401 fprintf (dump_file, " & T == T\nwith temporary T = (1 << ");
402 print_generic_expr (dump_file, bit1, 0);
403 fprintf (dump_file, ") | (1 << ");
404 print_generic_expr (dump_file, bit2, 0);
405 fprintf (dump_file, ")\n");
408 return true;
411 /* See if we have two bit tests of the same name in both tests.
412 In that case remove the outer test and change the inner one to
413 test for name & (bits1 | bits2) != 0. */
414 else if (recognize_bits_test (inner_cond, &name1, &bits1, !inner_inv)
415 && recognize_bits_test (outer_cond, &name2, &bits2, !outer_inv))
417 gimple_stmt_iterator gsi;
418 tree t;
420 /* Find the common name which is bit-tested. */
421 if (name1 == name2)
423 else if (bits1 == bits2)
425 std::swap (name2, bits2);
426 std::swap (name1, bits1);
428 else if (name1 == bits2)
429 std::swap (name2, bits2);
430 else if (bits1 == name2)
431 std::swap (name1, bits1);
432 else
433 return false;
435 /* As we strip non-widening conversions in finding a common
436 name that is tested make sure to end up with an integral
437 type for building the bit operations. */
438 if (TYPE_PRECISION (TREE_TYPE (bits1))
439 >= TYPE_PRECISION (TREE_TYPE (bits2)))
441 bits1 = fold_convert (unsigned_type_for (TREE_TYPE (bits1)), bits1);
442 name1 = fold_convert (TREE_TYPE (bits1), name1);
443 bits2 = fold_convert (unsigned_type_for (TREE_TYPE (bits2)), bits2);
444 bits2 = fold_convert (TREE_TYPE (bits1), bits2);
446 else
448 bits2 = fold_convert (unsigned_type_for (TREE_TYPE (bits2)), bits2);
449 name1 = fold_convert (TREE_TYPE (bits2), name1);
450 bits1 = fold_convert (unsigned_type_for (TREE_TYPE (bits1)), bits1);
451 bits1 = fold_convert (TREE_TYPE (bits2), bits1);
454 /* Do it. */
455 gsi = gsi_for_stmt (inner_cond);
456 t = fold_build2 (BIT_IOR_EXPR, TREE_TYPE (name1), bits1, bits2);
457 t = force_gimple_operand_gsi (&gsi, t, true, NULL_TREE,
458 true, GSI_SAME_STMT);
459 t = fold_build2 (BIT_AND_EXPR, TREE_TYPE (name1), name1, t);
460 t = force_gimple_operand_gsi (&gsi, t, true, NULL_TREE,
461 true, GSI_SAME_STMT);
462 t = fold_build2 (result_inv ? NE_EXPR : EQ_EXPR, boolean_type_node, t,
463 build_int_cst (TREE_TYPE (t), 0));
464 t = canonicalize_cond_expr_cond (t);
465 if (!t)
466 return false;
467 gimple_cond_set_condition_from_tree (inner_cond, t);
468 update_stmt (inner_cond);
470 /* Leave CFG optimization to cfg_cleanup. */
471 gimple_cond_set_condition_from_tree (outer_cond,
472 outer_inv ? boolean_false_node : boolean_true_node);
473 update_stmt (outer_cond);
475 if (dump_file)
477 fprintf (dump_file, "optimizing bits or bits test to ");
478 print_generic_expr (dump_file, name1, 0);
479 fprintf (dump_file, " & T != 0\nwith temporary T = ");
480 print_generic_expr (dump_file, bits1, 0);
481 fprintf (dump_file, " | ");
482 print_generic_expr (dump_file, bits2, 0);
483 fprintf (dump_file, "\n");
486 return true;
489 /* See if we have two comparisons that we can merge into one. */
490 else if (TREE_CODE_CLASS (gimple_cond_code (inner_cond)) == tcc_comparison
491 && TREE_CODE_CLASS (gimple_cond_code (outer_cond)) == tcc_comparison)
493 tree t;
494 enum tree_code inner_cond_code = gimple_cond_code (inner_cond);
495 enum tree_code outer_cond_code = gimple_cond_code (outer_cond);
497 /* Invert comparisons if necessary (and possible). */
498 if (inner_inv)
499 inner_cond_code = invert_tree_comparison (inner_cond_code,
500 HONOR_NANS (gimple_cond_lhs (inner_cond)));
501 if (inner_cond_code == ERROR_MARK)
502 return false;
503 if (outer_inv)
504 outer_cond_code = invert_tree_comparison (outer_cond_code,
505 HONOR_NANS (gimple_cond_lhs (outer_cond)));
506 if (outer_cond_code == ERROR_MARK)
507 return false;
508 /* Don't return false so fast, try maybe_fold_or_comparisons? */
510 if (!(t = maybe_fold_and_comparisons (inner_cond_code,
511 gimple_cond_lhs (inner_cond),
512 gimple_cond_rhs (inner_cond),
513 outer_cond_code,
514 gimple_cond_lhs (outer_cond),
515 gimple_cond_rhs (outer_cond))))
517 tree t1, t2;
518 gimple_stmt_iterator gsi;
519 if (!LOGICAL_OP_NON_SHORT_CIRCUIT)
520 return false;
521 /* Only do this optimization if the inner bb contains only the conditional. */
522 if (!gsi_one_before_end_p (gsi_start_nondebug_after_labels_bb (inner_cond_bb)))
523 return false;
524 t1 = fold_build2_loc (gimple_location (inner_cond),
525 inner_cond_code,
526 boolean_type_node,
527 gimple_cond_lhs (inner_cond),
528 gimple_cond_rhs (inner_cond));
529 t2 = fold_build2_loc (gimple_location (outer_cond),
530 outer_cond_code,
531 boolean_type_node,
532 gimple_cond_lhs (outer_cond),
533 gimple_cond_rhs (outer_cond));
534 t = fold_build2_loc (gimple_location (inner_cond),
535 TRUTH_AND_EXPR, boolean_type_node, t1, t2);
536 if (result_inv)
538 t = fold_build1 (TRUTH_NOT_EXPR, TREE_TYPE (t), t);
539 result_inv = false;
541 gsi = gsi_for_stmt (inner_cond);
542 t = force_gimple_operand_gsi_1 (&gsi, t, is_gimple_condexpr, NULL, true,
543 GSI_SAME_STMT);
545 if (result_inv)
546 t = fold_build1 (TRUTH_NOT_EXPR, TREE_TYPE (t), t);
547 t = canonicalize_cond_expr_cond (t);
548 if (!t)
549 return false;
550 gimple_cond_set_condition_from_tree (inner_cond, t);
551 update_stmt (inner_cond);
553 /* Leave CFG optimization to cfg_cleanup. */
554 gimple_cond_set_condition_from_tree (outer_cond,
555 outer_inv ? boolean_false_node : boolean_true_node);
556 update_stmt (outer_cond);
558 if (dump_file)
560 fprintf (dump_file, "optimizing two comparisons to ");
561 print_generic_expr (dump_file, t, 0);
562 fprintf (dump_file, "\n");
565 return true;
568 return false;
571 /* Helper function for tree_ssa_ifcombine_bb. Recognize a CFG pattern and
572 dispatch to the appropriate if-conversion helper for a particular
573 set of INNER_COND_BB, OUTER_COND_BB, THEN_BB and ELSE_BB.
574 PHI_PRED_BB should be one of INNER_COND_BB, THEN_BB or ELSE_BB. */
576 static bool
577 tree_ssa_ifcombine_bb_1 (basic_block inner_cond_bb, basic_block outer_cond_bb,
578 basic_block then_bb, basic_block else_bb,
579 basic_block phi_pred_bb)
581 /* The && form is characterized by a common else_bb with
582 the two edges leading to it mergable. The latter is
583 guaranteed by matching PHI arguments in the else_bb and
584 the inner cond_bb having no side-effects. */
585 if (phi_pred_bb != else_bb
586 && recognize_if_then_else (outer_cond_bb, &inner_cond_bb, &else_bb)
587 && same_phi_args_p (outer_cond_bb, phi_pred_bb, else_bb))
589 /* We have
590 <outer_cond_bb>
591 if (q) goto inner_cond_bb; else goto else_bb;
592 <inner_cond_bb>
593 if (p) goto ...; else goto else_bb;
595 <else_bb>
598 return ifcombine_ifandif (inner_cond_bb, false, outer_cond_bb, false,
599 false);
602 /* And a version where the outer condition is negated. */
603 if (phi_pred_bb != else_bb
604 && recognize_if_then_else (outer_cond_bb, &else_bb, &inner_cond_bb)
605 && same_phi_args_p (outer_cond_bb, phi_pred_bb, else_bb))
607 /* We have
608 <outer_cond_bb>
609 if (q) goto else_bb; else goto inner_cond_bb;
610 <inner_cond_bb>
611 if (p) goto ...; else goto else_bb;
613 <else_bb>
616 return ifcombine_ifandif (inner_cond_bb, false, outer_cond_bb, true,
617 false);
620 /* The || form is characterized by a common then_bb with the
621 two edges leading to it mergable. The latter is guaranteed
622 by matching PHI arguments in the then_bb and the inner cond_bb
623 having no side-effects. */
624 if (phi_pred_bb != then_bb
625 && recognize_if_then_else (outer_cond_bb, &then_bb, &inner_cond_bb)
626 && same_phi_args_p (outer_cond_bb, phi_pred_bb, then_bb))
628 /* We have
629 <outer_cond_bb>
630 if (q) goto then_bb; else goto inner_cond_bb;
631 <inner_cond_bb>
632 if (q) goto then_bb; else goto ...;
633 <then_bb>
636 return ifcombine_ifandif (inner_cond_bb, true, outer_cond_bb, true,
637 true);
640 /* And a version where the outer condition is negated. */
641 if (phi_pred_bb != then_bb
642 && recognize_if_then_else (outer_cond_bb, &inner_cond_bb, &then_bb)
643 && same_phi_args_p (outer_cond_bb, phi_pred_bb, then_bb))
645 /* We have
646 <outer_cond_bb>
647 if (q) goto inner_cond_bb; else goto then_bb;
648 <inner_cond_bb>
649 if (q) goto then_bb; else goto ...;
650 <then_bb>
653 return ifcombine_ifandif (inner_cond_bb, true, outer_cond_bb, false,
654 true);
657 return false;
660 /* Recognize a CFG pattern and dispatch to the appropriate
661 if-conversion helper. We start with BB as the innermost
662 worker basic-block. Returns true if a transformation was done. */
664 static bool
665 tree_ssa_ifcombine_bb (basic_block inner_cond_bb)
667 basic_block then_bb = NULL, else_bb = NULL;
669 if (!recognize_if_then_else (inner_cond_bb, &then_bb, &else_bb))
670 return false;
672 /* Recognize && and || of two conditions with a common
673 then/else block which entry edges we can merge. That is:
674 if (a || b)
677 if (a && b)
679 This requires a single predecessor of the inner cond_bb. */
680 if (single_pred_p (inner_cond_bb)
681 && bb_no_side_effects_p (inner_cond_bb))
683 basic_block outer_cond_bb = single_pred (inner_cond_bb);
685 if (tree_ssa_ifcombine_bb_1 (inner_cond_bb, outer_cond_bb,
686 then_bb, else_bb, inner_cond_bb))
687 return true;
689 if (forwarder_block_to (else_bb, then_bb))
691 /* Other possibilities for the && form, if else_bb is
692 empty forwarder block to then_bb. Compared to the above simpler
693 forms this can be treated as if then_bb and else_bb were swapped,
694 and the corresponding inner_cond_bb not inverted because of that.
695 For same_phi_args_p we look at equality of arguments between
696 edge from outer_cond_bb and the forwarder block. */
697 if (tree_ssa_ifcombine_bb_1 (inner_cond_bb, outer_cond_bb, else_bb,
698 then_bb, else_bb))
699 return true;
701 else if (forwarder_block_to (then_bb, else_bb))
703 /* Other possibilities for the || form, if then_bb is
704 empty forwarder block to else_bb. Compared to the above simpler
705 forms this can be treated as if then_bb and else_bb were swapped,
706 and the corresponding inner_cond_bb not inverted because of that.
707 For same_phi_args_p we look at equality of arguments between
708 edge from outer_cond_bb and the forwarder block. */
709 if (tree_ssa_ifcombine_bb_1 (inner_cond_bb, outer_cond_bb, else_bb,
710 then_bb, then_bb))
711 return true;
715 return false;
718 /* Main entry for the tree if-conversion pass. */
720 namespace {
722 const pass_data pass_data_tree_ifcombine =
724 GIMPLE_PASS, /* type */
725 "ifcombine", /* name */
726 OPTGROUP_NONE, /* optinfo_flags */
727 TV_TREE_IFCOMBINE, /* tv_id */
728 ( PROP_cfg | PROP_ssa ), /* properties_required */
729 0, /* properties_provided */
730 0, /* properties_destroyed */
731 0, /* todo_flags_start */
732 TODO_update_ssa, /* todo_flags_finish */
735 class pass_tree_ifcombine : public gimple_opt_pass
737 public:
738 pass_tree_ifcombine (gcc::context *ctxt)
739 : gimple_opt_pass (pass_data_tree_ifcombine, ctxt)
742 /* opt_pass methods: */
743 virtual unsigned int execute (function *);
745 }; // class pass_tree_ifcombine
747 unsigned int
748 pass_tree_ifcombine::execute (function *fun)
750 basic_block *bbs;
751 bool cfg_changed = false;
752 int i;
754 bbs = single_pred_before_succ_order ();
755 calculate_dominance_info (CDI_DOMINATORS);
757 /* Search every basic block for COND_EXPR we may be able to optimize.
759 We walk the blocks in order that guarantees that a block with
760 a single predecessor is processed after the predecessor.
761 This ensures that we collapse outter ifs before visiting the
762 inner ones, and also that we do not try to visit a removed
763 block. This is opposite of PHI-OPT, because we cascade the
764 combining rather than cascading PHIs. */
765 for (i = n_basic_blocks_for_fn (fun) - NUM_FIXED_BLOCKS - 1; i >= 0; i--)
767 basic_block bb = bbs[i];
768 gimple *stmt = last_stmt (bb);
770 if (stmt
771 && gimple_code (stmt) == GIMPLE_COND)
772 if (tree_ssa_ifcombine_bb (bb))
774 /* Clear range info from all stmts in BB which is now executed
775 conditional on a always true/false condition. */
776 reset_flow_sensitive_info_in_bb (bb);
777 cfg_changed |= true;
781 free (bbs);
783 return cfg_changed ? TODO_cleanup_cfg : 0;
786 } // anon namespace
788 gimple_opt_pass *
789 make_pass_tree_ifcombine (gcc::context *ctxt)
791 return new pass_tree_ifcombine (ctxt);