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