Eliminate FOR_EACH_BB macro.
[gcc.git] / gcc / tree-if-conv.c
1 /* If-conversion for vectorizer.
2 Copyright (C) 2004-2013 Free Software Foundation, Inc.
3 Contributed by Devang Patel <dpatel@apple.com>
4
5 This file is part of GCC.
6
7 GCC is free software; you can redistribute it and/or modify it under
8 the terms of the GNU General Public License as published by the Free
9 Software Foundation; either version 3, or (at your option) any later
10 version.
11
12 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
13 WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
15 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 /* This pass implements a tree level if-conversion of loops. Its
22 initial goal is to help the vectorizer to vectorize loops with
23 conditions.
24
25 A short description of if-conversion:
26
27 o Decide if a loop is if-convertible or not.
28 o Walk all loop basic blocks in breadth first order (BFS order).
29 o Remove conditional statements (at the end of basic block)
30 and propagate condition into destination basic blocks'
31 predicate list.
32 o Replace modify expression with conditional modify expression
33 using current basic block's condition.
34 o Merge all basic blocks
35 o Replace phi nodes with conditional modify expr
36 o Merge all basic blocks into header
37
38 Sample transformation:
39
40 INPUT
41 -----
42
43 # i_23 = PHI <0(0), i_18(10)>;
44 <L0>:;
45 j_15 = A[i_23];
46 if (j_15 > 41) goto <L1>; else goto <L17>;
47
48 <L17>:;
49 goto <bb 3> (<L3>);
50
51 <L1>:;
52
53 # iftmp.2_4 = PHI <0(8), 42(2)>;
54 <L3>:;
55 A[i_23] = iftmp.2_4;
56 i_18 = i_23 + 1;
57 if (i_18 <= 15) goto <L19>; else goto <L18>;
58
59 <L19>:;
60 goto <bb 1> (<L0>);
61
62 <L18>:;
63
64 OUTPUT
65 ------
66
67 # i_23 = PHI <0(0), i_18(10)>;
68 <L0>:;
69 j_15 = A[i_23];
70
71 <L3>:;
72 iftmp.2_4 = j_15 > 41 ? 42 : 0;
73 A[i_23] = iftmp.2_4;
74 i_18 = i_23 + 1;
75 if (i_18 <= 15) goto <L19>; else goto <L18>;
76
77 <L19>:;
78 goto <bb 1> (<L0>);
79
80 <L18>:;
81 */
82
83 #include "config.h"
84 #include "system.h"
85 #include "coretypes.h"
86 #include "tm.h"
87 #include "tree.h"
88 #include "stor-layout.h"
89 #include "flags.h"
90 #include "basic-block.h"
91 #include "gimple-pretty-print.h"
92 #include "tree-ssa-alias.h"
93 #include "internal-fn.h"
94 #include "gimple-fold.h"
95 #include "gimple-expr.h"
96 #include "is-a.h"
97 #include "gimple.h"
98 #include "gimplify.h"
99 #include "gimple-iterator.h"
100 #include "gimplify-me.h"
101 #include "gimple-ssa.h"
102 #include "tree-cfg.h"
103 #include "tree-phinodes.h"
104 #include "ssa-iterators.h"
105 #include "stringpool.h"
106 #include "tree-ssanames.h"
107 #include "tree-into-ssa.h"
108 #include "tree-ssa.h"
109 #include "cfgloop.h"
110 #include "tree-chrec.h"
111 #include "tree-data-ref.h"
112 #include "tree-scalar-evolution.h"
113 #include "tree-pass.h"
114 #include "dbgcnt.h"
115
116 /* List of basic blocks in if-conversion-suitable order. */
117 static basic_block *ifc_bbs;
118
119 /* Structure used to predicate basic blocks. This is attached to the
120 ->aux field of the BBs in the loop to be if-converted. */
121 typedef struct bb_predicate_s {
122
123 /* The condition under which this basic block is executed. */
124 tree predicate;
125
126 /* PREDICATE is gimplified, and the sequence of statements is
127 recorded here, in order to avoid the duplication of computations
128 that occur in previous conditions. See PR44483. */
129 gimple_seq predicate_gimplified_stmts;
130 } *bb_predicate_p;
131
132 /* Returns true when the basic block BB has a predicate. */
133
134 static inline bool
135 bb_has_predicate (basic_block bb)
136 {
137 return bb->aux != NULL;
138 }
139
140 /* Returns the gimplified predicate for basic block BB. */
141
142 static inline tree
143 bb_predicate (basic_block bb)
144 {
145 return ((bb_predicate_p) bb->aux)->predicate;
146 }
147
148 /* Sets the gimplified predicate COND for basic block BB. */
149
150 static inline void
151 set_bb_predicate (basic_block bb, tree cond)
152 {
153 gcc_assert ((TREE_CODE (cond) == TRUTH_NOT_EXPR
154 && is_gimple_condexpr (TREE_OPERAND (cond, 0)))
155 || is_gimple_condexpr (cond));
156 ((bb_predicate_p) bb->aux)->predicate = cond;
157 }
158
159 /* Returns the sequence of statements of the gimplification of the
160 predicate for basic block BB. */
161
162 static inline gimple_seq
163 bb_predicate_gimplified_stmts (basic_block bb)
164 {
165 return ((bb_predicate_p) bb->aux)->predicate_gimplified_stmts;
166 }
167
168 /* Sets the sequence of statements STMTS of the gimplification of the
169 predicate for basic block BB. */
170
171 static inline void
172 set_bb_predicate_gimplified_stmts (basic_block bb, gimple_seq stmts)
173 {
174 ((bb_predicate_p) bb->aux)->predicate_gimplified_stmts = stmts;
175 }
176
177 /* Adds the sequence of statements STMTS to the sequence of statements
178 of the predicate for basic block BB. */
179
180 static inline void
181 add_bb_predicate_gimplified_stmts (basic_block bb, gimple_seq stmts)
182 {
183 gimple_seq_add_seq
184 (&(((bb_predicate_p) bb->aux)->predicate_gimplified_stmts), stmts);
185 }
186
187 /* Initializes to TRUE the predicate of basic block BB. */
188
189 static inline void
190 init_bb_predicate (basic_block bb)
191 {
192 bb->aux = XNEW (struct bb_predicate_s);
193 set_bb_predicate_gimplified_stmts (bb, NULL);
194 set_bb_predicate (bb, boolean_true_node);
195 }
196
197 /* Free the predicate of basic block BB. */
198
199 static inline void
200 free_bb_predicate (basic_block bb)
201 {
202 gimple_seq stmts;
203
204 if (!bb_has_predicate (bb))
205 return;
206
207 /* Release the SSA_NAMEs created for the gimplification of the
208 predicate. */
209 stmts = bb_predicate_gimplified_stmts (bb);
210 if (stmts)
211 {
212 gimple_stmt_iterator i;
213
214 for (i = gsi_start (stmts); !gsi_end_p (i); gsi_next (&i))
215 free_stmt_operands (cfun, gsi_stmt (i));
216 }
217
218 free (bb->aux);
219 bb->aux = NULL;
220 }
221
222 /* Free the predicate of BB and reinitialize it with the true
223 predicate. */
224
225 static inline void
226 reset_bb_predicate (basic_block bb)
227 {
228 free_bb_predicate (bb);
229 init_bb_predicate (bb);
230 }
231
232 /* Returns a new SSA_NAME of type TYPE that is assigned the value of
233 the expression EXPR. Inserts the statement created for this
234 computation before GSI and leaves the iterator GSI at the same
235 statement. */
236
237 static tree
238 ifc_temp_var (tree type, tree expr, gimple_stmt_iterator *gsi)
239 {
240 tree new_name = make_temp_ssa_name (type, NULL, "_ifc_");
241 gimple stmt = gimple_build_assign (new_name, expr);
242 gsi_insert_before (gsi, stmt, GSI_SAME_STMT);
243 return new_name;
244 }
245
246 /* Return true when COND is a true predicate. */
247
248 static inline bool
249 is_true_predicate (tree cond)
250 {
251 return (cond == NULL_TREE
252 || cond == boolean_true_node
253 || integer_onep (cond));
254 }
255
256 /* Returns true when BB has a predicate that is not trivial: true or
257 NULL_TREE. */
258
259 static inline bool
260 is_predicated (basic_block bb)
261 {
262 return !is_true_predicate (bb_predicate (bb));
263 }
264
265 /* Parses the predicate COND and returns its comparison code and
266 operands OP0 and OP1. */
267
268 static enum tree_code
269 parse_predicate (tree cond, tree *op0, tree *op1)
270 {
271 gimple s;
272
273 if (TREE_CODE (cond) == SSA_NAME
274 && is_gimple_assign (s = SSA_NAME_DEF_STMT (cond)))
275 {
276 if (TREE_CODE_CLASS (gimple_assign_rhs_code (s)) == tcc_comparison)
277 {
278 *op0 = gimple_assign_rhs1 (s);
279 *op1 = gimple_assign_rhs2 (s);
280 return gimple_assign_rhs_code (s);
281 }
282
283 else if (gimple_assign_rhs_code (s) == TRUTH_NOT_EXPR)
284 {
285 tree op = gimple_assign_rhs1 (s);
286 tree type = TREE_TYPE (op);
287 enum tree_code code = parse_predicate (op, op0, op1);
288
289 return code == ERROR_MARK ? ERROR_MARK
290 : invert_tree_comparison (code, HONOR_NANS (TYPE_MODE (type)));
291 }
292
293 return ERROR_MARK;
294 }
295
296 if (TREE_CODE_CLASS (TREE_CODE (cond)) == tcc_comparison)
297 {
298 *op0 = TREE_OPERAND (cond, 0);
299 *op1 = TREE_OPERAND (cond, 1);
300 return TREE_CODE (cond);
301 }
302
303 return ERROR_MARK;
304 }
305
306 /* Returns the fold of predicate C1 OR C2 at location LOC. */
307
308 static tree
309 fold_or_predicates (location_t loc, tree c1, tree c2)
310 {
311 tree op1a, op1b, op2a, op2b;
312 enum tree_code code1 = parse_predicate (c1, &op1a, &op1b);
313 enum tree_code code2 = parse_predicate (c2, &op2a, &op2b);
314
315 if (code1 != ERROR_MARK && code2 != ERROR_MARK)
316 {
317 tree t = maybe_fold_or_comparisons (code1, op1a, op1b,
318 code2, op2a, op2b);
319 if (t)
320 return t;
321 }
322
323 return fold_build2_loc (loc, TRUTH_OR_EXPR, boolean_type_node, c1, c2);
324 }
325
326 /* Returns true if N is either a constant or a SSA_NAME. */
327
328 static bool
329 constant_or_ssa_name (tree n)
330 {
331 switch (TREE_CODE (n))
332 {
333 case SSA_NAME:
334 case INTEGER_CST:
335 case REAL_CST:
336 case COMPLEX_CST:
337 case VECTOR_CST:
338 return true;
339 default:
340 return false;
341 }
342 }
343
344 /* Returns either a COND_EXPR or the folded expression if the folded
345 expression is a MIN_EXPR, a MAX_EXPR, an ABS_EXPR,
346 a constant or a SSA_NAME. */
347
348 static tree
349 fold_build_cond_expr (tree type, tree cond, tree rhs, tree lhs)
350 {
351 tree rhs1, lhs1, cond_expr;
352 cond_expr = fold_ternary (COND_EXPR, type, cond,
353 rhs, lhs);
354
355 if (cond_expr == NULL_TREE)
356 return build3 (COND_EXPR, type, cond, rhs, lhs);
357
358 STRIP_USELESS_TYPE_CONVERSION (cond_expr);
359
360 if (constant_or_ssa_name (cond_expr))
361 return cond_expr;
362
363 if (TREE_CODE (cond_expr) == ABS_EXPR)
364 {
365 rhs1 = TREE_OPERAND (cond_expr, 1);
366 STRIP_USELESS_TYPE_CONVERSION (rhs1);
367 if (constant_or_ssa_name (rhs1))
368 return build1 (ABS_EXPR, type, rhs1);
369 }
370
371 if (TREE_CODE (cond_expr) == MIN_EXPR
372 || TREE_CODE (cond_expr) == MAX_EXPR)
373 {
374 lhs1 = TREE_OPERAND (cond_expr, 0);
375 STRIP_USELESS_TYPE_CONVERSION (lhs1);
376 rhs1 = TREE_OPERAND (cond_expr, 1);
377 STRIP_USELESS_TYPE_CONVERSION (rhs1);
378 if (constant_or_ssa_name (rhs1)
379 && constant_or_ssa_name (lhs1))
380 return build2 (TREE_CODE (cond_expr), type, lhs1, rhs1);
381 }
382 return build3 (COND_EXPR, type, cond, rhs, lhs);
383 }
384
385 /* Add condition NC to the predicate list of basic block BB. */
386
387 static inline void
388 add_to_predicate_list (basic_block bb, tree nc)
389 {
390 tree bc, *tp;
391
392 if (is_true_predicate (nc))
393 return;
394
395 if (!is_predicated (bb))
396 bc = nc;
397 else
398 {
399 bc = bb_predicate (bb);
400 bc = fold_or_predicates (EXPR_LOCATION (bc), nc, bc);
401 if (is_true_predicate (bc))
402 {
403 reset_bb_predicate (bb);
404 return;
405 }
406 }
407
408 /* Allow a TRUTH_NOT_EXPR around the main predicate. */
409 if (TREE_CODE (bc) == TRUTH_NOT_EXPR)
410 tp = &TREE_OPERAND (bc, 0);
411 else
412 tp = &bc;
413 if (!is_gimple_condexpr (*tp))
414 {
415 gimple_seq stmts;
416 *tp = force_gimple_operand_1 (*tp, &stmts, is_gimple_condexpr, NULL_TREE);
417 add_bb_predicate_gimplified_stmts (bb, stmts);
418 }
419 set_bb_predicate (bb, bc);
420 }
421
422 /* Add the condition COND to the previous condition PREV_COND, and add
423 this to the predicate list of the destination of edge E. LOOP is
424 the loop to be if-converted. */
425
426 static void
427 add_to_dst_predicate_list (struct loop *loop, edge e,
428 tree prev_cond, tree cond)
429 {
430 if (!flow_bb_inside_loop_p (loop, e->dest))
431 return;
432
433 if (!is_true_predicate (prev_cond))
434 cond = fold_build2 (TRUTH_AND_EXPR, boolean_type_node,
435 prev_cond, cond);
436
437 add_to_predicate_list (e->dest, cond);
438 }
439
440 /* Return true if one of the successor edges of BB exits LOOP. */
441
442 static bool
443 bb_with_exit_edge_p (struct loop *loop, basic_block bb)
444 {
445 edge e;
446 edge_iterator ei;
447
448 FOR_EACH_EDGE (e, ei, bb->succs)
449 if (loop_exit_edge_p (loop, e))
450 return true;
451
452 return false;
453 }
454
455 /* Return true when PHI is if-convertible. PHI is part of loop LOOP
456 and it belongs to basic block BB.
457
458 PHI is not if-convertible if:
459 - it has more than 2 arguments.
460
461 When the flag_tree_loop_if_convert_stores is not set, PHI is not
462 if-convertible if:
463 - a virtual PHI is immediately used in another PHI node,
464 - there is a virtual PHI in a BB other than the loop->header. */
465
466 static bool
467 if_convertible_phi_p (struct loop *loop, basic_block bb, gimple phi)
468 {
469 if (dump_file && (dump_flags & TDF_DETAILS))
470 {
471 fprintf (dump_file, "-------------------------\n");
472 print_gimple_stmt (dump_file, phi, 0, TDF_SLIM);
473 }
474
475 if (bb != loop->header && gimple_phi_num_args (phi) != 2)
476 {
477 if (dump_file && (dump_flags & TDF_DETAILS))
478 fprintf (dump_file, "More than two phi node args.\n");
479 return false;
480 }
481
482 if (flag_tree_loop_if_convert_stores)
483 return true;
484
485 /* When the flag_tree_loop_if_convert_stores is not set, check
486 that there are no memory writes in the branches of the loop to be
487 if-converted. */
488 if (virtual_operand_p (gimple_phi_result (phi)))
489 {
490 imm_use_iterator imm_iter;
491 use_operand_p use_p;
492
493 if (bb != loop->header)
494 {
495 if (dump_file && (dump_flags & TDF_DETAILS))
496 fprintf (dump_file, "Virtual phi not on loop->header.\n");
497 return false;
498 }
499
500 FOR_EACH_IMM_USE_FAST (use_p, imm_iter, gimple_phi_result (phi))
501 {
502 if (gimple_code (USE_STMT (use_p)) == GIMPLE_PHI)
503 {
504 if (dump_file && (dump_flags & TDF_DETAILS))
505 fprintf (dump_file, "Difficult to handle this virtual phi.\n");
506 return false;
507 }
508 }
509 }
510
511 return true;
512 }
513
514 /* Records the status of a data reference. This struct is attached to
515 each DR->aux field. */
516
517 struct ifc_dr {
518 /* -1 when not initialized, 0 when false, 1 when true. */
519 int written_at_least_once;
520
521 /* -1 when not initialized, 0 when false, 1 when true. */
522 int rw_unconditionally;
523 };
524
525 #define IFC_DR(DR) ((struct ifc_dr *) (DR)->aux)
526 #define DR_WRITTEN_AT_LEAST_ONCE(DR) (IFC_DR (DR)->written_at_least_once)
527 #define DR_RW_UNCONDITIONALLY(DR) (IFC_DR (DR)->rw_unconditionally)
528
529 /* Returns true when the memory references of STMT are read or written
530 unconditionally. In other words, this function returns true when
531 for every data reference A in STMT there exist other accesses to
532 a data reference with the same base with predicates that add up (OR-up) to
533 the true predicate: this ensures that the data reference A is touched
534 (read or written) on every iteration of the if-converted loop. */
535
536 static bool
537 memrefs_read_or_written_unconditionally (gimple stmt,
538 vec<data_reference_p> drs)
539 {
540 int i, j;
541 data_reference_p a, b;
542 tree ca = bb_predicate (gimple_bb (stmt));
543
544 for (i = 0; drs.iterate (i, &a); i++)
545 if (DR_STMT (a) == stmt)
546 {
547 bool found = false;
548 int x = DR_RW_UNCONDITIONALLY (a);
549
550 if (x == 0)
551 return false;
552
553 if (x == 1)
554 continue;
555
556 for (j = 0; drs.iterate (j, &b); j++)
557 {
558 tree ref_base_a = DR_REF (a);
559 tree ref_base_b = DR_REF (b);
560
561 if (DR_STMT (b) == stmt)
562 continue;
563
564 while (TREE_CODE (ref_base_a) == COMPONENT_REF
565 || TREE_CODE (ref_base_a) == IMAGPART_EXPR
566 || TREE_CODE (ref_base_a) == REALPART_EXPR)
567 ref_base_a = TREE_OPERAND (ref_base_a, 0);
568
569 while (TREE_CODE (ref_base_b) == COMPONENT_REF
570 || TREE_CODE (ref_base_b) == IMAGPART_EXPR
571 || TREE_CODE (ref_base_b) == REALPART_EXPR)
572 ref_base_b = TREE_OPERAND (ref_base_b, 0);
573
574 if (!operand_equal_p (ref_base_a, ref_base_b, 0))
575 {
576 tree cb = bb_predicate (gimple_bb (DR_STMT (b)));
577
578 if (DR_RW_UNCONDITIONALLY (b) == 1
579 || is_true_predicate (cb)
580 || is_true_predicate (ca
581 = fold_or_predicates (EXPR_LOCATION (cb), ca, cb)))
582 {
583 DR_RW_UNCONDITIONALLY (a) = 1;
584 DR_RW_UNCONDITIONALLY (b) = 1;
585 found = true;
586 break;
587 }
588 }
589 }
590
591 if (!found)
592 {
593 DR_RW_UNCONDITIONALLY (a) = 0;
594 return false;
595 }
596 }
597
598 return true;
599 }
600
601 /* Returns true when the memory references of STMT are unconditionally
602 written. In other words, this function returns true when for every
603 data reference A written in STMT, there exist other writes to the
604 same data reference with predicates that add up (OR-up) to the true
605 predicate: this ensures that the data reference A is written on
606 every iteration of the if-converted loop. */
607
608 static bool
609 write_memrefs_written_at_least_once (gimple stmt,
610 vec<data_reference_p> drs)
611 {
612 int i, j;
613 data_reference_p a, b;
614 tree ca = bb_predicate (gimple_bb (stmt));
615
616 for (i = 0; drs.iterate (i, &a); i++)
617 if (DR_STMT (a) == stmt
618 && DR_IS_WRITE (a))
619 {
620 bool found = false;
621 int x = DR_WRITTEN_AT_LEAST_ONCE (a);
622
623 if (x == 0)
624 return false;
625
626 if (x == 1)
627 continue;
628
629 for (j = 0; drs.iterate (j, &b); j++)
630 if (DR_STMT (b) != stmt
631 && DR_IS_WRITE (b)
632 && same_data_refs_base_objects (a, b))
633 {
634 tree cb = bb_predicate (gimple_bb (DR_STMT (b)));
635
636 if (DR_WRITTEN_AT_LEAST_ONCE (b) == 1
637 || is_true_predicate (cb)
638 || is_true_predicate (ca = fold_or_predicates (EXPR_LOCATION (cb),
639 ca, cb)))
640 {
641 DR_WRITTEN_AT_LEAST_ONCE (a) = 1;
642 DR_WRITTEN_AT_LEAST_ONCE (b) = 1;
643 found = true;
644 break;
645 }
646 }
647
648 if (!found)
649 {
650 DR_WRITTEN_AT_LEAST_ONCE (a) = 0;
651 return false;
652 }
653 }
654
655 return true;
656 }
657
658 /* Return true when the memory references of STMT won't trap in the
659 if-converted code. There are two things that we have to check for:
660
661 - writes to memory occur to writable memory: if-conversion of
662 memory writes transforms the conditional memory writes into
663 unconditional writes, i.e. "if (cond) A[i] = foo" is transformed
664 into "A[i] = cond ? foo : A[i]", and as the write to memory may not
665 be executed at all in the original code, it may be a readonly
666 memory. To check that A is not const-qualified, we check that
667 there exists at least an unconditional write to A in the current
668 function.
669
670 - reads or writes to memory are valid memory accesses for every
671 iteration. To check that the memory accesses are correctly formed
672 and that we are allowed to read and write in these locations, we
673 check that the memory accesses to be if-converted occur at every
674 iteration unconditionally. */
675
676 static bool
677 ifcvt_memrefs_wont_trap (gimple stmt, vec<data_reference_p> refs)
678 {
679 return write_memrefs_written_at_least_once (stmt, refs)
680 && memrefs_read_or_written_unconditionally (stmt, refs);
681 }
682
683 /* Wrapper around gimple_could_trap_p refined for the needs of the
684 if-conversion. Try to prove that the memory accesses of STMT could
685 not trap in the innermost loop containing STMT. */
686
687 static bool
688 ifcvt_could_trap_p (gimple stmt, vec<data_reference_p> refs)
689 {
690 if (gimple_vuse (stmt)
691 && !gimple_could_trap_p_1 (stmt, false, false)
692 && ifcvt_memrefs_wont_trap (stmt, refs))
693 return false;
694
695 return gimple_could_trap_p (stmt);
696 }
697
698 /* Return true when STMT is if-convertible.
699
700 GIMPLE_ASSIGN statement is not if-convertible if,
701 - it is not movable,
702 - it could trap,
703 - LHS is not var decl. */
704
705 static bool
706 if_convertible_gimple_assign_stmt_p (gimple stmt,
707 vec<data_reference_p> refs)
708 {
709 tree lhs = gimple_assign_lhs (stmt);
710 basic_block bb;
711
712 if (dump_file && (dump_flags & TDF_DETAILS))
713 {
714 fprintf (dump_file, "-------------------------\n");
715 print_gimple_stmt (dump_file, stmt, 0, TDF_SLIM);
716 }
717
718 if (!is_gimple_reg_type (TREE_TYPE (lhs)))
719 return false;
720
721 /* Some of these constrains might be too conservative. */
722 if (stmt_ends_bb_p (stmt)
723 || gimple_has_volatile_ops (stmt)
724 || (TREE_CODE (lhs) == SSA_NAME
725 && SSA_NAME_OCCURS_IN_ABNORMAL_PHI (lhs))
726 || gimple_has_side_effects (stmt))
727 {
728 if (dump_file && (dump_flags & TDF_DETAILS))
729 fprintf (dump_file, "stmt not suitable for ifcvt\n");
730 return false;
731 }
732
733 if (flag_tree_loop_if_convert_stores)
734 {
735 if (ifcvt_could_trap_p (stmt, refs))
736 {
737 if (dump_file && (dump_flags & TDF_DETAILS))
738 fprintf (dump_file, "tree could trap...\n");
739 return false;
740 }
741 return true;
742 }
743
744 if (gimple_assign_rhs_could_trap_p (stmt))
745 {
746 if (dump_file && (dump_flags & TDF_DETAILS))
747 fprintf (dump_file, "tree could trap...\n");
748 return false;
749 }
750
751 bb = gimple_bb (stmt);
752
753 if (TREE_CODE (lhs) != SSA_NAME
754 && bb != bb->loop_father->header
755 && !bb_with_exit_edge_p (bb->loop_father, bb))
756 {
757 if (dump_file && (dump_flags & TDF_DETAILS))
758 {
759 fprintf (dump_file, "LHS is not var\n");
760 print_gimple_stmt (dump_file, stmt, 0, TDF_SLIM);
761 }
762 return false;
763 }
764
765 return true;
766 }
767
768 /* Return true when STMT is if-convertible.
769
770 A statement is if-convertible if:
771 - it is an if-convertible GIMPLE_ASSIGN,
772 - it is a GIMPLE_LABEL or a GIMPLE_COND. */
773
774 static bool
775 if_convertible_stmt_p (gimple stmt, vec<data_reference_p> refs)
776 {
777 switch (gimple_code (stmt))
778 {
779 case GIMPLE_LABEL:
780 case GIMPLE_DEBUG:
781 case GIMPLE_COND:
782 return true;
783
784 case GIMPLE_ASSIGN:
785 return if_convertible_gimple_assign_stmt_p (stmt, refs);
786
787 case GIMPLE_CALL:
788 {
789 tree fndecl = gimple_call_fndecl (stmt);
790 if (fndecl)
791 {
792 int flags = gimple_call_flags (stmt);
793 if ((flags & ECF_CONST)
794 && !(flags & ECF_LOOPING_CONST_OR_PURE)
795 /* We can only vectorize some builtins at the moment,
796 so restrict if-conversion to those. */
797 && DECL_BUILT_IN (fndecl))
798 return true;
799 }
800 return false;
801 }
802
803 default:
804 /* Don't know what to do with 'em so don't do anything. */
805 if (dump_file && (dump_flags & TDF_DETAILS))
806 {
807 fprintf (dump_file, "don't know what to do\n");
808 print_gimple_stmt (dump_file, stmt, 0, TDF_SLIM);
809 }
810 return false;
811 break;
812 }
813
814 return true;
815 }
816
817 /* Return true when BB is if-convertible. This routine does not check
818 basic block's statements and phis.
819
820 A basic block is not if-convertible if:
821 - it is non-empty and it is after the exit block (in BFS order),
822 - it is after the exit block but before the latch,
823 - its edges are not normal.
824
825 EXIT_BB is the basic block containing the exit of the LOOP. BB is
826 inside LOOP. */
827
828 static bool
829 if_convertible_bb_p (struct loop *loop, basic_block bb, basic_block exit_bb)
830 {
831 edge e;
832 edge_iterator ei;
833
834 if (dump_file && (dump_flags & TDF_DETAILS))
835 fprintf (dump_file, "----------[%d]-------------\n", bb->index);
836
837 if (EDGE_COUNT (bb->preds) > 2
838 || EDGE_COUNT (bb->succs) > 2)
839 return false;
840
841 if (exit_bb)
842 {
843 if (bb != loop->latch)
844 {
845 if (dump_file && (dump_flags & TDF_DETAILS))
846 fprintf (dump_file, "basic block after exit bb but before latch\n");
847 return false;
848 }
849 else if (!empty_block_p (bb))
850 {
851 if (dump_file && (dump_flags & TDF_DETAILS))
852 fprintf (dump_file, "non empty basic block after exit bb\n");
853 return false;
854 }
855 else if (bb == loop->latch
856 && bb != exit_bb
857 && !dominated_by_p (CDI_DOMINATORS, bb, exit_bb))
858 {
859 if (dump_file && (dump_flags & TDF_DETAILS))
860 fprintf (dump_file, "latch is not dominated by exit_block\n");
861 return false;
862 }
863 }
864
865 /* Be less adventurous and handle only normal edges. */
866 FOR_EACH_EDGE (e, ei, bb->succs)
867 if (e->flags & (EDGE_EH | EDGE_ABNORMAL | EDGE_IRREDUCIBLE_LOOP))
868 {
869 if (dump_file && (dump_flags & TDF_DETAILS))
870 fprintf (dump_file, "Difficult to handle edges\n");
871 return false;
872 }
873
874 /* At least one incoming edge has to be non-critical as otherwise edge
875 predicates are not equal to basic-block predicates of the edge
876 source. */
877 if (EDGE_COUNT (bb->preds) > 1
878 && bb != loop->header)
879 {
880 bool found = false;
881 FOR_EACH_EDGE (e, ei, bb->preds)
882 if (EDGE_COUNT (e->src->succs) == 1)
883 found = true;
884 if (!found)
885 {
886 if (dump_file && (dump_flags & TDF_DETAILS))
887 fprintf (dump_file, "only critical predecessors\n");
888 return false;
889 }
890 }
891
892 return true;
893 }
894
895 /* Return true when all predecessor blocks of BB are visited. The
896 VISITED bitmap keeps track of the visited blocks. */
897
898 static bool
899 pred_blocks_visited_p (basic_block bb, bitmap *visited)
900 {
901 edge e;
902 edge_iterator ei;
903 FOR_EACH_EDGE (e, ei, bb->preds)
904 if (!bitmap_bit_p (*visited, e->src->index))
905 return false;
906
907 return true;
908 }
909
910 /* Get body of a LOOP in suitable order for if-conversion. It is
911 caller's responsibility to deallocate basic block list.
912 If-conversion suitable order is, breadth first sort (BFS) order
913 with an additional constraint: select a block only if all its
914 predecessors are already selected. */
915
916 static basic_block *
917 get_loop_body_in_if_conv_order (const struct loop *loop)
918 {
919 basic_block *blocks, *blocks_in_bfs_order;
920 basic_block bb;
921 bitmap visited;
922 unsigned int index = 0;
923 unsigned int visited_count = 0;
924
925 gcc_assert (loop->num_nodes);
926 gcc_assert (loop->latch != EXIT_BLOCK_PTR_FOR_FN (cfun));
927
928 blocks = XCNEWVEC (basic_block, loop->num_nodes);
929 visited = BITMAP_ALLOC (NULL);
930
931 blocks_in_bfs_order = get_loop_body_in_bfs_order (loop);
932
933 index = 0;
934 while (index < loop->num_nodes)
935 {
936 bb = blocks_in_bfs_order [index];
937
938 if (bb->flags & BB_IRREDUCIBLE_LOOP)
939 {
940 free (blocks_in_bfs_order);
941 BITMAP_FREE (visited);
942 free (blocks);
943 return NULL;
944 }
945
946 if (!bitmap_bit_p (visited, bb->index))
947 {
948 if (pred_blocks_visited_p (bb, &visited)
949 || bb == loop->header)
950 {
951 /* This block is now visited. */
952 bitmap_set_bit (visited, bb->index);
953 blocks[visited_count++] = bb;
954 }
955 }
956
957 index++;
958
959 if (index == loop->num_nodes
960 && visited_count != loop->num_nodes)
961 /* Not done yet. */
962 index = 0;
963 }
964 free (blocks_in_bfs_order);
965 BITMAP_FREE (visited);
966 return blocks;
967 }
968
969 /* Returns true when the analysis of the predicates for all the basic
970 blocks in LOOP succeeded.
971
972 predicate_bbs first allocates the predicates of the basic blocks.
973 These fields are then initialized with the tree expressions
974 representing the predicates under which a basic block is executed
975 in the LOOP. As the loop->header is executed at each iteration, it
976 has the "true" predicate. Other statements executed under a
977 condition are predicated with that condition, for example
978
979 | if (x)
980 | S1;
981 | else
982 | S2;
983
984 S1 will be predicated with "x", and
985 S2 will be predicated with "!x". */
986
987 static bool
988 predicate_bbs (loop_p loop)
989 {
990 unsigned int i;
991
992 for (i = 0; i < loop->num_nodes; i++)
993 init_bb_predicate (ifc_bbs[i]);
994
995 for (i = 0; i < loop->num_nodes; i++)
996 {
997 basic_block bb = ifc_bbs[i];
998 tree cond;
999 gimple_stmt_iterator itr;
1000
1001 /* The loop latch is always executed and has no extra conditions
1002 to be processed: skip it. */
1003 if (bb == loop->latch)
1004 {
1005 reset_bb_predicate (loop->latch);
1006 continue;
1007 }
1008
1009 cond = bb_predicate (bb);
1010
1011 for (itr = gsi_start_bb (bb); !gsi_end_p (itr); gsi_next (&itr))
1012 {
1013 gimple stmt = gsi_stmt (itr);
1014
1015 switch (gimple_code (stmt))
1016 {
1017 case GIMPLE_LABEL:
1018 case GIMPLE_ASSIGN:
1019 case GIMPLE_CALL:
1020 case GIMPLE_DEBUG:
1021 break;
1022
1023 case GIMPLE_COND:
1024 {
1025 tree c2;
1026 edge true_edge, false_edge;
1027 location_t loc = gimple_location (stmt);
1028 tree c = fold_build2_loc (loc, gimple_cond_code (stmt),
1029 boolean_type_node,
1030 gimple_cond_lhs (stmt),
1031 gimple_cond_rhs (stmt));
1032
1033 /* Add new condition into destination's predicate list. */
1034 extract_true_false_edges_from_block (gimple_bb (stmt),
1035 &true_edge, &false_edge);
1036
1037 /* If C is true, then TRUE_EDGE is taken. */
1038 add_to_dst_predicate_list (loop, true_edge,
1039 unshare_expr (cond),
1040 unshare_expr (c));
1041
1042 /* If C is false, then FALSE_EDGE is taken. */
1043 c2 = build1_loc (loc, TRUTH_NOT_EXPR,
1044 boolean_type_node, unshare_expr (c));
1045 add_to_dst_predicate_list (loop, false_edge,
1046 unshare_expr (cond), c2);
1047
1048 cond = NULL_TREE;
1049 break;
1050 }
1051
1052 default:
1053 /* Not handled yet in if-conversion. */
1054 return false;
1055 }
1056 }
1057
1058 /* If current bb has only one successor, then consider it as an
1059 unconditional goto. */
1060 if (single_succ_p (bb))
1061 {
1062 basic_block bb_n = single_succ (bb);
1063
1064 /* The successor bb inherits the predicate of its
1065 predecessor. If there is no predicate in the predecessor
1066 bb, then consider the successor bb as always executed. */
1067 if (cond == NULL_TREE)
1068 cond = boolean_true_node;
1069
1070 add_to_predicate_list (bb_n, cond);
1071 }
1072 }
1073
1074 /* The loop header is always executed. */
1075 reset_bb_predicate (loop->header);
1076 gcc_assert (bb_predicate_gimplified_stmts (loop->header) == NULL
1077 && bb_predicate_gimplified_stmts (loop->latch) == NULL);
1078
1079 return true;
1080 }
1081
1082 /* Return true when LOOP is if-convertible. This is a helper function
1083 for if_convertible_loop_p. REFS and DDRS are initialized and freed
1084 in if_convertible_loop_p. */
1085
1086 static bool
1087 if_convertible_loop_p_1 (struct loop *loop,
1088 vec<loop_p> *loop_nest,
1089 vec<data_reference_p> *refs,
1090 vec<ddr_p> *ddrs)
1091 {
1092 bool res;
1093 unsigned int i;
1094 basic_block exit_bb = NULL;
1095
1096 /* Don't if-convert the loop when the data dependences cannot be
1097 computed: the loop won't be vectorized in that case. */
1098 res = compute_data_dependences_for_loop (loop, true, loop_nest, refs, ddrs);
1099 if (!res)
1100 return false;
1101
1102 calculate_dominance_info (CDI_DOMINATORS);
1103
1104 /* Allow statements that can be handled during if-conversion. */
1105 ifc_bbs = get_loop_body_in_if_conv_order (loop);
1106 if (!ifc_bbs)
1107 {
1108 if (dump_file && (dump_flags & TDF_DETAILS))
1109 fprintf (dump_file, "Irreducible loop\n");
1110 return false;
1111 }
1112
1113 for (i = 0; i < loop->num_nodes; i++)
1114 {
1115 basic_block bb = ifc_bbs[i];
1116
1117 if (!if_convertible_bb_p (loop, bb, exit_bb))
1118 return false;
1119
1120 if (bb_with_exit_edge_p (loop, bb))
1121 exit_bb = bb;
1122 }
1123
1124 res = predicate_bbs (loop);
1125 if (!res)
1126 return false;
1127
1128 if (flag_tree_loop_if_convert_stores)
1129 {
1130 data_reference_p dr;
1131
1132 for (i = 0; refs->iterate (i, &dr); i++)
1133 {
1134 dr->aux = XNEW (struct ifc_dr);
1135 DR_WRITTEN_AT_LEAST_ONCE (dr) = -1;
1136 DR_RW_UNCONDITIONALLY (dr) = -1;
1137 }
1138 }
1139
1140 for (i = 0; i < loop->num_nodes; i++)
1141 {
1142 basic_block bb = ifc_bbs[i];
1143 gimple_stmt_iterator itr;
1144
1145 for (itr = gsi_start_phis (bb); !gsi_end_p (itr); gsi_next (&itr))
1146 if (!if_convertible_phi_p (loop, bb, gsi_stmt (itr)))
1147 return false;
1148
1149 /* Check the if-convertibility of statements in predicated BBs. */
1150 if (is_predicated (bb))
1151 for (itr = gsi_start_bb (bb); !gsi_end_p (itr); gsi_next (&itr))
1152 if (!if_convertible_stmt_p (gsi_stmt (itr), *refs))
1153 return false;
1154 }
1155
1156 if (dump_file)
1157 fprintf (dump_file, "Applying if-conversion\n");
1158
1159 return true;
1160 }
1161
1162 /* Return true when LOOP is if-convertible.
1163 LOOP is if-convertible if:
1164 - it is innermost,
1165 - it has two or more basic blocks,
1166 - it has only one exit,
1167 - loop header is not the exit edge,
1168 - if its basic blocks and phi nodes are if convertible. */
1169
1170 static bool
1171 if_convertible_loop_p (struct loop *loop)
1172 {
1173 edge e;
1174 edge_iterator ei;
1175 bool res = false;
1176 vec<data_reference_p> refs;
1177 vec<ddr_p> ddrs;
1178
1179 /* Handle only innermost loop. */
1180 if (!loop || loop->inner)
1181 {
1182 if (dump_file && (dump_flags & TDF_DETAILS))
1183 fprintf (dump_file, "not innermost loop\n");
1184 return false;
1185 }
1186
1187 /* If only one block, no need for if-conversion. */
1188 if (loop->num_nodes <= 2)
1189 {
1190 if (dump_file && (dump_flags & TDF_DETAILS))
1191 fprintf (dump_file, "less than 2 basic blocks\n");
1192 return false;
1193 }
1194
1195 /* More than one loop exit is too much to handle. */
1196 if (!single_exit (loop))
1197 {
1198 if (dump_file && (dump_flags & TDF_DETAILS))
1199 fprintf (dump_file, "multiple exits\n");
1200 return false;
1201 }
1202
1203 /* If one of the loop header's edge is an exit edge then do not
1204 apply if-conversion. */
1205 FOR_EACH_EDGE (e, ei, loop->header->succs)
1206 if (loop_exit_edge_p (loop, e))
1207 return false;
1208
1209 refs.create (5);
1210 ddrs.create (25);
1211 stack_vec<loop_p, 3> loop_nest;
1212 res = if_convertible_loop_p_1 (loop, &loop_nest, &refs, &ddrs);
1213
1214 if (flag_tree_loop_if_convert_stores)
1215 {
1216 data_reference_p dr;
1217 unsigned int i;
1218
1219 for (i = 0; refs.iterate (i, &dr); i++)
1220 free (dr->aux);
1221 }
1222
1223 free_data_refs (refs);
1224 free_dependence_relations (ddrs);
1225 return res;
1226 }
1227
1228 /* Basic block BB has two predecessors. Using predecessor's bb
1229 predicate, set an appropriate condition COND for the PHI node
1230 replacement. Return the true block whose phi arguments are
1231 selected when cond is true. LOOP is the loop containing the
1232 if-converted region, GSI is the place to insert the code for the
1233 if-conversion. */
1234
1235 static basic_block
1236 find_phi_replacement_condition (basic_block bb, tree *cond,
1237 gimple_stmt_iterator *gsi)
1238 {
1239 edge first_edge, second_edge;
1240 tree tmp_cond;
1241
1242 gcc_assert (EDGE_COUNT (bb->preds) == 2);
1243 first_edge = EDGE_PRED (bb, 0);
1244 second_edge = EDGE_PRED (bb, 1);
1245
1246 /* Prefer an edge with a not negated predicate.
1247 ??? That's a very weak cost model. */
1248 tmp_cond = bb_predicate (first_edge->src);
1249 gcc_assert (tmp_cond);
1250 if (TREE_CODE (tmp_cond) == TRUTH_NOT_EXPR)
1251 {
1252 edge tmp_edge;
1253
1254 tmp_edge = first_edge;
1255 first_edge = second_edge;
1256 second_edge = tmp_edge;
1257 }
1258
1259 /* Check if the edge we take the condition from is not critical.
1260 We know that at least one non-critical edge exists. */
1261 if (EDGE_COUNT (first_edge->src->succs) > 1)
1262 {
1263 *cond = bb_predicate (second_edge->src);
1264
1265 if (TREE_CODE (*cond) == TRUTH_NOT_EXPR)
1266 *cond = TREE_OPERAND (*cond, 0);
1267 else
1268 /* Select non loop header bb. */
1269 first_edge = second_edge;
1270 }
1271 else
1272 *cond = bb_predicate (first_edge->src);
1273
1274 /* Gimplify the condition to a valid cond-expr conditonal operand. */
1275 *cond = force_gimple_operand_gsi_1 (gsi, unshare_expr (*cond),
1276 is_gimple_condexpr, NULL_TREE,
1277 true, GSI_SAME_STMT);
1278
1279 return first_edge->src;
1280 }
1281
1282 /* Replace a scalar PHI node with a COND_EXPR using COND as condition.
1283 This routine does not handle PHI nodes with more than two
1284 arguments.
1285
1286 For example,
1287 S1: A = PHI <x1(1), x2(5)>
1288 is converted into,
1289 S2: A = cond ? x1 : x2;
1290
1291 The generated code is inserted at GSI that points to the top of
1292 basic block's statement list. When COND is true, phi arg from
1293 TRUE_BB is selected. */
1294
1295 static void
1296 predicate_scalar_phi (gimple phi, tree cond,
1297 basic_block true_bb,
1298 gimple_stmt_iterator *gsi)
1299 {
1300 gimple new_stmt;
1301 basic_block bb;
1302 tree rhs, res, arg, scev;
1303
1304 gcc_assert (gimple_code (phi) == GIMPLE_PHI
1305 && gimple_phi_num_args (phi) == 2);
1306
1307 res = gimple_phi_result (phi);
1308 /* Do not handle virtual phi nodes. */
1309 if (virtual_operand_p (res))
1310 return;
1311
1312 bb = gimple_bb (phi);
1313
1314 if ((arg = degenerate_phi_result (phi))
1315 || ((scev = analyze_scalar_evolution (gimple_bb (phi)->loop_father,
1316 res))
1317 && !chrec_contains_undetermined (scev)
1318 && scev != res
1319 && (arg = gimple_phi_arg_def (phi, 0))))
1320 rhs = arg;
1321 else
1322 {
1323 tree arg_0, arg_1;
1324 /* Use condition that is not TRUTH_NOT_EXPR in conditional modify expr. */
1325 if (EDGE_PRED (bb, 1)->src == true_bb)
1326 {
1327 arg_0 = gimple_phi_arg_def (phi, 1);
1328 arg_1 = gimple_phi_arg_def (phi, 0);
1329 }
1330 else
1331 {
1332 arg_0 = gimple_phi_arg_def (phi, 0);
1333 arg_1 = gimple_phi_arg_def (phi, 1);
1334 }
1335
1336 /* Build new RHS using selected condition and arguments. */
1337 rhs = fold_build_cond_expr (TREE_TYPE (res), unshare_expr (cond),
1338 arg_0, arg_1);
1339 }
1340
1341 new_stmt = gimple_build_assign (res, rhs);
1342 gsi_insert_before (gsi, new_stmt, GSI_SAME_STMT);
1343 update_stmt (new_stmt);
1344
1345 if (dump_file && (dump_flags & TDF_DETAILS))
1346 {
1347 fprintf (dump_file, "new phi replacement stmt\n");
1348 print_gimple_stmt (dump_file, new_stmt, 0, TDF_SLIM);
1349 }
1350 }
1351
1352 /* Replaces in LOOP all the scalar phi nodes other than those in the
1353 LOOP->header block with conditional modify expressions. */
1354
1355 static void
1356 predicate_all_scalar_phis (struct loop *loop)
1357 {
1358 basic_block bb;
1359 unsigned int orig_loop_num_nodes = loop->num_nodes;
1360 unsigned int i;
1361
1362 for (i = 1; i < orig_loop_num_nodes; i++)
1363 {
1364 gimple phi;
1365 tree cond = NULL_TREE;
1366 gimple_stmt_iterator gsi, phi_gsi;
1367 basic_block true_bb = NULL;
1368 bb = ifc_bbs[i];
1369
1370 if (bb == loop->header)
1371 continue;
1372
1373 phi_gsi = gsi_start_phis (bb);
1374 if (gsi_end_p (phi_gsi))
1375 continue;
1376
1377 /* BB has two predecessors. Using predecessor's aux field, set
1378 appropriate condition for the PHI node replacement. */
1379 gsi = gsi_after_labels (bb);
1380 true_bb = find_phi_replacement_condition (bb, &cond, &gsi);
1381
1382 while (!gsi_end_p (phi_gsi))
1383 {
1384 phi = gsi_stmt (phi_gsi);
1385 predicate_scalar_phi (phi, cond, true_bb, &gsi);
1386 release_phi_node (phi);
1387 gsi_next (&phi_gsi);
1388 }
1389
1390 set_phi_nodes (bb, NULL);
1391 }
1392 }
1393
1394 /* Insert in each basic block of LOOP the statements produced by the
1395 gimplification of the predicates. */
1396
1397 static void
1398 insert_gimplified_predicates (loop_p loop)
1399 {
1400 unsigned int i;
1401
1402 for (i = 0; i < loop->num_nodes; i++)
1403 {
1404 basic_block bb = ifc_bbs[i];
1405 gimple_seq stmts;
1406
1407 if (!is_predicated (bb))
1408 {
1409 /* Do not insert statements for a basic block that is not
1410 predicated. Also make sure that the predicate of the
1411 basic block is set to true. */
1412 reset_bb_predicate (bb);
1413 continue;
1414 }
1415
1416 stmts = bb_predicate_gimplified_stmts (bb);
1417 if (stmts)
1418 {
1419 if (flag_tree_loop_if_convert_stores)
1420 {
1421 /* Insert the predicate of the BB just after the label,
1422 as the if-conversion of memory writes will use this
1423 predicate. */
1424 gimple_stmt_iterator gsi = gsi_after_labels (bb);
1425 gsi_insert_seq_before (&gsi, stmts, GSI_SAME_STMT);
1426 }
1427 else
1428 {
1429 /* Insert the predicate of the BB at the end of the BB
1430 as this would reduce the register pressure: the only
1431 use of this predicate will be in successor BBs. */
1432 gimple_stmt_iterator gsi = gsi_last_bb (bb);
1433
1434 if (gsi_end_p (gsi)
1435 || stmt_ends_bb_p (gsi_stmt (gsi)))
1436 gsi_insert_seq_before (&gsi, stmts, GSI_SAME_STMT);
1437 else
1438 gsi_insert_seq_after (&gsi, stmts, GSI_SAME_STMT);
1439 }
1440
1441 /* Once the sequence is code generated, set it to NULL. */
1442 set_bb_predicate_gimplified_stmts (bb, NULL);
1443 }
1444 }
1445 }
1446
1447 /* Predicate each write to memory in LOOP.
1448
1449 This function transforms control flow constructs containing memory
1450 writes of the form:
1451
1452 | for (i = 0; i < N; i++)
1453 | if (cond)
1454 | A[i] = expr;
1455
1456 into the following form that does not contain control flow:
1457
1458 | for (i = 0; i < N; i++)
1459 | A[i] = cond ? expr : A[i];
1460
1461 The original CFG looks like this:
1462
1463 | bb_0
1464 | i = 0
1465 | end_bb_0
1466 |
1467 | bb_1
1468 | if (i < N) goto bb_5 else goto bb_2
1469 | end_bb_1
1470 |
1471 | bb_2
1472 | cond = some_computation;
1473 | if (cond) goto bb_3 else goto bb_4
1474 | end_bb_2
1475 |
1476 | bb_3
1477 | A[i] = expr;
1478 | goto bb_4
1479 | end_bb_3
1480 |
1481 | bb_4
1482 | goto bb_1
1483 | end_bb_4
1484
1485 insert_gimplified_predicates inserts the computation of the COND
1486 expression at the beginning of the destination basic block:
1487
1488 | bb_0
1489 | i = 0
1490 | end_bb_0
1491 |
1492 | bb_1
1493 | if (i < N) goto bb_5 else goto bb_2
1494 | end_bb_1
1495 |
1496 | bb_2
1497 | cond = some_computation;
1498 | if (cond) goto bb_3 else goto bb_4
1499 | end_bb_2
1500 |
1501 | bb_3
1502 | cond = some_computation;
1503 | A[i] = expr;
1504 | goto bb_4
1505 | end_bb_3
1506 |
1507 | bb_4
1508 | goto bb_1
1509 | end_bb_4
1510
1511 predicate_mem_writes is then predicating the memory write as follows:
1512
1513 | bb_0
1514 | i = 0
1515 | end_bb_0
1516 |
1517 | bb_1
1518 | if (i < N) goto bb_5 else goto bb_2
1519 | end_bb_1
1520 |
1521 | bb_2
1522 | if (cond) goto bb_3 else goto bb_4
1523 | end_bb_2
1524 |
1525 | bb_3
1526 | cond = some_computation;
1527 | A[i] = cond ? expr : A[i];
1528 | goto bb_4
1529 | end_bb_3
1530 |
1531 | bb_4
1532 | goto bb_1
1533 | end_bb_4
1534
1535 and finally combine_blocks removes the basic block boundaries making
1536 the loop vectorizable:
1537
1538 | bb_0
1539 | i = 0
1540 | if (i < N) goto bb_5 else goto bb_1
1541 | end_bb_0
1542 |
1543 | bb_1
1544 | cond = some_computation;
1545 | A[i] = cond ? expr : A[i];
1546 | if (i < N) goto bb_5 else goto bb_4
1547 | end_bb_1
1548 |
1549 | bb_4
1550 | goto bb_1
1551 | end_bb_4
1552 */
1553
1554 static void
1555 predicate_mem_writes (loop_p loop)
1556 {
1557 unsigned int i, orig_loop_num_nodes = loop->num_nodes;
1558
1559 for (i = 1; i < orig_loop_num_nodes; i++)
1560 {
1561 gimple_stmt_iterator gsi;
1562 basic_block bb = ifc_bbs[i];
1563 tree cond = bb_predicate (bb);
1564 bool swap;
1565 gimple stmt;
1566
1567 if (is_true_predicate (cond))
1568 continue;
1569
1570 swap = false;
1571 if (TREE_CODE (cond) == TRUTH_NOT_EXPR)
1572 {
1573 swap = true;
1574 cond = TREE_OPERAND (cond, 0);
1575 }
1576
1577 for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
1578 if ((stmt = gsi_stmt (gsi))
1579 && gimple_assign_single_p (stmt)
1580 && gimple_vdef (stmt))
1581 {
1582 tree lhs = gimple_assign_lhs (stmt);
1583 tree rhs = gimple_assign_rhs1 (stmt);
1584 tree type = TREE_TYPE (lhs);
1585
1586 lhs = ifc_temp_var (type, unshare_expr (lhs), &gsi);
1587 rhs = ifc_temp_var (type, unshare_expr (rhs), &gsi);
1588 if (swap)
1589 {
1590 tree tem = lhs;
1591 lhs = rhs;
1592 rhs = tem;
1593 }
1594 cond = force_gimple_operand_gsi_1 (&gsi, unshare_expr (cond),
1595 is_gimple_condexpr, NULL_TREE,
1596 true, GSI_SAME_STMT);
1597 rhs = fold_build_cond_expr (type, unshare_expr (cond), rhs, lhs);
1598 gimple_assign_set_rhs1 (stmt, ifc_temp_var (type, rhs, &gsi));
1599 update_stmt (stmt);
1600 }
1601 }
1602 }
1603
1604 /* Remove all GIMPLE_CONDs and GIMPLE_LABELs of all the basic blocks
1605 other than the exit and latch of the LOOP. Also resets the
1606 GIMPLE_DEBUG information. */
1607
1608 static void
1609 remove_conditions_and_labels (loop_p loop)
1610 {
1611 gimple_stmt_iterator gsi;
1612 unsigned int i;
1613
1614 for (i = 0; i < loop->num_nodes; i++)
1615 {
1616 basic_block bb = ifc_bbs[i];
1617
1618 if (bb_with_exit_edge_p (loop, bb)
1619 || bb == loop->latch)
1620 continue;
1621
1622 for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); )
1623 switch (gimple_code (gsi_stmt (gsi)))
1624 {
1625 case GIMPLE_COND:
1626 case GIMPLE_LABEL:
1627 gsi_remove (&gsi, true);
1628 break;
1629
1630 case GIMPLE_DEBUG:
1631 /* ??? Should there be conditional GIMPLE_DEBUG_BINDs? */
1632 if (gimple_debug_bind_p (gsi_stmt (gsi)))
1633 {
1634 gimple_debug_bind_reset_value (gsi_stmt (gsi));
1635 update_stmt (gsi_stmt (gsi));
1636 }
1637 gsi_next (&gsi);
1638 break;
1639
1640 default:
1641 gsi_next (&gsi);
1642 }
1643 }
1644 }
1645
1646 /* Combine all the basic blocks from LOOP into one or two super basic
1647 blocks. Replace PHI nodes with conditional modify expressions. */
1648
1649 static void
1650 combine_blocks (struct loop *loop)
1651 {
1652 basic_block bb, exit_bb, merge_target_bb;
1653 unsigned int orig_loop_num_nodes = loop->num_nodes;
1654 unsigned int i;
1655 edge e;
1656 edge_iterator ei;
1657
1658 remove_conditions_and_labels (loop);
1659 insert_gimplified_predicates (loop);
1660 predicate_all_scalar_phis (loop);
1661
1662 if (flag_tree_loop_if_convert_stores)
1663 predicate_mem_writes (loop);
1664
1665 /* Merge basic blocks: first remove all the edges in the loop,
1666 except for those from the exit block. */
1667 exit_bb = NULL;
1668 for (i = 0; i < orig_loop_num_nodes; i++)
1669 {
1670 bb = ifc_bbs[i];
1671 free_bb_predicate (bb);
1672 if (bb_with_exit_edge_p (loop, bb))
1673 {
1674 gcc_assert (exit_bb == NULL);
1675 exit_bb = bb;
1676 }
1677 }
1678 gcc_assert (exit_bb != loop->latch);
1679
1680 for (i = 1; i < orig_loop_num_nodes; i++)
1681 {
1682 bb = ifc_bbs[i];
1683
1684 for (ei = ei_start (bb->preds); (e = ei_safe_edge (ei));)
1685 {
1686 if (e->src == exit_bb)
1687 ei_next (&ei);
1688 else
1689 remove_edge (e);
1690 }
1691 }
1692
1693 if (exit_bb != NULL)
1694 {
1695 if (exit_bb != loop->header)
1696 {
1697 /* Connect this node to loop header. */
1698 make_edge (loop->header, exit_bb, EDGE_FALLTHRU);
1699 set_immediate_dominator (CDI_DOMINATORS, exit_bb, loop->header);
1700 }
1701
1702 /* Redirect non-exit edges to loop->latch. */
1703 FOR_EACH_EDGE (e, ei, exit_bb->succs)
1704 {
1705 if (!loop_exit_edge_p (loop, e))
1706 redirect_edge_and_branch (e, loop->latch);
1707 }
1708 set_immediate_dominator (CDI_DOMINATORS, loop->latch, exit_bb);
1709 }
1710 else
1711 {
1712 /* If the loop does not have an exit, reconnect header and latch. */
1713 make_edge (loop->header, loop->latch, EDGE_FALLTHRU);
1714 set_immediate_dominator (CDI_DOMINATORS, loop->latch, loop->header);
1715 }
1716
1717 merge_target_bb = loop->header;
1718 for (i = 1; i < orig_loop_num_nodes; i++)
1719 {
1720 gimple_stmt_iterator gsi;
1721 gimple_stmt_iterator last;
1722
1723 bb = ifc_bbs[i];
1724
1725 if (bb == exit_bb || bb == loop->latch)
1726 continue;
1727
1728 /* Make stmts member of loop->header. */
1729 for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
1730 gimple_set_bb (gsi_stmt (gsi), merge_target_bb);
1731
1732 /* Update stmt list. */
1733 last = gsi_last_bb (merge_target_bb);
1734 gsi_insert_seq_after (&last, bb_seq (bb), GSI_NEW_STMT);
1735 set_bb_seq (bb, NULL);
1736
1737 delete_basic_block (bb);
1738 }
1739
1740 /* If possible, merge loop header to the block with the exit edge.
1741 This reduces the number of basic blocks to two, to please the
1742 vectorizer that handles only loops with two nodes. */
1743 if (exit_bb
1744 && exit_bb != loop->header
1745 && can_merge_blocks_p (loop->header, exit_bb))
1746 merge_blocks (loop->header, exit_bb);
1747
1748 free (ifc_bbs);
1749 ifc_bbs = NULL;
1750 }
1751
1752 /* If-convert LOOP when it is legal. For the moment this pass has no
1753 profitability analysis. Returns true when something changed. */
1754
1755 static bool
1756 tree_if_conversion (struct loop *loop)
1757 {
1758 bool changed = false;
1759 ifc_bbs = NULL;
1760
1761 if (!if_convertible_loop_p (loop)
1762 || !dbg_cnt (if_conversion_tree))
1763 goto cleanup;
1764
1765 /* Now all statements are if-convertible. Combine all the basic
1766 blocks into one huge basic block doing the if-conversion
1767 on-the-fly. */
1768 combine_blocks (loop);
1769
1770 if (flag_tree_loop_if_convert_stores)
1771 mark_virtual_operands_for_renaming (cfun);
1772
1773 changed = true;
1774
1775 cleanup:
1776 if (ifc_bbs)
1777 {
1778 unsigned int i;
1779
1780 for (i = 0; i < loop->num_nodes; i++)
1781 free_bb_predicate (ifc_bbs[i]);
1782
1783 free (ifc_bbs);
1784 ifc_bbs = NULL;
1785 }
1786
1787 return changed;
1788 }
1789
1790 /* Tree if-conversion pass management. */
1791
1792 static unsigned int
1793 main_tree_if_conversion (void)
1794 {
1795 struct loop *loop;
1796 bool changed = false;
1797 unsigned todo = 0;
1798
1799 if (number_of_loops (cfun) <= 1)
1800 return 0;
1801
1802 FOR_EACH_LOOP (loop, 0)
1803 if (flag_tree_loop_if_convert == 1
1804 || flag_tree_loop_if_convert_stores == 1
1805 || flag_tree_loop_vectorize
1806 || loop->force_vect)
1807 changed |= tree_if_conversion (loop);
1808
1809 if (changed)
1810 todo |= TODO_cleanup_cfg;
1811
1812 if (changed && flag_tree_loop_if_convert_stores)
1813 todo |= TODO_update_ssa_only_virtuals;
1814
1815 #ifdef ENABLE_CHECKING
1816 {
1817 basic_block bb;
1818 FOR_EACH_BB_FN (bb, cfun)
1819 gcc_assert (!bb->aux);
1820 }
1821 #endif
1822
1823 return todo;
1824 }
1825
1826 /* Returns true when the if-conversion pass is enabled. */
1827
1828 static bool
1829 gate_tree_if_conversion (void)
1830 {
1831 return (((flag_tree_loop_vectorize || cfun->has_force_vect_loops)
1832 && flag_tree_loop_if_convert != 0)
1833 || flag_tree_loop_if_convert == 1
1834 || flag_tree_loop_if_convert_stores == 1);
1835 }
1836
1837 namespace {
1838
1839 const pass_data pass_data_if_conversion =
1840 {
1841 GIMPLE_PASS, /* type */
1842 "ifcvt", /* name */
1843 OPTGROUP_NONE, /* optinfo_flags */
1844 true, /* has_gate */
1845 true, /* has_execute */
1846 TV_NONE, /* tv_id */
1847 ( PROP_cfg | PROP_ssa ), /* properties_required */
1848 0, /* properties_provided */
1849 0, /* properties_destroyed */
1850 0, /* todo_flags_start */
1851 ( TODO_verify_stmts | TODO_verify_flow
1852 | TODO_verify_ssa ), /* todo_flags_finish */
1853 };
1854
1855 class pass_if_conversion : public gimple_opt_pass
1856 {
1857 public:
1858 pass_if_conversion (gcc::context *ctxt)
1859 : gimple_opt_pass (pass_data_if_conversion, ctxt)
1860 {}
1861
1862 /* opt_pass methods: */
1863 bool gate () { return gate_tree_if_conversion (); }
1864 unsigned int execute () { return main_tree_if_conversion (); }
1865
1866 }; // class pass_if_conversion
1867
1868 } // anon namespace
1869
1870 gimple_opt_pass *
1871 make_pass_if_conversion (gcc::context *ctxt)
1872 {
1873 return new pass_if_conversion (ctxt);
1874 }