]> git.ipfire.org Git - thirdparty/gcc.git/blob - gcc/tree-ssa-ifcombine.c
2015-06-17 Andrew MacLeod <amacleod@redhat.com>
[thirdparty/gcc.git] / gcc / tree-ssa-ifcombine.c
1 /* Combining of if-expressions on trees.
2 Copyright (C) 2007-2015 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 /* rtl is needed only because arm back-end requires it for
26 BRANCH_COST. */
27 #include "rtl.h"
28 #include "tm_p.h"
29 #include "alias.h"
30 #include "symtab.h"
31 #include "tree.h"
32 #include "fold-const.h"
33 #include "stor-layout.h"
34 #include "predict.h"
35 #include "hard-reg-set.h"
36 #include "function.h"
37 #include "dominance.h"
38 #include "cfg.h"
39 #include "cfganal.h"
40 #include "basic-block.h"
41 #include "tree-pretty-print.h"
42 #include "tree-ssa-alias.h"
43 #include "internal-fn.h"
44 #include "gimple-fold.h"
45 #include "gimple-expr.h"
46 #include "gimple.h"
47 #include "gimple-iterator.h"
48 #include "gimplify-me.h"
49 #include "gimple-ssa.h"
50 #include "tree-cfg.h"
51 #include "tree-phinodes.h"
52 #include "ssa-iterators.h"
53 #include "tree-pass.h"
54
55 #ifndef LOGICAL_OP_NON_SHORT_CIRCUIT
56 #define LOGICAL_OP_NON_SHORT_CIRCUIT \
57 (BRANCH_COST (optimize_function_for_speed_p (cfun), \
58 false) >= 2)
59 #endif
60
61 /* This pass combines COND_EXPRs to simplify control flow. It
62 currently recognizes bit tests and comparisons in chains that
63 represent logical and or logical or of two COND_EXPRs.
64
65 It does so by walking basic blocks in a approximate reverse
66 post-dominator order and trying to match CFG patterns that
67 represent logical and or logical or of two COND_EXPRs.
68 Transformations are done if the COND_EXPR conditions match
69 either
70
71 1. two single bit tests X & (1 << Yn) (for logical and)
72
73 2. two bit tests X & Yn (for logical or)
74
75 3. two comparisons X OPn Y (for logical or)
76
77 To simplify this pass, removing basic blocks and dead code
78 is left to CFG cleanup and DCE. */
79
80
81 /* Recognize a if-then-else CFG pattern starting to match with the
82 COND_BB basic-block containing the COND_EXPR. The recognized
83 then end else blocks are stored to *THEN_BB and *ELSE_BB. If
84 *THEN_BB and/or *ELSE_BB are already set, they are required to
85 match the then and else basic-blocks to make the pattern match.
86 Returns true if the pattern matched, false otherwise. */
87
88 static bool
89 recognize_if_then_else (basic_block cond_bb,
90 basic_block *then_bb, basic_block *else_bb)
91 {
92 edge t, e;
93
94 if (EDGE_COUNT (cond_bb->succs) != 2)
95 return false;
96
97 /* Find the then/else edges. */
98 t = EDGE_SUCC (cond_bb, 0);
99 e = EDGE_SUCC (cond_bb, 1);
100 if (!(t->flags & EDGE_TRUE_VALUE))
101 {
102 edge tmp = t;
103 t = e;
104 e = tmp;
105 }
106 if (!(t->flags & EDGE_TRUE_VALUE)
107 || !(e->flags & EDGE_FALSE_VALUE))
108 return false;
109
110 /* Check if the edge destinations point to the required block. */
111 if (*then_bb
112 && t->dest != *then_bb)
113 return false;
114 if (*else_bb
115 && e->dest != *else_bb)
116 return false;
117
118 if (!*then_bb)
119 *then_bb = t->dest;
120 if (!*else_bb)
121 *else_bb = e->dest;
122
123 return true;
124 }
125
126 /* Verify if the basic block BB does not have side-effects. Return
127 true in this case, else false. */
128
129 static bool
130 bb_no_side_effects_p (basic_block bb)
131 {
132 gimple_stmt_iterator gsi;
133
134 for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
135 {
136 gimple stmt = gsi_stmt (gsi);
137
138 if (is_gimple_debug (stmt))
139 continue;
140
141 if (gimple_has_side_effects (stmt)
142 || gimple_could_trap_p (stmt)
143 || gimple_vuse (stmt))
144 return false;
145 }
146
147 return true;
148 }
149
150 /* Return true if BB is an empty forwarder block to TO_BB. */
151
152 static bool
153 forwarder_block_to (basic_block bb, basic_block to_bb)
154 {
155 return empty_block_p (bb)
156 && single_succ_p (bb)
157 && single_succ (bb) == to_bb;
158 }
159
160 /* Verify if all PHI node arguments in DEST for edges from BB1 or
161 BB2 to DEST are the same. This makes the CFG merge point
162 free from side-effects. Return true in this case, else false. */
163
164 static bool
165 same_phi_args_p (basic_block bb1, basic_block bb2, basic_block dest)
166 {
167 edge e1 = find_edge (bb1, dest);
168 edge e2 = find_edge (bb2, dest);
169 gphi_iterator gsi;
170 gphi *phi;
171
172 for (gsi = gsi_start_phis (dest); !gsi_end_p (gsi); gsi_next (&gsi))
173 {
174 phi = gsi.phi ();
175 if (!operand_equal_p (PHI_ARG_DEF_FROM_EDGE (phi, e1),
176 PHI_ARG_DEF_FROM_EDGE (phi, e2), 0))
177 return false;
178 }
179
180 return true;
181 }
182
183 /* Return the best representative SSA name for CANDIDATE which is used
184 in a bit test. */
185
186 static tree
187 get_name_for_bit_test (tree candidate)
188 {
189 /* Skip single-use names in favor of using the name from a
190 non-widening conversion definition. */
191 if (TREE_CODE (candidate) == SSA_NAME
192 && has_single_use (candidate))
193 {
194 gimple def_stmt = SSA_NAME_DEF_STMT (candidate);
195 if (is_gimple_assign (def_stmt)
196 && CONVERT_EXPR_CODE_P (gimple_assign_rhs_code (def_stmt)))
197 {
198 if (TYPE_PRECISION (TREE_TYPE (candidate))
199 <= TYPE_PRECISION (TREE_TYPE (gimple_assign_rhs1 (def_stmt))))
200 return gimple_assign_rhs1 (def_stmt);
201 }
202 }
203
204 return candidate;
205 }
206
207 /* Recognize a single bit test pattern in GIMPLE_COND and its defining
208 statements. Store the name being tested in *NAME and the bit
209 in *BIT. The GIMPLE_COND computes *NAME & (1 << *BIT).
210 Returns true if the pattern matched, false otherwise. */
211
212 static bool
213 recognize_single_bit_test (gcond *cond, tree *name, tree *bit, bool inv)
214 {
215 gimple stmt;
216
217 /* Get at the definition of the result of the bit test. */
218 if (gimple_cond_code (cond) != (inv ? EQ_EXPR : NE_EXPR)
219 || TREE_CODE (gimple_cond_lhs (cond)) != SSA_NAME
220 || !integer_zerop (gimple_cond_rhs (cond)))
221 return false;
222 stmt = SSA_NAME_DEF_STMT (gimple_cond_lhs (cond));
223 if (!is_gimple_assign (stmt))
224 return false;
225
226 /* Look at which bit is tested. One form to recognize is
227 D.1985_5 = state_3(D) >> control1_4(D);
228 D.1986_6 = (int) D.1985_5;
229 D.1987_7 = op0 & 1;
230 if (D.1987_7 != 0) */
231 if (gimple_assign_rhs_code (stmt) == BIT_AND_EXPR
232 && integer_onep (gimple_assign_rhs2 (stmt))
233 && TREE_CODE (gimple_assign_rhs1 (stmt)) == SSA_NAME)
234 {
235 tree orig_name = gimple_assign_rhs1 (stmt);
236
237 /* Look through copies and conversions to eventually
238 find the stmt that computes the shift. */
239 stmt = SSA_NAME_DEF_STMT (orig_name);
240
241 while (is_gimple_assign (stmt)
242 && ((CONVERT_EXPR_CODE_P (gimple_assign_rhs_code (stmt))
243 && (TYPE_PRECISION (TREE_TYPE (gimple_assign_lhs (stmt)))
244 <= TYPE_PRECISION (TREE_TYPE (gimple_assign_rhs1 (stmt))))
245 && TREE_CODE (gimple_assign_rhs1 (stmt)) == SSA_NAME)
246 || gimple_assign_ssa_name_copy_p (stmt)))
247 stmt = SSA_NAME_DEF_STMT (gimple_assign_rhs1 (stmt));
248
249 /* If we found such, decompose it. */
250 if (is_gimple_assign (stmt)
251 && gimple_assign_rhs_code (stmt) == RSHIFT_EXPR)
252 {
253 /* op0 & (1 << op1) */
254 *bit = gimple_assign_rhs2 (stmt);
255 *name = gimple_assign_rhs1 (stmt);
256 }
257 else
258 {
259 /* t & 1 */
260 *bit = integer_zero_node;
261 *name = get_name_for_bit_test (orig_name);
262 }
263
264 return true;
265 }
266
267 /* Another form is
268 D.1987_7 = op0 & (1 << CST)
269 if (D.1987_7 != 0) */
270 if (gimple_assign_rhs_code (stmt) == BIT_AND_EXPR
271 && TREE_CODE (gimple_assign_rhs1 (stmt)) == SSA_NAME
272 && integer_pow2p (gimple_assign_rhs2 (stmt)))
273 {
274 *name = gimple_assign_rhs1 (stmt);
275 *bit = build_int_cst (integer_type_node,
276 tree_log2 (gimple_assign_rhs2 (stmt)));
277 return true;
278 }
279
280 /* Another form is
281 D.1986_6 = 1 << control1_4(D)
282 D.1987_7 = op0 & D.1986_6
283 if (D.1987_7 != 0) */
284 if (gimple_assign_rhs_code (stmt) == BIT_AND_EXPR
285 && TREE_CODE (gimple_assign_rhs1 (stmt)) == SSA_NAME
286 && TREE_CODE (gimple_assign_rhs2 (stmt)) == SSA_NAME)
287 {
288 gimple tmp;
289
290 /* Both arguments of the BIT_AND_EXPR can be the single-bit
291 specifying expression. */
292 tmp = SSA_NAME_DEF_STMT (gimple_assign_rhs1 (stmt));
293 if (is_gimple_assign (tmp)
294 && gimple_assign_rhs_code (tmp) == LSHIFT_EXPR
295 && integer_onep (gimple_assign_rhs1 (tmp)))
296 {
297 *name = gimple_assign_rhs2 (stmt);
298 *bit = gimple_assign_rhs2 (tmp);
299 return true;
300 }
301
302 tmp = SSA_NAME_DEF_STMT (gimple_assign_rhs2 (stmt));
303 if (is_gimple_assign (tmp)
304 && gimple_assign_rhs_code (tmp) == LSHIFT_EXPR
305 && integer_onep (gimple_assign_rhs1 (tmp)))
306 {
307 *name = gimple_assign_rhs1 (stmt);
308 *bit = gimple_assign_rhs2 (tmp);
309 return true;
310 }
311 }
312
313 return false;
314 }
315
316 /* Recognize a bit test pattern in a GIMPLE_COND and its defining
317 statements. Store the name being tested in *NAME and the bits
318 in *BITS. The COND_EXPR computes *NAME & *BITS.
319 Returns true if the pattern matched, false otherwise. */
320
321 static bool
322 recognize_bits_test (gcond *cond, tree *name, tree *bits, bool inv)
323 {
324 gimple stmt;
325
326 /* Get at the definition of the result of the bit test. */
327 if (gimple_cond_code (cond) != (inv ? EQ_EXPR : NE_EXPR)
328 || TREE_CODE (gimple_cond_lhs (cond)) != SSA_NAME
329 || !integer_zerop (gimple_cond_rhs (cond)))
330 return false;
331 stmt = SSA_NAME_DEF_STMT (gimple_cond_lhs (cond));
332 if (!is_gimple_assign (stmt)
333 || gimple_assign_rhs_code (stmt) != BIT_AND_EXPR)
334 return false;
335
336 *name = get_name_for_bit_test (gimple_assign_rhs1 (stmt));
337 *bits = gimple_assign_rhs2 (stmt);
338
339 return true;
340 }
341
342 /* If-convert on a and pattern with a common else block. The inner
343 if is specified by its INNER_COND_BB, the outer by OUTER_COND_BB.
344 inner_inv, outer_inv and result_inv indicate whether the conditions
345 are inverted.
346 Returns true if the edges to the common else basic-block were merged. */
347
348 static bool
349 ifcombine_ifandif (basic_block inner_cond_bb, bool inner_inv,
350 basic_block outer_cond_bb, bool outer_inv, bool result_inv)
351 {
352 gimple_stmt_iterator gsi;
353 gimple inner_stmt, outer_stmt;
354 gcond *inner_cond, *outer_cond;
355 tree name1, name2, bit1, bit2, bits1, bits2;
356
357 inner_stmt = last_stmt (inner_cond_bb);
358 if (!inner_stmt
359 || gimple_code (inner_stmt) != GIMPLE_COND)
360 return false;
361 inner_cond = as_a <gcond *> (inner_stmt);
362
363 outer_stmt = last_stmt (outer_cond_bb);
364 if (!outer_stmt
365 || gimple_code (outer_stmt) != GIMPLE_COND)
366 return false;
367 outer_cond = as_a <gcond *> (outer_stmt);
368
369 /* See if we test a single bit of the same name in both tests. In
370 that case remove the outer test, merging both else edges,
371 and change the inner one to test for
372 name & (bit1 | bit2) == (bit1 | bit2). */
373 if (recognize_single_bit_test (inner_cond, &name1, &bit1, inner_inv)
374 && recognize_single_bit_test (outer_cond, &name2, &bit2, outer_inv)
375 && name1 == name2)
376 {
377 tree t, t2;
378
379 /* Do it. */
380 gsi = gsi_for_stmt (inner_cond);
381 t = fold_build2 (LSHIFT_EXPR, TREE_TYPE (name1),
382 build_int_cst (TREE_TYPE (name1), 1), bit1);
383 t2 = fold_build2 (LSHIFT_EXPR, TREE_TYPE (name1),
384 build_int_cst (TREE_TYPE (name1), 1), bit2);
385 t = fold_build2 (BIT_IOR_EXPR, TREE_TYPE (name1), t, t2);
386 t = force_gimple_operand_gsi (&gsi, t, true, NULL_TREE,
387 true, GSI_SAME_STMT);
388 t2 = fold_build2 (BIT_AND_EXPR, TREE_TYPE (name1), name1, t);
389 t2 = force_gimple_operand_gsi (&gsi, t2, true, NULL_TREE,
390 true, GSI_SAME_STMT);
391 t = fold_build2 (result_inv ? NE_EXPR : EQ_EXPR,
392 boolean_type_node, t2, t);
393 t = canonicalize_cond_expr_cond (t);
394 if (!t)
395 return false;
396 gimple_cond_set_condition_from_tree (inner_cond, t);
397 update_stmt (inner_cond);
398
399 /* Leave CFG optimization to cfg_cleanup. */
400 gimple_cond_set_condition_from_tree (outer_cond,
401 outer_inv ? boolean_false_node : boolean_true_node);
402 update_stmt (outer_cond);
403
404 if (dump_file)
405 {
406 fprintf (dump_file, "optimizing double bit test to ");
407 print_generic_expr (dump_file, name1, 0);
408 fprintf (dump_file, " & T == T\nwith temporary T = (1 << ");
409 print_generic_expr (dump_file, bit1, 0);
410 fprintf (dump_file, ") | (1 << ");
411 print_generic_expr (dump_file, bit2, 0);
412 fprintf (dump_file, ")\n");
413 }
414
415 return true;
416 }
417
418 /* See if we have two bit tests of the same name in both tests.
419 In that case remove the outer test and change the inner one to
420 test for name & (bits1 | bits2) != 0. */
421 else if (recognize_bits_test (inner_cond, &name1, &bits1, !inner_inv)
422 && recognize_bits_test (outer_cond, &name2, &bits2, !outer_inv))
423 {
424 gimple_stmt_iterator gsi;
425 tree t;
426
427 /* Find the common name which is bit-tested. */
428 if (name1 == name2)
429 ;
430 else if (bits1 == bits2)
431 {
432 std::swap (name2, bits2);
433 std::swap (name1, bits1);
434 }
435 else if (name1 == bits2)
436 std::swap (name2, bits2);
437 else if (bits1 == name2)
438 std::swap (name1, bits1);
439 else
440 return false;
441
442 /* As we strip non-widening conversions in finding a common
443 name that is tested make sure to end up with an integral
444 type for building the bit operations. */
445 if (TYPE_PRECISION (TREE_TYPE (bits1))
446 >= TYPE_PRECISION (TREE_TYPE (bits2)))
447 {
448 bits1 = fold_convert (unsigned_type_for (TREE_TYPE (bits1)), bits1);
449 name1 = fold_convert (TREE_TYPE (bits1), name1);
450 bits2 = fold_convert (unsigned_type_for (TREE_TYPE (bits2)), bits2);
451 bits2 = fold_convert (TREE_TYPE (bits1), bits2);
452 }
453 else
454 {
455 bits2 = fold_convert (unsigned_type_for (TREE_TYPE (bits2)), bits2);
456 name1 = fold_convert (TREE_TYPE (bits2), name1);
457 bits1 = fold_convert (unsigned_type_for (TREE_TYPE (bits1)), bits1);
458 bits1 = fold_convert (TREE_TYPE (bits2), bits1);
459 }
460
461 /* Do it. */
462 gsi = gsi_for_stmt (inner_cond);
463 t = fold_build2 (BIT_IOR_EXPR, TREE_TYPE (name1), bits1, bits2);
464 t = force_gimple_operand_gsi (&gsi, t, true, NULL_TREE,
465 true, GSI_SAME_STMT);
466 t = fold_build2 (BIT_AND_EXPR, TREE_TYPE (name1), name1, t);
467 t = force_gimple_operand_gsi (&gsi, t, true, NULL_TREE,
468 true, GSI_SAME_STMT);
469 t = fold_build2 (result_inv ? NE_EXPR : EQ_EXPR, boolean_type_node, t,
470 build_int_cst (TREE_TYPE (t), 0));
471 t = canonicalize_cond_expr_cond (t);
472 if (!t)
473 return false;
474 gimple_cond_set_condition_from_tree (inner_cond, t);
475 update_stmt (inner_cond);
476
477 /* Leave CFG optimization to cfg_cleanup. */
478 gimple_cond_set_condition_from_tree (outer_cond,
479 outer_inv ? boolean_false_node : boolean_true_node);
480 update_stmt (outer_cond);
481
482 if (dump_file)
483 {
484 fprintf (dump_file, "optimizing bits or bits test to ");
485 print_generic_expr (dump_file, name1, 0);
486 fprintf (dump_file, " & T != 0\nwith temporary T = ");
487 print_generic_expr (dump_file, bits1, 0);
488 fprintf (dump_file, " | ");
489 print_generic_expr (dump_file, bits2, 0);
490 fprintf (dump_file, "\n");
491 }
492
493 return true;
494 }
495
496 /* See if we have two comparisons that we can merge into one. */
497 else if (TREE_CODE_CLASS (gimple_cond_code (inner_cond)) == tcc_comparison
498 && TREE_CODE_CLASS (gimple_cond_code (outer_cond)) == tcc_comparison)
499 {
500 tree t;
501 enum tree_code inner_cond_code = gimple_cond_code (inner_cond);
502 enum tree_code outer_cond_code = gimple_cond_code (outer_cond);
503
504 /* Invert comparisons if necessary (and possible). */
505 if (inner_inv)
506 inner_cond_code = invert_tree_comparison (inner_cond_code,
507 HONOR_NANS (gimple_cond_lhs (inner_cond)));
508 if (inner_cond_code == ERROR_MARK)
509 return false;
510 if (outer_inv)
511 outer_cond_code = invert_tree_comparison (outer_cond_code,
512 HONOR_NANS (gimple_cond_lhs (outer_cond)));
513 if (outer_cond_code == ERROR_MARK)
514 return false;
515 /* Don't return false so fast, try maybe_fold_or_comparisons? */
516
517 if (!(t = maybe_fold_and_comparisons (inner_cond_code,
518 gimple_cond_lhs (inner_cond),
519 gimple_cond_rhs (inner_cond),
520 outer_cond_code,
521 gimple_cond_lhs (outer_cond),
522 gimple_cond_rhs (outer_cond))))
523 {
524 tree t1, t2;
525 gimple_stmt_iterator gsi;
526 if (!LOGICAL_OP_NON_SHORT_CIRCUIT)
527 return false;
528 /* Only do this optimization if the inner bb contains only the conditional. */
529 if (!gsi_one_before_end_p (gsi_start_nondebug_after_labels_bb (inner_cond_bb)))
530 return false;
531 t1 = fold_build2_loc (gimple_location (inner_cond),
532 inner_cond_code,
533 boolean_type_node,
534 gimple_cond_lhs (inner_cond),
535 gimple_cond_rhs (inner_cond));
536 t2 = fold_build2_loc (gimple_location (outer_cond),
537 outer_cond_code,
538 boolean_type_node,
539 gimple_cond_lhs (outer_cond),
540 gimple_cond_rhs (outer_cond));
541 t = fold_build2_loc (gimple_location (inner_cond),
542 TRUTH_AND_EXPR, boolean_type_node, t1, t2);
543 if (result_inv)
544 {
545 t = fold_build1 (TRUTH_NOT_EXPR, TREE_TYPE (t), t);
546 result_inv = false;
547 }
548 gsi = gsi_for_stmt (inner_cond);
549 t = force_gimple_operand_gsi_1 (&gsi, t, is_gimple_condexpr, NULL, true,
550 GSI_SAME_STMT);
551 }
552 if (result_inv)
553 t = fold_build1 (TRUTH_NOT_EXPR, TREE_TYPE (t), t);
554 t = canonicalize_cond_expr_cond (t);
555 if (!t)
556 return false;
557 gimple_cond_set_condition_from_tree (inner_cond, t);
558 update_stmt (inner_cond);
559
560 /* Leave CFG optimization to cfg_cleanup. */
561 gimple_cond_set_condition_from_tree (outer_cond,
562 outer_inv ? boolean_false_node : boolean_true_node);
563 update_stmt (outer_cond);
564
565 if (dump_file)
566 {
567 fprintf (dump_file, "optimizing two comparisons to ");
568 print_generic_expr (dump_file, t, 0);
569 fprintf (dump_file, "\n");
570 }
571
572 return true;
573 }
574
575 return false;
576 }
577
578 /* Helper function for tree_ssa_ifcombine_bb. Recognize a CFG pattern and
579 dispatch to the appropriate if-conversion helper for a particular
580 set of INNER_COND_BB, OUTER_COND_BB, THEN_BB and ELSE_BB.
581 PHI_PRED_BB should be one of INNER_COND_BB, THEN_BB or ELSE_BB. */
582
583 static bool
584 tree_ssa_ifcombine_bb_1 (basic_block inner_cond_bb, basic_block outer_cond_bb,
585 basic_block then_bb, basic_block else_bb,
586 basic_block phi_pred_bb)
587 {
588 /* The && form is characterized by a common else_bb with
589 the two edges leading to it mergable. The latter is
590 guaranteed by matching PHI arguments in the else_bb and
591 the inner cond_bb having no side-effects. */
592 if (phi_pred_bb != else_bb
593 && recognize_if_then_else (outer_cond_bb, &inner_cond_bb, &else_bb)
594 && same_phi_args_p (outer_cond_bb, phi_pred_bb, else_bb)
595 && bb_no_side_effects_p (inner_cond_bb))
596 {
597 /* We have
598 <outer_cond_bb>
599 if (q) goto inner_cond_bb; else goto else_bb;
600 <inner_cond_bb>
601 if (p) goto ...; else goto else_bb;
602 ...
603 <else_bb>
604 ...
605 */
606 return ifcombine_ifandif (inner_cond_bb, false, outer_cond_bb, false,
607 false);
608 }
609
610 /* And a version where the outer condition is negated. */
611 if (phi_pred_bb != else_bb
612 && recognize_if_then_else (outer_cond_bb, &else_bb, &inner_cond_bb)
613 && same_phi_args_p (outer_cond_bb, phi_pred_bb, else_bb)
614 && bb_no_side_effects_p (inner_cond_bb))
615 {
616 /* We have
617 <outer_cond_bb>
618 if (q) goto else_bb; else goto inner_cond_bb;
619 <inner_cond_bb>
620 if (p) goto ...; else goto else_bb;
621 ...
622 <else_bb>
623 ...
624 */
625 return ifcombine_ifandif (inner_cond_bb, false, outer_cond_bb, true,
626 false);
627 }
628
629 /* The || form is characterized by a common then_bb with the
630 two edges leading to it mergable. The latter is guaranteed
631 by matching PHI arguments in the then_bb and the inner cond_bb
632 having no side-effects. */
633 if (phi_pred_bb != then_bb
634 && recognize_if_then_else (outer_cond_bb, &then_bb, &inner_cond_bb)
635 && same_phi_args_p (outer_cond_bb, phi_pred_bb, then_bb)
636 && bb_no_side_effects_p (inner_cond_bb))
637 {
638 /* We have
639 <outer_cond_bb>
640 if (q) goto then_bb; else goto inner_cond_bb;
641 <inner_cond_bb>
642 if (q) goto then_bb; else goto ...;
643 <then_bb>
644 ...
645 */
646 return ifcombine_ifandif (inner_cond_bb, true, outer_cond_bb, true,
647 true);
648 }
649
650 /* And a version where the outer condition is negated. */
651 if (phi_pred_bb != then_bb
652 && recognize_if_then_else (outer_cond_bb, &inner_cond_bb, &then_bb)
653 && same_phi_args_p (outer_cond_bb, phi_pred_bb, then_bb)
654 && bb_no_side_effects_p (inner_cond_bb))
655 {
656 /* We have
657 <outer_cond_bb>
658 if (q) goto inner_cond_bb; else goto then_bb;
659 <inner_cond_bb>
660 if (q) goto then_bb; else goto ...;
661 <then_bb>
662 ...
663 */
664 return ifcombine_ifandif (inner_cond_bb, true, outer_cond_bb, false,
665 true);
666 }
667
668 return false;
669 }
670
671 /* Recognize a CFG pattern and dispatch to the appropriate
672 if-conversion helper. We start with BB as the innermost
673 worker basic-block. Returns true if a transformation was done. */
674
675 static bool
676 tree_ssa_ifcombine_bb (basic_block inner_cond_bb)
677 {
678 basic_block then_bb = NULL, else_bb = NULL;
679
680 if (!recognize_if_then_else (inner_cond_bb, &then_bb, &else_bb))
681 return false;
682
683 /* Recognize && and || of two conditions with a common
684 then/else block which entry edges we can merge. That is:
685 if (a || b)
686 ;
687 and
688 if (a && b)
689 ;
690 This requires a single predecessor of the inner cond_bb. */
691 if (single_pred_p (inner_cond_bb))
692 {
693 basic_block outer_cond_bb = single_pred (inner_cond_bb);
694
695 if (tree_ssa_ifcombine_bb_1 (inner_cond_bb, outer_cond_bb,
696 then_bb, else_bb, inner_cond_bb))
697 return true;
698
699 if (forwarder_block_to (else_bb, then_bb))
700 {
701 /* Other possibilities for the && form, if else_bb is
702 empty forwarder block to then_bb. Compared to the above simpler
703 forms this can be treated as if then_bb and else_bb were swapped,
704 and the corresponding inner_cond_bb not inverted because of that.
705 For same_phi_args_p we look at equality of arguments between
706 edge from outer_cond_bb and the forwarder block. */
707 if (tree_ssa_ifcombine_bb_1 (inner_cond_bb, outer_cond_bb, else_bb,
708 then_bb, else_bb))
709 return true;
710 }
711 else if (forwarder_block_to (then_bb, else_bb))
712 {
713 /* Other possibilities for the || form, if then_bb is
714 empty forwarder block to else_bb. Compared to the above simpler
715 forms this can be treated as if then_bb and else_bb were swapped,
716 and the corresponding inner_cond_bb not inverted because of that.
717 For same_phi_args_p we look at equality of arguments between
718 edge from outer_cond_bb and the forwarder block. */
719 if (tree_ssa_ifcombine_bb_1 (inner_cond_bb, outer_cond_bb, else_bb,
720 then_bb, then_bb))
721 return true;
722 }
723 }
724
725 return false;
726 }
727
728 /* Main entry for the tree if-conversion pass. */
729
730 namespace {
731
732 const pass_data pass_data_tree_ifcombine =
733 {
734 GIMPLE_PASS, /* type */
735 "ifcombine", /* name */
736 OPTGROUP_NONE, /* optinfo_flags */
737 TV_TREE_IFCOMBINE, /* tv_id */
738 ( PROP_cfg | PROP_ssa ), /* properties_required */
739 0, /* properties_provided */
740 0, /* properties_destroyed */
741 0, /* todo_flags_start */
742 TODO_update_ssa, /* todo_flags_finish */
743 };
744
745 class pass_tree_ifcombine : public gimple_opt_pass
746 {
747 public:
748 pass_tree_ifcombine (gcc::context *ctxt)
749 : gimple_opt_pass (pass_data_tree_ifcombine, ctxt)
750 {}
751
752 /* opt_pass methods: */
753 virtual unsigned int execute (function *);
754
755 }; // class pass_tree_ifcombine
756
757 unsigned int
758 pass_tree_ifcombine::execute (function *fun)
759 {
760 basic_block *bbs;
761 bool cfg_changed = false;
762 int i;
763
764 bbs = single_pred_before_succ_order ();
765 calculate_dominance_info (CDI_DOMINATORS);
766
767 /* Search every basic block for COND_EXPR we may be able to optimize.
768
769 We walk the blocks in order that guarantees that a block with
770 a single predecessor is processed after the predecessor.
771 This ensures that we collapse outter ifs before visiting the
772 inner ones, and also that we do not try to visit a removed
773 block. This is opposite of PHI-OPT, because we cascade the
774 combining rather than cascading PHIs. */
775 for (i = n_basic_blocks_for_fn (fun) - NUM_FIXED_BLOCKS - 1; i >= 0; i--)
776 {
777 basic_block bb = bbs[i];
778 gimple stmt = last_stmt (bb);
779
780 if (stmt
781 && gimple_code (stmt) == GIMPLE_COND)
782 cfg_changed |= tree_ssa_ifcombine_bb (bb);
783 }
784
785 free (bbs);
786
787 return cfg_changed ? TODO_cleanup_cfg : 0;
788 }
789
790 } // anon namespace
791
792 gimple_opt_pass *
793 make_pass_tree_ifcombine (gcc::context *ctxt)
794 {
795 return new pass_tree_ifcombine (ctxt);
796 }