1 /* Combining of if-expressions on trees.
2 Copyright (C) 2007 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 2, or (at your option)
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 COPYING. If not, write to
19 the Free Software Foundation, 51 Franklin Street, Fifth Floor,
20 Boston, MA 02110-1301, USA. */
24 #include "coretypes.h"
27 #include "basic-block.h"
29 #include "diagnostic.h"
30 #include "tree-flow.h"
31 #include "tree-pass.h"
32 #include "tree-dump.h"
34 /* This pass combines COND_EXPRs to simplify control flow. It
35 currently recognizes bit tests and comparisons in chains that
36 represent logical and or logical or of two COND_EXPRs.
38 It does so by walking basic blocks in a approximate reverse
39 post-dominator order and trying to match CFG patterns that
40 represent logical and or logical or of two COND_EXPRs.
41 Transformations are done if the COND_EXPR conditions match
44 1. two single bit tests X & (1 << Yn) (for logical and)
46 2. two bit tests X & Yn (for logical or)
48 3. two comparisons X OPn Y (for logical or)
50 To simplify this pass, removing basic blocks and dead code
51 is left to CFG cleanup and DCE. */
54 /* Recognize a if-then-else CFG pattern starting to match with the
55 COND_BB basic-block containing the COND_EXPR. The recognized
56 then end else blocks are stored to *THEN_BB and *ELSE_BB. If
57 *THEN_BB and/or *ELSE_BB are already set, they are required to
58 match the then and else basic-blocks to make the pattern match.
59 Returns true if the pattern matched, false otherwise. */
62 recognize_if_then_else (basic_block cond_bb,
63 basic_block *then_bb, basic_block *else_bb)
67 if (EDGE_COUNT (cond_bb->succs) != 2)
70 /* Find the then/else edges. */
71 t = EDGE_SUCC (cond_bb, 0);
72 e = EDGE_SUCC (cond_bb, 1);
73 if (!(t->flags & EDGE_TRUE_VALUE))
79 if (!(t->flags & EDGE_TRUE_VALUE)
80 || !(e->flags & EDGE_FALSE_VALUE))
83 /* Check if the edge destinations point to the required block. */
85 && t->dest != *then_bb)
88 && e->dest != *else_bb)
99 /* Verify if the basic block BB does not have side-effects. Return
100 true in this case, else false. */
103 bb_no_side_effects_p (basic_block bb)
105 block_stmt_iterator bsi;
107 for (bsi = bsi_start (bb); !bsi_end_p (bsi); bsi_next (&bsi))
109 tree stmt = bsi_stmt (bsi);
110 stmt_ann_t ann = stmt_ann (stmt);
112 if (ann->has_volatile_ops
113 || !ZERO_SSA_OPERANDS (stmt, SSA_OP_ALL_VIRTUALS))
120 /* Verify if all PHI node arguments in DEST for edges from BB1 or
121 BB2 to DEST are the same. This makes the CFG merge point
122 free from side-effects. Return true in this case, else false. */
125 same_phi_args_p (basic_block bb1, basic_block bb2, basic_block dest)
127 edge e1 = find_edge (bb1, dest);
128 edge e2 = find_edge (bb2, dest);
131 for (phi = phi_nodes (dest); phi; phi = PHI_CHAIN (phi))
132 if (!operand_equal_p (PHI_ARG_DEF_FROM_EDGE (phi, e1),
133 PHI_ARG_DEF_FROM_EDGE (phi, e2), 0))
139 /* Recognize a single bit test pattern in COND_EXPR and its defining
140 statements. Store the name being tested in *NAME and the bit
141 in *BIT. The COND_EXPR computes *NAME & (1 << *BIT).
142 Returns true if the pattern matched, false otherwise. */
145 recognize_single_bit_test (tree cond_expr, tree *name, tree *bit)
149 /* Get at the definition of the result of the bit test. */
150 t = TREE_OPERAND (cond_expr, 0);
151 if (TREE_CODE (t) == NE_EXPR
152 && integer_zerop (TREE_OPERAND (t, 1)))
153 t = TREE_OPERAND (t, 0);
154 if (TREE_CODE (t) != SSA_NAME)
156 t = SSA_NAME_DEF_STMT (t);
157 if (TREE_CODE (t) != GIMPLE_MODIFY_STMT)
159 t = GIMPLE_STMT_OPERAND (t, 1);
161 /* Look at which bit is tested. One form to recognize is
162 D.1985_5 = state_3(D) >> control1_4(D);
163 D.1986_6 = (int) D.1985_5;
165 if (D.1987_7 != 0) */
166 if (TREE_CODE (t) == BIT_AND_EXPR
167 && integer_onep (TREE_OPERAND (t, 1))
168 && TREE_CODE (TREE_OPERAND (t, 0)) == SSA_NAME)
170 t = TREE_OPERAND (t, 0);
172 t = SSA_NAME_DEF_STMT (t);
173 if (TREE_CODE (t) != GIMPLE_MODIFY_STMT)
175 t = GIMPLE_STMT_OPERAND (t, 1);
176 if (TREE_CODE (t) == NOP_EXPR
177 || TREE_CODE (t) == CONVERT_EXPR)
178 t = TREE_OPERAND (t, 0);
179 } while (TREE_CODE (t) == SSA_NAME);
181 if (TREE_CODE (t) == RSHIFT_EXPR)
183 /* op0 & (1 << op1) */
184 *bit = TREE_OPERAND (t, 1);
185 *name = TREE_OPERAND (t, 0);
190 *bit = integer_one_node;
198 D.1987_7 = op0 & (1 << CST)
199 if (D.1987_7 != 0) */
200 if (TREE_CODE (t) == BIT_AND_EXPR
201 && TREE_CODE (TREE_OPERAND (t, 0)) == SSA_NAME
202 && integer_pow2p (TREE_OPERAND (t, 1)))
204 *name = TREE_OPERAND (t, 0);
205 *bit = build_int_cst (integer_type_node,
206 tree_log2 (TREE_OPERAND (t, 1)));
211 D.1986_6 = 1 << control1_4(D)
212 D.1987_7 = op0 & D.1986_6
213 if (D.1987_7 != 0) */
214 if (TREE_CODE (t) == BIT_AND_EXPR
215 && TREE_CODE (TREE_OPERAND (t, 0)) == SSA_NAME
216 && TREE_CODE (TREE_OPERAND (t, 1)) == SSA_NAME)
220 /* Both arguments of the BIT_AND_EXPR can be the single-bit
221 specifying expression. */
222 tmp = SSA_NAME_DEF_STMT (TREE_OPERAND (t, 0));
223 if (TREE_CODE (tmp) == GIMPLE_MODIFY_STMT
224 && TREE_CODE (GIMPLE_STMT_OPERAND (tmp, 1)) == LSHIFT_EXPR
225 && integer_onep (TREE_OPERAND (GIMPLE_STMT_OPERAND (tmp, 1), 0)))
227 *name = TREE_OPERAND (t, 1);
228 *bit = TREE_OPERAND (GIMPLE_STMT_OPERAND (tmp, 1), 1);
232 tmp = SSA_NAME_DEF_STMT (TREE_OPERAND (t, 1));
233 if (TREE_CODE (tmp) == GIMPLE_MODIFY_STMT
234 && TREE_CODE (GIMPLE_STMT_OPERAND (tmp, 1)) == LSHIFT_EXPR
235 && integer_onep (TREE_OPERAND (GIMPLE_STMT_OPERAND (tmp, 1), 0)))
237 *name = TREE_OPERAND (t, 0);
238 *bit = TREE_OPERAND (GIMPLE_STMT_OPERAND (tmp, 1), 1);
246 /* Recognize a bit test pattern in COND_EXPR and its defining
247 statements. Store the name being tested in *NAME and the bits
248 in *BITS. The COND_EXPR computes *NAME & *BITS.
249 Returns true if the pattern matched, false otherwise. */
252 recognize_bits_test (tree cond_expr, tree *name, tree *bits)
256 /* Get at the definition of the result of the bit test. */
257 t = TREE_OPERAND (cond_expr, 0);
258 if (TREE_CODE (t) == NE_EXPR
259 && integer_zerop (TREE_OPERAND (t, 1)))
260 t = TREE_OPERAND (t, 0);
261 if (TREE_CODE (t) != SSA_NAME)
263 t = SSA_NAME_DEF_STMT (t);
264 if (TREE_CODE (t) != GIMPLE_MODIFY_STMT)
266 t = GIMPLE_STMT_OPERAND (t, 1);
268 if (TREE_CODE (t) != BIT_AND_EXPR)
271 *name = TREE_OPERAND (t, 0);
272 *bits = TREE_OPERAND (t, 1);
277 /* If-convert on a and pattern with a common else block. The inner
278 if is specified by its INNER_COND_BB, the outer by OUTER_COND_BB.
279 Returns true if the edges to the common else basic-block were merged. */
282 ifcombine_ifandif (basic_block inner_cond_bb, basic_block outer_cond_bb)
284 block_stmt_iterator bsi;
285 tree inner_cond, outer_cond;
286 tree name1, name2, bit1, bit2;
288 inner_cond = last_stmt (inner_cond_bb);
290 || TREE_CODE (inner_cond) != COND_EXPR)
293 outer_cond = last_stmt (outer_cond_bb);
295 || TREE_CODE (outer_cond) != COND_EXPR)
298 /* See if we test a single bit of the same name in both tests. In
299 that case remove the outer test, merging both else edges,
300 and change the inner one to test for
301 name & (bit1 | bit2) == (bit1 | bit2). */
302 if (recognize_single_bit_test (inner_cond, &name1, &bit1)
303 && recognize_single_bit_test (outer_cond, &name2, &bit2)
309 bsi = bsi_for_stmt (inner_cond);
310 t = fold_build2 (LSHIFT_EXPR, TREE_TYPE (name1),
311 integer_one_node, bit1);
312 t2 = fold_build2 (LSHIFT_EXPR, TREE_TYPE (name1),
313 integer_one_node, bit2);
314 t = fold_build2 (BIT_IOR_EXPR, TREE_TYPE (name1), t, t2);
315 t = force_gimple_operand_bsi (&bsi, t, true, NULL_TREE);
316 t2 = fold_build2 (BIT_AND_EXPR, TREE_TYPE (name1), name1, t);
317 t2 = force_gimple_operand_bsi (&bsi, t2, true, NULL_TREE);
318 COND_EXPR_COND (inner_cond) = fold_build2 (EQ_EXPR, boolean_type_node,
320 update_stmt (inner_cond);
322 /* Leave CFG optimization to cfg_cleanup. */
323 COND_EXPR_COND (outer_cond) = boolean_true_node;
324 update_stmt (outer_cond);
328 fprintf (dump_file, "optimizing double bit test to ");
329 print_generic_expr (dump_file, name1, 0);
330 fprintf (dump_file, " & T == T\nwith temporary T = (1 << ");
331 print_generic_expr (dump_file, bit1, 0);
332 fprintf (dump_file, ") | (1 << ");
333 print_generic_expr (dump_file, bit2, 0);
334 fprintf (dump_file, ")\n");
343 /* If-convert on a or pattern with a common then block. The inner
344 if is specified by its INNER_COND_BB, the outer by OUTER_COND_BB.
345 Returns true, if the edges leading to the common then basic-block
349 ifcombine_iforif (basic_block inner_cond_bb, basic_block outer_cond_bb)
351 tree inner_cond, outer_cond;
352 tree name1, name2, bits1, bits2;
354 inner_cond = last_stmt (inner_cond_bb);
356 || TREE_CODE (inner_cond) != COND_EXPR)
359 outer_cond = last_stmt (outer_cond_bb);
361 || TREE_CODE (outer_cond) != COND_EXPR)
364 /* See if we have two bit tests of the same name in both tests.
365 In that case remove the outer test and change the inner one to
366 test for name & (bits1 | bits2) != 0. */
367 if (recognize_bits_test (inner_cond, &name1, &bits1)
368 && recognize_bits_test (outer_cond, &name2, &bits2))
370 block_stmt_iterator bsi;
373 /* Find the common name which is bit-tested. */
376 else if (bits1 == bits2)
385 else if (name1 == bits2)
391 else if (bits1 == name2)
401 bsi = bsi_for_stmt (inner_cond);
402 t = fold_build2 (BIT_IOR_EXPR, TREE_TYPE (name1), bits1, bits2);
403 t = force_gimple_operand_bsi (&bsi, t, true, NULL_TREE);
404 t = fold_build2 (BIT_AND_EXPR, TREE_TYPE (name1), name1, t);
405 t = force_gimple_operand_bsi (&bsi, t, true, NULL_TREE);
406 COND_EXPR_COND (inner_cond) = fold_build2 (NE_EXPR, boolean_type_node, t,
407 build_int_cst (TREE_TYPE (t), 0));
408 update_stmt (inner_cond);
410 /* Leave CFG optimization to cfg_cleanup. */
411 COND_EXPR_COND (outer_cond) = boolean_false_node;
412 update_stmt (outer_cond);
416 fprintf (dump_file, "optimizing bits or bits test to ");
417 print_generic_expr (dump_file, name1, 0);
418 fprintf (dump_file, " & T != 0\nwith temporary T = ");
419 print_generic_expr (dump_file, bits1, 0);
420 fprintf (dump_file, " | ");
421 print_generic_expr (dump_file, bits2, 0);
422 fprintf (dump_file, "\n");
428 /* See if we have two comparisons that we can merge into one.
429 This happens for C++ operator overloading where for example
430 GE_EXPR is implemented as GT_EXPR || EQ_EXPR. */
431 else if (COMPARISON_CLASS_P (COND_EXPR_COND (inner_cond))
432 && COMPARISON_CLASS_P (COND_EXPR_COND (outer_cond))
433 && operand_equal_p (TREE_OPERAND (COND_EXPR_COND (inner_cond), 0),
434 TREE_OPERAND (COND_EXPR_COND (outer_cond), 0), 0)
435 && operand_equal_p (TREE_OPERAND (COND_EXPR_COND (inner_cond), 1),
436 TREE_OPERAND (COND_EXPR_COND (outer_cond), 1), 0))
438 tree ccond1 = COND_EXPR_COND (inner_cond);
439 tree ccond2 = COND_EXPR_COND (outer_cond);
440 enum tree_code code1 = TREE_CODE (ccond1);
441 enum tree_code code2 = TREE_CODE (ccond2);
445 #define CHK(a,b) ((code1 == a ## _EXPR && code2 == b ## _EXPR) \
446 || (code2 == a ## _EXPR && code1 == b ## _EXPR))
447 /* Merge the two condition codes if possible. */
450 else if (CHK (EQ, LT))
452 else if (CHK (EQ, GT))
454 else if (CHK (LT, LE))
456 else if (CHK (GT, GE))
458 else if (INTEGRAL_TYPE_P (TREE_TYPE (TREE_OPERAND (ccond1, 0)))
459 || flag_unsafe_math_optimizations)
463 else if (CHK (LT, NE))
465 else if (CHK (GT, NE))
470 /* We could check for combinations leading to trivial true/false. */
476 t = fold_build2 (code, boolean_type_node,
477 TREE_OPERAND (ccond2, 0), TREE_OPERAND (ccond2, 1));
478 COND_EXPR_COND (inner_cond) = t;
479 update_stmt (inner_cond);
481 /* Leave CFG optimization to cfg_cleanup. */
482 COND_EXPR_COND (outer_cond) = boolean_false_node;
483 update_stmt (outer_cond);
487 fprintf (dump_file, "optimizing two comparisons to ");
488 print_generic_expr (dump_file, t, 0);
489 fprintf (dump_file, "\n");
498 /* Recognize a CFG pattern and dispatch to the appropriate
499 if-conversion helper. We start with BB as the innermost
500 worker basic-block. Returns true if a transformation was done. */
503 tree_ssa_ifcombine_bb (basic_block inner_cond_bb)
505 basic_block then_bb = NULL, else_bb = NULL;
507 if (!recognize_if_then_else (inner_cond_bb, &then_bb, &else_bb))
510 /* Recognize && and || of two conditions with a common
511 then/else block which entry edges we can merge. That is:
517 This requires a single predecessor of the inner cond_bb. */
518 if (single_pred_p (inner_cond_bb))
520 basic_block outer_cond_bb = single_pred (inner_cond_bb);
522 /* The && form is characterized by a common else_bb with
523 the two edges leading to it mergable. The latter is
524 guaranteed by matching PHI arguments in the else_bb and
525 the inner cond_bb having no side-effects. */
526 if (recognize_if_then_else (outer_cond_bb, &inner_cond_bb, &else_bb)
527 && same_phi_args_p (outer_cond_bb, inner_cond_bb, else_bb)
528 && bb_no_side_effects_p (inner_cond_bb))
532 if (q) goto inner_cond_bb; else goto else_bb;
534 if (p) goto ...; else goto else_bb;
539 return ifcombine_ifandif (inner_cond_bb, outer_cond_bb);
542 /* The || form is characterized by a common then_bb with the
543 two edges leading to it mergable. The latter is guaranteed
544 by matching PHI arguments in the then_bb and the inner cond_bb
545 having no side-effects. */
546 if (recognize_if_then_else (outer_cond_bb, &then_bb, &inner_cond_bb)
547 && same_phi_args_p (outer_cond_bb, inner_cond_bb, then_bb)
548 && bb_no_side_effects_p (inner_cond_bb))
552 if (q) goto then_bb; else goto inner_cond_bb;
554 if (q) goto then_bb; else goto ...;
558 return ifcombine_iforif (inner_cond_bb, outer_cond_bb);
565 /* Main entry for the tree if-conversion pass. */
568 tree_ssa_ifcombine (void)
571 bool cfg_changed = false;
574 bbs = blocks_in_phiopt_order ();
576 for (i = 0; i < n_basic_blocks - NUM_FIXED_BLOCKS; ++i)
578 basic_block bb = bbs[i];
579 tree stmt = last_stmt (bb);
582 && TREE_CODE (stmt) == COND_EXPR)
583 cfg_changed |= tree_ssa_ifcombine_bb (bb);
588 return cfg_changed ? TODO_cleanup_cfg : 0;
592 gate_ifcombine (void)
597 struct tree_opt_pass pass_tree_ifcombine = {
598 "ifcombine", /* name */
599 gate_ifcombine, /* gate */
600 tree_ssa_ifcombine, /* execute */
603 0, /* static_pass_number */
604 TV_TREE_IFCOMBINE, /* tv_id */
605 PROP_cfg | PROP_ssa, /* properties_required */
606 0, /* properties_provided */
607 0, /* properties_destroyed */
608 0, /* todo_flags_start */
612 | TODO_verify_ssa, /* todo_flags_finish */