Remove conditions in the code generation of if-conversion.
[gcc.git] / gcc / tree-if-conv.c
1 /* If-conversion for vectorizer.
2 Copyright (C) 2004, 2005, 2006, 2007, 2008, 2009, 2010
3 Free Software Foundation, Inc.
4 Contributed by Devang Patel <dpatel@apple.com>
5
6 This file is part of GCC.
7
8 GCC is free software; you can redistribute it and/or modify it under
9 the terms of the GNU General Public License as published by the Free
10 Software Foundation; either version 3, or (at your option) any later
11 version.
12
13 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
14 WARRANTY; without even the implied warranty of MERCHANTABILITY or
15 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
16 for more details.
17
18 You should have received a copy of the GNU General Public License
19 along with GCC; see the file COPYING3. If not see
20 <http://www.gnu.org/licenses/>. */
21
22 /* This pass implements a tree level if-conversion of loops. Its
23 initial goal is to help the vectorizer to vectorize loops with
24 conditions.
25
26 A short description of if-conversion:
27
28 o Decide if a loop is if-convertible or not.
29 o Walk all loop basic blocks in breadth first order (BFS order).
30 o Remove conditional statements (at the end of basic block)
31 and propagate condition into destination basic blocks'
32 predicate list.
33 o Replace modify expression with conditional modify expression
34 using current basic block's condition.
35 o Merge all basic blocks
36 o Replace phi nodes with conditional modify expr
37 o Merge all basic blocks into header
38
39 Sample transformation:
40
41 INPUT
42 -----
43
44 # i_23 = PHI <0(0), i_18(10)>;
45 <L0>:;
46 j_15 = A[i_23];
47 if (j_15 > 41) goto <L1>; else goto <L17>;
48
49 <L17>:;
50 goto <bb 3> (<L3>);
51
52 <L1>:;
53
54 # iftmp.2_4 = PHI <0(8), 42(2)>;
55 <L3>:;
56 A[i_23] = iftmp.2_4;
57 i_18 = i_23 + 1;
58 if (i_18 <= 15) goto <L19>; else goto <L18>;
59
60 <L19>:;
61 goto <bb 1> (<L0>);
62
63 <L18>:;
64
65 OUTPUT
66 ------
67
68 # i_23 = PHI <0(0), i_18(10)>;
69 <L0>:;
70 j_15 = A[i_23];
71
72 <L3>:;
73 iftmp.2_4 = j_15 > 41 ? 42 : 0;
74 A[i_23] = iftmp.2_4;
75 i_18 = i_23 + 1;
76 if (i_18 <= 15) goto <L19>; else goto <L18>;
77
78 <L19>:;
79 goto <bb 1> (<L0>);
80
81 <L18>:;
82 */
83
84 #include "config.h"
85 #include "system.h"
86 #include "coretypes.h"
87 #include "tm.h"
88 #include "tree.h"
89 #include "flags.h"
90 #include "timevar.h"
91 #include "basic-block.h"
92 #include "diagnostic.h"
93 #include "tree-pretty-print.h"
94 #include "gimple-pretty-print.h"
95 #include "tree-flow.h"
96 #include "tree-dump.h"
97 #include "cfgloop.h"
98 #include "tree-chrec.h"
99 #include "tree-data-ref.h"
100 #include "tree-scalar-evolution.h"
101 #include "tree-pass.h"
102
103 /* List of basic blocks in if-conversion-suitable order. */
104 static basic_block *ifc_bbs;
105
106 /* Create a new temp variable of type TYPE. Add GIMPLE_ASSIGN to assign EXP
107 to the new variable. */
108
109 static gimple
110 ifc_temp_var (tree type, tree exp)
111 {
112 const char *name = "_ifc_";
113 tree var, new_name;
114 gimple stmt;
115
116 /* Create new temporary variable. */
117 var = create_tmp_var (type, name);
118 add_referenced_var (var);
119
120 /* Build new statement to assign EXP to new variable. */
121 stmt = gimple_build_assign (var, exp);
122
123 /* Get SSA name for the new variable and set make new statement
124 its definition statement. */
125 new_name = make_ssa_name (var, stmt);
126 gimple_assign_set_lhs (stmt, new_name);
127 SSA_NAME_DEF_STMT (new_name) = stmt;
128 update_stmt (stmt);
129
130 return stmt;
131 }
132
133 /* Add condition NEW_COND to the predicate list of basic block BB. */
134
135 static void
136 add_to_predicate_list (basic_block bb, tree new_cond)
137 {
138 tree cond = (tree) bb->aux;
139
140 if (cond)
141 cond = fold_build2_loc (EXPR_LOCATION (cond),
142 TRUTH_OR_EXPR, boolean_type_node,
143 unshare_expr (cond), new_cond);
144 else
145 cond = new_cond;
146
147 bb->aux = cond;
148 }
149
150 /* Add the condition COND to the previous condition PREV_COND, and add this
151 to the predicate list of the destination of edge E. GSI is the
152 place where the gimplification of the resulting condition should
153 output code. LOOP is the loop to be if-converted. */
154
155 static tree
156 add_to_dst_predicate_list (struct loop *loop, edge e,
157 tree prev_cond, tree cond,
158 gimple_stmt_iterator *gsi)
159 {
160 tree new_cond = NULL_TREE;
161
162 if (!flow_bb_inside_loop_p (loop, e->dest))
163 return NULL_TREE;
164
165 if (prev_cond == boolean_true_node || !prev_cond)
166 new_cond = unshare_expr (cond);
167 else
168 {
169 tree tmp;
170 gimple tmp_stmt = NULL;
171
172 prev_cond = force_gimple_operand_gsi (gsi, unshare_expr (prev_cond),
173 true, NULL, true, GSI_SAME_STMT);
174
175 cond = force_gimple_operand_gsi (gsi, unshare_expr (cond),
176 true, NULL, true, GSI_SAME_STMT);
177
178 /* Add the condition COND to the e->aux field. In case the edge
179 destination is a PHI node, this condition will be added to
180 the block predicate to construct a complete condition. */
181 e->aux = cond;
182
183 tmp = build2 (TRUTH_AND_EXPR, boolean_type_node,
184 unshare_expr (prev_cond), cond);
185 tmp_stmt = ifc_temp_var (boolean_type_node, tmp);
186 gsi_insert_before (gsi, tmp_stmt, GSI_SAME_STMT);
187 new_cond = gimple_assign_lhs (tmp_stmt);
188 }
189
190 add_to_predicate_list (e->dest, new_cond);
191 return new_cond;
192 }
193
194 /* Return true if one of the successor edges of BB exits LOOP. */
195
196 static bool
197 bb_with_exit_edge_p (struct loop *loop, basic_block bb)
198 {
199 edge e;
200 edge_iterator ei;
201
202 FOR_EACH_EDGE (e, ei, bb->succs)
203 if (loop_exit_edge_p (loop, e))
204 return true;
205
206 return false;
207 }
208
209 /* STMT is a GIMPLE_COND. Update two destination's predicate list.
210 Otherwise update the exit condition of LOOP appropriately. GSI
211 points to the statement STMT. */
212
213 static void
214 tree_if_convert_cond_stmt (struct loop *loop, gimple stmt, tree cond,
215 gimple_stmt_iterator *gsi)
216 {
217 tree c2;
218 edge true_edge, false_edge;
219 location_t loc = gimple_location (stmt);
220 tree c = fold_build2_loc (loc, gimple_cond_code (stmt), boolean_type_node,
221 gimple_cond_lhs (stmt), gimple_cond_rhs (stmt));
222
223 extract_true_false_edges_from_block (gimple_bb (stmt),
224 &true_edge, &false_edge);
225
226 /* Add new condition into destination's predicate list. */
227
228 /* If C is true, then TRUE_EDGE is taken. */
229 add_to_dst_predicate_list (loop, true_edge, cond, c, gsi);
230
231 /* If C is false, then FALSE_EDGE is taken. */
232 c2 = invert_truthvalue_loc (loc, unshare_expr (c));
233 add_to_dst_predicate_list (loop, false_edge, cond, c2, gsi);
234 }
235
236 /* If-convert stmt T which is part of LOOP.
237
238 For conditional expressions, add a condition in the destination
239 basic block's predicate list. GSI points to the statement T. */
240
241 static tree
242 tree_if_convert_stmt (struct loop *loop, gimple t, tree cond,
243 gimple_stmt_iterator *gsi)
244 {
245 if (dump_file && (dump_flags & TDF_DETAILS))
246 {
247 fprintf (dump_file, "------if-convert stmt\n");
248 print_gimple_stmt (dump_file, t, 0, TDF_SLIM);
249 print_generic_stmt (dump_file, cond, TDF_SLIM);
250 }
251
252 switch (gimple_code (t))
253 {
254 /* Labels are harmless here. */
255 case GIMPLE_LABEL:
256 break;
257
258 case GIMPLE_DEBUG:
259 /* ??? Should there be conditional GIMPLE_DEBUG_BINDs? */
260 if (gimple_debug_bind_p (gsi_stmt (*gsi)))
261 {
262 gimple_debug_bind_reset_value (gsi_stmt (*gsi));
263 update_stmt (gsi_stmt (*gsi));
264 }
265 break;
266
267 case GIMPLE_ASSIGN:
268 /* This GIMPLE_ASSIGN is killing previous value of LHS. Appropriate
269 value will be selected by PHI node based on condition. It is possible
270 that before this transformation, PHI nodes was selecting default
271 value and now it will use this new value. This is OK because it does
272 not change the validity of the program. */
273 break;
274
275 case GIMPLE_COND:
276 tree_if_convert_cond_stmt (loop, t, cond, gsi);
277 cond = NULL_TREE;
278 break;
279
280 default:
281 gcc_unreachable ();
282 }
283
284 return cond;
285 }
286
287 /* Return true when PHI is if-convertible. PHI is part of loop LOOP
288 and it belongs to basic block BB.
289
290 PHI is not if-convertible if:
291 - it has more than 2 arguments,
292 - virtual PHI is immediately used in another PHI node,
293 - virtual PHI on BB other than header. */
294
295 static bool
296 if_convertible_phi_p (struct loop *loop, basic_block bb, gimple phi)
297 {
298 if (dump_file && (dump_flags & TDF_DETAILS))
299 {
300 fprintf (dump_file, "-------------------------\n");
301 print_gimple_stmt (dump_file, phi, 0, TDF_SLIM);
302 }
303
304 if (bb != loop->header && gimple_phi_num_args (phi) != 2)
305 {
306 if (dump_file && (dump_flags & TDF_DETAILS))
307 fprintf (dump_file, "More than two phi node args.\n");
308 return false;
309 }
310
311 if (!is_gimple_reg (SSA_NAME_VAR (gimple_phi_result (phi))))
312 {
313 imm_use_iterator imm_iter;
314 use_operand_p use_p;
315
316 if (bb != loop->header)
317 {
318 if (dump_file && (dump_flags & TDF_DETAILS))
319 fprintf (dump_file, "Virtual phi not on loop header.\n");
320 return false;
321 }
322 FOR_EACH_IMM_USE_FAST (use_p, imm_iter, gimple_phi_result (phi))
323 {
324 if (gimple_code (USE_STMT (use_p)) == GIMPLE_PHI)
325 {
326 if (dump_file && (dump_flags & TDF_DETAILS))
327 fprintf (dump_file, "Difficult to handle this virtual phi.\n");
328 return false;
329 }
330 }
331 }
332
333 return true;
334 }
335
336 /* Return true when STMT is if-convertible.
337
338 GIMPLE_ASSIGN statement is not if-convertible if,
339 - it is not movable,
340 - it could trap,
341 - LHS is not var decl.
342
343 GIMPLE_ASSIGN is part of block BB, which is inside loop LOOP. */
344
345 static bool
346 if_convertible_gimple_assign_stmt_p (struct loop *loop, basic_block bb,
347 gimple stmt)
348 {
349 tree lhs = gimple_assign_lhs (stmt);
350
351 if (dump_file && (dump_flags & TDF_DETAILS))
352 {
353 fprintf (dump_file, "-------------------------\n");
354 print_gimple_stmt (dump_file, stmt, 0, TDF_SLIM);
355 }
356
357 /* Some of these constrains might be too conservative. */
358 if (stmt_ends_bb_p (stmt)
359 || gimple_has_volatile_ops (stmt)
360 || (TREE_CODE (lhs) == SSA_NAME
361 && SSA_NAME_OCCURS_IN_ABNORMAL_PHI (lhs))
362 || gimple_has_side_effects (stmt))
363 {
364 if (dump_file && (dump_flags & TDF_DETAILS))
365 fprintf (dump_file, "stmt not suitable for ifcvt\n");
366 return false;
367 }
368
369 /* See if it needs speculative loading or not. */
370 if (bb != loop->header
371 && gimple_assign_rhs_could_trap_p (stmt))
372 {
373 if (dump_file && (dump_flags & TDF_DETAILS))
374 fprintf (dump_file, "tree could trap...\n");
375 return false;
376 }
377
378 if (TREE_CODE (lhs) != SSA_NAME
379 && bb != loop->header
380 && !bb_with_exit_edge_p (loop, bb))
381 {
382 if (dump_file && (dump_flags & TDF_DETAILS))
383 {
384 fprintf (dump_file, "LHS is not var\n");
385 print_gimple_stmt (dump_file, stmt, 0, TDF_SLIM);
386 }
387 return false;
388 }
389
390 return true;
391 }
392
393 /* Return true when STMT is if-convertible.
394
395 A statement is if-convertible if:
396 - it is an if-convertible GIMPLE_ASSGIN,
397 - it is a GIMPLE_LABEL or a GIMPLE_COND.
398
399 STMT is inside BB, which is inside loop LOOP. */
400
401 static bool
402 if_convertible_stmt_p (struct loop *loop, basic_block bb, gimple stmt)
403 {
404 switch (gimple_code (stmt))
405 {
406 case GIMPLE_LABEL:
407 case GIMPLE_DEBUG:
408 case GIMPLE_COND:
409 return true;
410
411 case GIMPLE_ASSIGN:
412 return if_convertible_gimple_assign_stmt_p (loop, bb, stmt);
413
414 default:
415 /* Don't know what to do with 'em so don't do anything. */
416 if (dump_file && (dump_flags & TDF_DETAILS))
417 {
418 fprintf (dump_file, "don't know what to do\n");
419 print_gimple_stmt (dump_file, stmt, 0, TDF_SLIM);
420 }
421 return false;
422 break;
423 }
424
425 return true;
426 }
427
428 /* Return true when BB is if-convertible. This routine does not check
429 basic block's statements and phis.
430
431 A basic block is not if-convertible if:
432 - it is non-empty and it is after the exit block (in BFS order),
433 - it is after the exit block but before the latch,
434 - its edges are not normal.
435
436 EXIT_BB is the basic block containing the exit of the LOOP. BB is
437 inside LOOP. */
438
439 static bool
440 if_convertible_bb_p (struct loop *loop, basic_block bb, basic_block exit_bb)
441 {
442 edge e;
443 edge_iterator ei;
444
445 if (dump_file && (dump_flags & TDF_DETAILS))
446 fprintf (dump_file, "----------[%d]-------------\n", bb->index);
447
448 if (EDGE_COUNT (bb->preds) > 2
449 || EDGE_COUNT (bb->succs) > 2)
450 return false;
451
452 if (exit_bb)
453 {
454 if (bb != loop->latch)
455 {
456 if (dump_file && (dump_flags & TDF_DETAILS))
457 fprintf (dump_file, "basic block after exit bb but before latch\n");
458 return false;
459 }
460 else if (!empty_block_p (bb))
461 {
462 if (dump_file && (dump_flags & TDF_DETAILS))
463 fprintf (dump_file, "non empty basic block after exit bb\n");
464 return false;
465 }
466 else if (bb == loop->latch
467 && bb != exit_bb
468 && !dominated_by_p (CDI_DOMINATORS, bb, exit_bb))
469 {
470 if (dump_file && (dump_flags & TDF_DETAILS))
471 fprintf (dump_file, "latch is not dominated by exit_block\n");
472 return false;
473 }
474 }
475
476 /* Be less adventurous and handle only normal edges. */
477 FOR_EACH_EDGE (e, ei, bb->succs)
478 if (e->flags &
479 (EDGE_ABNORMAL_CALL | EDGE_EH | EDGE_ABNORMAL | EDGE_IRREDUCIBLE_LOOP))
480 {
481 if (dump_file && (dump_flags & TDF_DETAILS))
482 fprintf (dump_file, "Difficult to handle edges\n");
483 return false;
484 }
485
486 return true;
487 }
488
489 /* Return true when all predecessor blocks of BB are visited. The
490 VISITED bitmap keeps track of the visited blocks. */
491
492 static bool
493 pred_blocks_visited_p (basic_block bb, bitmap *visited)
494 {
495 edge e;
496 edge_iterator ei;
497 FOR_EACH_EDGE (e, ei, bb->preds)
498 if (!bitmap_bit_p (*visited, e->src->index))
499 return false;
500
501 return true;
502 }
503
504 /* Get body of a LOOP in suitable order for if-conversion. It is
505 caller's responsibility to deallocate basic block list.
506 If-conversion suitable order is, breadth first sort (BFS) order
507 with an additional constraint: select a block only if all its
508 predecessors are already selected. */
509
510 static basic_block *
511 get_loop_body_in_if_conv_order (const struct loop *loop)
512 {
513 basic_block *blocks, *blocks_in_bfs_order;
514 basic_block bb;
515 bitmap visited;
516 unsigned int index = 0;
517 unsigned int visited_count = 0;
518
519 gcc_assert (loop->num_nodes);
520 gcc_assert (loop->latch != EXIT_BLOCK_PTR);
521
522 blocks = XCNEWVEC (basic_block, loop->num_nodes);
523 visited = BITMAP_ALLOC (NULL);
524
525 blocks_in_bfs_order = get_loop_body_in_bfs_order (loop);
526
527 index = 0;
528 while (index < loop->num_nodes)
529 {
530 bb = blocks_in_bfs_order [index];
531
532 if (bb->flags & BB_IRREDUCIBLE_LOOP)
533 {
534 free (blocks_in_bfs_order);
535 BITMAP_FREE (visited);
536 free (blocks);
537 return NULL;
538 }
539
540 if (!bitmap_bit_p (visited, bb->index))
541 {
542 if (pred_blocks_visited_p (bb, &visited)
543 || bb == loop->header)
544 {
545 /* This block is now visited. */
546 bitmap_set_bit (visited, bb->index);
547 blocks[visited_count++] = bb;
548 }
549 }
550
551 index++;
552
553 if (index == loop->num_nodes
554 && visited_count != loop->num_nodes)
555 /* Not done yet. */
556 index = 0;
557 }
558 free (blocks_in_bfs_order);
559 BITMAP_FREE (visited);
560 return blocks;
561 }
562
563 /* Return true when LOOP is if-convertible.
564 LOOP is if-convertible if:
565 - it is innermost,
566 - it has two or more basic blocks,
567 - it has only one exit,
568 - loop header is not the exit edge,
569 - if its basic blocks and phi nodes are if convertible. */
570
571 static bool
572 if_convertible_loop_p (struct loop *loop)
573 {
574 basic_block bb;
575 gimple_stmt_iterator itr;
576 unsigned int i;
577 edge e;
578 edge_iterator ei;
579 basic_block exit_bb = NULL;
580
581 /* Handle only innermost loop. */
582 if (!loop || loop->inner)
583 {
584 if (dump_file && (dump_flags & TDF_DETAILS))
585 fprintf (dump_file, "not innermost loop\n");
586 return false;
587 }
588
589 /* If only one block, no need for if-conversion. */
590 if (loop->num_nodes <= 2)
591 {
592 if (dump_file && (dump_flags & TDF_DETAILS))
593 fprintf (dump_file, "less than 2 basic blocks\n");
594 return false;
595 }
596
597 /* More than one loop exit is too much to handle. */
598 if (!single_exit (loop))
599 {
600 if (dump_file && (dump_flags & TDF_DETAILS))
601 fprintf (dump_file, "multiple exits\n");
602 return false;
603 }
604
605 /* ??? Check target's vector conditional operation support for vectorizer. */
606
607 /* If one of the loop header's edge is exit edge then do not apply
608 if-conversion. */
609 FOR_EACH_EDGE (e, ei, loop->header->succs)
610 {
611 if (loop_exit_edge_p (loop, e))
612 return false;
613 }
614
615 /* Don't if-convert the loop when the data dependences cannot be
616 computed: the loop won't be vectorized in that case. */
617 {
618 VEC (data_reference_p, heap) *refs = VEC_alloc (data_reference_p, heap, 5);
619 VEC (ddr_p, heap) *ddrs = VEC_alloc (ddr_p, heap, 25);
620 bool res = compute_data_dependences_for_loop (loop, true, &refs, &ddrs);
621
622 free_data_refs (refs);
623 free_dependence_relations (ddrs);
624
625 if (!res)
626 return false;
627 }
628
629 calculate_dominance_info (CDI_DOMINATORS);
630
631 /* Allow statements that can be handled during if-conversion. */
632 ifc_bbs = get_loop_body_in_if_conv_order (loop);
633 if (!ifc_bbs)
634 {
635 if (dump_file && (dump_flags & TDF_DETAILS))
636 fprintf (dump_file, "Irreducible loop\n");
637 return false;
638 }
639
640 for (i = 0; i < loop->num_nodes; i++)
641 {
642 bb = ifc_bbs[i];
643
644 if (!if_convertible_bb_p (loop, bb, exit_bb))
645 return false;
646
647 for (itr = gsi_start_bb (bb); !gsi_end_p (itr); gsi_next (&itr))
648 if (!if_convertible_stmt_p (loop, bb, gsi_stmt (itr)))
649 return false;
650
651 itr = gsi_start_phis (bb);
652
653 if (!gsi_end_p (itr))
654 FOR_EACH_EDGE (e, ei, bb->preds)
655 e->aux = NULL;
656
657 for (; !gsi_end_p (itr); gsi_next (&itr))
658 if (!if_convertible_phi_p (loop, bb, gsi_stmt (itr)))
659 return false;
660
661 if (bb_with_exit_edge_p (loop, bb))
662 exit_bb = bb;
663 }
664
665 if (dump_file)
666 fprintf (dump_file, "Applying if-conversion\n");
667
668 return true;
669 }
670
671 /* During if-conversion, the bb->aux field is used to hold a predicate
672 list. This function cleans for all the basic blocks in the given
673 LOOP their predicate list. It also cleans up the e->aux field of
674 all the successor edges: e->aux is used to hold the true and false
675 conditions for conditional expressions. */
676
677 static void
678 clean_predicate_lists (struct loop *loop)
679 {
680 basic_block *bb;
681 unsigned int i;
682 edge e;
683 edge_iterator ei;
684
685 bb = get_loop_body (loop);
686 for (i = 0; i < loop->num_nodes; i++)
687 {
688 bb[i]->aux = NULL;
689 FOR_EACH_EDGE (e, ei, bb[i]->succs)
690 e->aux = NULL;
691 }
692 free (bb);
693 }
694
695 /* Basic block BB has two predecessors. Using predecessor's bb->aux
696 field, set appropriate condition COND for the PHI node replacement.
697 Return true block whose phi arguments are selected when cond is
698 true. LOOP is the loop containing the if-converted region, GSI is
699 the place to insert the code for the if-conversion. */
700
701 static basic_block
702 find_phi_replacement_condition (struct loop *loop,
703 basic_block bb, tree *cond,
704 gimple_stmt_iterator *gsi)
705 {
706 edge first_edge, second_edge;
707 tree tmp_cond;
708
709 gcc_assert (EDGE_COUNT (bb->preds) == 2);
710 first_edge = EDGE_PRED (bb, 0);
711 second_edge = EDGE_PRED (bb, 1);
712
713 /* Use condition based on following criteria:
714 1)
715 S1: x = !c ? a : b;
716
717 S2: x = c ? b : a;
718
719 S2 is preferred over S1. Make 'b' first_bb and use its condition.
720
721 2) Do not make loop header first_bb.
722
723 3)
724 S1: x = !(c == d)? a : b;
725
726 S21: t1 = c == d;
727 S22: x = t1 ? b : a;
728
729 S3: x = (c == d) ? b : a;
730
731 S3 is preferred over S1 and S2*, Make 'b' first_bb and use
732 its condition.
733
734 4) If pred B is dominated by pred A then use pred B's condition.
735 See PR23115. */
736
737 /* Select condition that is not TRUTH_NOT_EXPR. */
738 tmp_cond = (tree) (first_edge->src)->aux;
739 gcc_assert (tmp_cond);
740
741 if (TREE_CODE (tmp_cond) == TRUTH_NOT_EXPR)
742 {
743 edge tmp_edge;
744
745 tmp_edge = first_edge;
746 first_edge = second_edge;
747 second_edge = tmp_edge;
748 }
749
750 /* Check if FIRST_BB is loop header or not and make sure that
751 FIRST_BB does not dominate SECOND_BB. */
752 if (first_edge->src == loop->header
753 || dominated_by_p (CDI_DOMINATORS,
754 second_edge->src, first_edge->src))
755 {
756 *cond = (tree) (second_edge->src)->aux;
757
758 /* If there is a condition on an incoming edge, add it to the
759 incoming bb predicate. */
760 if (second_edge->aux)
761 *cond = build2 (TRUTH_AND_EXPR, boolean_type_node,
762 *cond, (tree) second_edge->aux);
763
764 if (TREE_CODE (*cond) == TRUTH_NOT_EXPR)
765 *cond = invert_truthvalue (*cond);
766 else
767 /* Select non loop header bb. */
768 first_edge = second_edge;
769 }
770 else
771 {
772 *cond = (tree) (first_edge->src)->aux;
773
774 /* If there is a condition on an incoming edge, add it to the
775 incoming bb predicate. */
776 if (first_edge->aux)
777 *cond = build2 (TRUTH_AND_EXPR, boolean_type_node,
778 *cond, (tree) first_edge->aux);
779 }
780
781 /* Gimplify the condition: the vectorizer prefers to have gimple
782 values as conditions. Various targets use different means to
783 communicate conditions in vector compare operations. Using a
784 gimple value allows the compiler to emit vector compare and
785 select RTL without exposing compare's result. */
786 *cond = force_gimple_operand_gsi (gsi, unshare_expr (*cond),
787 false, NULL_TREE,
788 true, GSI_SAME_STMT);
789 if (!is_gimple_reg (*cond) && !is_gimple_condexpr (*cond))
790 {
791 gimple new_stmt;
792
793 new_stmt = ifc_temp_var (TREE_TYPE (*cond), unshare_expr (*cond));
794 gsi_insert_before (gsi, new_stmt, GSI_SAME_STMT);
795 *cond = gimple_assign_lhs (new_stmt);
796 }
797
798 gcc_assert (*cond);
799
800 return first_edge->src;
801 }
802
803 /* Replace PHI node with conditional modify expr using COND. This
804 routine does not handle PHI nodes with more than two arguments.
805
806 For example,
807 S1: A = PHI <x1(1), x2(5)
808 is converted into,
809 S2: A = cond ? x1 : x2;
810
811 The generated code is inserted at GSI that points to the top of
812 basic block's statement list. When COND is true, phi arg from
813 TRUE_BB is selected. */
814
815 static void
816 replace_phi_with_cond_gimple_assign_stmt (gimple phi, tree cond,
817 basic_block true_bb,
818 gimple_stmt_iterator *gsi)
819 {
820 gimple new_stmt;
821 basic_block bb;
822 tree rhs;
823 tree arg_0, arg_1;
824
825 gcc_assert (gimple_code (phi) == GIMPLE_PHI
826 && gimple_phi_num_args (phi) == 2);
827
828 bb = gimple_bb (phi);
829
830 /* Use condition that is not TRUTH_NOT_EXPR in conditional modify expr. */
831 if (EDGE_PRED (bb, 1)->src == true_bb)
832 {
833 arg_0 = gimple_phi_arg_def (phi, 1);
834 arg_1 = gimple_phi_arg_def (phi, 0);
835 }
836 else
837 {
838 arg_0 = gimple_phi_arg_def (phi, 0);
839 arg_1 = gimple_phi_arg_def (phi, 1);
840 }
841
842 /* Build new RHS using selected condition and arguments. */
843 rhs = build3 (COND_EXPR, TREE_TYPE (PHI_RESULT (phi)),
844 unshare_expr (cond), unshare_expr (arg_0),
845 unshare_expr (arg_1));
846
847 new_stmt = gimple_build_assign (unshare_expr (PHI_RESULT (phi)), rhs);
848 SSA_NAME_DEF_STMT (gimple_phi_result (phi)) = new_stmt;
849 gsi_insert_before (gsi, new_stmt, GSI_SAME_STMT);
850 update_stmt (new_stmt);
851
852 if (dump_file && (dump_flags & TDF_DETAILS))
853 {
854 fprintf (dump_file, "new phi replacement stmt\n");
855 print_gimple_stmt (dump_file, new_stmt, 0, TDF_SLIM);
856 }
857 }
858
859 /* Process phi nodes for the given LOOP. Replace phi nodes with
860 conditional modify expressions. */
861
862 static void
863 process_phi_nodes (struct loop *loop)
864 {
865 basic_block bb;
866 unsigned int orig_loop_num_nodes = loop->num_nodes;
867 unsigned int i;
868
869 for (i = 1; i < orig_loop_num_nodes; i++)
870 {
871 gimple phi;
872 tree cond = NULL_TREE;
873 gimple_stmt_iterator gsi, phi_gsi;
874 basic_block true_bb = NULL;
875 bb = ifc_bbs[i];
876
877 if (bb == loop->header)
878 continue;
879
880 phi_gsi = gsi_start_phis (bb);
881 gsi = gsi_after_labels (bb);
882
883 /* BB has two predecessors. Using predecessor's aux field, set
884 appropriate condition for the PHI node replacement. */
885 if (!gsi_end_p (phi_gsi))
886 true_bb = find_phi_replacement_condition (loop, bb, &cond, &gsi);
887
888 while (!gsi_end_p (phi_gsi))
889 {
890 phi = gsi_stmt (phi_gsi);
891 replace_phi_with_cond_gimple_assign_stmt (phi, cond, true_bb, &gsi);
892 release_phi_node (phi);
893 gsi_next (&phi_gsi);
894 }
895 set_phi_nodes (bb, NULL);
896 }
897 }
898
899 /* Remove all GIMPLE_CONDs and GIMPLE_LABELs of all the basic blocks
900 other than the exit and latch of the LOOP. */
901
902 static void
903 remove_conditions_and_labels (loop_p loop)
904 {
905 gimple_stmt_iterator gsi;
906 unsigned int i;
907
908 for (i = 0; i < loop->num_nodes; i++)
909 {
910 basic_block bb = ifc_bbs [i];
911
912 if (bb_with_exit_edge_p (loop, bb)
913 || bb == loop->latch)
914 continue;
915
916 for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); )
917 if (gimple_code (gsi_stmt (gsi)) == GIMPLE_COND
918 || gimple_code (gsi_stmt (gsi)) == GIMPLE_LABEL)
919 gsi_remove (&gsi, true);
920 else
921 gsi_next (&gsi);
922 }
923 }
924
925 /* Combine all the basic blocks from LOOP into one or two super basic
926 blocks. Replace PHI nodes with conditional modify expressions. */
927
928 static void
929 combine_blocks (struct loop *loop)
930 {
931 basic_block bb, exit_bb, merge_target_bb;
932 unsigned int orig_loop_num_nodes = loop->num_nodes;
933 unsigned int i;
934 edge e;
935 edge_iterator ei;
936
937 remove_conditions_and_labels (loop);
938
939 /* Process phi nodes to prepare blocks for merge. */
940 process_phi_nodes (loop);
941
942 /* Merge basic blocks: first remove all the edges in the loop,
943 except for those from the exit block. */
944 exit_bb = NULL;
945 for (i = 0; i < orig_loop_num_nodes; i++)
946 {
947 bb = ifc_bbs[i];
948 if (bb_with_exit_edge_p (loop, bb))
949 {
950 exit_bb = bb;
951 break;
952 }
953 }
954 gcc_assert (exit_bb != loop->latch);
955
956 for (i = 1; i < orig_loop_num_nodes; i++)
957 {
958 bb = ifc_bbs[i];
959
960 for (ei = ei_start (bb->preds); (e = ei_safe_edge (ei));)
961 {
962 if (e->src == exit_bb)
963 ei_next (&ei);
964 else
965 remove_edge (e);
966 }
967 }
968
969 if (exit_bb != NULL)
970 {
971 if (exit_bb != loop->header)
972 {
973 /* Connect this node to loop header. */
974 make_edge (loop->header, exit_bb, EDGE_FALLTHRU);
975 set_immediate_dominator (CDI_DOMINATORS, exit_bb, loop->header);
976 }
977
978 /* Redirect non-exit edges to loop->latch. */
979 FOR_EACH_EDGE (e, ei, exit_bb->succs)
980 {
981 if (!loop_exit_edge_p (loop, e))
982 redirect_edge_and_branch (e, loop->latch);
983 }
984 set_immediate_dominator (CDI_DOMINATORS, loop->latch, exit_bb);
985 }
986 else
987 {
988 /* If the loop does not have an exit, reconnect header and latch. */
989 make_edge (loop->header, loop->latch, EDGE_FALLTHRU);
990 set_immediate_dominator (CDI_DOMINATORS, loop->latch, loop->header);
991 }
992
993 merge_target_bb = loop->header;
994 for (i = 1; i < orig_loop_num_nodes; i++)
995 {
996 gimple_stmt_iterator gsi;
997 gimple_stmt_iterator last;
998
999 bb = ifc_bbs[i];
1000
1001 if (bb == exit_bb || bb == loop->latch)
1002 continue;
1003
1004 /* Make stmts member of loop->header. */
1005 for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
1006 gimple_set_bb (gsi_stmt (gsi), merge_target_bb);
1007
1008 /* Update stmt list. */
1009 last = gsi_last_bb (merge_target_bb);
1010 gsi_insert_seq_after (&last, bb_seq (bb), GSI_NEW_STMT);
1011 set_bb_seq (bb, NULL);
1012
1013 delete_basic_block (bb);
1014 }
1015
1016 /* If possible, merge loop header to the block with the exit edge.
1017 This reduces the number of basic blocks to two, to please the
1018 vectorizer that handles only loops with two nodes.
1019
1020 FIXME: Call cleanup_tree_cfg. */
1021 if (exit_bb
1022 && exit_bb != loop->header
1023 && can_merge_blocks_p (loop->header, exit_bb))
1024 merge_blocks (loop->header, exit_bb);
1025 }
1026
1027 /* If-convert LOOP when it is legal. For the moment this pass has no
1028 profitability analysis. */
1029
1030 static void
1031 tree_if_conversion (struct loop *loop)
1032 {
1033 gimple_stmt_iterator itr;
1034 unsigned int i;
1035
1036 ifc_bbs = NULL;
1037
1038 /* If-conversion is not appropriate for all loops. First, check if
1039 the loop is if-convertible. */
1040 if (!if_convertible_loop_p (loop))
1041 {
1042 if (dump_file && (dump_flags & TDF_DETAILS))
1043 fprintf (dump_file, "-------------------------\n");
1044 if (ifc_bbs)
1045 {
1046 free (ifc_bbs);
1047 ifc_bbs = NULL;
1048 }
1049 return;
1050 }
1051
1052 for (i = 0; i < loop->num_nodes; i++)
1053 {
1054 basic_block bb = ifc_bbs [i];
1055 tree cond = (tree) bb->aux;
1056
1057 /* Predicate basic block(s) with the condition expressions
1058 leading to their execution. */
1059 for (itr = gsi_start_bb (bb); !gsi_end_p (itr); /* empty */)
1060 {
1061 gimple t = gsi_stmt (itr);
1062 cond = tree_if_convert_stmt (loop, t, cond, &itr);
1063 if (!gsi_end_p (itr))
1064 gsi_next (&itr);
1065 }
1066
1067 /* If current bb has only one successor, then consider it as an
1068 unconditional goto. */
1069 if (single_succ_p (bb))
1070 {
1071 basic_block bb_n = single_succ (bb);
1072
1073 /* The successor bb inherits the predicate of its
1074 predecessor. If there is no predicate in the predecessor
1075 bb, then consider the successor bb as always executed. */
1076 if (cond == NULL_TREE)
1077 cond = boolean_true_node;
1078
1079 add_to_predicate_list (bb_n, cond);
1080 }
1081 }
1082
1083 /* Now, all statements are if-converted and basic blocks are
1084 annotated appropriately. Combine all the basic blocks into one
1085 huge basic block. */
1086 combine_blocks (loop);
1087
1088 /* clean up */
1089 clean_predicate_lists (loop);
1090 free (ifc_bbs);
1091 ifc_bbs = NULL;
1092 }
1093
1094 /* Tree if-conversion pass management. */
1095
1096 static unsigned int
1097 main_tree_if_conversion (void)
1098 {
1099 loop_iterator li;
1100 struct loop *loop;
1101
1102 if (number_of_loops () <= 1)
1103 return 0;
1104
1105 FOR_EACH_LOOP (li, loop, 0)
1106 tree_if_conversion (loop);
1107
1108 return 0;
1109 }
1110
1111 static bool
1112 gate_tree_if_conversion (void)
1113 {
1114 return flag_tree_vectorize != 0;
1115 }
1116
1117 struct gimple_opt_pass pass_if_conversion =
1118 {
1119 {
1120 GIMPLE_PASS,
1121 "ifcvt", /* name */
1122 gate_tree_if_conversion, /* gate */
1123 main_tree_if_conversion, /* execute */
1124 NULL, /* sub */
1125 NULL, /* next */
1126 0, /* static_pass_number */
1127 TV_NONE, /* tv_id */
1128 PROP_cfg | PROP_ssa, /* properties_required */
1129 0, /* properties_provided */
1130 0, /* properties_destroyed */
1131 0, /* todo_flags_start */
1132 TODO_dump_func | TODO_verify_stmts | TODO_verify_flow
1133 /* todo_flags_finish */
1134 }
1135 };